async-browser-context 0.2.0 → 0.2.2

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 CHANGED
@@ -1,5 +1,30 @@
1
1
  # Change history
2
2
 
3
+ ## 0.2.2
4
+
5
+ ### Corrections
6
+
7
+ - Transformed async functions are much faster in Firefox. The coroutine operates each async function with a native `await` in place of a `then` callback. In Firefox 157, 50,000 awaits of `null` took 292 ms and take 4 ms at this time. 50,000 awaits of promises took 228 ms and take 51 ms. Chromium and WebKit are also a little faster (`pnpm bench:browsers`).
8
+ - An error in a parameter default or in a parameter destructuring of a transformed async function rejects the promise, as with a native async function. Before, the call threw the error. The Node.js runtime has the same correction.
9
+ - The Babel preset does not change the runtime of the library, also when a bundler applies the preset to `node_modules`. The native async functions of the runtime have the directive `"async-browser-context runtime"`, and the preset skips them.
10
+
11
+ ### Documentation
12
+
13
+ - In Firefox, a patch of `Promise.prototype.then` makes each `await` of a promise slower, also in code without the transform. The performance page tells the cost.
14
+ - The command `pnpm bench:browsers` measures `await` and `then` in Chromium, Firefox and WebKit.
15
+
16
+ ## 0.2.1
17
+
18
+ ### New functions
19
+
20
+ - The option `names` of the Babel preset and of the Vite plugin. With `names: false`, the preset does not give the names of the async functions to `coroutine`. The code is then smaller by 0.1 to 1.6 percent.
21
+
22
+ ### Documentation and tests
23
+
24
+ - The guide "Build tools" gives the setup for webpack, Rspack, Rollup, esbuild, Next.js and SWC. The test `pnpm test:bundlers` builds a page with each setup and examines it in Chromium.
25
+ - SWC and esbuild with the target ES2016 keep all rules except rule C5. Two new test projects start all rule tests with these compilers in place of the preset.
26
+ - The command `pnpm bench:size` measures the size of the runtime and the size that the transform adds to real npm packages. The runtime is 5.9 kB with gzip. The transform adds 2.9 percent to an example application.
27
+
3
28
  ## 0.2.0
4
29
 
5
30
  ### New functions
package/README.md CHANGED
@@ -81,6 +81,7 @@ The plugin transforms the modules of the application and of its dependencies, al
81
81
  | `include` | All JavaScript and TypeScript modules | A regular expression or a function that selects the modules to transform. |
82
82
  | `exclude` | None | A regular expression or a function that selects the modules not to transform. |
83
83
  | `runtime` | `async-browser-context/runtime` | The module that the transformed code imports. |
84
+ | `names` | `true` | Set `false` to remove the names of the async functions for the callback hooks. The code is then smaller by 0.1 to 1.6 percent. |
84
85
  | `ssr` | `false` | Set `true` to also transform the modules of server-side rendering, for example for Vitest in Node.js. |
85
86
 
86
87
  ### Babel
@@ -97,6 +98,20 @@ The plugin transforms the modules of the application and of its dependencies, al
97
98
 
98
99
  The preset operates with Babel 7.22 and later, and with Babel 8. CI examines Babel 7.22, the newest Babel 7 and the newest Babel 8 with the packed package (`pnpm test:babel`).
99
100
 
