From 50121255a1b6729c2af420d4195ea77bbca50c43 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Sat, 19 Oct 2024 01:01:48 +0200 Subject: [PATCH 01/11] fix(browser): Remove `browserPerformanceTimeOrigin` side effects --- .../browser-utils/src/metrics/browserMetrics.ts | 15 ++++++++------- packages/browser-utils/src/metrics/cls.ts | 2 +- packages/browser-utils/src/metrics/inp.ts | 4 ++-- packages/browser/src/profiling/utils.ts | 6 +++--- .../src/tracing/browserTracingIntegration.ts | 3 ++- packages/browser/src/tracing/request.ts | 4 ++-- .../src/util/createPerformanceEntries.ts | 2 +- packages/utils/src/time.ts | 12 ++++++++++-- 8 files changed, 29 insertions(+), 19 deletions(-) diff --git a/packages/browser-utils/src/metrics/browserMetrics.ts b/packages/browser-utils/src/metrics/browserMetrics.ts index 3b98558f2728..0e0d2aefcbe0 100644 --- a/packages/browser-utils/src/metrics/browserMetrics.ts +++ b/packages/browser-utils/src/metrics/browserMetrics.ts @@ -79,7 +79,7 @@ interface StartTrackingWebVitalsOptions { */ export function startTrackingWebVitals({ recordClsStandaloneSpans }: StartTrackingWebVitalsOptions): () => void { const performance = getBrowserPerformanceAPI(); - if (performance && browserPerformanceTimeOrigin) { + if (performance && browserPerformanceTimeOrigin()) { // @ts-expect-error we want to make sure all of these are available, even if TS is sure they are if (performance.mark) { WINDOW.performance.mark('sentry-tracing-init'); @@ -109,7 +109,7 @@ export function startTrackingLongTasks(): void { return; } for (const entry of entries) { - const startTime = msToSec((browserPerformanceTimeOrigin as number) + entry.startTime); + const startTime = msToSec((browserPerformanceTimeOrigin() as number) + entry.startTime); const duration = msToSec(entry.duration); const span = startInactiveSpan({ @@ -143,7 +143,7 @@ export function startTrackingLongAnimationFrames(): void { continue; } - const startTime = msToSec((browserPerformanceTimeOrigin as number) + entry.startTime); + const startTime = msToSec((browserPerformanceTimeOrigin() as number) + entry.startTime); const duration = msToSec(entry.duration); const attributes: SpanAttributes = { @@ -189,7 +189,7 @@ export function startTrackingInteractions(): void { } for (const entry of entries) { if (entry.name === 'click') { - const startTime = msToSec((browserPerformanceTimeOrigin as number) + entry.startTime); + const startTime = msToSec((browserPerformanceTimeOrigin() as number) + entry.startTime); const duration = msToSec(entry.duration); const spanOptions: StartSpanOptions & Required> = { @@ -255,7 +255,7 @@ function _trackFID(): () => void { return; } - const timeOrigin = msToSec(browserPerformanceTimeOrigin as number); + const timeOrigin = msToSec(browserPerformanceTimeOrigin() as number); const startTime = msToSec(entry.startTime); DEBUG_BUILD && logger.log('[Measurements] Adding FID'); _measurements['fid'] = { value: metric.value, unit: 'millisecond' }; @@ -286,13 +286,14 @@ interface AddPerformanceEntriesOptions { /** Add performance related spans to a transaction */ export function addPerformanceEntries(span: Span, options: AddPerformanceEntriesOptions): void { const performance = getBrowserPerformanceAPI(); - if (!performance || !WINDOW.performance.getEntries || !browserPerformanceTimeOrigin) { + const origin = browserPerformanceTimeOrigin(); + if (!performance || !WINDOW.performance.getEntries || !origin) { // Gatekeeper if performance API not available return; } DEBUG_BUILD && logger.log('[Tracing] Adding & adjusting spans using Performance API'); - const timeOrigin = msToSec(browserPerformanceTimeOrigin); + const timeOrigin = msToSec(origin); const performanceEntries = performance.getEntries(); diff --git a/packages/browser-utils/src/metrics/cls.ts b/packages/browser-utils/src/metrics/cls.ts index e1d13286f5f9..0654379d0909 100644 --- a/packages/browser-utils/src/metrics/cls.ts +++ b/packages/browser-utils/src/metrics/cls.ts @@ -84,7 +84,7 @@ export function trackClsAsStandaloneSpan(): void { function sendStandaloneClsSpan(clsValue: number, entry: LayoutShift | undefined, pageloadSpanId: string) { DEBUG_BUILD && logger.log(`Sending CLS span (${clsValue})`); - const startTime = msToSec((browserPerformanceTimeOrigin || 0) + (entry?.startTime || 0)); + const startTime = msToSec((browserPerformanceTimeOrigin() || 0) + (entry?.startTime || 0)); const routeName = getCurrentScope().getScopeData().transactionName; const name = entry ? htmlTreeAsString(entry.sources[0]?.node) : 'Layout shift'; diff --git a/packages/browser-utils/src/metrics/inp.ts b/packages/browser-utils/src/metrics/inp.ts index 5814b139bd2d..c767fbc0ea5d 100644 --- a/packages/browser-utils/src/metrics/inp.ts +++ b/packages/browser-utils/src/metrics/inp.ts @@ -26,7 +26,7 @@ const INTERACTIONS_SPAN_MAP = new Map(); */ export function startTrackingINP(): () => void { const performance = getBrowserPerformanceAPI(); - if (performance && browserPerformanceTimeOrigin) { + if (performance && browserPerformanceTimeOrigin()) { const inpCallback = _trackINP(); return (): void => { @@ -83,7 +83,7 @@ function _trackINP(): () => void { const interactionType = INP_ENTRY_MAP[entry.name]; /** Build the INP span, create an envelope from the span, and then send the envelope */ - const startTime = msToSec((browserPerformanceTimeOrigin as number) + entry.startTime); + const startTime = msToSec((browserPerformanceTimeOrigin() as number) + entry.startTime); const duration = msToSec(metric.value); const activeSpan = getActiveSpan(); const rootSpan = activeSpan ? getRootSpan(activeSpan) : undefined; diff --git a/packages/browser/src/profiling/utils.ts b/packages/browser/src/profiling/utils.ts index 48e87450b0e8..b84c67f834db 100644 --- a/packages/browser/src/profiling/utils.ts +++ b/packages/browser/src/profiling/utils.ts @@ -251,9 +251,9 @@ export function convertJSSelfProfileToSampledFormat(input: JSSelfProfile): Profi // when that happens, we need to ensure we are correcting the profile timings so the two timelines stay in sync. // Since JS self profiling time origin is always initialized to performance.timeOrigin, we need to adjust for // the drift between the SDK selected value and our profile time origin. - const origin = - typeof performance.timeOrigin === 'number' ? performance.timeOrigin : browserPerformanceTimeOrigin || 0; - const adjustForOriginChange = origin - (browserPerformanceTimeOrigin || origin); + const perfOrigin = browserPerformanceTimeOrigin(); + const origin = typeof performance.timeOrigin === 'number' ? performance.timeOrigin : perfOrigin || 0; + const adjustForOriginChange = origin - (perfOrigin || origin); input.samples.forEach((jsSample, i) => { // If sample has no stack, add an empty sample diff --git a/packages/browser/src/tracing/browserTracingIntegration.ts b/packages/browser/src/tracing/browserTracingIntegration.ts index b3f8d9abdba8..ea8a92ab62a1 100644 --- a/packages/browser/src/tracing/browserTracingIntegration.ts +++ b/packages/browser/src/tracing/browserTracingIntegration.ts @@ -362,10 +362,11 @@ export const browserTracingIntegration = ((_options: Partial { diff --git a/packages/utils/src/time.ts b/packages/utils/src/time.ts index df67493888e3..2335aefd43bd 100644 --- a/packages/utils/src/time.ts +++ b/packages/utils/src/time.ts @@ -73,11 +73,16 @@ export const timestampInSeconds = createUnixTimestampInSecondsFunc(); */ export let _browserPerformanceTimeOriginMode: string; +let cachedbrowserPerformanceTimeOrigin: { value: number | undefined } | undefined; + /** * The number of milliseconds since the UNIX epoch. This value is only usable in a browser, and only when the * performance API is available. */ -export const browserPerformanceTimeOrigin = ((): number | undefined => { +export function browserPerformanceTimeOrigin(): number | undefined { + if (cachedbrowserPerformanceTimeOrigin) { + return cachedbrowserPerformanceTimeOrigin.value; + } // Unfortunately browsers may report an inaccurate time origin data, through either performance.timeOrigin or // performance.timing.navigationStart, which results in poor results in performance data. We only treat time origin // data as reliable if they are within a reasonable threshold of the current time. @@ -123,5 +128,8 @@ export const browserPerformanceTimeOrigin = ((): number | undefined => { // Either both timeOrigin and navigationStart are skewed or neither is available, fallback to Date. _browserPerformanceTimeOriginMode = 'dateNow'; + + cachedbrowserPerformanceTimeOrigin = { value: dateNow }; + return dateNow; -})(); +} From 1b694e03bc0bf89963351abe33d255edcc823acc Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Sat, 19 Oct 2024 01:13:36 +0200 Subject: [PATCH 02/11] oops --- .../src/client/routing/appRouterRoutingInstrumentation.ts | 3 ++- .../src/client/routing/pagesRouterRoutingInstrumentation.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/nextjs/src/client/routing/appRouterRoutingInstrumentation.ts b/packages/nextjs/src/client/routing/appRouterRoutingInstrumentation.ts index 741849c481ab..ec30435dc93e 100644 --- a/packages/nextjs/src/client/routing/appRouterRoutingInstrumentation.ts +++ b/packages/nextjs/src/client/routing/appRouterRoutingInstrumentation.ts @@ -11,10 +11,11 @@ export const INCOMPLETE_APP_ROUTER_INSTRUMENTATION_TRANSACTION_NAME = 'incomplet /** Instruments the Next.js app router for pageloads. */ export function appRouterInstrumentPageLoad(client: Client): void { + const origin = browserPerformanceTimeOrigin(); startBrowserTracingPageLoadSpan(client, { name: WINDOW.location.pathname, // pageload should always start at timeOrigin (and needs to be in s, not ms) - startTime: browserPerformanceTimeOrigin ? browserPerformanceTimeOrigin / 1000 : undefined, + startTime: origin ? origin / 1000 : undefined, attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload', [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.nextjs.app_router_instrumentation', diff --git a/packages/nextjs/src/client/routing/pagesRouterRoutingInstrumentation.ts b/packages/nextjs/src/client/routing/pagesRouterRoutingInstrumentation.ts index f6906a566050..4ae86ffcf65d 100644 --- a/packages/nextjs/src/client/routing/pagesRouterRoutingInstrumentation.ts +++ b/packages/nextjs/src/client/routing/pagesRouterRoutingInstrumentation.ts @@ -108,12 +108,13 @@ export function pagesRouterInstrumentPageLoad(client: Client): void { const { route, params, sentryTrace, baggage } = extractNextDataTagInformation(); const name = route || globalObject.location.pathname; + const origin = browserPerformanceTimeOrigin(); startBrowserTracingPageLoadSpan( client, { name, // pageload should always start at timeOrigin (and needs to be in s, not ms) - startTime: browserPerformanceTimeOrigin ? browserPerformanceTimeOrigin / 1000 : undefined, + startTime: origin ? origin / 1000 : undefined, attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload', [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.nextjs.pages_router_instrumentation', From 1f5a9ede5dec133bef74d2044df2b02f4a3dea47 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Sat, 19 Oct 2024 01:25:52 +0200 Subject: [PATCH 03/11] See if this is better --- packages/utils/src/time.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/utils/src/time.ts b/packages/utils/src/time.ts index 2335aefd43bd..17613556a545 100644 --- a/packages/utils/src/time.ts +++ b/packages/utils/src/time.ts @@ -73,16 +73,9 @@ export const timestampInSeconds = createUnixTimestampInSecondsFunc(); */ export let _browserPerformanceTimeOriginMode: string; -let cachedbrowserPerformanceTimeOrigin: { value: number | undefined } | undefined; +let cachedTimeOrigin: { v: number | undefined } | undefined; -/** - * The number of milliseconds since the UNIX epoch. This value is only usable in a browser, and only when the - * performance API is available. - */ -export function browserPerformanceTimeOrigin(): number | undefined { - if (cachedbrowserPerformanceTimeOrigin) { - return cachedbrowserPerformanceTimeOrigin.value; - } +function getBrowserTimeOrigin(): number | undefined { // Unfortunately browsers may report an inaccurate time origin data, through either performance.timeOrigin or // performance.timing.navigationStart, which results in poor results in performance data. We only treat time origin // data as reliable if they are within a reasonable threshold of the current time. @@ -129,7 +122,17 @@ export function browserPerformanceTimeOrigin(): number | undefined { // Either both timeOrigin and navigationStart are skewed or neither is available, fallback to Date. _browserPerformanceTimeOriginMode = 'dateNow'; - cachedbrowserPerformanceTimeOrigin = { value: dateNow }; - return dateNow; } + +/** + * The number of milliseconds since the UNIX epoch. This value is only usable in a browser, and only when the + * performance API is available. + */ +export function browserPerformanceTimeOrigin(): number | undefined { + if (!cachedTimeOrigin) { + cachedTimeOrigin = { v: getBrowserTimeOrigin() }; + } + + return cachedTimeOrigin.v; +} From 1280d803927ccb6c16135aeb92ac96bc47b50d5d Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Sat, 19 Oct 2024 01:32:18 +0200 Subject: [PATCH 04/11] Fix tests --- packages/browser/src/tracing/browserTracingIntegration.ts | 2 +- .../test/unit/util/createPerformanceEntry.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/browser/src/tracing/browserTracingIntegration.ts b/packages/browser/src/tracing/browserTracingIntegration.ts index ea8a92ab62a1..3d63f654c1b0 100644 --- a/packages/browser/src/tracing/browserTracingIntegration.ts +++ b/packages/browser/src/tracing/browserTracingIntegration.ts @@ -362,7 +362,7 @@ export const browserTracingIntegration = ((_options: Partial ({ ...(await vi.importActual('@sentry/utils')), - browserPerformanceTimeOrigin: new Date('2023-01-01').getTime(), + browserPerformanceTimeOrigin: () => new Date('2023-01-01').getTime(), })); import { WINDOW } from '../../../src/constants'; From 818a500bb4cef7dd60ef8f7a7957ff7648070573 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Sat, 19 Oct 2024 01:54:24 +0200 Subject: [PATCH 05/11] another fix --- .../ember/addon/instance-initializers/sentry-performance.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/ember/addon/instance-initializers/sentry-performance.ts b/packages/ember/addon/instance-initializers/sentry-performance.ts index f8d03a025217..d84522e53036 100644 --- a/packages/ember/addon/instance-initializers/sentry-performance.ts +++ b/packages/ember/addon/instance-initializers/sentry-performance.ts @@ -366,8 +366,9 @@ function _instrumentInitialLoad(config: EmberSentryConfig): void { return; } + const origin = browserPerformanceTimeOrigin(); // Split performance check in two so clearMarks still happens even if timeOrigin isn't available. - if (!HAS_PERFORMANCE_TIMING || browserPerformanceTimeOrigin === undefined) { + if (!HAS_PERFORMANCE_TIMING || origin === undefined) { return; } const measureName = '@sentry/ember:initial-load'; @@ -383,7 +384,7 @@ function _instrumentInitialLoad(config: EmberSentryConfig): void { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const measure = measures[0]!; - const startTime = (measure.startTime + browserPerformanceTimeOrigin) / 1000; + const startTime = (measure.startTime + origin) / 1000; const endTime = startTime + measure.duration / 1000; startInactiveSpan({ From f1e186c119e1649e7bc7ca4dea43ee1da706e194 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Sat, 19 Oct 2024 11:54:15 +0200 Subject: [PATCH 06/11] simplify more --- packages/utils/src/time.ts | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/packages/utils/src/time.ts b/packages/utils/src/time.ts index 17613556a545..a7d131b8c22c 100644 --- a/packages/utils/src/time.ts +++ b/packages/utils/src/time.ts @@ -69,21 +69,21 @@ function createUnixTimestampInSecondsFunc(): () => number { export const timestampInSeconds = createUnixTimestampInSecondsFunc(); /** - * Internal helper to store what is the source of browserPerformanceTimeOrigin below. For debugging only. + * Cached result of getBrowserTimeOrigin. */ -export let _browserPerformanceTimeOriginMode: string; +let cachedTimeOrigin: [number | undefined, string] | undefined; -let cachedTimeOrigin: { v: number | undefined } | undefined; - -function getBrowserTimeOrigin(): number | undefined { +/** + * Gets the time origin and the mode used to determine it. + */ +function getBrowserTimeOrigin(): [number | undefined, string] { // Unfortunately browsers may report an inaccurate time origin data, through either performance.timeOrigin or // performance.timing.navigationStart, which results in poor results in performance data. We only treat time origin // data as reliable if they are within a reasonable threshold of the current time. const { performance } = GLOBAL_OBJ as typeof GLOBAL_OBJ & Window; if (!performance || !performance.now) { - _browserPerformanceTimeOriginMode = 'none'; - return undefined; + return [undefined, 'none']; } const threshold = 3600 * 1000; @@ -111,18 +111,14 @@ function getBrowserTimeOrigin(): number | undefined { if (timeOriginIsReliable || navigationStartIsReliable) { // Use the more reliable time origin if (timeOriginDelta <= navigationStartDelta) { - _browserPerformanceTimeOriginMode = 'timeOrigin'; - return performance.timeOrigin; + return [performance.timeOrigin, 'timeOrigin']; } else { - _browserPerformanceTimeOriginMode = 'navigationStart'; - return navigationStart; + return [navigationStart, 'navigationStart']; } } // Either both timeOrigin and navigationStart are skewed or neither is available, fallback to Date. - _browserPerformanceTimeOriginMode = 'dateNow'; - - return dateNow; + return [dateNow, 'dateNow']; } /** @@ -131,8 +127,8 @@ function getBrowserTimeOrigin(): number | undefined { */ export function browserPerformanceTimeOrigin(): number | undefined { if (!cachedTimeOrigin) { - cachedTimeOrigin = { v: getBrowserTimeOrigin() }; + cachedTimeOrigin = getBrowserTimeOrigin(); } - return cachedTimeOrigin.v; + return cachedTimeOrigin[0]; } From 2ec6738fdddeb35d40f1c070bc4437044cf6e465 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Tue, 26 Nov 2024 10:42:05 -0800 Subject: [PATCH 07/11] Fix linting --- packages/browser-utils/src/metrics/cls.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/browser-utils/src/metrics/cls.ts b/packages/browser-utils/src/metrics/cls.ts index bf5685bfa384..46572241e9fb 100644 --- a/packages/browser-utils/src/metrics/cls.ts +++ b/packages/browser-utils/src/metrics/cls.ts @@ -88,7 +88,7 @@ export function trackClsAsStandaloneSpan(): void { function sendStandaloneClsSpan(clsValue: number, entry: LayoutShift | undefined, pageloadSpanId: string) { DEBUG_BUILD && logger.log(`Sending CLS span (${clsValue})`); - const startTime = msToSec((browserPerformanceTimeOrigin() || 0) + (entry?.startTime || 0)); + const startTime = msToSec((browserPerformanceTimeOrigin() || 0) + ((entry && entry.startTime) || 0)); const routeName = getCurrentScope().getScopeData().transactionName; const name = entry ? htmlTreeAsString(entry.sources[0] && entry.sources[0].node) : 'Layout shift'; From e2332c72d85882f3603083081aa2dce820690b84 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Fri, 13 Dec 2024 10:28:11 -0500 Subject: [PATCH 08/11] fix dodgy merge --- packages/browser-utils/src/metrics/browserMetrics.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/browser-utils/src/metrics/browserMetrics.ts b/packages/browser-utils/src/metrics/browserMetrics.ts index 47288dc5e427..fff27bff75b8 100644 --- a/packages/browser-utils/src/metrics/browserMetrics.ts +++ b/packages/browser-utils/src/metrics/browserMetrics.ts @@ -311,6 +311,7 @@ export function addPerformanceEntries(span: Span, options: AddPerformanceEntries const { op, start_timestamp: transactionStartTime } = spanToJSON(span); + performanceEntries.slice(_performanceCursor).forEach(entry => { const startTime = msToSec(entry.startTime); const duration = msToSec( // Inexplicably, Chrome sometimes emits a negative duration. We need to work around this. From 3f51a8bf1170df6e62bc2612512ce70606b38595 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Fri, 10 Jan 2025 22:04:51 +0100 Subject: [PATCH 09/11] oh linting --- packages/browser-utils/src/metrics/cls.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/browser-utils/src/metrics/cls.ts b/packages/browser-utils/src/metrics/cls.ts index 6cc6ec7338ec..f9a6c662d79d 100644 --- a/packages/browser-utils/src/metrics/cls.ts +++ b/packages/browser-utils/src/metrics/cls.ts @@ -90,7 +90,7 @@ export function trackClsAsStandaloneSpan(): void { function sendStandaloneClsSpan(clsValue: number, entry: LayoutShift | undefined, pageloadSpanId: string) { DEBUG_BUILD && logger.log(`Sending CLS span (${clsValue})`); - const startTime = msToSec((browserPerformanceTimeOrigin() || 0) + ((entry?.startTime) || 0)); + const startTime = msToSec((browserPerformanceTimeOrigin() || 0) + (entry?.startTime || 0)); const routeName = getCurrentScope().getScopeData().transactionName; const name = entry ? htmlTreeAsString(entry.sources[0]?.node) : 'Layout shift'; From b790c3517f64606a08c73caa974c36a60fae8a01 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Fri, 10 Jan 2025 22:33:24 +0100 Subject: [PATCH 10/11] More linting! --- .../wrapServerEntryWithDynamicImport.ts | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/packages/solidstart/src/config/wrapServerEntryWithDynamicImport.ts b/packages/solidstart/src/config/wrapServerEntryWithDynamicImport.ts index 6d069220e1ae..8f8c7a59f1f4 100644 --- a/packages/solidstart/src/config/wrapServerEntryWithDynamicImport.ts +++ b/packages/solidstart/src/config/wrapServerEntryWithDynamicImport.ts @@ -53,7 +53,7 @@ export function wrapServerEntryWithDynamicImport(config: WrapServerEntryPluginOp return { id: source, moduleSideEffects: true }; } - if (additionalImports && additionalImports.includes(source)) { + if (additionalImports?.includes(source)) { // When importing additional imports like "import-in-the-middle/hook.mjs" in the returned code of the `load()` function below: // By setting `moduleSideEffects` to `true`, the import is added to the bundle, although nothing is imported from it // By importing "import-in-the-middle/hook.mjs", we can make sure this file is included, as not all node builders are including files imported with `module.register()`. @@ -70,7 +70,7 @@ export function wrapServerEntryWithDynamicImport(config: WrapServerEntryPluginOp const resolution = await this.resolve(source, importer, options); // If it cannot be resolved or is external, just return it so that Rollup can display an error - if (!resolution || (resolution && resolution.external)) return resolution; + if (!resolution || resolution?.external) return resolution; const moduleInfo = await this.load(resolution); @@ -146,23 +146,21 @@ export function extractFunctionReexportQueryParameters(query: string): { wrap: s const wrapMatch = query.match(wrapRegex); const reexportMatch = query.match(reexportRegex); - const wrap = - wrapMatch && wrapMatch[1] - ? wrapMatch[1] - .split(',') - .filter(param => param !== '') - // Sanitize, as code could be injected with another rollup plugin - .map((str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')) - : []; - - const reexport = - reexportMatch && reexportMatch[1] - ? reexportMatch[1] - .split(',') - .filter(param => param !== '' && param !== 'default') - // Sanitize, as code could be injected with another rollup plugin - .map((str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')) - : []; + const wrap = wrapMatch?.[1] + ? wrapMatch[1] + .split(',') + .filter(param => param !== '') + // Sanitize, as code could be injected with another rollup plugin + .map((str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')) + : []; + + const reexport = reexportMatch?.[1] + ? reexportMatch[1] + .split(',') + .filter(param => param !== '' && param !== 'default') + // Sanitize, as code could be injected with another rollup plugin + .map((str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')) + : []; return { wrap, reexport }; } From 11d2986fff6b5a4a73d0746d43a2d92737cc67e7 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Mon, 13 Jan 2025 20:22:15 +0100 Subject: [PATCH 11/11] Fix test mock --- packages/replay-internal/test/integration/flush.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/replay-internal/test/integration/flush.test.ts b/packages/replay-internal/test/integration/flush.test.ts index 5de390581790..843fe6ceedfd 100644 --- a/packages/replay-internal/test/integration/flush.test.ts +++ b/packages/replay-internal/test/integration/flush.test.ts @@ -93,7 +93,7 @@ describe('Integration | flush', () => { mockEventBufferFinish.mockClear(); Object.defineProperty(SentryUtils, 'browserPerformanceTimeOrigin', { - value: BASE_TIMESTAMP, + value: () => BASE_TIMESTAMP, writable: true, }); }); @@ -107,7 +107,7 @@ describe('Integration | flush', () => { writable: true, }); Object.defineProperty(SentryUtils, 'browserPerformanceTimeOrigin', { - value: prevBrowserPerformanceTimeOrigin, + value: () => prevBrowserPerformanceTimeOrigin, writable: true, }); });