Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const async = require('../async.js'); const instrument = require('../instrument.js'); const options = require('../options.js'); const instrumentContext = require('../utils/instrumentContext.js'); const rpcMeta = require('../utils/rpcMeta.js'); const wrapMethodWithSentry = require('../wrapMethodWithSentry.js'); const instrumentEnv = require('./worker/instrumentEnv.js'); const instrumentFetch = require('./worker/instrumentFetch.js'); const instrumentQueue = require('./worker/instrumentQueue.js'); const instrumentScheduled = require('./worker/instrumentScheduled.js'); const instrumentTail = require('./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.wrapMethodWithSentry( { options, context, spanOp: "rpc", origin: WORKER_ENTRYPOINT_ORIGIN }, boundMethod, void 0, true ); if (!options.enableRpcTracePropagation) { return captureMethod; } const tracedMethod = wrapMethodWithSentry.wrapMethodWithSentry( { options, context, spanName: prop, spanOp: "rpc", origin: WORKER_ENTRYPOINT_ORIGIN }, boundMethod, void 0, true ); return (...args) => { const { rpcMeta: rpcMeta$1 } = rpcMeta.extractRpcMeta(args); return rpcMeta$1 ? tracedMethod.call(proxy, ...args) : captureMethod.call(proxy, ...args); }; } function instrumentWorkerEntrypoint(optionsCallback, WorkerEntrypointClass) { async.setAsyncLocalStorageAsyncContextStrategy(); const InstrumentedClass = new Proxy(WorkerEntrypointClass, { construct(target, [ctx, env]) { const context = instrumentContext.instrumentContext(ctx); const options$1 = options.getFinalOptions(optionsCallback(env), env); const instrumentedEnv = instrumentEnv.instrumentEnv(env, options$1); 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$1, enumerable: false, writable: false, configurable: false }); const entrypoint = obj; instrumentFetch.instrumentWorkerEntrypointFetch(entrypoint, options$1, context); instrumentScheduled.instrumentWorkerEntrypointScheduled(entrypoint, options$1, context); instrumentQueue.instrumentWorkerEntrypointQueue(entrypoint, options$1, context); instrumentTail.instrumentWorkerEntrypointTail(entrypoint, options$1, 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$1, context); methodCache.set(prop, { source: method, wrapped }); return wrapped; } }); return proxy; } }); instrument.markAsInstrumented(InstrumentedClass); return InstrumentedClass; } exports.instrumentWorkerEntrypoint = instrumentWorkerEntrypoint; //# sourceMappingURL=instrumentWorkerEntrypoint.js.map