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

hpropagate

v1.0.1

Published

[![NPM](https://nodei.co/npm/hpropagate.png?compact=true)](https://nodei.co/npm/hpropagate/)

Downloads

11,868

Readme

NPM

hpropagate CircleCI

This package automatically propagates HTTP headers from inbound to outbound HTTP requests.

The why

We use a micro-service architecture with a growing number of HTTP endpoints. We want to propagate certain HTTP headers received from the incoming HTTP requests to all subsequent outbound HTTP requests without the need for our engineers to do it programmatically in each services:

By default, the following headers are automatically propagated:

  1. x-correlation-id. If the header is missing from the inbound request, it will be created with a UUID as value.
  2. x-variant-id to allow us to deploy multiple versions of the same services at the same time.
  3. x-feature-flags to allow us to dynamically turn on feature flags.
  4. x-request-id for tracing
  5. x-b3-traceid for tracing
  6. x-b3-spanid for tracing
  7. x-b3-parentspanid for tracing
  8. x-b3-sampled for tracing
  9. x-b3-flags for tracing
  10. x-ot-span-context for tracing

Apart from x-correlation-id, only headers received on the incoming request will be propagated to outbound calls.

The list of headers can be overriden and the initialisation of x-correlation-id disabled, see below

Getting started

To use the default configuration:

// should have this line as early as possible in your code
// it must be before loading express and request
const hpropagate = require("hpropagate");

// then start it
hpropagate();

Or do it in one go:

require("hpropagate")();

Override Defaults

  • to disable the initialisation and generation of the correlation id header:
hpropagate({
  setAndPropagateCorrelationId: false,
});
  • to override the list of headers to propagate:
hpropagate({
  headersToPropagate: ["x-my-header", "x-another-header"],
});

You can also combine those, for example to disable the initialisation of the correlation id and only propagate it:

hpropagate({
  setAndPropagateCorrelationId: false,
  headersToPropagate: ["x-correlation-id"],
});
  • to enable the propagation of the headers in the response (to allow more traceability):
hpropagate({
  propagateInResponses: true,
});

The How

Inspiration from this talk (Slides and Code) and this module

The first goal is to be able to propagate certain headers (i.e. X-Correlation-ID) to outbound HTTP requests without the need to do it programmatically in the service.

It works by using a global tracer object which keeps a records of traces (a trace object per http request). The header value is saved in the trace object associated with the current request. The http core code is wrapped to record headers on the trace (on the request listener of the http server set with http.createServer) and inject headers to the outbound requests (currently only on http.request).

Node's async_hooks module (new in Node 8) is used to set/reset tracer.currentTrace to the trace relevant to the current execution context. tracer.currentTrace is used in the wrapped functions to record/access the headers data.

Limitations

  • Only tested with Express 4
  • Need Node >= 8
  • Many more....