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 🙏

© 2026 – Pkg Stats / Ryan Hefner

xypriss-on-headers

v2.0.1

Published

Execute a listener when a response is about to write headers - Customized for XyPriss

Readme

XyPriss Header Interception — xypriss-on-headers

[!NOTE] Internalized Fork: This module is a strictly typed TypeScript port of the original on-headers library. It has been internalized into the XyPriss ecosystem to reduce external dependency surfaces and ensure architectural consistency within XyPriss framework plugins.


Overview

xypriss-on-headers lets you register one or more listeners that fire once, immediately before an HTTP response flushes its headers to the client. This is the foundation for middleware that needs to inspect or mutate headers based on the final response state — compression filters, security policy enforcers, timing injectors, etc.


Installation

xfpm install xypriss-on-headers

Usage

Basic — single listener

import http from "http";
import onHeaders from "xypriss-on-headers";

http
  .createServer((req, res) => {
    onHeaders(res, function () {
      // Runs once, just before writeHead flushes headers
      this.setHeader("X-Powered-By", "XyPriss-Engine");
    });

    res.statusCode = 200;
    res.setHeader("Content-Type", "text/plain");
    res.end("Operational");
  })
  .listen(3000);

Multiple listeners on the same response

Multiple calls on the same res are safe. Listeners are queued and executed in registration order (FIFO).

onHeaders(res, function () {
  this.setHeader("X-Request-Id", generateId());
});

onHeaders(res, function () {
  this.setHeader("X-Response-Time", `${Date.now() - start}ms`);
});

Modifying the status code inside a listener

If a listener changes res.statusCode, the new value is automatically forwarded to writeHead.

onHeaders(res, function () {
  if (!this.getHeader("Content-Type")) {
    this.statusCode = 500;
  }
});

API

onHeaders(res, listener)

| Parameter | Type | Description | | ---------- | ------------------- | ----------------------------------------------------------------------- | | res | ServerResponse | The active HTTP response to observe. | | listener | OnHeadersListener | Callback executed before headers are written. this is bound to res. |

Returns void.

Throws

  • TypeError — if res is falsy or not a ServerResponse instance.
  • TypeError — if listener is not a function.

OnHeadersListener (exported type)

type OnHeadersListener = (this: ServerResponse) => void;

Use this type when building middleware that accepts or forwards listeners:

import onHeaders, { OnHeadersListener } from "xypriss-on-headers";

function applySecurityHeaders(res: ServerResponse, extra?: OnHeadersListener) {
  onHeaders(res, function () {
    this.setHeader("X-Content-Type-Options", "nosniff");
    extra?.call(this);
  });
}

Error Handling

If a listener throws, the error is re-thrown after all other listeners have run — no listener silently suppresses a failure in another.

onHeaders(res, function () {
  throw new Error("something went wrong");
});

onHeaders(res, function () {
  // This still runs even though the previous listener threw
  this.setHeader("X-Trace-Id", "abc123");
});
// → Error: something went wrong  (thrown after second listener completes)

Technical Implementation

  • Strict TypeScript port — built from the ground up with native TypeScript; no runtime dependencies.
  • Single patch, listener queuewriteHead is patched only once per response; subsequent onHeaders calls push to an internal FIFO queue, avoiding redundant wrapping.
  • Behavioral parity — fully compatible with all writeHead overloads: (statusCode), (statusCode, headers), and (statusCode, reason, headers). Supports both 2-D and flat 1-D header arrays as well as plain header objects.
  • appendHeader awareness — on Node.js versions that support it, flat header arrays use appendHeader to preserve duplicate header values.
  • CVE-2025-7339 patched — see changelog below.

Changelog

See HISTORY.md for a detailed list of changes and security fixes.


License

Copyright © 2014 Douglas Christopher Wilson
Copyright © 2026 Nehonix Team
Released under the MIT License.