@naturalcycles/backend-lib 9.85.1 → 9.86.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.
@@ -10,12 +10,11 @@ const traceIdByRequest = new WeakMap();
10
10
  * every log line.
11
11
  */
12
12
  export function getRequestTraceId(req) {
13
- let traceId = traceIdByRequest.get(req);
14
- if (traceId === undefined) {
15
- traceId = getRequestTraceContext(req)?.traceId ?? null;
16
- traceIdByRequest.set(req, traceId);
17
- }
18
- return traceId ?? undefined;
13
+ return traceIdByRequest.getOrInsertComputed(req, getRequestTraceIdOrNull) ?? undefined;
14
+ }
15
+ // Passed by reference, so the per-log-line call allocates no closure
16
+ function getRequestTraceIdOrNull(req) {
17
+ return getRequestTraceContext(req)?.traceId ?? null;
19
18
  }
20
19
  export function getRequestTraceContext(req) {
21
20
  // Reads from `req.headers` and not `req.header()`, so a bare request object (as in tests) works too
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
3
  "type": "module",
4
- "version": "9.85.1",
4
+ "version": "9.86.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/db-lib": "^10",
7
7
  "@naturalcycles/js-lib": "^15",
@@ -23,12 +23,12 @@ const traceIdByRequest = new WeakMap<BackendRequest, string | null>()
23
23
  * every log line.
24
24
  */
25
25
  export function getRequestTraceId(req: BackendRequest): string | undefined {
26
- let traceId = traceIdByRequest.get(req)
27
- if (traceId === undefined) {
28
- traceId = getRequestTraceContext(req)?.traceId ?? null
29
- traceIdByRequest.set(req, traceId)
30
- }
31
- return traceId ?? undefined
26
+ return traceIdByRequest.getOrInsertComputed(req, getRequestTraceIdOrNull) ?? undefined
27
+ }
28
+
29
+ // Passed by reference, so the per-log-line call allocates no closure
30
+ function getRequestTraceIdOrNull(req: BackendRequest): string | null {
31
+ return getRequestTraceContext(req)?.traceId ?? null
32
32
  }
33
33
 
34
34
  export function getRequestTraceContext(req: BackendRequest): TraceContext | undefined {