async-browser-context 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +102 -0
- package/README.md +45 -30
- package/dist/babel/async-generator-helpers.d.ts +6 -6
- package/dist/babel/async-generator-helpers.d.ts.map +1 -1
- package/dist/babel/async-generator-helpers.js +33 -22
- package/dist/babel/async-generator-helpers.js.map +1 -1
- package/dist/babel/async-generator-resources.d.ts +14 -0
- package/dist/babel/async-generator-resources.d.ts.map +1 -0
- package/dist/babel/async-generator-resources.js +38 -0
- package/dist/babel/async-generator-resources.js.map +1 -0
- package/dist/babel/bind-generators.d.ts +1 -1
- package/dist/babel/bind-generators.d.ts.map +1 -1
- package/dist/babel/bind-generators.js +88 -44
- package/dist/babel/bind-generators.js.map +1 -1
- package/dist/babel/name-coroutines.d.ts +14 -8
- package/dist/babel/name-coroutines.d.ts.map +1 -1
- package/dist/babel/name-coroutines.js +160 -39
- package/dist/babel/name-coroutines.js.map +1 -1
- package/dist/babel/preset.d.ts +24 -13
- package/dist/babel/preset.d.ts.map +1 -1
- package/dist/babel/preset.js +37 -9
- package/dist/babel/preset.js.map +1 -1
- package/dist/babel/runtime-calls.d.ts +22 -0
- package/dist/babel/runtime-calls.d.ts.map +1 -0
- package/dist/babel/runtime-calls.js +69 -0
- package/dist/babel/runtime-calls.js.map +1 -0
- package/dist/babel/skip-runtime.d.ts +18 -11
- package/dist/babel/skip-runtime.d.ts.map +1 -1
- package/dist/babel/skip-runtime.js +52 -16
- package/dist/babel/skip-runtime.js.map +1 -1
- package/dist/babel/static-members.d.ts +4 -0
- package/dist/babel/static-members.d.ts.map +1 -0
- package/dist/babel/static-members.js +112 -0
- package/dist/babel/static-members.js.map +1 -0
- package/dist/core/async-generator.d.ts +51 -13
- package/dist/core/async-generator.d.ts.map +1 -1
- package/dist/core/async-generator.js +255 -58
- package/dist/core/async-generator.js.map +1 -1
- package/dist/core/async-local-storage.d.ts +3 -0
- package/dist/core/async-local-storage.d.ts.map +1 -1
- package/dist/core/async-local-storage.js +3 -0
- package/dist/core/async-local-storage.js.map +1 -1
- package/dist/core/callback-hook.d.ts +3 -3
- package/dist/core/coroutine.d.ts +6 -2
- package/dist/core/coroutine.d.ts.map +1 -1
- package/dist/core/coroutine.js +106 -44
- package/dist/core/coroutine.js.map +1 -1
- package/dist/core/function-name.d.ts +9 -0
- package/dist/core/function-name.d.ts.map +1 -0
- package/dist/core/function-name.js +12 -0
- package/dist/core/function-name.js.map +1 -0
- package/dist/core/hooks.d.ts +2 -1
- package/dist/core/hooks.d.ts.map +1 -1
- package/dist/core/hooks.js +21 -6
- package/dist/core/hooks.js.map +1 -1
- package/dist/core/snapshot.d.ts +2 -1
- package/dist/core/snapshot.d.ts.map +1 -1
- package/dist/core/snapshot.js +7 -3
- package/dist/core/snapshot.js.map +1 -1
- package/dist/core/store.d.ts +28 -0
- package/dist/core/store.d.ts.map +1 -1
- package/dist/core/store.js +90 -20
- package/dist/core/store.js.map +1 -1
- package/dist/core/variable.d.ts +2 -1
- package/dist/core/variable.d.ts.map +1 -1
- package/dist/core/variable.js +5 -2
- package/dist/core/variable.js.map +1 -1
- package/dist/node/index.d.ts +10 -4
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +51 -16
- package/dist/node/index.js.map +1 -1
- package/dist/node/runtime.d.ts +7 -6
- package/dist/node/runtime.d.ts.map +1 -1
- package/dist/node/runtime.js +29 -16
- package/dist/node/runtime.js.map +1 -1
- package/dist/opentelemetry/context-manager.d.ts.map +1 -1
- package/dist/opentelemetry/context-manager.js +172 -13
- package/dist/opentelemetry/context-manager.js.map +1 -1
- package/dist/patches/callbacks.d.ts.map +1 -1
- package/dist/patches/callbacks.js +84 -29
- package/dist/patches/callbacks.js.map +1 -1
- package/dist/patches/events.d.ts.map +1 -1
- package/dist/patches/events.js +203 -91
- package/dist/patches/events.js.map +1 -1
- package/dist/patches/handler-interfaces.d.ts +3 -2
- package/dist/patches/handler-interfaces.d.ts.map +1 -1
- package/dist/patches/handler-interfaces.js +3 -2
- package/dist/patches/handler-interfaces.js.map +1 -1
- package/dist/patches/promise.d.ts +3 -0
- package/dist/patches/promise.d.ts.map +1 -1
- package/dist/patches/promise.js +13 -5
- package/dist/patches/promise.js.map +1 -1
- package/dist/patches/streams.d.ts.map +1 -1
- package/dist/patches/streams.js +36 -16
- package/dist/patches/streams.js.map +1 -1
- package/dist/patches/timers.d.ts.map +1 -1
- package/dist/patches/timers.js +17 -5
- package/dist/patches/timers.js.map +1 -1
- package/dist/patches/to-string.d.ts +4 -0
- package/dist/patches/to-string.d.ts.map +1 -1
- package/dist/patches/to-string.js +12 -6
- package/dist/patches/to-string.js.map +1 -1
- package/dist/runtime.d.ts +3 -3
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +3 -3
- package/dist/runtime.js.map +1 -1
- package/dist/vite/plugin.d.ts +7 -2
- package/dist/vite/plugin.d.ts.map +1 -1
- package/dist/vite/plugin.js +30 -6
- package/dist/vite/plugin.js.map +1 -1
- package/package.json +49 -25
- package/src/babel/async-generator-helpers.ts +34 -23
- package/src/babel/async-generator-resources.ts +40 -0
- package/src/babel/bind-generators.ts +91 -47
- package/src/babel/name-coroutines.ts +170 -41
- package/src/babel/preset.ts +47 -15
- package/src/babel/runtime-calls.ts +73 -0
- package/src/babel/skip-runtime.ts +58 -18
- package/src/babel/static-members.ts +117 -0
- package/src/core/async-generator.ts +273 -62
- package/src/core/async-local-storage.ts +3 -0
- package/src/core/callback-hook.ts +3 -3
- package/src/core/coroutine.ts +104 -43
- package/src/core/function-name.ts +11 -0
- package/src/core/hooks.ts +23 -6
- package/src/core/snapshot.ts +8 -3
- package/src/core/store.ts +107 -21
- package/src/core/variable.ts +6 -2
- package/src/node/index.ts +62 -19
- package/src/node/runtime.ts +33 -15
- package/src/opentelemetry/context-manager.ts +188 -13
- package/src/patches/callbacks.ts +87 -30
- package/src/patches/events.ts +249 -103
- package/src/patches/handler-interfaces.ts +3 -2
- package/src/patches/promise.ts +17 -13
- package/src/patches/streams.ts +36 -16
- package/src/patches/timers.ts +17 -5
- package/src/patches/to-string.ts +12 -6
- package/src/runtime.ts +3 -3
- package/src/vite/plugin.ts +38 -8
package/dist/core/store.js
CHANGED
|
@@ -29,20 +29,50 @@ function createStore() {
|
|
|
29
29
|
// Own properties from the start: each callback reads `hooks`, and the read of an own property is faster
|
|
30
30
|
hooks: undefined,
|
|
31
31
|
inHook: false,
|
|
32
|
+
idle: root,
|
|
32
33
|
};
|
|
33
34
|
}
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
/**
|
|
36
|
+
* This function gives the store of the global object `holder`. If `holder` has no store, the
|
|
37
|
+
* function makes one, and adds it as a property that code cannot change or remove.
|
|
38
|
+
*
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
export function getOrCreateStore(holder) {
|
|
36
42
|
const existing = holder[STORE_KEY];
|
|
37
43
|
if (existing !== undefined) {
|
|
44
|
+
// The stores of version 0.2 and later have the own property hooks from the start. Version 0.1 has no hooks.
|
|
45
|
+
if (!Object.prototype.hasOwnProperty.call(existing, "hooks") && existing.oldCopyPatches === undefined) {
|
|
46
|
+
existing.oldCopyPatches = new Set(existing.patches);
|
|
47
|
+
}
|
|
38
48
|
return existing;
|
|
39
49
|
}
|
|
40
50
|
const created = createStore();
|
|
41
|
-
Object.defineProperty(
|
|
51
|
+
Object.defineProperty(holder, STORE_KEY, { value: created, writable: false, enumerable: false, configurable: false });
|
|
42
52
|
return created;
|
|
43
53
|
}
|
|
44
54
|
/** The store of this global object. */
|
|
45
|
-
export const store = getOrCreateStore();
|
|
55
|
+
export const store = getOrCreateStore(globalThis);
|
|
56
|
+
/** `true` while a record of the idle frame waits in the microtask queue. */
|
|
57
|
+
let idleRecordQueued = false;
|
|
58
|
+
/**
|
|
59
|
+
* This function records `store.idle` in the next microtask. `enterWith()` starts it, because a
|
|
60
|
+
* top-level `enterWith()` changes the frame that stays current between tasks. A microtask
|
|
61
|
+
* operates when the stack is empty, after each wrapper set its previous frame again. The callback
|
|
62
|
+
* goes to the native `then`, so no wrapper sets a frame for it.
|
|
63
|
+
*/
|
|
64
|
+
export function recordIdleFrame() {
|
|
65
|
+
// Stryker disable next-line ConditionalExpression,BlockStatement: a second record in the same turn records the same frame, so the flag only prevents more microtasks
|
|
66
|
+
if (idleRecordQueued) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
// Stryker disable next-line BooleanLiteral: a second record in the same turn records the same frame, so the flag only prevents more microtasks
|
|
70
|
+
idleRecordQueued = true;
|
|
71
|
+
Reflect.apply(store.intrinsics.promiseThen, store.intrinsics.promiseResolve(undefined), [() => {
|
|
72
|
+
idleRecordQueued = false;
|
|
73
|
+
store.idle = store.current;
|
|
74
|
+
}]);
|
|
75
|
+
}
|
|
46
76
|
/** This function makes `frame` the current frame. It gives the frame that was current before. */
|
|
47
77
|
export function enter(frame) {
|
|
48
78
|
const previous = store.current;
|
|
@@ -64,7 +94,8 @@ class ValueFrame {
|
|
|
64
94
|
/**
|
|
65
95
|
* The result of the last search for another variable from this frame. A
|
|
66
96
|
* frame does not change, so the result stays correct. The cached value
|
|
67
|
-
* comes from a parent frame
|
|
97
|
+
* comes from a parent frame. The cache also keeps the last variable of a
|
|
98
|
+
* search, so a frame can keep one more variable in memory.
|
|
68
99
|
*/
|
|
69
100
|
#cachedVariable = undefined;
|
|
70
101
|
#cachedValue = undefined;
|
|
@@ -91,6 +122,15 @@ class ValueFrame {
|
|
|
91
122
|
start.#cachedValue = value;
|
|
92
123
|
return value;
|
|
93
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* This method makes a frame that sets `variable`. When `parent` sets the same variable,
|
|
127
|
+
* the new frame replaces it: the new frame has the parent of `parent`. A search gives the
|
|
128
|
+
* same values. Many uses of enterWith() in one context thus do not make a long chain.
|
|
129
|
+
*/
|
|
130
|
+
static create(parent, variable, value) {
|
|
131
|
+
const base = #variable in parent && parent.#variable === variable ? parent.parent : parent;
|
|
132
|
+
return new ValueFrame(base, variable, value);
|
|
133
|
+
}
|
|
94
134
|
static #search(start, variable) {
|
|
95
135
|
for (let frame = start; frame !== null; frame = frame.parent) {
|
|
96
136
|
if (#variable in frame && frame.#variable === variable) {
|
|
@@ -102,7 +142,7 @@ class ValueFrame {
|
|
|
102
142
|
}
|
|
103
143
|
/** This function makes a new frame that has `parent` as its parent and sets `variable` to `value`. */
|
|
104
144
|
export function createFrame(parent, variable, value) {
|
|
105
|
-
return
|
|
145
|
+
return ValueFrame.create(parent, variable, value);
|
|
106
146
|
}
|
|
107
147
|
/** This function gives the value of `variable` in `frame`, or `NOT_FOUND` if no frame sets it. */
|
|
108
148
|
export function findValue(frame, variable) {
|
|
@@ -110,12 +150,17 @@ export function findValue(frame, variable) {
|
|
|
110
150
|
}
|
|
111
151
|
/** This function gives the error of a hook to the host. A hook error does not stop the callback. */
|
|
112
152
|
function reportHookError(error) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
report
|
|
116
|
-
|
|
153
|
+
try {
|
|
154
|
+
const report = globalThis.reportError;
|
|
155
|
+
if (typeof report === "function") {
|
|
156
|
+
Reflect.apply(report, globalThis, [error]);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
console.error(error);
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
// The report itself failed. The callback must operate, so the error stops here.
|
|
117
163
|
}
|
|
118
|
-
console.error(error);
|
|
119
164
|
}
|
|
120
165
|
/**
|
|
121
166
|
* This function starts one method of each hook. `before` uses the order of
|
|
@@ -129,12 +174,12 @@ function callHooks(hooks, phase, callback, source) {
|
|
|
129
174
|
const count = hooks.length;
|
|
130
175
|
for (let step = 0; step < count; step++) {
|
|
131
176
|
const { hook } = hooks[phase === "before" ? step : count - 1 - step];
|
|
132
|
-
const method = hook[phase];
|
|
133
|
-
if (typeof method !== "function") {
|
|
134
|
-
continue;
|
|
135
|
-
}
|
|
136
177
|
try {
|
|
137
|
-
method
|
|
178
|
+
// A getter of the method can also throw
|
|
179
|
+
const method = hook[phase];
|
|
180
|
+
if (typeof method === "function") {
|
|
181
|
+
Reflect.apply(method, hook, [callback, source]);
|
|
182
|
+
}
|
|
138
183
|
}
|
|
139
184
|
catch (error) {
|
|
140
185
|
reportHookError(error);
|
|
@@ -154,16 +199,32 @@ function callHooks(hooks, phase, callback, source) {
|
|
|
154
199
|
*/
|
|
155
200
|
export function applyWithHooks(hooks, callback, source, fn, thisValue, args) {
|
|
156
201
|
if (store.inHook === true) {
|
|
157
|
-
return
|
|
202
|
+
return Reflect.apply(fn, thisValue, args);
|
|
158
203
|
}
|
|
159
204
|
callHooks(hooks, "before", callback, source);
|
|
160
205
|
try {
|
|
161
|
-
return
|
|
206
|
+
return Reflect.apply(fn, thisValue, args);
|
|
162
207
|
}
|
|
163
208
|
finally {
|
|
164
209
|
callHooks(hooks, "after", callback, source);
|
|
165
210
|
}
|
|
166
211
|
}
|
|
212
|
+
/**
|
|
213
|
+
* This function starts `fn` as a callback that a hook registered: the hooks do not operate for
|
|
214
|
+
* it. `store.inHook` stays `true` while it operates. Thus the callbacks and the async functions
|
|
215
|
+
* that it starts or registers do not start the hooks either. A hook cannot start itself without
|
|
216
|
+
* end, also through a chain of callbacks.
|
|
217
|
+
*/
|
|
218
|
+
export function applyQuietly(fn, thisValue, args) {
|
|
219
|
+
const previous = store.inHook;
|
|
220
|
+
store.inHook = true;
|
|
221
|
+
try {
|
|
222
|
+
return Reflect.apply(fn, thisValue, args);
|
|
223
|
+
}
|
|
224
|
+
finally {
|
|
225
|
+
store.inHook = previous;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
167
228
|
/*
|
|
168
229
|
* Each wrapper has the name `asyncBrowserContextWrapper`. Profilers, stack
|
|
169
230
|
* traces and the Long Animation Frames API show the static name of a
|
|
@@ -177,13 +238,18 @@ export function applyWithHooks(hooks, callback, source, fn, thisValue, args) {
|
|
|
177
238
|
* `fn`, for the callback hooks.
|
|
178
239
|
*/
|
|
179
240
|
export function bindToFrame(frame, fn, source) {
|
|
241
|
+
// A callback that a hook registers does not start the hooks, so a hook cannot start itself without end
|
|
242
|
+
const quiet = store.inHook === true;
|
|
180
243
|
return function asyncBrowserContextWrapper(...args) {
|
|
181
244
|
const previous = store.current;
|
|
182
245
|
store.current = frame;
|
|
183
246
|
try {
|
|
247
|
+
if (quiet) {
|
|
248
|
+
return applyQuietly(fn, this, args);
|
|
249
|
+
}
|
|
184
250
|
const hooks = store.hooks;
|
|
185
251
|
if (hooks === undefined) {
|
|
186
|
-
return
|
|
252
|
+
return Reflect.apply(fn, this, args);
|
|
187
253
|
}
|
|
188
254
|
return applyWithHooks(hooks, fn, source, fn, this, args);
|
|
189
255
|
}
|
|
@@ -198,13 +264,17 @@ export function bindToFrame(frame, fn, source) {
|
|
|
198
264
|
* `bindToFrame` on the hot path.
|
|
199
265
|
*/
|
|
200
266
|
export function bindOneArgument(frame, fn, source) {
|
|
267
|
+
const quiet = store.inHook === true;
|
|
201
268
|
return function asyncBrowserContextWrapper(argument) {
|
|
202
269
|
const previous = store.current;
|
|
203
270
|
store.current = frame;
|
|
204
271
|
try {
|
|
272
|
+
if (quiet) {
|
|
273
|
+
return applyQuietly(fn, this, [argument]);
|
|
274
|
+
}
|
|
205
275
|
const hooks = store.hooks;
|
|
206
276
|
if (hooks === undefined) {
|
|
207
|
-
return
|
|
277
|
+
return Reflect.apply(fn, this, [argument]);
|
|
208
278
|
}
|
|
209
279
|
return applyWithHooks(hooks, fn, source, fn, this, [argument]);
|
|
210
280
|
}
|
package/dist/core/store.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store.js","sourceRoot":"","sources":["../../src/core/store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;
|
|
1
|
+
{"version":3,"file":"store.js","sourceRoot":"","sources":["../../src/core/store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AA4DH,+FAA+F;AAC/F,MAAM,SA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AW,QAAY;QAC7D,MAAM,QAAQ,GAAG,KAAK,CAAC,OAAO,CAAC;QAC/B,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC;QACtB,IAAI,CAAC;YACD,IAAI,KAAK,EAAE,CAAC;gBACR,OAAO,YAAY,CAAC,EAAE,EAAE,IAAI,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;YAC9C,CAAC;YACD,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC;YAC1B,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACtB,OAAO,OAAO,CAAC,KAAK,CAAC,EAAE,EAAE,IAAI,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;YAC/C,CAAC;YACD,OAAO,cAAc,CAAC,KAAK,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;QACnE,CAAC;gBAAS,CAAC;YACP,KAAK,CAAC,OAAO,GAAG,QAAQ,CAAC;QAC7B,CAAC;IACL,CAAC,CAAC;AACN,CAAC"}
|
package/dist/core/variable.d.ts
CHANGED
|
@@ -34,7 +34,8 @@ export declare class Variable<T> {
|
|
|
34
34
|
get(): T | undefined;
|
|
35
35
|
/**
|
|
36
36
|
* This method starts `fn` with `args` in a new context in which `get()`
|
|
37
|
-
* gives `value`.
|
|
37
|
+
* gives `value`. As in the TC39 draft, `fn` gets `undefined` as its `this`
|
|
38
|
+
* value. After `fn` ends, also with an error, the previous context is
|
|
38
39
|
* current again. The method gives the result of `fn`.
|
|
39
40
|
*/
|
|
40
41
|
run<R, A extends unknown[]>(value: T, fn: (...args: A) => R, ...args: A): R;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"variable.d.ts","sourceRoot":"","sources":["../../src/core/variable.ts"],"names":[],"mappings":"AAEA,mCAAmC;AACnC,MAAM,WAAW,eAAe,CAAC,CAAC;IAC9B,6FAA6F;IAC7F,QAAQ,CAAC,IAAI,CAAC,EAAG,MAAM,GAAG,SAAS,CAAC;IACpC,iEAAiE;IACjE,QAAQ,CAAC,YAAY,CAAC,EAAG,CAAC,GAAG,SAAS,CAAC;CAC1C;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,QAAQ,CAAC,CAAC;;gBAIP,OAAO,GAAG,eAAe,CAAC,CAAC,CAAM;IAK7C,gCAAgC;IAChC,IAAI,IAAI,IAAK,MAAM,CAElB;IAED;;;OAGG;IACH,GAAG,IAAK,CAAC,GAAG,SAAS;IAKrB
|
|
1
|
+
{"version":3,"file":"variable.d.ts","sourceRoot":"","sources":["../../src/core/variable.ts"],"names":[],"mappings":"AAEA,mCAAmC;AACnC,MAAM,WAAW,eAAe,CAAC,CAAC;IAC9B,6FAA6F;IAC7F,QAAQ,CAAC,IAAI,CAAC,EAAG,MAAM,GAAG,SAAS,CAAC;IACpC,iEAAiE;IACjE,QAAQ,CAAC,YAAY,CAAC,EAAG,CAAC,GAAG,SAAS,CAAC;CAC1C;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,QAAQ,CAAC,CAAC;;gBAIP,OAAO,GAAG,eAAe,CAAC,CAAC,CAAM;IAK7C,gCAAgC;IAChC,IAAI,IAAI,IAAK,MAAM,CAElB;IAED;;;OAGG;IACH,GAAG,IAAK,CAAC,GAAG,SAAS;IAKrB;;;;;OAKG;IACH,GAAG,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,EAAE,KAAK,EAAG,CAAC,EAAE,EAAE,EAAG,CAAC,GAAG,IAAI,EAAG,CAAC,KAAK,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,GAAI,CAAC;CASnF"}
|
package/dist/core/variable.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createFrame, findValue, NOT_FOUND, store } from "./store.js";
|
|
1
|
+
import { createFrame, findValue, NOT_FOUND, recordIdleFrame, store } from "./store.js";
|
|
2
2
|
/**
|
|
3
3
|
* A value that is different in each context. This class is the
|
|
4
4
|
* `AsyncContext.Variable` of the TC39 AsyncContext proposal.
|
|
@@ -37,7 +37,8 @@ export class Variable {
|
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
39
39
|
* This method starts `fn` with `args` in a new context in which `get()`
|
|
40
|
-
* gives `value`.
|
|
40
|
+
* gives `value`. As in the TC39 draft, `fn` gets `undefined` as its `this`
|
|
41
|
+
* value. After `fn` ends, also with an error, the previous context is
|
|
41
42
|
* current again. The method gives the result of `fn`.
|
|
42
43
|
*/
|
|
43
44
|
run(value, fn, ...args) {
|
|
@@ -51,6 +52,7 @@ export class Variable {
|
|
|
51
52
|
}
|
|
52
53
|
}
|
|
53
54
|
}
|
|
55
|
+
Object.defineProperty(Variable.prototype, Symbol.toStringTag, { value: "AsyncContext.Variable", configurable: true });
|
|
54
56
|
/**
|
|
55
57
|
* This function makes a new current frame in which `variable` has `value`.
|
|
56
58
|
* The frame stays current until the code that controls the current step sets
|
|
@@ -60,5 +62,6 @@ export class Variable {
|
|
|
60
62
|
*/
|
|
61
63
|
export function enterValue(variable, value) {
|
|
62
64
|
store.current = createFrame(store.current, variable, value);
|
|
65
|
+
recordIdleFrame();
|
|
63
66
|
}
|
|
64
67
|
//# sourceMappingURL=variable.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"variable.js","sourceRoot":"","sources":["../../src/core/variable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"variable.js","sourceRoot":"","sources":["../../src/core/variable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAUvF;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAO,QAAQ;IACR,KAAK,CAAU;IACf,aAAa,CAAiB;IAEvC,YAAY,UAA+B,EAAE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/node/index.d.ts
CHANGED
|
@@ -21,20 +21,26 @@ export declare class Variable<T> {
|
|
|
21
21
|
get name(): string;
|
|
22
22
|
/** This method gives the value of this variable in the current context, or the default value. */
|
|
23
23
|
get(): T | undefined;
|
|
24
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* This method starts `fn` with `args` in a new context in which `get()` gives `value`. As
|
|
26
|
+
* in the TC39 draft, `fn` gets `undefined` as its `this` value.
|
|
27
|
+
*/
|
|
25
28
|
run<R, A extends unknown[]>(value: T, fn: (...args: A) => R, ...args: A): R;
|
|
26
29
|
}
|
|
27
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* The `AsyncContext.Snapshot` of the TC39 proposal. An `AsyncResource` records the context, as
|
|
32
|
+
* `AsyncLocalStorage.snapshot()` does, at a much lower cost (src/node/runtime.ts).
|
|
33
|
+
*/
|
|
28
34
|
export declare class Snapshot {
|
|
29
35
|
#private;
|
|
30
|
-
/** This method starts `fn` with `args` in the recorded context. */
|
|
36
|
+
/** This method starts `fn` with `args` in the recorded context. `fn` gets `undefined` as its `this` value. */
|
|
31
37
|
run<R, A extends unknown[]>(fn: (...args: A) => R, ...args: A): R;
|
|
32
38
|
/** This method records the current context. It gives a wrapper of `fn` that operates in that context. */
|
|
33
39
|
static wrap<T, A extends unknown[], R>(fn: (this: T, ...args: A) => R): (this: T, ...args: A) => R;
|
|
34
40
|
}
|
|
35
41
|
/**
|
|
36
42
|
* The native `AsyncLocalStorage` of Node.js. On a Node.js version without the
|
|
37
|
-
* `defaultValue` and `name` options, a
|
|
43
|
+
* `defaultValue` and `name` options, a compatible class adds them (compatible-storage.ts).
|
|
38
44
|
*/
|
|
39
45
|
export declare const AsyncLocalStorage: typeof NodeAsyncLocalStorage;
|
|
40
46
|
/** The type of the native `AsyncLocalStorage`. */
|
package/dist/node/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/node/index.ts"],"names":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AAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,CAAC,GAAI,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,CAAC;CAc3G;AAoBD;;;GAGG;AACH,eAAO,MAAM,iBAAiB,EAAG,OAAO,qBAEY,CAAC;AACrD,kDAAkD;AAClD,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI,qBAAqB,CAAC,CAAC,CAAC,CAAC;AAE5D,8DAA8D;AAC9D,eAAO,MAAM,YAAY,EAAG;IAAE,QAAQ,CAAC,QAAQ,EAAG,OAAO,QAAQ,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAG,OAAO,QAAQ,CAAA;CAA0C,CAAC;AAEjJ,mCAAmC;AACnC,eAAO,MAAM,aAAa,EAAG,OAAO,QAAmB,CAAC;AACxD,mCAAmC;AACnC,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAC;AAC3C,mCAAmC;AACnC,eAAO,MAAM,aAAa,EAAG,OAAO,QAAmB,CAAC;AACxD,mCAAmC;AACnC,MAAM,MAAM,aAAa,GAAG,QAAQ,CAAC;AAErC;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAG,YAAY,GAAI,MAAM,IAAI,CAKhE"}
|
package/dist/node/index.js
CHANGED
|
@@ -7,14 +7,19 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @packageDocumentation
|
|
9
9
|
*/
|
|
10
|
-
import { AsyncLocalStorage as NodeAsyncLocalStorage } from "node:async_hooks";
|
|
10
|
+
import { AsyncLocalStorage as NodeAsyncLocalStorage, AsyncResource } from "node:async_hooks";
|
|
11
11
|
import { assertCallbackHook } from "../core/callback-hook.js";
|
|
12
12
|
import { createCompatibleStorage } from "./compatible-storage.js";
|
|
13
|
+
/**
|
|
14
|
+
* One native storage for all variables. Its store is the chain of the values of the current
|
|
15
|
+
* context. On Node.js 22, each native storage that operated stays in a global list of
|
|
16
|
+
* async_hooks, and each new async resource examines the list. One storage keeps that list short.
|
|
17
|
+
*/
|
|
18
|
+
const values = new NodeAsyncLocalStorage();
|
|
13
19
|
/** The `AsyncContext.Variable` of the TC39 proposal, on the native `AsyncLocalStorage`. */
|
|
14
20
|
export class Variable {
|
|
15
21
|
#name;
|
|
16
22
|
#defaultValue;
|
|
17
|
-
#storage = new NodeAsyncLocalStorage();
|
|
18
23
|
constructor(options = {}) {
|
|
19
24
|
this.#name = options.name === undefined ? "" : String(options.name);
|
|
20
25
|
this.#defaultValue = options.defaultValue;
|
|
@@ -25,43 +30,73 @@ export class Variable {
|
|
|
25
30
|
}
|
|
26
31
|
/** This method gives the value of this variable in the current context, or the default value. */
|
|
27
32
|
get() {
|
|
28
|
-
|
|
29
|
-
|
|
33
|
+
for (let entry = values.getStore(); entry !== undefined; entry = entry.parent) {
|
|
34
|
+
if (entry.variable === this) {
|
|
35
|
+
return entry.value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return this.#defaultValue;
|
|
30
39
|
}
|
|
31
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* This method starts `fn` with `args` in a new context in which `get()` gives `value`. As
|
|
42
|
+
* in the TC39 draft, `fn` gets `undefined` as its `this` value.
|
|
43
|
+
*/
|
|
32
44
|
run(value, fn, ...args) {
|
|
33
|
-
|
|
45
|
+
const current = values.getStore();
|
|
46
|
+
// A value of the same variable in the current context gets replaced, so the chain stays short
|
|
47
|
+
const parent = current !== undefined && current.variable === this ? current.parent : current;
|
|
48
|
+
return values.run({ variable: this, value, parent }, () => fn(...args));
|
|
34
49
|
}
|
|
35
50
|
}
|
|
36
|
-
|
|
51
|
+
Object.defineProperty(Variable.prototype, Symbol.toStringTag, { value: "AsyncContext.Variable", configurable: true });
|
|
52
|
+
/** The type of the async resources of the snapshots, for async_hooks. */
|
|
53
|
+
const SNAPSHOT_TYPE = "AsyncContext.Snapshot";
|
|
54
|
+
/**
|
|
55
|
+
* The `AsyncContext.Snapshot` of the TC39 proposal. An `AsyncResource` records the context, as
|
|
56
|
+
* `AsyncLocalStorage.snapshot()` does, at a much lower cost (src/node/runtime.ts).
|
|
57
|
+
*/
|
|
37
58
|
export class Snapshot {
|
|
38
|
-
#
|
|
39
|
-
/** This method starts `fn` with `args` in the recorded context. */
|
|
59
|
+
#context = new AsyncResource(SNAPSHOT_TYPE);
|
|
60
|
+
/** This method starts `fn` with `args` in the recorded context. `fn` gets `undefined` as its `this` value. */
|
|
40
61
|
run(fn, ...args) {
|
|
41
|
-
return this.#
|
|
62
|
+
return this.#context.runInAsyncScope(fn, undefined, ...args);
|
|
42
63
|
}
|
|
43
64
|
/** This method records the current context. It gives a wrapper of `fn` that operates in that context. */
|
|
44
65
|
static wrap(fn) {
|
|
45
66
|
if (typeof fn !== "function") {
|
|
46
67
|
throw new TypeError("Snapshot.wrap: the argument is not a function");
|
|
47
68
|
}
|
|
48
|
-
const
|
|
69
|
+
const context = new AsyncResource(SNAPSHOT_TYPE);
|
|
49
70
|
const wrapped = function (...args) {
|
|
50
|
-
return
|
|
71
|
+
return context.runInAsyncScope(fn, this, ...args);
|
|
51
72
|
};
|
|
52
|
-
|
|
73
|
+
// As CopyNameAndLength with the prefix "wrapped" in the TC39 draft
|
|
74
|
+
const name = fn.name;
|
|
75
|
+
Object.defineProperty(wrapped, "name", { value: `wrapped ${typeof name === "string" ? name : ""}`, configurable: true });
|
|
53
76
|
Object.defineProperty(wrapped, "length", { value: fn.length, configurable: true });
|
|
54
77
|
return wrapped;
|
|
55
78
|
}
|
|
56
79
|
}
|
|
57
|
-
|
|
80
|
+
Object.defineProperty(Snapshot.prototype, Symbol.toStringTag, { value: "AsyncContext.Snapshot", configurable: true });
|
|
81
|
+
/**
|
|
82
|
+
* This function gives `true` if the native class supports the `defaultValue` option (Node.js 24
|
|
83
|
+
* and later), and `exit()` then gives `undefined` as in the browser class. Without
|
|
84
|
+
* AsyncContextFrame (`--no-async-context-frame`), `exit()` gives the default value, so the
|
|
85
|
+
* compatible class is used. The probe disables itself, so it does not stay in the list of
|
|
86
|
+
* async_hooks.
|
|
87
|
+
*/
|
|
58
88
|
function supportsOptions() {
|
|
59
89
|
const probe = new NodeAsyncLocalStorage({ defaultValue: 1 });
|
|
60
|
-
|
|
90
|
+
try {
|
|
91
|
+
return probe.getStore() === 1 && probe.run(2, () => probe.exit(() => probe.getStore())) === undefined;
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
probe.disable();
|
|
95
|
+
}
|
|
61
96
|
}
|
|
62
97
|
/**
|
|
63
98
|
* The native `AsyncLocalStorage` of Node.js. On a Node.js version without the
|
|
64
|
-
* `defaultValue` and `name` options, a
|
|
99
|
+
* `defaultValue` and `name` options, a compatible class adds them (compatible-storage.ts).
|
|
65
100
|
*/
|
|
66
101
|
export const AsyncLocalStorage = supportsOptions()
|
|
67
102
|
? NodeAsyncLocalStorage
|
package/dist/node/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,iBAAiB,IAAI,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,iBAAiB,IAAI,qBAAqB,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAC7F,OAAO,EAAE,kBAAkB,EAAqB,MAAM,0BAA0B,CAAC;AAEjF,OAAO,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AAgBlE;;;;GAIG;AACH,MAAM,MAAM,GAAG,IAAI,qBAAqB,EAAS,CAAC;AAElD,2FAA2F;AAC3F,MAAM,OAAO,QAAQ;IACR,KAAK,CAAU;IACf,aAAa,CAAiB;IAEvC,YAAY,UAA+B,EAAE;QACzC,IAAI,CAAC,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E,CAAC,CAAC;AAExH;;;;;;GAMG;AACH,SAAS,eAAe;IACpB,MAAM,KAAK,GAAG,IAAI,qBAAqB,CAAS,EAAE,YAAY,EAAG,CAAC,EAAW,CAAC,CAAC;IAC/E,IAAI,CAAC;QACD,OAAO,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,KAAK,SAAS,CAAC;IAC1G,CAAC;YAAS,CAAC;QACP,KAAK,CAAC,OAAO,EAAE,CAAC;IACpB,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAkC,eAAe,EAAE;IAC7E,CAAC,CAAC,qBAAqB;IACvB,CAAC,CAAC,uBAAuB,CAAC,qBAAqB,CAAC,CAAC;AAIrD,8DAA8D;AAC9D,MAAM,CAAC,MAAM,YAAY,GAAkF,MAAM,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC,CAAC;AAEjJ,mCAAmC;AACnC,MAAM,CAAC,MAAM,aAAa,GAAqB,QAAQ,CAAC;AAGxD,mCAAmC;AACnC,MAAM,CAAC,MAAM,aAAa,GAAqB,QAAQ,CAAC;AAIxD;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAAC,IAAmB;IAC/C,kBAAkB,CAAC,IAAI,CAAC,CAAC;IACzB,OAAO,SAAS,kBAAkB;QAC9B,6BAA6B;IACjC,CAAC,CAAC;AACN,CAAC"}
|
package/dist/node/runtime.d.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
export { OverloadYield } from "../core/async-generator.js";
|
|
1
|
+
export { asyncGeneratorDelegate, awaitAsyncGenerator, OverloadYield } from "../core/async-generator.js";
|
|
2
2
|
/**
|
|
3
3
|
* This function gives an async function that operates `generatorFunction`. The function is
|
|
4
4
|
* a native async function: each `await` of the source becomes one native `await`, with
|
|
5
|
-
* the same microtask turns. An error of the parameters rejects the promise.
|
|
5
|
+
* the same microtask turns. An error of the parameters rejects the promise. The function has the
|
|
6
|
+
* name and the length of the source, as in the browser runtime.
|
|
6
7
|
*/
|
|
7
|
-
export declare function coroutine<T, A extends unknown[], R>(generatorFunction: (this: T, ...args: A) => Generator<unknown, R, unknown
|
|
8
|
+
export declare function coroutine<T, A extends unknown[], R>(generatorFunction: (this: T, ...args: A) => Generator<unknown, R, unknown>, name?: string): (this: T, ...args: A) => Promise<R>;
|
|
8
9
|
/**
|
|
9
10
|
* This function gives a function that makes an async generator object, which operates
|
|
10
11
|
* `generatorFunction`. The preset gives it in place of the helper `wrapAsyncGenerator` of
|
|
11
|
-
* Babel. The driver starts in
|
|
12
|
-
*
|
|
12
|
+
* Babel. The driver starts in the context of the creation (rule C5). The native
|
|
13
|
+
* `AsyncLocalStorage` keeps that context through the `await` of the driver.
|
|
13
14
|
*/
|
|
14
|
-
export declare function asyncGenerator<T, A extends unknown[]>(generatorFunction: (this: T, ...args: A) => Generator<unknown, unknown, unknown
|
|
15
|
+
export declare function asyncGenerator<T, A extends unknown[]>(generatorFunction: (this: T, ...args: A) => Generator<unknown, unknown, unknown>, name?: string): (this: T, ...args: A) => AsyncGenerator<unknown, unknown, unknown>;
|
|
15
16
|
type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown, unknown, unknown>;
|
|
16
17
|
/** This function binds a generator object to the current context (rule C5). */
|
|
17
18
|
export declare function bindGenerator<I extends AnyIterator>(generator: I): I;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/node/runtime.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/node/runtime.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAKxG;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,EAAE,CAAC,EAC/C,iBAAiB,EAAG,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,SAAS,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,EAC7E,IAAI,CAAC,EAAG,MAAM,GACd,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAoBxC;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,EACjD,iBAAiB,EAAG,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EACnF,IAAI,CAAC,EAAG,MAAM,GACd,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAgBvE;AAED,KAAK,WAAW,GAAG,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,GAAG,aAAa,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;AAGlG,+EAA+E;AAC/E,wBAAgB,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,SAAS,EAAG,CAAC,GAAI,CAAC,CAuBtE"}
|
package/dist/node/runtime.js
CHANGED
|
@@ -2,20 +2,29 @@
|
|
|
2
2
|
* The runtime of transformed code on Node.js. On Node.js, the native
|
|
3
3
|
* `AsyncLocalStorage` keeps the context through promises, so `coroutine` is
|
|
4
4
|
* a plain driver of the generator. `bindGenerator` keeps the context of the
|
|
5
|
-
* generator creation (rule C5), with `
|
|
5
|
+
* generator creation (rule C5), with an `AsyncResource`.
|
|
6
|
+
*
|
|
7
|
+
* An `AsyncResource` records the context as `AsyncLocalStorage.snapshot()` does,
|
|
8
|
+
* but at a much lower cost. `snapshot()` is `AsyncResource.bind()`, which also
|
|
9
|
+
* defines properties on a new function (approximately 3 µs against 30 ns on
|
|
10
|
+
* Node.js 24). The drivers record the context again after each step.
|
|
6
11
|
*
|
|
7
12
|
* @packageDocumentation
|
|
8
13
|
*/
|
|
9
|
-
import {
|
|
14
|
+
import { AsyncResource } from "node:async_hooks";
|
|
10
15
|
import { createAsyncGenerator } from "../core/async-generator.js";
|
|
11
|
-
|
|
16
|
+
import { copyNameAndLength } from "../core/function-name.js";
|
|
17
|
+
export { asyncGeneratorDelegate, awaitAsyncGenerator, OverloadYield } from "../core/async-generator.js";
|
|
18
|
+
/** The type of the async resources of the generators, for async_hooks. */
|
|
19
|
+
const RESOURCE_TYPE = "async-browser-context:generator";
|
|
12
20
|
/**
|
|
13
21
|
* This function gives an async function that operates `generatorFunction`. The function is
|
|
14
22
|
* a native async function: each `await` of the source becomes one native `await`, with
|
|
15
|
-
* the same microtask turns. An error of the parameters rejects the promise.
|
|
23
|
+
* the same microtask turns. An error of the parameters rejects the promise. The function has the
|
|
24
|
+
* name and the length of the source, as in the browser runtime.
|
|
16
25
|
*/
|
|
17
|
-
export function coroutine(generatorFunction) {
|
|
18
|
-
|
|
26
|
+
export function coroutine(generatorFunction, name) {
|
|
27
|
+
const asyncFunction = async function asyncFunction(...args) {
|
|
19
28
|
// The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
|
|
20
29
|
"async-browser-context runtime";
|
|
21
30
|
const generator = generatorFunction.apply(this, args);
|
|
@@ -33,16 +42,18 @@ export function coroutine(generatorFunction) {
|
|
|
33
42
|
}
|
|
34
43
|
return result.value;
|
|
35
44
|
};
|
|
45
|
+
copyNameAndLength(asyncFunction, name, generatorFunction.length);
|
|
46
|
+
return asyncFunction;
|
|
36
47
|
}
|
|
37
48
|
/**
|
|
38
49
|
* This function gives a function that makes an async generator object, which operates
|
|
39
50
|
* `generatorFunction`. The preset gives it in place of the helper `wrapAsyncGenerator` of
|
|
40
|
-
* Babel. The driver starts in
|
|
41
|
-
*
|
|
51
|
+
* Babel. The driver starts in the context of the creation (rule C5). The native
|
|
52
|
+
* `AsyncLocalStorage` keeps that context through the `await` of the driver.
|
|
42
53
|
*/
|
|
43
|
-
export function asyncGenerator(generatorFunction) {
|
|
44
|
-
|
|
45
|
-
let
|
|
54
|
+
export function asyncGenerator(generatorFunction, name) {
|
|
55
|
+
const makeAsyncGenerator = function makeAsyncGenerator(...args) {
|
|
56
|
+
let context = new AsyncResource(RESOURCE_TYPE);
|
|
46
57
|
const generator = generatorFunction.apply(this, args);
|
|
47
58
|
const step = (key, argument) => {
|
|
48
59
|
try {
|
|
@@ -50,23 +61,25 @@ export function asyncGenerator(generatorFunction) {
|
|
|
50
61
|
}
|
|
51
62
|
finally {
|
|
52
63
|
// The body can change its context with enterWith before it stops
|
|
53
|
-
|
|
64
|
+
context = new AsyncResource(RESOURCE_TYPE);
|
|
54
65
|
}
|
|
55
66
|
};
|
|
56
|
-
return createAsyncGenerator(step, Promise, (start) =>
|
|
67
|
+
return createAsyncGenerator(step, Promise, (start) => context.runInAsyncScope(start));
|
|
57
68
|
};
|
|
69
|
+
copyNameAndLength(makeAsyncGenerator, name, generatorFunction.length);
|
|
70
|
+
return makeAsyncGenerator;
|
|
58
71
|
}
|
|
59
72
|
/** This function binds a generator object to the current context (rule C5). */
|
|
60
73
|
export function bindGenerator(generator) {
|
|
61
|
-
let
|
|
74
|
+
let context = new AsyncResource(RESOURCE_TYPE);
|
|
62
75
|
const target = generator;
|
|
63
|
-
const resume = (method, argument) =>
|
|
76
|
+
const resume = (method, argument) => context.runInAsyncScope(() => {
|
|
64
77
|
try {
|
|
65
78
|
return target[method](argument);
|
|
66
79
|
}
|
|
67
80
|
finally {
|
|
68
81
|
// The body can change its context with enterWith before it stops
|
|
69
|
-
|
|
82
|
+
context = new AsyncResource(RESOURCE_TYPE);
|
|
70
83
|
}
|
|
71
84
|
});
|
|
72
85
|
const methods = {
|
package/dist/node/runtime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.js","sourceRoot":"","sources":["../../src/node/runtime.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"runtime.js","sourceRoot":"","sources":["../../src/node/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,oBAAoB,EAAkB,MAAM,4BAA4B,CAAC;AAClF,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAE7D,OAAO,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAExG,0EAA0E;AAC1E,MAAM,aAAa,GAAG,iCAAiC,CAAC;AAExD;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CACrB,iBAA6E,EAC7E,IAAc;IAEd,MAAM,aAAa,GAAG,KAAK,UAAU,aAAa,CAAW,GAAG,IAAQ;QACpE,+FAA+F;QAC/F,+BAA+B,CAAC;QAChC,MAAM,SAAS,GAAG,iBAAiB,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACtD,IAAI,MAAM,GAAG,SAAS,CAAC,IAAI,EAAE,CAAC;QAC9B,OAAO,MAAM,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;YAC1B,IAAI,KAAe,CAAC;YACpB,IAAI,CAAC;gBACD,KAAK,GAAG,MAAM,MAAM,CAAC,KAAK,CAAC;YAC/B,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACb,MAAM,GAAG,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;gBAChC,SAAS;YACb,CAAC;YACD,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACnC,CAAC;QACD,OAAO,M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AAM,WAAW,cAAc;IAC3B,QAAQ,IAAK,OAAO,GAAG,SAAS,CAAC;IACjC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAG,OAAO,EAAE,EAAE,EAAG,MAAM,CAAC,GAAI,CAAC,CAAC;IAC1C,OAAO,IAAK,IAAI,CAAC;CACpB;AAED,6CAA6C;AAC7C,MAAM,MAAM,wBAAwB,GAAG,UAAU,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"context-manager.d.ts","sourceRoot":"","sources":["../../src/opentelemetry/context-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAErF,wEAAwE;AACxE,MAAM,WAAW,cAAc;IAC3B,QAAQ,IAAK,OAAO,GAAG,SAAS,CAAC;IACjC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAG,OAAO,EAAE,EAAE,EAAG,MAAM,CAAC,GAAI,CAAC,CAAC;IAC1C,OAAO,IAAK,IAAI,CAAC;CACpB;AAED,6CAA6C;AAC7C,MAAM,MAAM,wBAAwB,GAAG,UAAU,cAAc,CAAC;AA+ChE;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,aAAa,EAAG,MAAM,cAAc,GAAI,wBAAwB,CAuKpG"}
|