101
+ ### Other build tools
102
+
103
+ A test in CI (`pnpm test:bundlers`) builds a page with each of these setups and examines it in Chromium. The [build tools guide](https://mark1russell7.github.io/AsyncBrowserContext/docs/guides/build-tools) gives each configuration.
104
+
105
+ | Tool | Setup | Rules |
106
+ | --- | --- | --- |
107
+ | webpack 5, Rspack 2 | `babel-loader` with the preset. Do not exclude `node_modules`. | All |
108
+ | Rollup 4 | `@rollup/plugin-babel` with the preset | All |
109
+ | esbuild | A small plugin that applies the preset | All |
110
+ | Next.js 16, Turbopack or webpack | `babel.config.js` with `next/babel` and the preset, and `transpilePackages` for the dependencies | All |
111
+ | SWC, esbuild, Next.js without Babel | The target ES2016: the compiler changes async functions into generators | All except rule C5 |
112
+
113
+ With the target ES2016, the patch of `then` keeps the context through `await`. A generator body then gets the context of each `next()` call, and the callback hooks get no names.
114
+
100
115
  ### Node.js
101
116
 
102
117
  Do not set up a transform. The `node` export condition selects the Node.js entry, which uses the native `AsyncLocalStorage`.
@@ -259,6 +274,10 @@ One read with `get()` takes approximately 4 ns. The [documentation website](http
259
274
 
260
275
  When the page loads, the library installs its patches in approximately 8 ms: the median of Chromium, Firefox and WebKit on the same computer.
261
276
 
277
+ In Chromium, Firefox and WebKit, an `await` in transformed code takes approximately the time of a native `await` (`pnpm bench:browsers`). In Firefox, the patch of `then` adds approximately 0.8 µs to each `await` of a promise. The performance page of the website tells more.
278
+
279
+ The runtime is 5.9 kB with gzip, and zone.js 0.16.3 is 13.3 kB. The transform adds 4 to 9 percent to packages with much async code, and nothing to React DOM. In an example application with React DOM, TanStack Query, supabase-js and ky, it adds 2.9 percent (`pnpm bench:size`).
280
+
262
281
  ## Tests
263
282
 
264
283
  | Command | Function |
@@ -268,8 +287,11 @@ When the page loads, the library installs its patches in approximately 8 ms: the
268
287
  | `pnpm test:browser` | The tests in Chromium, Firefox and WebKit |
269
288
  | `pnpm smoke` | Packs the package, installs it in a new Vite application, and examines the application in Chromium |
270
289
  | `pnpm test:babel` | Packs the package, transforms a program with the preset in Babel 7.22, the newest Babel 7 and Babel 8, and starts the output |
290
+ | `pnpm test:bundlers` | Packs the package, builds a page with webpack, Rspack, Rollup, esbuild and Next.js, and examines each page in Chromium |
271
291
  | `pnpm mutation` | Mutation testing with Stryker |
272
292
  | `pnpm bench` | The benchmarks |
293
+ | `pnpm bench:browsers` | The time of `await` and `then` in Chromium, Firefox and WebKit, without the library, with the Babel helper and with the library |
294
+ | `pnpm bench:size` | The size of the runtime, and the size that the transform adds to a set of npm packages |
273
295
  | `pnpm lint:ste` | The STE linter of the documentation and the TSDoc comments |
274
296
 
275
297
  Set `BROWSERS=chromium,firefox,webkit` to select the browsers. Playwright WebKit does not start on all Windows computers, so the default on Windows is Chromium and Firefox. `docs/testing.md` tells more.
@@ -7,20 +7,31 @@ export interface PresetOptions {
7
7
  * `bindGenerator`. The default is `async-browser-context/runtime`.
8
8
  */
9
9
  readonly runtime?: string | undefined;
10
+ /**
11
+ * Give the name of each async function to `coroutine`, for the callback
12
+ * hooks. The default is `true`. With `false`, the hooks get no name for the
13
+ * steps after an `await`. The code is then smaller by 0.1 to 1.6 percent
14
+ * of its gzip size (`pnpm bench:size`).
15
+ */
16
+ readonly names?: boolean | undefined;
10
17
  }
11
18
  /** The default runtime module. */
12
19
  export declare const DEFAULT_RUNTIME = "async-browser-context/runtime";
13
20
  /**
14
21
  * The Babel preset of `async-browser-context`. It changes the code so that the
15
- * context stays through `await` and `yield`:
22
+ * context stays through `await` and `yield`. It does not change the runtime
23
+ * of the library, also when it transforms `node_modules`.
16
24
  *
17
- * 1. A plugin of this library binds each generator to the context of its
25
+ * 1. A plugin of this library skips the native async functions of the
26
+ * runtime, which a directive marks.
27
+ * 2. A plugin of this library binds each generator to the context of its
18
28
  * creation.
19
- * 2. A plugin of this library gives the name of each async function to
20
- * `coroutine`, for the callback hooks.
21
- * 3. `@babel/plugin-transform-async-generator-functions` changes async
29
+ * 3. A plugin of this library gives the name of each async function to
30
+ * `coroutine`, for the callback hooks. The option `names: false` removes
31
+ * this step.
32
+ * 4. `@babel/plugin-transform-async-generator-functions` changes async
22
33
  * generators and `for await` loops.
23
- * 4. `@babel/plugin-transform-async-to-generator` changes each async function
34
+ * 5. `@babel/plugin-transform-async-to-generator` changes each async function
24
35
  * into a generator that the `coroutine` function of the runtime operates.
25
36
  *
26
37
  * The preset operates with Babel 7.22 and later and with Babel 8. Put it last
@@ -1 +1 @@
1
- {"version":3,"file":"preset.d.ts","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAM3D,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,iCAAiC;AACjC,MAAM,WAAW,aAAa;IAC1B;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAG,MAAM,GAAG,SAAS,CAAC;CAC1C;AAED,kCAAkC;AAClC,eAAO,MAAM,eAAe,kCAAkC,CAAC;AAE/D;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAG,EAAG,SAAS,EAAE,OAAO,GAAG,aAAkB,GAAI,YAAY,CAW9G"}
1
+ {"version":3,"file":"preset.d.ts","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,SAAS,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAOvE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,iCAAiC;AACjC,MAAM,WAAW,aAAa;IAC1B;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAG,MAAM,GAAG,SAAS,CAAC;IACvC;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAG,OAAO,GAAG,SAAS,CAAC;CACzC;AAED,kCAAkC;AAClC,eAAO,MAAM,eAAe,kCAAkC,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAG,EAAG,SAAS,EAAE,OAAO,GAAG,aAAkB,GAAI,YAAY,CAa9G"}
@@ -2,20 +2,25 @@ import asyncGeneratorFunctions from "@babel/plugin-transform-async-generator-fun
2
2
  import asyncToGenerator from "@babel/plugin-transform-async-to-generator";
3
3
  import { bindGeneratorsPlugin } from "./bind-generators.js";
4
4
  import { nameCoroutinesPlugin } from "./name-coroutines.js";
5
+ import { skipRuntimePlugin } from "./skip-runtime.js";
5
6
  export { METADATA_KEY } from "./bind-generators.js";
6
7
  /** The default runtime module. */
7
8
  export const DEFAULT_RUNTIME = "async-browser-context/runtime";
8
9
  /**
9
10
  * The Babel preset of `async-browser-context`. It changes the code so that the
10
- * context stays through `await` and `yield`:
11
+ * context stays through `await` and `yield`. It does not change the runtime
12
+ * of the library, also when it transforms `node_modules`.
11
13
  *
12
- * 1. A plugin of this library binds each generator to the context of its
14
+ * 1. A plugin of this library skips the native async functions of the
15
+ * runtime, which a directive marks.
16
+ * 2. A plugin of this library binds each generator to the context of its
13
17
  * creation.
14
- * 2. A plugin of this library gives the name of each async function to
15
- * `coroutine`, for the callback hooks.
16
- * 3. `@babel/plugin-transform-async-generator-functions` changes async
18
+ * 3. A plugin of this library gives the name of each async function to
19
+ * `coroutine`, for the callback hooks. The option `names: false` removes
20
+ * this step.
21
+ * 4. `@babel/plugin-transform-async-generator-functions` changes async
17
22
  * generators and `for await` loops.
18
- * 4. `@babel/plugin-transform-async-to-generator` changes each async function
23
+ * 5. `@babel/plugin-transform-async-to-generator` changes each async function
19
24
  * into a generator that the `coroutine` function of the runtime operates.
20
25
  *
21
26
  * The preset operates with Babel 7.22 and later and with Babel 8. Put it last
@@ -24,10 +29,12 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
24
29
  export default function asyncBrowserContextPreset(api, options = {}) {
25
30
  api.assertVersion("^7.22.0 || ^8.0.0");
26
31
  const runtime = options.runtime ?? DEFAULT_RUNTIME;
32
+ const names = options.names === false ? [] : [[nameCoroutinesPlugin, { runtime }]];
27
33
  return {
28
34
  plugins: [
35
+ skipRuntimePlugin,
29
36
  [bindGeneratorsPlugin, { runtime }],
30
- [nameCoroutinesPlugin, { runtime }],
37
+ ...names,
31
38
  asyncGeneratorFunctions,
32
39
  [asyncToGenerator, { module: runtime, method: "coroutine" }],
33
40
  ],
@@ -1 +1 @@
1
- {"version":3,"file":"preset.js","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AACA,OAAO,uBAAuB,MAAM,mDAAmD,CAAC;AACxF,OAAO,gBAAgB,MAAM,4CAA4C,CAAC;AAC1E,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAWpD,kCAAkC;AAClC,MAAM,CAAC,MAAM,eAAe,GAAG,+BAA+B,CAAC;AAE/D;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAe,EAAE,UAA0B,EAAE;IAC3F,GAAG,CAAC,aAAa,CAAC,mBAAmB,CAAC,CAAC;IACvC,MAAM,OAAO,GA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YACvB,CAAC,gBAAgB,EAAE,EAAE,MAAM,EAAG,OAAO,EAAE,MAAM,EAAG,WAAW,EAAE,CAAC;SACjE;KACJ,CAAC;AACN,CAAC"}
@@ -0,0 +1,16 @@
1
+ import type { PluginObject, PluginPass } from "@babel/core";
2
+ /**
3
+ * The directive that marks a native async function of the runtime. If the
4
+ * preset changes that function, the function uses `coroutine` to operate
5
+ * `coroutine`, without end. A directive stays in the code after TypeScript,
6
+ * esbuild (also in the dependency bundles of the Vite dev server), Rollup and
7
+ * webpack, but a comment does not.
8
+ */
9
+ export declare const RUNTIME_DIRECTIVE = "async-browser-context runtime";
10
+ /**
11
+ * The first plugin of the preset. It skips each function with the directive,
12
+ * so that no other plugin of the preset changes it. Bundlers that apply the
13
+ * preset to `node_modules` thus do not change the runtime.
14
+ */
15
+ export declare function skipRuntimePlugin(): PluginObject<PluginPass>;
16
+ //# sourceMappingURL=skip-runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"skip-runtime.d.ts","sourceRoot":"","sources":["../../src/babel/skip-runtime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAY,YAAY,EAAE,UAAU,EAAS,MAAM,aAAa,CAAC;AAE7E;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,kCAAkC,CAAC;AAMjE;;;;GAIG;AACH,wBAAgB,iBAAiB,IAAK,YAAY,CAAC,UAAU,CAAC,CAW7D"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The directive that marks a native async function of the runtime. If the
3
+ * preset changes that function, the function uses `coroutine` to operate
4
+ * `coroutine`, without end. A directive stays in the code after TypeScript,
5
+ * esbuild (also in the dependency bundles of the Vite dev server), Rollup and
6
+ * webpack, but a comment does not.
7
+ */
8
+ export const RUNTIME_DIRECTIVE = "async-browser-context runtime";
9
+ function isRuntimeFunction(node) {
10
+ return node.body.type === "BlockStatement" && node.body.directives.some((directive) => directive.value.value === RUNTIME_DIRECTIVE);
11
+ }
12
+ /**
13
+ * The first plugin of the preset. It skips each function with the directive,
14
+ * so that no other plugin of the preset changes it. Bundlers that apply the
15
+ * preset to `node_modules` thus do not change the runtime.
16
+ */
17
+ export function skipRuntimePlugin() {
18
+ return {
19
+ name: "async-browser-context/skip-runtime",
20
+ visitor: {
21
+ Function(functionPath) {
22
+ if (isRuntimeFunction(functionPath.node)) {
23
+ functionPath.skip();
24
+ }
25
+ },
26
+ },
27
+ };
28
+ }
29
+ //# sourceMappingURL=skip-runtime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"skip-runtime.js","sourceRoot":"","sources":["../../src/babel/skip-runtime.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,+BAA+B,CAAC;AAEjE,SAAS,iBAAiB,CAAC,IAAqB;IAC5C,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,gBAAgB,IAAI,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,KAAK,iBAAiB,CAAC,CAAC;AACxI,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,iBAAiB;IAC7B,OAAO;QACH,IAAI,EAAG,oCAAoC;QAC3C,OAAO,EAAG;YACN,QAAQ,CAAC,YAAuC;gBAC5C,IAAI,iBAAiB,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC;oBACvC,YAAY,CAAC,IAAI,EAAE,CAAC;gBACxB,CAAC;YACL,CAAC;SACJ;KACJ,CAAC;AACN,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"coroutine.d.ts","sourceRoot":"","sources":["../../src/core/coroutine.ts"],"names":[],"mappings":"AAyBA;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,EAAE,CAAC,EAC/C,iBAAiB,EAAG,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,SAAS,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,EAC7E,IAAI,CAAC,EAAG,MAAM,GACd,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CA0CxC;AAED,KAAK,WAAW,GAAG,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,GAAG,aAAa,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;AAGlG;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,SAAS,EAAG,CAAC,GAAI,CAAC,CA2BtE"}
1
+ {"version":3,"file":"coroutine.d.ts","sourceRoot":"","sources":["../../src/core/coroutine.ts"],"names":[],"mappings":"AAyBA;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,EAAE,CAAC,EAC/C,iBAAiB,EAAG,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,SAAS,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,EAC7E,IAAI,CAAC,EAAG,MAAM,GACd,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CA4DxC;AAED,KAAK,WAAW,GAAG,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,GAAG,aAAa,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;AAGlG;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,SAAS,EAAG,CAAC,GAAI,CAAC,CA2BtE"}
@@ -26,47 +26,64 @@ function namedForHooks(generatorFunction, name) {
26
26
  * `method` option, and adds `name`, the name of the async function, if it has one.
27
27
  */
28
28
  export function coroutine(generatorFunction, name) {
29
- const { Promise: NativePromise, promiseThen, promiseResolve } = store.intrinsics;
29
+ const { promiseResolve } = store.intrinsics;
30
30
  return function asyncFunction(...args) {
31
31
  // The context of the call. The body can change it with AsyncLocalStorage.enterWith.
32
32
  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;
33
+ let generator;
34
+ const next = (value) => generator.next(value);
35
+ const throwInto = (error) => generator.throw(error);
36
+ /**
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.
40
+ */
41
+ const step = (resume, argument, resumed) => {
42
+ const previous = store.current;
43
+ store.current = frame;
44
+ try {
45
+ const hooks = resumed ? store.hooks : undefined;
46
+ const result = hooks === undefined
47
+ ? resume(argument)
48
+ : 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
+ }
55
+ return result;
56
+ }
57
+ finally {
58
+ frame = store.current;
59
+ store.current = previous;
60
+ }
61
+ };
62
+ /**
63
+ * The driver is a native async function: its `await` is faster than a `then` callback,
64
+ * mostly in Firefox. Each `await` takes the same number of microtask turns as the
65
+ * `await` of the source. Between the steps, the context of the caller stays current.
66
+ */
67
+ const drive = async () => {
68
+ // The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
69
+ "async-browser-context runtime";
70
+ // In the driver, an error of the parameters rejects the promise, as with a native async function
71
+ generator = generatorFunction.apply(this, args);
72
+ let result = step(next, undefined, false);
73
+ while (result.done !== true) {
74
+ let value;
41
75
  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);
76
+ value = await result.value;
54
77
  }
55
78
  catch (error) {
56
- store.current = previous;
57
- reject(error);
58
- return;
79
+ result = step(throwInto, error, true);
80
+ continue;
59
81
  }
60
- frame = store.current;
61
- 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
- });
82
+ result = step(next, value, true);
83
+ }
84
+ return result.value;
85
+ };
86
+ return drive();
70
87
  };
