Skip to content

Commit

Permalink
align async_hooks behavior with polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jan 29, 2025
1 parent 36ce1fc commit 25d93b7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
40 changes: 31 additions & 9 deletions src/node/async_hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
//

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

class AsyncHook {
public enable(): this {
return this;
}

public disable(): this {
return this;
}
}

export const { AsyncLocalStorage, AsyncResource } = async_hooks;

Expand All @@ -14,20 +23,33 @@ export const asyncWrapProviders: Record<string, number> = {
NONE: 0,
};

export function createHook(): void {
throw new ERR_METHOD_NOT_IMPLEMENTED('createHook');
export function createHook(): AsyncHook {
// Even though we don't implement this function, we return a default value
// in order to preserve backward compatibility and avoid breaking changes
// with unenv polyfills.
return new AsyncHook();
}

export function executionAsyncId(): void {
throw new ERR_METHOD_NOT_IMPLEMENTED('executionAsyncId');
export function executionAsyncId(): number {
// Even though we don't implement this function, we return a default value
// in order to preserve backward compatibility and avoid breaking changes
// with unenv polyfills.
return 0;
}

export function executionAsyncResource(): void {
throw new ERR_METHOD_NOT_IMPLEMENTED('executionAsyncResource');
export function executionAsyncResource(): Record<string, string> {
// Even though we don't implement this function, we return a default value
// in order to preserve backward compatibility and avoid breaking changes
// with unenv polyfills.
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Object.create(null);
}

export function triggerAsyncId(): void {
throw new ERR_METHOD_NOT_IMPLEMENTED('triggerAsyncId');
export function triggerAsyncId(): number {
// Even though we don't implement this function, we return a default value
// in order to preserve backward compatibility and avoid breaking changes
// with unenv polyfills.
return 0;
}

export default {
Expand Down
19 changes: 5 additions & 14 deletions src/workerd/api/node/tests/async_hooks-nodejs-test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import async_hooks from 'node:async_hooks';
import { throws } from 'node:assert';
import { ok, deepStrictEqual } 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',
});
}
deepStrictEqual(async_hooks.executionAsyncId(), 0);
deepStrictEqual(async_hooks.triggerAsyncId(), 0);
deepStrictEqual(async_hooks.executionAsyncResource(), Object.create(null));
ok(async_hooks.createHook({}));
},
};

0 comments on commit 25d93b7

Please sign in to comment.