async-browser-context 0.2.1 → 0.2.3

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 (42) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +4 -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 +11 -6
  11. package/dist/babel/preset.d.ts.map +1 -1
  12. package/dist/babel/preset.js +15 -6
  13. package/dist/babel/preset.js.map +1 -1
  14. package/dist/babel/skip-runtime.d.ts +16 -0
  15. package/dist/babel/skip-runtime.d.ts.map +1 -0
  16. package/dist/babel/skip-runtime.js +29 -0
  17. package/dist/babel/skip-runtime.js.map +1 -0
  18. package/dist/core/async-generator.d.ts +36 -0
  19. package/dist/core/async-generator.d.ts.map +1 -0
  20. package/dist/core/async-generator.js +132 -0
  21. package/dist/core/async-generator.js.map +1 -0
  22. package/dist/core/coroutine.d.ts +13 -3
  23. package/dist/core/coroutine.d.ts.map +1 -1
  24. package/dist/core/coroutine.js +104 -35
  25. package/dist/core/coroutine.js.map +1 -1
  26. package/dist/node/runtime.d.ts +13 -2
  27. package/dist/node/runtime.d.ts.map +1 -1
  28. package/dist/node/runtime.js +45 -24
  29. package/dist/node/runtime.js.map +1 -1
  30. package/dist/runtime.d.ts +4 -2
  31. package/dist/runtime.d.ts.map +1 -1
  32. package/dist/runtime.js +4 -2
  33. package/dist/runtime.js.map +1 -1
  34. package/package.json +3 -2
  35. package/src/babel/async-generator-helpers.ts +49 -0
  36. package/src/babel/bind-generators.ts +17 -5
  37. package/src/babel/preset.ts +15 -6
  38. package/src/babel/skip-runtime.ts +32 -0
  39. package/src/core/async-generator.ts +159 -0
  40. package/src/core/coroutine.ts +107 -35
  41. package/src/node/runtime.ts +47 -23
  42. package/src/runtime.ts +4 -2