71
88
  }
72
89
  /**
@@ -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,OAAO,EAAG,aAAa,EA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,MAAM,EAAE,QAAQ,EAAG,IAAI,EAAE,YAAY,EAAG,IAAI,EAAE;KAC5E,CAAC,CAAC;IACH,OAAO,KAAK,CAAC;AACjB,CAAC"}
@@ -1,4 +1,8 @@
1
- /** This function gives an async function that operates `generatorFunction`. */
1
+ /**
2
+ * This function gives an async function that operates `generatorFunction`. The function is
3
+ * a native async function: each `await` of the source becomes one native `await`, with
4
+ * the same microtask turns. An error of the parameters rejects the promise.
5
+ */
2
6
  export declare function coroutine<T, A extends unknown[], R>(generatorFunction: (this: T, ...args: A) => Generator<unknown, R, unknown>): (this: T, ...args: A) => Promise<R>;
3
7
  type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown, unknown, unknown>;
4
8
  /** This function binds a generator object to the current context (rule C5). */
@@ -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,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,GAAI,CAAC,IAAI,EAAG,CAAC,EAAE,GAAG,IAAI,EAAG,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAyB1K;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":"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"}
@@ -7,32 +7,29 @@
7
7
  * @packageDocumentation
8
8
  */
9
9
  import { AsyncLocalStorage } from "node:async_hooks";
