async-browser-context 0.1.1 → 0.2.0
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 +26 -3
- package/dist/babel/name-coroutines.d.ts +25 -0
- package/dist/babel/name-coroutines.d.ts.map +1 -0
- package/dist/babel/name-coroutines.js +116 -0
- package/dist/babel/name-coroutines.js.map +1 -0
- package/dist/babel/preset.d.ts +4 -2
- package/dist/babel/preset.d.ts.map +1 -1
- package/dist/babel/preset.js +6 -2
- package/dist/babel/preset.js.map +1 -1
- package/dist/core/callback-hook.d.ts +24 -0
- package/dist/core/callback-hook.d.ts.map +1 -0
- package/dist/core/callback-hook.js +13 -0
- package/dist/core/callback-hook.js.map +1 -0
- package/dist/core/coroutine.d.ts +2 -12
- package/dist/core/coroutine.d.ts.map +1 -1
- package/dist/core/coroutine.js +23 -8
- package/dist/core/coroutine.js.map +1 -1
- package/dist/core/hooks.d.ts +15 -0
- package/dist/core/hooks.d.ts.map +1 -0
- package/dist/core/hooks.js +28 -0
- package/dist/core/hooks.js.map +1 -0
- package/dist/core/store.d.ts +28 -3
- package/dist/core/store.d.ts.map +1 -1
- package/dist/core/store.js +81 -7
- package/dist/core/store.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/node/index.d.ts +10 -0
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +14 -0
- package/dist/node/index.js.map +1 -1
- package/dist/patches/callbacks.d.ts.map +1 -1
- package/dist/patches/callbacks.js +10 -8
- package/dist/patches/callbacks.js.map +1 -1
- package/dist/patches/events.d.ts.map +1 -1
- package/dist/patches/events.js +39 -22
- package/dist/patches/events.js.map +1 -1
- package/dist/patches/handler-interfaces.d.ts +16 -0
- package/dist/patches/handler-interfaces.d.ts.map +1 -0
- package/dist/patches/handler-interfaces.js +42 -0
- package/dist/patches/handler-interfaces.js.map +1 -0
- package/dist/patches/observers.js +1 -1
- package/dist/patches/observers.js.map +1 -1
- package/dist/patches/patch.d.ts +5 -2
- package/dist/patches/patch.d.ts.map +1 -1
- package/dist/patches/patch.js +6 -3
- package/dist/patches/patch.js.map +1 -1
- package/dist/patches/promise.js +1 -1
- package/dist/patches/promise.js.map +1 -1
- package/dist/patches/streams.d.ts.map +1 -1
- package/dist/patches/streams.js +12 -7
- package/dist/patches/streams.js.map +1 -1
- package/dist/patches/timers.d.ts.map +1 -1
- package/dist/patches/timers.js +6 -5
- package/dist/patches/timers.js.map +1 -1
- package/package.json +2 -1
- package/src/babel/name-coroutines.ts +128 -0
- package/src/babel/preset.ts +6 -2
- package/src/core/callback-hook.ts +34 -0
- package/src/core/coroutine.ts +27 -8
- package/src/core/hooks.ts +30 -0
- package/src/core/store.ts +108 -7
- package/src/index.ts +1 -0
- package/src/node/index.ts +16 -0
- package/src/patches/callbacks.ts +10 -8
- package/src/patches/events.ts +38 -22
- package/src/patches/handler-interfaces.ts +41 -0
- package/src/patches/observers.ts +1 -1
- package/src/patches/patch.ts +6 -3
- package/src/patches/promise.ts +2 -2
- package/src/patches/streams.ts +12 -7
- package/src/patches/timers.ts +6 -5
package/dist/patches/patch.js
CHANGED
|
@@ -63,14 +63,17 @@ export function globalPrototype(name) {
|
|
|
63
63
|
const prototype = constructor.prototype;
|
|
64
64
|
return typeof prototype === "object" && prototype !== null ? prototype : undefined;
|
|
65
65
|
}
|
|
66
|
-
/**
|
|
67
|
-
|
|
66
|
+
/**
|
|
67
|
+
* This function wraps each function argument with the current frame. Other arguments do not change.
|
|
68
|
+
* `source` is the name of the API, for the callback hooks.
|
|
69
|
+
*/
|
|
70
|
+
export function wrapFunctionArguments(args, source) {
|
|
68
71
|
const frame = store.current;
|
|
69
72
|
let changed;
|
|
70
73
|
for (const [index, argument] of args.entries()) {
|
|
71
74
|
if (typeof argument === "function") {
|
|
72
75
|
changed ??= [...args];
|
|
73
|
-
changed[index] = bindToFrame(frame, argument);
|
|
76
|
+
changed[index] = bindToFrame(frame, argument, source);
|
|
74
77
|
}
|
|
75
78
|
}
|
|
76
79
|
return changed ?? args;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"patch.js","sourceRoot":"","sources":["../../src/patches/patch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAEtD;;;;GAIG;AACH,MAAM,UAAU,KAAK,CAAC,IAAa;IAC/B,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;QAC1B,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACxB,OAAO,IAAI,CAAC;AAChB,CAAC;AAED,gIAAgI;AAChI,MAAM,UAAU,gBAAgB,CAAC,WAAoB,EAAE,QAAiB;IACpE,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,CAAC;AAChF,CAAC;AAED,6GAA6G;AAC7G,SAAS,sBAAsB,CAAC,WAAoB,EAAE,QAAiB;IACnE,KAAK,MAAM,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC1C,iDAAiD;QACjD,IAAI,GAAG,KAAK,WAAW,EAAE,CAAC;YACtB,SAAS;QACb,CAAC;QACD,MAAM,UAAU,GAAG,MAAM,CAAC,wBAAwB,CAAC,QAAQ,EAAE,GAAG,CAAuB,CAAC;QACxF,IAAI,CAAC;YACD,MAAM,CAAC,cAAc,CAAC,WAAW,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC;QACxD,CAAC;QAAC,MAAM,CAAC;YACL,+DAA+D;QACnE,CAAC;IACL,CAAC;AACL,CAAC;AAID;;;;;GAKG;AACH,MAAM,UAAU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F,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAgB,EAAE,MAAe;IACnE,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC;IAC5B,IAAI,OAA+B,CAAC;IACpC,KAAK,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,IAAI,IAAI,CAAC,OAAO,EAAE,EAAE,CAAC;QAC7C,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE,CAAC;YACjC,OAAO,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC;YACtB,OAAO,CAAC,KAAK,CAAC,GAAG,WAAW,CAAC,KAAK,EAAE,QAA8C,EAAE,MAAM,CAAC,CAAC;QAChG,CAAC;IACL,CAAC;IACD,OAAO,OAAO,IAAI,IAAI,CAAC;AAC3B,CAAC"}
|
package/dist/patches/promise.js
CHANGED
|
@@ -18,7 +18,7 @@ export function installPromisePatch() {
|
|
|
18
18
|
const nativeThen = store.intrinsics.promiseThen;
|
|
19
19
|
replaceFunction(Promise.prototype, "then", () => function then(onFulfilled, onRejected) {
|
|
20
20
|
const frame = store.current;
|
|
21
|
-
return nativeThen.call(this, typeof onFulfilled === "function" ? bindOneArgument(frame, onFulfilled) : onFulfilled, typeof onRejected === "function" ? bindOneArgument(frame, onRejected) : onRejected);
|
|
21
|
+
return nativeThen.call(this, typeof onFulfilled === "function" ? bindOneArgument(frame, onFulfilled, "Promise.then") : onFulfilled, typeof onRejected === "function" ? bindOneArgument(frame, onRejected, "Promise.then") : onRejected);
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
24
|
//# sourceMappingURL=promise.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promise.js","sourceRoot":"","sources":["../../src/patches/promise.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAE,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAIpD;;;;;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AAU,CAAC,CAAC,CAAC,eAAe,CAAC,KAAK,EAAE,UAAU,EAAE,cAAc,CAAC,CAAC,CAAC,CAAC,UAAU,CACjF,CAAC;IAC1B,CAAS,CAAC,CAAC;AACf,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streams.d.ts","sourceRoot":"","sources":["../../src/patches/streams.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"streams.d.ts","sourceRoot":"","sources":["../../src/patches/streams.ts"],"names":[],"mappings":"AAgDA,qDAAqD;AACrD,wBAAgB,oBAAoB,IAAK,IAAI,CA8B5C"}
|
package/dist/patches/streams.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { store } from "../core/store.js";
|
|
1
|
+
import { applyWithHooks, store } from "../core/store.js";
|
|
2
2
|
import { claim, ownDataValue, rememberOriginal } from "./patch.js";
|
|
3
3
|
/**
|
|
4
4
|
* The stream constructors and the methods of the object that each constructor
|
|
@@ -16,19 +16,24 @@ const STREAMS = [
|
|
|
16
16
|
* Each method gets `underlying` as its `this` value, as the specification
|
|
17
17
|
* tells.
|
|
18
18
|
*/
|
|
19
|
-
function wrapUnderlying(underlying, methods, frame) {
|
|
19
|
+
function wrapUnderlying(underlying, methods, frame, name) {
|
|
20
20
|
const wrapped = Object.create(underlying);
|
|
21
|
-
const
|
|
21
|
+
const properties = underlying;
|
|
22
22
|
for (const method of methods) {
|
|
23
|
-
const original =
|
|
23
|
+
const original = properties[method];
|
|
24
24
|
if (typeof original !== "function") {
|
|
25
25
|
continue;
|
|
26
26
|
}
|
|
27
|
-
|
|
27
|
+
const source = `${name}.${method}`;
|
|
28
|
+
wrapped[method] = function asyncBrowserContextWrapper(...args) {
|
|
28
29
|
const previous = store.current;
|
|
29
30
|
store.current = frame;
|
|
30
31
|
try {
|
|
31
|
-
|
|
32
|
+
const hooks = store.hooks;
|
|
33
|
+
if (hooks === undefined) {
|
|
34
|
+
return original.apply(underlying, args);
|
|
35
|
+
}
|
|
36
|
+
return applyWithHooks(hooks, original, source, original, underlying, args);
|
|
32
37
|
}
|
|
33
38
|
finally {
|
|
34
39
|
store.current = previous;
|
|
@@ -53,7 +58,7 @@ export function installStreamPatches() {
|
|
|
53
58
|
construct(target, args, newTarget) {
|
|
54
59
|
const underlying = args[0];
|
|
55
60
|
if (typeof underlying === "object" && underlying !== null) {
|
|
56
|
-
args[0] = wrapUnderlying(underlying, methods, store.current);
|
|
61
|
+
args[0] = wrapUnderlying(underlying, methods, store.current, name);
|
|
57
62
|
}
|
|
58
63
|
return Reflect.construct(target, args, newTarget);
|
|
59
64
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streams.js","sourceRoot":"","sources":["../../src/patches/streams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAc,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"streams.js","sourceRoot":"","sources":["../../src/patches/streams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,KAAK,EAAc,MAAM,kBAAkB,CAAC;AACrE,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnE;;;;GAIG;AACH,MAAM,OAAO,GAAuD;IAChE,CAAC,gBAAgB,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC;IAC/C,CAAC,gBAAgB,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IACxD,CAAC,iBAAiB,EAAE,CAAC,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;CACjE,CAAC;AAIF;;;;;GAKG;AACH,SAAS,cAAc,CAAC,UAAmB,EAAE,OAA2B,EAAE,KAAa,EAAE,IAAa;IAClG,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,UAAU,CAA4B,CAAC;IACrE,MAAM,UAAU,GAAG,U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sources":["../../src/patches/timers.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"timers.d.ts","sourceRoot":"","sources":["../../src/patches/timers.ts"],"names":[],"mappings":"AAmCA;;;;GAIG;AACH,wBAAgB,mBAAmB,IAAK,IAAI,CAe3C"}
|
package/dist/patches/timers.js
CHANGED
|
@@ -20,9 +20,10 @@ const PROTOTYPE_SCHEDULERS = [
|
|
|
20
20
|
["XRSession", "requestAnimationFrame"],
|
|
21
21
|
];
|
|
22
22
|
// Stryker restore all
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
/** `source` is the name of the scheduler, for the callback hooks. */
|
|
24
|
+
function wrapFirstArgument(source) {
|
|
25
|
+
return (original) => function (callback, ...rest) {
|
|
26
|
+
const wrapped = typeof callback === "function" ? bindToFrame(store.current, callback, source) : callback;
|
|
26
27
|
return original.call(this, wrapped, ...rest);
|
|
27
28
|
};
|
|
28
29
|
}
|
|
@@ -36,13 +37,13 @@ export function installTimerPatches() {
|
|
|
36
37
|
return;
|
|
37
38
|
}
|
|
38
39
|
for (const name of GLOBAL_SCHEDULERS) {
|
|
39
|
-
replaceFunction(globalThis, name, wrapFirstArgument);
|
|
40
|
+
replaceFunction(globalThis, name, wrapFirstArgument(name));
|
|
40
41
|
}
|
|
41
42
|
// Stryker disable all: Node.js has none of these classes. The browser tests examine them.
|
|
42
43
|
for (const [constructorName, method] of PROTOTYPE_SCHEDULERS) {
|
|
43
44
|
const prototype = globalPrototype(constructorName);
|
|
44
45
|
if (prototype !== undefined) {
|
|
45
|
-
replaceFunction(prototype, method, wrapFirstArgument);
|
|
46
|
+
replaceFunction(prototype, method, wrapFirstArgument(`${constructorName}.${method}`));
|
|
46
47
|
}
|
|
47
48
|
}
|
|
48
49
|
// Stryker restore all
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timers.js","sourceRoot":"","sources":["../../src/patches/timers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,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C,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;QAC7I,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC,CAAC;IACjD,CAAC,CAAC;AACN,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,mBAAmB;IAC/B,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC;QACnB,OAAO;IACX,CAAC;IACD,KAAK,MAAM,IAAI,IAAI,iBAAiB,EAAE,CAAC;QACnC,eAAe,CAAY,UAAU,EAAE,IAAI,EAAE,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC;IAC1E,CAAC;IACD,0FAA0F;IAC1F,KAAK,MAAM,CAAC,eAAe,EAAE,MAAM,CAAC,IAAI,oBAAoB,EAAE,CAAC;QAC3D,MAAM,SAAS,GAAG,eAAe,CAAC,eAAe,CAAC,CAAC;QACnD,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;YAC1B,eAAe,CAAY,SAAS,EAAE,MAAM,EAAE,iBAAiB,CAAC,GAAG,eAAe,IAAI,MAAM,EAAE,CAAC,CAAC,CAAC;QACrG,CAAC;IACL,CAAC;IACD,sBAAsB;AAC1B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "async-browser-context",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
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",
|
|
@@ -112,6 +112,7 @@
|
|
|
112
112
|
"mutation": "stryker run",
|
|
113
113
|
"bench": "pnpm build && tsx bench/run.ts",
|
|
114
114
|
"smoke": "pnpm build && tsx test/smoke/run.ts",
|
|
115
|
+
"test:babel": "pnpm build && tsx test/babel/run.ts",
|
|
115
116
|
"lint:ste": "tsx tools/ste-lint/src/cli.ts",
|
|
116
117
|
"site:dev": "pnpm --filter ./site dev",
|
|
117
118
|
"site:build": "pnpm --filter ./site build",
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { types, type NodePath, type PluginObject, type PluginPass } from "@babel/core";
|
|
2
|
+
|
|
3
|
+
/** The options of the plugin. */
|
|
4
|
+
export interface NameCoroutinesOptions {
|
|
5
|
+
/** The module that exports `coroutine`. */
|
|
6
|
+
readonly runtime : string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** The local names that Babel gives to the import of `coroutine`. */
|
|
10
|
+
const COROUTINE_NAME = /^_coroutine\d*$/;
|
|
11
|
+
|
|
12
|
+
/** The assignment operators that give a name to an anonymous function, as in the specification (NamedEvaluation). */
|
|
13
|
+
const NAMING_OPERATORS = new Set(["=", "&&=", "||=", "??="]);
|
|
14
|
+
|
|
15
|
+
/** This function gives the name of a property key, or `undefined` if the name is known only at run time. */
|
|
16
|
+
function keyName(key : types.Node, computed : boolean) : string | undefined {
|
|
17
|
+
if (types.isStringLiteral(key)) {
|
|
18
|
+
return key.value;
|
|
19
|
+
}
|
|
20
|
+
if (types.isNumericLiteral(key)) {
|
|
21
|
+
return String(key.value);
|
|
22
|
+
}
|
|
23
|
+
if (computed) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
if (types.isIdentifier(key)) {
|
|
27
|
+
return key.name;
|
|
28
|
+
}
|
|
29
|
+
return types.isPrivateName(key) ? `#${key.id.name}` : undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* This function gives the `name` that JavaScript gives to the function, or
|
|
34
|
+
* `undefined` if the name is not known before run time.
|
|
35
|
+
*/
|
|
36
|
+
export function functionName(path : NodePath<types.Function>) : string | undefined {
|
|
37
|
+
const node = path.node;
|
|
38
|
+
if ((types.isFunctionDeclaration(node) || types.isFunctionExpression(node)) && node.id) {
|
|
39
|
+
return node.id.name;
|
|
40
|
+
}
|
|
41
|
+
if (types.isObjectMethod(node) || types.isClassMethod(node) || types.isClassPrivateMethod(node)) {
|
|
42
|
+
return keyName(node.key, node.computed === true);
|
|
43
|
+
}
|
|
44
|
+
const parent = path.parent;
|
|
45
|
+
if (types.isVariableDeclarator(parent) && parent.init === node && types.isIdentifier(parent.id)) {
|
|
46
|
+
return parent.id.name;
|
|
47
|
+
}
|
|
48
|
+
if (types.isAssignmentExpression(parent) && parent.right === node && NAMING_OPERATORS.has(parent.operator) && types.isIdentifier(parent.left)) {
|
|
49
|
+
return parent.left.name;
|
|
50
|
+
}
|
|
51
|
+
if (types.isAssignmentPattern(parent) && parent.right === node && types.isIdentifier(parent.left)) {
|
|
52
|
+
return parent.left.name;
|
|
53
|
+
}
|
|
54
|
+
if ((types.isObjectProperty(parent) || types.isClassProperty(parent) || types.isClassPrivateProperty(parent)) && parent.value === node) {
|
|
55
|
+
return keyName(parent.key, "computed" in parent && parent.computed === true);
|
|
56
|
+
}
|
|
57
|
+
return types.isExportDefaultDeclaration(parent) ? "default" : undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** This function gives `true` if `callee` is the `coroutine` function of the runtime, also in CommonJS output. */
|
|
61
|
+
function isCoroutineCallee(callee : types.Node, path : NodePath, runtime : string) : boolean {
|
|
62
|
+
if (types.isIdentifier(callee)) {
|
|
63
|
+
return COROUTINE_NAME.test(callee.name) || path.referencesImport(runtime, "coroutine");
|
|
64
|
+
}
|
|
65
|
+
const member = types.isSequenceExpression(callee) ? callee.expressions.at(-1) : callee;
|
|
66
|
+
return types.isMemberExpression(member) && types.isIdentifier(member.property, { name : "coroutine" });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The key of the names of one file in the plugin state. */
|
|
70
|
+
const NAMES_KEY = "async-browser-context/coroutine-names";
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The Babel plugin that gives the name of each async function to `coroutine`:
|
|
74
|
+
* `_coroutine(function* () {...}, "load")`. The generator function of
|
|
75
|
+
* `@babel/plugin-transform-async-to-generator` has no name, and the callback
|
|
76
|
+
* hooks of the runtime show the name of the async function.
|
|
77
|
+
*
|
|
78
|
+
* In `Program` enter, before the async transform, the plugin records the
|
|
79
|
+
* name of each async function. In `Program` exit, it adds the name to each
|
|
80
|
+
* `coroutine` call. For a plain function, the async transform keeps the
|
|
81
|
+
* function node as the argument of `coroutine`. For a method, the call is in
|
|
82
|
+
* the body of the method.
|
|
83
|
+
*/
|
|
84
|
+
export function nameCoroutinesPlugin(_api : unknown, options : object) : PluginObject<PluginPass> {
|
|
85
|
+
const { runtime } = options as NameCoroutinesOptions;
|
|
86
|
+
return {
|
|
87
|
+
name : "async-browser-context/name-coroutines",
|
|
88
|
+
visitor : {
|
|
89
|
+
Program : {
|
|
90
|
+
enter(programPath, state) {
|
|
91
|
+
const names = new WeakMap<types.Node, string>();
|
|
92
|
+
programPath.traverse({
|
|
93
|
+
Function(path) {
|
|
94
|
+
if (!path.node.async || path.node.generator) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const name = functionName(path);
|
|
98
|
+
if (name !== undefined && name !== "") {
|
|
99
|
+
names.set(path.node, name);
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
state.set(NAMES_KEY, names);
|
|
104
|
+
},
|
|
105
|
+
exit(programPath, state) {
|
|
106
|
+
const names = state.get(NAMES_KEY) as WeakMap<types.Node, string>;
|
|
107
|
+
programPath.traverse({
|
|
108
|
+
CallExpression(path) {
|
|
109
|
+
const { callee, arguments : args } = path.node;
|
|
110
|
+
const [body] = args;
|
|
111
|
+
if (args.length !== 1 || !types.isFunctionExpression(body) || !body.generator) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (!isCoroutineCallee(callee, path.get("callee"), runtime)) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const parent = path.getFunctionParent()?.node;
|
|
118
|
+
const name = names.get(body) ?? (types.isMethod(parent) ? names.get(parent) : undefined);
|
|
119
|
+
if (name !== undefined) {
|
|
120
|
+
args.push(types.stringLiteral(name));
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
package/src/babel/preset.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { 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
|
+
import { nameCoroutinesPlugin } from "./name-coroutines.js";
|
|
5
6
|
|
|
6
7
|
export { METADATA_KEY } from "./bind-generators.js";
|
|
7
8
|
|
|
@@ -23,9 +24,11 @@ export const DEFAULT_RUNTIME = "async-browser-context/runtime";
|
|
|
23
24
|
*
|
|
24
25
|
* 1. A plugin of this library binds each generator to the context of its
|
|
25
26
|
* creation.
|
|
26
|
-
* 2.
|
|
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
|
|
27
30
|
* generators and `for await` loops.
|
|
28
|
-
*
|
|
31
|
+
* 4. `@babel/plugin-transform-async-to-generator` changes each async function
|
|
29
32
|
* into a generator that the `coroutine` function of the runtime operates.
|
|
30
33
|
*
|
|
31
34
|
* The preset operates with Babel 7.22 and later and with Babel 8. Put it last
|
|
@@ -37,6 +40,7 @@ export default function asyncBrowserContextPreset(api : PresetAPI, options : Pre
|
|
|
37
40
|
return {
|
|
38
41
|
plugins : [
|
|
39
42
|
[bindGeneratorsPlugin, { runtime }],
|
|
43
|
+
[nameCoroutinesPlugin, { runtime }],
|
|
40
44
|
asyncGeneratorFunctions,
|
|
41
45
|
[asyncToGenerator, { module : runtime, method : "coroutine" }],
|
|
42
46
|
],
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A callback hook. The library starts `before` immediately before each
|
|
3
|
+
* callback that it starts, and `after` immediately after the callback, also
|
|
4
|
+
* after an error. Both methods operate in the context of the callback, so
|
|
5
|
+
* `variable.get()` and `storage.getStore()` give the values of the callback.
|
|
6
|
+
*/
|
|
7
|
+
export interface CallbackHook {
|
|
8
|
+
/**
|
|
9
|
+
* The library starts this method before the callback.
|
|
10
|
+
*
|
|
11
|
+
* @param callback - The function that the library starts. For the step
|
|
12
|
+
* of an async function after an `await`, it is the body of the async
|
|
13
|
+
* function, with the name of the async function.
|
|
14
|
+
* @param source - The API that got the callback, for example
|
|
15
|
+
* `"setTimeout"`, `"Promise.then"`, `"addEventListener:click"`,
|
|
16
|
+
* `"onclick"` or `"await"`.
|
|
17
|
+
*/
|
|
18
|
+
before?(callback : Function, source : string) : void;
|
|
19
|
+
/** The library starts this method after the callback, also after an error. The arguments are the same as for `before`. */
|
|
20
|
+
after?(callback : Function, source : string) : void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** This function makes sure that `hook` is an object, and that `before` and `after` are functions or `undefined`. */
|
|
24
|
+
export function assertCallbackHook(hook : CallbackHook) : void {
|
|
25
|
+
if (typeof hook !== "object" || hook === null) {
|
|
26
|
+
throw new TypeError("addCallbackHook: the hook is not an object");
|
|
27
|
+
}
|
|
28
|
+
for (const phase of ["before", "after"] as const) {
|
|
29
|
+
const method : unknown = hook[phase];
|
|
30
|
+
if (method !== undefined && typeof method !== "function") {
|
|
31
|
+
throw new TypeError(`addCallbackHook: hook.${phase} is not a function`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/core/coroutine.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { store, type Frame } from "./store.js";
|
|
1
|
+
import { applyWithHooks, store, type Frame } from "./store.js";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* The code from the Babel preset uses these runtime functions. Do not use them
|
|
@@ -11,25 +11,44 @@ import { store, type Frame } from "./store.js";
|
|
|
11
11
|
* also in the same expression, and no other code gets this context.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* This function gives the generator function to the callback hooks, with the name of the
|
|
16
|
+
* async function. The preset gives the name, because the generator function
|
|
17
|
+
* of Babel has no name. The function sets the name only when a hook operates.
|
|
18
|
+
*/
|
|
19
|
+
function namedForHooks(generatorFunction : Function, name : string | undefined) : Function {
|
|
20
|
+
if (name !== undefined && generatorFunction.name !== name) {
|
|
21
|
+
Object.defineProperty(generatorFunction, "name", { value : name, configurable : true });
|
|
22
|
+
}
|
|
23
|
+
return generatorFunction;
|
|
24
|
+
}
|
|
25
|
+
|
|
14
26
|
/**
|
|
15
27
|
* This function gives an async function that operates `generatorFunction`. The preset gives
|
|
16
28
|
* this function to `@babel/plugin-transform-async-to-generator` as its
|
|
17
|
-
* `method` option.
|
|
29
|
+
* `method` option, and adds `name`, the name of the async function, if it has one.
|
|
18
30
|
*/
|
|
19
|
-
export function coroutine<T, A extends unknown[], R>(
|
|
31
|
+
export function coroutine<T, A extends unknown[], R>(
|
|
32
|
+
generatorFunction : (this : T, ...args : A) => Generator<unknown, R, unknown>,
|
|
33
|
+
name? : string,
|
|
34
|
+
) : (this : T, ...args : A) => Promise<R> {
|
|
20
35
|
const { Promise : NativePromise, promiseThen, promiseResolve } = store.intrinsics;
|
|
21
36
|
return function asyncFunction(this : T, ...args : A) : Promise<R> {
|
|
22
37
|
// The context of the call. The body can change it with AsyncLocalStorage.enterWith.
|
|
23
38
|
let frame : Frame = store.current;
|
|
24
39
|
const generator = generatorFunction.apply(this, args);
|
|
25
40
|
return new NativePromise<R>((resolve, reject) => {
|
|
26
|
-
|
|
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 => {
|
|
27
43
|
const previous = store.current;
|
|
28
44
|
store.current = frame;
|
|
29
45
|
let result : IteratorResult<unknown, R>;
|
|
30
46
|
let awaited : Promise<unknown>;
|
|
31
47
|
try {
|
|
32
|
-
|
|
48
|
+
const hooks = resumed ? store.hooks : undefined;
|
|
49
|
+
result = hooks === undefined
|
|
50
|
+
? resume(argument)
|
|
51
|
+
: applyWithHooks(hooks, namedForHooks(generatorFunction, name), "await", resume, undefined, [argument]);
|
|
33
52
|
if (result.done) {
|
|
34
53
|
frame = store.current;
|
|
35
54
|
store.current = previous;
|
|
@@ -49,9 +68,9 @@ export function coroutine<T, A extends unknown[], R>(generatorFunction : (this :
|
|
|
49
68
|
};
|
|
50
69
|
const next = (value : unknown) : IteratorResult<unknown, R> => generator.next(value);
|
|
51
70
|
const throwInto = (error : unknown) : IteratorResult<unknown, R> => generator.throw(error);
|
|
52
|
-
const onFulfilled = (value : unknown) : void => step(next, value);
|
|
53
|
-
const onRejected = (error : unknown) : void => step(throwInto, error);
|
|
54
|
-
step(next, undefined);
|
|
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);
|
|
55
74
|
});
|
|
56
75
|
};
|
|
57
76
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { assertCallbackHook, type CallbackHook } from "./callback-hook.js";
|
|
2
|
+
import { store, type HookEntry } from "./store.js";
|
|
3
|
+
|
|
4
|
+
export type { CallbackHook } from "./callback-hook.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* This function adds a hook. The library starts its methods before and after each
|
|
8
|
+
* callback that it starts. These callbacks are the callbacks of the patched
|
|
9
|
+
* APIs and the step of each transformed async function after an `await`. The
|
|
10
|
+
* function gives a function that removes the hook.
|
|
11
|
+
*
|
|
12
|
+
* A tool can use the hook to measure the time of each callback, with the
|
|
13
|
+
* name of the callback and the values of its context. An error in a hook goes
|
|
14
|
+
* to `reportError()`, and the callback operates as usual. A callback that
|
|
15
|
+
* starts synchronously in a hook does not start the hook methods.
|
|
16
|
+
*/
|
|
17
|
+
export function addCallbackHook(hook : CallbackHook) : () => void {
|
|
18
|
+
assertCallbackHook(hook);
|
|
19
|
+
const entry : HookEntry = { hook };
|
|
20
|
+
store.hooks = [...(store.hooks ?? []), entry];
|
|
21
|
+
let removed = false;
|
|
22
|
+
return function removeCallbackHook() : void {
|
|
23
|
+
if (removed) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
removed = true;
|
|
27
|
+
const remaining = (store.hooks ?? []).filter((other) => other !== entry);
|
|
28
|
+
store.hooks = remaining.length === 0 ? undefined : remaining;
|
|
29
|
+
};
|
|
30
|
+
}
|
package/src/core/store.ts
CHANGED
|
@@ -34,6 +34,25 @@ export interface ContextStore {
|
|
|
34
34
|
/** The original function of each patched function. `Function.prototype.toString` shows the original. */
|
|
35
35
|
readonly originals : WeakMap<object, object>;
|
|
36
36
|
readonly intrinsics : Intrinsics;
|
|
37
|
+
/**
|
|
38
|
+
* The callback hooks (`addCallbackHook`), or `undefined` if there are none. Each change
|
|
39
|
+
* makes a new array. The store of an older copy of the library does not
|
|
40
|
+
* have this property.
|
|
41
|
+
*/
|
|
42
|
+
hooks? : readonly HookEntry[] | undefined;
|
|
43
|
+
/** `true` while a hook operates. Callbacks that start in a hook do not start the hook methods. */
|
|
44
|
+
inHook? : boolean | undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The methods of a callback hook. The public type is `CallbackHook` in `hooks.ts`. */
|
|
48
|
+
export interface HookMethods {
|
|
49
|
+
before?(callback : Function, source : string) : void;
|
|
50
|
+
after?(callback : Function, source : string) : void;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** One registration of a hook. Each `addCallbackHook` call makes a new entry. */
|
|
54
|
+
export interface HookEntry {
|
|
55
|
+
readonly hook : HookMethods;
|
|
37
56
|
}
|
|
38
57
|
|
|
39
58
|
/** The key of the store on `globalThis`. The version changes when the store format changes. */
|
|
@@ -53,6 +72,9 @@ function createStore() : ContextStore {
|
|
|
53
72
|
promiseResolve : NativePromise.resolve.bind(NativePromise) as Intrinsics["promiseResolve"],
|
|
54
73
|
functionToString : Function.prototype.toString,
|
|
55
74
|
}),
|
|
75
|
+
// Own properties from the start: each callback reads `hooks`, and the read of an own property is faster
|
|
76
|
+
hooks : undefined,
|
|
77
|
+
inHook : false,
|
|
56
78
|
};
|
|
57
79
|
}
|
|
58
80
|
|
|
@@ -143,17 +165,92 @@ export function findValue(frame : Frame, variable : object) : unknown {
|
|
|
143
165
|
return ValueFrame.find(frame, variable);
|
|
144
166
|
}
|
|
145
167
|
|
|
168
|
+
/** This function gives the error of a hook to the host. A hook error does not stop the callback. */
|
|
169
|
+
function reportHookError(error : unknown) : void {
|
|
170
|
+
const report = (globalThis as { reportError? : unknown }).reportError;
|
|
171
|
+
if (typeof report === "function") {
|
|
172
|
+
(report as (error : unknown) => void)(error);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
console.error(error);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* This function starts one method of each hook. `before` uses the order of
|
|
180
|
+
* registration, and `after` uses the opposite order. Each hook operates in
|
|
181
|
+
* the current frame, and a hook cannot change the frame of the callback.
|
|
182
|
+
*/
|
|
183
|
+
function callHooks(hooks : readonly HookEntry[], phase : "before" | "after", callback : Function, source : string) : void {
|
|
184
|
+
const frame = store.current;
|
|
185
|
+
store.inHook = true;
|
|
186
|
+
try {
|
|
187
|
+
const count = hooks.length;
|
|
188
|
+
for (let step = 0; step < count; step++) {
|
|
189
|
+
const { hook } = hooks[phase === "before" ? step : count - 1 - step] as HookEntry;
|
|
190
|
+
const method = hook[phase];
|
|
191
|
+
if (typeof method !== "function") {
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
try {
|
|
195
|
+
method.call(hook, callback, source);
|
|
196
|
+
} catch (error) {
|
|
197
|
+
reportHookError(error);
|
|
198
|
+
}
|
|
199
|
+
store.current = frame;
|
|
200
|
+
}
|
|
201
|
+
} finally {
|
|
202
|
+
store.inHook = false;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* This function starts `fn` between the `before` and the `after` methods of
|
|
208
|
+
* the hooks. The current frame must be the frame of the callback. The hooks
|
|
209
|
+
* get `callback` and `source`. A callback that starts in a hook does not start
|
|
210
|
+
* the hooks again.
|
|
211
|
+
*/
|
|
212
|
+
export function applyWithHooks<T, A extends unknown[], R>(
|
|
213
|
+
hooks : readonly HookEntry[],
|
|
214
|
+
callback : Function,
|
|
215
|
+
source : string,
|
|
216
|
+
fn : (this : T, ...args : A) => R,
|
|
217
|
+
thisValue : T,
|
|
218
|
+
args : A,
|
|
219
|
+
) : R {
|
|
220
|
+
if (store.inHook === true) {
|
|
221
|
+
return fn.apply(thisValue, args);
|
|
222
|
+
}
|
|
223
|
+
callHooks(hooks, "before", callback, source);
|
|
224
|
+
try {
|
|
225
|
+
return fn.apply(thisValue, args);
|
|
226
|
+
} finally {
|
|
227
|
+
callHooks(hooks, "after", callback, source);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/*
|
|
232
|
+
* Each wrapper has the name `asyncBrowserContextWrapper`. Profilers, stack
|
|
233
|
+
* traces and the Long Animation Frames API show the static name of a
|
|
234
|
+
* function, so they show this name in place of the name of the callback. The
|
|
235
|
+
* name tells the reader that the next frame is the callback.
|
|
236
|
+
*/
|
|
237
|
+
|
|
146
238
|
/**
|
|
147
239
|
* This function wraps `fn`. The wrapper starts `fn` in `frame`, with the `this`
|
|
148
240
|
* value and the arguments of the call. After `fn` ends, also with an error, the
|
|
149
|
-
* previous frame is current again.
|
|
241
|
+
* previous frame is current again. `source` is the name of the API that got
|
|
242
|
+
* `fn`, for the callback hooks.
|
|
150
243
|
*/
|
|
151
|
-
export function bindToFrame<T, A extends unknown[], R>(frame : Frame, fn : (this : T, ...args : A) => R) : (this : T, ...args : A) => R {
|
|
152
|
-
return function
|
|
244
|
+
export function bindToFrame<T, A extends unknown[], R>(frame : Frame, fn : (this : T, ...args : A) => R, source : string) : (this : T, ...args : A) => R {
|
|
245
|
+
return function asyncBrowserContextWrapper(this : T, ...args : A) : R {
|
|
153
246
|
const previous = store.current;
|
|
154
247
|
store.current = frame;
|
|
155
248
|
try {
|
|
156
|
-
|
|
249
|
+
const hooks = store.hooks;
|
|
250
|
+
if (hooks === undefined) {
|
|
251
|
+
return fn.apply(this, args);
|
|
252
|
+
}
|
|
253
|
+
return applyWithHooks(hooks, fn, source, fn, this, args);
|
|
157
254
|
} finally {
|
|
158
255
|
store.current = previous;
|
|
159
256
|
}
|
|
@@ -165,12 +262,16 @@ export function bindToFrame<T, A extends unknown[], R>(frame : Frame, fn : (this
|
|
|
165
262
|
* wrapper does not make an array of the arguments, so it is faster than
|
|
166
263
|
* `bindToFrame` on the hot path.
|
|
167
264
|
*/
|
|
168
|
-
export function bindOneArgument<T, A, R>(frame : Frame, fn : (this : T, argument : A) => R) : (this : T, argument : A) => R {
|
|
169
|
-
return function
|
|
265
|
+
export function bindOneArgument<T, A, R>(frame : Frame, fn : (this : T, argument : A) => R, source : string) : (this : T, argument : A) => R {
|
|
266
|
+
return function asyncBrowserContextWrapper(this : T, argument : A) : R {
|
|
170
267
|
const previous = store.current;
|
|
171
268
|
store.current = frame;
|
|
172
269
|
try {
|
|
173
|
-
|
|
270
|
+
const hooks = store.hooks;
|
|
271
|
+
if (hooks === undefined) {
|
|
272
|
+
return fn.call(this, argument);
|
|
273
|
+
}
|
|
274
|
+
return applyWithHooks(hooks, fn, source, fn, this, [argument]);
|
|
174
275
|
} finally {
|
|
175
276
|
store.current = previous;
|
|
176
277
|
}
|
package/src/index.ts
CHANGED
|
@@ -15,6 +15,7 @@ import { Snapshot } from "./core/snapshot.js";
|
|
|
15
15
|
import { Variable } from "./core/variable.js";
|
|
16
16
|
|
|
17
17
|
export { AsyncLocalStorage, type AsyncLocalStorageOptions } from "./core/async-local-storage.js";
|
|
18
|
+
export { addCallbackHook, type CallbackHook } from "./core/hooks.js";
|
|
18
19
|
export { Snapshot } from "./core/snapshot.js";
|
|
19
20
|
export { Variable, type VariableOptions } from "./core/variable.js";
|
|
20
21
|
|