@dalet-oss/express-http-context 1.0.0-DEV → 1.2.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/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.
@@ -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"}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -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,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -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,102 @@
1
1
  {
2
- "name": "@dalet-oss/express-http-context",
3
- "version": "1.0.0-DEV",
4
- "description": "Get and set request-scoped context anywhere",
5
- "main": "index.js",
6
- "browser": "browser.js",
7
- "types": "./index.d.ts",
8
- "engines": {
9
- "node": ">=16.0.0"
10
- },
11
- "scripts": {
12
- "test": "node --unhandled-rejections=strict ./node_modules/.bin/mocha --config test/.mocharc.json",
13
- "cover": "nyc --reporter lcov --reporter text-summary _mocha --config test/.mocharc.json",
14
- "coveralls": "npm run cover -- --report lcovonly && cat ./coverage/lcov.info | coveralls"
15
- },
16
- "repository": {
17
- "type": "git",
18
- "url": "git+https://github.com/dalet-oss/express-http-context.git"
19
- },
20
- "keywords": [
21
- "express",
22
- "http",
23
- "request",
24
- "context"
25
- ],
26
- "author": "Oliver Lockwood",
27
- "contributors": [
28
- "Oliver Lockwood (@oliverlockwood)",
29
- "Steve Konves (@skonves)",
30
- "Amiram Korach (@amiram)",
31
- "Yoni Rabinovitch (@yonirab)",
32
- "DontRelaX (@dontrelax)",
33
- "William Durand (@willdurand)"
34
- ],
35
- "license": "MIT",
36
- "bugs": {
37
- "url": "https://github.com/dalet-oss/express-http-context/issues"
38
- },
39
- "homepage": "https://github.com/dalet-oss/express-http-context#readme",
40
- "directories": {
41
- "test": "test"
42
- },
43
- "dependencies": {
44
- "@types/cls-hooked": "^4.2.1",
45
- "@types/express": "^4.17.17",
46
- "cls-hooked": "^4.2.2"
47
- },
48
- "devDependencies": {
49
- "chai": "^4.3.7",
50
- "coveralls": "^3.1.1",
51
- "express": "^4.18.2",
52
- "nyc": "^15.1.0",
53
- "mocha": "^10.2.0",
54
- "mocha-lcov-reporter": "^1.3.0",
55
- "supertest": "^6.3.3"
56
- }
2
+ "name": "@dalet-oss/express-http-context",
3
+ "version": "1.2.0",
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
+ "@oliverlockwood/express-http-context-intermediate-library": "0.0.4",
84
+ "@types/express": "^4.17.21",
85
+ "@types/node": "^18.19.84",
86
+ "@types/supertest": "^2.0.16",
87
+ "@types/tap": "^15.0.12",
88
+ "@typescript-eslint/eslint-plugin": "^5.62.0",
89
+ "@typescript-eslint/parser": "^5.62.0",
90
+ "concurrently": "^7.6.0",
91
+ "eslint": "^8.35.0",
92
+ "eslint-config-prettier": "^8.10.0",
93
+ "eslint-plugin-import": "^2.31.0",
94
+ "express": "^4.21.2",
95
+ "nanoid": "3.3.4",
96
+ "prettier": "^2.8.8",
97
+ "supertest": "^6.3.4",
98
+ "tap": "^16.3.10",
99
+ "ts-node": "^10.9.2",
100
+ "typescript": "^4.9.5"
101
+ }
57
102
  }
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
@@ -1,12 +0,0 @@
1
- module.exports = {
2
- middleware: function (req, res, next) {
3
- throw new Error('`middleware` cannot be called from the browser code.');
4
- },
5
- get: function () {
6
- return null;
7
- },
8
- set: function (key, value) {
9
- // noop
10
- },
11
- ns: null,
12
- };
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
- };