async-browser-context 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Change history
2
2
 
3
+ ## 0.1.1
4
+
5
+ This version changes only the documentation. The code is the same as in 0.1.0.
6
+
7
+ - The README describes the boundaries with code without the transform: the guarantee of rule C7, and the function that keeps the context at each boundary.
8
+
3
9
  ## 0.1.0
4
10
 
5
11
  This version replaces all of the first design. `docs/remediation-plan.md` gives the reasons, and `docs/design.md` describes the new design.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 AsyncBrowserContext Contributors
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2025 AsyncBrowserContext Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -171,7 +171,7 @@ The tests examine each rule on the browser runtime, on the Node.js runtime and i
171
171
  | C4 | A `then`, `catch` or `finally` callback gets the context of the `then`, `catch` or `finally` call. |
172
172
  | C5 | A generator body gets the context of the call that made the generator. After each step, the context of the caller is current again. |
173
173
  | C6 | A timer callback gets the context of the call that scheduled it. |
174
- | C7 | Code that the transform does not change does not get the context of a different operation at any time. |
174
+ | C7 | Code that the transform does not change cannot get the context of a different operation. At most, it gets no context after its own native `await`. |
175
175
  | C8 | `snapshot.run()` and `Snapshot.wrap()` start functions in the recorded context. |
176
176
  | C9 | Only code that has a `Variable` can read its values. The library puts no values on `globalThis`. |
177
177
  | C10 | Two copies of the library on one page use one context store. |
@@ -194,13 +194,33 @@ The tests examine each rule on the browser runtime, on the Node.js runtime and i
194
194
 
195
195
  The patches keep the names, the lengths and the source text of the native functions. `Function.prototype.toString` gives the source text of the original function.
196
196
 
197
- ## Limits
197
+ ## Boundaries
198
198
 
199
- - **Code that the transform does not change.** On the browser runtime, this code gets the root context after `await` (rule C7). Use the Vite plugin, which transforms the dependencies, or use `Snapshot.wrap()` for a callback.
200
- - **Other patches of `Promise.prototype.then`.** Do not use the library together with zone.js or with another library that patches `then`.
201
- - **Transformed generator functions.** A transformed generator function is an ordinary function that gives a generator. `fn.prototype` and `instanceof fn` do not operate as for a native generator function. The generator objects operate as native generator objects.
199
+ In transformed code, each step gets the context of its own operation. Code without the transform cannot get the context of a different operation (rule C7). At most, that code gets no context after its own native `await`. Where your code meets that code, one function of the library keeps the context:
200
+
201
+ | The other code | What to do |
202
+ | --- | --- |
203
+ | Gives a promise, and your code awaits it | Nothing. After the `await`, your code gets its context again. |
204
+ | Starts your callback synchronously, or from a timer, an observer or a promise that it registers before its first `await` | Nothing. The patched APIs keep the context. Event listeners follow rule C13. |
205
+ | Starts your callback after its own native `await` | `AsyncLocalStorage.bind(callback)` or `AsyncContext.Snapshot.wrap(callback)` |
206
+ | Keeps your callback in a list and starts it later from other code | `AsyncLocalStorage.bind(callback)` when you give the callback |
207
+ | Reads the context itself after its own native `await` | Give the values as arguments, or transform the code. |
208
+ | Operates in a worker or an iframe | Send the values in the message, and use `run()` on the other side. |
209
+
210
+ ```ts
211
+ untransformedLibrary.onDone(AsyncLocalStorage.bind((result) => {
212
+ log(requestId.getStore(), result); // the context of the bind() use
213
+ }));
214
+ ```
215
+
216
+ The Vite plugin transforms the dependencies in `node_modules` by default. Thus, these boundaries are usually only scripts from other servers, browser extensions and code from `eval()` or `new Function()`. The tests in `test/rules/c07-boundaries.test.ts` examine each row. The [boundaries page](https://mark1russell7.github.io/AsyncBrowserContext/docs/concepts/boundaries) of the website gives the details.
217
+
218
+ Do not use the library together with zone.js or with another library that patches `Promise.prototype.then`. The two libraries patch the same functions with different rules. For OpenTelemetry, use `AsyncContextManager` in place of the `ZoneContextManager`.
219
+
220
+ ### Other differences of transformed code
221
+
222
+ - **Generator functions.** A transformed generator function is an ordinary function that gives a generator. `fn.prototype` and `instanceof fn` do not operate as for a native generator function. The generator objects operate as native generator objects.
202
223
  - **Stack traces.** In transformed code, a stack trace shows the generator and the `coroutine` function of the runtime.
203
- - **Other realms.** The context does not go to web workers, iframes or code in `eval()` and `new Function()`.
204
224
 
205
225
  ## Performance
206
226
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "async-browser-context",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
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",