10
- /** This function gives an async function that operates `generatorFunction`. */
10
+ /**
11
+ * This function gives an async function that operates `generatorFunction`. The function is
12
+ * a native async function: each `await` of the source becomes one native `await`, with
13
+ * the same microtask turns. An error of the parameters rejects the promise.
14
+ */
11
15
  export function coroutine(generatorFunction) {
12
- return function asyncFunction(...args) {
16
+ return async function asyncFunction(...args) {
17
+ // The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
18
+ "async-browser-context runtime";
13
19
  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
- });
20
+ let result = generator.next();
21
+ while (result.done !== true) {
22
+ let value;
23
+ try {
24
+ value = await result.value;
25
+ }
26
+ catch (error) {
27
+ result = generator.throw(error);
28
+ continue;
29
+ }
30
+ result = generator.next(value);
31
+ }
32
+ return result.value;
36
33
  };
37
34
  }
38
35
  /** 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,MAAM,CAAC,KAAK,CAAC,CAAC;gBAC5C,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACb,MAAM,CAAC,KAAK,CAAC,CAAC;oBACd,OAAO;gBACX,CAAC;gBACD,OAAO,CAAC,IAAI,CAAC,WAAW,EAAE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AO,KAAK,CAAC;AACjB,CAAC"}
@@ -12,6 +12,11 @@ export interface AsyncContextPluginOptions {
12
12
  readonly exclude?: ModuleFilter | undefined;
13
13
  /** The runtime module. The default is `async-browser-context/runtime`. */
