async-browser-context 0.2.2 → 0.2.4

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.
Files changed (37) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +1 -1
  3. package/dist/babel/async-generator-helpers.d.ts +17 -0
  4. package/dist/babel/async-generator-helpers.d.ts.map +1 -0
  5. package/dist/babel/async-generator-helpers.js +40 -0
  6. package/dist/babel/async-generator-helpers.js.map +1 -0
  7. package/dist/babel/bind-generators.d.ts.map +1 -1
  8. package/dist/babel/bind-generators.js +17 -5
  9. package/dist/babel/bind-generators.js.map +1 -1
  10. package/dist/babel/preset.d.ts +6 -4
  11. package/dist/babel/preset.d.ts.map +1 -1
  12. package/dist/babel/preset.js +8 -4
  13. package/dist/babel/preset.js.map +1 -1
  14. package/dist/core/async-generator.d.ts +56 -0
  15. package/dist/core/async-generator.d.ts.map +1 -0
  16. package/dist/core/async-generator.js +211 -0
  17. package/dist/core/async-generator.js.map +1 -0
  18. package/dist/core/coroutine.d.ts +13 -3
  19. package/dist/core/coroutine.d.ts.map +1 -1
  20. package/dist/core/coroutine.js +116 -16
  21. package/dist/core/coroutine.js.map +1 -1
  22. package/dist/node/runtime.d.ts +8 -1
  23. package/dist/node/runtime.d.ts.map +1 -1
  24. package/dist/node/runtime.js +24 -0
  25. package/dist/node/runtime.js.map +1 -1
  26. package/dist/runtime.d.ts +4 -2
  27. package/dist/runtime.d.ts.map +1 -1
  28. package/dist/runtime.js +4 -2
  29. package/dist/runtime.js.map +1 -1
  30. package/package.json +1 -1
  31. package/src/babel/async-generator-helpers.ts +49 -0
  32. package/src/babel/bind-generators.ts +17 -5
  33. package/src/babel/preset.ts +8 -4
  34. package/src/core/async-generator.ts +247 -0
  35. package/src/core/coroutine.ts +117 -16
  36. package/src/node/runtime.ts +27 -0
  37. package/src/runtime.ts +4 -2
@@ -1,3 +1,4 @@
1
+ import { createAsyncGenerator, PlainValue, unbox } from "./async-generator.js";
1
2
  import { applyWithHooks, store } from "./store.js";
2
3
  /**
3
4
  * The code from the Babel preset uses these runtime functions. Do not use them
@@ -20,13 +21,65 @@ function namedForHooks(generatorFunction, name) {
20
21
  }
21
22
  return generatorFunction;
22
23
  }
24
+ /**
25
+ * This function gives an async function that operates `generatorFunction`. The preset gives
26
+ * this function to `@babel/plugin-transform-async-to-generator` as its
27
+ * `method` option, and adds `name`, the name of the async function, if it has one.
28
+ */
29
+ /** This function gives `true` for a value that can have a `then` property: an object or a function. */
30
+ function isObject(value) {
31
+ return (typeof value === "object" && value !== null) || typeof value === "function";
32
+ }
33
+ /**
34
+ * This function gives the value that a driver awaits or gives as its result, in place of `value`. It
35
+ * operates in the context of the body, as `await` reads the `then` property there:
36
+ *
37
+ * - A primitive value and a native promise stay the same. `await` does not read a `then`
38
+ * property of them.
39
+ * - For another object, the function reads `then` one time. A `then` method gets the current
40
+ * frame for its later call. Thus the method operates in the context of the body, as with
41
+ * the native `AsyncLocalStorage`. With `box`, an object without a `then` method goes into a
42
+ * box, so that the engine does not read `then` a second time.
43
+ * - When the read throws, the function gives a rejected promise: the error goes into the body.
44
+ */
45
+ function awaitable(value, NativePromise, box) {
46
+ if (!isObject(value)) {
47
+ return value;
48
+ }
49
+ let then;
50
+ try {
51
+ if (value instanceof NativePromise && value.constructor === NativePromise) {
52
+ return value;
53
+ }
54
+ then = value.then;
55
+ }
56
+ catch (error) {
57
+ return new NativePromise((_resolve, reject) => reject(error));
58
+ }
59
+ if (typeof then !== "function") {
60
+ return box ? new PlainValue(value) : value;
61
+ }
62
+ const frame = store.current;
63
+ return {
64
+ then(resolve, reject) {
65
+ const previous = store.current;
66
+ store.current = frame;
67
+ try {
68
+ return Reflect.apply(then, value, [resolve, reject]);
69
+ }
70
+ finally {
71
+ store.current = previous;
72
+ }
73
+ },
74
+ };
75
+ }
23
76
  /**
24
77
  * This function gives an async function that operates `generatorFunction`. The preset gives
25
78
  * this function to `@babel/plugin-transform-async-to-generator` as its
26
79
  * `method` option, and adds `name`, the name of the async function, if it has one.
27
80
  */
