New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

@marceloraineri/async-context

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@marceloraineri/async-context

> Async context propagation and structured logging for Node.js, with first-class framework integrations.

latest
Source
npmnpm
Version
1.1.1
Version published
Maintainers
1
Created
Source

AsyncContext

Async context propagation and structured logging for Node.js, with first-class framework integrations.

AsyncContext helps you carry contextual data across asynchronous boundaries without passing parameters through every function. It provides a Context wrapper around AsyncLocalStorage, ready-made middleware for popular frameworks, and a structured logger that automatically includes the active context in every log entry.

Highlights

  • Consistent per-request context without parameter threading.
  • Simple, direct API for reading and writing context anywhere in the async flow.
  • Observability-ready with correlation IDs, tenant/user data, and tracing metadata.
  • Full-featured logger with levels, redaction, sampling, timers, and transports.
  • DX-friendly configuration via presets and environment variables.
  • Zero runtime dependencies and performance-focused design.

Installation

npm i @marceloraineri/async-context

Quick start

import crypto from "node:crypto";
import { Context } from "@marceloraineri/async-context";

await Context.run({ requestId: crypto.randomUUID() }, async () => {
  Context.addValue("user", { id: 42, name: "Ada" });
  Context.addOptions({ feature: "beta", retry: 2 });

  await Promise.resolve();

  const store = Context.getStore();
  console.log(store?.requestId); // 184fa9a3-f967-4a98-9d8f-57152e7cbe64
  console.log(store?.user); // { id: 42, name: "Ada" }
  console.log(store?.options); // { feature: "beta", retry: 2 }
});

Structured logging

The logger automatically merges the active async context and supports redaction, sampling, timers, and JSON or pretty output.

import crypto from "node:crypto";
import { Context, createLogger } from "@marceloraineri/async-context";

const logger = createLogger({
  name: "api",
  level: "info",
  contextKey: "ctx",
  redactKeys: ["ctx.token", "data.password"],
});

await Context.run({ requestId: crypto.randomUUID(), token: "secret" }, async () => {
  logger.info("request started", { route: "/ping" });
});

By default, common sensitive keys (for example password, token, authorization) are automatically redacted. You can disable this with redactDefaults: false or add extra key names with redactFieldNames.

Timers and child loggers

import { createLogger } from "@marceloraineri/async-context";

const logger = createLogger({ name: "jobs", level: "debug" });
const jobLogger = logger.child({ job: "import-users" });

const end = jobLogger.startTimer("debug");
await Promise.resolve();
end("job completed");

const noisyLogger = logger.child({ job: "debug-import" }, { level: "trace" });
noisyLogger.trace("verbose logging enabled");

JSON output or custom transports

import { createConsoleTransport, createLogger } from "@marceloraineri/async-context";

const logger = createLogger({
  level: "info",
  transports: [createConsoleTransport({ format: "json" })],
});

logger.info("structured log", { feature: "json" });

OpenAI wrapper

Capture OpenAI call metadata inside the current async context.

import OpenAI from "openai";
import { Context, withOpenAIContext } from "@marceloraineri/async-context";

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

await Context.run({ requestId: "req_123" }, async () => {
  const response = await withOpenAIContext(
    "responses.create",
    { model: "gpt-4o", input: "Hello!" },
    (req) => openai.responses.create(req),
    { includeRequest: true }
  );

  console.log(response.id);
  console.log(Context.getValue("openai"));
});

By default, the wrapper appends summaries to the openai context key, and only includes safe request fields unless you explicitly allow more keys.

OpenTelemetry (optional)

Create spans, propagate trace context from headers, and sync baggage with AsyncContext when @opentelemetry/api is available.

import * as otel from "@opentelemetry/api";
import {
  Context,
  createAsyncContextExpressOpenTelemetryMiddleware,
  setOpenTelemetryBaggageFromContext,
  withOpenTelemetrySpan,
} from "@marceloraineri/async-context";

app.use(
  createAsyncContextExpressOpenTelemetryMiddleware({
    otel: {
      api: otel,
      tracerName: "api",
      contextAttributeKeys: ["requestId", "tenantId"],
    },
  })
);

