Skip to content

Commit

Permalink
feat(worker): expose TextEncoder and TextDecoder (#1562)
Browse files Browse the repository at this point in the history
Co-authored-by: James Watkins-Harvey <[email protected]>
  • Loading branch information
lukeramsden and mjameswh authored Nov 15, 2024
1 parent d6a4992 commit 147ec50
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 3 deletions.
36 changes: 36 additions & 0 deletions packages/test/src/test-worker-exposes-textencoderdecoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import test from 'ava';
import { v4 as uuid4 } from 'uuid';
import { Client } from '@temporalio/client';
import { RUN_INTEGRATION_TESTS, Worker } from './helpers';
import { defaultOptions } from './mock-native-worker';
import { textEncoderDecoder, textEncoderDecoderFromImport } from './workflows';

if (RUN_INTEGRATION_TESTS) {
test('Worker runtime exposes TextEncoder and TextDecoder as globals', async (t) => {
const worker = await Worker.create({ ...defaultOptions, taskQueue: 'test-worker-exposes-textencoderdecoder' });
const client = new Client();
const result = await worker.runUntil(
client.workflow.execute(textEncoderDecoder, {
args: ['a string that will be encoded and decoded'],
taskQueue: 'test-worker-exposes-textencoderdecoder',
workflowId: uuid4(),
workflowExecutionTimeout: '5s',
})
);
t.is(result, 'a string that will be encoded and decoded');
});

test('Worker runtime exposes TextEncoder and TextDecoder as overrided import of util', async (t) => {
const worker = await Worker.create({ ...defaultOptions, taskQueue: 'test-worker-exposes-textencoderdecoder' });
const client = new Client();
const result = await worker.runUntil(
client.workflow.execute(textEncoderDecoderFromImport, {
args: ['a string that will be encoded and decoded'],
taskQueue: 'test-worker-exposes-textencoderdecoder',
workflowId: uuid4(),
workflowExecutionTimeout: '5s',
})
);
t.is(result, 'a string that will be encoded and decoded');
});
}
1 change: 1 addition & 0 deletions packages/test/src/workflows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export * from './stack-tracer';
export * from './success-string';
export * from './swc';
export * from './tasks-and-microtasks';
export * from './text-encoder-decoder';
export * from './throw-async';
export * from './throw-big-int';
export * from './throw-object';
Expand Down
18 changes: 18 additions & 0 deletions packages/test/src/workflows/text-encoder-decoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TextEncoder as TextEncoderFromImport, TextDecoder as TextDecoderFromImport } from 'util';

export async function textEncoderDecoder(text: string): Promise<string> {
// we don't import these - they are exposed as globals
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const encoded = encoder.encode(text);
const decoded = decoder.decode(encoded);
return decoded;
}

export async function textEncoderDecoderFromImport(text: string): Promise<string> {
const encoder = new TextEncoderFromImport();
const decoder = new TextDecoderFromImport();
const encoded = encoder.encode(text);
const decoded = decoder.decode(encoded);
return decoded;
}
2 changes: 1 addition & 1 deletion packages/worker/src/workflow/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { toMB } from '../utils';

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

export const allowedBuiltinModules = ['assert', 'url'];
export const allowedBuiltinModules = ['assert', 'url', 'util'];
export const disallowedBuiltinModules = builtinModules.filter((module) => !allowedBuiltinModules.includes(module));
export const disallowedModules = [
...disallowedBuiltinModules,
Expand Down
3 changes: 3 additions & 0 deletions packages/worker/src/workflow/module-overrides/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable import/unambiguous */
// Only expose the TextEncoder and TextDecoder APIs
module.exports = { TextEncoder, TextDecoder };
11 changes: 10 additions & 1 deletion packages/worker/src/workflow/reusable-vm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'node:assert';
import { URL, URLSearchParams } from 'node:url';
import { TextDecoder, TextEncoder } from 'node:util';
import { AsyncLocalStorage } from 'node:async_hooks';
import vm from 'node:vm';
import * as internals from '@temporalio/workflow/lib/worker-interface';
Expand Down Expand Up @@ -69,7 +70,15 @@ export class ReusableVMWorkflowCreator implements WorkflowCreator {
},
}
);
const globals = { AsyncLocalStorage, URL, URLSearchParams, assert, __webpack_module_cache__ };
const globals = {
AsyncLocalStorage,
URL,
URLSearchParams,
assert,
__webpack_module_cache__,
TextEncoder,
TextDecoder,
};
this._context = vm.createContext(globals, { microtaskMode: 'afterEvaluate' });
this.injectConsole();
script.runInContext(this.context);
Expand Down
11 changes: 10 additions & 1 deletion packages/worker/src/workflow/vm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'node:assert';
import { URL, URLSearchParams } from 'node:url';
import { TextEncoder, TextDecoder } from 'node:util';
import { AsyncLocalStorage } from 'node:async_hooks';
import vm from 'node:vm';
import { IllegalStateError } from '@temporalio/common';
Expand Down Expand Up @@ -77,7 +78,15 @@ export class VMWorkflowCreator implements WorkflowCreator {
if (this.script === undefined) {
throw new IllegalStateError('Isolate context provider was destroyed');
}
const globals = { AsyncLocalStorage, URL, URLSearchParams, assert, __webpack_module_cache__: {} };
const globals = {
AsyncLocalStorage,
URL,
URLSearchParams,
assert,
__webpack_module_cache__: {},
TextEncoder,
TextDecoder,
};
const context = vm.createContext(globals, { microtaskMode: 'afterEvaluate' });
this.script.runInContext(context);
return context;
Expand Down

0 comments on commit 147ec50

Please sign in to comment.