async-browser-context 0.2.4 → 0.3.1
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 +94 -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 +26 -8
- package/dist/core/async-generator.d.ts.map +1 -1
- package/dist/core/async-generator.js +162 -44
- 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 +35 -21
- 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 +4 -3
- package/dist/patches/handler-interfaces.d.ts.map +1 -1
- package/dist/patches/handler-interfaces.js +7 -5
- 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 +52 -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 +169 -46
- package/src/core/async-local-storage.ts +3 -0
- package/src/core/callback-hook.ts +3 -3
- package/src/core/coroutine.ts +35 -21
- 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 +7 -5
- 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
|
@@ -2,33 +2,115 @@
|
|
|
2
2
|
* The async generators of transformed code. `@babel/plugin-transform-async-generator-functions`
|
|
3
3
|
* changes each async generator into a sync generator and its helper `wrapAsyncGenerator`.
|
|
4
4
|
* The helper continued each step in a `then` callback, and a `then` callback is slow in
|
|
5
|
-
* Firefox. The preset replaces the helper with `asyncGenerator` of the runtime
|
|
6
|
-
*
|
|
7
|
-
* continues each step with a native `await`.
|
|
5
|
+
* Firefox. The preset replaces the helper with `asyncGenerator` of the runtime. It also takes
|
|
6
|
+
* the helpers `OverloadYield`, `awaitAsyncGenerator` and `asyncGeneratorDelegate` from this
|
|
7
|
+
* module. The driver in this module then continues each step with a native `await`.
|
|
8
8
|
*
|
|
9
9
|
* The protocol is the protocol of the Babel 8 helper: the body gives
|
|
10
10
|
* `new OverloadYield(value, 0)` for an `await`, and `new OverloadYield(promise, 1)` for a
|
|
11
11
|
* step of `yield*`. Each other value is a `yield`. Where the helper differs from a native async
|
|
12
12
|
* generator, the driver does as the native generator. Thus `return()` at a `yield` awaits its
|
|
13
|
-
* value first, and a request on a complete generator settles at once.
|
|
13
|
+
* value first, and a request on a complete generator settles at once. The delegate of this
|
|
14
|
+
* module also gives kind 2 for a step of `yield*` after `return()`.
|
|
14
15
|
*/
|
|
15
16
|
|
|
16
17
|
/** The Promise of the time when the module loads, for the rejections of the methods. */
|
|
17
18
|
const NativePromise : PromiseConstructor = Promise;
|
|
18
19
|
|
|
19
20
|
/**
|
|
20
|
-
* The value that the body gives for an `await` (kind 0)
|
|
21
|
-
*
|
|
21
|
+
* The value that the body gives for an `await` (kind 0), for a step of `yield*` (kind 1), or
|
|
22
|
+
* for a step of `yield*` after `return()` (kind 2). The helpers `awaitAsyncGenerator` and
|
|
23
|
+
* `asyncGeneratorDelegate` make it with `new`.
|
|
22
24
|
*/
|
|
23
25
|
export class OverloadYield {
|
|
24
26
|
readonly v : unknown;
|
|
25
|
-
readonly k : 0 | 1;
|
|
26
|
-
constructor(value : unknown, kind : 0 | 1) {
|
|
27
|
+
readonly k : 0 | 1 | 2;
|
|
28
|
+
constructor(value : unknown, kind : 0 | 1 | 2) {
|
|
27
29
|
this.v = value;
|
|
28
30
|
this.k = kind;
|
|
29
31
|
}
|
|
30
32
|
}
|
|
31
33
|
|
|
34
|
+
/**
|
|
35
|
+
* The error of a step of `yield*` that was rejected. The driver gives it back to the delegate,
|
|
36
|
+
* and the delegate throws the error. Thus the error goes into the body at the `yield*`, also
|
|
37
|
+
* when the delegate has no `throw` method.
|
|
38
|
+
*/
|
|
39
|
+
class DelegateRejection {
|
|
40
|
+
readonly error : unknown;
|
|
41
|
+
constructor(error : unknown) {
|
|
42
|
+
this.error = error;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The helper `awaitAsyncGenerator` of Babel: the body gives its value for an `await`. The preset
|
|
48
|
+
* gives this function in place of the helper of Babel. Thus the marker is always the class of this
|
|
49
|
+
* module, also when `@babel/plugin-transform-runtime` gives the other helpers.
|
|
50
|
+
*/
|
|
51
|
+
export function awaitAsyncGenerator(value : unknown) : OverloadYield {
|
|
52
|
+
return new OverloadYield(value, 0);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The helper `asyncGeneratorDelegate` of Babel 8: a sync iterator for `yield*` in an async
|
|
57
|
+
* generator. Each step of the inner async iterator becomes `new OverloadYield(promise, 1)`, or
|
|
58
|
+
* kind 2 after `return()`. The driver awaits the promise, and gives a copy of its result back to
|
|
59
|
+
* the iterator. After kind 2, the driver gives it back with `return()`. Thus a complete inner
|
|
60
|
+
* iterator ends the body, as with a native `yield*`.
|
|
61
|
+
*/
|
|
62
|
+
export function asyncGeneratorDelegate(inner : AsyncIterator<unknown, unknown, unknown>) : Iterator<unknown, unknown, unknown> {
|
|
63
|
+
const iterator = inner as unknown as Record<ResumeKey, ((value : unknown) => unknown) | undefined>;
|
|
64
|
+
let waiting = false;
|
|
65
|
+
const pump = (key : ResumeKey, value : unknown) : IteratorResult<unknown, unknown> => {
|
|
66
|
+
waiting = true;
|
|
67
|
+
const promise = new NativePromise((resolve) => {
|
|
68
|
+
resolve(Reflect.apply(iterator[key] as (value : unknown) => unknown, inner, [value]));
|
|
69
|
+
});
|
|
70
|
+
return { done : false, value : new OverloadYield(promise, key === "return" ? 2 : 1) };
|
|
71
|
+
};
|
|
72
|
+
const delegate : Record<PropertyKey, unknown> = {
|
|
73
|
+
[Symbol.iterator]() { return this; },
|
|
74
|
+
next(value : unknown) : unknown {
|
|
75
|
+
if (!waiting) {
|
|
76
|
+
return pump("next", value);
|
|
77
|
+
}
|
|
78
|
+
waiting = false;
|
|
79
|
+
if (value instanceof DelegateRejection) {
|
|
80
|
+
throw value.error;
|
|
81
|
+
}
|
|
82
|
+
return value;
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
delegate["throw"] = (value : unknown) : unknown => {
|
|
86
|
+
if (typeof iterator.throw === "function") {
|
|
87
|
+
return pump("throw", value);
|
|
88
|
+
}
|
|
89
|
+
// Without a throw method, a native yield* closes the inner iterator and awaits its return()
|
|
90
|
+
// (AsyncIteratorClose). An error of return() goes into the body, else a TypeError.
|
|
91
|
+
waiting = true;
|
|
92
|
+
const closed = new NativePromise<never>((resolve) => {
|
|
93
|
+
resolve(typeof iterator.return === "function" ? Reflect.apply(iterator.return, inner, []) : undefined);
|
|
94
|
+
}).then((result : unknown) : never => {
|
|
95
|
+
if (typeof iterator.return === "function" && !isObject(result)) {
|
|
96
|
+
throw new TypeError("The result of return() of the iterator of yield* is not an object");
|
|
97
|
+
}
|
|
98
|
+
throw new TypeError("The iterator of yield* has no throw method");
|
|
99
|
+
});
|
|
100
|
+
return { done : false, value : new OverloadYield(closed, 1) };
|
|
101
|
+
};
|
|
102
|
+
if (typeof iterator.return === "function") {
|
|
103
|
+
delegate["return"] = (value : unknown) : unknown => {
|
|
104
|
+
if (waiting) {
|
|
105
|
+
waiting = false;
|
|
106
|
+
return value;
|
|
107
|
+
}
|
|
108
|
+
return pump("return", value);
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
return delegate as unknown as Iterator<unknown, unknown, unknown>;
|
|
112
|
+
}
|
|
113
|
+
|
|
32
114
|
/**
|
|
33
115
|
* A value that is not a thenable, in a box. The browser runtime reads the `then` property of an
|
|
34
116
|
* awaited object one time, in the context of the body. When the object has no `then` method,
|
|
@@ -47,6 +129,11 @@ export function unbox(value : unknown) : unknown {
|
|
|
47
129
|
return value instanceof PlainValue ? value.value : value;
|
|
48
130
|
}
|
|
49
131
|
|
|
132
|
+
/** This function gives `true` for a value that can have properties: an object or a function. */
|
|
133
|
+
export function isObject(value : unknown) : value is object {
|
|
134
|
+
return (typeof value === "object" && value !== null) || typeof value === "function";
|
|
135
|
+
}
|
|
136
|
+
|
|
50
137
|
/** The methods of a generator that a request of the consumer starts. */
|
|
51
138
|
export type ResumeKey = "next" | "throw" | "return";
|
|
52
139
|
|
|
@@ -87,27 +174,31 @@ function request(target : unknown, key : ResumeKey, argument : unknown) : Promis
|
|
|
87
174
|
}
|
|
88
175
|
|
|
89
176
|
/**
|
|
90
|
-
* This function gives %AsyncIteratorPrototype% of the engine
|
|
91
|
-
* `Symbol.asyncDispose` and the async iterator
|
|
92
|
-
* generator
|
|
177
|
+
* This function gives %AsyncIteratorPrototype% of the engine from an async generator function.
|
|
178
|
+
* The prototype gives `Symbol.asyncIterator`, `Symbol.asyncDispose` and the async iterator
|
|
179
|
+
* helpers. If a compiler changed the async generator function, the function gives `undefined`.
|
|
180
|
+
*
|
|
181
|
+
* @internal
|
|
93
182
|
*/
|
|
94
|
-
function
|
|
183
|
+
export function asyncIteratorPrototypeOf(generatorFunction : Function) : object | undefined {
|
|
184
|
+
let prototype : Record<symbol, unknown> | null;
|
|
95
185
|
try {
|
|
96
|
-
|
|
97
|
-
// The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
|
|
98
|
-
// Stryker disable next-line all: the directive matters only when a bundler applies the preset to the runtime
|
|
99
|
-
"async-browser-context runtime";
|
|
100
|
-
};
|
|
101
|
-
const prototype = Object.getPrototypeOf(Object.getPrototypeOf(generatorFunction.prototype) as object) as Record<symbol, unknown> | null;
|
|
102
|
-
return prototype !== null && typeof prototype[Symbol.asyncIterator] === "function" ? prototype : undefined;
|
|
186
|
+
prototype = Object.getPrototypeOf(Object.getPrototypeOf(generatorFunction.prototype) as object) as Record<symbol, unknown> | null;
|
|
103
187
|
} catch {
|
|
188
|
+
// A function without a prototype object, for example an arrow function
|
|
104
189
|
return undefined;
|
|
105
190
|
}
|
|
191
|
+
return prototype !== null && typeof prototype[Symbol.asyncIterator] === "function" ? prototype : undefined;
|
|
106
192
|
}
|
|
107
193
|
|
|
108
|
-
/**
|
|
109
|
-
|
|
110
|
-
|
|
194
|
+
/**
|
|
195
|
+
* This function makes the prototype of the async generator objects of transformed code. Its
|
|
196
|
+
* methods are not enumerable, as native methods. Without `parent`, the prototype inherits from
|
|
197
|
+
* `Object.prototype` and gets its own `Symbol.asyncIterator` method.
|
|
198
|
+
*
|
|
199
|
+
* @internal
|
|
200
|
+
*/
|
|
201
|
+
export function makeAsyncGeneratorPrototype(parent : object | undefined) : object {
|
|
111
202
|
const prototype = Object.create(parent ?? Object.prototype) as object;
|
|
112
203
|
const methods : Record<ResumeKey, (this : unknown, argument? : unknown) => Promise<IteratorResult<unknown, unknown>>> = {
|
|
113
204
|
next(value) { return request(this, "next", value); },
|
|
@@ -122,7 +213,29 @@ const AsyncGeneratorPrototype : object = (() => {
|
|
|
122
213
|
}
|
|
123
214
|
Object.defineProperty(prototype, Symbol.toStringTag, { value : "AsyncGenerator", configurable : true });
|
|
124
215
|
return prototype;
|
|
125
|
-
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* This function gives %AsyncIteratorPrototype% of the engine, or `undefined`. Another preset in
|
|
220
|
+
* the same Babel configuration, for example `next/babel` with its preset-env, can change the
|
|
221
|
+
* async generator below in the runtime itself. Its helper is then a module that loads at this
|
|
222
|
+
* time, and the call can throw. The catch keeps the runtime available, with a prototype
|
|
223
|
+
* without the parent of the engine.
|
|
224
|
+
*/
|
|
225
|
+
function engineAsyncIteratorPrototype() : object | undefined {
|
|
226
|
+
try {
|
|
227
|
+
return asyncIteratorPrototypeOf(async function* () {
|
|
228
|
+
// The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
|
|
229
|
+
// Stryker disable next-line all: the directive matters only when a bundler applies the preset to the runtime
|
|
230
|
+
"async-browser-context runtime";
|
|
231
|
+
});
|
|
232
|
+
} catch {
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** The prototype of the async generator objects of transformed code. */
|
|
238
|
+
const AsyncGeneratorPrototype : object = makeAsyncGeneratorPrototype(engineAsyncIteratorPrototype());
|
|
126
239
|
|
|
127
240
|
/**
|
|
128
241
|
* This function makes an async generator object. The object queues the requests of the
|
|
@@ -137,8 +250,10 @@ export function createAsyncGenerator(
|
|
|
137
250
|
) : AsyncGenerator<unknown, unknown, unknown> {
|
|
138
251
|
let first : Request | undefined;
|
|
139
252
|
let last : Request | undefined;
|
|
140
|
-
/**
|
|
141
|
-
let
|
|
253
|
+
/** `true` after the first step of the body. While a step operates, the requests wait in the queue. */
|
|
254
|
+
let started = false;
|
|
255
|
+
/** `true` when the body is complete. */
|
|
256
|
+
let complete = false;
|
|
142
257
|
|
|
143
258
|
/** This function starts the requests in the queue, one after the other, until the queue is empty. */
|
|
144
259
|
const run = async (key : ResumeKey, argument : unknown) : Promise<void> => {
|
|
@@ -149,12 +264,10 @@ export function createAsyncGenerator(
|
|
|
149
264
|
for (;;) {
|
|
150
265
|
let failed = false;
|
|
151
266
|
let settled : unknown;
|
|
152
|
-
let done = false;
|
|
153
267
|
try {
|
|
154
|
-
if (
|
|
268
|
+
if (complete || (!started && key !== "next")) {
|
|
155
269
|
// The body does not operate: it is complete, or return() or throw() came before the start
|
|
156
|
-
|
|
157
|
-
done = true;
|
|
270
|
+
complete = true;
|
|
158
271
|
if (key === "throw") {
|
|
159
272
|
throw argument;
|
|
160
273
|
}
|
|
@@ -163,6 +276,7 @@ export function createAsyncGenerator(
|
|
|
163
276
|
settled = await argument;
|
|
164
277
|
}
|
|
165
278
|
} else {
|
|
279
|
+
started = true;
|
|
166
280
|
if (key === "return") {
|
|
167
281
|
// At a yield, return() awaits its value first. A rejection goes into the body at the yield.
|
|
168
282
|
try {
|
|
@@ -174,43 +288,52 @@ export function createAsyncGenerator(
|
|
|
174
288
|
resumed = true;
|
|
175
289
|
}
|
|
176
290
|
let result = step(key, argument, resumed);
|
|
291
|
+
// return() at a yield already awaited its value. If the body completes with that value, a native generator does not await it again.
|
|
292
|
+
if (key === "return" && result.done === true && result.value === argument) {
|
|
293
|
+
settled = argument;
|
|
294
|
+
complete = true;
|
|
295
|
+
result = { done : true, value : undefined };
|
|
296
|
+
}
|
|
177
297
|
for (;;) {
|
|
298
|
+
if (complete) {
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
178
301
|
const yielded : unknown = result.value;
|
|
179
302
|
const overloaded = yielded instanceof OverloadYield;
|
|
180
303
|
let value : unknown;
|
|
181
304
|
try {
|
|
182
305
|
value = unbox(await prepare(overloaded ? yielded.v : yielded));
|
|
183
306
|
} catch (error) {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
result = step("throw", error, true);
|
|
307
|
+
// The error goes into the body at the await or the yield. The delegate of a yield* throws it.
|
|
308
|
+
result = overloaded && yielded.k !== 0 ? step("next", new DelegateRejection(error), true) : step("throw", error, true);
|
|
187
309
|
continue;
|
|
188
310
|
}
|
|
189
311
|
resumed = true;
|
|
190
312
|
if (overloaded) {
|
|
191
|
-
const
|
|
192
|
-
if (yielded.k === 0 ||
|
|
193
|
-
|
|
194
|
-
result = step(
|
|
313
|
+
const resumeKey : ResumeKey = yielded.k === 2 ? "return" : "next";
|
|
314
|
+
if (yielded.k === 0 || !isObject(value)) {
|
|
315
|
+
// The value of an await. For a result of yield* that is not an object, the body throws a TypeError.
|
|
316
|
+
result = step(resumeKey, value, true);
|
|
195
317
|
continue;
|
|
196
318
|
}
|
|
197
|
-
// A step of yield
|
|
198
|
-
//
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
|
|
319
|
+
// A step of yield*. As a native yield*, the driver reads done and value one time, and the body gets a copy.
|
|
320
|
+
// The driver does not compare the result of the body with the copy, because the yield* of WebKit makes a new object.
|
|
321
|
+
const done = Boolean((value as { done? : unknown }).done);
|
|
322
|
+
const item = (value as { value? : unknown }).value;
|
|
323
|
+
result = step(resumeKey, { value : item, done }, true);
|
|
324
|
+
if (done) {
|
|
202
325
|
continue;
|
|
203
326
|
}
|
|
204
|
-
|
|
327
|
+
// The body gives the result back. Its value is not awaited, as with a native yield*.
|
|
328
|
+
value = item;
|
|
205
329
|
}
|
|
206
330
|
settled = value;
|
|
207
|
-
|
|
331
|
+
complete = result.done === true;
|
|
208
332
|
break;
|
|
209
333
|
}
|
|
210
|
-
state = done ? "done" : "yield";
|
|
211
334
|
}
|
|
212
335
|
} catch (error) {
|
|
213
|
-
|
|
336
|
+
complete = true;
|
|
214
337
|
failed = true;
|
|
215
338
|
settled = error;
|
|
216
339
|
}
|
|
@@ -220,7 +343,7 @@ export function createAsyncGenerator(
|
|
|
220
343
|
if (failed) {
|
|
221
344
|
current.reject(settled);
|
|
222
345
|
} else {
|
|
223
|
-
current.resolve({ value : settled, done });
|
|
346
|
+
current.resolve({ value : settled, done : complete });
|
|
224
347
|
}
|
|
225
348
|
if (first === undefined) {
|
|
226
349
|
last = undefined;
|
|
@@ -63,6 +63,9 @@ export class AsyncLocalStorage<T> {
|
|
|
63
63
|
* This method sets `store` for the rest of the current synchronous step, and for the
|
|
64
64
|
* code that this step starts later. Prefer `run()`: `enterWith()` can also
|
|
65
65
|
* change the store of the code that called the current function.
|
|
66
|
+
*
|
|
67
|
+
* Outside all steps, for example at the top level of a module, the store stays after the
|
|
68
|
+
* task as a default store, as on Node.js. No code sets the root context again.
|
|
66
69
|
*/
|
|
67
70
|
enterWith(store : T) : void {
|
|
68
71
|
this.#enabled = true;
|
|
@@ -8,9 +8,9 @@ export interface CallbackHook {
|
|
|
8
8
|
/**
|
|
9
9
|
* The library starts this method before the callback.
|
|
10
10
|
*
|
|
11
|
-
* @param callback - The function that the library starts. For
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* @param callback - The function that the library starts. For a step
|
|
12
|
+
* after an `await`, it is the body of the async function or the async
|
|
13
|
+
* generator. It has the name of that function.
|
|
14
14
|
* @param source - The API that got the callback, for example
|
|
15
15
|
* `"setTimeout"`, `"Promise.then"`, `"addEventListener:click"`,
|
|
16
16
|
* `"onclick"` or `"await"`.
|
package/src/core/coroutine.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { createAsyncGenerator, PlainValue, unbox } from "./async-generator.js";
|
|
2
|
-
import {
|
|
1
|
+
import { createAsyncGenerator, isObject, PlainValue, unbox } from "./async-generator.js";
|
|
2
|
+
import { copyNameAndLength } from "./function-name.js";
|
|
3
|
+
import { applyQuietly, applyWithHooks, store, type Frame } from "./store.js";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* The code from the Babel preset uses these runtime functions. Do not use them
|
|
@@ -14,26 +15,20 @@ import { applyWithHooks, store, type Frame } from "./store.js";
|
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* This function gives the generator function to the callback hooks, with the name of the
|
|
17
|
-
* async function. The preset gives the name, because the generator function
|
|
18
|
+
* async function or the async generator. The preset gives the name, because the generator function
|
|
18
19
|
* of Babel has no name. The function sets the name only when a hook operates.
|
|
19
20
|
*/
|
|
20
21
|
function namedForHooks(generatorFunction : Function, name : string | undefined) : Function {
|
|
21
|
-
if (name
|
|
22
|
+
if (name === undefined) {
|
|
23
|
+
return generatorFunction;
|
|
24
|
+
}
|
|
25
|
+
// Stryker disable next-line ConditionalExpression: the check only prevents a second change to the same name
|
|
26
|
+
if (generatorFunction.name !== name) {
|
|
22
27
|
Object.defineProperty(generatorFunction, "name", { value : name, configurable : true });
|
|
23
28
|
}
|
|
24
29
|
return generatorFunction;
|
|
25
30
|
}
|
|
26
31
|
|
|
27
|
-
/**
|
|
28
|
-
* This function gives an async function that operates `generatorFunction`. The preset gives
|
|
29
|
-
* this function to `@babel/plugin-transform-async-to-generator` as its
|
|
30
|
-
* `method` option, and adds `name`, the name of the async function, if it has one.
|
|
31
|
-
*/
|
|
32
|
-
/** This function gives `true` for a value that can have a `then` property: an object or a function. */
|
|
33
|
-
function isObject(value : unknown) : value is object {
|
|
34
|
-
return (typeof value === "object" && value !== null) || typeof value === "function";
|
|
35
|
-
}
|
|
36
|
-
|
|
37
32
|
/**
|
|
38
33
|
* This function gives the value that a driver awaits or gives as its result, in place of `value`. It
|
|
39
34
|
* operates in the context of the body, as `await` reads the `then` property there:
|
|
@@ -80,15 +75,20 @@ function awaitable(value : unknown, NativePromise : PromiseConstructor, box : bo
|
|
|
80
75
|
* This function gives an async function that operates `generatorFunction`. The preset gives
|
|
81
76
|
* this function to `@babel/plugin-transform-async-to-generator` as its
|
|
82
77
|
* `method` option, and adds `name`, the name of the async function, if it has one.
|
|
78
|
+
*
|
|
79
|
+
* The first step of the body operates in the step of the caller. Thus an `enterWith()` of that
|
|
80
|
+
* step stays for the caller, as on Node.js.
|
|
83
81
|
*/
|
|
84
82
|
export function coroutine<T, A extends unknown[], R>(
|
|
85
83
|
generatorFunction : (this : T, ...args : A) => Generator<unknown, R, unknown>,
|
|
86
84
|
name? : string,
|
|
87
85
|
) : (this : T, ...args : A) => Promise<R> {
|
|
88
86
|
const NativePromise = store.intrinsics.Promise;
|
|
89
|
-
|
|
87
|
+
const asyncFunction = function asyncFunction(this : T, ...args : A) : Promise<R> {
|
|
90
88
|
// The context of the call. The body can change it with AsyncLocalStorage.enterWith.
|
|
91
89
|
let frame : Frame = store.current;
|
|
90
|
+
// An async function that a hook starts does not start the hooks (store.ts, applyQuietly)
|
|
91
|
+
const quiet = store.inHook === true;
|
|
92
92
|
let generator : Generator<unknown, R, unknown>;
|
|
93
93
|
const next = (value : unknown) : IteratorResult<unknown, R> => generator.next(value);
|
|
94
94
|
const throwInto = (error : unknown) : IteratorResult<unknown, R> => generator.throw(error);
|
|
@@ -105,9 +105,11 @@ export function coroutine<T, A extends unknown[], R>(
|
|
|
105
105
|
store.current = frame;
|
|
106
106
|
try {
|
|
107
107
|
const hooks = resumed ? store.hooks : undefined;
|
|
108
|
-
const result =
|
|
109
|
-
? resume
|
|
110
|
-
:
|
|
108
|
+
const result = quiet
|
|
109
|
+
? applyQuietly(resume, undefined, [argument])
|
|
110
|
+
: hooks === undefined
|
|
111
|
+
? resume(argument)
|
|
112
|
+
: applyWithHooks(hooks, namedForHooks(generatorFunction, name), "await", resume, undefined, [argument]);
|
|
111
113
|
// The result object is new for each step, so the change is not visible to other code
|
|
112
114
|
(result as { value : unknown }).value = awaitable(result.value, NativePromise, result.done !== true);
|
|
113
115
|
return result;
|
|
@@ -143,22 +145,29 @@ export function coroutine<T, A extends unknown[], R>(
|
|
|
143
145
|
};
|
|
144
146
|
return drive();
|
|
145
147
|
};
|
|
148
|
+
// Where Babel gives no named wrapper, for example in a class field, the function keeps the name and the length of the source
|
|
149
|
+
copyNameAndLength(asyncFunction, name, generatorFunction.length);
|
|
150
|
+
return asyncFunction;
|
|
146
151
|
}
|
|
147
152
|
|
|
148
153
|
/**
|
|
149
154
|
* This function gives a function that makes an async generator object, which operates
|
|
150
155
|
* `generatorFunction`. The preset gives this function in place of the helper
|
|
151
|
-
* `wrapAsyncGenerator` of `@babel/plugin-transform-async-generator-functions`.
|
|
156
|
+
* `wrapAsyncGenerator` of `@babel/plugin-transform-async-generator-functions`. It adds `name`,
|
|
157
|
+
* the name of the async generator, if it has one.
|
|
152
158
|
*
|
|
153
159
|
* Each step of the body operates in the context of the call that made the generator
|
|
154
160
|
* (rule C5). The driver continues each step with a native `await` (`async-generator.ts`).
|
|
155
161
|
*/
|
|
156
162
|
export function asyncGenerator<T, A extends unknown[]>(
|
|
157
163
|
generatorFunction : (this : T, ...args : A) => Generator<unknown, unknown, unknown>,
|
|
164
|
+
name? : string,
|
|
158
165
|
) : (this : T, ...args : A) => AsyncGenerator<unknown, unknown, unknown> {
|
|
159
166
|
const NativePromise = store.intrinsics.Promise;
|
|
160
|
-
|
|
167
|
+
const makeAsyncGenerator = function makeAsyncGenerator(this : T, ...args : A) : AsyncGenerator<unknown, unknown, unknown> {
|
|
161
168
|
let frame : Frame = store.current;
|
|
169
|
+
// An async generator that a hook makes does not start the hooks (store.ts, applyQuietly)
|
|
170
|
+
const quiet = store.inHook === true;
|
|
162
171
|
const generator = Reflect.apply(generatorFunction, this, args);
|
|
163
172
|
const methods : Record<ResumeMethod, (argument : unknown) => IteratorResult<unknown, unknown>> = {
|
|
164
173
|
next : (argument) => generator.next(argument),
|
|
@@ -170,10 +179,13 @@ export function asyncGenerator<T, A extends unknown[]>(
|
|
|
170
179
|
const previous = store.current;
|
|
171
180
|
store.current = frame;
|
|
172
181
|
try {
|
|
182
|
+
if (quiet) {
|
|
183
|
+
return applyQuietly(methods[key], undefined, [argument]);
|
|
184
|
+
}
|
|
173
185
|
const hooks = resumed ? store.hooks : undefined;
|
|
174
186
|
return hooks === undefined
|
|
175
187
|
? methods[key](argument)
|
|
176
|
-
: applyWithHooks(hooks, generatorFunction, "await", methods[key], undefined, [argument]);
|
|
188
|
+
: applyWithHooks(hooks, namedForHooks(generatorFunction, name), "await", methods[key], undefined, [argument]);
|
|
177
189
|
} finally {
|
|
178
190
|
frame = store.current;
|
|
179
191
|
store.current = previous;
|
|
@@ -191,6 +203,8 @@ export function asyncGenerator<T, A extends unknown[]>(
|
|
|
191
203
|
};
|
|
192
204
|
return createAsyncGenerator(step, NativePromise, undefined, prepare);
|
|
193
205
|
};
|
|
206
|
+
copyNameAndLength(makeAsyncGenerator, name, generatorFunction.length);
|
|
207
|
+
return makeAsyncGenerator;
|
|
194
208
|
}
|
|
195
209
|
|
|
196
210
|
type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown, unknown, unknown>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This function gives the function of the runtime the `name` and the `length` of the source
|
|
3
|
+
* function, as the native function has them. Without a name, the name is the empty string, as
|
|
4
|
+
* for a native anonymous function. The preset gives the name, if the source has one. Where Babel
|
|
5
|
+
* gives no named wrapper, for example in a class field, the function of the runtime is the
|
|
6
|
+
* function that the program sees. The browser runtime and the Node.js runtime both use it.
|
|
7
|
+
*/
|
|
8
|
+
export function copyNameAndLength(fn : Function, name : string | undefined, length : number) : void {
|
|
9
|
+
Object.defineProperty(fn, "name", { value : name ?? "", configurable : true });
|
|
10
|
+
Object.defineProperty(fn, "length", { value : length, configurable : true });
|
|
11
|
+
}
|
package/src/core/hooks.ts
CHANGED
|
@@ -3,6 +3,25 @@ import { store, type HookEntry } from "./store.js";
|
|
|
3
3
|
|
|
4
4
|
export type { CallbackHook } from "./callback-hook.js";
|
|
5
5
|
|
|
6
|
+
let warned = false;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Version 0.1 of the library has no callback hooks. If a copy of version 0.1 installed some
|
|
10
|
+
* patches first, the callbacks of those patches do not start the hooks. The function tells
|
|
11
|
+
* this one time on the console. The patches of version 0.2 start the hooks, and the patch of
|
|
12
|
+
* `Function.prototype.toString` has no callbacks.
|
|
13
|
+
*/
|
|
14
|
+
function warnAboutOldPatches() : void {
|
|
15
|
+
if (warned) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const without = [...(store.oldCopyPatches ?? [])].filter((name) => name !== "Function.prototype.toString");
|
|
19
|
+
if (without.length > 0) {
|
|
20
|
+
warned = true;
|
|
21
|
+
console.warn(`async-browser-context: an older copy of the library installed these patches, and their callbacks do not start the callback hooks: ${without.join(", ")}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
6
25
|
/**
|
|
7
26
|
* This function adds a hook. The library starts its methods before and after each
|
|
8
27
|
* callback that it starts. These callbacks are the callbacks of the patched
|
|
@@ -12,18 +31,16 @@ export type { CallbackHook } from "./callback-hook.js";
|
|
|
12
31
|
* A tool can use the hook to measure the time of each callback, with the
|
|
13
32
|
* name of the callback and the values of its context. An error in a hook goes
|
|
14
33
|
* to `reportError()`, and the callback operates as usual. A callback that
|
|
15
|
-
* starts synchronously in a hook does not start the hook methods.
|
|
34
|
+
* starts synchronously in a hook does not start the hook methods. A callback
|
|
35
|
+
* that a hook schedules does not start them either, also later.
|
|
16
36
|
*/
|
|
17
37
|
export function addCallbackHook(hook : CallbackHook) : () => void {
|
|
18
38
|
assertCallbackHook(hook);
|
|
39
|
+
warnAboutOldPatches();
|
|
19
40
|
const entry : HookEntry = { hook };
|
|
20
41
|
store.hooks = [...(store.hooks ?? []), entry];
|
|
21
|
-
|
|
42
|
+
// A second removal finds no entry, and the list stays the same
|
|
22
43
|
return function removeCallbackHook() : void {
|
|
23
|
-
if (removed) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
removed = true;
|
|
27
44
|
const remaining = (store.hooks ?? []).filter((other) => other !== entry);
|
|
28
45
|
store.hooks = remaining.length === 0 ? undefined : remaining;
|
|
29
46
|
};
|
package/src/core/snapshot.ts
CHANGED
|
@@ -28,7 +28,8 @@ export class Snapshot {
|
|
|
28
28
|
/**
|
|
29
29
|
* This method records the current context and gives a wrapper of `fn`. The
|
|
30
30
|
* wrapper starts `fn` in the recorded context, with the `this` value and the arguments of
|
|
31
|
-
* each call.
|
|
31
|
+
* each call. The name of the wrapper is "wrapped" and the name of `fn`, and its length is
|
|
32
|
+
* the length of `fn`.
|
|
32
33
|
*/
|
|
33
34
|
static wrap<T, A extends unknown[], R>(fn : (this : T, ...args : A) => R) : (this : T, ...args : A) => R {
|
|
34
35
|
if (typeof fn !== "function") {
|
|
@@ -39,13 +40,17 @@ export class Snapshot {
|
|
|
39
40
|
const previous = store.current;
|
|
40
41
|
store.current = frame;
|
|
41
42
|
try {
|
|
42
|
-
return
|
|
43
|
+
return Reflect.apply(fn, this, args);
|
|
43
44
|
} finally {
|
|
44
45
|
store.current = previous;
|
|
45
46
|
}
|
|
46
47
|
};
|
|
47
|
-
|
|
48
|
+
// As CopyNameAndLength with the prefix "wrapped" in the TC39 draft
|
|
49
|
+
const name : unknown = fn.name;
|
|
50
|
+
Object.defineProperty(wrapped, "name", { value : `wrapped ${typeof name === "string" ? name : ""}`, configurable : true });
|
|
48
51
|
Object.defineProperty(wrapped, "length", { value : fn.length, configurable : true });
|
|
49
52
|
return wrapped;
|
|
50
53
|
}
|
|
51
54
|
}
|
|
55
|
+
|
|
56
|
+
Object.defineProperty(Snapshot.prototype, Symbol.toStringTag, { value : "AsyncContext.Snapshot", configurable : true });
|