{"version":3,"file":"instrumentFetch.js","sources":["../../../../src/instrumentations/worker/instrumentFetch.ts"],"sourcesContent":["import type { ExportedHandler } from '@cloudflare/workers-types';\nimport type { env as cloudflareEnv, WorkerEntrypoint } from 'cloudflare:workers';\nimport type { CloudflareOptions } from '../../client';\nimport { ensureInstrumented } from '../../instrument';\nimport { getFinalOptions } from '../../options';\nimport { wrapRequestHandler } from '../../request';\nimport { instrumentContext } from '../../utils/instrumentContext';\nimport { instrumentEnv } from './instrumentEnv';\n\n/**\n * Instruments a fetch handler for ExportedHandler (env/ctx come from args).\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function instrumentExportedHandlerFetch<T extends ExportedHandler<any, any, any>>(\n  handler: T,\n  optionsCallback: (env: typeof cloudflareEnv) => CloudflareOptions | undefined,\n): void {\n  if (!('fetch' in handler) || typeof handler.fetch !== 'function') {\n    return;\n  }\n\n  handler.fetch = ensureInstrumented(\n    handler.fetch,\n    original =>\n      new Proxy(original, {\n        apply(target, thisArg, args: Parameters<NonNullable<T['fetch']>>) {\n          const [request, env, ctx] = args;\n          const context = instrumentContext(ctx);\n          const options = getFinalOptions(optionsCallback(env), env);\n          args[1] = instrumentEnv(env, options);\n          args[2] = context;\n\n          return wrapRequestHandler({ options, request, context }, () => target.apply(thisArg, args));\n        },\n      }),\n  );\n}\n\n/**\n * Instruments a fetch method for WorkerEntrypoint (options/context already available).\n */\nexport function instrumentWorkerEntrypointFetch<T extends WorkerEntrypoint>(\n  instance: T,\n  options: CloudflareOptions,\n  context: ExecutionContext,\n): void {\n  if (!instance.fetch) {\n    return;\n  }\n\n  const original = instance.fetch.bind(instance);\n  instance.fetch = new Proxy(original, {\n    apply(target, thisArg, args: [Request]) {\n      const [request] = args;\n\n      return wrapRequestHandler({ options, request, context }, () => Reflect.apply(target, thisArg, args));\n    },\n  });\n}\n"],"names":["ensureInstrumented","request","instrumentContext","options","getFinalOptions","instrumentEnv","wrapRequestHandler"],"mappings":";;;;;;;;AASA;AACA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