@@ -1,3 +1,4 @@
1
+ import { createAsyncGenerator, OverloadYield } 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
@@ -26,47 +27,114 @@ function namedForHooks(generatorFunction, name) {
26
27
  * `method` option, and adds `name`, the name of the async function, if it has one.
27
28
  */
28
29
  export function coroutine(generatorFunction, name) {
29
- const { Promise: NativePromise, promiseThen, promiseResolve } = store.intrinsics;
30
+ const { promiseResolve } = store.intrinsics;
30
31
  return function asyncFunction(...args) {
31
32
  // The context of the call. The body can change it with AsyncLocalStorage.enterWith.
32
33
  let frame = store.current;
33
- const generator = generatorFunction.apply(this, args);
34
- return new NativePromise((resolve, reject) => {
35
- /** `resumed` is `true` for a step after an `await`. Only these steps start the hook methods. */
36
- const step = (resume, argument, resumed) => {
37
- const previous = store.current;
38
- store.current = frame;
39
- let result;
40
- let awaited;
34
+ let generator;
35
+ const next = (value) => generator.next(value);
36
+ const throwInto = (error) => generator.throw(error);
37
+ /**
38
+ * This function does one step of the body in the context of the async function, and sets
39
+ * the previous context again. `resumed` is `true` for a step after an `await`. Only these
40
+ * steps start the hook methods. The function gives the value to await.
41
+ */
42
+ const step = (resume, argument, resumed) => {
43
+ const previous = store.current;
44
+ store.current = frame;
45
+ try {
46
+ const hooks = resumed ? store.hooks : undefined;
47
+ const result = hooks === undefined
48
+ ? resume(argument)
49
+ : applyWithHooks(hooks, namedForHooks(generatorFunction, name), "await", resume, undefined, [argument]);
50
+ const value = result.value;
51
+ if (!result.done && ((typeof value === "object" && value !== null) || typeof value === "function")) {
52
+ // Promise.resolve reads the "then" property of an object. Do it in the context of the body, as await does.
53
+ // The result object is new for each step, so the change is not visible to other code.
54
+ result.value = promiseResolve(value);
55
+ }
56
+ return result;
57
+ }
58
+ finally {
59
+ frame = store.current;
60
+ store.current = previous;
61
+ }
62
+ };
63
+ /**
64
+ * The driver is a native async function: its `await` is faster than a `then` callback,
65
+ * mostly in Firefox. Each `await` takes the same number of microtask turns as the
66
+ * `await` of the source. Between the steps, the context of the caller stays current.
67
+ */
68
+ const drive = async () => {
69
+ // The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
70
+ "async-browser-context runtime";
71
+ // In the driver, an error of the parameters rejects the promise, as with a native async function
72
+ generator = generatorFunction.apply(this, args);
73
+ let result = step(next, undefined, false);
74
+ while (result.done !== true) {
75
+ let value;
41
76
  try {
42
- const hooks = resumed ? store.hooks : undefined;
43
- result = hooks === undefined
44
- ? resume(argument)
45
- : applyWithHooks(hooks, namedForHooks(generatorFunction, name), "await", resume, undefined, [argument]);
46
- if (result.done) {
47
- frame = store.current;
48
- store.current = previous;
49
- resolve(result.value);
50
- return;
51
- }
52
- // Promise.resolve reads a "then" property of the value. Do it in the context of the body, as await does.
53
- awaited = promiseResolve(result.value);
77
+ value = await result.value;
54
78
  }
55
79
  catch (error) {
56
- store.current = previous;
57
- reject(error);
58
- return;
80
+ result = step(throwInto, error, true);
81
+ continue;
82
+ }
83
+ result = step(next, value, true);
84
+ }
85
+ return result.value;
86
+ };
87
+ return drive();
88
+ };
89
+ }
90
+ /** This function gives `true` for a value that can have a `then` property: an object or a function. */
91
+ function isObject(value) {
92
+ return (typeof value === "object" && value !== null) || typeof value === "function";
93
+ }
94
+ /**
95
+ * This function gives a function that makes an async generator object, which operates
96
+ * `generatorFunction`. The preset gives this function in place of the helper
97
+ * `wrapAsyncGenerator` of `@babel/plugin-transform-async-generator-functions`.
98
+ *
99
+ * Each step of the body operates in the context of the call that made the generator
100
+ * (rule C5). The driver continues each step with a native `await` (`async-generator.ts`).
101
+ */
102
+ export function asyncGenerator(generatorFunction) {
103
+ const { Promise: NativePromise, promiseResolve } = store.intrinsics;
104
+ return function makeAsyncGenerator(...args) {
105
+ let frame = store.current;
106
+ const generator = generatorFunction.apply(this, args);
107
+ const methods = {
108
+ next: (argument) => generator.next(argument),
109
+ throw: (argument) => generator.throw(argument),
110
+ return: (argument) => generator.return(argument),
111
+ };
112
+ const step = (key, argument, resumed) => {
113
+ const previous = store.current;
114
+ store.current = frame;
115
+ try {
116
+ const hooks = resumed ? store.hooks : undefined;
117
+ const result = hooks === undefined
118
+ ? methods[key](argument)
119
+ : applyWithHooks(hooks, generatorFunction, "await", methods[key], undefined, [argument]);
120
+ // Read the "then" property of an awaited or yielded object in the context of the body, as await and yield do
121
+ const value = result.value;
122
+ if (value instanceof OverloadYield) {
123
+ if (isObject(value.v)) {
124
+ value.v = promiseResolve(value.v);
125
+ }
126
+ }
127
+ else if (isObject(value)) {
128
+ result.value = promiseResolve(value);
59
129
  }
130
+ return result;
131
+ }
132
+ finally {
60
133
  frame = store.current;
61
134
  store.current = previous;
62
- promiseThen.call(awaited, onFulfilled, onRejected);
63
- };
64
- const next = (value) => generator.next(value);
65
- const throwInto = (error) => generator.throw(error);
66
- const onFulfilled = (value) => step(next, value, true);
67
- const onRejected = (error) => step(throwInto, error, true);
68
- step(next, undefined, false);
69
- });
135
+ }
136
+ };
137
+ return createAsyncGenerator(step, NativePromise);
70
138
  };
71
139
  }
72
140
  /**
@@ -75,9 +143,10 @@ export function coroutine(generatorFunction, name) {
75
143
  * proposal specifies (`GeneratorStart` and `GeneratorResume`). After each step,
76
144
  * the context of the caller is current again.
77
145
  *
78
- * The preset changes each generator function so that it gives
79
- * `bindGenerator(generator)`. The function works with sync generators and
80
- * with the async generators of `@babel/plugin-transform-async-generator-functions`.
146
+ * The preset changes each sync generator function so that it gives
147
+ * `bindGenerator(generator)`. An async generator does not need it, because
148
+ * `asyncGenerator` keeps the context itself. The function also works with
149
+ * async generator objects, for example in the output of older versions.
81
150
  */
82
151
  export function bindGenerator(generator) {
83
152
  let frame = store.current;
@@ -1 +1 @@
1
- {"version":3,"file":"coroutine.js","sourceRoot":"","sources":["../../src/core/coroutine.ts"],"names":[],"mapp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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,7 +1,18 @@
1
- /** This function gives an async function that operates `generatorFunction`. */
1
+ export { OverloadYield } from "../core/async-generator.js";
2
+ /**
3
+ * This function gives an async function that operates `generatorFunction`. The function is
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.
6
+ */
2
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>;
3
15
  type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown, unknown, unknown>;
4
16
  /** This function binds a generator object to the current context (rule C5). */
5
17
  export declare function bindGenerator<I extends AnyIterator>(generator: I): I;
6
- export {};
7
18
  //# sourceMappingURL=runtime.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/node/runtime.ts"],"names":[],"mappings":"AAUA,+EAA+E;AAC/E,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,C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,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,32 +7,53 @@
7
7
  * @packageDocumentation
8
8
  */
9
9
  import { AsyncLocalStorage } from "node:async_hooks";
10
- /** This function gives an async function that operates `generatorFunction`. */
10
+ import { createAsyncGenerator } from "../core/async-generator.js";
11
+ export { OverloadYield } from "../core/async-generator.js";
12
+ /**
13
+ * This function gives an async function that operates `generatorFunction`. The function is
14
+ * 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.
16
+ */
11
17
  export function coroutine(generatorFunction) {
12
- return function asyncFunction(...args) {
18
+ return async function asyncFunction(...args) {
19
+ // The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
20
+ "async-browser-context runtime";
21
+ const generator = generatorFunction.apply(this, args);
22
+ let result = generator.next();
23
+ while (result.done !== true) {
24
+ let value;
25
+ try {
26
+ value = await result.value;
27
+ }
28
+ catch (error) {
29
+ result = generator.throw(error);
30
+ continue;
31
+ }
32
+ result = generator.next(value);
33
+ }
34
+ return result.value;
35
+ };
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();
13
46
  const generator = generatorFunction.apply(this, args);
14
- return new Promise((resolve, reject) => {
15
- const step = (method, argument) => {
16
- let result;
17
- let awaited;
18
- try {
19
- result = method === "next" ? generator.next(argument) : generator.throw(argument);
20
- if (result.done) {
21
- resolve(result.value);
22
- return;
23
- }
24
- awaited = Promise.resolve(result.value);
25
- }
26
- catch (error) {
27
- reject(error);
28
- return;
29
- }
30
- awaited.then(onFulfilled, onRejected);
31
- };
32
- const onFulfilled = (value) => step("next", value);
33
- const onRejected = (error) => step("throw", error);
34
- step("next", undefined);
35
- });
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));
36
57
  };
37
58
  }
38
59
  /** This function binds a generator object to the current context (rule C5). */
@@ -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,+EAA+E;AAC/E,MAAM,UAAU,SAAS,CAA4B,iBAA6E;IAC9H,OAAO,SAAS,aAAa,CAAW,GAAG,IAAQ;QAC/C,MAAM,SAAS,GAAG,iBAAiB,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACtD,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACtC,MAAM,IAAI,GAAG,CAAC,MAAyB,EAAE,QAAkB,EAAS,EAAE;gBAClE,IAAI,MAAmC,CAAC;gBACxC,IAAI,OAA0B,CAAC;gBAC/B,IAAI,CAAC;oBACD,MAAM,GAAG,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;oBAClF,IAAI,MAAM,CAAC,IAAI,EAAE,CAAC;wBACd,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;wBACtB,OAAO;oBACX,CAAC;oBACD,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC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CAAC;QACpC,CAAC;gBAAS,CAAC;YACP,iEAAiE;YACjE,GAAG,GAAG,iBAAiB,CAAC,QAAQ,EAAE,CAAC;QACvC,CAAC;IACL,CAAC,CAAC,CAAC;IACH,MAAM,OAAO,GAAG;QACZ,IAAI,CAAC,KAAgB,IAAc,OAAO,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;QAClE,KAAK,CAAC,KAAgB,IAAc,OAAO,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;QACpE,MAAM,CAAC,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"}
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.1",
3
+ "version": "0.2.3",
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",
@@ -120,7 +120,8 @@
120
120
  "site:data": "tsx scripts/site-data.ts",
121
121
  "readme:media": "tsx scripts/readme-media.ts",
122
122
  "test:bundlers": "pnpm build && tsx test/bundlers/run.ts",
123
- "bench:size": "tsx bench/size.ts"
123
+ "bench:size": "tsx bench/size.ts",
124
+ "bench:browsers": "tsx bench/browsers.ts"
124
125
  },
