1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function reqIdGenFactory (requestIdHeader, optGenReqId) {
|
15 | const genReqId = optGenReqId || buildDefaultGenReqId()
|
16 |
|
17 | if (requestIdHeader) {
|
18 | return buildOptionalHeaderReqId(requestIdHeader, genReqId)
|
19 | }
|
20 |
|
21 | return genReqId
|
22 | }
|
23 |
|
24 | function getGenReqId (contextServer, req) {
|
25 | return contextServer.genReqId(req)
|
26 | }
|
27 |
|
28 | function buildDefaultGenReqId () {
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | const maxInt = 2147483647
|
35 |
|
36 | let nextReqId = 0
|
37 | return function defaultGenReqId () {
|
38 | nextReqId = (nextReqId + 1) & maxInt
|
39 | return `req-${nextReqId.toString(36)}`
|
40 | }
|
41 | }
|
42 |
|
43 | function buildOptionalHeaderReqId (requestIdHeader, genReqId) {
|
44 | return function (req) {
|
45 | return req.headers[requestIdHeader] || genReqId(req)
|
46 | }
|
47 | }
|
48 |
|
49 | module.exports = {
|
50 | getGenReqId,
|
51 | reqIdGenFactory
|
52 | }
|