@hoajs/context-storage 0.1.1 → 0.1.2
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 +12 -7
- package/LICENSE +21 -21
- package/README.md +51 -51
- package/dist/cjs/context-storage.cjs +23 -0
- package/dist/esm/context-storage.mjs +20 -0
- package/package.json +12 -12
- package/types/index.d.ts +8 -8
- package/dist/cjs/context-storage.js +0 -47
- package/dist/esm/context-storage.js +0 -22
package/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
## v0.1.
|
|
2
|
-
|
|
3
|
-
- feat:
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
## v0.1.2 / 2026-02-10
|
|
2
|
+
|
|
3
|
+
- feat: use tsdown instead of tsup
|
|
4
|
+
- chore(deps): update deps
|
|
5
|
+
|
|
6
|
+
## v0.1.1 / 2026-02-10
|
|
7
|
+
|
|
8
|
+
- feat: add MockContext
|
|
9
|
+
|
|
10
|
+
## v0.1.0 / 2025-12-11
|
|
11
|
+
|
|
12
|
+
- init
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 - present, Hoa 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 - present, Hoa 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
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
## @hoajs/context-storage
|
|
2
|
-
|
|
3
|
-
Context storage middleware for Hoa.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
$ npm i @hoajs/context-storage --save
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Quick Start
|
|
12
|
-
|
|
13
|
-
```js
|
|
14
|
-
import { Hoa } from 'hoa'
|
|
15
|
-
import { contextStorage, getContext } from '@hoajs/context-storage'
|
|
16
|
-
|
|
17
|
-
const app = new Hoa()
|
|
18
|
-
app.use(contextStorage())
|
|
19
|
-
|
|
20
|
-
app.use(async (ctx, next) => {
|
|
21
|
-
ctx.state.requestId = crypto.randomUUID()
|
|
22
|
-
await next()
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
app.use(async (ctx, next) => {
|
|
26
|
-
log('Request start')
|
|
27
|
-
await doSomething()
|
|
28
|
-
log('Request end')
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
function log (msg) {
|
|
32
|
-
const ctx = getContext()
|
|
33
|
-
console.log(`[${ctx.state.requestId}] ${msg}`)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export default app
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
## Documentation
|
|
40
|
-
|
|
41
|
-
The documentation is available on [hoa-js.com](https://hoa-js.com/middleware/context-storage.html)
|
|
42
|
-
|
|
43
|
-
## Test (100% coverage)
|
|
44
|
-
|
|
45
|
-
```sh
|
|
46
|
-
$ npm test
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
## License
|
|
50
|
-
|
|
51
|
-
MIT
|
|
1
|
+
## @hoajs/context-storage
|
|
2
|
+
|
|
3
|
+
Context storage middleware for Hoa.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
$ npm i @hoajs/context-storage --save
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { Hoa } from 'hoa'
|
|
15
|
+
import { contextStorage, getContext } from '@hoajs/context-storage'
|
|
16
|
+
|
|
17
|
+
const app = new Hoa()
|
|
18
|
+
app.use(contextStorage())
|
|
19
|
+
|
|
20
|
+
app.use(async (ctx, next) => {
|
|
21
|
+
ctx.state.requestId = crypto.randomUUID()
|
|
22
|
+
await next()
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
app.use(async (ctx, next) => {
|
|
26
|
+
log('Request start')
|
|
27
|
+
await doSomething()
|
|
28
|
+
log('Request end')
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
function log (msg) {
|
|
32
|
+
const ctx = getContext()
|
|
33
|
+
console.log(`[${ctx.state.requestId}] ${msg}`)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default app
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Documentation
|
|
40
|
+
|
|
41
|
+
The documentation is available on [hoa-js.com](https://hoa-js.com/middleware/context-storage.html)
|
|
42
|
+
|
|
43
|
+
## Test (100% coverage)
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
$ npm test
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
|
|
51
|
+
MIT
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
let node_async_hooks = require("node:async_hooks");
|
|
3
|
+
|
|
4
|
+
//#region src/context-storage.js
|
|
5
|
+
const asyncLocalStorage = new node_async_hooks.AsyncLocalStorage();
|
|
6
|
+
function contextStorage() {
|
|
7
|
+
return async function contextStorageMiddleware(ctx, next) {
|
|
8
|
+
await asyncLocalStorage.run(ctx, next);
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function getContext() {
|
|
12
|
+
const context = asyncLocalStorage.getStore();
|
|
13
|
+
if (!context) throw new Error("Context is not available");
|
|
14
|
+
return context;
|
|
15
|
+
}
|
|
16
|
+
function MockContext(ctx, next) {
|
|
17
|
+
return asyncLocalStorage.run(ctx, next);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
exports.MockContext = MockContext;
|
|
22
|
+
exports.contextStorage = contextStorage;
|
|
23
|
+
exports.getContext = getContext;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
|
|
3
|
+
//#region src/context-storage.js
|
|
4
|
+
const asyncLocalStorage = new AsyncLocalStorage();
|
|
5
|
+
function contextStorage() {
|
|
6
|
+
return async function contextStorageMiddleware(ctx, next) {
|
|
7
|
+
await asyncLocalStorage.run(ctx, next);
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
function getContext() {
|
|
11
|
+
const context = asyncLocalStorage.getStore();
|
|
12
|
+
if (!context) throw new Error("Context is not available");
|
|
13
|
+
return context;
|
|
14
|
+
}
|
|
15
|
+
function MockContext(ctx, next) {
|
|
16
|
+
return asyncLocalStorage.run(ctx, next);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
export { MockContext, contextStorage, getContext };
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hoajs/context-storage",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Context storage middleware for Hoa.",
|
|
5
|
-
"main": "./dist/cjs/context-storage.
|
|
5
|
+
"main": "./dist/cjs/context-storage.cjs",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"module": "./dist/esm/context-storage.
|
|
7
|
+
"module": "./dist/esm/context-storage.mjs",
|
|
8
8
|
"types": "./types/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"types": "./types/index.d.ts",
|
|
12
|
-
"import": "./dist/esm/context-storage.
|
|
13
|
-
"require": "./dist/cjs/context-storage.
|
|
14
|
-
"default": "./dist/esm/context-storage.
|
|
12
|
+
"import": "./dist/esm/context-storage.mjs",
|
|
13
|
+
"require": "./dist/cjs/context-storage.cjs",
|
|
14
|
+
"default": "./dist/esm/context-storage.mjs"
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
17
|
"files": [
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
],
|
|
22
22
|
"scripts": {
|
|
23
23
|
"lint": "eslint .",
|
|
24
|
-
"build": "
|
|
24
|
+
"build": "tsdown",
|
|
25
25
|
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
|
|
26
26
|
"prepublishOnly": "npm run lint && npm run test && npm run build",
|
|
27
27
|
"prepare": "husky"
|
|
@@ -40,16 +40,16 @@
|
|
|
40
40
|
"async_hooks"
|
|
41
41
|
],
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@commitlint/cli": "20.
|
|
44
|
-
"@commitlint/config-conventional": "20.
|
|
43
|
+
"@commitlint/cli": "20.4.1",
|
|
44
|
+
"@commitlint/config-conventional": "20.4.1",
|
|
45
45
|
"@jest/globals": "30.2.0",
|
|
46
|
-
"eslint": "9.39.
|
|
47
|
-
"globals": "
|
|
46
|
+
"eslint": "9.39.2",
|
|
47
|
+
"globals": "17.3.0",
|
|
48
48
|
"hoa": "*",
|
|
49
49
|
"husky": "9.1.7",
|
|
50
50
|
"jest": "30.2.0",
|
|
51
51
|
"neostandard": "0.12.2",
|
|
52
|
-
"
|
|
52
|
+
"tsdown": "^0.20.3"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"hoa": "*"
|
package/types/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { HoaMiddleware, HoaContext } from 'hoa'
|
|
2
|
-
|
|
3
|
-
type MaybePromise<T> = T | Promise<T>
|
|
4
|
-
|
|
5
|
-
export declare function contextStorage (): HoaMiddleware
|
|
6
|
-
|
|
7
|
-
export declare function getContext (): HoaContext
|
|
8
|
-
|
|
1
|
+
import type { HoaMiddleware, HoaContext } from 'hoa'
|
|
2
|
+
|
|
3
|
+
type MaybePromise<T> = T | Promise<T>
|
|
4
|
+
|
|
5
|
+
export declare function contextStorage (): HoaMiddleware
|
|
6
|
+
|
|
7
|
+
export declare function getContext (): HoaContext
|
|
8
|
+
|
|
9
9
|
export declare function MockContext<T = unknown> (ctx: HoaContext, next: () => MaybePromise<T>): MaybePromise<T>
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
var context_storage_exports = {};
|
|
19
|
-
__export(context_storage_exports, {
|
|
20
|
-
MockContext: () => MockContext,
|
|
21
|
-
contextStorage: () => contextStorage,
|
|
22
|
-
getContext: () => getContext
|
|
23
|
-
});
|
|
24
|
-
module.exports = __toCommonJS(context_storage_exports);
|
|
25
|
-
var import_node_async_hooks = require("node:async_hooks");
|
|
26
|
-
const asyncLocalStorage = new import_node_async_hooks.AsyncLocalStorage();
|
|
27
|
-
function contextStorage() {
|
|
28
|
-
return async function contextStorageMiddleware(ctx, next) {
|
|
29
|
-
await asyncLocalStorage.run(ctx, next);
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
function getContext() {
|
|
33
|
-
const context = asyncLocalStorage.getStore();
|
|
34
|
-
if (!context) {
|
|
35
|
-
throw new Error("Context is not available");
|
|
36
|
-
}
|
|
37
|
-
return context;
|
|
38
|
-
}
|
|
39
|
-
function MockContext(ctx, next) {
|
|
40
|
-
return asyncLocalStorage.run(ctx, next);
|
|
41
|
-
}
|
|
42
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
43
|
-
0 && (module.exports = {
|
|
44
|
-
MockContext,
|
|
45
|
-
contextStorage,
|
|
46
|
-
getContext
|
|
47
|
-
});
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
-
const asyncLocalStorage = new AsyncLocalStorage();
|
|
3
|
-
function contextStorage() {
|
|
4
|
-
return async function contextStorageMiddleware(ctx, next) {
|
|
5
|
-
await asyncLocalStorage.run(ctx, next);
|
|
6
|
-
};
|
|
7
|
-
}
|
|
8
|
-
function getContext() {
|
|
9
|
-
const context = asyncLocalStorage.getStore();
|
|
10
|
-
if (!context) {
|
|
11
|
-
throw new Error("Context is not available");
|
|
12
|
-
}
|
|
13
|
-
return context;
|
|
14
|
-
}
|
|
15
|
-
function MockContext(ctx, next) {
|
|
16
|
-
return asyncLocalStorage.run(ctx, next);
|
|
17
|
-
}
|
|
18
|
-
export {
|
|
19
|
-
MockContext,
|
|
20
|
-
contextStorage,
|
|
21
|
-
getContext
|
|
22
|
-
};
|