@hkdigital/lib-sveltekit 0.1.96 → 0.1.98

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.
@@ -38,16 +38,19 @@
38
38
 
39
39
  /** @typedef {import('./typedef').IDBVersionChangeEvent} IDBVersionChangeEvent */
40
40
 
41
- const DEFAULT_DB_NAME ='http-cache';
42
- const DEFAULT_STORE_NAME ='responses';
43
- const DEFAULT_MAX_SIZE = 50 * 1024 * 1024; // 50 MB
44
- const DEFAULT_MAX_AGE = 90 * 24 * 60 * 60 * 1000; // 90 days
41
+ const DEFAULT_DB_NAME = 'http-cache';
42
+ const DEFAULT_STORE_NAME = 'responses';
43
+ const DEFAULT_MAX_SIZE = 50 * 1024 * 1024; // 50 MB
44
+ const DEFAULT_MAX_AGE = 90 * 24 * 60 * 60 * 1000; // 90 days
45
45
 
46
46
  const DEFAULT_CLEANUP_BATCH_SIZE = 100;
47
47
  const DEFAULT_CLEANUP_INTERVAL = 5 * 60 * 1000; // 5 minutes;
48
48
 
49
49
  const DEFAULT_CLEANUP_POSTPONE_MS = 5000; // 5 seconds
50
50
 
51
+ // Add logging to track concurrent access
52
+ const concurrentReadsByKey = new Map();
53
+
51
54
  /**
52
55
  * IndexedDbCache with automatic background cleanup
53
56
  */
@@ -72,10 +75,12 @@ export default class IndexedDbCache {
72
75
  this.maxSize = options.maxSize || DEFAULT_MAX_SIZE;
73
76
  this.maxAge = options.maxAge || DEFAULT_MAX_AGE;
74
77
 
75
- this.cleanupBatchSize = options.cleanupBatchSize || DEFAULT_CLEANUP_BATCH_SIZE;
78
+ this.cleanupBatchSize =
79
+ options.cleanupBatchSize || DEFAULT_CLEANUP_BATCH_SIZE;
76
80
  this.cleanupInterval = options.cleanupInterval || DEFAULT_CLEANUP_INTERVAL;
77
81
 
78
- this.cleanupPostponeTimeout = options.cleanupPostponeTimeout || DEFAULT_CLEANUP_POSTPONE_MS;
82
+ this.cleanupPostponeTimeout =
83
+ options.cleanupPostponeTimeout || DEFAULT_CLEANUP_POSTPONE_MS;
79
84
  this.cacheVersion = options.cacheVersion || '1.0.0';
80
85
 
81
86
  // Define index names as constants to ensure consistency
@@ -318,10 +323,15 @@ export default class IndexedDbCache {
318
323
  * @returns {Promise<CacheEntry|null>} Cache entry or null if not found/expired
319
324
  */
320
325
  async get(key) {
326
+ // Track concurrent reads per key
327
+ const current = concurrentReadsByKey.get(key) || 0;
328
+ concurrentReadsByKey.set(key, current + 1);
329
+ // console.log(`Concurrent reads for ${key}: ${current + 1}`);
330
+
321
331
  try {
322
332
  const db = await this.dbPromise;
323
333
 
324
- return new Promise((resolve, reject) => {
334
+ const result = new Promise((resolve, reject) => {
325
335
  try {
326
336
  const transaction = db.transaction(this.storeName, 'readonly');
327
337
  const store = transaction.objectStore(this.storeName);
@@ -376,10 +386,17 @@ export default class IndexedDbCache {
376
386
  try {
377
387
  let responseHeaders = new Headers(entry.headers);
378
388
 
389
+ let responseBody = entry.body;
390
+
391
+ if (responseBody instanceof Blob) {
392
+ // Clone the blob to ensure it's not consumed
393
+ responseBody = entry.body.slice(0, entry.body.size, entry.body.type);
394
+ }
395
+
379
396
  // Create Response safely
380
397
  let response;
381
398
  try {
382
- response = new Response(entry.body, {
399
+ response = new Response(responseBody, {
383
400
  status: entry.status,
384
401
  statusText: entry.statusText,
385
402
  headers: responseHeaders
@@ -421,9 +438,23 @@ export default class IndexedDbCache {
421
438
  resolve(null);
422
439
  }
423
440
  });
441
+
442
+ return result;
424
443
  } catch (err) {
425
444
  console.error('Cache get error:', err);
426
445
  return null;
446
+ } finally {
447
+ // Always decrement
448
+ const current = concurrentReadsByKey.get(key) || 1;
449
+ if (current <= 1) {
450
+ concurrentReadsByKey.delete(key);
451
+ } else {
452
+ concurrentReadsByKey.set(key, current - 1);
453
+ }
454
+
455
+ // console.debug(
456
+ // `Concurrent reads for ${key} after decrement: ${current - 1}`
457
+ // );
427
458
  }
428
459
  }
429
460
 
@@ -490,7 +521,7 @@ export default class IndexedDbCache {
490
521
  // Calculate rough size estimate
491
522
  const headerSize = JSON.stringify(headers).length * 2;
492
523
  const size =
493
- /** @type {Blob} */ ((body).size || 0) + headerSize + key.length * 2;
524
+ /** @type {Blob} */ (body.size || 0) + headerSize + key.length * 2;
494
525
 
495
526
  const entry = {
496
527
  key,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-sveltekit",
3
- "version": "0.1.96",
3
+ "version": "0.1.98",
4
4
  "author": {
5
5
  "name": "HKdigital",
6
6
  "url": "https://hkdigital.nl"