@hkdigital/lib-sveltekit 0.1.102 → 0.2.1
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.
@@ -32,8 +32,6 @@
|
|
32
32
|
* }
|
33
33
|
*/
|
34
34
|
|
35
|
-
import { HkPromise } from '../promise';
|
36
|
-
|
37
35
|
/** @typedef {import('./typedef').CacheEntry} CacheEntry */
|
38
36
|
|
39
37
|
/** @typedef {import('./typedef').IDBRequestEvent} IDBRequestEvent */
|
@@ -347,10 +345,22 @@ export default class IndexedDbCache {
|
|
347
345
|
return;
|
348
346
|
}
|
349
347
|
|
348
|
+
// Skip old entries or corrupted blobs
|
349
|
+
if (!entry.bodyType || entry.bodyType !== 'ab') {
|
350
|
+
// Delete old/corrupted entry
|
351
|
+
this._deleteEntry(key).catch(console.error);
|
352
|
+
resolve(null);
|
353
|
+
return;
|
354
|
+
}
|
355
|
+
|
350
356
|
// Clone Blob before reference becomes invalid
|
351
|
-
let
|
352
|
-
|
353
|
-
|
357
|
+
let responseBody = entry.body;
|
358
|
+
|
359
|
+
if (entry.bodyType === 'ab') {
|
360
|
+
// Reconstruct Blob from ArrayBuffer
|
361
|
+
responseBody = new Blob([entry.body], {
|
362
|
+
type: entry.contentType || 'application/octet-stream'
|
363
|
+
});
|
354
364
|
}
|
355
365
|
|
356
366
|
// Check if expired
|
@@ -363,7 +373,7 @@ export default class IndexedDbCache {
|
|
363
373
|
return;
|
364
374
|
}
|
365
375
|
|
366
|
-
// Update access timestamp
|
376
|
+
// Update access timestamp
|
367
377
|
await this._updateAccessTime(key).catch((err) => {
|
368
378
|
console.error('Failed to update access time:', err);
|
369
379
|
});
|
@@ -396,7 +406,7 @@ export default class IndexedDbCache {
|
|
396
406
|
// Create Response safely
|
397
407
|
let response;
|
398
408
|
try {
|
399
|
-
response = new Response(
|
409
|
+
response = new Response(responseBody, {
|
400
410
|
status: entry.status,
|
401
411
|
statusText: entry.statusText,
|
402
412
|
headers: responseHeaders
|
@@ -458,7 +468,6 @@ export default class IndexedDbCache {
|
|
458
468
|
try {
|
459
469
|
// Postpone cleanup when storing items
|
460
470
|
this._postponeCleanup();
|
461
|
-
|
462
471
|
const db = await this.dbPromise;
|
463
472
|
|
464
473
|
// Clone the response to avoid consuming it
|
@@ -466,27 +475,39 @@ export default class IndexedDbCache {
|
|
466
475
|
|
467
476
|
// Extract response data - handle both browser Response and test mocks
|
468
477
|
let body;
|
478
|
+
let bodyType = 'ab'; // Default is ArrayBuffer
|
479
|
+
let contentType = '';
|
480
|
+
|
469
481
|
try {
|
482
|
+
contentType = clonedResponse.headers.get('content-type') || '';
|
483
|
+
|
470
484
|
// Try standard Response.blob() first (browser environment)
|
471
|
-
|
485
|
+
const blob = await clonedResponse.blob();
|
486
|
+
|
487
|
+
// Convert to ArrayBuffer
|
488
|
+
body = await blob.arrayBuffer();
|
489
|
+
|
472
490
|
} catch (err) {
|
473
491
|
// Fallback for test environment
|
474
|
-
if (
|
475
|
-
|
492
|
+
if (typeof clonedResponse.body === 'string') {
|
493
|
+
const blob = new Blob([clonedResponse.body]);
|
494
|
+
body = await blob.arrayBuffer();
|
495
|
+
} else if (
|
476
496
|
clonedResponse.body instanceof ArrayBuffer ||
|
477
497
|
clonedResponse.body instanceof Uint8Array
|
478
498
|
) {
|
479
|
-
|
499
|
+
// Already have array-like data
|
500
|
+
body = clonedResponse.body instanceof ArrayBuffer ?
|
501
|
+
clonedResponse.body :
|
502
|
+
clonedResponse.body.buffer;
|
480
503
|
} else {
|
481
|
-
// Last resort -
|
482
|
-
body =
|
504
|
+
// Last resort - create empty ArrayBuffer
|
505
|
+
body = new ArrayBuffer(0);
|
483
506
|
}
|
484
507
|
}
|
485
508
|
|
486
509
|
// Extract headers
|
487
|
-
|
488
510
|
let headers = [];
|
489
|
-
|
490
511
|
try {
|
491
512
|
headers = Array.from(clonedResponse.headers.entries());
|
492
513
|
} catch (err) {
|
@@ -495,21 +516,9 @@ export default class IndexedDbCache {
|
|
495
516
|
headers = [];
|
496
517
|
}
|
497
518
|
|
498
|
-
// let headers = [];
|
499
|
-
// try {
|
500
|
-
// headers = Array.from(clonedResponse.headers.entries());
|
501
|
-
// } catch (err) {
|
502
|
-
// // Fallback for test environment - extract headers if available
|
503
|
-
// if (clonedResponse._headers &&
|
504
|
-
// typeof clonedResponse._headers.entries === 'function') {
|
505
|
-
// headers = Array.from(clonedResponse._headers.entries());
|
506
|
-
// }
|
507
|
-
// }
|
508
|
-
|
509
519
|
// Calculate rough size estimate
|
510
520
|
const headerSize = JSON.stringify(headers).length * 2;
|
511
|
-
const size =
|
512
|
-
/** @type {Blob} */ (body.size || 0) + headerSize + key.length * 2;
|
521
|
+
const size = body.byteLength + headerSize + key.length * 2;
|
513
522
|
|
514
523
|
const entry = {
|
515
524
|
key,
|
@@ -518,6 +527,8 @@ export default class IndexedDbCache {
|
|
518
527
|
statusText: clonedResponse.statusText || '',
|
519
528
|
headers,
|
520
529
|
body,
|
530
|
+
bodyType,
|
531
|
+
contentType,
|
521
532
|
metadata,
|
522
533
|
timestamp: Date.now(),
|
523
534
|
lastAccessed: Date.now(),
|