28
81
  export function coroutine(generatorFunction, name) {
29
- const { promiseResolve } = store.intrinsics;
82
+ const NativePromise = store.intrinsics.Promise;
30
83
  return function asyncFunction(...args) {
31
84
  // The context of the call. The body can change it with AsyncLocalStorage.enterWith.
32
85
  let frame = store.current;
@@ -34,9 +87,11 @@ export function coroutine(generatorFunction, name) {
34
87
  const next = (value) => generator.next(value);
35
88
  const throwInto = (error) => generator.throw(error);
36
89
  /**
37
- * This function does one step of the body in the context of the async function, and sets
38
- * the previous context again. `resumed` is `true` for a step after an `await`. Only these
39
- * steps start the hook methods. The function gives the value to await.
90
+ * This function does one step of the body in the context of the async function. `resumed`
91
+ * is `true` for a step after an `await`. Only these steps start the hook methods. After a
92
+ * resumed step, the function sets the previous context again. The first step operates in
93
+ * the code of the caller, so an `enterWith` of that step stays for the caller, as on
94
+ * Node.js. The function gives the value to await, or the result of the function.
40
95
  */
41
96
  const step = (resume, argument, resumed) => {
42
97
  const previous = store.current;
@@ -46,17 +101,13 @@ export function coroutine(generatorFunction, name) {
46
101
  const result = hooks === undefined
47
102
  ? resume(argument)
48
103
  : applyWithHooks(hooks, namedForHooks(generatorFunction, name), "await", resume, undefined, [argument]);
49
- const value = result.value;
50
- if (!result.done && ((typeof value === "object" && value !== null) || typeof value === "function")) {
51
- // Promise.resolve reads the "then" property of an object. Do it in the context of the body, as await does.
52
- // The result object is new for each step, so the change is not visible to other code.
53
- result.value = promiseResolve(value);
54
- }
104
+ // The result object is new for each step, so the change is not visible to other code
105
+ result.value = awaitable(result.value, NativePromise, result.done !== true);
55
106
  return result;
56
107
  }
57
108
  finally {
58
109
  frame = store.current;
59
- store.current = previous;
110
+ store.current = resumed ? previous : frame;
60
111
  }
61
112
  };
62
113
  /**
@@ -66,14 +117,15 @@ export function coroutine(generatorFunction, name) {
66
117
  */
67
118
  const drive = async () => {
68
119
  // The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
120
+ // Stryker disable next-line all: the directive matters only when a bundler applies the preset to the runtime
69
121
  "async-browser-context runtime";
70
122
  // In the driver, an error of the parameters rejects the promise, as with a native async function
71
- generator = generatorFunction.apply(this, args);
123
+ generator = Reflect.apply(generatorFunction, this, args);
72
124
  let result = step(next, undefined, false);
73
125
  while (result.done !== true) {
74
126
  let value;
75
127
  try {
76
- value = await result.value;
128
+ value = unbox(await result.value);
77
129
  }
78
130
  catch (error) {
79
131
  result = step(throwInto, error, true);
@@ -86,15 +138,63 @@ export function coroutine(generatorFunction, name) {
86
138
  return drive();
87
139
  };
88
140
  }
141
+ /**
142
+ * This function gives a function that makes an async generator object, which operates
143
+ * `generatorFunction`. The preset gives this function in place of the helper
144
+ * `wrapAsyncGenerator` of `@babel/plugin-transform-async-generator-functions`.
145
+ *
146
+ * Each step of the body operates in the context of the call that made the generator
147
+ * (rule C5). The driver continues each step with a native `await` (`async-generator.ts`).
148
+ */
149
+ export function asyncGenerator(generatorFunction) {
150
+ const NativePromise = store.intrinsics.Promise;
151
+ return function makeAsyncGenerator(...args) {
152
+ let frame = store.current;
153
+ const generator = Reflect.apply(generatorFunction, this, args);
154
+ const methods = {
155
+ next: (argument) => generator.next(argument),
156
+ throw: (argument) => generator.throw(argument),
157
+ return: (argument) => generator.return(argument),
158
+ };
159
+ /** One step of the body in the context of the generator. The result stays the same: `yield*` gives the result of the delegate back. */
160
+ const step = (key, argument, resumed) => {
161
+ const previous = store.current;
162
+ store.current = frame;
163
+ try {
164
+ const hooks = resumed ? store.hooks : undefined;
165
+ return hooks === undefined
166
+ ? methods[key](argument)
167
+ : applyWithHooks(hooks, generatorFunction, "await", methods[key], undefined, [argument]);
168
+ }
169
+ finally {
170
+ frame = store.current;
171
+ store.current = previous;
172
+ }
173
+ };
174
+ /** The value to await, in the context of the generator: `await` and `yield` read the `then` property of an object there. */
175
+ const prepare = (value) => {
176
+ const previous = store.current;
177
+ store.current = frame;
178
+ try {
179
+ return awaitable(value, NativePromise, true);
180
+ }
181
+ finally {
182
+ store.current = previous;
183
+ }
184
+ };
185
+ return createAsyncGenerator(step, NativePromise, undefined, prepare);
186
+ };
187
+ }
89
188
  /**
90
189
  * This function binds a generator object to the current context. The body of
91
190
  * the generator operates in the context of the call that made the generator, as the TC39
92
191
  * proposal specifies (`GeneratorStart` and `GeneratorResume`). After each step,
93
192
  * the context of the caller is current again.
94
193
  *
95
- * The preset changes each generator function so that it gives
96
- * `bindGenerator(generator)`. The function works with sync generators and
97
- * with the async generators of `@babel/plugin-transform-async-generator-functions`.
194
+ * The preset changes each sync generator function so that it gives
195
+ * `bindGenerator(generator)`. An async generator does not need it, because
196
+ * `asyncGenerator` keeps the context itself. The function also works with
197
+ * async generator objects, for example in the output of older versions.
98
198
  */
99
199
  export function bindGenerator(generator) {
100
200
  let frame = store.current;
@@ -1 +1 @@
1
- {"version":3,"file":"coroutine.js","sourceRoot":"","sources":["../../src/core/coroutine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,KAAK,EAAc,MAAM,YAAY,CAAC;AAE/D;;;;;;;;;GASG;AAEH;;;;GAIG;AACH,SAAS,aAAa,CAAC,iBAA4B,EAAE,IAAyB;IAC1E,IAAI,IAAI,KAAK,SAAS,IAAI,iBAAiB,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;QACxD,MAAM,CAAC,cAAc,CAAC,iBAAiB,EAAE,MAAM,EAAE,EAAE,KAAK,EAAG,IAAI,EAAE,YAAY,EAAG,IAAI,EAAE,CAAC,CAAC;IAC5F,CAAC;IACD,OAAO,iBAAiB,CAAC;AAC7B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,SAAS,CACrB,iBAA6E,EAC7E,IAAc;IAEd,MAAM,EAAE,cAAc,EAAE,GAAG,KAAK,CAAC,UAAU,CAAC;IAC5C,OAAO,SAAS,aAAa,CAAW,GAAG,IAAQ;QAC/C,oFAAoF;QACpF,IAAI,KAAK,GAAW,KAAK,CAAC,OAAO,CAAC;QAClC,IAAI,SAA0C,CAAC;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,KAAgB,IAAc,OAAO,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;KACzE,CAAC;IACF,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,SAAS,CAAW,CAAM,CAAC;IAC7E,MAAM,CAAC,gBAAgB,CAAC,KAAK,EAAE;QAC3B,IAAI,EAAG,EAAE,KAAK,EAAG,OAAO,CAAC,IAAI,EAAE,QAAQ,EAAG,IAAI,EAAE,YAAY,EAAG,IAAI,EAAE;QACrE,KAAK,EAAG,EAAE,KAAK,EAAG,OAAO,CAAC,KAAK,EAAE,QAAQ,EAAG,IAAI,EAAE,YAAY,EAAG,IAAI,EAAE;QACvE,MAAM,EAAG,EAAE,KAAK,EAAG,OAAO,CAAC,MAAM,EAAE,QAAQ,EAAG,IAAI,EAAE,YAAY,EAAG,IAAI,EAAE;KAC5E,CAAC,CAAC;IACH,OAAO,KAAK,CAAC;AACjB,CAAC"}
@@ -1,11 +1,18 @@
1
+ export { OverloadYield } from "../core/async-generator.js";
1
2
  /**
2
3
  * This function gives an async function that operates `generatorFunction`. The function is
3
4
  * a native async function: each `await` of the source becomes one native `await`, with
4
5
  * the same microtask turns. An error of the parameters rejects the promise.
5
6
  */
6
7
  export declare function coroutine<T, A extends unknown[], R>(generatorFunction: (this: T, ...args: A) => Generator<unknown, R, unknown>): (this: T, ...args: A) => Promise<R>;
8
+ /**
9
+ * This function gives a function that makes an async generator object, which operates
10
+ * `generatorFunction`. The preset gives it in place of the helper `wrapAsyncGenerator` of
11
+ * Babel. The driver starts in a snapshot of the context of the creation (rule C5). The
12
+ * native `AsyncLocalStorage` keeps that context through the `await` of the driver.
13
+ */
14
+ export declare function asyncGenerator<T, A extends unknown[]>(generatorFunction: (this: T, ...args: A) => Generator<unknown, unknown, unknown>): (this: T, ...args: A) => AsyncGenerator<unknown, unknown, unknown>;
7
15
  type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown, unknown, unknown>;
8
16
  /** This function binds a generator object to the current context (rule C5). */
9
17
  export declare function bindGenerator<I extends AnyIterator>(generator: I): I;
10
- export {};
11
18
  //# sourceMappingURL=runtime.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/node/runtime.ts"],"names":[],"mappings":"AAUA;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,EAAE,CAAC,EAAE,iBAAiB,EAAG,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,SAAS,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,GAAI,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAkB1K;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"}
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/node/runtime.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,EAAE,CAAC,EAAE,iBAAiB,EAAG,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,SAAS,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,GAAI,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAkB1K;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,GACnF,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAcvE;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"}
@@ -7,6 +7,8 @@
7
7
  * @packageDocumentation
8
8
  */
9
9
  import { AsyncLocalStorage } from "node:async_hooks";
10
+ import { createAsyncGenerator } from "../core/async-generator.js";
11
+ export { OverloadYield } from "../core/async-generator.js";
10
12
  /**
11
13
  * This function gives an async function that operates `generatorFunction`. The function is
12
14
  * a native async function: each `await` of the source becomes one native `await`, with
@@ -32,6 +34,28 @@ export function coroutine(generatorFunction) {
32
34
  return result.value;
33
35
  };
34
36
  }
37
+ /**
38
+ * This function gives a function that makes an async generator object, which operates
39
+ * `generatorFunction`. The preset gives it in place of the helper `wrapAsyncGenerator` of
40
+ * Babel. The driver starts in a snapshot of the context of the creation (rule C5). The
41
+ * native `AsyncLocalStorage` keeps that context through the `await` of the driver.
42
+ */
43
+ export function asyncGenerator(generatorFunction) {
44
+ return function makeAsyncGenerator(...args) {
45
+ let snapshot = AsyncLocalStorage.snapshot();
46
+ const generator = generatorFunction.apply(this, args);
47
+ const step = (key, argument) => {
48
+ try {
49
+ return generator[key](argument);
50
+ }
51
+ finally {
52
+ // The body can change its context with enterWith before it stops
53
+ snapshot = AsyncLocalStorage.snapshot();
54
+ }
55
+ };
56
+ return createAsyncGenerator(step, Promise, (start) => snapshot(start));
57
+ };
58
+ }
35
59
  /** This function binds a generator object to the current context (rule C5). */
36
60
  export function bindGenerator(generator) {
37
61
  let run = AsyncLocalStorage.snapshot();
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../../src/node/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAA4B,iBAA6E;IAC9H,OAAO,KAAK,UAAU,aAAa,CAAW,GAAG,IAAQ;QACrD,+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,MAAM,CAAC,KAAK,CAAC;IACxB,CAAC,CAAC;AACN,CAAC;AAKD,+EAA+E;AAC/E,MAAM,UAAU,aAAa,CAAwB,SAAa;IAC9D,IAAI,GAAG,GAAG,iBAAiB,CAAC,QAAQ,EAAE,CAAC;IACvC,MAAM,MAAM,GAAG,SAA8E,CAAC;IAC9F,MAAM,MAAM,GAAG,CAAC,MAAqB,EAAE,QAAkB,EAAY,EAAE,CAAC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G,OAAO,CAAC,KAAK,EAAE,QAAQ,EAAG,IAAI,EAAE,YAAY,EAAG,IAAI,EAAE;QACvE,MAAM,EAAG,EAAE,KAAK,EAAG,OAAO,CAAC,MAAM,EAAE,QAAQ,EAAG,IAAI,EAAE,YAAY,EAAG,IAAI,EAAE;KAC5E,CAAC,CAAC;IACH,OAAO,KAAK,CAAC;AACjB,CAAC"}
package/dist/runtime.d.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  /**
2
2
  * The runtime of the code that the Babel preset transforms. The transformed
3
- * code imports `coroutine` and `bindGenerator` from this module. When the
3
+ * code imports `coroutine`, `bindGenerator`, `asyncGenerator` and
4
+ * `OverloadYield` from this module. When the
4
5
  * module loads, it installs the patches of the runtime.
5
6
  *
6
7
  * @packageDocumentation
7
8
  */
8
9
  import "./install.js";
9
- export { bindGenerator, coroutine } from "./core/coroutine.js";
10
+ export { OverloadYield } from "./core/async-generator.js";
11
+ export { asyncGenerator, bindGenerator, coroutine } from "./core/coroutine.js";
10
12
  //# sourceMappingURL=runtime.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC"}
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC"}
package/dist/runtime.js CHANGED
@@ -1,10 +1,12 @@
1
1
  /**
2
2
  * The runtime of the code that the Babel preset transforms. The transformed
3
- * code imports `coroutine` and `bindGenerator` from this module. When the
3
+ * code imports `coroutine`, `bindGenerator`, `asyncGenerator` and
4
+ * `OverloadYield` from this module. When the
4
5
  * module loads, it installs the patches of the runtime.
5
6
  *
6
7
  * @packageDocumentation
7
8
  */
8
9
  import "./install.js";
9
- export { bindGenerator, coroutine } from "./core/coroutine.js";
10
+ export { OverloadYield } from "./core/async-generator.js";
11
+ export { asyncGenerator, bindGenerator, coroutine } from "./core/coroutine.js";
10
12
  //# sourceMappingURL=runtime.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC"}
1
+ {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "async-browser-context",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "AsyncLocalStorage and the TC39 AsyncContext API (Variable, Snapshot) for browsers. The context stays through await, promises, timers, events and generators. A Babel preset and a Vite plugin transform async functions. On Node.js, the package uses the native AsyncLocalStorage.",
5
5
  "license": "MIT",
6
6
  "author": "Mark Russell",
@@ -0,0 +1,49 @@
1
+ import { types, type PluginObject, type PluginPass } from "@babel/core";
2
+ import { addNamed } from "@babel/helper-module-imports";
3
+
4
+ /** The options of the plugin. */
5
+ export interface AsyncGeneratorHelpersOptions {
6
+ /** The module that exports `asyncGenerator` and `OverloadYield`. */
7
+ readonly runtime : string;
8
+ }
9
+
10
+ /** The Babel helpers that the runtime replaces, and the export of the runtime for each one. */
11
+ const REPLACED_HELPERS : Readonly<Record<string, string>> = {
12
+ wrapAsyncGenerator : "asyncGenerator",
13
+ OverloadYield : "OverloadYield",
14
+ };
15
+
16
+ type HelperGenerator = (name : string) => types.Expression | undefined;
17
+
18
+ /**
19
+ * The Babel plugin that gives the async generators to the driver of the runtime. It sets the
20
+ * `helperGenerator` of the file, as `@babel/plugin-transform-runtime` does. Thus, the helpers
21
+ * `wrapAsyncGenerator` and `OverloadYield` of `@babel/plugin-transform-async-generator-functions`
22
+ * become imports of `asyncGenerator` and `OverloadYield` from the runtime. The helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` stay in the
23
+ * file, and they make the `OverloadYield` values of the runtime.
24
+ *
25
+ * The plugin keeps a `helperGenerator` of an earlier plugin for the other helpers.
26
+ */
27
+ export function asyncGeneratorHelpersPlugin(_api : unknown, options : object) : PluginObject<PluginPass> {
28
+ const { runtime } = options as AsyncGeneratorHelpersOptions;
29
+ return {
30
+ name : "async-browser-context/async-generator-helpers",
31
+ pre(file : PluginPass["file"]) {
32
+ const previous = file.get("helperGenerator") as HelperGenerator | undefined;
33
+ const imports = new Map<string, types.Expression>();
34
+ const generator : HelperGenerator = (name) => {
35
+ const exported = REPLACED_HELPERS[name];
36
+ if (exported === undefined) {
37
+ return previous?.(name);
38
+ }
39
+ let local = imports.get(exported);
40
+ if (local === undefined) {
41
+ local = addNamed(file.path, exported, runtime, { nameHint : exported }) as types.Expression;
42
+ imports.set(exported, local);
43
+ }
44
+ return types.cloneNode(local);
45
+ };
46
+ file.set("helperGenerator", generator);
47
+ },
48
+ };
49
+ }
@@ -10,8 +10,12 @@ export interface BindGeneratorsOptions {
10
10
  /** The key of the file metadata that tells if the preset changed the file. */
11
11
  export const METADATA_KEY = "asyncBrowserContext";
12
12
 
13
- /** The names of the helper of `@babel/plugin-transform-async-generator-functions`. Babel gives them an underscore. */
14
- const ASYNC_GENERATOR_HELPER = /^_wrapAsyncGenerator\d*$/;
13
+ /**
14
+ * The local names of the async generator helper: `wrapAsyncGenerator` of
15
+ * `@babel/plugin-transform-async-generator-functions`, or `asyncGenerator` of the runtime,
16
+ * which the preset gives in its place. Babel gives them an underscore.
17
+ */
18
+ const ASYNC_GENERATOR_HELPER = /^_(wrapAsyncGenerator|asyncGenerator)\d*$/;
15
19
  /** The local names that `addNamed` gives to `bindGenerator`. */
16
20
  const BIND_GENERATOR_NAME = /^_bindGenerator\d*$/;
17
21
 
@@ -20,8 +24,15 @@ type FunctionPath = NodePath<types.Function>;
20
24
  /** This function gives `true` if `path` is the generator that Babel gives to its async generator helper. */
21
25
  function isAsyncGeneratorHelperArgument(path : FunctionPath) : boolean {
22
26
  const parent = path.parent;
23
- // Stryker disable next-line LogicalOperator: without the call check, the name test also fails, because a member callee has no name
24
- return types.isCallExpression(parent) && types.isIdentifier(parent.callee) && ASYNC_GENERATOR_HELPER.test(parent.callee.name);
27
+ if (!types.isCallExpression(parent)) {
28
+ return false;
29
+ }
30
+ if (types.isIdentifier(parent.callee)) {
31
+ return ASYNC_GENERATOR_HELPER.test(parent.callee.name);
32
+ }
33
+ // CommonJS output of the preset: _runtime.asyncGenerator or (0, _runtime.asyncGenerator)
34
+ const member = types.isSequenceExpression(parent.callee) ? parent.callee.expressions.at(-1) : parent.callee;
35
+ return types.isMemberExpression(member) && types.isIdentifier(member.property, { name : "asyncGenerator" });
25
36
  }
26
37
 
27
38
  /**
@@ -101,7 +112,8 @@ export function bindGeneratorsPlugin(_api : unknown, options : object) : PluginO
101
112
  if (node.async) {
102
113
  changed = true;
103
114
  }
104
- if (!node.generator || isAsyncGeneratorHelperArgument(path) || isAlreadyBound(path, runtime)) {
115
+ // The driver of the runtime keeps the context of an async generator (asyncGenerator)
116
+ if (!node.generator || node.async || isAsyncGeneratorHelperArgument(path) || isAlreadyBound(path, runtime)) {
105
117
  return;
106
118
  }
107
119
  changed = true;
@@ -1,6 +1,7 @@
1
1
  import type { PluginItem, PresetAPI, PresetObject } from "@babel/core";
2
2
  import asyncGeneratorFunctions from "@babel/plugin-transform-async-generator-functions";
3
3
  import asyncToGenerator from "@babel/plugin-transform-async-to-generator";
4
+ import { asyncGeneratorHelpersPlugin } from "./async-generator-helpers.js";
4
5
  import { bindGeneratorsPlugin } from "./bind-generators.js";
5
6
  import { nameCoroutinesPlugin } from "./name-coroutines.js";
6
7
  import { skipRuntimePlugin } from "./skip-runtime.js";
@@ -33,14 +34,16 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
33
34
  *
34
35
  * 1. A plugin of this library skips the native async functions of the
35
36
  * runtime, which a directive marks.
36
- * 2. A plugin of this library binds each generator to the context of its
37
- * creation.
37
+ * 2. A plugin of this library binds each sync generator to the context of
38
+ * its creation.
38
39
  * 3. A plugin of this library gives the name of each async function to
39
40
  * `coroutine`, for the callback hooks. The option `names: false` removes
40
41
  * this step.
41
- * 4. `@babel/plugin-transform-async-generator-functions` changes async
42
+ * 4. A plugin of this library gives the async generators to `asyncGenerator`
43
+ * of the runtime, in place of the Babel helper.
44
+ * 5. `@babel/plugin-transform-async-generator-functions` changes async
42
45
  * generators and `for await` loops.
43
- * 5. `@babel/plugin-transform-async-to-generator` changes each async function
46
+ * 6. `@babel/plugin-transform-async-to-generator` changes each async function
44
47
  * into a generator that the `coroutine` function of the runtime operates.
45
48
  *
46
49
  * The preset operates with Babel 7.22 and later and with Babel 8. Put it last
@@ -55,6 +58,7 @@ export default function asyncBrowserContextPreset(api : PresetAPI, options : Pre
55
58
  skipRuntimePlugin,
56
59
  [bindGeneratorsPlugin, { runtime }],
57
60
  ...names,
61
+ [asyncGeneratorHelpersPlugin, { runtime }],
58
62
  asyncGeneratorFunctions,
59
63
  [asyncToGenerator, { module : runtime, method : "coroutine" }],
60
64
  ],