125
126
  "dependencies": {
126
127
  "@babel/core": "^8.0.7",
@@ -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,8 +1,10 @@
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";
7
+ import { skipRuntimePlugin } from "./skip-runtime.js";
6
8
 
7
9
  export { METADATA_KEY } from "./bind-generators.js";
8
10
 
@@ -27,16 +29,21 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
27
29
 
28
30
  /**
29
31
  * The Babel preset of `async-browser-context`. It changes the code so that the
30
- * context stays through `await` and `yield`:
32
+ * context stays through `await` and `yield`. It does not change the runtime
33
+ * of the library, also when it transforms `node_modules`.
31
34
  *
32
- * 1. A plugin of this library binds each generator to the context of its
33
- * creation.
34
- * 2. A plugin of this library gives the name of each async function to
35
+ * 1. A plugin of this library skips the native async functions of the
36
+ * runtime, which a directive marks.
37
+ * 2. A plugin of this library binds each sync generator to the context of
38
+ * its creation.
39
+ * 3. A plugin of this library gives the name of each async function to
35
40
  * `coroutine`, for the callback hooks. The option `names: false` removes
36
41
  * this step.
37
- * 3. `@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
38
45
  * generators and `for await` loops.
39
- * 4. `@babel/plugin-transform-async-to-generator` changes each async function
46
+ * 6. `@babel/plugin-transform-async-to-generator` changes each async function
40
47
  * into a generator that the `coroutine` function of the runtime operates.
41
48
  *
42
49
  * The preset operates with Babel 7.22 and later and with Babel 8. Put it last
@@ -48,8 +55,10 @@ export default function asyncBrowserContextPreset(api : PresetAPI, options : Pre
48
55
  const names : PluginItem[] = options.names === false ? [] : [[nameCoroutinesPlugin, { runtime }]];
49
56
  return {
50
57
  plugins : [
58
+ skipRuntimePlugin,
51
59
  [bindGeneratorsPlugin, { runtime }],
52
60
  ...names,
61
+ [asyncGeneratorHelpersPlugin, { runtime }],
53
62
  asyncGeneratorFunctions,
54
63
  [asyncToGenerator, { module : runtime, method : "coroutine" }],
55
64
  ],
@@ -0,0 +1,32 @@
1
+ import type { NodePath, PluginObject, PluginPass, types } from "@babel/core";
2
+
3
+ /**
4
+ * The directive that marks a native async function of the runtime. If the
5
+ * preset changes that function, the function uses `coroutine` to operate
6
+ * `coroutine`, without end. A directive stays in the code after TypeScript,
7
+ * esbuild (also in the dependency bundles of the Vite dev server), Rollup and
8
+ * webpack, but a comment does not.
9
+ */
10
+ export const RUNTIME_DIRECTIVE = "async-browser-context runtime";
11
+
12
+ function isRuntimeFunction(node : types.Function) : boolean {
13
+ return node.body.type === "BlockStatement" && node.body.directives.some((directive) => directive.value.value === RUNTIME_DIRECTIVE);
14
+ }
15
+
16
+ /**
17
+ * The first plugin of the preset. It skips each function with the directive,
18
+ * so that no other plugin of the preset changes it. Bundlers that apply the
19
+ * preset to `node_modules` thus do not change the runtime.
20
+ */
21
+ export function skipRuntimePlugin() : PluginObject<PluginPass> {
22
+ return {
23
+ name : "async-browser-context/skip-runtime",
24
+ visitor : {
25
+ Function(functionPath : NodePath<types.Function>) {
26
+ if (isRuntimeFunction(functionPath.node)) {
27
+ functionPath.skip();
28
+ }
29
+ },
30
+ },
31
+ };
32
+ }