UNPKG

@sentry/remix

Version:
96 lines (93 loc) • 2.52 kB
import * as diagnosticsChannel from 'node:diagnostics_channel'; import { createMultiMatcher } from 'remix/route-pattern/match'; import { consoleSandbox } from '@sentry/core'; import { remixV3Channels } from '@sentry/server-utils/orchestrion/config'; import { captureRequestError } from './errorFilter.js'; import { sentryRemixMiddleware } from './middleware.js'; const NOOP = () => { }; const INJECTED = /* @__PURE__ */ Symbol.for("SentryRemixV3Injected"); let subscribed = false; function instrumentRemixV3() { if (subscribed || !diagnosticsChannel.tracingChannel) { return; } subscribed = true; subscribeToCreateRouter(); subscribeToCreateRequestListener(); } function subscribeToCreateRouter() { diagnosticsChannel.tracingChannel(remixV3Channels.REMIX_V3_CREATE_ROUTER).subscribe({ start(data) { try { injectRouterMiddleware(ensureOptions(data.arguments)); } catch { } }, end: NOOP, asyncStart: NOOP, asyncEnd: NOOP, error: NOOP }); } function subscribeToCreateRequestListener() { diagnosticsChannel.tracingChannel( remixV3Channels.REMIX_V3_CREATE_REQUEST_LISTENER ).subscribe({ start(data) { try { injectOnError(ensureOptions(data.arguments, 1)); } catch { } }, end: NOOP, asyncStart: NOOP, asyncEnd: NOOP, error: NOOP }); } function injectOnError(raw) { if (!raw || markInjected(raw)) { return; } const options = raw; const appOnError = options.onError; options.onError = (error) => { captureRequestError(error, void 0, "auto.http.remix_v3.on_error"); if (appOnError) { return appOnError(error); } consoleSandbox(() => { console.error(error); }); return void 0; }; } function ensureOptions(args, index = 0) { const existing = args[index]; if (existing === void 0 || existing === null) { const created = {}; args[index] = created; return created; } return typeof existing === "object" ? existing : void 0; } function markInjected(raw) { const marker = raw; if (marker[INJECTED]) { return true; } marker[INJECTED] = true; return false; } function injectRouterMiddleware(raw) { if (!raw || markInjected(raw)) { return; } const options = raw; const matcher = options.matcher ?? createMultiMatcher(); options.matcher = matcher; options.middleware = [sentryRemixMiddleware(matcher), ...options.middleware ?? []]; } export { instrumentRemixV3 }; //# sourceMappingURL=instrument.js.map