async-browser-context 0.2.4 → 0.3.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.
Files changed (140) hide show
  1. package/CHANGELOG.md +88 -0
  2. package/README.md +45 -30
  3. package/dist/babel/async-generator-helpers.d.ts +6 -6
  4. package/dist/babel/async-generator-helpers.d.ts.map +1 -1
  5. package/dist/babel/async-generator-helpers.js +33 -22
  6. package/dist/babel/async-generator-helpers.js.map +1 -1
  7. package/dist/babel/async-generator-resources.d.ts +14 -0
  8. package/dist/babel/async-generator-resources.d.ts.map +1 -0
  9. package/dist/babel/async-generator-resources.js +38 -0
  10. package/dist/babel/async-generator-resources.js.map +1 -0
  11. package/dist/babel/bind-generators.d.ts +1 -1
  12. package/dist/babel/bind-generators.d.ts.map +1 -1
  13. package/dist/babel/bind-generators.js +88 -44
  14. package/dist/babel/bind-generators.js.map +1 -1
  15. package/dist/babel/name-coroutines.d.ts +14 -8
  16. package/dist/babel/name-coroutines.d.ts.map +1 -1
  17. package/dist/babel/name-coroutines.js +160 -39
  18. package/dist/babel/name-coroutines.js.map +1 -1
  19. package/dist/babel/preset.d.ts +24 -13
  20. package/dist/babel/preset.d.ts.map +1 -1
  21. package/dist/babel/preset.js +37 -9
  22. package/dist/babel/preset.js.map +1 -1
  23. package/dist/babel/runtime-calls.d.ts +22 -0
  24. package/dist/babel/runtime-calls.d.ts.map +1 -0
  25. package/dist/babel/runtime-calls.js +69 -0
  26. package/dist/babel/runtime-calls.js.map +1 -0
  27. package/dist/babel/skip-runtime.d.ts +18 -11
  28. package/dist/babel/skip-runtime.d.ts.map +1 -1
  29. package/dist/babel/skip-runtime.js +52 -16
  30. package/dist/babel/skip-runtime.js.map +1 -1
  31. package/dist/babel/static-members.d.ts +4 -0
  32. package/dist/babel/static-members.d.ts.map +1 -0
  33. package/dist/babel/static-members.js +112 -0
  34. package/dist/babel/static-members.js.map +1 -0
  35. package/dist/core/async-generator.d.ts +26 -8
  36. package/dist/core/async-generator.d.ts.map +1 -1
  37. package/dist/core/async-generator.js +162 -44
  38. package/dist/core/async-generator.js.map +1 -1
  39. package/dist/core/async-local-storage.d.ts +3 -0
  40. package/dist/core/async-local-storage.d.ts.map +1 -1
  41. package/dist/core/async-local-storage.js +3 -0
  42. package/dist/core/async-local-storage.js.map +1 -1
  43. package/dist/core/callback-hook.d.ts +3 -3
  44. package/dist/core/coroutine.d.ts +6 -2
  45. package/dist/core/coroutine.d.ts.map +1 -1
  46. package/dist/core/coroutine.js +35 -21
  47. package/dist/core/coroutine.js.map +1 -1
  48. package/dist/core/function-name.d.ts +9 -0
  49. package/dist/core/function-name.d.ts.map +1 -0
  50. package/dist/core/function-name.js +12 -0
  51. package/dist/core/function-name.js.map +1 -0
  52. package/dist/core/hooks.d.ts +2 -1
  53. package/dist/core/hooks.d.ts.map +1 -1
  54. package/dist/core/hooks.js +21 -6
  55. package/dist/core/hooks.js.map +1 -1
  56. package/dist/core/snapshot.d.ts +2 -1
  57. package/dist/core/snapshot.d.ts.map +1 -1
  58. package/dist/core/snapshot.js +7 -3
  59. package/dist/core/snapshot.js.map +1 -1
  60. package/dist/core/store.d.ts +28 -0
  61. package/dist/core/store.d.ts.map +1 -1
  62. package/dist/core/store.js +90 -20
  63. package/dist/core/store.js.map +1 -1
  64. package/dist/core/variable.d.ts +2 -1
  65. package/dist/core/variable.d.ts.map +1 -1
  66. package/dist/core/variable.js +5 -2
  67. package/dist/core/variable.js.map +1 -1
  68. package/dist/node/index.d.ts +10 -4
  69. package/dist/node/index.d.ts.map +1 -1
  70. package/dist/node/index.js +51 -16
  71. package/dist/node/index.js.map +1 -1
  72. package/dist/node/runtime.d.ts +7 -6
  73. package/dist/node/runtime.d.ts.map +1 -1
  74. package/dist/node/runtime.js +29 -16
  75. package/dist/node/runtime.js.map +1 -1
  76. package/dist/opentelemetry/context-manager.d.ts.map +1 -1
  77. package/dist/opentelemetry/context-manager.js +172 -13
  78. package/dist/opentelemetry/context-manager.js.map +1 -1
  79. package/dist/patches/callbacks.d.ts.map +1 -1
  80. package/dist/patches/callbacks.js +84 -29
  81. package/dist/patches/callbacks.js.map +1 -1
  82. package/dist/patches/events.d.ts.map +1 -1
  83. package/dist/patches/events.js +203 -91
  84. package/dist/patches/events.js.map +1 -1
  85. package/dist/patches/handler-interfaces.d.ts +3 -2
  86. package/dist/patches/handler-interfaces.d.ts.map +1 -1
  87. package/dist/patches/handler-interfaces.js +3 -2
  88. package/dist/patches/handler-interfaces.js.map +1 -1
  89. package/dist/patches/promise.d.ts +3 -0
  90. package/dist/patches/promise.d.ts.map +1 -1
  91. package/dist/patches/promise.js +13 -5
  92. package/dist/patches/promise.js.map +1 -1
  93. package/dist/patches/streams.d.ts.map +1 -1
  94. package/dist/patches/streams.js +36 -16
  95. package/dist/patches/streams.js.map +1 -1
  96. package/dist/patches/timers.d.ts.map +1 -1
  97. package/dist/patches/timers.js +17 -5
  98. package/dist/patches/timers.js.map +1 -1
  99. package/dist/patches/to-string.d.ts +4 -0
  100. package/dist/patches/to-string.d.ts.map +1 -1
  101. package/dist/patches/to-string.js +12 -6
  102. package/dist/patches/to-string.js.map +1 -1
  103. package/dist/runtime.d.ts +3 -3
  104. package/dist/runtime.d.ts.map +1 -1
  105. package/dist/runtime.js +3 -3
  106. package/dist/runtime.js.map +1 -1
  107. package/dist/vite/plugin.d.ts +7 -2
  108. package/dist/vite/plugin.d.ts.map +1 -1
  109. package/dist/vite/plugin.js +30 -6
  110. package/dist/vite/plugin.js.map +1 -1
  111. package/package.json +49 -25
  112. package/src/babel/async-generator-helpers.ts +34 -23
  113. package/src/babel/async-generator-resources.ts +40 -0
  114. package/src/babel/bind-generators.ts +91 -47
  115. package/src/babel/name-coroutines.ts +170 -41
  116. package/src/babel/preset.ts +47 -15
  117. package/src/babel/runtime-calls.ts +73 -0
  118. package/src/babel/skip-runtime.ts +58 -18
  119. package/src/babel/static-members.ts +117 -0
  120. package/src/core/async-generator.ts +169 -46
  121. package/src/core/async-local-storage.ts +3 -0
  122. package/src/core/callback-hook.ts +3 -3
  123. package/src/core/coroutine.ts +35 -21
  124. package/src/core/function-name.ts +11 -0
  125. package/src/core/hooks.ts +23 -6
  126. package/src/core/snapshot.ts +8 -3
  127. package/src/core/store.ts +107 -21
  128. package/src/core/variable.ts +6 -2
  129. package/src/node/index.ts +62 -19
  130. package/src/node/runtime.ts +33 -15
  131. package/src/opentelemetry/context-manager.ts +188 -13
  132. package/src/patches/callbacks.ts +87 -30
  133. package/src/patches/events.ts +249 -103
  134. package/src/patches/handler-interfaces.ts +3 -2
  135. package/src/patches/promise.ts +17 -13
  136. package/src/patches/streams.ts +36 -16
  137. package/src/patches/timers.ts +17 -5
  138. package/src/patches/to-string.ts +12 -6
  139. package/src/runtime.ts +3 -3
  140. package/src/vite/plugin.ts +38 -8
