Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
1 line
5.7 KiB
Plaintext
1 line
5.7 KiB
Plaintext
{"version":3,"file":"prepareSafeIdGeneratorContext.js","sources":["../../../src/server/prepareSafeIdGeneratorContext.ts"],"sourcesContent":["import { type _INTERNAL_RandomSafeContextRunner as RandomSafeContextRunner, debug, GLOBAL_OBJ } from '@sentry/core';\nimport { DEBUG_BUILD } from '../common/debug-build';\n\n// Inline AsyncLocalStorage interface from current types\n// Avoids conflict with resolving it from getBuiltinModule\ntype OriginalAsyncLocalStorage = typeof AsyncLocalStorage;\n\n/**\n * Prepares the global object to generate safe random IDs in cache components contexts\n * See: https://github.com/getsentry/sentry-javascript/blob/ceb003c15973c2d8f437dfb7025eedffbc8bc8b0/packages/core/src/utils/propagationContext.ts#L1\n */\nexport function prepareSafeIdGeneratorContext(): void {\n const sym = Symbol.for('__SENTRY_SAFE_RANDOM_ID_WRAPPER__');\n const globalWithSymbol: typeof GLOBAL_OBJ & { [sym]?: RandomSafeContextRunner } = GLOBAL_OBJ;\n\n // Get initial snapshot - if unavailable, don't set up the wrapper at all\n const initialSnapshot = getAsyncLocalStorageSnapshot();\n if (!initialSnapshot) {\n return;\n }\n\n // We store a wrapper function instead of the raw snapshot because in serverless\n // environments (e.g., Cloudflare Workers), the snapshot is bound to the request\n // context it was created in. Once that request ends, the snapshot becomes invalid.\n // The wrapper catches this and creates a fresh snapshot for the current request context.\n let cachedSnapshot: RandomSafeContextRunner = initialSnapshot;\n\n globalWithSymbol[sym] = <T>(callback: () => T): T => {\n try {\n return cachedSnapshot(callback);\n } catch (error) {\n // Only handle AsyncLocalStorage-related errors, rethrow others\n if (!isAsyncLocalStorageError(error)) {\n throw error;\n }\n\n // Snapshot likely stale, try to get a fresh one and retry\n const freshSnapshot = getAsyncLocalStorageSnapshot();\n // No snapshot available, fall back to direct execution\n if (!freshSnapshot) {\n return callback();\n }\n\n // Update the cached snapshot\n cachedSnapshot = freshSnapshot;\n\n // Retry the callback with the fresh snapshot\n try {\n return cachedSnapshot(callback);\n } catch (retryError) {\n // Only fall back for AsyncLocalStorage errors, rethrow others\n if (!isAsyncLocalStorageError(retryError)) {\n throw retryError;\n }\n // If fresh snapshot also fails with ALS error, fall back to direct execution\n return callback();\n }\n }\n };\n\n DEBUG_BUILD && debug.log('[@sentry/nextjs] Prepared safe random ID generator context');\n}\n\nfunction getAsyncLocalStorage(): OriginalAsyncLocalStorage | undefined {\n // May exist in the Next.js runtime globals\n // Doesn't exist in some of our tests\n if (typeof AsyncLocalStorage !== 'undefined') {\n return AsyncLocalStorage;\n }\n\n // Try to resolve it dynamically without synchronously importing the module\n // This is done to avoid importing the module synchronously at the top\n // which means this is safe across runtimes\n if ('getBuiltinModule' in process && typeof process.getBuiltinModule === 'function') {\n const { AsyncLocalStorage } = process.getBuiltinModule('async_hooks') ?? {};\n\n return AsyncLocalStorage as OriginalAsyncLocalStorage;\n }\n\n return undefined;\n}\n\nfunction getAsyncLocalStorageSnapshot(): RandomSafeContextRunner | undefined {\n const als = getAsyncLocalStorage();\n\n if (!als || typeof als.snapshot !== 'function') {\n DEBUG_BUILD &&\n debug.warn(\n '[@sentry/nextjs] No AsyncLocalStorage found in the runtime or AsyncLocalStorage.snapshot() is not available, skipping safe random ID generator context preparation, you may see some errors with cache components.',\n );\n return undefined;\n }\n\n return als.snapshot();\n}\n\nfunction isAsyncLocalStorageError(error: unknown): boolean {\n return error instanceof Error && error.message.includes('AsyncLocalStorage');\n}\n"],"names":["GLOBAL_OBJ","DEBUG_BUILD","debug"],"mappings":";;;;;AAGA;AACA;;AAGA;AACA;AACA;AACA;AACO,SAAS,6BAA6B,GAAS;AACtD,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