async-browser-context 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +4 -1
  3. package/dist/babel/async-generator-helpers.d.ts +17 -0
  4. package/dist/babel/async-generator-helpers.d.ts.map +1 -0
  5. package/dist/babel/async-generator-helpers.js +40 -0
  6. package/dist/babel/async-generator-helpers.js.map +1 -0
  7. package/dist/babel/bind-generators.d.ts.map +1 -1
  8. package/dist/babel/bind-generators.js +17 -5
  9. package/dist/babel/bind-generators.js.map +1 -1
  10. package/dist/babel/preset.d.ts +11 -6
  11. package/dist/babel/preset.d.ts.map +1 -1
  12. package/dist/babel/preset.js +15 -6
  13. package/dist/babel/preset.js.map +1 -1
  14. package/dist/babel/skip-runtime.d.ts +16 -0
  15. package/dist/babel/skip-runtime.d.ts.map +1 -0
  16. package/dist/babel/skip-runtime.js +29 -0
  17. package/dist/babel/skip-runtime.js.map +1 -0
  18. package/dist/core/async-generator.d.ts +36 -0
  19. package/dist/core/async-generator.d.ts.map +1 -0
  20. package/dist/core/async-generator.js +132 -0
  21. package/dist/core/async-generator.js.map +1 -0
  22. package/dist/core/coroutine.d.ts +13 -3
  23. package/dist/core/coroutine.d.ts.map +1 -1
  24. package/dist/core/coroutine.js +104 -35
  25. package/dist/core/coroutine.js.map +1 -1
  26. package/dist/node/runtime.d.ts +13 -2
  27. package/dist/node/runtime.d.ts.map +1 -1
  28. package/dist/node/runtime.js +45 -24
  29. package/dist/node/runtime.js.map +1 -1
  30. package/dist/runtime.d.ts +4 -2
  31. package/dist/runtime.d.ts.map +1 -1
  32. package/dist/runtime.js +4 -2
  33. package/dist/runtime.js.map +1 -1
  34. package/package.json +3 -2
  35. package/src/babel/async-generator-helpers.ts +49 -0
  36. package/src/babel/bind-generators.ts +17 -5
  37. package/src/babel/preset.ts +15 -6
  38. package/src/babel/skip-runtime.ts +32 -0
  39. package/src/core/async-generator.ts +159 -0
  40. package/src/core/coroutine.ts +107 -35
  41. package/src/node/runtime.ts +47 -23
  42. package/src/runtime.ts +4 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # Change history
2
2
 
3
+ ## 0.2.3
4
+
5
+ ### Corrections
6
+
7
+ - Async generators are faster. The runtime has a driver for async generators (`asyncGenerator`), which continues each step with a native `await`. Before, the helper of Babel was in each module, and each step was a `then` callback. A `for await` loop over an async generator with 50,000 steps took 11.2 ms in Chromium, 560 ms in Firefox and 16 ms in WebKit. It takes 9.0 ms, 370 ms and 13 ms at this time (`pnpm bench:browsers`).
8
+ - The modules with async generators are smaller, because they do not contain the async generator helper of Babel. In the size measurement, idb changed from +31.9 to +8.1 percent and openai from +9.1 to +7.5 percent.
9
+ - The preset does not wrap async generators with `bindGenerator`. The driver keeps the context of the creation itself (rule C5).
10
+
11
+ ### Tests
12
+
13
+ - The tests examine the behavior of async generators: the queue of requests, `throw()`, `return()` with `await` and `yield` in a `finally` block, and `yield*`. The project without a transform starts the same tests on native async generators.
14
+
15
+ ## 0.2.2
16
+
17
+ ### Corrections
18
+
19
+ - 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`).
20
+ - 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.
21
+ - 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.
22
+
23
+ ### Documentation
24
+
25
+ - 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.
26
+ - The command `pnpm bench:browsers` measures `await` and `then` in Chromium, Firefox and WebKit.
27
+
3
28
  ## 0.2.1
4
29
 
5
30
  ### New functions
package/README.md CHANGED
@@ -274,7 +274,9 @@ 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
- 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`).
277
+ In Chromium, Firefox and WebKit, an `await` in transformed code takes approximately the time of a native `await` (`pnpm bench:browsers`). In Firefox, the patch of `then` adds approximately 0.8 µs to each `await` of a promise. The performance page of the website tells more.
278
+
279
+ The runtime is 5.9 kB with gzip, and zone.js 0.16.3 is 13.3 kB. The transform adds 3 to 8 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
280
282
 
