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
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ type StorageMethod = (typeof STORAGE_METHODS_TO_INSTRUMENT)[number];
*/
export function instrumentDurableObjectStorage(storage: DurableObjectStorage): DurableObjectStorage {
return new Proxy(storage, {
get(target, prop, receiver) {
const original = Reflect.get(target, prop, receiver);
get(target, prop, _receiver) {
// Use `target` as the receiver instead of the proxy (`_receiver`).
// Native workerd getters (e.g., `storage.sql`) validate `this` via
// internal slots. Passing the proxy as receiver breaks that check,
// causing "Illegal invocation: function called with incorrect `this`
// reference" errors.
const original = Reflect.get(target, prop, target);

if (typeof original !== 'function') {
return original;
Expand Down
19 changes: 19 additions & 0 deletions packages/cloudflare/test/instrumentDurableObjectStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,25 @@ describe('instrumentDurableObjectStorage', () => {
});
});

describe('native getter preservation', () => {
it('preserves native getter `this` binding through the proxy', () => {
// Private fields simulate workerd's native brand check —
// accessing #sqlInstance on wrong `this` throws TypeError,
// like workerd's "Illegal invocation".
class BrandCheckedStorage {
#sqlInstance = { exec: () => {} };
get sql() {
return this.#sqlInstance;
}
}

const storage = new BrandCheckedStorage();
const instrumented = instrumentDurableObjectStorage(storage as any);

expect(() => (instrumented as any).sql).not.toThrow();
});
});

describe('error handling', () => {
it('propagates errors from storage operations', async () => {
const mockStorage = createMockStorage();
Expand Down
Loading