Skip to content

Commit 877dcd0

Browse files
committed
fix(mdc): pass context through async/await call stack
closes #2
1 parent 4d69bf9 commit 877dcd0

File tree

7 files changed

+239
-185
lines changed

7 files changed

+239
-185
lines changed

flow-typed/cls-hooked.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
declare module 'cls-hooked' {
2+
declare type IAny = any
3+
4+
declare export type INamespace = {
5+
run: (fn: Function) => void,
6+
set: (name: string, data: IAny) => void,
7+
get: (name: string) => IAny,
8+
bindEmitter: (fn: Function) => void
9+
}
10+
11+
declare module.exports: {
12+
createNamespace: (name: string) => INamespace,
13+
getNamespace: (name: string) => INamespace
14+
}
15+
}

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,35 @@
3131
"author": "Anton Golub <a.golub@qiwi.com>",
3232
"license": "MIT",
3333
"devDependencies": {
34-
"@babel/cli": "^7.1.2",
35-
"@babel/core": "^7.1.2",
34+
"@babel/cli": "^7.1.5",
35+
"@babel/core": "^7.1.5",
3636
"@babel/plugin-proposal-class-properties": "^7.1.0",
3737
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
3838
"@babel/plugin-transform-modules-commonjs": "^7.1.0",
3939
"@babel/plugin-transform-runtime": "^7.1.0",
40-
"@babel/preset-env": "^7.1.0",
40+
"@babel/preset-env": "^7.1.5",
4141
"@babel/preset-flow": "^7.0.0",
4242
"@babel/register": "^7.0.0",
4343
"@semantic-release/changelog": "^3.0.1",
4444
"@semantic-release/commit-analyzer": "^6.1.0",
4545
"@semantic-release/git": "7.0.5",
4646
"@semantic-release/github": "5.2.1",
47-
"@semantic-release/npm": "5.0.5",
48-
"@semantic-release/release-notes-generator": "7.1.3",
47+
"@semantic-release/npm": "5.0.6",
48+
"@semantic-release/release-notes-generator": "7.1.4",
4949
"babel-core": "^7.0.0-bridge.0",
5050
"babel-eslint": "^10.0.1",
5151
"babel-jest": "^23.6.0",
5252
"coveralls": "^3.0.2",
5353
"docma": "^2.1.0",
5454
"eslint": "^5.8.0",
5555
"eslint-plugin-flowtype": "^3.2.0",
56-
"flow-bin": "^0.85.0",
56+
"flow-bin": "^0.86.0",
5757
"flow-coverage-report": "^0.6.0",
5858
"flow-remove-types": "^1.2.3",
5959
"flow-typed": "^2.5.1",
6060
"jest": "^23.6.0",
6161
"lerna": "^3.4.3",
62-
"semantic-release": "15.10.7",
62+
"semantic-release": "15.10.8",
6363
"semantic-release-monorepo": "6.1.1",
6464
"semantic-release-monorepo-hooks": "2.6.2",
6565
"semantic-release-plugin-decorators": "^2.0.0",

packages/mware-mdc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"homepage": "https://github.com/qiwi/mware#readme",
3030
"dependencies": {
31-
"continuation-local-storage": "^3.2.1"
31+
"cls-hooked":"^4.2.2"
3232
},
3333
"devDependencies": {
3434
"lodash": "^4.17.11",

packages/mware-mdc/src/Mdc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// @flow
22

33
import crypto from 'crypto'
4-
import {createNamespace, getNamespace} from 'continuation-local-storage'
5-
import type {INamespace} from 'continuation-local-storage'
4+
import {createNamespace, getNamespace} from 'cls-hooked'
5+
import type {INamespace} from 'cls-hooked'
66
import type {
77
IAny,
88
IRequest,

packages/mware-mdc/test/index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import factory from '../src'
22
import reqresnext from 'reqresnext'
33

44
describe('mware-mdc', () => {
5-
65
beforeEach(() => {
76
jest.resetAllMocks()
87
})
@@ -23,16 +22,26 @@ describe('mware-mdc', () => {
2322
expect(next).toHaveBeenCalled()
2423
})
2524

26-
it('attaches req.trace field', () => {
25+
it('attaches req.trace field and passes through inner async context', done => {
2726
const mware = factory({})
28-
const {req, res} = reqresnext(null, null)
29-
30-
mware(req, res, () => {
27+
const {req, res} = reqresnext()
28+
const delayed = (req) => new Promise(resolve => setTimeout(() => resolve(req.trace), 200))
29+
const inner = () => {
3130
expect(req.trace).toMatchObject({
3231
trace_id: expect.stringMatching(/^[0-9a-f]{16}$/),
3332
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
3433
})
35-
})
34+
35+
delayed(req)
36+
.then(trace => {
37+
expect(trace).toMatchObject({
38+
trace_id: expect.stringMatching(/^[0-9a-f]{16}$/),
39+
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
40+
})
41+
})
42+
.then(() => done())
43+
}
44+
mware(req, res, inner)
3645

3746
expect(req.trace).toBeUndefined()
3847
})

packages/mware/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ npm i @qiwi/mware
1010
### Middlewares
1111
* [mware-mdc](../mware-mdc/README.md)
1212
* [mware-logger](../mware-logger/README.md)
13+
* [mware-crumbs](../mware-crumbs/README.md)
14+
* [mware-cors](../mware-cors/README.md)
1315

1416
### Usage
1517

@@ -22,4 +24,23 @@ const app = express()
2224
app.use(cors())
2325
app.use(mdc())
2426
app.listen(...)
27+
```
28+
29+
### Utils
30+
```
31+
import express from 'express'
32+
import {util} from '@qiwi/mware'
33+
34+
const {asyncMiddleware} = util
35+
...
36+
37+
app.get('/', asyncMiddleware(async (req, res, next) => {
38+
...
39+
throw new Error('Something went wrong')
40+
}))
41+
42+
app.use((error, req, res, next) => {
43+
error // Error('Something went wrong')
44+
})
45+
2546
```

0 commit comments

Comments
 (0)