@b9g/async-context 0.1.3 → 0.2.0-beta.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/README.md +14 -16
- package/package.json +2 -5
- package/src/index.js +1 -1
package/README.md
CHANGED
|
@@ -2,8 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
Lightweight polyfill for the [TC39 AsyncContext proposal](https://github.com/tc39/proposal-async-context) using Node.js `AsyncLocalStorage`.
|
|
4
4
|
|
|
5
|
-
## Why This Package?
|
|
6
|
-
|
|
7
5
|
The TC39 AsyncContext proposal aims to standardize async context propagation in JavaScript. However:
|
|
8
6
|
|
|
9
7
|
- The proposal is still Stage 2 (not yet standardized)
|
|
@@ -12,11 +10,11 @@ The TC39 AsyncContext proposal aims to standardize async context propagation in
|
|
|
12
10
|
|
|
13
11
|
This package provides a **lightweight, maintainable polyfill** that:
|
|
14
12
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
- Implements the TC39 `AsyncContext.Variable` and `AsyncContext.Snapshot` APIs
|
|
14
|
+
- Uses battle-tested `AsyncLocalStorage` under the hood
|
|
15
|
+
- Zero dependencies (beyond Node.js built-ins)
|
|
16
|
+
- Full TypeScript support
|
|
17
|
+
- Production-ready and well-tested
|
|
20
18
|
|
|
21
19
|
## Installation
|
|
22
20
|
|
|
@@ -31,7 +29,7 @@ bun add @b9g/async-context
|
|
|
31
29
|
### Basic Example
|
|
32
30
|
|
|
33
31
|
```typescript
|
|
34
|
-
import {
|
|
32
|
+
import {AsyncContext} from "@b9g/async-context";
|
|
35
33
|
|
|
36
34
|
// Create a context variable
|
|
37
35
|
const userContext = new AsyncContext.Variable<User>();
|
|
@@ -48,7 +46,7 @@ userContext.run(currentUser, async () => {
|
|
|
48
46
|
### Request Context (Web Server)
|
|
49
47
|
|
|
50
48
|
```typescript
|
|
51
|
-
import {
|
|
49
|
+
import {AsyncVariable} from "@b9g/async-context";
|
|
52
50
|
|
|
53
51
|
interface RequestContext {
|
|
54
52
|
requestId: string;
|
|
@@ -227,17 +225,17 @@ The polyfill provides:
|
|
|
227
225
|
|
|
228
226
|
This package works in any JavaScript runtime that supports `AsyncLocalStorage`:
|
|
229
227
|
|
|
230
|
-
-
|
|
231
|
-
-
|
|
232
|
-
-
|
|
233
|
-
-
|
|
228
|
+
- Node.js 12.17+
|
|
229
|
+
- Bun
|
|
230
|
+
- Deno
|
|
231
|
+
- Cloudflare Workers (with "nodejs_compat" flag)
|
|
234
232
|
|
|
235
233
|
## Differences from TC39 Proposal
|
|
236
234
|
|
|
237
235
|
This polyfill implements the core TC39 AsyncContext API:
|
|
238
236
|
|
|
239
|
-
-
|
|
240
|
-
-
|
|
237
|
+
- `AsyncContext.Variable` - context variables with `run()` and `get()`
|
|
238
|
+
- `AsyncContext.Snapshot` - context capture with `run()` and `wrap()`
|
|
241
239
|
|
|
242
240
|
The implementation uses Node.js `AsyncLocalStorage` rather than the pure-JS reference implementation, which means async context propagation works natively without monkey-patching `Promise.prototype.then`.
|
|
243
241
|
|
|
@@ -273,4 +271,4 @@ MIT
|
|
|
273
271
|
|
|
274
272
|
- [TC39 AsyncContext Proposal](https://github.com/tc39/proposal-async-context)
|
|
275
273
|
- [Node.js AsyncLocalStorage](https://nodejs.org/api/async_context.html#class-asynclocalstorage)
|
|
276
|
-
- [Shovel Framework](https://github.com/
|
|
274
|
+
- [Shovel Framework](https://github.com/bikeshaving/shovel)
|
package/package.json
CHANGED
|
@@ -1,24 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@b9g/async-context",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0-beta.0",
|
|
4
4
|
"description": "Lightweight AsyncContext polyfill for JavaScript runtimes. Implements TC39 AsyncContext proposal using AsyncLocalStorage.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"asynccontext",
|
|
7
7
|
"async-context",
|
|
8
|
-
"context",
|
|
9
8
|
"async-local-storage",
|
|
10
9
|
"asynclocalstorage",
|
|
11
10
|
"tc39",
|
|
12
11
|
"polyfill",
|
|
13
12
|
"async",
|
|
14
|
-
"continuation",
|
|
15
13
|
"shovel"
|
|
16
14
|
],
|
|
17
15
|
"dependencies": {},
|
|
18
16
|
"devDependencies": {
|
|
19
17
|
"@b9g/libuild": "^0.1.18",
|
|
20
|
-
"@types/node": "^20.0.0"
|
|
21
|
-
"bun-types": "latest"
|
|
18
|
+
"@types/node": "^20.0.0"
|
|
22
19
|
},
|
|
23
20
|
"type": "module",
|
|
24
21
|
"types": "src/index.d.ts",
|
package/src/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// src/index.ts
|
|
3
3
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
4
|
var variableRegistry = /* @__PURE__ */ new Set();
|
|
5
|
-
var NO_VALUE = Symbol("NO_VALUE");
|
|
5
|
+
var NO_VALUE = /* @__PURE__ */ Symbol("NO_VALUE");
|
|
6
6
|
var AsyncVariable = class {
|
|
7
7
|
#storage;
|
|
8
8
|
#defaultValue;
|