fastify-request-context icon indicating copy to clipboard operation
fastify-request-context copied to clipboard

feat: add asyncLocalStorage option for external AsyncLocalStorage inj…

Open katreniak opened this issue 1 month ago • 0 comments

…ection

Add an asyncLocalStorage plugin option to allow injecting an external AsyncLocalStorage instance. This enables sharing a single AsyncLocalStorage across multiple request sources in applications that handle requests from various origins.

Use cases:

  • Fastify HTTP requests
  • Queue consumers
  • Scheduled tasks and timers
  • Other HTTP servers or frameworks

Previously, the plugin created its own AsyncLocalStorage instance, making it difficult to maintain a unified request context across different entry points. By providing the asyncLocalStorage option, users can now create a central AsyncLocalStorage instance and share it across all request handlers, without coupling non-HTTP code to Fastify.

Usage:

  const sharedALS = new AsyncLocalStorage()
  app.register(fastifyRequestContext, {
    asyncLocalStorage: sharedALS
  })

Breaking change: None.

  • The plugin maintains backward compatibility by using a default AsyncLocalStorage instance when the setter is not called.
  • Exported asyncLocalStorage is untouched.

Checklist

katreniak avatar Oct 28 '25 08:10 katreniak