{"version":3,"file":"instrumentContext.js","sources":["../../../src/utils/instrumentContext.ts"],"sourcesContent":["import { type DurableObjectState, type DurableObjectStorage, type ExecutionContext } from '@cloudflare/workers-types';\nimport { instrumentDurableObjectStorage } from '../instrumentations/instrumentDurableObjectStorage';\n\ntype ContextType = ExecutionContext | DurableObjectState;\ntype OverridesStore<T extends ContextType> = Map<keyof T, (...args: unknown[]) => unknown>;\n\n/**\n * Instruments an execution context or DurableObjectState with Sentry tracing.\n *\n * Creates a copy of the context that:\n * - Allows overriding of methods (e.g., waitUntil)\n * - For DurableObjectState: instruments storage operations (get, put, delete, list, etc.)\n *   to create Sentry spans automatically\n *\n * @param ctx - The execution context or DurableObjectState to instrument\n * @returns An instrumented copy of the context\n */\nexport function instrumentContext<T extends ContextType>(ctx: T): T {\n  if (!ctx) return ctx;\n\n  const overrides: OverridesStore<T> = new Map();\n  const contextPrototype = Object.getPrototypeOf(ctx);\n  const prototypeMethodNames = Object.getOwnPropertyNames(contextPrototype) as unknown as (keyof T)[];\n  const ownPropertyNames = Object.getOwnPropertyNames(ctx) as unknown as (keyof T)[];\n  const instrumented = new Set<unknown>(['constructor']);\n  const descriptors: PropertyDescriptorMap = [...ownPropertyNames, ...prototypeMethodNames].reduce(\n    (prevDescriptors, methodName) => {\n      if (instrumented.has(methodName)) return prevDescriptors;\n      if (typeof ctx[methodName] !== 'function') return prevDescriptors;\n      instrumented.add(methodName);\n      const overridableDescriptor = makeOverridableDescriptor(overrides, ctx, methodName);\n      return {\n        ...prevDescriptors,\n        [methodName]: overridableDescriptor,\n      };\n    },\n    {} as PropertyDescriptorMap,\n  );\n\n  // Check if this is a DurableObjectState context with a storage property\n  // If so, wrap the storage with instrumentation\n  if ('storage' in ctx && ctx.storage) {\n    const originalStorage = ctx.storage;\n    const waitUntil = 'waitUntil' in ctx && typeof ctx.waitUntil === 'function' ? ctx.waitUntil.bind(ctx) : undefined;\n    let instrumentedStorage: DurableObjectStorage | undefined;\n    descriptors.storage = {\n      configurable: true,\n      enumerable: true,\n      get: () => {\n        if (!instrumentedStorage) {\n          instrumentedStorage = instrumentDurableObjectStorage(originalStorage, waitUntil);\n        }\n        return instrumentedStorage;\n      },\n    };\n    // Expose the original uninstrumented storage for internal Sentry operations\n    // This avoids creating spans for internal storage operations\n    descriptors.originalStorage = {\n      configurable: true,\n      enumerable: false,\n      get: () => originalStorage,\n    };\n  }\n\n  return Object.create(ctx, descriptors);\n}\n\n/**\n * Creates a property descriptor that allows overriding of a method on the given context object.\n *\n * This descriptor supports property overriding with functions only. It delegates method calls to\n * the provided store if an override exists or to the original method on the context otherwise.\n *\n * @param {OverridesStore<ContextType>} store - The storage for overridden methods specific to the context type.\n * @param {ContextType} ctx - The context object that contains the method to be overridden.\n * @param {keyof ContextType} method - The method on the context object to create the overridable descriptor for.\n * @return {PropertyDescriptor} A property descriptor enabling the overriding of the specified method.\n */\nfunction makeOverridableDescriptor<T extends ContextType>(\n  store: OverridesStore<T>,\n  ctx: T,\n  method: keyof T,\n): PropertyDescriptor {\n  return {\n    configurable: true,\n    enumerable: true,\n    set: newValue => {\n      if (typeof newValue == 'function') {\n        store.set(method, newValue);\n        return;\n      }\n      Reflect.set(ctx, method, newValue);\n    },\n\n    get: () => {\n      if (store.has(method)) return store.get(method);\n      const methodFunction = Reflect.get(ctx, method);\n      if (typeof methodFunction !== 'function') return methodFunction;\n      // We should do bind() to make sure that the method is bound to the context object - otherwise it will not work\n      return methodFunction.bind(ctx);\n    },\n  };\n}\n"],"names":["instrumentDurableObjectStorage"],"mappings":";;;;AAMA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,iBAAiB,CAAwB,GAAG,EAAQ;AACpE,EAAE,IAAI,CAAC,GAAG,EAAE,OAAO,GAAG;;AAEtB,EAAE,MAAM,SAAS,GAAsB,IAAI,GAAG,EAAE;AAChD,EAAE,MAAM,mBAAmB,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC;AACrD,EAAE,MAAM,uBAA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