dexie-cloud-addon 4.4.10 → 4.4.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,7 +8,7 @@
8
8
  *
9
9
  * ==========================================================================
10
10
  *
11
- * Version 4.4.10, Sat Apr 04 2026
11
+ * Version 4.4.12, Mon May 25 2026
12
12
  *
13
13
  * https://dexie.org
14
14
  *
@@ -13623,7 +13623,7 @@
13623
13623
  *
13624
13624
  * ==========================================================================
13625
13625
  *
13626
- * Version 4.4.0, Sat Apr 04 2026
13626
+ * Version 4.4.0, Mon May 25 2026
13627
13627
  *
13628
13628
  * https://dexie.org
13629
13629
  *
@@ -15231,22 +15231,208 @@
15231
15231
  }
15232
15232
 
15233
15233
  /**
15234
- * Deduplicates in-flight blob downloads.
15234
+ * BlobSavingQueue - Queues resolved blobs for saving back to IndexedDB.
15235
15235
  *
15236
- * Both the blob-resolve middleware and the eager blob downloader may
15237
- * try to fetch the same blob concurrently. This tracker ensures each
15238
- * unique blob ref is only downloaded once — subsequent requests for
15239
- * the same ref piggyback on the existing promise.
15236
+ * This is an internal collaborator of BlobDownloadTracker and is not
15237
+ * intended to be used directly by middleware or other code. See
15238
+ * BlobDownloadTracker.enqueueSave().
15240
15239
  *
15241
- * Instantiate once per DexieCloudDB.
15240
+ * Uses setTimeout(fn, 0) instead of queueMicrotask to completely isolate
15241
+ * from Dexie's Promise.PSD context. This prevents the save operation
15242
+ * from inheriting any ongoing transaction.
15243
+ *
15244
+ * Each blob is saved atomically using downCore transaction with the specific
15245
+ * keyPath to avoid race conditions with other property changes.
15246
+ */
15247
+ class BlobSavingQueue {
15248
+ constructor(db, onPersisted) {
15249
+ this.queue = [];
15250
+ this.isProcessing = false;
15251
+ this.drainResolvers = [];
15252
+ this.db = db;
15253
+ this.onPersisted = onPersisted;
15254
+ }
15255
+ /**
15256
+ * Queue a resolved blob for saving.
15257
+ * Only the specific blob property will be updated atomically.
15258
+ */
15259
+ saveBlobs(tableName, primaryKey, resolvedBlobs) {
15260
+ this.queue.push({
15261
+ tableName,
15262
+ primaryKey,
15263
+ resolvedBlobs,
15264
+ });
15265
+ this.startConsumer();
15266
+ }
15267
+ /**
15268
+ * Returns a promise that resolves when the queue is empty AND no item
15269
+ * is currently being processed. Used by callers that need to know when
15270
+ * all previously enqueued saves have been persisted to IndexedDB before
15271
+ * making decisions based on the on-disk state (e.g., the eager blob
15272
+ * downloader looping over `_hasBlobRefs=1` rows in chunks).
15273
+ *
15274
+ * Note: New work enqueued AFTER drain() is called does NOT extend the
15275
+ * wait. Callers that race against concurrent producers should treat the
15276
+ * returned promise as "queue was empty at some point after this call".
15277
+ */
15278
+ drain() {
15279
+ if (!this.isProcessing && this.queue.length === 0) {
15280
+ return Promise.resolve();
15281
+ }
15282
+ return new Promise((resolve) => {
15283
+ this.drainResolvers.push(resolve);
15284
+ });
15285
+ }
15286
+ /**
15287
+ * Start the consumer if not already processing.
15288
+ * Uses setTimeout(fn, 0) to completely break out of any
15289
+ * Dexie transaction context (Promise.PSD).
15290
+ */
15291
+ startConsumer() {
15292
+ if (this.isProcessing)
15293
+ return;
15294
+ this.isProcessing = true;
15295
+ // Use setTimeout to completely isolate from Dexie's PSD context
15296
+ // queueMicrotask would risk inheriting the current transaction
15297
+ setTimeout(() => {
15298
+ this.processQueue();
15299
+ }, 0);
15300
+ }
15301
+ /**
15302
+ * Process all queued blobs.
15303
+ * Runs in a completely isolated context (no inherited transaction).
15304
+ * Uses atomic updates to avoid race conditions.
15305
+ */
15306
+ processQueue() {
15307
+ const item = this.queue.shift();
15308
+ if (!item) {
15309
+ this.isProcessing = false;
15310
+ // Fire any pending drain() waiters. New saveBlobs() calls that
15311
+ // arrive after this point will start a fresh processing cycle
15312
+ // and have their own drain() semantics.
15313
+ const resolvers = this.drainResolvers;
15314
+ if (resolvers.length > 0) {
15315
+ this.drainResolvers = [];
15316
+ for (const resolve of resolvers)
15317
+ resolve();
15318
+ }
15319
+ return;
15320
+ }
15321
+ // Atomic update of just the blob property
15322
+ this.db
15323
+ .transaction('rw', item.tableName, (tx) => {
15324
+ const trans = tx.idbtrans;
15325
+ trans.disableChangeTracking = true; // Don't regard this as a change for sync purposes
15326
+ trans.disableAccessControl = true; // Bypass any access control checks since this is an internal operation
15327
+ trans.disableBlobResolve = true; // Custom flag to skip blob resolve middleware during this transaction
15328
+ const updateSpec = {};
15329
+ for (const blob of item.resolvedBlobs) {
15330
+ updateSpec[blob.keyPath] = blob.data;
15331
+ }
15332
+ tx.table(item.tableName).update(item.primaryKey, (obj) => {
15333
+ // Check that object still has the same unresolved blob refs before applying update (i.e. it hasn't been modified since we read it)
15334
+ for (const blob of item.resolvedBlobs) {
15335
+ // Verify atomicity - none of the blob properties has been modified since we read it. If any of them was modified, skip updating this item to avoid overwriting user changes.
15336
+ const currentValue = Dexie.getByKeyPath(obj, blob.keyPath);
15337
+ if (currentValue === undefined) {
15338
+ // Blob property was removed - skip updating this blob
15339
+ continue;
15340
+ }
15341
+ if (!isBlobRef(currentValue)) {
15342
+ // Blob property was modified to a non-blob-ref value - skip updating this blob
15343
+ continue;
15344
+ }
15345
+ if (currentValue.ref !== blob.ref) {
15346
+ // Blob property was modified - skip updating this blob
15347
+ return; // Stop. Another items has been queued to fully fix the object.
15348
+ }
15349
+ Dexie.setByKeyPath(obj, blob.keyPath, blob.data);
15350
+ }
15351
+ delete obj._hasBlobRefs; // Clear the _hasBlobRefs marker if all refs was resolved.
15352
+ });
15353
+ // Note: we intentionally do NOT clear trans.mutatedParts here.
15354
+ // Letting the normal mutation signal through means the
15355
+ // blobProgress liveQuery (and any user-defined liveQuery that
15356
+ // depends on the resolved fields) wakes up and reflects progress
15357
+ // as blobs land in IndexedDB.
15358
+ })
15359
+ .catch((error) => {
15360
+ console.error(`Error saving resolved blobs on ${item.tableName}:${item.primaryKey}:`, error);
15361
+ })
15362
+ .finally(() => {
15363
+ // At this point, the transaction has completed (either successfully or with error),
15364
+ // and the blobs have been saved (or failed to save).
15365
+ // Notify the owner (BlobDownloadTracker) so it can release the
15366
+ // in-flight download cache entries for these refs. The cache was
15367
+ // kept alive until now to maximize reuse while the blob was still
15368
+ // in-flight (downloading or queued for save).
15369
+ this.onPersisted(item.resolvedBlobs.map((b) => b.ref));
15370
+ // Process next item in the queue
15371
+ return this.processQueue();
15372
+ });
15373
+ }
15374
+ }
15375
+
15376
+ /**
15377
+ * Owns the full lifecycle of downloaded blobs:
15378
+ * 1. Deduplicates concurrent downloads for the same ref.
15379
+ * 2. Bounds the number of concurrent network fetches (MAX_CONCURRENT)
15380
+ * so that ad-hoc reads can't starve the HTTP connection pool. Calls
15381
+ * beyond the cap queue in FIFO order as slots free. The slot is held
15382
+ * only for the duration of the fetch — NOT until persistence — to
15383
+ * avoid deadlocks when a single object contains more blob refs than
15384
+ * MAX_CONCURRENT (a sequential resolver would otherwise hold every
15385
+ * slot itself while waiting for the next).
15386
+ * 3. Keeps the in-flight promise alive after the network fetch completes,
15387
+ * until the blob has been persisted back to IndexedDB. This way,
15388
+ * readers that ask for the same ref while it is queued for saving
15389
+ * can piggyback on the existing promise instead of refetching.
15390
+ * In-flight membership and slot ownership are independent: a piggyback
15391
+ * reader consumes neither a slot nor extra memory beyond the existing
15392
+ * cached Uint8Array.
15393
+ * 4. Persists resolved blobs via an internal BlobSavingQueue, and
15394
+ * releases the in-flight entry when persistence completes.
15395
+ *
15396
+ * Both the blob-resolve middleware and the eager blob downloader use this
15397
+ * tracker. Instantiate once per DexieCloudDB.
15242
15398
  */
