Skip to content

Conversation

@katreniak
Copy link

…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

…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.
}
}

const requestContext = createRequestContext(asyncLocalStorage)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed? we create it later again

Copy link
Member

@Eomm Eomm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good feature

I'm wondering if this feature is still needed:
https://github.com/fastify/fastify-request-context?tab=readme-ov-file#usage-outside-of-a-request

because it may creates some headache 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants