import { ensureInstrumented } from '../../instrument.js'; import { getFinalOptions } from '../../options.js'; import { wrapRequestHandler } from '../../request.js'; import { instrumentContext } from '../../utils/instrumentContext.js'; import { instrumentEnv } from './instrumentEnv.js'; function instrumentExportedHandlerFetch(handler, optionsCallback) { if (!("fetch" in handler) || typeof handler.fetch !== "function") { return; } handler.fetch = ensureInstrumented( handler.fetch, (original) => new Proxy(original, { apply(target, thisArg, args) { const [request, env, ctx] = args; if (request.method === "OPTIONS" || request.method === "HEAD") { return target.apply(thisArg, args); } const context = instrumentContext(ctx); const options = getFinalOptions(optionsCallback(env), env); args[1] = instrumentEnv(env, options); args[2] = context; return wrapRequestHandler({ options, request, context }, () => target.apply(thisArg, args)); } }) ); } function instrumentWorkerEntrypointFetch(instance, options, context) { if (!instance.fetch) { return; } const original = instance.fetch.bind(instance); instance.fetch = new Proxy(original, { apply(target, thisArg, args) { const [request] = args; if (request.method === "OPTIONS" || request.method === "HEAD") { return Reflect.apply(target, thisArg, args); } return wrapRequestHandler({ options, request, context }, () => Reflect.apply(target, thisArg, args)); } }); } export { instrumentExportedHandlerFetch, instrumentWorkerEntrypointFetch }; //# sourceMappingURL=instrumentFetch.js.map