@@ -8,6 +8,9 @@
8
8
  *
9
9
  * `await` in native code does not use `then`. The Babel preset changes each
10
10
  * `await` so that the coroutine sets the context (rule C3).
11
+ *
12
+ * The patch starts the `then` that it replaces, as the other patches do. Thus a
13
+ * patch of `then` that another tool installed before stays in effect.
11
14
  */
12
15
  export declare function installPromisePatch(): void;
13
16
  //# sourceMappingURL=promise.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"promise.d.ts","sourceRoot":"","sources":["../../src/patches/promise.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,IAAK,IAAI,CAiB3C"}
1
+ {"version":3,"file":"promise.d.ts","sourceRoot":"","sources":["../../src/patches/promise.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,IAAK,IAAI,CAkB3C"}
@@ -10,15 +10,23 @@ import { claim, replaceFunction } from "./patch.js";
10
10
  *
11
11
  * `await` in native code does not use `then`. The Babel preset changes each
12
12
  * `await` so that the coroutine sets the context (rule C3).
13
+ *
14
+ * The patch starts the `then` that it replaces, as the other patches do. Thus a
15
+ * patch of `then` that another tool installed before stays in effect.
13
16
  */
14
17
  export function installPromisePatch() {
15
18
  if (!claim("Promise.prototype.then")) {
16
19
  return;
17
20
  }
18
- const nativeThen = store.intrinsics.promiseThen;
19
- replaceFunction(Promise.prototype, "then", () => function then(onFulfilled, onRejected) {
20
- const frame = store.current;
21
- return nativeThen.call(this, typeof onFulfilled === "function" ? bindOneArgument(frame, onFulfilled, "Promise.then") : onFulfilled, typeof onRejected === "function" ? bindOneArgument(frame, onRejected, "Promise.then") : onRejected);
22
- });
21
+ // Method syntax: a native method has no prototype property and is not a constructor
22
+ replaceFunction(Promise.prototype, "then", (original) => ({
23
+ then(onFulfilled, onRejected) {
24
+ const frame = store.current;
25
+ return Reflect.apply(original, this, [
26
+ typeof onFulfilled === "function" ? bindOneArgument(frame, onFulfilled, "Promise.then") : onFulfilled,
27
+ typeof onRejected === "function" ? bindOneArgument(frame, onRejected, "Promise.then") : onRejected,
28
+ ]);
29
+ },
30
+ }).then);
23
31
  }
24
32
  //# 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;;;;;;;;;;GAUG;AACH,MAAM,UAAU,mBAAmB;IAC/B,IAAI,CAAC,KAAK,CAAC,wBAAwB,CAAC,EAAE,CAAC;QACnC,OAAO;IACX,CAAC;IACD,MAAM,UAAU,GAAG,KAAK,CAAC,UAAU,CAAC,WAAW,CAAC;IAChD,eAAe,CAAO,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,SAAS,IAAI,CAEhE,WAA2D,EAC3D,UAAiE;QAEjE,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC;QAC5B,OAAO,UAAU,CAAC,IAAI,CAClB,IAAI,EACJ,OAAO,WAA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":"","sources":["../../src/patches/streams.ts"],"names":[],"mappings":"AAgDA,qDAAqD;AACrD,wBAAgB,oBAAoB,IAAK,IAAI,CA8B5C"}
1
+ {"version":3,"file":"streams.d.ts","sourceRoot":"","sources":["../../src/patches/streams.ts"],"names":[],"mappings":"AAoEA,qDAAqD;AACrD,wBAAgB,oBAAoB,IAAK,IAAI,CA8B5C"}
@@ -1,4 +1,4 @@
1
- import { applyWithHooks, store } from "../core/store.js";
1
+ import { applyQuietly, 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
@@ -11,36 +11,56 @@ const STREAMS = [
11
11
  ["TransformStream", ["start", "transform", "flush", "cancel"]],
12
12
  ];
13
13
  /**
14
- * This function gives an object that inherits from `underlying` and has a wrapper for each
15
- * method. The other properties, for example `type`, come from `underlying`.
16
- * Each method gets `underlying` as its `this` value, as the specification
17
- * tells.
14
+ * This function gives a view of `underlying` in which each method of `methods` is a wrapper.
15
+ * The view is a Proxy, so the other properties, for example `type`, come from `underlying`
16
+ * with their getters and private fields. The target of the Proxy is an empty object, so a
17
+ * frozen object also operates. The specification reads the members with [[Get]]. But the
18
+ * streams of WebKit first examine each member with the `in` operator, thus the `has` trap
19
+ * gives the answer of `underlying`. Each method gets `underlying` as its `this` value, as the
20
+ * specification tells.
18
21
  */
19
22
  function wrapUnderlying(underlying, methods, frame, name) {
20
- const wrapped = Object.create(underlying);
21
- const properties = underlying;
22
- for (const method of methods) {
23
- const original = properties[method];
24
- if (typeof original !== "function") {
25
- continue;
23
+ // A method that a hook registers does not start the hooks
24
+ const quiet = store.inHook === true;
25
+ const wrappers = new Map();
26
+ const wrapperOf = (method, original) => {
27
+ let wrapper = wrappers.get(method);
28
+ if (wrapper !== undefined) {
29
+ return wrapper;
26
30
  }
27
31
  const source = `${name}.${method}`;
28
- wrapped[method] = function asyncBrowserContextWrapper(...args) {
32
+ wrapper = function asyncBrowserContextWrapper() {
29
33
  const previous = store.current;
30
34
  store.current = frame;
31
35
  try {
36
+ if (quiet) {
37
+ return applyQuietly(original, underlying, Array.prototype.slice.call(arguments));
38
+ }
32
39
  const hooks = store.hooks;
33
40
  if (hooks === undefined) {
34
- return original.apply(underlying, args);
41
+ return Reflect.apply(original, underlying, arguments);
35
42
  }
36
- return applyWithHooks(hooks, original, source, original, underlying, args);
43
+ return applyWithHooks(hooks, original, source, original, underlying, Array.prototype.slice.call(arguments));
37
44
  }
38
45
  finally {
39
46
  store.current = previous;
40
47
  }
41
48
  };
42
- }
43
- return wrapped;
49
+ wrappers.set(method, wrapper);
50
+ return wrapper;
51
+ };
52
+ return new Proxy({}, {
53
+ get(_target, property) {
54
+ const value = Reflect.get(underlying, property, underlying);
55
+ if (typeof property === "string" && typeof value === "function" && methods.includes(property)) {
56
+ return wrapperOf(property, value);
57
+ }
58
+ return value;
59
+ },
60
+ has(_target, property) {
61
+ return Reflect.has(underlying, property);
62
+ },
63
+ });
44
64
  }
45
65
  /** This function patches the stream constructors. */
46
66
  export function installStreamPatches() {
@@ -1 +1 @@
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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I,qBAAqB,EAAE,YAAY,KAAK,IAAI,EAAE,CAAC;gBAC/C,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,aAAa,EAAE,EAAE,GAAG,qBAAqB,EAAE,KAAK,EAAG,KAAK,EAAE,CAAC,CAAC;YACjG,CAAC;QACL,CAAC;QACD,MAAM,CAAC,cAAc,CAAC,UAAU,EAAE,IAAI,EAAE,EAAE,GAAG,UAAU,EAAE,KAAK,EAAG,KAAK,EAAE,CAAC,CAAC;IAC9E,CAAC;AACL,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"timers.d.ts","sourceRoot":"","sources":["../../src/patches/timers.ts"],"names":[],"mappings":"AAmCA;;;;GAIG;AACH,wBAAgB,mBAAmB,IAAK,IAAI,CAe3C"}
1
+ {"version":3,"file":"timers.d.ts","sourceRoot":"","sources":["../../src/patches/timers.ts"],"names":[],"mappings":"AA+CA;;;;GAIG;AACH,wBAAgB,mBAAmB,IAAK,IAAI,CAe3C"}
@@ -20,12 +20,24 @@ const PROTOTYPE_SCHEDULERS = [
20
20
  ["XRSession", "requestAnimationFrame"],
21
21
  ];
22
22
  // Stryker restore all
23
- /** `source` is the name of the scheduler, for the callback hooks. */
23
+ /**
24
+ * `source` is the name of the scheduler, for the callback hooks. The replacement is a method,
25
+ * so it has no prototype property and is not a constructor, as the native function. It gives
26
+ * the native function the same number of arguments, so `setTimeout()` without arguments
27
+ * throws as the native function does.
28
+ */
24
29
  function wrapFirstArgument(source) {
25
- return (original) => function (callback, ...rest) {
26
- const wrapped = typeof callback === "function" ? bindToFrame(store.current, callback, source) : callback;
27
- return original.call(this, wrapped, ...rest);
28
- };
30
+ return (original) => ({
31
+ scheduler(callback) {
32
+ if (typeof callback !== "function") {
33
+ // A string callback of setTimeout, or no callback: the native function decides
34
+ return Reflect.apply(original, this, arguments);
35
+ }
36
+ const args = Array.prototype.slice.call(arguments);
37
+ args[0] = bindToFrame(store.current, callback, source);
38
+ return Reflect.apply(original, this, args);
39
+ },
40
+ }).scheduler;
29
41
  }
30
42
  /**
31
43
  * This function patches the timers and the other schedulers. The callback
@@ -1 +1 @@
1
- {"version":3,"file":"timers.js","sourceRoot":"","sources":["../../src/patches/timers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,KAAK,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAIrE,yFAAyF;AACzF,MAAM,iBAAiB,GAAG;IACtB,YAAY;IACZ,aAAa;IACb,cAAc;IACd,gBAAgB;IAChB,uBAAuB;IACvB,qBAAqB;CACf,CAAC;AAEX;;;GAGG;AACH,6EAA6E;AAC7E,MAAM,oBAAoB,GAA4C;IAClE,CAAC,WAAW,EAAE,UAAU,CAAC;IACzB,CAAC,kBAAkB,EAAE,2BAA2B,CAAC;IACjD,CAAC,WAAW,EAAE,uBAAuB,CAAC;CACzC,CAAC;AACF,sBAAsB;AAEtB,qEAAqE;AACrE,SAAS,iBAAiB,CAAC,MAAe;IACtC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,UAA0B,QAAkB,EAAE,GAAG,IAAgB;QAClF,MAAM,OAAO,GAAG,OAAO,QAAQ,KAAK,UAAU,CAAC,CAAC,CAAC,WAAW,CAAC,KAAK,CAAC,OAAO,EAAE,QAA4C,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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CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAc,CAAC;YAChE,IAAI,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,KAAK,CAAC,OAAO,EAAE,QAA8C,EAAE,MAAM,CAAC,CAAC;YAC7F,OAAO,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QAC/C,CAAC;KACJ,CAAC,CAAC,SAAS,CAAC;AACjB,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"}
@@ -2,6 +2,10 @@
2
2
  * This function patches `Function.prototype.toString`. For a patched function, the patch gives
3
3
  * the source text of the original function. Some libraries examine this text
4
4
  * to find native functions, and a changed text can stop them.
5
+ *
6
+ * The patch starts the `toString` that it replaces, as the other patches do. Thus a patch of
7
+ * `toString` that another tool installed before, for example to show the source of its own
8
+ * wrappers, stays in effect.
5
9
  */
6
10
  export declare function installToStringPatch(): void;
7
11
  //# sourceMappingURL=to-string.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"to-string.d.ts","sourceRoot":"","sources":["../../src/patches/to-string.ts"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,wBAAgB,oBAAoB,IAAK,IAAI,CAU5C"}
1
+ {"version":3,"file":"to-string.d.ts","sourceRoot":"","sources":["../../src/patches/to-string.ts"],"names":[],"mappings":"AAKA;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,IAAK,IAAI,CAY5C"}
@@ -4,16 +4,22 @@ import { claim, replaceFunction } from "./patch.js";
4
4
  * This function patches `Function.prototype.toString`. For a patched function, the patch gives
5
5
  * the source text of the original function. Some libraries examine this text
6
6
  * to find native functions, and a changed text can stop them.
7
+ *
8
+ * The patch starts the `toString` that it replaces, as the other patches do. Thus a patch of
9
+ * `toString` that another tool installed before, for example to show the source of its own
10
+ * wrappers, stays in effect.
7
11
  */
8
12
  export function installToStringPatch() {
9
13
  if (!claim("Function.prototype.toString")) {
10
14
  return;
11
15
  }
12
- const nativeToString = store.intrinsics.functionToString;
13
- replaceFunction(Function.prototype, "toString", () => function toString() {
14
- // WeakMap.get gives undefined for a primitive. The native function then throws its TypeError.
15
- const original = store.originals.get(this);
16
- return nativeToString.call(original ?? this);
17
- });
16
+ // Method syntax: a native method has no prototype property and is not a constructor
17
+ replaceFunction(Function.prototype, "toString", (replaced) => ({
18
+ toString() {
19
+ // WeakMap.get gives undefined for a primitive. The native function then throws its TypeError.
20
+ const original = store.originals.get(this);
21
+ return Reflect.apply(replaced, original ?? this, []);
22
+ },
23
+ }).toString);
18
24
  }
19
25
  //# sourceMappingURL=to-string.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"to-string.js","sourceRoot":"","sources":["../../src/patches/to-string.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAIpD;;;;GAIG;AACH,MAAM,UAAU,oBAAoB;IAChC,IAAI,CAAC,KAAK,CAAC,6BAA6B,CAAC,EAAE,CAAC;QACxC,OAAO;IACX,CAAC;IACD,MAAM,cAAc,GAAG,KAAK,CAAC,UAAU,CAAC,gBAAgB,CAAC;IACzD,eAAe,CAAW,QAAQ,CAAC,SAAS,EAAE,UAAU,EAAE,GAAG,EAAE,CAAC,SAAS,QAAQ;QAC7E,8FAA8F;QAC9F,MAAM,QAAQ,GAAG,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,IAAc,CAAC,CAAC;QACrD,OAAO,cAAc,CAAC,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,CAAC;IACjD,CAAC,CAAC,CAAC;AACP,CAAC"}
1
+ {"version":3,"file":"to-string.js","sourceRoot":"","sources":["../../src/patches/to-string.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAIpD;;;;;;;;GAQG;AACH,MAAM,UAAU,oBAAoB;IAChC,IAAI,CAAC,KAAK,CAAC,6BAA6B,CAAC,EAAE,CAAC;QACxC,OAAO;IACX,CAAC;IACD,oFAAoF;IACpF,eAAe,CAAW,QAAQ,CAAC,SAAS,EAAE,UAAU,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QACrE,QAAQ;YACJ,8FAA8F;YAC9F,MAAM,QAAQ,GAAG,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,IAAc,CAAC,CAAC;YACrD,OAAO,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,QAAQ,IAAI,IAAI,EAAE,EAAE,CAAC,CAAC;QACzD,CAAC;KACJ,CAAC,CAAC,QAAQ,CAAC,CAAC;AACjB,CAAC"}
package/dist/runtime.d.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  /**
2
2
  * The runtime of the code that the Babel preset transforms. The transformed
3
- * code imports `coroutine`, `bindGenerator`, `asyncGenerator` and
4
- * `OverloadYield` from this module. When the
3
+ * code imports `coroutine`, `bindGenerator`, `asyncGenerator`, `OverloadYield`,
4
+ * `awaitAsyncGenerator` and `asyncGeneratorDelegate` from this module. When the
5
5
  * module loads, it installs the patches of the runtime.
6
6
  *
7
7
  * @packageDocumentation
8
8
  */
9
9
  import "./install.js";
10
- export { OverloadYield } from "./core/async-generator.js";
10
+ export { asyncGeneratorDelegate, awaitAsyncGenerator, OverloadYield } from "./core/async-generator.js";
11
11
  export { asyncGenerator, bindGenerator, coroutine } from "./core/coroutine.js";
12
12
  //# sourceMappingURL=runtime.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC"}
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,cAAc,CAAC;AAEtB,OAAO,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AACvG,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC"}
package/dist/runtime.js CHANGED
@@ -1,12 +1,12 @@
1
1
  /**
2
2
  * The runtime of the code that the Babel preset transforms. The transformed
3
- * code imports `coroutine`, `bindGenerator`, `asyncGenerator` and
4
- * `OverloadYield` from this module. When the
3
+ * code imports `coroutine`, `bindGenerator`, `asyncGenerator`, `OverloadYield`,
4
+ * `awaitAsyncGenerator` and `asyncGeneratorDelegate` from this module. When the
5
5
  * module loads, it installs the patches of the runtime.
6
6
  *
7
7
  * @packageDocumentation
8
8
  */
9
9
  import "./install.js";
10
- export { OverloadYield } from "./core/async-generator.js";
10
+ export { asyncGeneratorDelegate, awaitAsyncGenerator, OverloadYield } from "./core/async-generator.js";
11
11
  export { asyncGenerator, bindGenerator, coroutine } from "./core/coroutine.js";
12
12
  //# sourceMappingURL=runtime.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC"}
1
+ {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,cAAc,CAAC;AAEtB,OAAO,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AACvG,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC"}
@@ -5,12 +5,17 @@ export type ModuleFilter = RegExp | ((id: string) => boolean);
5
5
  export interface AsyncContextPluginOptions {
6
6
  /**
7
7
  * The modules to transform. The default is all JavaScript and TypeScript
8
- * modules, also the modules in `node_modules`.
8
+ * modules, also the modules in `node_modules`, and the JavaScript of Vue,
9
+ * Svelte, Astro and MDX components. This filter can only select from these
10
+ * modules: the plugin does not transform styles.
9
11
  */
10
12
  readonly include?: ModuleFilter | undefined;
11
13
  /** The modules not to transform. */
12
14
  readonly exclude?: ModuleFilter | undefined;
13
- /** The runtime module. The default is `async-browser-context/runtime`. */
15
+ /**
16
+ * The runtime module. It must export the six names of `async-browser-context/runtime`.
17
+ * The default is `async-browser-context/runtime`.
18
+ */
14
19
  readonly runtime?: string | undefined;
15
20
  /**
16
21
  * Give the name of each async function to the callback hooks. The default
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/vite/plugin.ts"],"names":[],"mappings":"AACA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,KAAK,CAAC,EAAG,OAAO,GAAG,SAAS,CAAC;IACtC;;;;OAIG;IACH,QAAQ,CAAC,GAAG,CAAC,EAAG,OAAO,GAAG,SAAS,CAAC;CACvC;AA8BD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAG,yBAA8B,GAAI,MAAM,CAmD9E"}
@@ -1,13 +1,30 @@
1
1
  import { transformAsync } from "@babel/core";
2
2
  import preset, { DEFAULT_RUNTIME, METADATA_KEY } from "../babel/preset.js";
3
3
  const SCRIPT = /\.[cm]?[jt]sx?$/;
4
+ /** The component files that other Vite plugins compile to JavaScript before this plugin operates. */
5
+ const COMPONENT = /\.(?:vue|svelte|astro|mdx)$/;
6
+ /** The parts of a component that are styles, not JavaScript, for example `App.vue?vue&type=style&lang.css`. */
7
+ const STYLE_QUERY = /[?&](?:type=style|lang\.(?:css|scss|sass|less|styl|stylus|pcss|postcss|sss))(?:&|$)/;
4
8
  /** A module without these words has no async function, generator or `for await`. */
5
9
  const MIGHT_CHANGE = /\basync\b|\byield\b|function\s*\*/;
6
10
  /** The runtime of this library. The plugin must not transform it. */
7
11
  const OWN_RUNTIME = /[\\/]async-browser-context[\\/]dist[\\/]/;
12
+ /** The marks of a CommonJS module. */
13
+ const COMMONJS = /\brequire\s*\(|\bmodule\.exports\b|\bexports\.[A-Za-z_$]/;
8
14
  function matches(filter, id) {
9
15
  return filter instanceof RegExp ? filter.test(id) : filter(id);
10
16
  }
17
+ /**
18
+ * This function gives the source type for Babel. Vite serves each module as an ES module, also
19
+ * a worker or a file with only a dynamic `import()`. With "unambiguous", Babel reads such a file
20
+ * as a script and adds a `require()` of the runtime, which a browser does not have. Thus only a
21
+ * `.cjs` or `.cts` file, or a file with CommonJS marks, gets "unambiguous". Babel then reads it
22
+ * as a script, unless it has a static import or export. A text of the code that looks like an
23
+ * import, for example in a string, does not change this.
24
+ */
25
+ function sourceTypeOf(file, code) {
26
+ return /\.c[jt]s$/.test(file) || COMMONJS.test(code) ? "unambiguous" : "module";
27
+ }
11
28
  /**
12
29
  * The Vite plugin of `async-browser-context`. It applies the Babel preset to
13
30
  * the modules of the application and of its dependencies. Thus, the context
@@ -27,6 +44,8 @@ function matches(filter, id) {
27
44
  */
28
45
  export function asyncContext(options = {}) {
29
46
  const runtime = options.runtime ?? DEFAULT_RUNTIME;
47
+ // One presets array for all modules, so the configuration cache of Babel operates
48
+ const presets = [[preset, { runtime, names: options.names }]];
30
49
  return {
31
50
  name: "async-browser-context",
32
51
  enforce: "post",
@@ -40,7 +59,7 @@ export function asyncContext(options = {}) {
40
59
  return null;
41
60
  }
42
61
  const file = id.split("?", 1)[0] ?? id;
43
- if (!SCRIPT.test(file) || OWN_RUNTIME.test(file)) {
62
+ if (!(SCRIPT.test(file) || COMPONENT.test(file)) || STYLE_QUERY.test(id) || OWN_RUNTIME.test(file)) {
44
63
  return null;
45
64
  }
46
65
  if (options.include !== undefined && !matches(options.include, file)) {
@@ -56,16 +75,21 @@ export function asyncContext(options = {}) {
56
75
  filename: file,
57
76
  babelrc: false,
58
77
  configFile: false,
59
- sourceType: "unambiguous",
78
+ sourceType: sourceTypeOf(file, code),
60
79
  sourceMaps: true,
61
- presets: [[preset, { runtime, names: options.names }]],
80
+ // The map of Vite describes the code that this plugin gets, not a map file of the module
81
+ inputSourceMap: false,
82
+ // Large modules, for example the dependency bundles of Vite, keep their format
83
+ compact: false,
84
+ presets,
62
85
  });
63
- const changed = result?.metadata?.[METADATA_KEY] === true;
64
- if (result?.code == null || !changed) {
86
+ // Without the options ignore and only, Babel gives a result with code. With sourceMaps, it also gives a map.
87
+ const { code: output, map, metadata } = result;
88
+ if (metadata[METADATA_KEY] !== true) {
65
89
  return null;
66
90
  }
67
91
  // A source map in JSON text satisfies both Rollup and Rolldown
68
- return { code: result.code, map: result.map == null ? null : JSON.stringify(result.map) };
92
+ return { code: output, map: JSON.stringify(map) };
69
93
  },
70
94
  },
71
95
  };
@@ -1 +1 @@
1
- {"version":3,"file":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,EAAG,IAAI;oBACf,OAAO,EAAG,KAAK;oBACf,UAAU,EAAG,KAAK;oBAClB,UAAU,EAAG,YAAY,CAAC,IAAI,EAAE,IAAI,CAAC;oBACrC,UAAU,EAAG,IAAI;oBACjB,yFAAyF;oBACzF,cAAc,EAAG,KAAK;oBACtB,+EAA+E;oBAC/E,OAAO,EAAG,KAAK;oBACf,OAAO;iBACV,CAAC,CAAC;gBACH,6GAA6G;gBAC7G,MAAM,EAAE,IAAI,EAAG,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,MAA6E,CAAC;gBACvH,IAAI,QAAQ,CAAC,YAAY,CAAC,KAAK,IAAI,EAAE,CAAC;oBAClC,OAAO,IAAI,CAAC;gBAChB,CAAC;gBACD,+DAA+D;gBAC/D,OAAO,EAAE,IAAI,EAAG,MAAM,EAAE,GAAG,EAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;YACxD,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.4",
3
+ "version": "0.3.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",
@@ -54,6 +54,30 @@
54
54
  "./src/install.ts",
55
55
  "./src/patches/*.ts"
56
56
  ],
57
+ "main": "./dist/index.js",
58
+ "types": "./dist/index.d.ts",
59
+ "typesVersions": {
60
+ "*": {
61
+ "browser": [
62
+ "./dist/index.d.ts"
63
+ ],
64
+ "browser/runtime": [
65
+ "./dist/runtime.d.ts"
66
+ ],
67
+ "runtime": [
68
+ "./dist/runtime.d.ts"
69
+ ],
70
+ "opentelemetry": [
71
+ "./dist/opentelemetry.d.ts"
72
+ ],
73
+ "babel-preset": [
74
+ "./dist/babel/preset.d.ts"
75
+ ],
76
+ "vite": [
77
+ "./dist/vite/plugin.d.ts"
78
+ ]
79
+ }
80
+ },
57
81
  "exports": {
58
82
  ".": {
59
83
  "types": "./dist/index.d.ts",
@@ -101,33 +125,12 @@
101
125
  "engines": {
102
126
  "node": "^22.18.0 || >=24.11.0"
103
127
  },
104
- "packageManager": "pnpm@10.26.0",
105
- "scripts": {
106
- "build": "tsc -p tsconfig.build.json",
107
- "typecheck": "tsc -p tsconfig.json",
108
- "test": "vitest run",
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
- "test:browser": "vitest run --project \"browser (chromium)\" --project \"browser (firefox)\" --project \"browser (webkit)\"",
111
- "coverage": "vitest run --coverage --project unit --project \"rules (browser runtime, Node.js)\" --project \"rules (node runtime)\"",
112
- "mutation": "stryker run",
113
- "bench": "pnpm build && tsx bench/run.ts",
114
- "smoke": "pnpm build && tsx test/smoke/run.ts",
115
- "test:babel": "pnpm build && tsx test/babel/run.ts",
116
- "lint:ste": "tsx tools/ste-lint/src/cli.ts",
117
- "site:dev": "pnpm --filter ./site dev",
118
- "site:build": "pnpm --filter ./site build",
119
- "prepublishOnly": "pnpm build && pnpm test",
120
- "site:data": "tsx scripts/site-data.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"
125
- },
126
128
  "dependencies": {
127
129
  "@babel/core": "^8.0.7",
128
130
  "@babel/helper-module-imports": "^8.0.0",
129
131
  "@babel/plugin-transform-async-generator-functions": "^8.0.6",
130
- "@babel/plugin-transform-async-to-generator": "^8.0.1"
132
+ "@babel/plugin-transform-async-to-generator": "^8.0.1",
133
+ "@babel/plugin-transform-explicit-resource-management": "^8.0.5"
131
134
  },
132
135
  "peerDependencies": {
133
136
  "@opentelemetry/api": "^1.0.0",
@@ -156,5 +159,26 @@
156
159
  "typescript": "^5.9.3",
157
160
  "vite": "^8.3.4",
158
161
  "vitest": "^5.0.3"
162
+ },
163
+ "scripts": {
164
+ "build": "tsc -p tsconfig.build.json",
165
+ "typecheck": "tsc -p tsconfig.json",
166
+ "test": "vitest run",
167
+ "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)\"",
168
+ "test:browser": "vitest run --project \"browser (chromium)\" --project \"browser (firefox)\" --project \"browser (webkit)\"",
169
+ "coverage": "vitest run --coverage --project unit --project \"rules (browser runtime, Node.js)\" --project \"rules (node runtime)\"",
170
+ "mutation": "stryker run",
171
+ "bench": "tsx bench/run.ts",
172
+ "smoke": "pnpm build && tsx test/smoke/run.ts",
173
+ "test:babel": "pnpm build && tsx test/babel/run.ts",
174
+ "lint:ste": "tsx tools/ste-lint/src/cli.ts --max-warnings 0",
175
+ "test:ste-lint": "vitest run --config tools/ste-lint/vitest.config.ts",
176
+ "site:dev": "pnpm --filter ./site dev",
177
+ "site:build": "pnpm --filter ./site build",
178
+ "site:data": "tsx scripts/site-data.ts",
179
+ "readme:media": "tsx scripts/readme-media.ts",
180
+ "test:bundlers": "pnpm build && tsx test/bundlers/run.ts",
181
+ "bench:size": "tsx bench/size.ts",
182
+ "bench:browsers": "tsx bench/browsers.ts"
159
183
  }
160
- }
184
+ }
@@ -3,47 +3,58 @@ import { addNamed } from "@babel/helper-module-imports";
3
3
 
4
4
  /** The options of the plugin. */
5
5
  export interface AsyncGeneratorHelpersOptions {
6
- /** The module that exports `asyncGenerator` and `OverloadYield`. */
6
+ /** The module that exports the async generator functions of the runtime. */
7
7
  readonly runtime : string;
8
8
  }
9
9
 
10
- /** The Babel helpers that the runtime replaces, and the export of the runtime for each one. */
10
+ /**
11
+ * The Babel helpers of async generators, and the export of the runtime for each one. The
12
+ * runtime gives all four, so the markers of the body (`OverloadYield`) are always the class of
13
+ * the runtime. Without this, another plugin (for example `@babel/plugin-transform-runtime`) can
14
+ * give `awaitAsyncGenerator` with the class of `@babel/runtime`. Then the driver operates each
15
+ * `await` as a `yield`.
16
+ */
11
17
  const REPLACED_HELPERS : Readonly<Record<string, string>> = {
12
18
  wrapAsyncGenerator : "asyncGenerator",
13
19
  OverloadYield : "OverloadYield",
20
+ awaitAsyncGenerator : "awaitAsyncGenerator",
21
+ asyncGeneratorDelegate : "asyncGeneratorDelegate",
14
22
  };
15
23
 
16
24
  type HelperGenerator = (name : string) => types.Expression | undefined;
17
25
 
18
26
  /**
19
27
  * The Babel plugin that gives the async generators to the driver of the runtime. It sets the
20
- * `helperGenerator` of the file, as `@babel/plugin-transform-runtime` does. Thus, the helpers
21
- * `wrapAsyncGenerator` and `OverloadYield` of `@babel/plugin-transform-async-generator-functions`
22
- * become imports of `asyncGenerator` and `OverloadYield` from the runtime. The helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` stay in the
23
- * file, and they make the `OverloadYield` values of the runtime.
28
+ * `helperGenerator` of the file, as `@babel/plugin-transform-runtime` does. Thus, the helpers of
29
+ * `@babel/plugin-transform-async-generator-functions` become imports from the runtime.
24
30
  *
25
- * The plugin keeps a `helperGenerator` of an earlier plugin for the other helpers.
31
+ * The plugin sets the generator in `Program` enter, after the `pre` methods of all plugins. A
32
+ * preset that comes before this preset in the list operates later, and its `pre` method can set
33
+ * a generator too. This plugin keeps that generator for the other helpers.
26
34
  */
27
35
  export function asyncGeneratorHelpersPlugin(_api : unknown, options : object) : PluginObject<PluginPass> {
28
36
  const { runtime } = options as AsyncGeneratorHelpersOptions;
29
37
  return {
30
38
  name : "async-browser-context/async-generator-helpers",
31
- pre(file : PluginPass["file"]) {
32
- const previous = file.get("helperGenerator") as HelperGenerator | undefined;
33
- const imports = new Map<string, types.Expression>();
34
- const generator : HelperGenerator = (name) => {
35
- const exported = REPLACED_HELPERS[name];
36
- if (exported === undefined) {
37
- return previous?.(name);
38
- }
39
- let local = imports.get(exported);
40
- if (local === undefined) {
41
- local = addNamed(file.path, exported, runtime, { nameHint : exported }) as types.Expression;
42
- imports.set(exported, local);
43
- }
44
- return types.cloneNode(local);
45
- };
46
- file.set("helperGenerator", generator);
39
+ visitor : {
40
+ Program(programPath, state) {
41
+ const file = state.file;
42
+ const previous = file.get("helperGenerator") as HelperGenerator | undefined;
43
+ const imports = new Map<string, types.Expression>();
44
+ const generator : HelperGenerator = (name) => {
45
+ const exported = REPLACED_HELPERS[name];
46
+ if (exported === undefined) {
47
+ return previous?.(name);
48
+ }
49
+ let local = imports.get(exported);
50
+ if (local === undefined) {
51
+ local = addNamed(programPath, exported, runtime, { nameHint : exported }) as types.Expression;
52
+ imports.set(exported, local);
53
+ }
54
+ return types.cloneNode(local);
55
+ };
56
+ file.set("helperGenerator", generator);
57
+ },
47
58
  },
48
59
  };
49
60
  }