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

correlation-id

v5.2.0

Published

Correlation id for node.js

Downloads

193,707

Readme

Correlation id

Correlation id maintains a consistent id across asynchronous calls in node.js applications; this is especially useful for logging purposes.

For example within an API, each incoming request can be assigned an id that will be available in all function calls made processing that request, so we can see which requests caused errors.

100% test coverage, no prod dependencies.

Installation

yarn:

yarn add correlation-id

npm:

npm i correlation-id --save

Compatibility

From v5 onwards this library requires node >=14. For older node versions use v3.x or v4.x.

Simple example

As demonstrated by this example, all calls to getId() within the same withId() block will return the same id. The id can be supplied, otherwise a v4 uuid will be generated.

const correlator = require("correlation-id");

function printCurrentId(name) {
  console.log("%s id: %s", name, correlator.getId());
}

correlator.withId(() => {
  setTimeout(() => {
    printCurrentId("withId block 1, call 1");
  });
  setTimeout(() => {
    printCurrentId("withId block 1, call 2");
  }, 1000);
});

correlator.withId("my-custom-id", () => {
  setTimeout(() => {
    printCurrentId("withId block 2, call 1");
  }, 500);
});

// Output:
// withId block 1, call 1 id: 5816e2d3-6b90-43be-8738-f6e1b2654f39
// withId block 2, call 1 id: my-custom-id
// withId block 1, call 2 id: 5816e2d3-6b90-43be-8738-f6e1b2654f39

API

withId([id,] work)

Executes function work within a correlation scope and returns any result returned from work. Within work and any other function executions (sync or async) calls to getId() will return the same id. The id for the context may be set explicitly with the optional id parameter, otherwise it will be a v4 uuid. Calls to withId() may be nested.

correlator.withId(() => {
  console.log(correlator.getId()); // Writes a uuid to stdout
});
correlator.withId("my-custom-id", () => {
  console.log(correlator.getId()); // Writes 'my-custom-id' to stdout
});

bindId([id,] work)

Returns function work bound with a correlation scope. When work is executed all calls to getId() will return the same id. The id for the context may be set explicitly with the optional id parameter, otherwise it will be a v4 uuid. Arguments passed to the bound function will be applied to work.

const boundFunction = correlator.bindId((p1) => {
  console.log("p1 is", p1);
  console.log(correlator.getId());
});
boundFunction("foo"); // Writes 'p1 is foo' and then a uuid to stdout

const boundFunction2 = correlator.bindId("my-custom-id", (p1) => {
  console.log("p1 is", p1);
  console.log(correlator.getId());
});
boundFunction2("foo"); // Writes 'p1 is foo' and then 'my-custom-id' to stdout

getId()

Returns the id for the current correlation scope (created via withId or bindId). If called outside of a correlation scope returns undefined.

correlator.getId(); // Returns the current id or undefined

setId(id)

Sets the id for the current correlation scope (created via withId or bindId). If called outside of a correlation scope throws an error.

correlator.setId("my-correlation-id");

Typescript

Type defs are included.

License

MIT