14
14
  readonly runtime?: string | undefined;
15
+ /**
16
+ * Give the name of each async function to the callback hooks. The default
17
+ * is `true`. Refer to the option `names` of the Babel preset.
18
+ */
19
+ readonly names?: boolean | undefined;
15
20
  /**
16
21
  * Also transform the modules of server-side rendering. The default is
17
22
  * `false`, because on Node.js the native `AsyncLocalStorage` keeps the
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/vite/plugin.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAGnC,mEAAmE;AACnE,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,CAAC,CAAC,EAAE,EAAG,MAAM,KAAK,OAAO,CAAC,CAAC;AAE/D,sCAAsC;AACtC,MAAM,WAAW,yBAAyB;IACtC;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAG,YAAY,GAAG,SAAS,CAAC;IAC7C,oCAAoC;IACpC,QAAQ,CAAC,OAAO,CAAC,EAAG,YAAY,GAAG,SAAS,CAAC;IAC7C,0EAA0E;IAC1E,QAAQ,CAAC,OAAO,CAAC,EAAG,MAAM,GAAG,SAAS,CAAC;IACvC;;;;OAIG;IACH,QAAQ,CAAC,GAAG,CAAC,EAAG,OAAO,GAAG,SAAS,CAAC;CACvC;AAYD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAG,yBAA8B,GAAI,MAAM,CA4C9E"}
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/vite/plugin.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAGnC,mEAAmE;AACnE,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,CAAC,CAAC,EAAE,EAAG,MAAM,KAAK,OAAO,CAAC,CAAC;AAE/D,sCAAsC;AACtC,MAAM,WAAW,yBAAyB;IACtC;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAG,YAAY,GAAG,SAAS,CAAC;IAC7C,oCAAoC;IACpC,QAAQ,CAAC,OAAO,CAAC,EAAG,YAAY,GAAG,SAAS,CAAC;IAC7C,0EAA0E;IAC1E,QAAQ,CAAC,OAAO,CAAC,EAAG,MAAM,GAAG,SAAS,CAAC;IACvC;;;OAGG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAG,OAAO,GAAG,SAAS,CAAC;IACtC;;;;OAIG;IACH,QAAQ,CAAC,GAAG,CAAC,EAAG,OAAO,GAAG,SAAS,CAAC;CACvC;AAYD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAG,yBAA8B,GAAI,MAAM,CA4C9E"}
@@ -58,7 +58,7 @@ export function asyncContext(options = {}) {
58
58
  configFile: false,
59
59
  sourceType: "unambiguous",
60
60
  sourceMaps: true,
61
- presets: [[preset, { runtime }]],
61
+ presets: [[preset, { runtime, names: options.names }]],
62
62
  });
63
63
  const changed = result?.metadata?.[METADATA_KEY] === true;
64
64
  if (result?.code == null || !changed) {
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/vite/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,MAAM,EAAE,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAwB3E,MAAM,MAAM,GAAG,iBAAiB,CAAC;AACjC,oFAAoF;AACpF,MAAM,YAAY,GAAG,mCAAmC,CAAC;AACzD,qEAAqE;AACrE,MAAM,WAAW,GAAG,0CAA0C,CAAC;AAE/D,SAAS,OAAO,CAAC,MAAqB,EAAE,EAAW;IAC/C,OAAO,MAAM,YAAY,MAAM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,CAAC;gBACD,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;oBAC3B,OAAO,IAAI,CAAC;gBAChB,CAAC;gBACD,MAAM,MAAM,GAAG,MAAM,cAAc,CAAC,IAAI,EAAE;oBACtC,QAAQ,EAAG,IAAI;oBACf,OAAO,EAAG,KAAK;oBACf,UAAU,EAAG,KAAK;oBAClB,UAAU,EAAG,aAAa;oBAC1B,UAAU,EAAG,IAAI;oBACjB,OAAO,EAAG,CAAC,CAAC,MAAM,EAAE,EAAE,OAAO,EAAE,KAAK,EAAG,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC;iBAC3D,CAAC,CAAC;gBACH,MAAM,OAAO,GAAI,MAAM,EAAE,QAAgD,EAAE,CAAC,YAAY,CAAC,KAAK,IAAI,CAAC;gBACnG,IAAI,MAAM,EAAE,IAAI,IAAI,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;oBACnC,OAAO,IAAI,CAAC;gBAChB,CAAC;gBACD,+DAA+D;gBAC/D,OAAO,EAAE,IAAI,EAAG,MAAM,CAAC,IAAI,EAAE,GAAG,EAAG,MAAM,CAAC,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;YAChG,CAAC;SACJ;KACJ,CAAC;AACN,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "async-browser-context",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
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",
@@ -106,7 +106,7 @@
106
106
  "build": "tsc -p tsconfig.build.json",
107
107
  "typecheck": "tsc -p tsconfig.json",
108
108
  "test": "vitest run",
109
- "test:node": "vitest run --project unit --project \"rules (browser runtime, Node.js)\" --project \"rules (node runtime)\" --project \"rules (node runtime, no transform)\"",
109
+ "test:node": "vitest run --project unit --project \"rules (browser runtime, Node.js)\" --project \"rules (browser runtime, SWC ES2016)\" --project \"rules (browser runtime, esbuild ES2016)\" --project \"rules (node runtime)\" --project \"rules (node runtime, no transform)\"",
110
110
  "test:browser": "vitest run --project \"browser (chromium)\" --project \"browser (firefox)\" --project \"browser (webkit)\"",
111
111
  "coverage": "vitest run --coverage --project unit --project \"rules (browser runtime, Node.js)\" --project \"rules (node runtime)\"",
112
112
  "mutation": "stryker run",
@@ -118,7 +118,10 @@
118
118
  "site:build": "pnpm --filter ./site build",
119
119
  "prepublishOnly": "pnpm build && pnpm test",
120
120
  "site:data": "tsx scripts/site-data.ts",
121
- "readme:media": "tsx scripts/readme-media.ts"
121
+ "readme:media": "tsx scripts/readme-media.ts",
122
+ "test:bundlers": "pnpm build && tsx test/bundlers/run.ts",
123
+ "bench:size": "tsx bench/size.ts",
124
+ "bench:browsers": "tsx bench/browsers.ts"
122
125
  },
123
126
  "dependencies": {
124
127
  "@babel/core": "^8.0.7",
@@ -141,10 +144,12 @@
141
144
  "devDependencies": {
142
145
  "@opentelemetry/api": "^1.9.1",
143
146
  "@stryker-mutator/core": "^10.0.0",
147
+ "@swc/core": "^1.16.18",
144
148
  "@types/node": "^25.3.0",
145
149
  "@vitest/browser": "^5.0.3",
146
150
  "@vitest/browser-playwright": "^5.0.3",
147
151
  "@vitest/coverage-v8": "^5.0.3",
152
+ "esbuild": "^0.28.2",
148
153
  "playwright": "^1.64.0",
149
154
  "tinybench": "^6.2.1",
150
155
  "tsx": "^4.23.15",
@@ -1,8 +1,9 @@
1
- import type { PresetAPI, PresetObject } from "@babel/core";
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
4
  import { bindGeneratorsPlugin } from "./bind-generators.js";
5
5
  import { nameCoroutinesPlugin } from "./name-coroutines.js";
6
+ import { skipRuntimePlugin } from "./skip-runtime.js";
6
7
 
7
8
  export { METADATA_KEY } from "./bind-generators.js";
8
9
 
@@ -13,6 +14,13 @@ export interface PresetOptions {
13
14
  * `bindGenerator`. The default is `async-browser-context/runtime`.
14
15
  */
