Skip to content

Commit 147ec50

Browse files
feat(worker): expose TextEncoder and TextDecoder (#1562)
Co-authored-by: James Watkins-Harvey <james.watkinsharvey@temporal.io>
1 parent d6a4992 commit 147ec50

File tree

7 files changed

+79
-3
lines changed

7 files changed

+79
-3
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import test from 'ava';
2+
import { v4 as uuid4 } from 'uuid';
3+
import { Client } from '@temporalio/client';
4+
import { RUN_INTEGRATION_TESTS, Worker } from './helpers';
5+
import { defaultOptions } from './mock-native-worker';
6+
import { textEncoderDecoder, textEncoderDecoderFromImport } from './workflows';
7+
8+
if (RUN_INTEGRATION_TESTS) {
9+
test('Worker runtime exposes TextEncoder and TextDecoder as globals', async (t) => {
10+
const worker = await Worker.create({ ...defaultOptions, taskQueue: 'test-worker-exposes-textencoderdecoder' });
11+
const client = new Client();
12+
const result = await worker.runUntil(
13+
client.workflow.execute(textEncoderDecoder, {
14+
args: ['a string that will be encoded and decoded'],
15+
taskQueue: 'test-worker-exposes-textencoderdecoder',
16+
workflowId: uuid4(),
17+
workflowExecutionTimeout: '5s',
18+
})
19+
);
20+
t.is(result, 'a string that will be encoded and decoded');
21+
});
22+
23+
test('Worker runtime exposes TextEncoder and TextDecoder as overrided import of util', async (t) => {
24+
const worker = await Worker.create({ ...defaultOptions, taskQueue: 'test-worker-exposes-textencoderdecoder' });
25+
const client = new Client();
26+
const result = await worker.runUntil(
27+
client.workflow.execute(textEncoderDecoderFromImport, {
28+
args: ['a string that will be encoded and decoded'],
29+
taskQueue: 'test-worker-exposes-textencoderdecoder',
30+
workflowId: uuid4(),
31+
workflowExecutionTimeout: '5s',
32+
})
33+
);
34+
t.is(result, 'a string that will be encoded and decoded');
35+
});
36+
}

packages/test/src/workflows/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export * from './stack-tracer';
8585
export * from './success-string';
8686
export * from './swc';
8787
export * from './tasks-and-microtasks';
88+
export * from './text-encoder-decoder';
8889
export * from './throw-async';
8990
export * from './throw-big-int';
9091
export * from './throw-object';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { TextEncoder as TextEncoderFromImport, TextDecoder as TextDecoderFromImport } from 'util';
2+
3+
export async function textEncoderDecoder(text: string): Promise<string> {
4+
// we don't import these - they are exposed as globals
5+
const encoder = new TextEncoder();
6+
const decoder = new TextDecoder();
7+
const encoded = encoder.encode(text);
8+
const decoded = decoder.decode(encoded);
9+
return decoded;
10+
}
11+
12+
export async function textEncoderDecoderFromImport(text: string): Promise<string> {
13+
const encoder = new TextEncoderFromImport();
14+
const decoder = new TextDecoderFromImport();
15+
const encoded = encoder.encode(text);
16+
const decoded = decoder.decode(encoded);
17+
return decoded;
18+
}

packages/worker/src/workflow/bundler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { toMB } from '../utils';
1010

1111
export const defaultWorkflowInterceptorModules = [require.resolve('../workflow-log-interceptor')];
1212

13-
export const allowedBuiltinModules = ['assert', 'url'];
13+
export const allowedBuiltinModules = ['assert', 'url', 'util'];
1414
export const disallowedBuiltinModules = builtinModules.filter((module) => !allowedBuiltinModules.includes(module));
1515
export const disallowedModules = [
1616
...disallowedBuiltinModules,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* eslint-disable import/unambiguous */
2+
// Only expose the TextEncoder and TextDecoder APIs
3+
module.exports = { TextEncoder, TextDecoder };

packages/worker/src/workflow/reusable-vm.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import assert from 'node:assert';
22
import { URL, URLSearchParams } from 'node:url';
3+
import { TextDecoder, TextEncoder } from 'node:util';
34
import { AsyncLocalStorage } from 'node:async_hooks';
45
import vm from 'node:vm';
56
import * as internals from '@temporalio/workflow/lib/worker-interface';
@@ -69,7 +70,15 @@ export class ReusableVMWorkflowCreator implements WorkflowCreator {
6970
},
7071
}
7172
);
72-
const globals = { AsyncLocalStorage, URL, URLSearchParams, assert, __webpack_module_cache__ };
73+
const globals = {
74+
AsyncLocalStorage,
75+
URL,
76+
URLSearchParams,
77+
assert,
78+
__webpack_module_cache__,
79+
TextEncoder,
80+
TextDecoder,
81+
};
7382
this._context = vm.createContext(globals, { microtaskMode: 'afterEvaluate' });
7483
this.injectConsole();
7584
script.runInContext(this.context);

packages/worker/src/workflow/vm.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import assert from 'node:assert';
22
import { URL, URLSearchParams } from 'node:url';
3+
import { TextEncoder, TextDecoder } from 'node:util';
34
import { AsyncLocalStorage } from 'node:async_hooks';
45
import vm from 'node:vm';
56
import { IllegalStateError } from '@temporalio/common';
@@ -77,7 +78,15 @@ export class VMWorkflowCreator implements WorkflowCreator {
7778
if (this.script === undefined) {
7879
throw new IllegalStateError('Isolate context provider was destroyed');
7980
}
80-
const globals = { AsyncLocalStorage, URL, URLSearchParams, assert, __webpack_module_cache__: {} };
81+
const globals = {
82+
AsyncLocalStorage,
83+
URL,
84+
URLSearchParams,
85+
assert,
86+
__webpack_module_cache__: {},
87+
TextEncoder,
88+
TextDecoder,
89+
};
8190
const context = vm.createContext(globals, { microtaskMode: 'afterEvaluate' });
8291
this.script.runInContext(context);
8392
return context;

0 commit comments

Comments
 (0)