@sentry/nextjs
Version:
Official Sentry SDK for Next.js
50 lines (40 loc) • 1.98 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const core = require('@sentry/core');
const debugBuild = require('../common/debug-build.js');
// Inline AsyncLocalStorage interface from current types
// Avoids conflict with resolving it from getBuiltinModule
/**
* Prepares the global object to generate safe random IDs in cache components contexts
* See: https://github.com/getsentry/sentry-javascript/blob/ceb003c15973c2d8f437dfb7025eedffbc8bc8b0/packages/core/src/utils/propagationContext.ts#L1
*/
function prepareSafeIdGeneratorContext() {
const sym = Symbol.for('__SENTRY_SAFE_RANDOM_ID_WRAPPER__');
const globalWithSymbol = core.GLOBAL_OBJ;
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;
}
globalWithSymbol[sym] = als.snapshot();
debugBuild.DEBUG_BUILD && core.debug.log('[@sentry/nextjs] Prepared safe random ID generator context');
}
function getAsyncLocalStorage() {
// May exist in the Next.js runtime globals
// Doesn't exist in some of our tests
if (typeof AsyncLocalStorage !== 'undefined') {
return AsyncLocalStorage;
}
// Try to resolve it dynamically without synchronously importing the module
// This is done to avoid importing the module synchronously at the top
// which means this is safe across runtimes
if ('getBuiltinModule' in process && typeof process.getBuiltinModule === 'function') {
const { AsyncLocalStorage } = process.getBuiltinModule('async_hooks') ?? {};
return AsyncLocalStorage ;
}
return undefined;
}
exports.prepareSafeIdGeneratorContext = prepareSafeIdGeneratorContext;
//# sourceMappingURL=prepareSafeIdGeneratorContext.js.map