From 270a365f63f38cf49de72a5a77497b3109426a0e Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Tue, 28 Jan 2025 13:22:00 -0500 Subject: [PATCH] add remaining async_hooks methods --- src/node/async_hooks.ts | 28 +++++++++++++++++++ src/workerd/api/node/BUILD.bazel | 6 ++++ .../api/node/tests/async_hooks-nodejs-test.js | 20 +++++++++++++ .../tests/async_hooks-nodejs-test.wd-test | 15 ++++++++++ 4 files changed, 69 insertions(+) create mode 100644 src/workerd/api/node/tests/async_hooks-nodejs-test.js create mode 100644 src/workerd/api/node/tests/async_hooks-nodejs-test.wd-test diff --git a/src/node/async_hooks.ts b/src/node/async_hooks.ts index d6888f10d9d..8995b76e129 100644 --- a/src/node/async_hooks.ts +++ b/src/node/async_hooks.ts @@ -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 = { + 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, }; diff --git a/src/workerd/api/node/BUILD.bazel b/src/workerd/api/node/BUILD.bazel index c1349c203ad..a95f5ccadd0 100644 --- a/src/workerd/api/node/BUILD.bazel +++ b/src/workerd/api/node/BUILD.bazel @@ -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"], +) diff --git a/src/workerd/api/node/tests/async_hooks-nodejs-test.js b/src/workerd/api/node/tests/async_hooks-nodejs-test.js new file mode 100644 index 00000000000..b96211ce24a --- /dev/null +++ b/src/workerd/api/node/tests/async_hooks-nodejs-test.js @@ -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', + }); + } + }, +}; diff --git a/src/workerd/api/node/tests/async_hooks-nodejs-test.wd-test b/src/workerd/api/node/tests/async_hooks-nodejs-test.wd-test new file mode 100644 index 00000000000..4c3733f8f55 --- /dev/null +++ b/src/workerd/api/node/tests/async_hooks-nodejs-test.wd-test @@ -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"], + ) + ), + ], +);