15
16
  readonly runtime? : string | undefined;
17
+ /**
18
+ * Give the name of each async function to `coroutine`, for the callback
19
+ * hooks. The default is `true`. With `false`, the hooks get no name for the
20
+ * steps after an `await`. The code is then smaller by 0.1 to 1.6 percent
21
+ * of its gzip size (`pnpm bench:size`).
22
+ */
23
+ readonly names? : boolean | undefined;
16
24
  }
17
25
 
18
26
  /** The default runtime module. */
@@ -20,15 +28,19 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
20
28
 
21
29
  /**
22
30
  * The Babel preset of `async-browser-context`. It changes the code so that the
23
- * context stays through `await` and `yield`:
31
+ * context stays through `await` and `yield`. It does not change the runtime
32
+ * of the library, also when it transforms `node_modules`.
24
33
  *
25
- * 1. A plugin of this library binds each generator to the context of its
34
+ * 1. A plugin of this library skips the native async functions of the
35
+ * runtime, which a directive marks.
36
+ * 2. A plugin of this library binds each generator to the context of its
26
37
  * creation.
27
- * 2. A plugin of this library gives the name of each async function to
28
- * `coroutine`, for the callback hooks.
29
- * 3. `@babel/plugin-transform-async-generator-functions` changes async
38
+ * 3. A plugin of this library gives the name of each async function to
39
+ * `coroutine`, for the callback hooks. The option `names: false` removes
40
+ * this step.
41
+ * 4. `@babel/plugin-transform-async-generator-functions` changes async
30
42
  * generators and `for await` loops.
31
- * 4. `@babel/plugin-transform-async-to-generator` changes each async function
43
+ * 5. `@babel/plugin-transform-async-to-generator` changes each async function
32
44
  * into a generator that the `coroutine` function of the runtime operates.
33
45
  *
34
46
  * The preset operates with Babel 7.22 and later and with Babel 8. Put it last
@@ -37,10 +49,12 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
37
49
  export default function asyncBrowserContextPreset(api : PresetAPI, options : PresetOptions = {}) : PresetObject {
38
50
  api.assertVersion("^7.22.0 || ^8.0.0");
39
51
  const runtime = options.runtime ?? DEFAULT_RUNTIME;
52
+ const names : PluginItem[] = options.names === false ? [] : [[nameCoroutinesPlugin, { runtime }]];
40
53
  return {
41
54
  plugins : [
55
+ skipRuntimePlugin,
42
56
  [bindGeneratorsPlugin, { runtime }],
43
- [nameCoroutinesPlugin, { runtime }],
57
+ ...names,
44
58
  asyncGeneratorFunctions,
45
59
  [asyncToGenerator, { module : runtime, method : "coroutine" }],
46
60
  ],
@@ -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
+ }
@@ -32,46 +32,64 @@ export function coroutine<T, A extends unknown[], R>(
32
32
  generatorFunction : (this : T, ...args : A) => Generator<unknown, R, unknown>,
33
33
  name? : string,
34
34
  ) : (this : T, ...args : A) => Promise<R> {
35
- const { Promise : NativePromise, promiseThen, promiseResolve } = store.intrinsics;
35
+ const { promiseResolve } = store.intrinsics;
36
36
  return function asyncFunction(this : T, ...args : A) : Promise<R> {
37
37
  // The context of the call. The body can change it with AsyncLocalStorage.enterWith.
38
38
  let frame : Frame = store.current;
39
- const generator = generatorFunction.apply(this, args);
40
- return new NativePromise<R>((resolve, reject) => {
41
- /** `resumed` is `true` for a step after an `await`. Only these steps start the hook methods. */
42
- const step = (resume : (argument : unknown) => IteratorResult<unknown, R>, argument : unknown, resumed : boolean) : void => {
43
- const previous = store.current;
44
- store.current = frame;
45
- let result : IteratorResult<unknown, R>;
46
- let awaited : Promise<unknown>;
47
- try {
48
- const hooks = resumed ? store.hooks : undefined;
49
- result = hooks === undefined
50
- ? resume(argument)
51
- : applyWithHooks(hooks, namedForHooks(generatorFunction, name), "await", resume, undefined, [argument]);
52
- if (result.done) {
53
- frame = store.current;
54
- store.current = previous;
55
- resolve(result.value);
56
- return;
57
- }
58
- // Promise.resolve reads a "then" property of the value. Do it in the context of the body, as await does.
59
- awaited = promiseResolve(result.value);
60
- } catch (error) {
61
- store.current = previous;
62
- reject(error);
63
- return;
39
+ let generator : Generator<unknown, R, unknown>;
40
+ const next = (value : unknown) : IteratorResult<unknown, R> => generator.next(value);
41
+ const throwInto = (error : unknown) : IteratorResult<unknown, R> => generator.throw(error);
42
+
43
+ /**
44
+ * This function does one step of the body in the context of the async function, and sets
45
+ * the previous context again. `resumed` is `true` for a step after an `await`. Only these
46
+ * steps start the hook methods. The function gives the value to await.
47
+ */
48
+ const step = (resume : (argument : unknown) => IteratorResult<unknown, R>, argument : unknown, resumed : boolean) : IteratorResult<unknown, R> => {
49
+ const previous = store.current;
50
+ store.current = frame;
51
+ try {
52
+ const hooks = resumed ? store.hooks : undefined;
53
+ const result = hooks === undefined
54
+ ? resume(argument)
55
+ : applyWithHooks(hooks, namedForHooks(generatorFunction, name), "await", resume, undefined, [argument]);
56
+ const value : unknown = result.value;
57
+ if (!result.done && ((typeof value === "object" && value !== null) || typeof value === "function")) {
58
+ // Promise.resolve reads the "then" property of an object. Do it in the context of the body, as await does.
59
+ // The result object is new for each step, so the change is not visible to other code.
60
+ (result as { value : unknown }).value = promiseResolve(value);
64
61
  }
62
+ return result;
63
+ } finally {
65
64
  frame = store.current;
66
65
  store.current = previous;
67
- promiseThen.call(awaited, onFulfilled, onRejected);
68
- };
69
- const next = (value : unknown) : IteratorResult<unknown, R> => generator.next(value);
70
- const throwInto = (error : unknown) : IteratorResult<unknown, R> => generator.throw(error);
71
- const onFulfilled = (value : unknown) : void => step(next, value, true);
72
- const onRejected = (error : unknown) : void => step(throwInto, error, true);
73
- step(next, undefined, false);
74
- });
66
+ }
67
+ };
68
+
69
+ /**
70
+ * The driver is a native async function: its `await` is faster than a `then` callback,
71
+ * mostly in Firefox. Each `await` takes the same number of microtask turns as the
72
+ * `await` of the source. Between the steps, the context of the caller stays current.
73
+ */
74
+ const drive = async () : Promise<R> => {
75
+ // The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
76
+ "async-browser-context runtime";
77
+ // In the driver, an error of the parameters rejects the promise, as with a native async function
78
+ generator = generatorFunction.apply(this, args);
79
+ let result = step(next, undefined, false);
80
+ while (result.done !== true) {
81
+ let value : unknown;
82
+ try {
83
+ value = await result.value;
84
+ } catch (error) {
85
+ result = step(throwInto, error, true);
86
+ continue;
87
+ }
88
+ result = step(next, value, true);
89
+ }
90
+ return result.value;
91
+ };
92
+ return drive();
75
93
  };
76
94
  }
77
95
 
@@ -8,31 +8,28 @@
8
8
  */
9
9
  import { AsyncLocalStorage } from "node:async_hooks";
10
10
 
11
- /** This function gives an async function that operates `generatorFunction`. */
11
+ /**
12
+ * This function gives an async function that operates `generatorFunction`. The function is
13
+ * a native async function: each `await` of the source becomes one native `await`, with
14
+ * the same microtask turns. An error of the parameters rejects the promise.
15
+ */
12
16
  export function coroutine<T, A extends unknown[], R>(generatorFunction : (this : T, ...args : A) => Generator<unknown, R, unknown>) : (this : T, ...args : A) => Promise<R> {
13
- return function asyncFunction(this : T, ...args : A) : Promise<R> {
17
+ return async function asyncFunction(this : T, ...args : A) : Promise<R> {
18
+ // The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
19
+ "async-browser-context runtime";
14
20
  const generator = generatorFunction.apply(this, args);
15
- return new Promise<R>((resolve, reject) => {
16
- const step = (method : "next" | "throw", argument : unknown) : void => {
17
- let result : IteratorResult<unknown, R>;
18
- let awaited : Promise<unknown>;
19
- try {
20
- result = method === "next" ? generator.next(argument) : generator.throw(argument);
21
- if (result.done) {
22
- resolve(result.value);
23
- return;
24
- }
25
- awaited = Promise.resolve(result.value);
26
- } catch (error) {
27
- reject(error);
28
- return;
29
- }
30
- awaited.then(onFulfilled, onRejected);
31
- };
32
- const onFulfilled = (value : unknown) : void => step("next", value);
33
- const onRejected = (error : unknown) : void => step("throw", error);
34
- step("next", undefined);
35
- });
21
+ let result = generator.next();
22
+ while (result.done !== true) {
23
+ let value : unknown;
24
+ try {
25
+ value = await result.value;
26
+ } catch (error) {
27
+ result = generator.throw(error);
28
+ continue;
29
+ }
30
+ result = generator.next(value);
31
+ }
32
+ return result.value;
36
33
  };
37
34
  }
38
35
 
@@ -16,6 +16,11 @@ export interface AsyncContextPluginOptions {
16
16
  readonly exclude? : ModuleFilter | undefined;
17
17
  /** The runtime module. The default is `async-browser-context/runtime`. */
18
18
  readonly runtime? : string | undefined;
19
+ /**
20
+ * Give the name of each async function to the callback hooks. The default
21
+ * is `true`. Refer to the option `names` of the Babel preset.
22
+ */
23
+ readonly names? : boolean | undefined;
19
24
  /**
20
25
  * Also transform the modules of server-side rendering. The default is
21
26
  * `false`, because on Node.js the native `AsyncLocalStorage` keeps the
@@ -84,7 +89,7 @@ export function asyncContext(options : AsyncContextPluginOptions = {}) : Plugin
84
89
  configFile : false,
85
90
  sourceType : "unambiguous",
86
91
  sourceMaps : true,
87
- presets : [[preset, { runtime }]],
92
+ presets : [[preset, { runtime, names : options.names }]],
88
93
  });
89
94
  const changed = (result?.metadata as Record<string, unknown> | undefined)?.[METADATA_KEY] === true;
90
95
  if (result?.code == null || !changed) {