@sentry/nextjs
Version:
Official Sentry SDK for Next.js
82 lines (78 loc) • 2.85 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const core = require('@sentry/core');
const responseEnd = require('./utils/responseEnd.js');
const tunnelPathnameMatch = require('./utils/tunnelPathnameMatch.js');
function wrapMiddlewareWithSentry(middleware) {
return new Proxy(middleware, {
apply: async (wrappingTarget, thisArg, args) => {
const tunnelRoute = "_sentryRewritesTunnelPath" in globalThis ? globalThis._sentryRewritesTunnelPath : void 0;
if (tunnelRoute && typeof tunnelRoute === "string") {
const req = args[0];
if (req instanceof Request) {
const url = new URL(req.url);
const isTunnelRequest = tunnelPathnameMatch.isPathnameUnderSentryTunnelRoute(url.pathname, tunnelRoute);
if (isTunnelRequest) {
return new Response(null, {
status: 200,
headers: {
"x-middleware-next": "1"
}
});
}
}
}
return core.withIsolationScope((isolationScope) => {
const req = args[0];
const currentScope = core.getCurrentScope();
let spanName;
let spanSource;
if (req instanceof Request) {
isolationScope.setSDKProcessingMetadata({
normalizedRequest: core.winterCGRequestToRequestData(req)
});
spanName = `middleware ${req.method}`;
spanSource = "url";
} else {
spanName = "middleware";
spanSource = "component";
}
currentScope.setTransactionName(spanName);
const runMiddleware = () => core.handleCallbackErrors(
() => wrappingTarget.apply(thisArg, args),
(error) => {
core.captureException(error, {
mechanism: {
type: "auto.function.nextjs.wrap_middleware",
handled: false
}
});
},
() => {
responseEnd.waitUntil(responseEnd.flushSafelyWithTimeout());
}
);
const activeSpan = core.getActiveSpan();
if (activeSpan) {
const rootSpan = core.getRootSpan(activeSpan);
if (rootSpan) {
core.setCapturedScopesOnSpan(rootSpan, currentScope, isolationScope);
}
return runMiddleware();
}
return core.startSpan(
{
name: spanName,
op: "http.server.middleware",
attributes: {
[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: spanSource,
[core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: "auto.function.nextjs.wrap_middleware"
}
},
runMiddleware
);
});
}
});
}
exports.wrapMiddlewareWithSentry = wrapMiddlewareWithSentry;
//# sourceMappingURL=wrapMiddlewareWithSentry.js.map