UNPKG

@nestjs/core

Version:

Nest - modern, fast, powerful node.js web framework (@core)

45 lines (44 loc) 1.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContextIdFactory = exports.createContextId = void 0; const request_constants_1 = require("../router/request/request-constants"); function createContextId() { /** * We are generating random identifier to track asynchronous * execution context. An identifier does not have to be neither unique * nor unpredictable because WeakMap uses objects as keys (reference comparison). * Thus, even though identifier number might be equal, WeakMap would properly * associate asynchronous context with its internal map values using object reference. * Object is automatically removed once request has been processed (closure). */ return { id: Math.random() }; } exports.createContextId = createContextId; class ContextIdFactory { /** * Generates a context identifier based on the request object. */ static create() { return createContextId(); } /** * Generates a random identifier to track asynchronous execution context. * @param request request object */ static getByRequest(request, propsToInspect = ['raw']) { var _a; if (!request) { return ContextIdFactory.create(); } if (request[request_constants_1.REQUEST_CONTEXT_ID]) { return request[request_constants_1.REQUEST_CONTEXT_ID]; } for (const key of propsToInspect) { if ((_a = request[key]) === null || _a === void 0 ? void 0 : _a[request_constants_1.REQUEST_CONTEXT_ID]) { return request[key][request_constants_1.REQUEST_CONTEXT_ID]; } } return ContextIdFactory.create(); } } exports.ContextIdFactory = ContextIdFactory;