Home / File/ req-id-gen-factory.js — fastify Source File

req-id-gen-factory.js — fastify Source File

Architecture documentation for req-id-gen-factory.js, a javascript file in the fastify codebase.

Entity Profile

Relationship Graph

Source Code

'use strict'

/**
 * @callback GenerateRequestId
 * @param {Object} req
 * @returns {string}
 */

/**
 * @param {string} [requestIdHeader]
 * @param {GenerateRequestId} [optGenReqId]
 * @returns {GenerateRequestId}
 */
function reqIdGenFactory (requestIdHeader, optGenReqId) {
  const genReqId = optGenReqId || buildDefaultGenReqId()

  if (requestIdHeader) {
    return buildOptionalHeaderReqId(requestIdHeader, genReqId)
  }

  return genReqId
}

function getGenReqId (contextServer, req) {
  return contextServer.genReqId(req)
}

function buildDefaultGenReqId () {
  // 2,147,483,647 (2^31 − 1) stands for max SMI value (an internal optimization of V8).
  // With this upper bound, if you'll be generating 1k ids/sec, you're going to hit it in ~25 days.
  // This is very likely to happen in real-world applications, hence the limit is enforced.
  // Growing beyond this value will make the id generation slower and cause a deopt.
  // In the worst cases, it will become a float, losing accuracy.
  const maxInt = 2147483647

  let nextReqId = 0
  return function defaultGenReqId () {
    nextReqId = (nextReqId + 1) & maxInt
    return `req-${nextReqId.toString(36)}`
  }
}

function buildOptionalHeaderReqId (requestIdHeader, genReqId) {
  return function (req) {
    return req.headers[requestIdHeader] || genReqId(req)
  }
}

module.exports = {
  getGenReqId,
  reqIdGenFactory
}

Domain

Subdomains

Frequently Asked Questions

What does req-id-gen-factory.js do?
req-id-gen-factory.js is a source file in the fastify codebase, written in javascript. It belongs to the CoreKernel domain, InstanceFactory subdomain.
What functions are defined in req-id-gen-factory.js?
req-id-gen-factory.js defines 4 function(s): buildDefaultGenReqId, buildOptionalHeaderReqId, getGenReqId, reqIdGenFactory.
Where is req-id-gen-factory.js in the architecture?
req-id-gen-factory.js is located at lib/req-id-gen-factory.js (domain: CoreKernel, subdomain: InstanceFactory, directory: lib).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free