@dalet-oss/express-http-context 1.0.0-DEV → 1.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 +25 -0
- package/lib/cjs/index.js +36 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/package.json +3 -0
- package/lib/esm/index.js +30 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/lib/types/index.d.ts +29 -0
- package/package.json +99 -55
- package/src/index.ts +51 -0
- package/browser.js +0 -12
- package/index.d.ts +0 -24
- package/index.js +0 -39
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@dalet-oss/express-http-context` project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [1.0.0] - 2023-03-06
|
|
9
|
+
|
|
10
|
+
First stable version released after being tested in a production environment with intense traffic.
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
|
|
14
|
+
- Improved `README.md` file.
|
|
15
|
+
- Improved variable names in TS typings.
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
|
|
19
|
+
- Upgrade dependencies: Eslint plugins and Node.js types.
|
|
20
|
+
|
|
21
|
+
## [0.0.1] - 2023-03-04
|
|
22
|
+
|
|
23
|
+
### Added
|
|
24
|
+
|
|
25
|
+
- First version.
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.set = exports.get = exports.middleware = void 0;
|
|
4
|
+
const async_hooks_1 = require("async_hooks");
|
|
5
|
+
if (!global.expressHttpContextStorage) {
|
|
6
|
+
global.expressHttpContextStorage = new async_hooks_1.AsyncLocalStorage();
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Express.js middleware that is responsible for initializing the context for each request.
|
|
10
|
+
*/
|
|
11
|
+
const middleware = (_req, _res, next) => {
|
|
12
|
+
global.expressHttpContextStorage.run(new Map(), () => next());
|
|
13
|
+
};
|
|
14
|
+
exports.middleware = middleware;
|
|
15
|
+
/**
|
|
16
|
+
* Gets a value from the context by key.
|
|
17
|
+
* Will return undefined if the context has not yet been initialized for this request
|
|
18
|
+
* or if a value is not found for the specified key.
|
|
19
|
+
*/
|
|
20
|
+
function get(key) {
|
|
21
|
+
const store = global.expressHttpContextStorage.getStore();
|
|
22
|
+
return store?.get(key);
|
|
23
|
+
}
|
|
24
|
+
exports.get = get;
|
|
25
|
+
/**
|
|
26
|
+
* Adds a value to the context by key.
|
|
27
|
+
* If the key already exists, its value will be overwritten.
|
|
28
|
+
* No value will persist if the context has not yet been initialized.
|
|
29
|
+
*/
|
|
30
|
+
function set(key, value) {
|
|
31
|
+
const store = global.expressHttpContextStorage.getStore();
|
|
32
|
+
store?.set(key, value);
|
|
33
|
+
}
|
|
34
|
+
exports.set = set;
|
|
35
|
+
exports.default = { get, middleware: exports.middleware, set };
|
|
36
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,6CAA+C;AAS/C,IAAI,CAAC,MAAM,CAAC,yBAAyB,EAAE;IACtC,MAAM,CAAC,yBAAyB,GAAG,IAAI,+BAAiB,EAAoB,CAAA;CAC5E;AAID;;GAEG;AACI,MAAM,UAAU,GAAmB,CACzC,IAAa,EACb,IAAc,EACd,IAAkB,EACX,EAAE;IACT,MAAM,CAAC,yBAAyB,CAAC,GAAG,CAAC,IAAI,GAAG,EAAE,EAAE,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC,CAAA;AAC9D,CAAC,CAAA;AANY,QAAA,UAAU,cAMtB;AAED;;;;GAIG;AACH,SAAgB,GAAG,CAAU,GAAW;IAEvC,MAAM,KAAK,GAAG,MAAM,CAAC,yBAAyB,CAAC,QAAQ,EAAE,CAAC;IAE1D,OAAO,KAAK,EAAE,GAAG,CAAC,GAAG,CAAC,CAAA;AACvB,CAAC;AALD,kBAKC;AAED;;;;GAIG;AACH,SAAgB,GAAG,CAAU,GAAW,EAAE,KAAQ;IAEjD,MAAM,KAAK,GAAG,MAAM,CAAC,yBAAyB,CAAC,QAAQ,EAAE,CAAC;IAE1D,KAAK,EAAE,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;AACvB,CAAC;AALD,kBAKC;AAED,kBAAe,EAAE,GAAG,EAAE,UAAU,EAAV,kBAAU,EAAE,GAAG,EAAE,CAAA"}
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
2
|
+
if (!global.expressHttpContextStorage) {
|
|
3
|
+
global.expressHttpContextStorage = new AsyncLocalStorage();
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Express.js middleware that is responsible for initializing the context for each request.
|
|
7
|
+
*/
|
|
8
|
+
export const middleware = (_req, _res, next) => {
|
|
9
|
+
global.expressHttpContextStorage.run(new Map(), () => next());
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Gets a value from the context by key.
|
|
13
|
+
* Will return undefined if the context has not yet been initialized for this request
|
|
14
|
+
* or if a value is not found for the specified key.
|
|
15
|
+
*/
|
|
16
|
+
export function get(key) {
|
|
17
|
+
const store = global.expressHttpContextStorage.getStore();
|
|
18
|
+
return store?.get(key);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Adds a value to the context by key.
|
|
22
|
+
* If the key already exists, its value will be overwritten.
|
|
23
|
+
* No value will persist if the context has not yet been initialized.
|
|
24
|
+
*/
|
|
25
|
+
export function set(key, value) {
|
|
26
|
+
const store = global.expressHttpContextStorage.getStore();
|
|
27
|
+
store?.set(key, value);
|
|
28
|
+
}
|
|
29
|
+
export default { get, middleware, set };
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAS/C,IAAI,CAAC,MAAM,CAAC,yBAAyB,EAAE;IACtC,MAAM,CAAC,yBAAyB,GAAG,IAAI,iBAAiB,EAAoB,CAAA;CAC5E;AAID;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAmB,CACzC,IAAa,EACb,IAAc,EACd,IAAkB,EACX,EAAE;IACT,MAAM,CAAC,yBAAyB,CAAC,GAAG,CAAC,IAAI,GAAG,EAAE,EAAE,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC,CAAA;AAC9D,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,UAAU,GAAG,CAAU,GAAW;IAEvC,MAAM,KAAK,GAAG,MAAM,CAAC,yBAAyB,CAAC,QAAQ,EAAE,CAAC;IAE1D,OAAO,KAAK,EAAE,GAAG,CAAC,GAAG,CAAC,CAAA;AACvB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,GAAG,CAAU,GAAW,EAAE,KAAQ;IAEjD,MAAM,KAAK,GAAG,MAAM,CAAC,yBAAyB,CAAC,QAAQ,EAAE,CAAC;IAE1D,KAAK,EAAE,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;AACvB,CAAC;AAED,eAAe,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,CAAA"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="qs" />
|
|
3
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
4
|
+
import type { RequestHandler } from 'express';
|
|
5
|
+
declare global {
|
|
6
|
+
var expressHttpContextStorage: AsyncLocalStorage<Map<string, any>>;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Express.js middleware that is responsible for initializing the context for each request.
|
|
10
|
+
*/
|
|
11
|
+
export declare const middleware: RequestHandler;
|
|
12
|
+
/**
|
|
13
|
+
* Gets a value from the context by key.
|
|
14
|
+
* Will return undefined if the context has not yet been initialized for this request
|
|
15
|
+
* or if a value is not found for the specified key.
|
|
16
|
+
*/
|
|
17
|
+
export declare function get<T = any>(key: string): T | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Adds a value to the context by key.
|
|
20
|
+
* If the key already exists, its value will be overwritten.
|
|
21
|
+
* No value will persist if the context has not yet been initialized.
|
|
22
|
+
*/
|
|
23
|
+
export declare function set<T = any>(key: string, value: T): void;
|
|
24
|
+
declare const _default: {
|
|
25
|
+
get: typeof get;
|
|
26
|
+
middleware: RequestHandler<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>;
|
|
27
|
+
set: typeof set;
|
|
28
|
+
};
|
|
29
|
+
export default _default;
|
package/package.json
CHANGED
|
@@ -1,57 +1,101 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
"
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
2
|
+
"name": "@dalet-oss/express-http-context",
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"description": "Modern request-scoped storage support for Express.js based on Asynchronous Local Storage.",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Dalet OSS",
|
|
7
|
+
"url": "https://github.com/dalet-oss"
|
|
8
|
+
},
|
|
9
|
+
"contributors": [
|
|
10
|
+
{
|
|
11
|
+
"name": "Oliver Lockwood",
|
|
12
|
+
"url": "https://github.com/oliverlockwood"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"name": "Alberto Varela",
|
|
16
|
+
"email": "hello@albertovarela.net",
|
|
17
|
+
"url": "https://www.albertovarela.net"
|
|
18
|
+
}
|
|
19
|
+
],
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/dalet-oss/express-http-context.git"
|
|
23
|
+
},
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/dalet-oss/express-http-context/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/dalet-oss/express-http-context",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"main": "lib/cjs/index.js",
|
|
30
|
+
"module": "lib/esm/index.js",
|
|
31
|
+
"typings": "lib/types/index.d.ts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./lib/types/index.d.ts",
|
|
35
|
+
"import": "./lib/esm/index.js",
|
|
36
|
+
"require": "./lib/cjs/index.js",
|
|
37
|
+
"default": "./lib/cjs/index.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=14.0.0"
|
|
42
|
+
},
|
|
43
|
+
"files": [
|
|
44
|
+
"lib",
|
|
45
|
+
"src",
|
|
46
|
+
"CHANGELOG.md",
|
|
47
|
+
"LICENSE"
|
|
48
|
+
],
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "yarn clean && yarn compile",
|
|
51
|
+
"clean": "rm -rf lib/ .nyc_output/ coverage/",
|
|
52
|
+
"compile": "concurrently \"yarn compile:cjs\" \"yarn compile:esm\" \"yarn compile:types\" && ./scripts/fixup.sh",
|
|
53
|
+
"compile:cjs": "tsc -p tsconfig.cjs.json",
|
|
54
|
+
"compile:esm": "tsc -p tsconfig.esm.json",
|
|
55
|
+
"compile:types": "tsc -p tsconfig.types.json",
|
|
56
|
+
"coverage:html": "tap --coverage-report=html",
|
|
57
|
+
"coverage:cobertura": "tap --coverage-report=cobertura",
|
|
58
|
+
"hook:commit-msg": "commitlint -e",
|
|
59
|
+
"hook:pre-commit": "yarn qa",
|
|
60
|
+
"lint": "eslint .",
|
|
61
|
+
"prepack": "yarn qa && yarn build",
|
|
62
|
+
"qa": "yarn lint && yarn type-check && yarn test",
|
|
63
|
+
"test": "yarn clean && TS_NODE_PROJECT=tsconfig.json tap --node-arg=--require=ts-node/register",
|
|
64
|
+
"test:watch": "TS_NODE_PROJECT=tsconfig.json tap --node-arg=--require=ts-node/register --watch",
|
|
65
|
+
"type-check": "tsc -p tsconfig.json --noEmit"
|
|
66
|
+
},
|
|
67
|
+
"keywords": [
|
|
68
|
+
"express",
|
|
69
|
+
"middleware",
|
|
70
|
+
"plugin",
|
|
71
|
+
"request",
|
|
72
|
+
"context",
|
|
73
|
+
"http-context",
|
|
74
|
+
"request-context",
|
|
75
|
+
"express-http-context",
|
|
76
|
+
"express-request-context",
|
|
77
|
+
"asynchronouslocalstorage",
|
|
78
|
+
"asynchronous-local-storage"
|
|
79
|
+
],
|
|
80
|
+
"devDependencies": {
|
|
81
|
+
"@commitlint/cli": "^17.8.1",
|
|
82
|
+
"@commitlint/config-conventional": "^17.8.1",
|
|
83
|
+
"@types/express": "^4.17.21",
|
|
84
|
+
"@types/node": "^18.19.84",
|
|
85
|
+
"@types/supertest": "^2.0.16",
|
|
86
|
+
"@types/tap": "^15.0.12",
|
|
87
|
+
"@typescript-eslint/eslint-plugin": "^5.62.0",
|
|
88
|
+
"@typescript-eslint/parser": "^5.62.0",
|
|
89
|
+
"concurrently": "^7.6.0",
|
|
90
|
+
"eslint": "^8.35.0",
|
|
91
|
+
"eslint-config-prettier": "^8.10.0",
|
|
92
|
+
"eslint-plugin-import": "^2.31.0",
|
|
93
|
+
"express": "^4.21.2",
|
|
94
|
+
"nanoid": "3.3.4",
|
|
95
|
+
"prettier": "^2.8.8",
|
|
96
|
+
"supertest": "^6.3.4",
|
|
97
|
+
"tap": "^16.3.10",
|
|
98
|
+
"ts-node": "^10.9.2",
|
|
99
|
+
"typescript": "^4.9.5"
|
|
100
|
+
}
|
|
57
101
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'async_hooks'
|
|
2
|
+
import type { NextFunction, RequestHandler, Request, Response } from 'express'
|
|
3
|
+
|
|
4
|
+
declare global {
|
|
5
|
+
// eslint-disable-next-line no-var
|
|
6
|
+
var expressHttpContextStorage: AsyncLocalStorage<Map<string, any>>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
if (!global.expressHttpContextStorage) {
|
|
11
|
+
global.expressHttpContextStorage = new AsyncLocalStorage<Map<string, any>>()
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Express.js middleware that is responsible for initializing the context for each request.
|
|
18
|
+
*/
|
|
19
|
+
export const middleware: RequestHandler = (
|
|
20
|
+
_req: Request,
|
|
21
|
+
_res: Response,
|
|
22
|
+
next: NextFunction
|
|
23
|
+
): void => {
|
|
24
|
+
global.expressHttpContextStorage.run(new Map(), () => next())
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Gets a value from the context by key.
|
|
29
|
+
* Will return undefined if the context has not yet been initialized for this request
|
|
30
|
+
* or if a value is not found for the specified key.
|
|
31
|
+
*/
|
|
32
|
+
export function get<T = any>(key: string): T | undefined {
|
|
33
|
+
|
|
34
|
+
const store = global.expressHttpContextStorage.getStore();
|
|
35
|
+
|
|
36
|
+
return store?.get(key)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Adds a value to the context by key.
|
|
41
|
+
* If the key already exists, its value will be overwritten.
|
|
42
|
+
* No value will persist if the context has not yet been initialized.
|
|
43
|
+
*/
|
|
44
|
+
export function set<T = any>(key: string, value: T): void {
|
|
45
|
+
|
|
46
|
+
const store = global.expressHttpContextStorage.getStore();
|
|
47
|
+
|
|
48
|
+
store?.set(key, value)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export default { get, middleware, set }
|
package/browser.js
DELETED
package/index.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { Request, Response, NextFunction } from "express";
|
|
2
|
-
import { Namespace } from 'cls-hooked';
|
|
3
|
-
|
|
4
|
-
/** Express.js middleware that is responsible for initializing the context for each request. */
|
|
5
|
-
export declare function middleware(
|
|
6
|
-
req: Request,
|
|
7
|
-
res: Response,
|
|
8
|
-
next: NextFunction
|
|
9
|
-
): void;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Gets a value from the context by key. Will return undefined if the context has not yet been initialized for this request or if a value is not found for the specified key.
|
|
13
|
-
*/
|
|
14
|
-
export declare function get(key: string): any;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Adds a value to the context by key. If the key already exists, its value will be overwritten. No value will persist if the context has not yet been initialized.
|
|
18
|
-
*/
|
|
19
|
-
export declare function set(key: string, value: any): void;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Gets the underlying continuation namespace.
|
|
23
|
-
*/
|
|
24
|
-
export declare const ns: Namespace;
|
package/index.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const cls = require('cls-hooked');
|
|
4
|
-
|
|
5
|
-
const nsid = 'a6a29a6f-6747-4b5f-b99f-07ee96e32f88';
|
|
6
|
-
const ns = cls.createNamespace(nsid);
|
|
7
|
-
|
|
8
|
-
/** Express.js middleware that is responsible for initializing the context for each request. */
|
|
9
|
-
function middleware(req, res, next) {
|
|
10
|
-
ns.run(() => next());
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Gets a value from the context by key. Will return undefined if the context has not yet been initialized for this request or if a value is not found for the specified key.
|
|
15
|
-
* @param {string} key
|
|
16
|
-
*/
|
|
17
|
-
function get(key) {
|
|
18
|
-
if (ns && ns.active) {
|
|
19
|
-
return ns.get(key);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Adds a value to the context by key. If the key already exists, its value will be overwritten. No value will persist if the context has not yet been initialized.
|
|
25
|
-
* @param {string} key
|
|
26
|
-
* @param {*} value
|
|
27
|
-
*/
|
|
28
|
-
function set(key, value) {
|
|
29
|
-
if (ns && ns.active) {
|
|
30
|
-
return ns.set(key, value);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
module.exports = {
|
|
35
|
-
middleware,
|
|
36
|
-
get: get,
|
|
37
|
-
set: set,
|
|
38
|
-
ns: ns
|
|
39
|
-
};
|