File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import type { AsyncLocalStorage } from 'node:async_hooks' ;
2+
3+ type ProcessWithBuiltins = {
4+ getBuiltinModule ?: ( id : string ) => unknown ;
5+ } ;
6+
7+ /** @internal */
8+ export const getAsyncLocalStorage = ( ) => {
9+ const process = ( globalThis as { process ?: ProcessWithBuiltins } ) . process ;
10+ return (
11+ process ?. getBuiltinModule ?.( 'node:async_hooks' ) as
12+ | { AsyncLocalStorage ?: new < T > ( ) => AsyncLocalStorage < T > }
13+ | undefined
14+ ) ?. AsyncLocalStorage ;
15+ } ;
Original file line number Diff line number Diff line change 11import { tryGetInvokeContext } from './use-core' ;
2+ import { getAsyncLocalStorage } from '../shared/platform/async-local-storage' ;
23import { isServer } from '@qwik.dev/core/build' ;
34import type { AsyncLocalStorage } from 'node:async_hooks' ;
45
@@ -7,20 +8,10 @@ let _locale: string | undefined = undefined;
78let localAsyncStore : AsyncLocalStorage < string > | undefined ;
89
910if ( isServer ) {
10- // Hide the `node:` specifier from static analysis so consumers bundling the
11- // published core for the browser without the Qwik Vite plugin (e.g. raw
12- // webpack used by Cypress's webpack-preprocessor) don't fail with
13- // "UnhandledSchemeError" on this dead-code branch. The `.join` defeats
14- // Rollup's constant folding so the literal isn't restored at qwik build
15- // time, and the magic comments cover Vite/webpack at consumer build time.
16- const moduleName = [ 'node' , 'async_hooks' ] . join ( ':' ) ;
17- import ( /* @vite -ignore */ /* webpackIgnore: true */ moduleName )
18- . then ( ( module : typeof import ( 'node:async_hooks' ) ) => {
19- localAsyncStore = new module . AsyncLocalStorage ( ) ;
20- } )
21- . catch ( ( ) => {
22- // ignore if AsyncLocalStorage is not available
23- } ) ;
11+ const AsyncLocalStorage = getAsyncLocalStorage ( ) ;
12+ if ( AsyncLocalStorage ) {
13+ localAsyncStore = new AsyncLocalStorage ( ) ;
14+ }
2415}
2516
2617/**
You can’t perform that action at this time.
0 commit comments