2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-02-14 11:38:45 -05:00
|
|
|
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
|
|
|
|
|
2023-06-27 02:18:22 -04:00
|
|
|
// TODO(petamoriken): enable prefer-primordials for node polyfills
|
|
|
|
// deno-lint-ignore-file prefer-primordials
|
|
|
|
|
2024-08-28 22:25:38 -04:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2023-03-08 06:44:54 -05:00
|
|
|
import { validateFunction } from "ext:deno_node/internal/validators.mjs";
|
2024-04-15 08:54:42 -04:00
|
|
|
import { newAsyncId } from "ext:deno_node/internal/async_hooks.ts";
|
2023-04-20 00:24:28 -04:00
|
|
|
|
2024-08-28 22:25:38 -04:00
|
|
|
const {
|
|
|
|
ObjectDefineProperties,
|
|
|
|
ReflectApply,
|
|
|
|
FunctionPrototypeBind,
|
|
|
|
ArrayPrototypeUnshift,
|
|
|
|
ObjectFreeze,
|
|
|
|
} = primordials;
|
2024-08-02 14:16:59 -04:00
|
|
|
|
2024-08-28 22:25:38 -04:00
|
|
|
const {
|
|
|
|
AsyncVariable,
|
|
|
|
getAsyncContext,
|
|
|
|
setAsyncContext,
|
|
|
|
} = core;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
export class AsyncResource {
|
|
|
|
type: string;
|
2024-08-28 22:25:38 -04:00
|
|
|
#snapshot: unknown;
|
2024-04-15 08:54:42 -04:00
|
|
|
#asyncId: number;
|
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
constructor(type: string) {
|
|
|
|
this.type = type;
|
2024-08-28 22:25:38 -04:00
|
|
|
this.#snapshot = getAsyncContext();
|
2024-04-15 08:54:42 -04:00
|
|
|
this.#asyncId = newAsyncId();
|
|
|
|
}
|
|
|
|
|
|
|
|
asyncId() {
|
|
|
|
return this.#asyncId;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
runInAsyncScope(
|
|
|
|
fn: (...args: unknown[]) => unknown,
|
|
|
|
thisArg: unknown,
|
|
|
|
...args: unknown[]
|
|
|
|
) {
|
2024-08-28 22:25:38 -04:00
|
|
|
const previousContext = getAsyncContext();
|
2023-02-14 11:38:45 -05:00
|
|
|
try {
|
2024-08-28 22:25:38 -04:00
|
|
|
setAsyncContext(this.#snapshot);
|
|
|
|
return ReflectApply(fn, thisArg, args);
|
2023-02-14 11:38:45 -05:00
|
|
|
} finally {
|
2024-08-28 22:25:38 -04:00
|
|
|
setAsyncContext(previousContext);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-14 18:42:43 -04:00
|
|
|
emitDestroy() {}
|
|
|
|
|
2024-08-28 22:25:38 -04:00
|
|
|
bind(fn: (...args: unknown[]) => unknown, thisArg) {
|
2023-02-14 11:38:45 -05:00
|
|
|
validateFunction(fn, "fn");
|
2024-08-28 22:25:38 -04:00
|
|
|
let bound;
|
|
|
|
if (thisArg === undefined) {
|
|
|
|
// deno-lint-ignore no-this-alias
|
|
|
|
const resource = this;
|
|
|
|
bound = function (...args) {
|
|
|
|
ArrayPrototypeUnshift(args, fn, this);
|
|
|
|
return ReflectApply(resource.runInAsyncScope, resource, args);
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
bound = FunctionPrototypeBind(this.runInAsyncScope, this, fn, thisArg);
|
|
|
|
}
|
|
|
|
ObjectDefineProperties(bound, {
|
2023-02-14 11:38:45 -05:00
|
|
|
"length": {
|
2024-08-28 22:25:38 -04:00
|
|
|
__proto__: null,
|
2023-02-14 11:38:45 -05:00
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
|
|
|
value: fn.length,
|
|
|
|
writable: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return bound;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bind(
|
|
|
|
fn: (...args: unknown[]) => unknown,
|
|
|
|
type?: string,
|
|
|
|
thisArg?: AsyncResource,
|
|
|
|
) {
|
2024-08-28 22:25:38 -04:00
|
|
|
type = type || fn.name || "bound-anonymous-fn";
|
|
|
|
return (new AsyncResource(type)).bind(fn, thisArg);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class AsyncLocalStorage {
|
2024-08-28 22:25:38 -04:00
|
|
|
#variable = new AsyncVariable();
|
|
|
|
enabled = false;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
run(store: any, callback: any, ...args: any[]): any {
|
2024-08-28 22:25:38 -04:00
|
|
|
this.enabled = true;
|
|
|
|
const previous = this.#variable.enter(store);
|
2023-02-14 11:38:45 -05:00
|
|
|
try {
|
2024-08-28 22:25:38 -04:00
|
|
|
return ReflectApply(callback, null, args);
|
2023-02-14 11:38:45 -05:00
|
|
|
} finally {
|
2024-08-28 22:25:38 -04:00
|
|
|
setAsyncContext(previous);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
exit(callback: (...args: unknown[]) => any, ...args: any[]): any {
|
2024-08-28 22:25:38 -04:00
|
|
|
if (!this.enabled) {
|
|
|
|
return ReflectApply(callback, null, args);
|
|
|
|
}
|
|
|
|
this.enabled = false;
|
|
|
|
try {
|
|
|
|
return ReflectApply(callback, null, args);
|
|
|
|
} finally {
|
|
|
|
this.enabled = true;
|
|
|
|
}
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
getStore(): any {
|
2024-08-28 22:25:38 -04:00
|
|
|
if (!this.enabled) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return this.#variable.get();
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
2024-03-06 07:05:57 -05:00
|
|
|
|
|
|
|
enterWith(store: unknown) {
|
2024-08-28 22:25:38 -04:00
|
|
|
this.enabled = true;
|
|
|
|
this.#variable.enter(store);
|
|
|
|
}
|
|
|
|
|
|
|
|
disable() {
|
|
|
|
this.enabled = false;
|
2024-03-06 07:05:57 -05:00
|
|
|
}
|
2024-04-02 18:53:29 -04:00
|
|
|
|
|
|
|
static bind(fn: (...args: unknown[]) => unknown) {
|
|
|
|
return AsyncResource.bind(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static snapshot() {
|
|
|
|
return AsyncLocalStorage.bind((
|
|
|
|
cb: (...args: unknown[]) => unknown,
|
|
|
|
...args: unknown[]
|
2024-08-28 22:25:38 -04:00
|
|
|
) => ReflectApply(cb, null, args));
|
2024-04-02 18:53:29 -04:00
|
|
|
}
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function executionAsyncId() {
|
2024-08-28 22:25:38 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function triggerAsyncId() {
|
|
|
|
return 0;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2024-08-28 22:25:38 -04:00
|
|
|
export function executionAsyncResource() {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-09-11 20:25:46 -04:00
|
|
|
export const asyncWrapProviders = ObjectFreeze({
|
|
|
|
__proto__: null,
|
|
|
|
NONE: 0,
|
|
|
|
DIRHANDLE: 1,
|
|
|
|
DNSCHANNEL: 2,
|
|
|
|
ELDHISTOGRAM: 3,
|
|
|
|
FILEHANDLE: 4,
|
|
|
|
FILEHANDLECLOSEREQ: 5,
|
|
|
|
BLOBREADER: 6,
|
|
|
|
FSEVENTWRAP: 7,
|
|
|
|
FSREQCALLBACK: 8,
|
|
|
|
FSREQPROMISE: 9,
|
|
|
|
GETADDRINFOREQWRAP: 10,
|
|
|
|
GETNAMEINFOREQWRAP: 11,
|
|
|
|
HEAPSNAPSHOT: 12,
|
|
|
|
HTTP2SESSION: 13,
|
|
|
|
HTTP2STREAM: 14,
|
|
|
|
HTTP2PING: 15,
|
|
|
|
HTTP2SETTINGS: 16,
|
|
|
|
HTTPINCOMINGMESSAGE: 17,
|
|
|
|
HTTPCLIENTREQUEST: 18,
|
|
|
|
JSSTREAM: 19,
|
|
|
|
JSUDPWRAP: 20,
|
|
|
|
MESSAGEPORT: 21,
|
|
|
|
PIPECONNECTWRAP: 22,
|
|
|
|
PIPESERVERWRAP: 23,
|
|
|
|
PIPEWRAP: 24,
|
|
|
|
PROCESSWRAP: 25,
|
|
|
|
PROMISE: 26,
|
|
|
|
QUERYWRAP: 27,
|
|
|
|
QUIC_ENDPOINT: 28,
|
|
|
|
QUIC_LOGSTREAM: 29,
|
|
|
|
QUIC_PACKET: 30,
|
|
|
|
QUIC_SESSION: 31,
|
|
|
|
QUIC_STREAM: 32,
|
|
|
|
QUIC_UDP: 33,
|
|
|
|
SHUTDOWNWRAP: 34,
|
|
|
|
SIGNALWRAP: 35,
|
|
|
|
STATWATCHER: 36,
|
|
|
|
STREAMPIPE: 37,
|
|
|
|
TCPCONNECTWRAP: 38,
|
|
|
|
TCPSERVERWRAP: 39,
|
|
|
|
TCPWRAP: 40,
|
|
|
|
TTYWRAP: 41,
|
|
|
|
UDPSENDWRAP: 42,
|
|
|
|
UDPWRAP: 43,
|
|
|
|
SIGINTWATCHDOG: 44,
|
|
|
|
WORKER: 45,
|
|
|
|
WORKERHEAPSNAPSHOT: 46,
|
|
|
|
WRITEWRAP: 47,
|
|
|
|
ZLIB: 48,
|
|
|
|
CHECKPRIMEREQUEST: 49,
|
|
|
|
PBKDF2REQUEST: 50,
|
|
|
|
KEYPAIRGENREQUEST: 51,
|
|
|
|
KEYGENREQUEST: 52,
|
|
|
|
KEYEXPORTREQUEST: 53,
|
|
|
|
CIPHERREQUEST: 54,
|
|
|
|
DERIVEBITSREQUEST: 55,
|
|
|
|
HASHREQUEST: 56,
|
|
|
|
RANDOMBYTESREQUEST: 57,
|
|
|
|
RANDOMPRIMEREQUEST: 58,
|
|
|
|
SCRYPTREQUEST: 59,
|
|
|
|
SIGNREQUEST: 60,
|
|
|
|
TLSWRAP: 61,
|
|
|
|
VERIFYREQUEST: 62,
|
|
|
|
});
|
2024-08-28 22:25:38 -04:00
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
class AsyncHook {
|
|
|
|
enable() {
|
|
|
|
}
|
|
|
|
|
|
|
|
disable() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createHook() {
|
|
|
|
return new AsyncHook();
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
2024-08-02 14:16:59 -04:00
|
|
|
AsyncLocalStorage,
|
2024-08-28 22:25:38 -04:00
|
|
|
createHook,
|
|
|
|
executionAsyncId,
|
|
|
|
triggerAsyncId,
|
|
|
|
executionAsyncResource,
|
|
|
|
asyncWrapProviders,
|
|
|
|
AsyncResource,
|
2023-02-14 11:38:45 -05:00
|
|
|
};
|