Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const core = require('@sentry/core'); const httpServer = require('./integrations/httpServer.js'); const flush = require('./flush.js'); const scopeUtils = require('./scope-utils.js'); const sdk = require('./sdk.js'); const streaming = require('./utils/streaming.js'); function getRequestErrorMechanismType(context) { return context && "storage" in context ? "auto.faas.cloudflare.durable_object" : "auto.http.cloudflare"; } function wrapRequestHandler(wrapperOptions, handler) { return core.withIsolationScope(async (isolationScope) => { const { options, request, captureErrors = true } = wrapperOptions; const context = wrapperOptions.context; const waitUntil = context ? flush.getOriginalWaitUntil(context)?.bind(context) : void 0; const errorMechanismType = getRequestErrorMechanismType(context); const client = sdk.init({ ...options, ctx: context }); isolationScope.setClient(client); const urlObject = core.parseStringToURLObject(request.url); const [name, attributes] = core.getHttpSpanDetailsFromUrlObject(urlObject, "server", "auto.http.cloudflare", request); const contentLength = request.headers.get("content-length"); if (contentLength) { attributes["http.request.body.size"] = parseInt(contentLength, 10); } const userAgentHeader = request.headers.get("user-agent"); if (userAgentHeader) { attributes["user_agent.original"] = userAgentHeader; } Object.assign( attributes, core.httpHeadersToSpanAttributes( core.winterCGHeadersToDict(request.headers), core.getClient()?.getDataCollectionOptions() ?? false ) ); attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_OP] = "http.server"; scopeUtils.addCloudResourceContext(isolationScope); scopeUtils.addRequest(isolationScope, request); if (request.cf) { scopeUtils.addCultureContext(isolationScope, request.cf); if (typeof request.cf.httpProtocol === "string") { attributes["network.protocol.name"] = request.cf.httpProtocol; } } if (request.method === "OPTIONS" || request.method === "HEAD") { try { return await handler(); } catch (e) { if (captureErrors) { core.captureException(e, { mechanism: { handled: false, type: errorMechanismType } }); } throw e; } finally { waitUntil?.(flush.flushAndDispose(client)); } } if (client) { await httpServer.captureIncomingRequestBody(client, request); } return core.continueTrace( { sentryTrace: request.headers.get("sentry-trace") || "", baggage: request.headers.get("baggage") }, () => { return core.startSpanManual({ name, attributes }, async (span) => { let res; try { res = await handler(); core.setHttpStatus(span, res.status); } catch (e) { span.end(); if (captureErrors) { core.captureException(e, { mechanism: { handled: false, type: errorMechanismType } }); } waitUntil?.(flush.flushAndDispose(client)); throw e; } const classification = streaming.classifyResponseStreaming(res); if (classification.isStreaming && res.body) { try { let ended = false; const endSpanOnce = () => { if (ended) return; ended = true; span.end(); waitUntil?.(flush.flushAndDispose(client)); }; const transform = new TransformStream({ flush() { endSpanOnce(); }, cancel() { endSpanOnce(); } }); return new Response(res.body.pipeThrough(transform), { status: res.status, statusText: res.statusText, headers: res.headers }); } catch { span.end(); waitUntil?.(flush.flushAndDispose(client)); return res; } } span.end(); if (res.status === 101) { waitUntil?.(client?.flush(2e3)); } else { waitUntil?.(flush.flushAndDispose(client)); } return res; }); } ); }); } exports.wrapRequestHandler = wrapRequestHandler; //# sourceMappingURL=request.js.map