15399
+ /**
15400
+ * Maximum number of concurrent blob fetches.
15401
+ *
15402
+ * Historically 6 to match the HTTP/1.1 same-origin connection cap that
15403
+ * browsers enforce. With HTTP/2 (the typical transport for Dexie Cloud
15404
+ * today) many streams multiplex over a single TCP connection, so the
15405
+ * old cap is overly conservative. 10 is a modest bump that still keeps
15406
+ * memory pressure (in-flight Uint8Arrays) and server load bounded.
15407
+ * Can be made configurable via DexieCloudOptions if a real need arises.
15408
+ */
15409
+ const MAX_CONCURRENT = 10;
15243
15410
  class BlobDownloadTracker {
15244
15411
  constructor(db) {
15245
15412
  this.inFlight = new Map();
15413
+ this.activeFetches = 0;
15414
+ this.waiting = [];
15246
15415
  this.db = db;
15416
+ this.savingQueue = new BlobSavingQueue(db, (refs) => {
15417
+ // Called by the queue when a save transaction has completed
15418
+ // (regardless of success). Drop the in-flight cache entries now —
15419
+ // any future reader will go through IndexedDB instead.
15420
+ for (const ref of refs) {
15421
+ this.inFlight.delete(ref);
15422
+ }
15423
+ });
15247
15424
  }
15248
15425
  /**
15249
- * Download a blob, deduplicating concurrent requests for the same ref.
15426
+ * Download a blob, deduplicating concurrent requests for the same ref
15427
+ * and respecting the global fetch concurrency cap.
15428
+ *
15429
+ * Lifecycle:
15430
+ * - Slot is acquired before the fetch and released as soon as the
15431
+ * fetch settles (success or failure).
15432
+ * - The in-flight entry survives a successful fetch and lives on
15433
+ * until persistence completes (via enqueueSave) or releaseRefs
15434
+ * is called. On fetch failure, the entry is removed immediately
15435
+ * so a future call can retry.
15250
15436
  *
15251
15437
  * @param blobRef - The BlobRef to download
15252
15438
  * @param dbUrl - Base URL for the database (e.g., 'https://mydb.dexie.cloud')
@@ -15254,45 +15440,103 @@
15254
15440
  download(blobRef, dbUrl) {
15255
15441
  let promise = this.inFlight.get(blobRef.ref);
15256
15442
  if (!promise) {
15257
- promise = loadCachedAccessToken(this.db)
15258
- .then((accessToken) => {
15259
- // accessToken may be null for anonymous/unauthenticated users.
15260
- // Public realm blobs (rlm-public) are accessible without auth.
15261
- // downloadBlob will omit the Authorization header when token is null.
15262
- return downloadBlob(blobRef, dbUrl, accessToken);
15263
- })
15264
- .finally(() => this.inFlight.delete(blobRef.ref));
15265
- // When the promise settles (either fulfilled or rejected), remove it from the in-flight map
15443
+ promise = this.acquireSlot()
15444
+ .then(() => this.downloadBlob(blobRef, dbUrl).finally(() => this.releaseSlot()))
15445
+ .catch((err) => {
15446
+ // On error, remove immediately so a future call can retry.
15447
+ // (Slot already released by the .finally above.)
15448
+ this.inFlight.delete(blobRef.ref);
15449
+ throw err;
15450
+ });
15266
15451
  this.inFlight.set(blobRef.ref, promise);
15267
15452
  }
15268
15453
  return promise;
15269
15454
  }
15270
- }
15271
- /**
15272
- * Download blob data from server via proxy endpoint.
15273
- * Uses auth header for authentication (same as sync).
15274
- * When accessToken is null, the request is made without Authorization header —
15275
- * this allows downloading blobs from public realms (rlm-public) for
15276
- * unauthenticated users.
15277
- *
15278
- * @param blobRef - The BlobRef to download
15279
- * @param dbUrl - Base URL for the database (e.g., 'https://mydb.dexie.cloud')
15280
- * @param accessToken - Access token for authentication, or null for anonymous access
15281
- */
15282
- function downloadBlob(blobRef, dbUrl, accessToken) {
15283
- return __awaiter(this, void 0, void 0, function* () {
15284
- const downloadUrl = `${dbUrl}/blob/${blobRef.ref}`;
15285
- const headers = {};
15286
- if (accessToken) {
15287
- headers['Authorization'] = `Bearer ${accessToken}`;
15455
+ /**
15456
+ * Queue resolved blobs for persisting back to IndexedDB.
15457
+ * When the save transaction completes, the corresponding in-flight
15458
+ * entries are released.
15459
+ */
15460
+ enqueueSave(tableName, primaryKey, resolvedBlobs) {
15461
+ this.savingQueue.saveBlobs(tableName, primaryKey, resolvedBlobs);
15462
+ }
15463
+ /**
15464
+ * Wait until all previously enqueued saves have been persisted to
15465
+ * IndexedDB. Used by callers that need to make decisions based on
15466
+ * on-disk state — e.g., the eager downloader looping over rows with
15467
+ * `_hasBlobRefs=1` in chunks, where each iteration must see the
15468
+ * previous chunk's writes before re-querying.
15469
+ *
15470
+ * New saves enqueued AFTER drainPendingSaves() is called do NOT extend
15471
+ * the wait.
15472
+ */
15473
+ drainPendingSaves() {
15474
+ return this.savingQueue.drain();
15475
+ }
15476
+ /**
15477
+ * Release in-flight entries without going through the internal saving
15478
+ * queue. Used when the caller persists the blobs itself, or when no
15479
+ * primary key was available and the data won't be persisted at all.
15480
+ */
15481
+ releaseRefs(refs) {
15482
+ for (const ref of refs) {
15483
+ this.inFlight.delete(ref);
15288
15484
  }
15289
- const response = yield fetch(downloadUrl, { headers });
15290
- if (!response.ok) {
15291
- throw new Error(`Failed to download blob ${blobRef.ref}: ${response.status} ${response.statusText}`);
15485
+ }
15486
+ acquireSlot() {
15487
+ if (this.activeFetches < MAX_CONCURRENT) {
15488
+ this.activeFetches++;
15489
+ return Promise.resolve();
15292
15490
  }
15293
- const arrayBuffer = yield response.arrayBuffer();
15294
- return new Uint8Array(arrayBuffer);
15295
- });
15491
+ return new Promise((resolve) => {
15492
+ this.waiting.push(() => {
15493
+ this.activeFetches++;
15494
+ resolve();
15495
+ });
15496
+ });
15497
+ }
15498
+ releaseSlot() {
15499
+ this.activeFetches--;
15500
+ const next = this.waiting.shift();
15501
+ if (next)
15502
+ next();
15503
+ }
15504
+ /**
15505
+ * Download blob data from server via proxy endpoint.
15506
+ * Uses auth header for authentication (same as sync).
15507
+ * When accessToken is null, the request is made without Authorization header —
15508
+ * this allows downloading blobs from public realms (rlm-public) for
15509
+ * unauthenticated users.
15510
+ *
15511
+ * @param blobRef - The BlobRef to download
15512
+ * @param dbUrl - Base URL for the database (e.g., 'https://mydb.dexie.cloud')
15513
+ */
15514
+ downloadBlob(blobRef, dbUrl) {
15515
+ return __awaiter(this, void 0, void 0, function* () {
15516
+ const accessToken = yield loadCachedAccessToken(this.db);
15517
+ const downloadUrl = `${dbUrl}/blob/${blobRef.ref}`;
15518
+ const headers = {};
15519
+ if (accessToken) {
15520
+ // accessToken may be null for anonymous/unauthenticated users.
15521
+ // Public realm blobs (rlm-public) are accessible without auth.
15522
+ // downloadBlob will omit the Authorization header when token is null.
15523
+ headers['Authorization'] = `Bearer ${accessToken}`;
15524
+ }
15525
+ // cache: 'no-store' prevents the browser from storing this response in its
15526
+ // HTTP cache. The server sets a long Expires/Cache-Control header on blob
15527
+ // responses (blobs are immutable and content-addressed), which would
15528
+ // otherwise cause the browser to keep a copy in its disk cache in addition
15529
+ // to the copy we persist to IndexedDB — doubling storage for every blob.
15530
+ // Since we always persist to IndexedDB and subsequent reads go through
15531
+ // IndexedDB (never re-fetch), the browser cache copy is pure overhead.
15532
+ const response = yield fetch(downloadUrl, { headers, cache: 'no-store' });
15533
+ if (!response.ok) {
15534
+ throw new Error(`Failed to download blob ${blobRef.ref}: ${response.status} ${response.statusText}`);
15535
+ }
15536
+ const arrayBuffer = yield response.arrayBuffer();
15537
+ return new Uint8Array(arrayBuffer);
15538
+ });
15539
+ }
15296
15540
  }
15297
15541
 
15298
15542
  const wm$2 = new WeakMap();
@@ -15513,118 +15757,116 @@
15513
15757
  * Downloads unresolved blobs in the background when blobMode='eager'.
15514
15758
  * Called after sync completes to prefetch blobs for offline access.
15515
15759
  *
15760
+ * Strategy:
15761
+ * 1. Snapshot the primary keys of all rows currently flagged
15762
+ * `_hasBlobRefs=1` for each syncable table.
15763
+ * 2. Walk that key list in chunks via `bulkGet`. Each `bulkGet`
15764
+ * triggers the blob-resolve middleware, which does all the actual
15765
+ * work — downloading blobs (throttled and deduplicated by the
15766
+ * shared BlobDownloadTracker) and enqueueing them for persistence
15767
+ * via the internal save queue.
15768
+ *
15769
+ * This keeps a single, symmetric code path with normal application
15770
+ * reads, which is important when other middlewares are present
15771
+ * (e.g., a hypothetical encryption middleware): writes from the save
15772
+ * queue and reads from this loop both pass through the full middleware
15773
+ * stack, so on-disk representation stays consistent.
15774
+ *
15775
+ * Why a snapshot of primary keys (rather than re-querying the index)?
15776
+ * - Rows that get resolved by parallel application reads simply
15777
+ * disappear from the table contents we're about to re-fetch; the
15778
+ * middleware skips them since `_hasBlobRefs` is already cleared.
15779
+ * - Stuck rows (e.g., blob 404s) are naturally bypassed: we just
15780
+ * advance to the next chunk in the snapshot. No `seenKeys`
15781
+ * bookkeeping required.
15782
+ * - The snapshot is `string[]`-shaped for typical Dexie Cloud rows
15783
+ * (~36 bytes/UUID), so ~28K keys per MB. Acceptable for any
15784
+ * realistic dataset.
15785
+ *
15516
15786
  * Progress is tracked automatically via liveQuery in blobProgress.ts —
15517
15787
  * no manual progress reporting needed here.
15518
- */
15788
+ *
15789
+ * --- Throughput note ---
15790
+ * The chunk loop is sequential: bulkGet → wait for all downloads to
15791
+ * settle → next bulkGet. The save queue drains in the background and
15792
+ * does not block iteration (saves no longer need to be persisted before
15793
+ * the next iteration, since we don't re-query the index). For typical
15794
+ * blob sizes (10 KB – 10 MB) the network dominates total time. If
15795
+ * real-world profiling later shows the per-chunk fixed cost matters,
15796
+ * the next bulkGet could be kicked off in parallel with the current
15797
+ * one's middleware work — but we keep it simple until measurements
15798
+ * justify otherwise.
15799
+ */
15800
+ // One chunk = one full saturation of the tracker's concurrency semaphore.
15801
+ // Larger chunks would only buffer more downloaded Uint8Arrays in memory
15802
+ // while waiting for the save queue to persist them, without any throughput
15803
+ // benefit (the semaphore is the gate, not the bulkGet).
15804
+ const CHUNK_SIZE = MAX_CONCURRENT - 1; // Leave one slot for parallel app reads that might also trigger downloads
15519
15805
  /**
15520
15806
  * Download all unresolved blobs in the background.
15521
15807
  *
15522
15808
  * This is called when blobMode='eager' (default) after sync completes.
15523
- * BlobRef URLs are signed (SAS tokens) so no auth header needed.
15524
- *
15525
- * Each blob is saved atomically using Table.update() to avoid race conditions.
15526
15809
  */
15527
15810
  function downloadUnresolvedBlobs(db, downloading$, signal) {
15528
15811
  return __awaiter(this, void 0, void 0, function* () {
15529
- var _a;
15530
15812
  const debugLog = (msg) => console.debug(`[dexie-cloud] ${msg}`);
15531
15813
  debugLog('Eager download: Starting...');
15532
- // Scan for unresolved blobs
15533
- const syncedTables = getSyncableTables(db);
15534
- let hasWork = false;
15535
- for (const table of syncedTables) {
15536
- try {
15537
- const hasIndex = !!table.schema.idxByName['_hasBlobRefs'];
15538
- if (!hasIndex)
15539
- continue;
15540
- const count = yield table.where('_hasBlobRefs').equals(1).count();
15541
- if (count > 0) {
15542
- hasWork = true;
15543
- break;
15544
- }
15545
- }
15546
- catch (_b) {
15547
- // skip
15548
- }
15549
- }
15550
- if (!hasWork) {
15551
- debugLog('Eager download: No blobs remaining, exiting');
15552
- return;
15553
- }
15554
- setDownloadingState(downloading$, true);
15814
+ const syncedTables = getSyncableTables(db).filter((t) => t.schema.indexes.some((idx) => idx.name === '_hasBlobRefs'));
15815
+ let started = false;
15816
+ let totalProcessed = 0;
15555
15817
  try {
15556
- debugLog(`Eager download: Found ${syncedTables.length} syncable tables: ${syncedTables.map((t) => t.name).join(', ')}`);
15557
15818
  for (const table of syncedTables) {
15558
15819
  if (signal === null || signal === void 0 ? void 0 : signal.aborted)
15559
15820
  ;
15821
+ let keys;
15560
15822
  try {
15561
- // Check if table has _hasBlobRefs index
15562
- const hasIndex = table.schema.indexes.some((idx) => idx.name === '_hasBlobRefs');
15563
- if (!hasIndex)
15564
- continue;
15565
- // Query objects with _hasBlobRefs marker
15566
- const unresolvedObjects = yield table
15567
- .where('_hasBlobRefs')
15568
- .equals(1)
15569
- .toArray();
15570
- debugLog(`Eager download: Table ${table.name} has ${unresolvedObjects.length} unresolved objects`);
15571
- const databaseUrl = (_a = db.cloud.options) === null || _a === void 0 ? void 0 : _a.databaseUrl;
15572
- if (!databaseUrl)
15573
- throw new Error('Database URL is required to download blobs');
15574
- // Download up to MAX_CONCURRENT blobs in parallel
15575
- const MAX_CONCURRENT = 6;
15576
- const primaryKey = table.schema.primKey;
15577
- // Filter to actionable objects first
15578
- const pending = unresolvedObjects.filter((obj) => {
15579
- if (!hasUnresolvedBlobRefs(obj))
15580
- return false;
15581
- const key = primaryKey.keyPath
15582
- ? Dexie.getByKeyPath(obj, primaryKey.keyPath)
15583
- : undefined;
15584
- return key !== undefined;
15585
- });
15586
- // Process in parallel with concurrency limit
15587
- let i = 0;
15588
- const runNext = () => __awaiter(this, void 0, void 0, function* () {
15589
- while (i < pending.length) {
15590
- if (signal === null || signal === void 0 ? void 0 : signal.aborted)
15591
- ;
15592
- const obj = pending[i++];
15593
- const key = Dexie.getByKeyPath(obj, primaryKey.keyPath);
15594
- try {
15595
- // Refresh token per object — cheap (returns cached) but ensures
15596
- // we pick up renewed tokens during long download sessions.
15597
- const resolvedBlobs = [];
15598
- yield resolveAllBlobRefs(obj, databaseUrl, resolvedBlobs, '', new WeakMap(), db.blobDownloadTracker);
15599
- const updateSpec = {
15600
- _hasBlobRefs: undefined,
15601
- };
15602
- for (const blob of resolvedBlobs) {
15603
- updateSpec[blob.keyPath] = blob.data;
15604
- }
15605
- debugLog(`Eager download: Updating ${table.name}:${key} with ${resolvedBlobs.length} blobs`);
15606
- yield table.update(key, updateSpec);
15607
- // liveQuery in blobProgress.ts auto-detects this change
15608
- }
15609
- catch (err) {
15610
- console.error(`Failed to download blobs for ${table.name}:${key}:`, err);
15611
- }
15612
- }
15613
- });
15614
- // Launch up to MAX_CONCURRENT workers
15615
- const workers = [];
15616
- for (let w = 0; w < Math.min(MAX_CONCURRENT, pending.length); w++) {
15617
- workers.push(runNext());
15618
- }
15619
- yield Promise.all(workers);
15823
+ keys = yield table.where('_hasBlobRefs').equals(1).primaryKeys();
15620
15824
  }
15621
15825
  catch (err) {
15622
- // Table might not have _hasBlobRefs index or other issues - skip silently
15826
+ console.error(`Eager download: failed to list unresolved rows for ${table.name}:`, err);
15827
+ continue;
15828
+ }
15829
+ if (keys.length === 0)
15830
+ continue;
15831
+ if (!started) {
15832
+ setDownloadingState(downloading$, true);
15833
+ started = true;
15623
15834
  }
15835
+ debugLog(`Eager download: ${table.name} has ${keys.length} row(s)`);
15836
+ for (let i = 0; i < keys.length; i += CHUNK_SIZE) {
15837
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted)
15838
+ ;
15839
+ const slice = keys.slice(i, i + CHUNK_SIZE);
15840
+ try {
15841
+ // bulkGet triggers the blob-resolve middleware for each row that
15842
+ // still has `_hasBlobRefs=1`. Rows already resolved by parallel
15843
+ // reads come back without the marker and the middleware no-ops.
15844
+ // Rows that have been deleted return `undefined` and are
15845
+ // likewise skipped.
15846
+ yield table.bulkGet(slice);
15847
+ }
15848
+ catch (err) {
15849
+ console.error(`Eager download: ${table.name} chunk failed:`, err);
15850
+ continue;
15851
+ }
15852
+ totalProcessed += slice.length;
15853
+ debugLog(`Eager download: ${table.name} ${Math.min(i + CHUNK_SIZE, keys.length)}/${keys.length}`);
15854
+ }
15855
+ }
15856
+ if (started) {
15857
+ // Make sure all middleware-enqueued saves have landed before we flip
15858
+ // `downloading$` to false — otherwise observers might see a "done"
15859
+ // signal while writes are still in flight.
15860
+ yield db.blobDownloadTracker.drainPendingSaves();
15861
+ debugLog(`Eager download: done (${totalProcessed} row(s) processed)`);
15862
+ }
15863
+ else {
15864
+ debugLog('Eager download: No blobs remaining, exiting');
15624
15865
  }
15625
15866
  }
15626
15867
  finally {
15627
- setDownloadingState(downloading$, false);
15868
+ if (started)
15869
+ setDownloadingState(downloading$, false);
15628
15870
  }
15629
15871
  });
15630
15872
  }
@@ -16960,99 +17202,6 @@
16960
17202
  };
16961
17203
  }
16962
17204
 
16963
- /**
16964
- * BlobSavingQueue - Queues resolved blobs for saving back to IndexedDB
16965
- *
16966
- * Uses setTimeout(fn, 0) instead of queueMicrotask to completely isolate
16967
- * from Dexie's Promise.PSD context. This prevents the save operation
16968
- * from inheriting any ongoing transaction.
16969
- *
16970
- * Each blob is saved atomically using downCore transaction with the specific
16971
- * keyPath to avoid race conditions with other property changes.
16972
- */
16973
- class BlobSavingQueue {
16974
- constructor(db) {
16975
- this.queue = [];
16976
- this.isProcessing = false;
16977
- this.db = db;
16978
- }
16979
- /**
16980
- * Queue a resolved blob for saving.
16981
- * Only the specific blob property will be updated atomically.
16982
- */
16983
- saveBlobs(tableName, primaryKey, resolvedBlobs) {
16984
- this.queue.push({ tableName, primaryKey, resolvedBlobs });
16985
- this.startConsumer();
16986
- }
16987
- /**
16988
- * Start the consumer if not already processing.
16989
- * Uses setTimeout(fn, 0) to completely break out of any
16990
- * Dexie transaction context (Promise.PSD).
16991
- */
16992
- startConsumer() {
16993
- if (this.isProcessing)
16994
- return;
16995
- this.isProcessing = true;
16996
- // Use setTimeout to completely isolate from Dexie's PSD context
16997
- // queueMicrotask would risk inheriting the current transaction
16998
- setTimeout(() => {
16999
- this.processQueue();
17000
- }, 0);
17001
- }
17002
- /**
17003
- * Process all queued blobs.
17004
- * Runs in a completely isolated context (no inherited transaction).
17005
- * Uses atomic updates to avoid race conditions.
17006
- */
17007
- processQueue() {
17008
- const item = this.queue.shift();
17009
- if (!item) {
17010
- this.isProcessing = false;
17011
- return;
17012
- }
17013
- // Atomic update of just the blob property
17014
- this.db
17015
- .transaction('rw', item.tableName, (tx) => {
17016
- const trans = tx.idbtrans;
17017
- trans.disableChangeTracking = true; // Don't regard this as a change for sync purposes
17018
- trans.disableAccessControl = true; // Bypass any access control checks since this is an internal operation
17019
- trans.disableBlobResolve = true; // Custom flag to skip blob resolve middleware during this transaction
17020
- const updateSpec = {};
17021
- for (const blob of item.resolvedBlobs) {
17022
- updateSpec[blob.keyPath] = blob.data;
17023
- }
17024
- tx.table(item.tableName).update(item.primaryKey, (obj) => {
17025
- // Check that object still has the same unresolved blob refs before applying update (i.e. it hasn't been modified since we read it)
17026
- for (const blob of item.resolvedBlobs) {
17027
- // Verify atomicity - none of the blob properties has been modified since we read it. If any of them was modified, skip updating this item to avoid overwriting user changes.
17028
- const currentValue = Dexie.getByKeyPath(obj, blob.keyPath);
17029
- if (currentValue === undefined) {
17030
- // Blob property was removed - skip updating this blob
17031
- continue;
17032
- }
17033
- if (!isBlobRef(currentValue)) {
17034
- // Blob property was modified to a non-blob-ref value - skip updating this blob
17035
- continue;
17036
- }
17037
- if (currentValue.ref !== blob.ref) {
17038
- // Blob property was modified - skip updating this blob
17039
- return; // Stop. Another items has been queued to fully fix the object.
17040
- }
17041
- Dexie.setByKeyPath(obj, blob.keyPath, blob.data);
17042
- }
17043
- delete obj._hasBlobRefs; // Clear the _hasBlobRefs marker if all refs was resolved.
17044
- });
17045
- })
17046
- .catch((error) => {
17047
- console.error(`Error saving resolved blobs on ${item.tableName}:${item.primaryKey}:`, error);
17048
- })
17049
- .finally(() => {
17050
- // Process next item in the queue
17051
- return this.processQueue();
17052
- });
17053
- }
17054
- }
17055
-
17056
17205
  /**
17057
17206
  * DBCore Middleware for resolving BlobRefs on read
17058
17207
  *
@@ -17063,10 +17212,11 @@
17063
17212
  * Uses Dexie.waitFor() only for explicit rw transactions to keep them alive.
17064
17213
  * For readonly or implicit transactions, resolves directly (no waitFor needed).
17065
17214
  *
17066
- * Resolved blobs are queued for saving via BlobSavingQueue, which uses
17067
- * setTimeout(fn, 0) to completely isolate from Dexie's transaction context.
17068
- * Each blob is saved atomically using Table.update() with its keyPath to
17069
- * avoid race conditions with other property changes.
17215
+ * Resolved blobs are persisted via db.blobDownloadTracker.enqueueSave(),
17216
+ * which internally uses a queue that runs in a fresh JS task to completely
17217
+ * isolate from Dexie's transaction context. Each blob is saved atomically
17218
+ * using Table.update() with its keyPath to avoid race conditions with other
17219
+ * property changes.
17070
17220
  *
17071
17221
  * Blob downloads use Authorization header (same as sync) via the server
17072
17222
  * proxy endpoint: GET /blob/{ref}
@@ -17077,8 +17227,6 @@
17077
17227
  name: 'blobResolve',
17078
17228
  level: 2, // Run above cache (0) and other middlewares (1) to resolve BlobRefs from cached data
17079
17229
  create(downlevelDatabase) {
17080
- // Create a single queue instance for this database
17081
- const blobSavingQueue = new BlobSavingQueue(db);
17082
17230
  return Object.assign(Object.assign({}, downlevelDatabase), { table(tableName) {
17083
17231
  var _a;
17084
17232
  if (!db.cloud) {
@@ -17099,7 +17247,7 @@
17099
17247
  }
17100
17248
  return downlevelTable.get(req).then((result) => {
17101
17249
  if (result && hasUnresolvedBlobRefs(result)) {
17102
- return resolveAndSave(downlevelTable, req.trans, req.key, result, blobSavingQueue, db);
17250
+ return resolveAndSave(downlevelTable, req.trans, req.key, result, db);
17103
17251
  }
17104
17252
  return result;
17105
17253
  });
@@ -17116,7 +17264,7 @@
17116
17264
  return results;
17117
17265
  return Dexie.Promise.all(results.map((result, index) => {
17118
17266
  if (result && hasUnresolvedBlobRefs(result)) {
17119
- return resolveAndSave(downlevelTable, req.trans, req.keys[index], result, blobSavingQueue, db);
17267
+ return resolveAndSave(downlevelTable, req.trans, req.keys[index], result, db);
17120
17268
  }
17121
17269
  return result;
17122
17270
  }));
@@ -17136,7 +17284,7 @@
17136
17284
  return result;
17137
17285
  return Dexie.Promise.all(result.result.map((item) => {
17138
17286
  if (item && hasUnresolvedBlobRefs(item)) {
17139
- return resolveAndSave(downlevelTable, req.trans, undefined, item, blobSavingQueue, db);
17287
+ return resolveAndSave(downlevelTable, req.trans, undefined, item, db);
17140
17288
  }
17141
17289
  return item;
17142
17290
  })).then((resolved) => (Object.assign(Object.assign({}, result), { result: resolved })));
@@ -17154,7 +17302,7 @@
17154
17302
  return cursor; // No values requested, so no resolution needed
17155
17303
  if (!dbUrl)
17156
17304
  return cursor; // No database URL configured, can't resolve blobs
17157
- return createBlobResolvingCursor(cursor, downlevelTable, blobSavingQueue, db);
17305
+ return createBlobResolvingCursor(cursor, downlevelTable, db);
17158
17306
  });
17159
17307
  } });
17160
17308
  } });
@@ -17171,7 +17319,7 @@
17171
17319
  * Returns the cursor synchronously. Resolution happens in start() before
17172
17320
  * each onNext callback, ensuring cursor.value is always available.
17173
17321
  */
17174
- function createBlobResolvingCursor(cursor, table, blobSavingQueue, db) {
17322
+ function createBlobResolvingCursor(cursor, table, db) {
17175
17323
  // Create wrapped cursor using Object.create() - inherits everything.
17176
17324
  // Important: .key and .primaryKey must be explicitly overridden with
17177
17325
  // closure-based getters to prevent native IDBCursorWithValue getters from
@@ -17179,11 +17327,15 @@
17179
17327
  // throws "Illegal invocation" in Chrome 146+.
17180
17328
  const wrappedCursor = Object.create(cursor, {
17181
17329
  key: {
17182
- get() { return cursor.key; },
17330
+ get() {
17331
+ return cursor.key;
17332
+ },
17183
17333
  configurable: true,
17184
17334
  },
17185
17335
  primaryKey: {
17186
- get() { return cursor.primaryKey; },
17336
+ get() {
17337
+ return cursor.primaryKey;
17338
+ },
17187
17339
  configurable: true,
17188
17340
  },
17189
17341
  value: {
@@ -17201,7 +17353,7 @@
17201
17353
  onNext();
17202
17354
  return;
17203
17355
  }
17204
- resolveAndSave(table, cursor.trans, cursor.primaryKey, rawValue, blobSavingQueue, db, true).then((resolved) => {
17356
+ resolveAndSave(table, cursor.trans, cursor.primaryKey, rawValue, db, true).then((resolved) => {
17205
17357
  wrappedCursor.value = resolved;
17206
17358
  onNext();
17207
17359
  }, (err) => {
@@ -17229,7 +17381,7 @@
17229
17381
  * Returns Dexie.Promise to preserve PSD context.
17230
17382
  */
17231
17383
  function resolveAndSave(table, trans, pKey, // optional. If missing, tries to extract from object using primary key path
17232
- obj, blobSavingQueue, db, isCursorValue = false // Flag to indicate if we're resolving a cursor value (which may not have a primary key)
17384
+ obj, db, isCursorValue = false // Flag to indicate if we're resolving a cursor value (which may not have a primary key)
17233
17385
  ) {
17234
17386
  var _a;
17235
17387
  try {
@@ -17266,21 +17418,19 @@
17266
17418
  ? Dexie.getByKeyPath(obj, primaryKey.keyPath)
17267
17419
  : undefined;
17268
17420
  if (key !== undefined) {
17269
- // Queue each resolved blob individually for atomic update
17270
- // This uses setTimeout(fn, 0) to completely isolate from
17271
- // Dexie's transaction context (avoids inheriting PSD)
17272
- if (isReadonly) {
17273
- blobSavingQueue.saveBlobs(table.name, key, resolvedBlobs);
17274
- }
17275
- else {
17276
- // For rw transactions, we can save directly without queueing
17277
- // since we're still in the same transaction context
17278
- table
17279
- .mutate({ type: 'put', keys: [key], values: [resolved], trans })
17280
- .catch((err) => {
17281
- console.error(`Failed to save resolved blob on ${table.name}:${key}:`, err);
17282
- });
17283
- }
17421
+ // Hand off persistence to the tracker. The tracker owns an
17422
+ // internal save-queue that runs in a fresh JS task (setTimeout 0)
17423
+ // completely outside any PSD context, so opening a Dexie rw
17424
+ // transaction there is always safe regardless of the calling
17425
+ // context. The tracker also keeps the in-flight download cache
17426
+ // alive until the save completes, so concurrent readers piggyback
17427
+ // on the already-downloaded data instead of refetching.
17428
+ db.blobDownloadTracker.enqueueSave(table.name, key, resolvedBlobs);
17429
+ }
17430
+ else if (resolvedBlobs.length > 0) {
17431
+ // No primary key — we can't persist. Release the in-flight cache
17432
+ // entries explicitly so they don't leak.
17433
+ db.blobDownloadTracker.releaseRefs(resolvedBlobs.map((b) => b.ref));
17284
17434
  }
17285
17435
  return resolved;
17286
17436
  })
@@ -18113,8 +18263,7 @@
18113
18263
  throw new Error(`No database URL to connect WebSocket to`);
18114
18264
  }
18115
18265
  const readyForChangesMessage = db.messageConsumer.readyToServe.pipe(operators.filter((isReady) => isReady), // When consumer is ready for new messages, produce such a message to inform server about it
18116
- operators.switchMap(() => db.cloud.persistedSyncState.pipe(operators.filter((syncState) => !!(syncState && syncState.serverRevision)), operators.take(1))), // Wait reactively for syncState with serverRevision (avoids race with logout/re-sync)
18117
- operators.switchMap((syncState) => __awaiter(this, void 0, void 0, function* () {
18266
+ operators.switchMap(() => db.getPersistedSyncState()), operators.filter((syncState) => !!(syncState && syncState.serverRevision)), operators.switchMap((syncState) => __awaiter(this, void 0, void 0, function* () {
18118
18267
  return ({
18119
18268
  // Produce the message to trigger server to send us new messages to consume:
18120
18269
  type: 'ready',
@@ -19571,7 +19720,7 @@
19571
19720
  const downloading$ = createDownloadingState();
19572
19721
  dexie.cloud = {
19573
19722
  // @ts-ignore
19574
- version: "4.4.10",
19723
+ version: "4.4.12",
19575
19724
  options: Object.assign({}, DEFAULT_OPTIONS),
19576
19725
  schema: null,
19577
19726
  get currentUserId() {
@@ -20016,7 +20165,7 @@
20016
20165
  }
20017
20166
  }
20018
20167
  // @ts-ignore
20019
- dexieCloud.version = "4.4.10";
20168
+ dexieCloud.version = "4.4.12";
20020
20169
  Dexie.Cloud = dexieCloud;
20021
20170
 
20022
20171
  // In case the SW lives for a while, let it reuse already opened connections: