async-browser-context 0.2.1 → 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 +13 -0
- package/README.md +3 -0
- package/dist/babel/preset.d.ts +8 -5
- package/dist/babel/preset.d.ts.map +1 -1
- package/dist/babel/preset.js +10 -5
- package/dist/babel/preset.js.map +1 -1
- package/dist/babel/skip-runtime.d.ts +16 -0
- package/dist/babel/skip-runtime.d.ts.map +1 -0
- package/dist/babel/skip-runtime.js +29 -0
- package/dist/babel/skip-runtime.js.map +1 -0
- package/dist/core/coroutine.d.ts.map +1 -1
- package/dist/core/coroutine.js +51 -34
- package/dist/core/coroutine.js.map +1 -1
- package/dist/node/runtime.d.ts +5 -1
- package/dist/node/runtime.d.ts.map +1 -1
- package/dist/node/runtime.js +21 -24
- package/dist/node/runtime.js.map +1 -1
- package/package.json +3 -2
- package/src/babel/preset.ts +10 -5
- package/src/babel/skip-runtime.ts +32 -0
- package/src/core/coroutine.ts +52 -34
- package/src/node/runtime.ts +20 -23
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
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
|
+
|
|
3
16
|
## 0.2.1
|
|
4
17
|
|
|
5
18
|
### New functions
|
package/README.md
CHANGED
|
@@ -274,6 +274,8 @@ One read with `get()` takes approximately 4 ns. The [documentation website](http
|
|
|
274
274
|
|
|
275
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.
|
|
276
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
|
+
|
|
277
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`).
|
|
278
280
|
|
|
279
281
|
## Tests
|
|
@@ -288,6 +290,7 @@ The runtime is 5.9 kB with gzip, and zone.js 0.16.3 is 13.3 kB. The transform ad
|
|
|
288
290
|
| `pnpm test:bundlers` | Packs the package, builds a page with webpack, Rspack, Rollup, esbuild and Next.js, and examines each page in Chromium |
|
|
289
291
|
| `pnpm mutation` | Mutation testing with Stryker |
|
|
290
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 |
|
|
291
294
|
| `pnpm bench:size` | The size of the runtime, and the size that the transform adds to a set of npm packages |
|
|
292
295
|
| `pnpm lint:ste` | The STE linter of the documentation and the TSDoc comments |
|
|
293
296
|
|
package/dist/babel/preset.d.ts
CHANGED
|
@@ -19,16 +19,19 @@ export interface PresetOptions {
|
|
|
19
19
|
export declare const DEFAULT_RUNTIME = "async-browser-context/runtime";
|
|
20
20
|
/**
|
|
21
21
|
* The Babel preset of `async-browser-context`. It changes the code so that the
|
|
22
|
-
* 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`.
|
|
23
24
|
*
|
|
24
|
-
* 1. A plugin of this library
|
|
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
|
|
25
28
|
* creation.
|
|
26
|
-
*
|
|
29
|
+
* 3. A plugin of this library gives the name of each async function to
|
|
27
30
|
* `coroutine`, for the callback hooks. The option `names: false` removes
|
|
28
31
|
* this step.
|
|
29
|
-
*
|
|
32
|
+
* 4. `@babel/plugin-transform-async-generator-functions` changes async
|
|
30
33
|
* generators and `for await` loops.
|
|
31
|
-
*
|
|
34
|
+
* 5. `@babel/plugin-transform-async-to-generator` changes each async function
|
|
32
35
|
* into a generator that the `coroutine` function of the runtime operates.
|
|
33
36
|
*
|
|
34
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,EAAc,SAAS,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;
|
|
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"}
|
package/dist/babel/preset.js
CHANGED
|
@@ -2,21 +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
|
|
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
|
-
*
|
|
18
|
+
* 3. A plugin of this library gives the name of each async function to
|
|
15
19
|
* `coroutine`, for the callback hooks. The option `names: false` removes
|
|
16
20
|
* this step.
|
|
17
|
-
*
|
|
21
|
+
* 4. `@babel/plugin-transform-async-generator-functions` changes async
|
|
18
22
|
* generators and `for await` loops.
|
|
19
|
-
*
|
|
23
|
+
* 5. `@babel/plugin-transform-async-to-generator` changes each async function
|
|
20
24
|
* into a generator that the `coroutine` function of the runtime operates.
|
|
21
25
|
*
|
|
22
26
|
* The preset operates with Babel 7.22 and later and with Babel 8. Put it last
|
|
@@ -28,6 +32,7 @@ export default function asyncBrowserContextPreset(api, options = {}) {
|
|
|
28
32
|
const names = options.names === false ? [] : [[nameCoroutinesPlugin, { runtime }]];
|
|
29
33
|
return {
|
|
30
34
|
plugins: [
|
|
35
|
+
skipRuntimePlugin,
|
|
31
36
|
[bindGeneratorsPlugin, { runtime }],
|
|
32
37
|
...names,
|
|
33
38
|
asyncGeneratorFunctions,
|
package/dist/babel/preset.js.map
CHANGED
|
@@ -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;
|
|
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;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEtD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAkBpD,kCAAkC;AAClC,MAAM,CAAC,MAAM,eAAe,GAAG,+BAA+B,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAe,EAAE,UAA0B,EAAE;IAC3F,GAAG,CAAC,aAAa,CAAC,mBAAmB,CAAC,CAAC;IACvC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,eAAe,CAAC;IACnD,MAAM,KAAK,GAAkB,OAAO,CAAC,KAAK,KAAK,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,oBAAoB,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;IAClG,OAAO;QACH,OAAO,EAAG;YACN,iBAAiB;YACjB,CAAC,oBAAoB,EAAE,EAAE,OAAO,EAAE,CAAC;YACnC,GAAG,KAAK;YACR,uBAAuB;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,
|
|
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"}
|
package/dist/core/coroutine.js
CHANGED
|
@@ -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 {
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
return;
|
|
79
|
+
result = step(throwInto, error, true);
|
|
80
|
+
continue;
|
|
59
81
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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,
|
|
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,IA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AAC,MAAM,EAAE,QAAQ,EAAG,IAAI,EAAE,YAAY,EAAG,IAAI,EAAE;KAC5E,CAAC,CAAC;IACH,OAAO,KAAK,CAAC;AACjB,CAAC"}
|
package/dist/node/runtime.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
/**
|
|
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
|
|
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"}
|
package/dist/node/runtime.js
CHANGED
|
@@ -7,32 +7,29 @@
|
|
|
7
7
|
* @packageDocumentation
|
|
8
8
|
*/
|
|
9
9
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
10
|
-
/**
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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). */
|
package/dist/node/runtime.js.map
CHANGED
|
@@ -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
|
|
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,C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.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",
|
|
@@ -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",
|
package/src/babel/preset.ts
CHANGED
|
@@ -3,6 +3,7 @@ import asyncGeneratorFunctions from "@babel/plugin-transform-async-generator-fun
|
|
|
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
|
|
|
@@ -27,16 +28,19 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
|
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
30
|
* The Babel preset of `async-browser-context`. It changes the code so that the
|
|
30
|
-
* 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`.
|
|
31
33
|
*
|
|
32
|
-
* 1. A plugin of this library
|
|
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
|
|
33
37
|
* creation.
|
|
34
|
-
*
|
|
38
|
+
* 3. A plugin of this library gives the name of each async function to
|
|
35
39
|
* `coroutine`, for the callback hooks. The option `names: false` removes
|
|
36
40
|
* this step.
|
|
37
|
-
*
|
|
41
|
+
* 4. `@babel/plugin-transform-async-generator-functions` changes async
|
|
38
42
|
* generators and `for await` loops.
|
|
39
|
-
*
|
|
43
|
+
* 5. `@babel/plugin-transform-async-to-generator` changes each async function
|
|
40
44
|
* into a generator that the `coroutine` function of the runtime operates.
|
|
41
45
|
*
|
|
42
46
|
* The preset operates with Babel 7.22 and later and with Babel 8. Put it last
|
|
@@ -48,6 +52,7 @@ export default function asyncBrowserContextPreset(api : PresetAPI, options : Pre
|
|
|
48
52
|
const names : PluginItem[] = options.names === false ? [] : [[nameCoroutinesPlugin, { runtime }]];
|
|
49
53
|
return {
|
|
50
54
|
plugins : [
|
|
55
|
+
skipRuntimePlugin,
|
|
51
56
|
[bindGeneratorsPlugin, { runtime }],
|
|
52
57
|
...names,
|
|
53
58
|
asyncGeneratorFunctions,
|
|
@@ -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
|
+
}
|
package/src/core/coroutine.ts
CHANGED
|
@@ -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 {
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
// Promise.resolve reads
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
|
package/src/node/runtime.ts
CHANGED
|
@@ -8,31 +8,28 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
10
10
|
|
|
11
|
-
/**
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
|