Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ALS is deprecated and archived, Better to use AsyncLocalStorage class fom the async_hook std module #148

Closed
ankitsny opened this issue Jan 11, 2023 · 12 comments · Fixed by #158
Assignees

Comments

@ankitsny
Copy link

ankitsny commented Jan 11, 2023

https://github.com/vicanso/async-local-storage is archived.

Working Example with the native AsyncLocalStorage:

// ctx.ts
import { AsyncLocalStorage } from "async_hooks";

const asyncLocalStorage = new AsyncLocalStorage<Map<string, unknown>>();

export const setInCtx = <T = unknown>(key: string, value: T): void => {
  asyncLocalStorage.getStore()?.set(key, value);
}

export const getFromCtx = <T = unknown>(key: string): T | undefined => asyncLocalStorage.getStore()?.get(key) as T;

export const initReqContext = (req: any, _res: any, next: any) => {
  asyncLocalStorage.run(new Map(), () => {
    const store = asyncLocalStorage.getStore();
    if (store){
      store.set("key",  "value");
    }
    next();
  });
}
// server.ts / index.ts 

import { initReqContext } from './ctx';

// this only works with preHandler hooks
server.addHook("preHandler", initReqContext)

you can start using get and set function to access the store

@kibertoad
Copy link
Member

Check out the underlying library that we use, it already uses ALS when supported by used Node version.

@Uzlopak
Copy link
Contributor

Uzlopak commented Jan 11, 2023

As far as I can see if is part of node 14. So i agree, we can replace this dependency with the built in.

Can you provide a PR?

@ankitsny
Copy link
Author

This is not an issue, but I struggled to integrate the AsyncLocalStorage with fastify so I am sharing the solution here.
I noticed it only work with the preHandler hook.

@kibertoad
Copy link
Member

This plugin works with any lifecycle phase, and does not use the library that you mentioned.
@Uzlopak what's the minimum Node requirement for fastify 4?

@Uzlopak
Copy link
Contributor

Uzlopak commented Jan 11, 2023

node 14 is the minimum requirement.

@kibertoad
Copy link
Member

I will create a PR then

@kibertoad kibertoad self-assigned this Feb 23, 2023
@voxpelli
Copy link
Contributor

I think there's a mix up between @kibertoad's asynchronous-local-storage module which is used here and the archived async-local-storage module.

The asynchronous-local-storage module uses the AsyncLocalStorage from node:async_hooks when that's enabled, else downgrades to cls-hooked.

It's probably time to drop cls-hooked in asynchronous-local-storage, but that's a separate issue for that repository I think, its only an issue for this project if that would make asynchronous-local-storage so small that it would make more sense to include it directly within this module.

@kibertoad
Copy link
Member

Yeah, I was planning to do that for quite a while. PR would be welcome

@voxpelli
Copy link
Contributor

Yeah, I was planning to do that for quite a while. PR would be welcome

@kibertoad PR for which of the two?

  1. Removing cls-hooked from asynchronous-local-storage
  2. Moving the node:async_hooks based part of asynchronous-local-storage into this module, removing asynchronous-local-storage as a separate dependency

@kibertoad
Copy link
Member

The second one.

@mcollina
Copy link
Member

While bumping Fastify major is hard, bumping an individual package is easy. There are good reasons to bump this package major version and drop Node.js v14.

@voxpelli
Copy link
Contributor

I'm making a PR

voxpelli added a commit to voxpelli/fastify-request-context that referenced this issue Jun 10, 2023
Fixes fastify#148

Signed-off-by: Pelle Wessman <pelle@kodfabrik.se>
voxpelli added a commit to voxpelli/fastify-request-context that referenced this issue Jun 11, 2023
Fixes fastify#148

Signed-off-by: Pelle Wessman <pelle@kodfabrik.se>
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 a pull request may close this issue.

5 participants