Skip to content

Commit

Permalink
feat(worker): expose AbortController to the worker sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeramsden committed Dec 10, 2024
1 parent 9c0052c commit 88c9450
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/test/src/test-worker-exposes-abortcontroller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 { abortController } from './workflows';


if (RUN_INTEGRATION_TESTS) {
test(`Worker runtime exposes AbortController as a global`, async (t) => {
const worker = await Worker.create({ ...defaultOptions, taskQueue: 'test-worker-exposes-abortcontroller' });
const client = new Client();
const result = await worker.runUntil(
client.workflow.execute(abortController, {
args: [],
taskQueue: 'test-worker-exposes-abortcontroller',
workflowId: uuid4(),
workflowExecutionTimeout: '5s',
})
);
t.is(result, 'abort successful');
});
}
16 changes: 16 additions & 0 deletions packages/test/src/workflows/abort-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export async function abortController(): Promise<string | null> {
let aborted: string | null = null;

const controller = new AbortController();
const { signal } = controller;

const abortEventListener = () => {
aborted = signal.reason;
};

signal.addEventListener("abort", abortEventListener);
controller.abort("abort successful");
signal.removeEventListener("abort", abortEventListener);

return aborted;
}
1 change: 1 addition & 0 deletions packages/test/src/workflows/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './abort-controller';
export * from './activity-failure';
export * from './activity-failures';
export * from './args-and-return';
Expand Down
1 change: 1 addition & 0 deletions packages/worker/src/workflow/reusable-vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class ReusableVMWorkflowCreator implements WorkflowCreator {
__webpack_module_cache__,
TextEncoder,
TextDecoder,
AbortController,
};
this._context = vm.createContext(globals, { microtaskMode: 'afterEvaluate' });
this.injectConsole();
Expand Down
1 change: 1 addition & 0 deletions packages/worker/src/workflow/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class VMWorkflowCreator implements WorkflowCreator {
__webpack_module_cache__: {},
TextEncoder,
TextDecoder,
AbortController,
};
const context = vm.createContext(globals, { microtaskMode: 'afterEvaluate' });
this.script.runInContext(context);
Expand Down

0 comments on commit 88c9450

Please sign in to comment.