npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@g2a/request-id

v1.3.0

Published

Library providing utility functions for manipulating and generating request ids

Downloads

8

Readme

@g2a/request-id

npm

Library providing utility functions for manipulating and generating Hierarchical Request IDs.

Installation

$ npm i @g2a/request-id

Basic methods

The following methods are useful in everyday applications.

generateIncomingId()

(baseId?: string) => string;

Generates an incoming ID based on a provided base ID. If base ID is not provided or is invalid, generates it from scratch.

Use this function to generate a new ID based on one received in the request from an external service. ID generated by this function should be attached to all related log entries and should be used to create Outgoing IDs assigned to all subsequential outgoing requests.

Example of use with Express.js:

const logger = new Logger();

// Generate new incoming id for each incoming request
app.use((req, res, next) => {
  res.locals.requestId = generateIncomingId(req.get("request-id"));
  next();
});

app.get("/some-endpoint", (req, res) => {
  // Attach previously generated id to log entries
  logger.trace({
    message: "Some very interesting message",
    requestId: res.locals.requestId
  });

  res.sendStatus(204);
});

generateOutgoingId()

(id: string) => string;

Generates an outgoing ID based on a provided base ID. If base ID is not provided or is invalid, generates ID from scratch.

Use this function to generate unique ID each time you make a request to any external service.

Example of use with:

app.post("/do-something", (req, res) => {
  // See how it's generated in the example to  the`generateIncomingId()` method
  const currentRequestId = res.locals.requestId;

  request.post({
    url: "http://some-external-service/api",
    headers: {
      "Request-Id": generateOutgoingId(currentRequestId)
    }
  });
});

Advanced methods

Using the following methods requires a better understanding of the Hierarchical Request Id format. These methods could be useful if you need to do some more advanced manipulations on request IDs.

isValid()

(id: string) => boolean;

Checks if the provided ID conforms to Hierarchical Request ID standard.

isIncoming()

(id: string) => boolean;

Checks if the provided ID is an Incoming ID.

isOutgoing()

(id: string) => boolean;

Checks if the provided ID is an Outgoing ID (but not a bare Root ID).

isRoot()

(id: string) => boolean;

Checks if the provided ID is a Root ID.

generateRootId()

() => string;

Generates a new root ID.

generateOverflowedId()

(id: string) => string;

Generates an overflowed ID based on provided one. If provided id is invalid, generates it from scratch.

extractRootId()

(id: string) => string | null;

Extracts root ID from the provided hierarchical request ID. If root ID cannot be extracted (ID is invalid) returns null instead.