Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/cloudflare/src/durableobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,11 @@ function instrumentPrototypeRpcMethods(
}

const wrapped = createRpcPrototypeWrapper(methodName, descriptor.value as UncheckedMethod);
Object.defineProperty(prototype, methodName, { ...descriptor, value: wrapped });

try {
Object.defineProperty(prototype, methodName, { ...descriptor, value: wrapped });
} catch {}

// Only the wrapper is marked, not the original method: `wrapMethodWithSentry` resolves
// through the same global map and must not resolve the original to this wrapper,
// which would recurse.
Expand Down
37 changes: 37 additions & 0 deletions packages/cloudflare/test/durableobject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,43 @@ describe('instrumentDurableObjectWithSentry', () => {
expect(obj.rpcMethod()).toBe('result');
});

it('skips non-configurable prototype methods instead of failing construction', () => {
const testClass = class {
sealedMethod() {
return 'sealed-result';
}

rpcMethod() {
return 'rpc-result';
}
};
Object.defineProperty(testClass.prototype, 'sealedMethod', {
value: testClass.prototype.sealedMethod,
writable: false,
enumerable: false,
configurable: false,
});
const originalSealedMethod = testClass.prototype.sealedMethod;

const instrumented = instrumentDurableObjectWithSentry(
vi.fn().mockReturnValue({ enableRpcTracePropagation: true }),
testClass as any,
);

let obj: any;
expect(() => {
obj = Reflect.construct(instrumented, []);
}).not.toThrow();

// The non-configurable method keeps its original (unwrapped) implementation
expect(testClass.prototype.sealedMethod).toBe(originalSealedMethod);
expect(obj.sealedMethod()).toBe('sealed-result');

// Other methods on the same prototype are still wrapped
expect(getInstrumented(obj.rpcMethod)).toBeTruthy();
expect(obj.rpcMethod()).toBe('rpc-result');
});

it('does not wrap Object.prototype methods as RPC methods', () => {
const testClass = class {
rpcMethod() {
Expand Down
Loading