Skip to content

Resolve IndexedDB adapter globals from globalThis instead of window - #554

Open
raphyabak wants to merge 2 commits into
nextapps-de:masterfrom
raphyabak:fix/indexeddb-web-worker-globalthis
Open

Resolve IndexedDB adapter globals from globalThis instead of window#554
raphyabak wants to merge 2 commits into
nextapps-de:masterfrom
raphyabak:fix/indexeddb-web-worker-globalthis

Conversation

@raphyabak

Copy link
Copy Markdown

Summary

Fixes #546.

The IndexedDB persistent adapter resolves indexedDB/IDBTransaction/IDBKeyRange only from window.*, guarded by typeof window !== "undefined":

const IndexedDB = typeof window !== "undefined" && (
    window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB
);

Inside a Web Worker there is no window object, even though indexedDB is available there (via self/globalThis). The guard short-circuits to false, and later IndexedDB.open(...) throws TypeError: IndexedDB.open is not a function (surfaces as Bb.open is not a function in the minified bundle, per the issue).

Fix

Read these three globals from globalThis instead of window. globalThis resolves correctly on the main thread, inside Web Workers, and in Node, so no environment-detection branching is needed.

Test plan

  • Added test/db.indexeddb.js. Node has no window global either, which conveniently reproduces the exact broken precondition from the issue, so the test sets a fake globalThis.indexedDB, imports the adapter fresh, and asserts .open() calls through to it correctly.
  • Verified the regression: reverting the source change turns the new test from passing into TypeError: IndexedDB.open is not a function — the exact error reported in the issue.
  • Full suite passes locally: npx mocha test/*.js --exit — 130 passing, 1 pending (pre-existing, unrelated).

Fixes nextapps-de#532.

Document.search() with { merge: true } applies "limit" to each field's
search independently, then merges the per-field results by unique id.
When different fields match different, non-overlapping sets of ids, the
merged total can exceed "limit" by up to (number of fields x limit),
since nothing re-applies the cap after merging.

Capture the overall requested limit before per-field options can
override it, and stop merge_fields() once that many unique results have
been collected.
Fixes nextapps-de#546.

The IndexedDB persistent adapter resolved indexedDB/IDBTransaction/
IDBKeyRange only from window.*, guarded by typeof window !== "undefined".
Inside a Web Worker there is no window object, even though IndexedDB is
available there, so the guard short-circuits to false and the adapter
fails with "IndexedDB.open is not a function".

Read these from globalThis instead, which resolves correctly in the main
thread, Web Workers, and Node.
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.

IndexedDB adapter fails in Web Workers because distributed bundle resolves IndexedDB from window.indexedDB

1 participant