Skip to content

Commit

Permalink
add remaining async_hooks methods
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jan 29, 2025
1 parent 3baf503 commit 270a365
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/node/async_hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,38 @@
//

import { default as async_hooks } from 'node-internal:async_hooks';
import { ERR_METHOD_NOT_IMPLEMENTED } from 'node-internal:internal_errors';

export const { AsyncLocalStorage, AsyncResource } = async_hooks;

// We don't add all the async wrap providers since we don't use them
// and will not expose any APIs that use them.
export const asyncWrapProviders: Record<string, number> = {
NONE: 0,
};

export function createHook(): void {
throw new ERR_METHOD_NOT_IMPLEMENTED('createHook');
}

export function executionAsyncId(): void {
throw new ERR_METHOD_NOT_IMPLEMENTED('executionAsyncId');
}

export function executionAsyncResource(): void {
throw new ERR_METHOD_NOT_IMPLEMENTED('executionAsyncResource');
}

export function triggerAsyncId(): void {
throw new ERR_METHOD_NOT_IMPLEMENTED('triggerAsyncId');
}

export default {
AsyncLocalStorage,
AsyncResource,
asyncWrapProviders,
createHook,
executionAsyncId,
executionAsyncResource,
triggerAsyncId,
};
6 changes: 6 additions & 0 deletions src/workerd/api/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,9 @@ wd_test(
args = ["--experimental"],
data = ["tests/timers-nodejs-test.js"],
)

wd_test(
src = "tests/async_hooks-nodejs-test.wd-test",
args = ["--experimental"],
data = ["tests/async_hooks-nodejs-test.js"],
)
20 changes: 20 additions & 0 deletions src/workerd/api/node/tests/async_hooks-nodejs-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import async_hooks from 'node:async_hooks';
import { throws } from 'node:assert';

export const testErrorMethodNotImplemented = {
async test() {
const methods = [
'createHook',
'executionAsyncId',
'executionAsyncResource',
'triggerAsyncId',
];

for (const method of methods) {
throws(() => async_hooks[method](), {
name: 'Error',
code: 'ERR_METHOD_NOT_IMPLEMENTED',
});
}
},
};
15 changes: 15 additions & 0 deletions src/workerd/api/node/tests/async_hooks-nodejs-test.wd-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Workerd = import "/workerd/workerd.capnp";

const unitTests :Workerd.Config = (
services = [
( name = "nodejs-async_hooks-test",
worker = (
modules = [
(name = "worker", esModule = embed "async_hooks-nodejs-test.js")
],
compatibilityDate = "2025-01-24",
compatibilityFlags = ["nodejs_compat"],
)
),
],
);

0 comments on commit 270a365

Please sign in to comment.