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

@fikrisr/nestjs-ctx

v1.0.1

Published

High-performance request-scoped context for NestJS using AsyncLocalStorage

Downloads

202

Readme

@fikrisr/nestjs-ctx

A high-performance, type-safe alternative to Scope.REQUEST in NestJS using AsyncLocalStorage.

Why this library?

NestJS's Scope.REQUEST is powerful but comes with a performance cost:

  1. Dependency Infection: Making one service request-scoped makes everything that depends on it request-scoped, leading to a cascade of new instances per request.
  2. Memory Overhead: High memory usage due to constant object creation and garbage collection.

@fikrisr/nestjs-ctx uses Node.js's native AsyncLocalStorage to store context data without breaking the Singleton pattern of your services.

Features

  • Singleton Safe: Access request data inside Singleton services.
  • Type-Safe: Full TypeScript support for your context data.
  • High Performance: 60x+ faster than native Scope.REQUEST in high-concurrency scenarios.

Benchmark Results

Running the included benchmark suite (tests/benchmark.ts) with a deep dependency tree (5 levels) and 1,000,000 iterations:

| Strategy | Time (ms) | Throughput (ops/s) | | :--- | :--- | :--- | | Scope.REQUEST | 14,912.25 | 67,059 | | nestjs-ctx | 225.92 | 4,426,422 |

Result: @fikrisr/nestjs-ctx is 66.0x faster than native request-scoped providers.

Installation

npm install @fikrisr/nestjs-ctx

Usage

1. Register the Module

import { CtxModule } from '@fikrisr/nestjs-ctx';

@Module({
  imports: [
    CtxModule.forRoot(),
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(CtxMiddleware).forRoutes('*');
  }
}

2. Define Your Context Type

import { ICtx } from '@fikrisr/nestjs-ctx';

export interface MyContext extends ICtx {
  userId?: string;
  role?: string;
}

3. Set or Update Context

You can update the context anywhere in your application (Services, Guards, Interceptors).

@Injectable()
export class AuthService {
  constructor(private readonly store: CtxStore<MyContext>) {}

  async validateUser(token: string) {
    const user = await this.verify(token);
    
    // Set data into context
    this.store.set('userId', user.id);
    this.store.set('role', user.role);
  }
}

4. Access Context in Services

import { Injectable } from '@nestjs/common';
import { CtxStore } from '@fikrisr/nestjs-ctx';
import { MyContext } from './interfaces/my-context';

@Injectable()
export class MyService {
  constructor(private readonly store: CtxStore<MyContext>) {}

  doSomething() {
    const { correlationId, userId } = this.store.get();
    console.log(`Processing for user ${userId} with ID ${correlationId}`);
  }
}

API Reference

CtxStore<T>

  • run(context: T, callback: () => void): Starts a new context.
  • get(): T: Returns the current context. Throws error if not in context.
  • getStore(): T | undefined: Returns the current context or undefined.
  • set<K extends keyof T>(key: K, value: T[K]): Updates a property in the current context object.

CtxMiddleware

Automatically extracts x-correlation-id from headers or generates a new one, then wraps the request in a context.

License

MIT