fastify-request-context
fastify-request-context copied to clipboard
feat: add asyncLocalStorage option for external AsyncLocalStorage inj…
…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
- [x] run
npm run test && npm run benchmark --if-present - [x] tests and/or benchmarks are included
- [x] documentation is changed or added
- [x] commit message and code follows the Developer's Certification of Origin and the Code of conduct