Skip to content

fix(dexie-cloud): fix 'Illegal invocation' in blobResolveMiddleware cursor proxy - #2291

Closed
liz709 wants to merge 1 commit into
masterfrom
liz/fix-blob-cursor-illegal-invocation
Closed

fix(dexie-cloud): fix 'Illegal invocation' in blobResolveMiddleware cursor proxy#2291
liz709 wants to merge 1 commit into
masterfrom
liz/fix-blob-cursor-illegal-invocation

Conversation

@liz709

@liz709 liz709 commented Apr 3, 2026

Copy link
Copy Markdown
Contributor

Problem

createBlobResolvingCursor() in blobResolveMiddleware wraps the IDB cursor via Object.create(cursor, { value, start }) but did not override key or primaryKey. Accessing these properties on the wrapped cursor falls through the prototype chain to the native IDBCursorWithValue getters, which require this to be the actual native IDB cursor object. Since this is a plain proxy object, the browser throws:

TypeError: Illegal invocation
    at IDBCursorWithValue.get [as key/primaryKey]
    at dexie-cloud-addon.js:...

Why does this happen? The observabilityMiddleware (level 0) only wraps cursors for secondary index queries — not primary key queries. For primary key queries, blobResolve (level 2) receives the cursor with no intermediate key/primaryKey getter override, so property lookup hits the native IDB getter directly.

This is a regression triggered by the level change in #2268 (moving blobResolve above the cache). At level -2 (below observability), blobResolve always received cursors already wrapped by observability. At level 2 (above observability), blobResolve can receive unwrapped native IDB cursors on primary key paths.

Fix

Add explicit key and primaryKey getters to the Object.create() property descriptor map. They close over the original cursor reference, matching the pattern used by Dexie's own ProxyCursor helper and the observabilityMiddleware/virtual-index-middleware:

key: {
  get() { return cursor.key; },
  enumerable: true,
},
primaryKey: {
  get() { return cursor.primaryKey; },
  enumerable: true,
},

What's NOT changed

Testing

Build passes (pnpm build), unit tests pass (npm test in dexie-cloud-common), CodeRabbit: no findings.

Fixes: bug reported in Discord where app crashed on load with 'Illegal invocation' on dexie-cloud-addon 4.4.6–4.4.9 in Chrome on primary-key cursor queries.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed cursor operation errors that occurred in specific query contexts, resolving "Illegal invocation" TypeErrors during certain data retrieval operations.

…esolveMiddleware

Without explicit key/primaryKey property descriptors in Object.create(),
accessing these properties on the wrapped cursor falls through the prototype
chain to the native IDBCursorWithValue getters, which require `this` to be
the actual native IDB cursor object. When `this` is a plain proxy object,
the browser throws 'TypeError: Illegal invocation'.

This crash occurs specifically on primary-key-index queries because the
observability middleware (level 0) only wraps cursors on secondary indexes,
leaving primary-key cursors unwrapped — so blobResolve's Object.create()
wraps the native IDB cursor directly.

Fix: add `key` and `primaryKey` getters that close over the original cursor
reference, matching the pattern used by Dexie's own proxy-cursor helpers
and the observability/virtual-index middlewares.

Reported in: https://github.com/dexie/Dexie.js/issues/...
@coderabbitai

coderabbitai Bot commented Apr 3, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ec90f72f-c43e-4040-820d-094cec2955ca

📥 Commits

Reviewing files that changed from the base of the PR and between 2d92a26 and c52f589.

📒 Files selected for processing (1)
  • addons/dexie-cloud/src/middlewares/blobResolveMiddleware.ts

📝 Walkthrough

Walkthrough

The createBlobResolvingCursor() function in the blob resolve middleware is updated to explicitly add key and primaryKey property accessors on the wrapped cursor. These accessors delegate to the underlying cursor, preventing prototype chain inheritance issues that cause "Illegal invocation" errors in specific query contexts.

Changes

Cohort / File(s) Summary
Blob Cursor Wrapping
addons/dexie-cloud/src/middlewares/blobResolveMiddleware.ts
Added explicit key and primaryKey property getters to the wrapped cursor object to override native IDBCursorWithValue prototype properties and avoid "Illegal invocation" TypeErrors when wrapping is otherwise unsafe.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • dfahlander

Poem

🐰 A cursor wrapped with care, so fine,
Its key and primaryKey in line,
No longer shall the prototype betray,
With explicit getters all the way! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately summarizes the main change: fixing an 'Illegal invocation' error in the blobResolveMiddleware cursor proxy, which is the primary objective of this PR.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch liz/fix-blob-cursor-illegal-invocation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dfahlander

dfahlander commented Apr 13, 2026

Copy link
Copy Markdown
Collaborator

Fixed and merged in #2293 (released in dexie-cloud-addon@4.4.10)

@dfahlander dfahlander closed this Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants