@villedemontreal/correlation-id 5.3.5 → 5.3.7
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/dist/config/configs.d.ts +16 -0
- package/dist/config/configs.js +15 -0
- package/dist/config/configs.js.map +1 -0
- package/dist/scripts/index.d.ts +2 -0
- package/dist/scripts/index.js +14 -0
- package/dist/scripts/index.js.map +1 -0
- package/dist/scripts/lint.d.ts +6 -0
- package/dist/scripts/lint.js +31 -0
- package/dist/scripts/lint.js.map +1 -0
- package/dist/scripts/lintFix.d.ts +6 -0
- package/dist/scripts/lintFix.js +40 -0
- package/dist/scripts/lintFix.js.map +1 -0
- package/dist/src/config/configs.d.ts +1 -1
- package/dist/src/config/configs.js.map +1 -1
- package/dist/src/config/constants.d.ts +1 -1
- package/dist/src/config/constants.js +1 -1
- package/dist/src/config/constants.js.map +1 -1
- package/dist/src/config/init.js.map +1 -1
- package/dist/src/middleware/correlationIdMiddleware.d.ts +1 -1
- package/dist/src/middleware/correlationIdMiddleware.js +1 -1
- package/dist/src/middleware/correlationIdMiddleware.js.map +1 -1
- package/dist/src/middleware/correlationIdMiddleware.test.js +112 -135
- package/dist/src/middleware/correlationIdMiddleware.test.js.map +1 -1
- package/dist/src/services/correlationIdService.d.ts +1 -1
- package/dist/src/services/correlationIdService.js +27 -16
- package/dist/src/services/correlationIdService.js.map +1 -1
- package/dist/src/services/correlationIdService.test.js +98 -79
- package/dist/src/services/correlationIdService.test.js.map +1 -1
- package/dist/src/utils/logger.js.map +1 -1
- package/package.json +10 -8
- package/src/config/configs.ts +4 -2
- package/src/config/constants.ts +2 -2
- package/src/config/init.ts +1 -1
- package/src/middleware/correlationIdMiddleware.test.ts +111 -77
- package/src/middleware/correlationIdMiddleware.ts +3 -3
- package/src/services/correlationIdService.test.ts +27 -19
- package/src/services/correlationIdService.ts +5 -7
- package/src/utils/logger.ts +8 -1
|
@@ -7,10 +7,10 @@ import { setTestingConfigurations } from '../utils/testingConfigurations';
|
|
|
7
7
|
import { createCorrelationIdMiddleware } from './correlationIdMiddleware';
|
|
8
8
|
|
|
9
9
|
function delay(millis: number): Promise<void> {
|
|
10
|
-
return new Promise<void>(resolve =>
|
|
10
|
+
return new Promise<void>((resolve) =>
|
|
11
11
|
setTimeout(() => {
|
|
12
12
|
resolve();
|
|
13
|
-
}, millis)
|
|
13
|
+
}, millis),
|
|
14
14
|
);
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -19,7 +19,7 @@ function delay(millis: number): Promise<void> {
|
|
|
19
19
|
// ==========================================
|
|
20
20
|
setTestingConfigurations();
|
|
21
21
|
|
|
22
|
-
const uuidMatcher
|
|
22
|
+
const uuidMatcher = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
23
23
|
|
|
24
24
|
// ==========================================
|
|
25
25
|
// Correlation ID service
|
|
@@ -35,30 +35,29 @@ describe('Correlation ID Middleware', () => {
|
|
|
35
35
|
res.end();
|
|
36
36
|
});
|
|
37
37
|
|
|
38
|
-
await request(app)
|
|
39
|
-
.get('/')
|
|
40
|
-
.send();
|
|
38
|
+
await request(app).get('/').send();
|
|
41
39
|
});
|
|
42
40
|
|
|
43
41
|
it('should get correlation id from incoming request', async () => {
|
|
44
|
-
const testId
|
|
42
|
+
const testId = 'correlation-id-123';
|
|
45
43
|
|
|
46
44
|
const app: express.Application = express();
|
|
47
45
|
app.use(createCorrelationIdMiddleware());
|
|
48
46
|
app.get('/', (req, res) => {
|
|
49
47
|
const actual = correlationIdService.getId();
|
|
50
|
-
assert.strictEqual(
|
|
48
|
+
assert.strictEqual(
|
|
49
|
+
actual,
|
|
50
|
+
testId,
|
|
51
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
52
|
+
);
|
|
51
53
|
res.end();
|
|
52
54
|
});
|
|
53
55
|
|
|
54
|
-
await request(app)
|
|
55
|
-
.get('/')
|
|
56
|
-
.set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId)
|
|
57
|
-
.send();
|
|
56
|
+
await request(app).get('/').set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId).send();
|
|
58
57
|
});
|
|
59
58
|
|
|
60
59
|
it('should maintain correlation id with async callbacks', async () => {
|
|
61
|
-
const testId
|
|
60
|
+
const testId = 'correlation-id-123';
|
|
62
61
|
|
|
63
62
|
let actual = '';
|
|
64
63
|
let actual2 = '';
|
|
@@ -73,18 +72,23 @@ describe('Correlation ID Middleware', () => {
|
|
|
73
72
|
});
|
|
74
73
|
|
|
75
74
|
assert.isUndefined(correlationIdService.getId());
|
|
76
|
-
await request(app)
|
|
77
|
-
.get('/')
|
|
78
|
-
.set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId)
|
|
79
|
-
.send();
|
|
75
|
+
await request(app).get('/').set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId).send();
|
|
80
76
|
|
|
81
77
|
assert.isUndefined(correlationIdService.getId());
|
|
82
|
-
assert.strictEqual(
|
|
83
|
-
|
|
78
|
+
assert.strictEqual(
|
|
79
|
+
actual,
|
|
80
|
+
testId,
|
|
81
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
82
|
+
);
|
|
83
|
+
assert.strictEqual(
|
|
84
|
+
actual2,
|
|
85
|
+
testId,
|
|
86
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
87
|
+
);
|
|
84
88
|
});
|
|
85
89
|
|
|
86
90
|
it('should maintain correlation id with async/await operations', async () => {
|
|
87
|
-
const testId
|
|
91
|
+
const testId = 'correlation-id-123';
|
|
88
92
|
|
|
89
93
|
let actual = '';
|
|
90
94
|
let actual2 = '';
|
|
@@ -103,19 +107,24 @@ describe('Correlation ID Middleware', () => {
|
|
|
103
107
|
});
|
|
104
108
|
|
|
105
109
|
assert.isUndefined(correlationIdService.getId());
|
|
106
|
-
await request(app)
|
|
107
|
-
.get('/')
|
|
108
|
-
.set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId)
|
|
109
|
-
.send();
|
|
110
|
+
await request(app).get('/').set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId).send();
|
|
110
111
|
|
|
111
112
|
assert.isUndefined(correlationIdService.getId());
|
|
112
|
-
assert.strictEqual(
|
|
113
|
-
|
|
113
|
+
assert.strictEqual(
|
|
114
|
+
actual,
|
|
115
|
+
testId,
|
|
116
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
117
|
+
);
|
|
118
|
+
assert.strictEqual(
|
|
119
|
+
actual2,
|
|
120
|
+
testId,
|
|
121
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
122
|
+
);
|
|
114
123
|
});
|
|
115
124
|
|
|
116
125
|
it('should keep correlation ids in nested requests', async () => {
|
|
117
|
-
const testId
|
|
118
|
-
const testId2
|
|
126
|
+
const testId = 'correlation-id-123';
|
|
127
|
+
const testId2 = 'correlation-id-456';
|
|
119
128
|
|
|
120
129
|
let actual = '';
|
|
121
130
|
let actual2 = '';
|
|
@@ -129,10 +138,7 @@ describe('Correlation ID Middleware', () => {
|
|
|
129
138
|
actual = correlationIdService.getId();
|
|
130
139
|
await delay(250);
|
|
131
140
|
actual2 = correlationIdService.getId();
|
|
132
|
-
await request(app)
|
|
133
|
-
.get('/foo')
|
|
134
|
-
.set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId2)
|
|
135
|
-
.send();
|
|
141
|
+
await request(app).get('/foo').set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId2).send();
|
|
136
142
|
actual3 = correlationIdService.getId();
|
|
137
143
|
} catch (e) {
|
|
138
144
|
next(e);
|
|
@@ -153,24 +159,41 @@ describe('Correlation ID Middleware', () => {
|
|
|
153
159
|
});
|
|
154
160
|
|
|
155
161
|
assert.isUndefined(correlationIdService.getId());
|
|
156
|
-
await request(app)
|
|
157
|
-
.get('/')
|
|
158
|
-
.set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId)
|
|
159
|
-
.send();
|
|
162
|
+
await request(app).get('/').set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId).send();
|
|
160
163
|
|
|
161
164
|
assert.isUndefined(correlationIdService.getId());
|
|
162
|
-
assert.strictEqual(
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
165
|
+
assert.strictEqual(
|
|
166
|
+
actual,
|
|
167
|
+
testId,
|
|
168
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
169
|
+
);
|
|
170
|
+
assert.strictEqual(
|
|
171
|
+
actual2,
|
|
172
|
+
testId,
|
|
173
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
174
|
+
);
|
|
175
|
+
assert.strictEqual(
|
|
176
|
+
actual3,
|
|
177
|
+
testId,
|
|
178
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
179
|
+
);
|
|
180
|
+
assert.strictEqual(
|
|
181
|
+
actual4,
|
|
182
|
+
testId2,
|
|
183
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
184
|
+
);
|
|
185
|
+
assert.strictEqual(
|
|
186
|
+
actual5,
|
|
187
|
+
testId2,
|
|
188
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
189
|
+
);
|
|
167
190
|
});
|
|
168
191
|
|
|
169
192
|
// tslint:disable: no-console
|
|
170
|
-
it('should keep correlation ids separated in parallel requests', async function() {
|
|
193
|
+
it('should keep correlation ids separated in parallel requests', async function () {
|
|
171
194
|
this.timeout(5000);
|
|
172
|
-
const testId
|
|
173
|
-
const testId2
|
|
195
|
+
const testId = 'correlation-id-123';
|
|
196
|
+
const testId2 = 'correlation-id-456';
|
|
174
197
|
|
|
175
198
|
let actual = '';
|
|
176
199
|
let actual2 = '';
|
|
@@ -212,10 +235,7 @@ describe('Correlation ID Middleware', () => {
|
|
|
212
235
|
|
|
213
236
|
console.log('send req1');
|
|
214
237
|
assert.isUndefined(correlationIdService.getId());
|
|
215
|
-
const req1 = request(app)
|
|
216
|
-
.get('/')
|
|
217
|
-
.set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId)
|
|
218
|
-
.send();
|
|
238
|
+
const req1 = request(app).get('/').set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId).send();
|
|
219
239
|
console.log('send req2');
|
|
220
240
|
assert.isUndefined(correlationIdService.getId());
|
|
221
241
|
const req2 = request(app)
|
|
@@ -224,14 +244,30 @@ describe('Correlation ID Middleware', () => {
|
|
|
224
244
|
.send();
|
|
225
245
|
await Promise.all([req1, req2]);
|
|
226
246
|
assert.isUndefined(correlationIdService.getId());
|
|
227
|
-
assert.strictEqual(
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
247
|
+
assert.strictEqual(
|
|
248
|
+
actual,
|
|
249
|
+
testId,
|
|
250
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
251
|
+
);
|
|
252
|
+
assert.strictEqual(
|
|
253
|
+
actual2,
|
|
254
|
+
testId,
|
|
255
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
256
|
+
);
|
|
257
|
+
assert.strictEqual(
|
|
258
|
+
actual4,
|
|
259
|
+
testId2,
|
|
260
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
261
|
+
);
|
|
262
|
+
assert.strictEqual(
|
|
263
|
+
actual5,
|
|
264
|
+
testId2,
|
|
265
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
266
|
+
);
|
|
231
267
|
});
|
|
232
268
|
|
|
233
269
|
it('should work with operations causing errors', async () => {
|
|
234
|
-
const testId
|
|
270
|
+
const testId = 'correlation-id-123';
|
|
235
271
|
|
|
236
272
|
let actual = '';
|
|
237
273
|
const app: express.Application = express();
|
|
@@ -248,11 +284,15 @@ describe('Correlation ID Middleware', () => {
|
|
|
248
284
|
.send();
|
|
249
285
|
assert.strictEqual(result.status, 500);
|
|
250
286
|
assert.isUndefined(correlationIdService.getId());
|
|
251
|
-
assert.strictEqual(
|
|
287
|
+
assert.strictEqual(
|
|
288
|
+
actual,
|
|
289
|
+
testId,
|
|
290
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
291
|
+
);
|
|
252
292
|
});
|
|
253
293
|
|
|
254
294
|
it('a filter can be specified', async () => {
|
|
255
|
-
const testId
|
|
295
|
+
const testId = 'correlation-id-123';
|
|
256
296
|
|
|
257
297
|
const filter = (req: express.Request): boolean => {
|
|
258
298
|
if (req.path.startsWith('/ok/')) {
|
|
@@ -266,12 +306,20 @@ describe('Correlation ID Middleware', () => {
|
|
|
266
306
|
|
|
267
307
|
app.get('/ok/1', (req, res) => {
|
|
268
308
|
const actual = correlationIdService.getId();
|
|
269
|
-
assert.strictEqual(
|
|
309
|
+
assert.strictEqual(
|
|
310
|
+
actual,
|
|
311
|
+
testId,
|
|
312
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
313
|
+
);
|
|
270
314
|
res.end();
|
|
271
315
|
});
|
|
272
316
|
app.get('/ok/2', (req, res) => {
|
|
273
317
|
const actual = correlationIdService.getId();
|
|
274
|
-
assert.strictEqual(
|
|
318
|
+
assert.strictEqual(
|
|
319
|
+
actual,
|
|
320
|
+
testId,
|
|
321
|
+
'getId() should return id from x-correlation-id header of inbound request',
|
|
322
|
+
);
|
|
275
323
|
res.end();
|
|
276
324
|
});
|
|
277
325
|
app.get('/notok/1', (req, res) => {
|
|
@@ -280,24 +328,15 @@ describe('Correlation ID Middleware', () => {
|
|
|
280
328
|
res.end();
|
|
281
329
|
});
|
|
282
330
|
|
|
283
|
-
await request(app)
|
|
284
|
-
.get('/ok/1')
|
|
285
|
-
.set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId)
|
|
286
|
-
.send();
|
|
331
|
+
await request(app).get('/ok/1').set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId).send();
|
|
287
332
|
|
|
288
|
-
await request(app)
|
|
289
|
-
.get('/ok/2')
|
|
290
|
-
.set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId)
|
|
291
|
-
.send();
|
|
333
|
+
await request(app).get('/ok/2').set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId).send();
|
|
292
334
|
|
|
293
|
-
await request(app)
|
|
294
|
-
.get('/notok/1')
|
|
295
|
-
.set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId)
|
|
296
|
-
.send();
|
|
335
|
+
await request(app).get('/notok/1').set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId).send();
|
|
297
336
|
});
|
|
298
337
|
|
|
299
338
|
it('getCidInfo() cid received', async () => {
|
|
300
|
-
const testId
|
|
339
|
+
const testId = 'correlation-id-123';
|
|
301
340
|
|
|
302
341
|
const app: express.Application = express();
|
|
303
342
|
app.use(createCorrelationIdMiddleware());
|
|
@@ -310,10 +349,7 @@ describe('Correlation ID Middleware', () => {
|
|
|
310
349
|
res.end();
|
|
311
350
|
});
|
|
312
351
|
|
|
313
|
-
await request(app)
|
|
314
|
-
.get('/')
|
|
315
|
-
.set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId)
|
|
316
|
-
.send();
|
|
352
|
+
await request(app).get('/').set(httpHeaderFieldsTyped.X_CORRELATION_ID, testId).send();
|
|
317
353
|
});
|
|
318
354
|
|
|
319
355
|
it('getCidInfo() cid generated', async () => {
|
|
@@ -328,8 +364,6 @@ describe('Correlation ID Middleware', () => {
|
|
|
328
364
|
res.end();
|
|
329
365
|
});
|
|
330
366
|
|
|
331
|
-
await request(app)
|
|
332
|
-
.get('/')
|
|
333
|
-
.send();
|
|
367
|
+
await request(app).get('/').send();
|
|
334
368
|
});
|
|
335
369
|
});
|
|
@@ -9,13 +9,13 @@ import { correlationIdService } from '../services/correlationIdService';
|
|
|
9
9
|
* @param filter a filter which is going to be called with the request to see if a
|
|
10
10
|
* colleration id scope must be managed or not.
|
|
11
11
|
*/
|
|
12
|
-
export
|
|
13
|
-
filter?: (req: express.Request) => boolean
|
|
12
|
+
export const createCorrelationIdMiddleware = (
|
|
13
|
+
filter?: (req: express.Request) => boolean,
|
|
14
14
|
): ((req: express.Request, res: express.Response, next: express.NextFunction) => void) => {
|
|
15
15
|
const correlationIdMiddleware = (
|
|
16
16
|
req: express.Request,
|
|
17
17
|
res: express.Response,
|
|
18
|
-
next: express.NextFunction
|
|
18
|
+
next: express.NextFunction,
|
|
19
19
|
): void => {
|
|
20
20
|
if (filter && !filter(req)) {
|
|
21
21
|
next();
|
|
@@ -7,14 +7,14 @@ import { correlationIdService } from './correlationIdService';
|
|
|
7
7
|
|
|
8
8
|
setTestingConfigurations();
|
|
9
9
|
|
|
10
|
-
const uuidMatcher
|
|
10
|
+
const uuidMatcher = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
11
11
|
|
|
12
|
-
async function timeout(work: (params?: any) => any, ms
|
|
13
|
-
return new Promise<void>(resolve =>
|
|
12
|
+
async function timeout(work: (params?: any) => any, ms = 50) {
|
|
13
|
+
return new Promise<void>((resolve) =>
|
|
14
14
|
setTimeout(() => {
|
|
15
15
|
work();
|
|
16
16
|
resolve();
|
|
17
|
-
}, ms)
|
|
17
|
+
}, ms),
|
|
18
18
|
);
|
|
19
19
|
}
|
|
20
20
|
|
|
@@ -29,7 +29,7 @@ describe('Correlation Id Service', () => {
|
|
|
29
29
|
describe('withId correlator', () => {
|
|
30
30
|
it('with sync function', () => {
|
|
31
31
|
// GIVEN
|
|
32
|
-
const expectedResult
|
|
32
|
+
const expectedResult = '⭐';
|
|
33
33
|
|
|
34
34
|
// WHEN
|
|
35
35
|
const result = correlationIdService.withId(() => {
|
|
@@ -43,7 +43,7 @@ describe('Correlation Id Service', () => {
|
|
|
43
43
|
});
|
|
44
44
|
|
|
45
45
|
it('with async function', async () => {
|
|
46
|
-
let done
|
|
46
|
+
let done = false;
|
|
47
47
|
|
|
48
48
|
const promise = correlationIdService.withId(async () => {
|
|
49
49
|
await timeout(() => {
|
|
@@ -71,7 +71,7 @@ describe('Correlation Id Service', () => {
|
|
|
71
71
|
const emitter = new EventEmitter();
|
|
72
72
|
let receivedValue = 0;
|
|
73
73
|
let receivedCid = '';
|
|
74
|
-
emitter.on('test', value => {
|
|
74
|
+
emitter.on('test', (value) => {
|
|
75
75
|
receivedValue = value;
|
|
76
76
|
receivedCid = correlationIdService.getId();
|
|
77
77
|
});
|
|
@@ -110,7 +110,7 @@ describe('Correlation Id Service', () => {
|
|
|
110
110
|
title: 'Hello-',
|
|
111
111
|
say(msg: string): string {
|
|
112
112
|
return this.title + msg + correlationIdService.getId();
|
|
113
|
-
}
|
|
113
|
+
},
|
|
114
114
|
};
|
|
115
115
|
|
|
116
116
|
// WHEN
|
|
@@ -124,13 +124,13 @@ describe('Correlation Id Service', () => {
|
|
|
124
124
|
});
|
|
125
125
|
|
|
126
126
|
it('with resolved promise', async () => {
|
|
127
|
-
let done
|
|
127
|
+
let done = false;
|
|
128
128
|
|
|
129
129
|
const promise = correlationIdService.withId(() =>
|
|
130
|
-
Promise.resolve(correlationIdService.getId()).then(id => {
|
|
130
|
+
Promise.resolve(correlationIdService.getId()).then((id) => {
|
|
131
131
|
assert.match(id, uuidMatcher, 'Promise should resolve correlation id');
|
|
132
132
|
done = true;
|
|
133
|
-
})
|
|
133
|
+
}),
|
|
134
134
|
);
|
|
135
135
|
|
|
136
136
|
// "withId" doesn't care about a promise to be floating or not
|
|
@@ -145,22 +145,30 @@ describe('Correlation Id Service', () => {
|
|
|
145
145
|
|
|
146
146
|
correlationIdService.withId(() => {
|
|
147
147
|
const cid2 = correlationIdService.getId();
|
|
148
|
-
assert.notEqual(
|
|
148
|
+
assert.notEqual(
|
|
149
|
+
cid2,
|
|
150
|
+
cid1,
|
|
151
|
+
'correlationIdService.getId() should return a different id for every scope',
|
|
152
|
+
);
|
|
149
153
|
assert.match(cid2, uuidMatcher, 'correlationIdService.getId() should return a UUID');
|
|
150
154
|
});
|
|
151
155
|
|
|
152
156
|
const cid3 = correlationIdService.getId();
|
|
153
|
-
assert.strictEqual(
|
|
157
|
+
assert.strictEqual(
|
|
158
|
+
cid3,
|
|
159
|
+
cid1,
|
|
160
|
+
'correlationIdService.getId() should return the same id for the same scope',
|
|
161
|
+
);
|
|
154
162
|
assert.match(cid3, uuidMatcher, 'correlationIdService.getId() should return a UUID');
|
|
155
163
|
});
|
|
156
164
|
});
|
|
157
165
|
|
|
158
|
-
it('with async function', async function() {
|
|
166
|
+
it('with async function', async function () {
|
|
159
167
|
const duration = 25;
|
|
160
168
|
setSlowThreshold(this, duration);
|
|
161
169
|
|
|
162
170
|
// GIVEN
|
|
163
|
-
const expectedResult
|
|
171
|
+
const expectedResult = '⭐';
|
|
164
172
|
|
|
165
173
|
// WHEN
|
|
166
174
|
const result = await correlationIdService.withId(async () => {
|
|
@@ -178,12 +186,12 @@ describe('Correlation Id Service', () => {
|
|
|
178
186
|
});
|
|
179
187
|
|
|
180
188
|
describe('withIdAsync correlator', () => {
|
|
181
|
-
it('with async function', async function() {
|
|
189
|
+
it('with async function', async function () {
|
|
182
190
|
const duration = 25;
|
|
183
191
|
setSlowThreshold(this, duration);
|
|
184
192
|
|
|
185
193
|
// GIVEN
|
|
186
|
-
const expectedResult
|
|
194
|
+
const expectedResult = '⭐';
|
|
187
195
|
|
|
188
196
|
// WHEN
|
|
189
197
|
const result = await correlationIdService.withIdAsync(async () => {
|
|
@@ -211,7 +219,7 @@ describe('Correlation Id Service', () => {
|
|
|
211
219
|
}
|
|
212
220
|
});
|
|
213
221
|
|
|
214
|
-
it('with error after await', async function() {
|
|
222
|
+
it('with error after await', async function () {
|
|
215
223
|
const duration = 25;
|
|
216
224
|
setSlowThreshold(this, duration);
|
|
217
225
|
|
|
@@ -228,7 +236,7 @@ describe('Correlation Id Service', () => {
|
|
|
228
236
|
}
|
|
229
237
|
});
|
|
230
238
|
|
|
231
|
-
it('with nested async functions', async function() {
|
|
239
|
+
it('with nested async functions', async function () {
|
|
232
240
|
const duration = 25;
|
|
233
241
|
setSlowThreshold(this, 2 * duration);
|
|
234
242
|
|
|
@@ -111,9 +111,7 @@ class CorrelationIdServiceWithClsHooked implements ICorrelationIdService {
|
|
|
111
111
|
return new Promise<T>((resolve, reject) => {
|
|
112
112
|
this.withId(() => {
|
|
113
113
|
try {
|
|
114
|
-
work()
|
|
115
|
-
.then(resolve)
|
|
116
|
-
.catch(reject);
|
|
114
|
+
work().then(resolve).catch(reject);
|
|
117
115
|
} catch (err) {
|
|
118
116
|
reject(err);
|
|
119
117
|
}
|
|
@@ -139,7 +137,7 @@ class CorrelationIdServiceWithClsHooked implements ICorrelationIdService {
|
|
|
139
137
|
return {
|
|
140
138
|
current: this.getId(),
|
|
141
139
|
receivedInRequest: req[constants.requestExtraVariables.cidReceivedInRequest],
|
|
142
|
-
generated: req[constants.requestExtraVariables.cidNew]
|
|
140
|
+
generated: req[constants.requestExtraVariables.cidNew],
|
|
143
141
|
};
|
|
144
142
|
}
|
|
145
143
|
|
|
@@ -212,7 +210,7 @@ class CorrelationIdServiceWithAsyncLocalStorage implements ICorrelationIdService
|
|
|
212
210
|
return {
|
|
213
211
|
current: this.getId(),
|
|
214
212
|
receivedInRequest: req[constants.requestExtraVariables.cidReceivedInRequest],
|
|
215
|
-
generated: req[constants.requestExtraVariables.cidNew]
|
|
213
|
+
generated: req[constants.requestExtraVariables.cidNew],
|
|
216
214
|
};
|
|
217
215
|
}
|
|
218
216
|
|
|
@@ -239,7 +237,7 @@ class CorrelationIdServiceWithAsyncLocalStorage implements ICorrelationIdService
|
|
|
239
237
|
private bindFunction<T extends Function>(target: T): T {
|
|
240
238
|
const storage = this.storage;
|
|
241
239
|
const store = this.storage.getStore();
|
|
242
|
-
return function(...args: any[]) {
|
|
240
|
+
return function (...args: any[]) {
|
|
243
241
|
storage.enterWith(store);
|
|
244
242
|
return target.call(this, ...args);
|
|
245
243
|
} as any;
|
|
@@ -250,6 +248,6 @@ function canUseAsyncLocalStorage() {
|
|
|
250
248
|
return semver.satisfies(process.versions.node, '>=13.10.0') && !!AsyncLocalStorage;
|
|
251
249
|
}
|
|
252
250
|
|
|
253
|
-
export
|
|
251
|
+
export const correlationIdService: ICorrelationIdService = canUseAsyncLocalStorage()
|
|
254
252
|
? new CorrelationIdServiceWithAsyncLocalStorage()
|
|
255
253
|
: new CorrelationIdServiceWithClsHooked();
|
package/src/utils/logger.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
ILogger,
|
|
3
|
+
initLogger,
|
|
4
|
+
LazyLogger,
|
|
5
|
+
Logger,
|
|
6
|
+
LoggerConfigs,
|
|
7
|
+
LogLevel,
|
|
8
|
+
} from '@villedemontreal/logger';
|
|
2
9
|
import { configs } from '../config/configs';
|
|
3
10
|
|
|
4
11
|
let testingLoggerLibInitialised = false;
|