@dalet-oss/express-http-context 1.2.0 → 1.2.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/LICENSE +1 -1
- package/README.md +122 -58
- package/package.json +1 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,86 +1,150 @@
|
|
|
1
|
-
[](https://www.npmjs.com/package/@dalet-oss/express-http-context)
|
|
4
|
-
[](https://www.npmjs.com/package/@dalet-oss/express-http-context)
|
|
5
|
-
[](https://david-dm.org/dalet-oss/express-http-context)
|
|
1
|
+
[](https://www.npmjs.com/package/@dalet-oss/express-http-context)
|
|
2
|
+
[](https://github.com/dalet-oss/express-http-context/actions)
|
|
6
3
|
|
|
7
|
-
# Express HTTP Context
|
|
8
|
-
Get and set request-scoped context anywhere. This is just an unopinionated, idiomatic ExpressJS implementation of
|
|
9
|
-
[cls-hooked](https://github.com/Jeff-Lewis/cls-hooked) (forked from [continuation-local-storage](https://www.npmjs.com/package/continuation-local-storage)).
|
|
10
|
-
It's a great place to store user state, claims from a JWT, request/correlation IDs, and any other request-scoped data.
|
|
11
|
-
Context is preserved even over async/await (in node 8+).
|
|
12
4
|
|
|
13
|
-
|
|
5
|
+
# Express HTTP Context
|
|
6
|
+
Modern request-scoped storage support for Express.js, based on native Node.js
|
|
7
|
+
Asynchronous Local Storage. It's a great place to store user state, claims from
|
|
8
|
+
a JWT, request/correlation IDs, and any other request-scoped data.
|
|
9
|
+
|
|
10
|
+
This package is a drop-in replacement and has been inspired by the work done in
|
|
11
|
+
[express-http-context](https://github.com/skonves/express-http-context), but
|
|
12
|
+
with ESM support, no dependencies, and only supporting modern versions of
|
|
13
|
+
Node.js (+v14).
|
|
14
|
+
|
|
15
|
+
The implementation is derived from the start made in
|
|
16
|
+
[express-http-context2](https://github.com/artberri/express-http-context2).
|
|
17
|
+
We diverged to support our own implementation because the bug highlighted in
|
|
18
|
+
[artberri/express-http-context2#1](https://github.com/artberri/express-http-context2/issues/1)
|
|
19
|
+
was ultimately ignored and the repository apparently abandoned by its maintainer.
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @dalet-oss/express-http-context
|
|
26
|
+
# or
|
|
27
|
+
yarn add @dalet-oss/express-http-context
|
|
28
|
+
# or
|
|
29
|
+
pnpm add @dalet-oss/express-http-context
|
|
30
|
+
```
|
|
14
31
|
|
|
15
|
-
|
|
16
|
-
|
|
32
|
+
> **Requirements**
|
|
33
|
+
>
|
|
34
|
+
> `@dalet-oss/express-http-context` is a middleware intended for `express`, so
|
|
35
|
+
> although it is not explicitly declared as a dependency or peer dependency, it
|
|
36
|
+
> requires `express` to work, as well as `@types/express` if you are using
|
|
37
|
+
> Typescript. The reason the dependency is not explicitly declared is that it
|
|
38
|
+
> could also eventually be used with `fastify` or other Node.js HTTP servers.
|
|
17
39
|
|
|
18
|
-
|
|
19
|
-
You won't have access to the context in any middleware "used" before this one.
|
|
40
|
+
## Configuration
|
|
20
41
|
|
|
21
|
-
|
|
22
|
-
To workaround such issues, you are advised to use any third party middleware that does NOT need the context
|
|
23
|
-
BEFORE you use this middleware.
|
|
42
|
+
Use the middleware immediately before the first middleware that needs to have access to the context. You won't have access to the context in any middleware "used" before this one.
|
|
24
43
|
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
|
|
44
|
+
```js
|
|
45
|
+
const express = require('express')
|
|
46
|
+
const httpContext = require('@dalet-oss/express-http-context')
|
|
28
47
|
|
|
29
|
-
|
|
30
|
-
// Use any third party middleware that does not need access
|
|
48
|
+
const app = express()
|
|
49
|
+
// Use any third party middleware that does not need to access the context here, e.g.
|
|
31
50
|
// app.use(some3rdParty.middleware);
|
|
32
|
-
app.use(httpContext.middleware)
|
|
33
|
-
//
|
|
51
|
+
app.use(httpContext.middleware)
|
|
52
|
+
// All code from this point on will have access to the per-request context
|
|
34
53
|
```
|
|
35
54
|
|
|
36
|
-
|
|
55
|
+
Note that some popular middlewares (such as `body-parser`, `express-jwt`) can
|
|
56
|
+
cause the context to be lost. To work around such problems, it is recommended
|
|
57
|
+
that you use any third-party middleware that does NOT require the context
|
|
58
|
+
BEFORE using this middleware.
|
|
59
|
+
|
|
60
|
+
## Usage
|
|
61
|
+
|
|
62
|
+
Examples of setting values:
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
const httpContext = require('@dalet-oss/express-http-context')
|
|
66
|
+
const { nanoid } = require('nanoid') // This is just an example, nanoid is not included in this lib
|
|
37
67
|
|
|
38
|
-
``` js
|
|
39
|
-
// Example authorization middleware
|
|
40
68
|
app.use((req, res, next) => {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
next();
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
});
|
|
69
|
+
// Get the user ID from wherever and save it for later use...
|
|
70
|
+
httpContext.set('userId', userId)
|
|
71
|
+
// Create a request ID to be able to trace/correlate everything that happens within the same request
|
|
72
|
+
httpContext.set('requestId', nanoid())
|
|
73
|
+
})
|
|
50
74
|
```
|
|
51
75
|
|
|
52
|
-
Get them from
|
|
76
|
+
Get them from anywhere in your code:
|
|
53
77
|
|
|
54
|
-
```
|
|
55
|
-
var httpContext = require('express-http-context')
|
|
78
|
+
```js
|
|
79
|
+
var httpContext = require('@dalet-oss/express-http-context')
|
|
56
80
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
81
|
+
function logError(error) {
|
|
82
|
+
const userId = httpContext.get('userId')
|
|
83
|
+
const requestId = httpContext.get('requestId')
|
|
84
|
+
console.error(error, { userId, requestId })
|
|
61
85
|
}
|
|
62
86
|
```
|
|
63
87
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
88
|
+
## API
|
|
89
|
+
|
|
90
|
+
### middleware
|
|
91
|
+
|
|
92
|
+
It is an Express.js middleware that is responsible for initializing the
|
|
93
|
+
independent context for each request. The `get` and `set` calls will operate on
|
|
94
|
+
a set of keys/values unique to those contexts.
|
|
95
|
+
|
|
96
|
+
#### Example
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
import { middleware } from '@dalet-oss/express-http-context'
|
|
100
|
+
|
|
101
|
+
app.use(middleware)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### set
|
|
105
|
+
|
|
106
|
+
Adds a value to the request context by key.
|
|
107
|
+
If the key already exists, its value will be overwritten.
|
|
108
|
+
No value will persist if the context has not yet been initialized.
|
|
109
|
+
|
|
110
|
+
#### Parameters
|
|
111
|
+
|
|
112
|
+
- `key` a string key to store the variable by
|
|
113
|
+
- `value` any value to store under the key for the later lookup.
|
|
114
|
+
|
|
115
|
+
#### Example
|
|
116
|
+
|
|
117
|
+
```js
|
|
118
|
+
import { set } from '@dalet-oss/express-http-context'
|
|
119
|
+
|
|
120
|
+
set('user', { id: 'overwrittenUser', email: 'foo@bar.com' })
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### get
|
|
124
|
+
|
|
125
|
+
Gets a value from the request context by key.
|
|
126
|
+
Will return `undefined` if the context has not yet been initialized for this
|
|
127
|
+
request or if a value is not found for the specified key.
|
|
128
|
+
|
|
129
|
+
#### Parameters
|
|
130
|
+
|
|
131
|
+
- `key` a string key to retrieve the stored value for
|
|
132
|
+
|
|
133
|
+
#### Example
|
|
134
|
+
|
|
135
|
+
```js
|
|
136
|
+
import { get } from '@dalet-oss/express-http-context'
|
|
137
|
+
|
|
138
|
+
const user = get('user')
|
|
67
139
|
```
|
|
68
140
|
|
|
69
|
-
##
|
|
70
|
-
To avoid weird behavior with express:
|
|
71
|
-
1. Make sure you require `express-http-context` in the first row of your app. Some popular packages use async which breaks CLS.
|
|
141
|
+
## License
|
|
72
142
|
|
|
73
|
-
|
|
74
|
-
1. Node 10.0.x - 10.3.x are not supported. V8 version 6.6 introduced a bug that breaks async_hooks during async/await. Node 10.4.x uses V8 v6.7 in which the bug is fixed. See: https://github.com/nodejs/node/issues/20274.
|
|
143
|
+
MIT License. See [LICENSE](LICENSE).
|
|
75
144
|
|
|
76
|
-
See [Issue #4](https://github.com/dalet-oss/express-http-context/issues/4) for more context. If you find any other weird behaviors, please feel free to open an issue.
|
|
77
145
|
|
|
78
146
|
## Contributors
|
|
79
147
|
* Oliver Lockwood (@oliverlockwood)
|
|
80
|
-
*
|
|
81
|
-
* Amiram Korach (@amiram)
|
|
82
|
-
* Yoni Rabinovitch (@yonirab)
|
|
83
|
-
* DontRelaX (@dontrelax)
|
|
84
|
-
* William Durand (@willdurand)
|
|
148
|
+
* Alberto Varela Sánchez (@artberri)
|
|
85
149
|
|
|
86
150
|
Interesting in contributing? Take a look at the [Contributing Guidlines](/CONTRIBUTING.md)
|