@@ -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
 
@@ -0,0 +1,17 @@
1
+ import { type PluginObject, type PluginPass } from "@babel/core";
2
+ /** The options of the plugin. */
3
+ export interface AsyncGeneratorHelpersOptions {
4
+ /** The module that exports `asyncGenerator` and `OverloadYield`. */
5
+ readonly runtime: string;
6
+ }
7
+ /**
8
+ * The Babel plugin that gives the async generators to the driver of the runtime. It sets the
9
+ * `helperGenerator` of the file, as `@babel/plugin-transform-runtime` does. Thus, the helpers
10
+ * `wrapAsyncGenerator` and `OverloadYield` of `@babel/plugin-transform-async-generator-functions`
11
+ * become imports of `asyncGenerator` and `OverloadYield` from the runtime. The helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` stay in the
12
+ * file, and they make the `OverloadYield` values of the runtime.
13
+ *
14
+ * The plugin keeps a `helperGenerator` of an earlier plugin for the other helpers.
15
+ */
16
+ export declare function asyncGeneratorHelpersPlugin(_api: unknown, options: object): PluginObject<PluginPass>;
17
+ //# sourceMappingURL=async-generator-helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async-generator-helpers.d.ts","sourceRoot":"","sources":["../../src/babel/async-generator-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAGxE,iCAAiC;AACjC,MAAM,WAAW,4BAA4B;IACzC,oEAAoE;IACpE,QAAQ,CAAC,OAAO,EAAG,MAAM,CAAC;CAC7B;AAUD;;;;;;;;GAQG;AACH,wBAAgB,2BAA2B,CAAC,IAAI,EAAG,OAAO,EAAE,OAAO,EAAG,MAAM,GAAI,YAAY,CAAC,UAAU,CAAC,CAsBvG"}
@@ -0,0 +1,40 @@
1
+ import { types } from "@babel/core";
2
+ import { addNamed } from "@babel/helper-module-imports";
3
+ /** The Babel helpers that the runtime replaces, and the export of the runtime for each one. */
4
+ const REPLACED_HELPERS = {
5
+ wrapAsyncGenerator: "asyncGenerator",
6
+ OverloadYield: "OverloadYield",
7
+ };
8
+ /**
9
+ * The Babel plugin that gives the async generators to the driver of the runtime. It sets the
10
+ * `helperGenerator` of the file, as `@babel/plugin-transform-runtime` does. Thus, the helpers
11
+ * `wrapAsyncGenerator` and `OverloadYield` of `@babel/plugin-transform-async-generator-functions`
12
+ * become imports of `asyncGenerator` and `OverloadYield` from the runtime. The helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` stay in the
13
+ * file, and they make the `OverloadYield` values of the runtime.
14
+ *
15
+ * The plugin keeps a `helperGenerator` of an earlier plugin for the other helpers.
16
+ */
17
+ export function asyncGeneratorHelpersPlugin(_api, options) {
18
+ const { runtime } = options;
19
+ return {
20
+ name: "async-browser-context/async-generator-helpers",
21
+ pre(file) {
22
+ const previous = file.get("helperGenerator");
23
+ const imports = new Map();
24
+ const generator = (name) => {
25
+ const exported = REPLACED_HELPERS[name];
26
+ if (exported === undefined) {
27
+ return previous?.(name);
28
+ }
29
+ let local = imports.get(exported);
30
+ if (local === undefined) {
31
+ local = addNamed(file.path, exported, runtime, { nameHint: exported });
32
+ imports.set(exported, local);
33
+ }
34
+ return types.cloneNode(local);
35
+ };
36
+ file.set("helperGenerator", generator);
37
+ },
38
+ };
39
+ }
40
+ //# sourceMappingURL=async-generator-helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async-generator-helpers.js","sourceRoot":"","sources":["../../src/babel/async-generator-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAsC,MAAM,aAAa,CAAC;AACxE,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AAQxD,+FAA+F;AAC/F,MAAM,gBAAgB,GAAsC;IACxD,kBAAkB,EAAG,gBAAgB;IACrC,aAAa,EAAG,eAAe;CAClC,CAAC;AAIF;;;;;;;;GAQG;AACH,MAAM,UAAU,2BAA2B,CAAC,IAAc,EAAE,OAAgB;IACxE,MAAM,EAAE,OAAO,EAAE,GAAG,OAAuC,CAAC;IAC5D,OAAO;QACH,IAAI,EAAG,+CAA+C;QACtD,GAAG,CAAC,IAAyB;YACzB,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,iBAAiB,CAAgC,CAAC;YAC5E,MAAM,OAAO,GAAG,IAAI,GAAG,EAA4B,CAAC;YACpD,MAAM,SAAS,GAAqB,CAAC,IAAI,EAAE,EAAE;gBACzC,MAAM,QAAQ,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;gBACxC,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;oBACzB,OAAO,QAAQ,EAAE,CAAC,IAAI,CAAC,CAAC;gBAC5B,CAAC;gBACD,IAAI,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;gBAClC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;oBACtB,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,QAAQ,EAAG,QAAQ,EAAE,CAAqB,CAAC;oBAC5F,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;gBACjC,CAAC;gBACD,OAAO,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;YAClC,CAAC,CAAC;YACF,IAAI,CAAC,GAAG,CAAC,iBAAiB,EAAE,SAAS,CAAC,CAAC;QAC3C,CAAC;KACJ,CAAC;AACN,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"bind-generators.d.ts","sourceRoot":"","sources":["../../src/babel/bind-generators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAGvF,iCAAiC;AACjC,MAAM,WAAW,qBAAqB;IAClC,+CAA+C;IAC/C,QAAQ,CAAC,OAAO,EAAG,MAAM,CAAC;CAC7B;AAED,8EAA8E;AAC9E,eAAO,MAAM,YAAY,wBAAwB,CAAC;AAsElD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAG,OAAO,EAAE,OAAO,EAAG,MAAM,GAAI,YAAY,CAAC,UAAU,CAAC,CAoChG"}
1
+ {"version":3,"file":"bind-generators.d.ts","sourceRoot":"","sources":["../../src/babel/bind-generators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAGvF,iCAAiC;AACjC,MAAM,WAAW,qBAAqB;IAClC,+CAA+C;IAC/C,QAAQ,CAAC,OAAO,EAAG,MAAM,CAAC;CAC7B;AAED,8EAA8E;AAC9E,eAAO,MAAM,YAAY,wBAAwB,CAAC;AAiFlD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAG,OAAO,EAAE,OAAO,EAAG,MAAM,GAAI,YAAY,CAAC,UAAU,CAAC,CAqChG"}
@@ -2,15 +2,26 @@ import { types } from "@babel/core";
2
2
  import { addNamed } from "@babel/helper-module-imports";
3
3
  /** The key of the file metadata that tells if the preset changed the file. */
4
4
  export const METADATA_KEY = "asyncBrowserContext";
5
- /** The names of the helper of `@babel/plugin-transform-async-generator-functions`. Babel gives them an underscore. */
6
- const ASYNC_GENERATOR_HELPER = /^_wrapAsyncGenerator\d*$/;
5
+ /**
6
+ * The local names of the async generator helper: `wrapAsyncGenerator` of
7
+ * `@babel/plugin-transform-async-generator-functions`, or `asyncGenerator` of the runtime,
8
+ * which the preset gives in its place. Babel gives them an underscore.
9
+ */
10
+ const ASYNC_GENERATOR_HELPER = /^_(wrapAsyncGenerator|asyncGenerator)\d*$/;
7
11
  /** The local names that `addNamed` gives to `bindGenerator`. */
8
12
  const BIND_GENERATOR_NAME = /^_bindGenerator\d*$/;
9
13
  /** This function gives `true` if `path` is the generator that Babel gives to its async generator helper. */
10
14
  function isAsyncGeneratorHelperArgument(path) {
11
15
  const parent = path.parent;
12
- // Stryker disable next-line LogicalOperator: without the call check, the name test also fails, because a member callee has no name
13
- return types.isCallExpression(parent) && types.isIdentifier(parent.callee) && ASYNC_GENERATOR_HELPER.test(parent.callee.name);
16
+ if (!types.isCallExpression(parent)) {
17
+ return false;
18
+ }
19
+ if (types.isIdentifier(parent.callee)) {
20
+ return ASYNC_GENERATOR_HELPER.test(parent.callee.name);
21
+ }
22
+ // CommonJS output of the preset: _runtime.asyncGenerator or (0, _runtime.asyncGenerator)
23
+ const member = types.isSequenceExpression(parent.callee) ? parent.callee.expressions.at(-1) : parent.callee;
24
+ return types.isMemberExpression(member) && types.isIdentifier(member.property, { name: "asyncGenerator" });
14
25
  }
15
26
  /**
16
27
  * This function gives `true` if `callee` is the `bindGenerator` function of
@@ -83,7 +94,8 @@ export function bindGeneratorsPlugin(_api, options) {
83
94
  if (node.async) {
84
95
  changed = true;
85
96
  }
86
- if (!node.generator || isAsyncGeneratorHelperArgument(path) || isAlreadyBound(path, runtime)) {
97
+ // The driver of the runtime keeps the context of an async generator (asyncGenerator)
98
+ if (!node.generator || node.async || isAsyncGeneratorHelperArgument(path) || isAlreadyBound(path, runtime)) {
87
99
  return;
88
100
  }
89
101
  changed = true;
@@ -1 +1 @@
1
- {"version":3,"file":"bind-generators.js","sourceRoot":"","sources":["../../src/babel/bind-generators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAqD,MAAM,aAAa,CAAC;AACvF,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AAQxD,8EAA8E;AAC9E,MAAM,CAAC,MAAM,YAAY,GAAG,qBAAqB,CAAC;AAElD,sHAAsH;AACtH,MAAM,sBAAsB,GAAG,0BAA0B,CAAC;AAC1D,gEAAgE;AAChE,MAAM,mBAAmB,GAAG,qBAAqB,CAAC;AAIlD,4GAA4G;AAC5G,SAAS,8BAA8B,CAAC,IAAmB;IACvD,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;IAC3B,mIAAmI;IACnI,OAAO,KAAK,CAAC,gBAAgB,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,sBAAsB,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AAClI,CAAC;AAED;;;;GAIG;AACH,SAAS,qBAAqB,CAAC,MAAmB,EAAE,IAAe,EAAE,OAAgB;IACjF,IAAI,KAAK,CAAC,YAAY,CAAC,MAAM,CAAC,EAAE,CAAC;QAC7B,oJ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qC,CAAC;gBAC1C,MAAM,aAAa,GAAG,GAAsB,EAAE;oBAC1C,MAAM,KAAK,QAAQ,CAAC,WAAW,EAAE,eAAe,EAAE,OAAO,EAAE,EAAE,QAAQ,EAAG,eAAe,EAAE,CAAqB,CAAC;oBAC/G,OAAO,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;gBACnC,CAAC,CAAC;gBACF,IAAI,OAAO,GAAG,KAAK,CAAC;gBACpB,WAAW,CAAC,QAAQ,CAAC;oBACjB,QAAQ,CAAC,IAAI;wBACT,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;wBACvB,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;4BACb,OAAO,GAAG,IAAI,CAAC;wBACnB,CAAC;wBACD,qFAAqF;wBACrF,IAAI,CAAC,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,KAAK,IAAI,8BAA8B,CAAC,IAAI,CAAC,IAAI,cAAc,CAAC,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC;4BACzG,OAAO;wBACX,CAAC;wBACD,OAAO,GAAG,IAAI,CAAC;wBACf,qBAAqB,CAAC,IAAI,EAAE,aAAa,EAAE,CAAC,CAAC;oBACjD,CAAC;oBACD,cAAc,CAAC,IAAI;wBACf,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;4BAClB,OAAO,GAAG,IAAI,CAAC;wBACnB,CAAC;oBACL,CAAC;iBACJ,CAAC,CAAC;gBACH,IAAI,OAAO,EAAE,CAAC;oBACT,KAAK,CAAC,IAAI,CAAC,QAA+C,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC;gBACrF,CAAC;YACL,CAAC;SACJ;KACJ,CAAC;AACN,CAAC"}
@@ -19,16 +19,21 @@ 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 binds each generator to the context of its
25
- * creation.
26
- * 2. A plugin of this library gives the name of each async function to
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 sync generator to the context of
28
+ * its creation.
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
- * 3. `@babel/plugin-transform-async-generator-functions` changes async
32
+ * 4. A plugin of this library gives the async generators to `asyncGenerator`
33
+ * of the runtime, in place of the Babel helper.
34
+ * 5. `@babel/plugin-transform-async-generator-functions` changes async
30
35
  * generators and `for await` loops.
31
- * 4. `@babel/plugin-transform-async-to-generator` changes each async function
36
+ * 6. `@babel/plugin-transform-async-to-generator` changes each async function
32
37
  * into a generator that the `coroutine` function of the runtime operates.
33
38
  *
34
39
  * 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;AAMvE,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;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAG,EAAG,SAAS,EAAE,OAAO,GAAG,aAAkB,GAAI,YAAY,CAY9G"}
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;AAQvE,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;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAG,EAAG,SAAS,EAAE,OAAO,GAAG,aAAkB,GAAI,YAAY,CAc9G"}
@@ -1,22 +1,29 @@
1
1
  import asyncGeneratorFunctions from "@babel/plugin-transform-async-generator-functions";
2
2
  import asyncToGenerator from "@babel/plugin-transform-async-to-generator";
3
+ import { asyncGeneratorHelpersPlugin } from "./async-generator-helpers.js";
3
4
  import { bindGeneratorsPlugin } from "./bind-generators.js";
4
5
  import { nameCoroutinesPlugin } from "./name-coroutines.js";
6
+ import { skipRuntimePlugin } from "./skip-runtime.js";
5
7
  export { METADATA_KEY } from "./bind-generators.js";
6
8
  /** The default runtime module. */
7
9
  export const DEFAULT_RUNTIME = "async-browser-context/runtime";
8
10
  /**
9
11
  * The Babel preset of `async-browser-context`. It changes the code so that the
10
- * context stays through `await` and `yield`:
12
+ * context stays through `await` and `yield`. It does not change the runtime
13
+ * of the library, also when it transforms `node_modules`.
11
14
  *
12
- * 1. A plugin of this library binds each generator to the context of its
13
- * creation.
14
- * 2. A plugin of this library gives the name of each async function to
15
+ * 1. A plugin of this library skips the native async functions of the
16
+ * runtime, which a directive marks.
17
+ * 2. A plugin of this library binds each sync generator to the context of
18
+ * its creation.
19
+ * 3. A plugin of this library gives the name of each async function to
15
20
  * `coroutine`, for the callback hooks. The option `names: false` removes
16
21
  * this step.
17
- * 3. `@babel/plugin-transform-async-generator-functions` changes async
22
+ * 4. A plugin of this library gives the async generators to `asyncGenerator`
23
+ * of the runtime, in place of the Babel helper.
24
+ * 5. `@babel/plugin-transform-async-generator-functions` changes async
18
25
  * generators and `for await` loops.
19
- * 4. `@babel/plugin-transform-async-to-generator` changes each async function
26
+ * 6. `@babel/plugin-transform-async-to-generator` changes each async function
20
27
  * into a generator that the `coroutine` function of the runtime operates.
21
28
  *
22
29
  * The preset operates with Babel 7.22 and later and with Babel 8. Put it last
@@ -28,8 +35,10 @@ export default function asyncBrowserContextPreset(api, options = {}) {
28
35
  const names = options.names === false ? [] : [[nameCoroutinesPlugin, { runtime }]];
29
36
  return {
30
37
  plugins: [
38
+ skipRuntimePlugin,
31
39
  [bindGeneratorsPlugin, { runtime }],
32
40
  ...names,
41
+ [asyncGeneratorHelpersPlugin, { runtime }],
33
42
  asyncGeneratorFunctions,
34
43
  [asyncToGenerator, { module: runtime, method: "coroutine" }],
35
44
  ],
@@ -1 +1 @@
1
- {"version":3,"file":"preset.js","sourceRoot":"","sources":["../../src/babel/preset.ts"],"names":[],"mappings":"AACA,OAAO,uBAAuB,MAAM,mDAAmD,CAAC;AACxF,OAAO,gBAAgB,MAAM,4CAA4C,CAAC;AAC1E,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAkBpD,kCAAkC;AAClC,MAAM,CAAC,MAAM,eAAe,GAAG,+BAA+B,CAAC;AAE/D;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,CAAC,GAAe,EAAE,UAA0B,EAAE;IAC3F,GAAG,CAAC,aAAa,CAAC,mBAAmB,CAAC,CA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E,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,CAAC,2BAA2B,EAAE,EAAE,OAAO,EAAE,CAAC;YAC1C,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"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * The async generators of transformed code. `@babel/plugin-transform-async-generator-functions`
3
+ * changes each async generator into a sync generator and its helper `wrapAsyncGenerator`.
4
+ * The helper continued each step in a `then` callback, and a `then` callback is slow in
5
+ * Firefox. The preset replaces the helper with `asyncGenerator` of the runtime, and the
6
+ * helper `OverloadYield` with the class of this module. The driver in this module then
7
+ * continues each step with a native `await`.
8
+ *
9
+ * The protocol is the protocol of the Babel 8 helper: the body gives
10
+ * `new OverloadYield(value, 0)` for an `await`, and `new OverloadYield(promise, 1)` for a
11
+ * step of `yield*`. Each other value is a `yield`.
12
+ */
13
+ /**
14
+ * The value that the body gives for an `await` (kind 0) or for a step of `yield*` (kind 1).
15
+ * The Babel helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` make it with `new`.
16
+ */
17
+ export declare class OverloadYield {
18
+ v: unknown;
19
+ readonly k: 0 | 1;
20
+ constructor(value: unknown, kind: 0 | 1);
21
+ }
22
+ /** The methods of a generator that a request of the consumer starts. */
23
+ export type ResumeKey = "next" | "throw" | "return";
24
+ /**
25
+ * This function does one step of the body: `generator[key](argument)`. The browser runtime sets
26
+ * the context of the generator around the step. `resumed` is `true` for a step after an
27
+ * `await` of the driver.
28
+ */
29
+ export type AsyncGeneratorStep = (key: ResumeKey, argument: unknown, resumed: boolean) => IteratorResult<unknown, unknown>;
30
+ /**
31
+ * This function makes an async generator object. The object queues the requests of the
32
+ * consumer, and starts one request at a time, as the Babel helper does. `enter` starts the
33
+ * driver: the Node.js runtime starts it in the context of the generator.
34
+ */
35
+ export declare function createAsyncGenerator(step: AsyncGeneratorStep, NativePromise: PromiseConstructor, enter?: (start: () => Promise<void>) => Promise<void>): AsyncGenerator<unknown, unknown, unknown>;
36
+ //# sourceMappingURL=async-generator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async-generator.d.ts","sourceRoot":"","sources":["../../src/core/async-generator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH;;;GAGG;AACH,qBAAa,aAAa;IACtB,CAAC,EAAG,OAAO,CAAC;IACZ,QAAQ,CAAC,CAAC,EAAG,CAAC,GAAG,CAAC,CAAC;gBACP,KAAK,EAAG,OAAO,EAAE,IAAI,EAAG,CAAC,GAAG,CAAC;CAI5C;AAED,wEAAwE;AACxE,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAC;AAEpD;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,GAAG,CAAC,GAAG,EAAG,SAAS,EAAE,QAAQ,EAAG,OAAO,EAAE,OAAO,EAAG,OAAO,KAAK,cAAc,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAwC9H;;;;GAIG;AACH,wBAAgB,oBAAoB,CAChC,IAAI,EAAG,kBAAkB,EACzB,aAAa,EAAG,kBAAkB,EAClC,KAAK,GAAG,CAAC,KAAK,EAAG,MAAM,OAAO,CAAC,IAAI,CAAC,KAAK,OAAO,CAAC,IAAI,CAAsB,GAC3E,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CA2E5C"}
@@ -0,0 +1,132 @@
1
+ /**
2
+ * The async generators of transformed code. `@babel/plugin-transform-async-generator-functions`
3
+ * changes each async generator into a sync generator and its helper `wrapAsyncGenerator`.
4
+ * The helper continued each step in a `then` callback, and a `then` callback is slow in
5
+ * Firefox. The preset replaces the helper with `asyncGenerator` of the runtime, and the
6
+ * helper `OverloadYield` with the class of this module. The driver in this module then
7
+ * continues each step with a native `await`.
8
+ *
9
+ * The protocol is the protocol of the Babel 8 helper: the body gives
10
+ * `new OverloadYield(value, 0)` for an `await`, and `new OverloadYield(promise, 1)` for a
11
+ * step of `yield*`. Each other value is a `yield`.
12
+ */
13
+ /**
14
+ * The value that the body gives for an `await` (kind 0) or for a step of `yield*` (kind 1).
15
+ * The Babel helpers `awaitAsyncGenerator` and `asyncGeneratorDelegate` make it with `new`.
16
+ */
17
+ export class OverloadYield {
18
+ v;
19
+ k;
20
+ constructor(value, kind) {
21
+ this.v = value;
22
+ this.k = kind;
23
+ }
24
+ }
25
+ /** The key of the request function of each async generator object. */
26
+ const INVOKE = Symbol("async-browser-context/invoke");
27
+ function invokeOf(target) {
28
+ const invoke = target[INVOKE];
29
+ if (invoke === undefined) {
30
+ throw new TypeError("The object is not an async generator of async-browser-context");
31
+ }
32
+ return invoke;
33
+ }
34
+ /** The prototype of the async generator objects of transformed code. */
35
+ const AsyncGeneratorPrototype = {
36
+ next(value) {
37
+ return invokeOf(this)("next", value);
38
+ },
39
+ throw(error) {
40
+ return invokeOf(this)("throw", error);
41
+ },
42
+ return(value) {
43
+ return invokeOf(this)("return", value);
44
+ },
45
+ [Symbol.asyncIterator]() {
46
+ return this;
47
+ },
48
+ [Symbol.toStringTag]: "AsyncGenerator",
49
+ };
50
+ /**
51
+ * This function makes an async generator object. The object queues the requests of the
52
+ * consumer, and starts one request at a time, as the Babel helper does. `enter` starts the
53
+ * driver: the Node.js runtime starts it in the context of the generator.
54
+ */
55
+ export function createAsyncGenerator(step, NativePromise, enter = (start) => start()) {
56
+ let first;
57
+ let last;
58
+ /** This function starts the requests in the queue, one after the other, until the queue is empty. */
59
+ const run = async (key, argument) => {
60
+ // The directive tells the Babel preset not to change this function (src/babel/skip-runtime.ts)
61
+ "async-browser-context runtime";
62
+ let resumed = false;
63
+ for (;;) {
64
+ let failed = false;
65
+ let settled;
66
+ let done = false;
67
+ try {
68
+ let result = step(key, argument, resumed);
69
+ for (;;) {
70
+ const yielded = result.value;
71
+ const overloaded = yielded instanceof OverloadYield;
72
+ let value;
73
+ try {
74
+ value = await (overloaded ? yielded.v : yielded);
75
+ }
76
+ catch (error) {
77
+ resumed = true;
78
+ key = "throw";
79
+ result = step("throw", error, true);
80
+ continue;
81
+ }
82
+ resumed = true;
83
+ if (overloaded) {
84
+ const nextKey = key === "return" && yielded.k === 1 ? "return" : "next";
85
+ if (yielded.k === 0 || Boolean(value.done)) {
86
+ key = nextKey;
87
+ result = step(nextKey, value, true);
88
+ continue;
89
+ }
90
+ value = step(nextKey, value, true).value;
91
+ }
92
+ settled = value;
93
+ done = result.done === true;
94
+ break;
95
+ }
96
+ }
97
+ catch (error) {
98
+ failed = true;
99
+ settled = error;
100
+ }
101
+ // A request always waits in the queue while the driver runs
102
+ const request = first;
103
+ first = request.next;
104
+ if (failed) {
105
+ request.reject(settled);
106
+ }
107
+ else {
108
+ request.resolve({ value: settled, done });
109
+ }
110
+ if (first === undefined) {
111
+ last = undefined;
112
+ return;
113
+ }
114
+ key = first.key;
115
+ argument = first.argument;
116
+ }
117
+ };
118
+ const invoke = (key, argument) => new NativePromise((resolve, reject) => {
119
+ const request = { key, argument, resolve, reject, next: undefined };
120
+ if (last === undefined) {
121
+ first = last = request;
122
+ void enter(() => run(key, argument));
123
+ }
124
+ else {
125
+ last = last.next = request;
126
+ }
127
+ });
128
+ const generator = Object.create(AsyncGeneratorPrototype);
129
+ Object.defineProperty(generator, INVOKE, { value: invoke });
130
+ return generator;
131
+ }
132
+ //# sourceMappingURL=async-generator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"async-generator.js","sourceRoot":"","sources":["../../src/core/async-generator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH;;;GAGG;AACH,MAAM,OAAO,aAAa;IACtB,CAAC,CAAW;IACH,CAAC,CAAS;IACnB,YAAY,KAAe,EAAE,IAAY;QACrC,IAAI,CAAC,CAAC,GAAG,KAAK,CAAC;QACf,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC;IAClB,CAAC;CACJ;AAsBD,sEAAsE;AACtE,MAAM,MAAM,GAAmB,MAAM,CAAC,8BAA8B,CAAC,CAAC;AAEtE,SAAS,QAAQ,CAAC,MAAe;IAC7B,MAAM,MAAM,GAAI,MAAiC,CAAC,MAAM,CAAC,CAAC;IAC1D,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACvB,MAAM,IAAI,SAAS,CAAC,+DAA+D,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AO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YAC5B,CAAC;iBAAM,CAAC;gBACJ,OAAO,CAAC,OAAO,CAAC,EAAE,KAAK,EAAG,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;YAC/C,CAAC;YACD,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACtB,IAAI,GAAG,SAAS,CAAC;gBACjB,OAAO;YACX,CAAC;YACD,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC;YAChB,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAC;QAC9B,CAAC;IACL,CAAC,CAAC;IAEF,MAAM,MAAM,GAAY,CAAC,GAAG,EAAE,QAAQ,EAAE,EAAE,CAAC,IAAI,aAAa,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC7E,MAAM,OAAO,GAAa,EAAE,GAAG,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAG,SAAS,EAAE,CAAC;QAC/E,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACrB,KAAK,GAAG,IAAI,GAAG,OAAO,CAAC;YACvB,KAAK,KAAK,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC;QACzC,CAAC;aAAM,CAAC;YACJ,IAAI,GAAG,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;QAC/B,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,uBAAuB,CAA8C,CAAC;IACtG,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,KAAK,EAAG,MAAM,EAAE,CAAC,CAAC;IAC7D,OAAO,SAAS,CAAC;AACrB,CAAC"}
@@ -4,6 +4,15 @@
4
4
  * `method` option, and adds `name`, the name of the async function, if it has one.
5
5
  */
6
6
  export declare function coroutine<T, A extends unknown[], R>(generatorFunction: (this: T, ...args: A) => Generator<unknown, R, unknown>, name?: string): (this: T, ...args: A) => Promise<R>;
7
+ /**
8
+ * This function gives a function that makes an async generator object, which operates
9
+ * `generatorFunction`. The preset gives this function in place of the helper
10
+ * `wrapAsyncGenerator` of `@babel/plugin-transform-async-generator-functions`.
11
+ *
12
+ * Each step of the body operates in the context of the call that made the generator
13
+ * (rule C5). The driver continues each step with a native `await` (`async-generator.ts`).
14
+ */
15
+ export declare function asyncGenerator<T, A extends unknown[]>(generatorFunction: (this: T, ...args: A) => Generator<unknown, unknown, unknown>): (this: T, ...args: A) => AsyncGenerator<unknown, unknown, unknown>;
7
16
  type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown, unknown, unknown>;
8
17
  /**
9
18
  * This function binds a generator object to the current context. The body of
@@ -11,9 +20,10 @@ type AnyIterator = Iterator<unknown, unknown, unknown> | AsyncIterator<unknown,
11
20
  * proposal specifies (`GeneratorStart` and `GeneratorResume`). After each step,
12
21
  * the context of the caller is current again.
13
22
  *
14
- * The preset changes each generator function so that it gives
15
- * `bindGenerator(generator)`. The function works with sync generators and
16
- * with the async generators of `@babel/plugin-transform-async-generator-functions`.
23
+ * The preset changes each sync generator function so that it gives
24
+ * `bindGenerator(generator)`. An async generator does not need it, because
25
+ * `asyncGenerator` keeps the context itself. The function also works with
26
+ * async generator objects, for example in the output of older versions.
17
27
  */
18
28
  export declare function bindGenerator<I extends AnyIterator>(generator: I): I;
19
29
  export {};
@@ -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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