UNPKG

@sentry/nextjs

Version:
64 lines (60 loc) 2.22 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const core = require('@sentry/core'); const debugBuild = require('../common/debug-build.js'); function prepareSafeIdGeneratorContext() { const sym = /* @__PURE__ */ Symbol.for("__SENTRY_SAFE_RANDOM_ID_WRAPPER__"); const globalWithSymbol = core.GLOBAL_OBJ; const initialSnapshot = getAsyncLocalStorageSnapshot(); if (!initialSnapshot) { return; } let cachedSnapshot = initialSnapshot; globalWithSymbol[sym] = (callback) => { try { return cachedSnapshot(callback); } catch (error) { if (!isAsyncLocalStorageError(error)) { throw error; } const freshSnapshot = getAsyncLocalStorageSnapshot(); if (!freshSnapshot) { return callback(); } cachedSnapshot = freshSnapshot; try { return cachedSnapshot(callback); } catch (retryError) { if (!isAsyncLocalStorageError(retryError)) { throw retryError; } return callback(); } } }; debugBuild.DEBUG_BUILD && core.debug.log("[@sentry/nextjs] Prepared safe random ID generator context"); } function getAsyncLocalStorage() { if (typeof AsyncLocalStorage !== "undefined") { return AsyncLocalStorage; } if ("getBuiltinModule" in process && typeof process.getBuiltinModule === "function") { const { AsyncLocalStorage: AsyncLocalStorage2 } = process.getBuiltinModule("async_hooks") ?? {}; return AsyncLocalStorage2; } return void 0; } function getAsyncLocalStorageSnapshot() { const als = getAsyncLocalStorage(); if (!als || typeof als.snapshot !== "function") { debugBuild.DEBUG_BUILD && core.debug.warn( "[@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." ); return void 0; } return als.snapshot(); } function isAsyncLocalStorageError(error) { return error instanceof Error && error.message.includes("AsyncLocalStorage"); } exports.prepareSafeIdGeneratorContext = prepareSafeIdGeneratorContext; //# sourceMappingURL=prepareSafeIdGeneratorContext.js.map