From f36a8951a420e34d8189cda5792f5eeaa5ce85b7 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 15 Apr 2024 18:24:42 +0530 Subject: [PATCH] fix(ext/node): add stub for AsyncResource#asyncId() (#23372) Ref https://github.com/denoland/deno/issues/23263 --- ext/node/polyfills/async_hooks.ts | 8 ++++++++ tests/unit_node/async_hooks_test.ts | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/ext/node/polyfills/async_hooks.ts b/ext/node/polyfills/async_hooks.ts index ea0bfb9442..e8960c4dc0 100644 --- a/ext/node/polyfills/async_hooks.ts +++ b/ext/node/polyfills/async_hooks.ts @@ -10,6 +10,7 @@ import { core } from "ext:core/mod.js"; import { op_node_is_promise_rejected } from "ext:core/ops"; import { validateFunction } from "ext:deno_node/internal/validators.mjs"; +import { newAsyncId } from "ext:deno_node/internal/async_hooks.ts"; function assert(cond: boolean) { if (!cond) throw new Error("Assertion failed"); @@ -180,9 +181,16 @@ class AsyncContextFrame { export class AsyncResource { frame: AsyncContextFrame; type: string; + #asyncId: number; + constructor(type: string) { this.type = type; this.frame = AsyncContextFrame.current(); + this.#asyncId = newAsyncId(); + } + + asyncId() { + return this.#asyncId; } runInAsyncScope( diff --git a/tests/unit_node/async_hooks_test.ts b/tests/unit_node/async_hooks_test.ts index 2ed197c2da..8d94180cf4 100644 --- a/tests/unit_node/async_hooks_test.ts +++ b/tests/unit_node/async_hooks_test.ts @@ -125,3 +125,8 @@ Deno.test(async function bind() { assertEquals(await deferred.promise, null); }); + +Deno.test(function asyncResourceStub() { + const resource = new AsyncResource("dbquery"); + assert(typeof resource.asyncId() === "number"); +});