Skip to content

Commit 28665c7

Browse files
committed
fix: format
1 parent 7543412 commit 28665c7

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

packages/eslint-plugin-sdk/test/lib/rules/no-unsafe-random-apis.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,3 @@ describe('no-unsafe-random-apis', () => {
154154
});
155155
});
156156
});
157-

packages/nextjs/src/server/prepareSafeIdGeneratorContext.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,40 @@ import { DEBUG_BUILD } from '../common/debug-build';
33

44
type SafeRandomContextRunner = <T>(callback: () => T) => T;
55

6+
type OriginalAsyncLocalStorage = typeof AsyncLocalStorage;
7+
68
/**
79
* Prepares the global object to generate safe random IDs in cache components contexts
810
* See: https://github.com/getsentry/sentry-javascript/blob/ceb003c15973c2d8f437dfb7025eedffbc8bc8b0/packages/core/src/utils/propagationContext.ts#L1
911
*/
1012
export function prepareSafeIdGeneratorContext(): void {
1113
const sym = Symbol.for('__SENTRY_SAFE_RANDOM_ID_WRAPPER__');
1214
const globalWithSymbol: typeof GLOBAL_OBJ & { [sym]?: SafeRandomContextRunner } = GLOBAL_OBJ;
13-
globalWithSymbol[sym] = AsyncLocalStorage.snapshot();
15+
const als = getAsyncLocalStorage();
16+
if (!als) {
17+
DEBUG_BUILD &&
18+
debug.warn(
19+
'[@sentry/nextjs] No AsyncLocalStorage found in the runtime, skipping safe random ID generator context preparation, you may see some errors with Cache components.',
20+
);
21+
return;
22+
}
1423

24+
globalWithSymbol[sym] = als.snapshot();
1525
DEBUG_BUILD && debug.log('[@sentry/nextjs] Prepared safe random ID generator context');
1626
}
27+
28+
function getAsyncLocalStorage(): OriginalAsyncLocalStorage | undefined {
29+
// May exist in the Next.js runtime globals
30+
if ('AsyncLocalStorage' in GLOBAL_OBJ) {
31+
return AsyncLocalStorage;
32+
}
33+
34+
// Try to resolve it dynamically without synchronously importing the module
35+
if ('getBuiltinModule' in process && typeof process.getBuiltinModule === 'function') {
36+
const { AsyncLocalStorage } = process.getBuiltinModule('async_hooks') ?? {};
37+
38+
return AsyncLocalStorage as OriginalAsyncLocalStorage;
39+
}
40+
41+
return undefined;
42+
}

0 commit comments

Comments
 (0)