{"version":3,"file":"index.js","sources":["../../../src/edge/index.ts"],"sourcesContent":["// import/export got a false positive, and affects most of our index barrel files\n// can be removed once following issue is fixed: https://github.com/import-js/eslint-plugin-import/issues/703\n/* eslint-disable import/export */\nimport { context } from '@opentelemetry/api';\nimport {\n applySdkMetadata,\n type EventProcessor,\n getCapturedScopesOnSpan,\n getCurrentScope,\n getGlobalScope,\n getIsolationScope,\n getRootSpan,\n GLOBAL_OBJ,\n registerSpanErrorInstrumentation,\n SEMANTIC_ATTRIBUTE_SENTRY_OP,\n SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,\n SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,\n setCapturedScopesOnSpan,\n spanToJSON,\n stripUrlQueryAndFragment,\n} from '@sentry/core';\nimport { getScopesFromContext } from '@sentry/opentelemetry';\nimport type { VercelEdgeOptions } from '@sentry/vercel-edge';\nimport { getDefaultIntegrations, init as vercelEdgeInit } from '@sentry/vercel-edge';\nimport { DEBUG_BUILD } from '../common/debug-build';\nimport { ATTR_NEXT_SPAN_TYPE } from '../common/nextSpanAttributes';\nimport { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../common/span-attributes-with-logic-attached';\nimport { addHeadersAsAttributes } from '../common/utils/addHeadersAsAttributes';\nimport { dropMiddlewareTunnelRequests } from '../common/utils/dropMiddlewareTunnelRequests';\nimport { isBuild } from '../common/utils/isBuild';\nimport { flushSafelyWithTimeout, isCloudflareWaitUntilAvailable, waitUntil } from '../common/utils/responseEnd';\nimport { setUrlProcessingMetadata } from '../common/utils/setUrlProcessingMetadata';\nimport { distDirRewriteFramesIntegration } from './distDirRewriteFramesIntegration';\n\nexport * from '@sentry/vercel-edge';\nexport * from '../common';\nexport { captureUnderscoreErrorException } from '../common/pages-router-instrumentation/_error';\n\n// Override core span methods with Next.js-specific implementations that support Cache Components\nexport { startSpan, startSpanManual, startInactiveSpan } from '../common/utils/nextSpan';\nexport { wrapApiHandlerWithSentry } from './wrapApiHandlerWithSentry';\n\nexport type EdgeOptions = VercelEdgeOptions;\n\nconst globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & {\n _sentryRewriteFramesDistDir?: string;\n _sentryRelease?: string;\n _sentryRewritesTunnelPath?: string;\n};\n\n/** Inits the Sentry NextJS SDK on the Edge Runtime. */\nexport function init(options: VercelEdgeOptions = {}): void {\n registerSpanErrorInstrumentation();\n\n if (isBuild()) {\n return;\n }\n\n if (!DEBUG_BUILD && options.debug) {\n // eslint-disable-next-line no-console\n console.warn(\n '[@sentry/nextjs] You have enabled `debug: true`, but Sentry debug logging was removed from your bundle (likely via `withSentryConfig({ disableLogger: true })` / `webpack.treeshake.removeDebugLogging: true`). Set that option to `false` to see Sentry debug output.',\n );\n }\n\n const customDefaultIntegrations = getDefaultIntegrations(options);\n\n // This value is injected at build time, based on the output directory specified in the build config. Though a default\n // is set there, we set it here as well, just in case something has gone wrong with the injection.\n const distDirName = process.env._sentryRewriteFramesDistDir || globalWithInjectedValues._sentryRewriteFramesDistDir;\n\n if (distDirName) {\n customDefaultIntegrations.push(distDirRewriteFramesIntegration({ distDirName }));\n }\n\n // Detect if running on OpenNext/Cloudflare\n const isRunningOnCloudflare = isCloudflareWaitUntilAvailable();\n\n const opts: VercelEdgeOptions = {\n defaultIntegrations: customDefaultIntegrations,\n release: process.env._sentryRelease || globalWithInjectedValues._sentryRelease,\n ...options,\n // Override runtime to 'cloudflare' when running on OpenNext/Cloudflare\n ...(isRunningOnCloudflare && { runtime: { name: 'cloudflare' } }),\n };\n\n // Use appropriate SDK metadata based on the runtime environment\n if (isRunningOnCloudflare) {\n applySdkMetadata(opts, 'nextjs', ['nextjs', 'cloudflare']);\n } else {\n applySdkMetadata(opts, 'nextjs', ['nextjs', 'vercel-edge']);\n }\n\n const client = vercelEdgeInit(opts);\n\n client?.on('spanStart', span => {\n const spanAttributes = spanToJSON(span).data;\n const rootSpan = getRootSpan(span);\n const isRootSpan = span === rootSpan;\n\n dropMiddlewareTunnelRequests(span, spanAttributes);\n\n // Mark all spans generated by Next.js as 'auto'\n if (spanAttributes?.['next.span_type'] !== undefined) {\n span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto');\n }\n\n // Make sure middleware spans get the right op\n if (spanAttributes?.['next.span_type'] === 'Middleware.execute') {\n span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'http.server.middleware');\n span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'url');\n }\n\n // We want to fork the isolation scope for incoming requests\n if (spanAttributes?.[ATTR_NEXT_SPAN_TYPE] === 'BaseServer.handleRequest' && isRootSpan) {\n const scopes = getCapturedScopesOnSpan(span);\n\n const isolationScope = (scopes.isolationScope || getIsolationScope()).clone();\n const scope = scopes.scope || getCurrentScope();\n\n const currentScopesPointer = getScopesFromContext(context.active());\n if (currentScopesPointer) {\n currentScopesPointer.isolationScope = isolationScope;\n }\n\n setCapturedScopesOnSpan(span, scope, isolationScope);\n }\n\n if (isRootSpan) {\n // todo: check if we can set request headers for edge on sdkProcessingMetadata\n const headers = getIsolationScope().getScopeData().sdkProcessingMetadata?.normalizedRequest?.headers;\n addHeadersAsAttributes(headers, rootSpan);\n }\n });\n\n // Use the preprocessEvent hook instead of an event processor, so that the users event processors receive the most\n // up-to-date value, but also so that the logic that detects changes to the transaction names to set the source to\n // \"custom\", doesn't trigger.\n client?.on('preprocessEvent', event => {\n // The otel auto inference will clobber the transaction name because the span has an http.target\n if (\n event.type === 'transaction' &&\n event.contexts?.trace?.data?.['next.span_type'] === 'Middleware.execute' &&\n event.contexts?.trace?.data?.['next.span_name']\n ) {\n if (event.transaction) {\n // Older nextjs versions pass the full url appended to the middleware name, which results in high cardinality transaction names.\n // We want to remove the url from the name here.\n const spanName = event.contexts.trace.data['next.span_name'];\n\n if (typeof spanName === 'string') {\n const match = spanName.match(/^middleware (GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS)/);\n if (match) {\n const normalizedName = `middleware ${match[1]}`;\n event.transaction = normalizedName;\n } else {\n event.transaction = stripUrlQueryAndFragment(event.contexts.trace.data['next.span_name']);\n }\n }\n }\n }\n\n setUrlProcessingMetadata(event);\n });\n\n client?.on('spanEnd', span => {\n if (span === getRootSpan(span)) {\n waitUntil(flushSafelyWithTimeout());\n }\n });\n\n getGlobalScope().addEventProcessor(\n Object.assign(\n (event => {\n // Filter transactions that we explicitly want to drop.\n if (event.type === 'transaction') {\n if (event.contexts?.trace?.data?.[TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION]) {\n return null;\n }\n\n return event;\n } else {\n return event;\n }\n }) satisfies EventProcessor,\n { id: 'NextLowQualityTransactionsFilter' },\n ),\n );\n\n try {\n // @ts-expect-error `process.turbopack` is a magic string that will be replaced by Next.js\n if (process.turbopack) {\n getGlobalScope().setTag('turbopack', true);\n }\n } catch {\n // Noop\n // The statement above can throw because process is not defined on the client\n }\n}\n\n/**\n * Just a passthrough in case this is imported from the client.\n */\nexport function withSentryConfig(exportedUserNextConfig: T): T {\n return exportedUserNextConfig;\n}\n"],"names":["vercelEdgeInit"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AACA;AACA;;AA0CA,MAAM,wBAAA,GAA2B;;AAIjC;;AAEA;AACO,SAAS,IAAI,CAAC,OAAO,GAAsB,EAAE,EAAQ;AAC5D,EAAE,gCAAgC,EAAE;;AAEpC,EAAE,IAAI,OAAO,EAAE,EAAE;AACjB,IAAI;AACJ,EAAE;;AAEF,EAAE,IAAI,CAAC,WAAA,IAAe,OAAO,CAAC,KAAK,EAAE;AACrC;AACA,IAAI,OAAO,CAAC,IAAI;AAChB,MAAM,wQAAwQ;AAC9Q,KAAK;AACL,EAAE;;AAEF,EAAE,MAAM,yBAAA,GAA4B,sBAAsB,CAAC,OAAO,CAAC;;AAEnE;AACA;AACA,EAAE,MAAM,WAAA,GAAc,OAAO,CAAC,GAAG,CAAC,2BAAA,IAA+B,wBAAwB,CAAC,2BAA2B;;AAErH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