await Context.run({ requestId: "req_1", tenantId: "t_123" }, async () => {
  await withOpenTelemetrySpan(
    "db.query",
    async () => Promise.resolve(),
    { api: otel, attributes: { db: "users" } }
  );

  setOpenTelemetryBaggageFromContext({
    api: otel,
    contextKeys: ["requestId", "tenantId"],
    baggagePrefix: "ctx.",
  });
});

Performance timing

Measure sync or async work and store timing data in the active context.

import { Context } from "@marceloraineri/async-context";

await Context.run({}, async () => {
  await Context.measure("db.query", async () => {
    await Promise.resolve();
  }, { data: { table: "users" } });

  console.log(Context.getValue("perf"));
});

Use key or mode to control where entries are stored.

Context.measure("cache.lookup", () => "hit", { key: "performance", mode: "overwrite" });

DX and configuration

Use presets or environment variables to configure logging without code changes.

import { createLoggerFromEnv, loggerPreset } from "@marceloraineri/async-context";

const logger = createLoggerFromEnv({
  name: "api",
  defaults: loggerPreset("production"),
});

Environment variables:

VariableDescriptionExample
LOG_PRESETdevelopment, production, or test presetproduction
LOG_LEVELMinimum log levelinfo
LOG_FORMATjson or prettyjson
LOG_COLORSEnable ANSI colorstrue
LOG_CONTEXTAttach async contexttrue
LOG_CONTEXT_KEYKey name for contextctx
LOG_CONTEXT_KEYSComma-separated allowlistrequestId,tenantId
LOG_REDACT_KEYSComma-separated redaction pathsctx.token,data.password
LOG_REDACT_DEFAULTSEnable default sensitive-field redactiontrue
LOG_REDACT_FIELDSExtra sensitive field names (comma-separated)accessToken,creditCard
LOG_REDACT_PLACEHOLDERMask value placeholder[REDACTED]
LOG_SAMPLE_RATE0..1 sampling0.25
LOG_INCLUDE_PIDInclude process idtrue
LOG_INCLUDE_HOSTNAMEInclude hostnamefalse
LOG_TIMESTAMPInclude timestamptrue
LOG_NAMELogger nameapi

Framework integrations

Express

AsyncContextExpresssMiddleware (with three "s") and AsyncContextExpressMiddleware (alias) create a new context per request and seed it with a unique instance_id.

import express from "express";
import { AsyncContextExpressMiddleware, Context } from "@marceloraineri/async-context";

const app = express();
app.use(AsyncContextExpressMiddleware);

app.get("/ping", (_req, res) => {
  const store = Context.getStore();
  res.json({ instanceId: store?.instance_id ?? null });
});

app.listen(3000, () => console.log("API listening on :3000"));

If you need a custom request id or seed data, use createAsyncContextExpressMiddleware.

import express from "express";
import { createAsyncContextExpressMiddleware, Context } from "@marceloraineri/async-context";

const app = express();

app.use(
  createAsyncContextExpressMiddleware({
    idKey: "request_id",
    seed: (req) => ({ method: req.method, path: req.url }),
  })
);

app.get("/ping", (_req, res) => {
  const store = Context.getStore();
  res.json({ requestId: store?.request_id ?? null });
});

Fastify

Use the onRequest hook or the convenience registration helper.

import fastify from "fastify";
import { createAsyncContextFastifyHook, Context } from "@marceloraineri/async-context";

const app = fastify();
app.addHook("onRequest", createAsyncContextFastifyHook());

app.get("/ping", async () => {
  const store = Context.getStore();
  return { instanceId: store?.instance_id ?? null };
});

Koa

import Koa from "koa";
import { createAsyncContextKoaMiddleware, Context } from "@marceloraineri/async-context";

const app = new Koa();
app.use(createAsyncContextKoaMiddleware());

app.use(async (ctx) => {
  const store = Context.getStore();
  ctx.body = { instanceId: store?.instance_id ?? null };
});

Next.js API routes

