import { setAsyncLocalStorageAsyncContextStrategy } from '../async.js'; import { markAsInstrumented } from '../instrument.js'; import { getFinalOptions } from '../options.js'; import { instrumentContext } from '../utils/instrumentContext.js'; import { extractRpcMeta } from '../utils/rpcMeta.js'; import { wrapMethodWithSentry } from '../wrapMethodWithSentry.js'; import { instrumentEnv } from './worker/instrumentEnv.js'; import { instrumentWorkerEntrypointFetch } from './worker/instrumentFetch.js'; import { instrumentWorkerEntrypointQueue } from './worker/instrumentQueue.js'; import { instrumentWorkerEntrypointScheduled } from './worker/instrumentScheduled.js'; import { instrumentWorkerEntrypointTail } from './worker/instrumentTail.js'; const WORKER_ENTRYPOINT_ORIGIN = "auto.faas.cloudflare.worker_entrypoint"; const RESERVED_METHODS = /* @__PURE__ */ new Set([ "connect", "constructor", "dup", "email", "fetch", "queue", "scheduled", "tail", "tailStream", "test", "trace" ]); function isPrototypeMethod(instance, prop, method) { let prototype = Object.getPrototypeOf(instance); while (prototype && prototype !== Object.prototype) { const descriptor = Object.getOwnPropertyDescriptor(prototype, prop); if (descriptor) { return descriptor.value === method; } prototype = Object.getPrototypeOf(prototype); } return false; } function bindToInstance(instance, proxy, method) { return new Proxy(method, { apply(target, thisArg, args) { return Reflect.apply(target, thisArg === proxy ? instance : thisArg, args); } }); } function instrumentMethod(instance, proxy, prop, method, options, context) { const ownDescriptor = Object.getOwnPropertyDescriptor(instance, prop); if (ownDescriptor && "value" in ownDescriptor && !ownDescriptor.configurable && !ownDescriptor.writable) { return method; } const boundMethod = bindToInstance(instance, proxy, method); if (typeof prop !== "string" || RESERVED_METHODS.has(prop) || !isPrototypeMethod(instance, prop, method)) { return boundMethod; } const captureMethod = wrapMethodWithSentry( { options, context, spanOp: "rpc", origin: WORKER_ENTRYPOINT_ORIGIN }, boundMethod, void 0, true ); if (!options.enableRpcTracePropagation) { return captureMethod; } const tracedMethod = wrapMethodWithSentry( { options, context, spanName: prop, spanOp: "rpc", origin: WORKER_ENTRYPOINT_ORIGIN }, boundMethod, void 0, true ); return (...args) => { const { rpcMeta } = extractRpcMeta(args); return rpcMeta ? tracedMethod.call(proxy, ...args) : captureMethod.call(proxy, ...args); }; } function instrumentWorkerEntrypoint(optionsCallback, WorkerEntrypointClass) { setAsyncLocalStorageAsyncContextStrategy(); const InstrumentedClass = new Proxy(WorkerEntrypointClass, { construct(target, [ctx, env]) { const context = instrumentContext(ctx); const options = getFinalOptions(optionsCallback(env), env); const instrumentedEnv = instrumentEnv(env, options); const obj = new target(context, instrumentedEnv); if ("ctx" in obj) { Object.defineProperty(obj, "ctx", { value: context, writable: true, enumerable: true, configurable: true }); } if ("env" in obj) { Object.defineProperty(obj, "env", { value: instrumentedEnv, writable: true, enumerable: true, configurable: true }); } Object.defineProperty(obj, "__SENTRY_CONTEXT__", { value: context, enumerable: false, writable: false, configurable: false }); Object.defineProperty(obj, "__SENTRY_OPTIONS__", { value: options, enumerable: false, writable: false, configurable: false }); const entrypoint = obj; instrumentWorkerEntrypointFetch(entrypoint, options, context); instrumentWorkerEntrypointScheduled(entrypoint, options, context); instrumentWorkerEntrypointQueue(entrypoint, options, context); instrumentWorkerEntrypointTail(entrypoint, options, context); const methodCache = /* @__PURE__ */ new Map(); const proxy = new Proxy(obj, { get(instance, prop) { const value = Reflect.get(instance, prop, instance); if (typeof value !== "function") { return value; } const method = value; const cached = methodCache.get(prop); if (cached?.source === method) { return cached.wrapped; } const wrapped = instrumentMethod(instance, proxy, prop, method, options, context); methodCache.set(prop, { source: method, wrapped }); return wrapped; } }); return proxy; } }); markAsInstrumented(InstrumentedClass); return InstrumentedClass; } export { instrumentWorkerEntrypoint }; //# sourceMappingURL=instrumentWorkerEntrypoint.js.map