Skip to content

server.js: two inline require()s in pino-http config — fire on every request instead of at module load #201

@CryptoJones

Description

@CryptoJones

Problem

In server.js's pinoHttp({ ... }) block, two require() calls live inside per-request callbacks:

genReqId: (req, res) => {
    ...
    : require('crypto').randomUUID();
    ...
},
serializers: {
    req: (req) => ({
        ...
        url: require('./app/middleware/redact-url.js').redactUrl(req.url),
        ...
    }),
},

Node caches require() results so the cost is just a hashmap lookup — not a real perf hit — but every other import in server.js is hoisted to the top-of-file block, and these two stick out as inline. They belong with the rest of the imports.

Fix

Hoist both to the module-top block. Switch the crypto import to node:crypto (preferred explicit form for built-ins).

Acceptance

  • crypto and redactUrl imported at the top of server.js
  • No require() calls inside the pinoHttp config
  • All 649 tests still pass

Proudly Made in Nebraska. Go Big Red! 🌽 https://xkcd.com/2347/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions