import { instrumentDurableObjectStorage } from '../instrumentations/instrumentDurableObjectStorage.js'; function instrumentContext(ctx) { if (!ctx) return ctx; const overrides = /* @__PURE__ */ new Map(); const contextPrototype = Object.getPrototypeOf(ctx); const prototypeMethodNames = Object.getOwnPropertyNames(contextPrototype); const ownPropertyNames = Object.getOwnPropertyNames(ctx); const instrumented = /* @__PURE__ */ new Set(["constructor"]); const descriptors = [...ownPropertyNames, ...prototypeMethodNames].reduce( (prevDescriptors, methodName) => { if (instrumented.has(methodName)) return prevDescriptors; if (typeof ctx[methodName] !== "function") return prevDescriptors; instrumented.add(methodName); const overridableDescriptor = makeOverridableDescriptor(overrides, ctx, methodName); return { ...prevDescriptors, [methodName]: overridableDescriptor }; }, {} ); if ("storage" in ctx && ctx.storage) { const originalStorage = ctx.storage; const waitUntil = "waitUntil" in ctx && typeof ctx.waitUntil === "function" ? ctx.waitUntil.bind(ctx) : void 0; let instrumentedStorage; descriptors.storage = { configurable: true, enumerable: true, get: () => { if (!instrumentedStorage) { instrumentedStorage = instrumentDurableObjectStorage(originalStorage, waitUntil); } return instrumentedStorage; } }; descriptors.originalStorage = { configurable: true, enumerable: false, get: () => originalStorage }; } return Object.create(ctx, descriptors); } function makeOverridableDescriptor(store, ctx, method) { return { configurable: true, enumerable: true, set: (newValue) => { if (typeof newValue == "function") { store.set(method, newValue); return; } Reflect.set(ctx, method, newValue); }, get: () => { if (store.has(method)) return store.get(method); const methodFunction = Reflect.get(ctx, method); if (typeof methodFunction !== "function") return methodFunction; return methodFunction.bind(ctx); } }; } export { instrumentContext }; //# sourceMappingURL=instrumentContext.js.map