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

unreq

v0.2.0

Published

HTTP cancellation propagation library

Readme

Unreq

Automatic HTTP Request Cancellation Propagation for Node.js servers. Detect client disconnections, manage cancellable operations, and cleanly integrate with your database and observability tools.

Built with TypeScript MIT License

Features

  • Automatic Cancellation Detection: Detects when clients abort HTTP requests on the server side
  • Configurable Cancellation Registry: Choose between in-memory (default) or Redis for distributed systems
  • Database Cancellation Integration: Includes hooks and utilities for PostgreSQL, MySQL, and MongoDB
  • Framework Support: Ready-to-use adapters for Express, Fastify, and Elysia
  • OpenTelemetry Integration: Built-in tracing for observability

Installation

pnpm add unreq

# For Redis registry (optional)
pnpm add ioredis

# For PostgreSQL hook (optional)
pnpm add pg

# For MySQL hook (optional)
pnpm add mysql2

# For MongoDB hook (optional)
pnpm add mongodb

Quick Start

Express Example

import express from 'express';
import { createExpressCancellationMiddleware, withExpressDbCancellation, createPostgresCancellationHook } from 'unreq';
import { Pool } from 'pg';

const app = express();
const pool = new Pool();

// 1. Create the cancellation hook specific to your database
const pgCancelHook = createPostgresCancellationHook(pool);

// 2. Configure middleware with in-memory registry (default)
const { middleware, registry } = createExpressCancellationMiddleware({
  dbCancellationHook: pgCancelHook,
  requestIdHeader: 'x-request-id' // optional, default value
});

// 3. Apply middleware early
app.use(middleware);

// 4. Define routes with DB cancellation support
app.get('/api/long-query', withExpressDbCancellation(
  async (req, res) => {
    const client = await pool.connect();
    try {
      // Execute a query that might take a long time
      const result = await client.query('SELECT pg_sleep(30), * FROM large_table');
      res.json(result.rows);
    } finally {
      client.release();
    }
  },
  registry,
  async (req) => {
    // Get the PostgreSQL backend PID to identify the query process
    const client = await pool.connect();
    try {
      const result = await client.query('SELECT pg_backend_pid()'); 
      return { pid: result.rows[0].pg_backend_pid };
    } finally {
      client.release();
    }
  }
));

app.listen(3000);

Using Redis Registry for Distributed Systems

import { createExpressCancellationMiddleware } from 'unreq';
import Redis from 'ioredis';

const redisClient = new Redis('redis://localhost:6379');

const { middleware, registry } = createExpressCancellationMiddleware({
  dbCancellationHook: myDbCancelHook,
  registry: {
    type: 'redis',
    redisClient: redisClient,
    ttlSeconds: 3600 // Optional: override default TTL
  }
});

Framework Support

Fastify

import Fastify from 'fastify';
import { fastifyCancellation, withFastifyDbCancellation } from 'unreq';

const fastify = Fastify();

// Register the plugin
fastify.register(fastifyCancellation, {
  dbCancellationHook: myDbCancelHook
});

// Use with cancelable route
fastify.get('/api/query', withFastifyDbCancellation(
  async (request, reply) => {
    // Handle request
    return { result: 'data' };
  },
  (request) => {
    // Return database identifier
    return { id: 'db-operation-id' };
  }
));

Elysia (Bun)

import { Elysia } from 'elysia';
import { createElysiaCancellationPlugin, withElysiaDbCancellation } from 'unreq';

const app = new Elysia();

// Create and use the plugin
const cancellationPlugin = createElysiaCancellationPlugin({
  dbCancellationHook: myDbCancelHook
});

app.use(cancellationPlugin);

// Use with cancelable route
app.get('/api/query', withElysiaDbCancellation(
  async (context) => {
    // Handle request
    return { result: 'data' };
  },
  (context) => {
    // Return database identifier
    return { id: 'db-operation-id' };
  }
));

Database Hooks

The library includes ready-made hooks for popular databases:

PostgreSQL

import { Pool } from 'pg';
import { createPostgresCancellationHook, getPostgresBackendPid } from 'unreq';

const pool = new Pool();
const pgCancelHook = createPostgresCancellationHook(pool);

// Later, get the backend PID to associate with a request
const client = await pool.connect();
try {
  const pid = await getPostgresBackendPid(client);
  await registry.associateDbIdentifier(requestId, { pid });
  // Run your query...
} finally {
  client.release();
}

MySQL

import mysql from 'mysql2/promise';
import { createMySqlCancellationHook, getMySqlThreadId } from 'unreq';

const pool = mysql.createPool({/* config */});
const mysqlCancelHook = createMySqlCancellationHook(pool);

// Associate the thread ID with a request
const connection = await pool.getConnection();
try {
  const threadId = getMySqlThreadId(connection);
  await registry.associateDbIdentifier(requestId, { threadId });
  // Run your query...
} finally {
  connection.release();
}

MongoDB

import { MongoClient } from 'mongodb';
import { createMongoDbCancellationHook, getMongoDbOperationId } from 'unreq';

const client = new MongoClient('mongodb://localhost:27017');
await client.connect();
const db = client.db('mydb');

const mongodbCancelHook = createMongoDbCancellationHook(db);

// Associate with the current operation
const operationId = await getMongoDbOperationId(db, { ns: 'mydb.collection' });
if (operationId) {
  await registry.associateDbIdentifier(requestId, { operationId });
}

OpenTelemetry Integration

import { NodeTracerProvider } from '@opentelemetry/sdk-node';
import { createExpressCancellationMiddleware } from 'unreq';

// Set up your tracer provider
const tracerProvider = new NodeTracerProvider();
tracerProvider.register();

// Pass it to the middleware
const { middleware } = createExpressCancellationMiddleware({
  dbCancellationHook: myDbCancelHook,
  otelTracerProvider: tracerProvider
});

Architecture

  • Request ID Middleware: Extracts or generates a unique ID for each request and sets up cancellation detection
  • Cancellation Registry: Tracks active requests and their associated database operations
  • Database Cancellation Hook: A user-provided function that handles the actual database-specific cancellation logic
  • OpenTelemetry Integration: Creates spans and events for the entire cancellation lifecycle

License

MIT