import type { NextApiRequest, NextApiResponse } from "next";
import { createAsyncContextNextHandler, Context } from "@marceloraineri/async-context";

export default createAsyncContextNextHandler(
  async (_req: NextApiRequest, res: NextApiResponse) => {
    const store = Context.getStore();
    res.status(200).json({ instanceId: store?.instance_id ?? null });
  }
);

NestJS

AsyncContextNestMiddleware reuses the Express integration to enable async context in Nest (Express adapter).

import { MiddlewareConsumer, Module, NestModule } from "@nestjs/common";
import { AsyncContextNestMiddleware } from "@marceloraineri/async-context";

@Module({})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(AsyncContextNestMiddleware).forRoutes("*");
  }
}

AdonisJS

AsyncContextAdonisMiddleware plugs into the AdonisJS pipeline and creates one context per request.

// app/Http/Middleware/AsyncContext.ts
import { AsyncContextAdonisMiddleware } from "@marceloraineri/async-context";

export default AsyncContextAdonisMiddleware;

Sentry (optional)

AsyncContext can enrich Sentry events with the active store. If @sentry/node is not installed, the helpers safely no-op. Sensitive fields are redacted by default. Disable with redactDefaults: false or add redactFieldNames.

import express from "express";
import {
  AsyncContextExpressMiddleware,
  initSentryWithAsyncContext,
  sentryAsyncContextExpressMiddleware,
  sentryErrorHandler,
} from "@marceloraineri/async-context";

initSentryWithAsyncContext({
  sentryInit: {
    dsn: process.env.SENTRY_DSN,
    tracesSampleRate: 1.0,
  },
  redactKeys: ["async_context.token"],
});

const app = express();
app.use(AsyncContextExpressMiddleware);
app.use(sentryAsyncContextExpressMiddleware());

app.get("/ping", (_req, res) => res.json({ ok: true }));

app.use(sentryErrorHandler());

API overview

  • Context.run(store, callback) and Context.run(callback)
  • Context.getStore() and Context.requireStore()
  • Context.getValue(key) and Context.requireValue(key)
  • Context.addValue(key, value) and Context.addObjectValue(values)
  • Context.runWith(values, callback)
  • Context.snapshot() and Context.reset()
  • createLogger(options) and new Logger(options)
  • Logger.child(bindings, options?), Logger.withBindings(bindings, callback, options?), and Logger.startTimer(level?)
  • createConsoleTransport(options)
  • createLoggerFromEnv(options) and loggerPreset(preset)
  • parseBooleanEnv(value), parseNumberEnv(value), parseCsvEnv(value), parseLogLevelEnv(value), parseLogFormatEnv(value), parseLoggerPresetEnv(value)
  • createAsyncContextExpressMiddleware(options)
  • createAsyncContextFastifyHook(options) and registerAsyncContextFastify(app, options)
  • createAsyncContextKoaMiddleware(options)
  • createAsyncContextNextHandler(handler, options)
  • withOpenTelemetrySpan(name, callback, options) and recordOpenTelemetrySpan(summary, options)
  • createAsyncContextExpressOpenTelemetryMiddleware(options)
  • createAsyncContextFastifyOpenTelemetryHook(options)
  • createAsyncContextKoaOpenTelemetryMiddleware(options)
  • createAsyncContextNextOpenTelemetryHandler(handler, options)
  • setOpenTelemetryBaggageFromContext(options) and mergeContextFromOpenTelemetryBaggage(options)
  • extractOpenTelemetryContextFromHeaders(headers, options) and injectOpenTelemetryContextToHeaders(headers, options)
  • initSentryWithAsyncContext(options) and captureExceptionWithContext(error)

Best practices

  • Avoid replacing the entire store object; prefer addValue and addObjectValue.
  • Long-lived contexts can retain memory; always complete the flow (next() in middleware).
  • AsyncLocalStorage is per-process, so each worker maintains its own context.

Contributing

Issues and pull requests are welcome. Include repro steps, tests, or concrete usage scenarios when possible.

FAQs

Package last updated on 13 Feb 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts