@2112-lab/central-plant 0.2.12 → 0.3.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.
@@ -0,0 +1,1441 @@
1
+ import { asyncToGenerator as _asyncToGenerator, regenerator as _regenerator, objectSpread2 as _objectSpread2, toConsumableArray as _toConsumableArray, createForOfIteratorHelper as _createForOfIteratorHelper } from '../../../_virtual/_rollupPluginBabelHelpers.js';
2
+ import { cacheManager, CACHE_NAME_PREFIX, CACHE_EXPIRY } from './CacheManager.js';
3
+
4
+ /**
5
+ * Retrieve the injected Auth instance. Throws if not configured.
6
+ */
7
+ function getAuth() {
8
+ var auth = cacheManager.config.auth;
9
+ if (!auth) throw new Error('cacheManager.configure({ auth }) must be called before using S3 cache functions');
10
+ return auth;
11
+ }
12
+
13
+ /**
14
+ * Retrieve the injected Storage instance. Throws if not configured.
15
+ */
16
+ function getStorage() {
17
+ var storage = cacheManager.config.storage;
18
+ if (!storage) throw new Error('cacheManager.configure({ storage }) must be called before using S3 cache functions');
19
+ return storage;
20
+ }
21
+
22
+ /**
23
+ * Guard used inside S3 fetchers. Throws a noRetry error (suppressed by onError)
24
+ * instead of letting Storage.get() fail with an Amplify credential warning.
25
+ */
26
+ function requireAuthentication() {
27
+ return _requireAuthentication.apply(this, arguments);
28
+ }
29
+ /**
30
+ * Reset the cache manager identity.
31
+ * Call this when auth state changes to ensure the correct cache partition is used.
32
+ */
33
+ function _requireAuthentication() {
34
+ _requireAuthentication = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
35
+ var err;
36
+ return _regenerator().w(function (_context) {
37
+ while (1) switch (_context.n) {
38
+ case 0:
39
+ _context.p = 0;
40
+ _context.n = 1;
41
+ return getAuth().currentAuthenticatedUser();
42
+ case 1:
43
+ _context.n = 3;
44
+ break;
45
+ case 2:
46
+ _context.p = 2;
47
+ _context.v;
48
+ err = new Error('S3 access requires authentication');
49
+ err.code = 'S3CacheNotAuthenticated';
50
+ err.noRetry = true;
51
+ throw err;
52
+ case 3:
53
+ return _context.a(2);
54
+ }
55
+ }, _callee, null, [[0, 2]]);
56
+ }));
57
+ return _requireAuthentication.apply(this, arguments);
58
+ }
59
+ function resetCacheIdentity() {
60
+ cacheManager.resetIdentity();
61
+ console.log('🔄 Cache identity reset - will re-evaluate on next cache access');
62
+ }
63
+
64
+ // --- Global Cache for Bundled/Static Access ---
65
+ var GLOBAL_CACHE_NAME = 'asset-manager-global-v1';
66
+ function getGlobalCache() {
67
+ return _getGlobalCache.apply(this, arguments);
68
+ }
69
+ function _getGlobalCache() {
70
+ _getGlobalCache = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
71
+ return _regenerator().w(function (_context2) {
72
+ while (1) switch (_context2.n) {
73
+ case 0:
74
+ if ('caches' in window) {
75
+ _context2.n = 1;
76
+ break;
77
+ }
78
+ return _context2.a(2, null);
79
+ case 1:
80
+ return _context2.a(2, caches.open(GLOBAL_CACHE_NAME));
81
+ }
82
+ }, _callee2);
83
+ }));
84
+ return _getGlobalCache.apply(this, arguments);
85
+ }
86
+ function executeGlobal(_x) {
87
+ return _executeGlobal.apply(this, arguments);
88
+ }
89
+ /**
90
+ * Clear the entire global cache (bundled models, thumbnails, etc.).
91
+ */
92
+ function _executeGlobal() {
93
+ _executeGlobal = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(_ref) {
94
+ var cacheKey, fetcher, processor, metadata, cache, cachedResponse, response, responseClone, headers, responseToCache, _response, _t2, _t3, _t4, _t5, _t6, _t7;
95
+ return _regenerator().w(function (_context3) {
96
+ while (1) switch (_context3.n) {
97
+ case 0:
98
+ cacheKey = _ref.cacheKey, fetcher = _ref.fetcher, processor = _ref.processor, metadata = _ref.metadata;
99
+ _context3.p = 1;
100
+ _context3.n = 2;
101
+ return getGlobalCache();
102
+ case 2:
103
+ cache = _context3.v;
104
+ if (cache) {
105
+ _context3.n = 5;
106
+ break;
107
+ }
108
+ _t2 = processor;
109
+ _context3.n = 3;
110
+ return fetcher();
111
+ case 3:
112
+ _t3 = _context3.v;
113
+ _context3.n = 4;
114
+ return _t2(_t3);
115
+ case 4:
116
+ return _context3.a(2, _context3.v);
117
+ case 5:
118
+ _context3.n = 6;
119
+ return cache.match(cacheKey);
120
+ case 6:
121
+ cachedResponse = _context3.v;
122
+ if (!cachedResponse) {
123
+ _context3.n = 8;
124
+ break;
125
+ }
126
+ _context3.n = 7;
127
+ return processor(cachedResponse);
128
+ case 7:
129
+ return _context3.a(2, _context3.v);
130
+ case 8:
131
+ _context3.n = 9;
132
+ return fetcher();
133
+ case 9:
134
+ response = _context3.v;
135
+ // Clone for cache
136
+ responseClone = response.clone(); // Store with metadata headers if possible
137
+ headers = new Headers(responseClone.headers);
138
+ headers.append('X-Cached-Time', Date.now().toString());
139
+ if (metadata) {
140
+ headers.append('X-Cache-Metadata', JSON.stringify(metadata));
141
+ }
142
+
143
+ // We need to create a new Response with updated headers to store it
144
+ _t4 = Response;
145
+ _context3.n = 10;
146
+ return responseClone.blob();
147
+ case 10:
148
+ _t5 = _context3.v;
149
+ _t6 = {
150
+ status: responseClone.status,
151
+ statusText: responseClone.statusText,
152
+ headers: headers
153
+ };
154
+ responseToCache = new _t4(_t5, _t6);
155
+ _context3.n = 11;
156
+ return cache.put(cacheKey, responseToCache);
157
+ case 11:
158
+ _context3.n = 12;
159
+ return processor(response);
160
+ case 12:
161
+ return _context3.a(2, _context3.v);
162
+ case 13:
163
+ _context3.p = 13;
164
+ _t7 = _context3.v;
165
+ console.warn('Global Cache execution failed, falling back to network', _t7);
166
+ _context3.n = 14;
167
+ return fetcher();
168
+ case 14:
169
+ _response = _context3.v;
170
+ _context3.n = 15;
171
+ return processor(_response);
172
+ case 15:
173
+ return _context3.a(2, _context3.v);
174
+ }
175
+ }, _callee3, null, [[1, 13]]);
176
+ }));
177
+ return _executeGlobal.apply(this, arguments);
178
+ }
179
+ function clearGlobalCache() {
180
+ return _clearGlobalCache.apply(this, arguments);
181
+ }
182
+ /**
183
+ * Remove expired entries from the global cache.
184
+ * Returns the number of entries removed.
185
+ */
186
+ function _clearGlobalCache() {
187
+ _clearGlobalCache = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4() {
188
+ var deleted, _t8;
189
+ return _regenerator().w(function (_context4) {
190
+ while (1) switch (_context4.n) {
191
+ case 0:
192
+ _context4.p = 0;
193
+ if ('caches' in window) {
194
+ _context4.n = 1;
195
+ break;
196
+ }
197
+ return _context4.a(2, false);
198
+ case 1:
199
+ _context4.n = 2;
200
+ return caches.delete(GLOBAL_CACHE_NAME);
201
+ case 2:
202
+ deleted = _context4.v;
203
+ if (deleted) console.log('🗑️ Global cache cleared');
204
+ return _context4.a(2, deleted);
205
+ case 3:
206
+ _context4.p = 3;
207
+ _t8 = _context4.v;
208
+ console.warn('Failed to clear global cache:', _t8);
209
+ return _context4.a(2, false);
210
+ }
211
+ }, _callee4, null, [[0, 3]]);
212
+ }));
213
+ return _clearGlobalCache.apply(this, arguments);
214
+ }
215
+ function cleanExpiredGlobalCache() {
216
+ return _cleanExpiredGlobalCache.apply(this, arguments);
217
+ }
218
+ function _cleanExpiredGlobalCache() {
219
+ _cleanExpiredGlobalCache = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6() {
220
+ var cache, keys, cleaned;
221
+ return _regenerator().w(function (_context6) {
222
+ while (1) switch (_context6.n) {
223
+ case 0:
224
+ _context6.n = 1;
225
+ return getGlobalCache();
226
+ case 1:
227
+ cache = _context6.v;
228
+ if (cache) {
229
+ _context6.n = 2;
230
+ break;
231
+ }
232
+ return _context6.a(2, 0);
233
+ case 2:
234
+ _context6.n = 3;
235
+ return cache.keys();
236
+ case 3:
237
+ keys = _context6.v;
238
+ cleaned = 0;
239
+ _context6.n = 4;
240
+ return Promise.all(keys.map(/*#__PURE__*/function () {
241
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(request) {
242
+ var response, cachedTime, age, s3Key, expiryMs, _t9;
243
+ return _regenerator().w(function (_context5) {
244
+ while (1) switch (_context5.n) {
245
+ case 0:
246
+ _context5.p = 0;
247
+ _context5.n = 1;
248
+ return cache.match(request);
249
+ case 1:
250
+ response = _context5.v;
251
+ if (response) {
252
+ _context5.n = 2;
253
+ break;
254
+ }
255
+ return _context5.a(2);
256
+ case 2:
257
+ cachedTime = parseInt(response.headers.get('X-Cached-Time') || '0');
258
+ age = Date.now() - cachedTime;
259
+ s3Key = request.url.replace('https://local.cache', '');
260
+ expiryMs = cacheManager.getExpiryForPath(s3Key);
261
+ if (!(age > expiryMs)) {
262
+ _context5.n = 4;
263
+ break;
264
+ }
265
+ _context5.n = 3;
266
+ return cache.delete(request);
267
+ case 3:
268
+ cleaned++;
269
+ case 4:
270
+ _context5.n = 6;
271
+ break;
272
+ case 5:
273
+ _context5.p = 5;
274
+ _t9 = _context5.v;
275
+ console.warn('Failed to check global cache entry:', _t9);
276
+ case 6:
277
+ return _context5.a(2);
278
+ }
279
+ }, _callee5, null, [[0, 5]]);
280
+ }));
281
+ return function (_x16) {
282
+ return _ref2.apply(this, arguments);
283
+ };
284
+ }()));
285
+ case 4:
286
+ if (cleaned > 0) console.log("\uD83E\uDDF9 Cleaned ".concat(cleaned, " expired global cache entries"));
287
+ return _context6.a(2, cleaned);
288
+ }
289
+ }, _callee6);
290
+ }));
291
+ return _cleanExpiredGlobalCache.apply(this, arguments);
292
+ }
293
+ function getGlobalCacheStatsDetail() {
294
+ return _getGlobalCacheStatsDetail.apply(this, arguments);
295
+ } // --- Exports delegating to CacheManager ---
296
+ function _getGlobalCacheStatsDetail() {
297
+ _getGlobalCacheStatsDetail = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8() {
298
+ var cache, keys, items, totalSize;
299
+ return _regenerator().w(function (_context8) {
300
+ while (1) switch (_context8.n) {
301
+ case 0:
302
+ _context8.n = 1;
303
+ return getGlobalCache();
304
+ case 1:
305
+ cache = _context8.v;
306
+ if (cache) {
307
+ _context8.n = 2;
308
+ break;
309
+ }
310
+ return _context8.a(2, {
311
+ count: 0,
312
+ totalSize: 0,
313
+ totalSizeMB: '0.00',
314
+ items: []
315
+ });
316
+ case 2:
317
+ _context8.n = 3;
318
+ return cache.keys();
319
+ case 3:
320
+ keys = _context8.v;
321
+ items = [];
322
+ totalSize = 0;
323
+ _context8.n = 4;
324
+ return Promise.all(keys.map(/*#__PURE__*/function () {
325
+ var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(request) {
326
+ var response, cachedTime, metadataHeader, metadata, size, cl;
327
+ return _regenerator().w(function (_context7) {
328
+ while (1) switch (_context7.n) {
329
+ case 0:
330
+ _context7.n = 1;
331
+ return cache.match(request);
332
+ case 1:
333
+ response = _context7.v;
334
+ if (response) {
335
+ _context7.n = 2;
336
+ break;
337
+ }
338
+ return _context7.a(2);
339
+ case 2:
340
+ cachedTime = parseInt(response.headers.get('X-Cached-Time') || '0');
341
+ metadataHeader = response.headers.get('X-Cache-Metadata');
342
+ metadata = metadataHeader ? JSON.parse(metadataHeader) : {};
343
+ size = 0;
344
+ cl = response.headers.get('Content-Length');
345
+ if (cl) size = parseInt(cl);
346
+ totalSize += size;
347
+ items.push(_objectSpread2({
348
+ url: request.url,
349
+ s3Key: request.url.replace('https://local.cache', ''),
350
+ size: size,
351
+ sizeMB: (size / (1024 * 1024)).toFixed(2),
352
+ cachedTime: cachedTime,
353
+ age: Date.now() - cachedTime,
354
+ isLocal: true,
355
+ type: response.headers.get('Content-Type') || 'application/octet-stream'
356
+ }, metadata));
357
+ case 3:
358
+ return _context7.a(2);
359
+ }
360
+ }, _callee7);
361
+ }));
362
+ return function (_x17) {
363
+ return _ref3.apply(this, arguments);
364
+ };
365
+ }()));
366
+ case 4:
367
+ return _context8.a(2, {
368
+ count: items.length,
369
+ totalSize: totalSize,
370
+ totalSizeMB: (totalSize / (1024 * 1024)).toFixed(2),
371
+ items: items
372
+ });
373
+ }
374
+ }, _callee8);
375
+ }));
376
+ return _getGlobalCacheStatsDetail.apply(this, arguments);
377
+ }
378
+ function getGlobalCacheStats() {
379
+ return cacheManager.getGlobalStats();
380
+ }
381
+ function resetGlobalCacheStats() {
382
+ cacheManager.resetStats();
383
+ }
384
+ function getCurrentCacheName() {
385
+ return _getCurrentCacheName.apply(this, arguments);
386
+ }
387
+ function _getCurrentCacheName() {
388
+ _getCurrentCacheName = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9() {
389
+ return _regenerator().w(function (_context9) {
390
+ while (1) switch (_context9.n) {
391
+ case 0:
392
+ return _context9.a(2, cacheManager.getCacheName());
393
+ }
394
+ }, _callee9);
395
+ }));
396
+ return _getCurrentCacheName.apply(this, arguments);
397
+ }
398
+ function cleanExpiredCache() {
399
+ return _cleanExpiredCache.apply(this, arguments);
400
+ }
401
+ function _cleanExpiredCache() {
402
+ _cleanExpiredCache = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0() {
403
+ var userCount, globalCount, count;
404
+ return _regenerator().w(function (_context0) {
405
+ while (1) switch (_context0.n) {
406
+ case 0:
407
+ _context0.n = 1;
408
+ return cacheManager.cleanExpired();
409
+ case 1:
410
+ userCount = _context0.v;
411
+ _context0.n = 2;
412
+ return cleanExpiredGlobalCache();
413
+ case 2:
414
+ globalCount = _context0.v;
415
+ count = userCount + globalCount;
416
+ if (count > 0) console.log("\uD83E\uDDF9 Cleaned ".concat(count, " expired cache entries (user: ").concat(userCount, ", global: ").concat(globalCount, ")"));
417
+ return _context0.a(2, count);
418
+ }
419
+ }, _callee0);
420
+ }));
421
+ return _cleanExpiredCache.apply(this, arguments);
422
+ }
423
+ function cleanInvalidCacheEntries() {
424
+ return _cleanInvalidCacheEntries.apply(this, arguments);
425
+ }
426
+ function _cleanInvalidCacheEntries() {
427
+ _cleanInvalidCacheEntries = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1() {
428
+ var count;
429
+ return _regenerator().w(function (_context1) {
430
+ while (1) switch (_context1.n) {
431
+ case 0:
432
+ _context1.n = 1;
433
+ return cacheManager.cleanInvalid();
434
+ case 1:
435
+ count = _context1.v;
436
+ if (count > 0) console.log("\uD83E\uDDF9 Cleaned ".concat(count, " invalid cache entries"));
437
+ return _context1.a(2, count);
438
+ }
439
+ }, _callee1);
440
+ }));
441
+ return _cleanInvalidCacheEntries.apply(this, arguments);
442
+ }
443
+ function getCacheStats() {
444
+ return _getCacheStats.apply(this, arguments);
445
+ }
446
+ function _getCacheStats() {
447
+ _getCacheStats = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee10() {
448
+ var allUsers,
449
+ userStats,
450
+ globalStats,
451
+ userItems,
452
+ _args10 = arguments;
453
+ return _regenerator().w(function (_context10) {
454
+ while (1) switch (_context10.n) {
455
+ case 0:
456
+ allUsers = _args10.length > 0 && _args10[0] !== undefined ? _args10[0] : false;
457
+ _context10.n = 1;
458
+ return cacheManager.getDetailedStats(allUsers);
459
+ case 1:
460
+ userStats = _context10.v;
461
+ _context10.n = 2;
462
+ return getGlobalCacheStatsDetail();
463
+ case 2:
464
+ globalStats = _context10.v;
465
+ // Ensure user stats items have isLocal: false
466
+ userItems = userStats.items.map(function (item) {
467
+ return _objectSpread2(_objectSpread2({}, item), {}, {
468
+ isLocal: false
469
+ });
470
+ }); // Merge stats
471
+ return _context10.a(2, {
472
+ count: userStats.count + globalStats.count,
473
+ totalSize: userStats.totalSize + globalStats.totalSize,
474
+ totalSizeMB: (parseFloat(userStats.totalSizeMB) + parseFloat(globalStats.totalSizeMB)).toFixed(2),
475
+ items: [].concat(_toConsumableArray(userItems), _toConsumableArray(globalStats.items))
476
+ });
477
+ }
478
+ }, _callee10);
479
+ }));
480
+ return _getCacheStats.apply(this, arguments);
481
+ }
482
+ function getUserOnlyCacheStats() {
483
+ return _getUserOnlyCacheStats.apply(this, arguments);
484
+ }
485
+ function _getUserOnlyCacheStats() {
486
+ _getUserOnlyCacheStats = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee11() {
487
+ var allUsers,
488
+ stats,
489
+ _args11 = arguments;
490
+ return _regenerator().w(function (_context11) {
491
+ while (1) switch (_context11.n) {
492
+ case 0:
493
+ allUsers = _args11.length > 0 && _args11[0] !== undefined ? _args11[0] : false;
494
+ _context11.n = 1;
495
+ return cacheManager.getDetailedStats(allUsers);
496
+ case 1:
497
+ stats = _context11.v;
498
+ // Ensure items have isLocal: false
499
+ stats.items = stats.items.map(function (item) {
500
+ return _objectSpread2(_objectSpread2({}, item), {}, {
501
+ isLocal: false
502
+ });
503
+ });
504
+ return _context11.a(2, stats);
505
+ }
506
+ }, _callee11);
507
+ }));
508
+ return _getUserOnlyCacheStats.apply(this, arguments);
509
+ }
510
+ function getGlobalOnlyCacheStats() {
511
+ return _getGlobalOnlyCacheStats.apply(this, arguments);
512
+ }
513
+ function _getGlobalOnlyCacheStats() {
514
+ _getGlobalOnlyCacheStats = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee12() {
515
+ return _regenerator().w(function (_context12) {
516
+ while (1) switch (_context12.n) {
517
+ case 0:
518
+ _context12.n = 1;
519
+ return getGlobalCacheStatsDetail();
520
+ case 1:
521
+ return _context12.a(2, _context12.v);
522
+ }
523
+ }, _callee12);
524
+ }));
525
+ return _getGlobalOnlyCacheStats.apply(this, arguments);
526
+ }
527
+ function clearS3Cache() {
528
+ return _clearS3Cache.apply(this, arguments);
529
+ }
530
+ function _clearS3Cache() {
531
+ _clearS3Cache = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee13() {
532
+ var specificKey,
533
+ clearAllUsers,
534
+ result,
535
+ _args13 = arguments;
536
+ return _regenerator().w(function (_context13) {
537
+ while (1) switch (_context13.n) {
538
+ case 0:
539
+ specificKey = _args13.length > 0 && _args13[0] !== undefined ? _args13[0] : null;
540
+ clearAllUsers = _args13.length > 1 && _args13[1] !== undefined ? _args13[1] : false;
541
+ _context13.n = 1;
542
+ return cacheManager.clearCache(specificKey, clearAllUsers);
543
+ case 1:
544
+ result = _context13.v;
545
+ if (specificKey) {
546
+ _context13.n = 2;
547
+ break;
548
+ }
549
+ _context13.n = 2;
550
+ return clearGlobalCache();
551
+ case 2:
552
+ if (result) console.log(specificKey ? "\uD83D\uDDD1\uFE0F Cleared cache for ".concat(specificKey) : "\uD83D\uDDD1\uFE0F Cache cleared (user + global)");
553
+ return _context13.a(2, result);
554
+ }
555
+ }, _callee13);
556
+ }));
557
+ return _clearS3Cache.apply(this, arguments);
558
+ }
559
+ function getAllCacheKeys() {
560
+ return _getAllCacheKeys.apply(this, arguments);
561
+ }
562
+ function _getAllCacheKeys() {
563
+ _getAllCacheKeys = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee14() {
564
+ var allKeys, addKey, globalCache, keys, allCacheNames, appCaches, _iterator, _step, cacheName, cache, _keys, _t0;
565
+ return _regenerator().w(function (_context14) {
566
+ while (1) switch (_context14.n) {
567
+ case 0:
568
+ allKeys = new Set(); // Helper to add both raw and decoded versions for robust matching
569
+ addKey = function addKey(url) {
570
+ allKeys.add(url);
571
+ try {
572
+ var decoded = decodeURI(url);
573
+ if (decoded !== url) allKeys.add(decoded);
574
+ } catch (e) {
575
+ // Ignore encoding errors
576
+ }
577
+ }; // 1. Get Global Cache Keys (Custom Global Cache)
578
+ _context14.n = 1;
579
+ return getGlobalCache();
580
+ case 1:
581
+ globalCache = _context14.v;
582
+ if (!globalCache) {
583
+ _context14.n = 3;
584
+ break;
585
+ }
586
+ _context14.n = 2;
587
+ return globalCache.keys();
588
+ case 2:
589
+ keys = _context14.v;
590
+ keys.forEach(function (request) {
591
+ return addKey(request.url);
592
+ });
593
+ case 3:
594
+ if (!('caches' in window)) {
595
+ _context14.n = 13;
596
+ break;
597
+ }
598
+ _context14.n = 4;
599
+ return caches.keys();
600
+ case 4:
601
+ allCacheNames = _context14.v;
602
+ appCaches = allCacheNames.filter(function (name) {
603
+ return name.startsWith(CACHE_NAME_PREFIX);
604
+ });
605
+ _iterator = _createForOfIteratorHelper(appCaches);
606
+ _context14.p = 5;
607
+ _iterator.s();
608
+ case 6:
609
+ if ((_step = _iterator.n()).done) {
610
+ _context14.n = 10;
611
+ break;
612
+ }
613
+ cacheName = _step.value;
614
+ _context14.n = 7;
615
+ return caches.open(cacheName);
616
+ case 7:
617
+ cache = _context14.v;
618
+ if (!cache) {
619
+ _context14.n = 9;
620
+ break;
621
+ }
622
+ _context14.n = 8;
623
+ return cache.keys();
624
+ case 8:
625
+ _keys = _context14.v;
626
+ _keys.forEach(function (request) {
627
+ return addKey(request.url);
628
+ });
629
+ case 9:
630
+ _context14.n = 6;
631
+ break;
632
+ case 10:
633
+ _context14.n = 12;
634
+ break;
635
+ case 11:
636
+ _context14.p = 11;
637
+ _t0 = _context14.v;
638
+ _iterator.e(_t0);
639
+ case 12:
640
+ _context14.p = 12;
641
+ _iterator.f();
642
+ return _context14.f(12);
643
+ case 13:
644
+ return _context14.a(2, allKeys);
645
+ }
646
+ }, _callee14, null, [[5, 11, 12, 13]]);
647
+ }));
648
+ return _getAllCacheKeys.apply(this, arguments);
649
+ }
650
+ function switchCachePartition(_x2) {
651
+ return _switchCachePartition.apply(this, arguments);
652
+ }
653
+
654
+ // --- Data Fetchers ---
655
+
656
+ /**
657
+ * Check all app cache partitions for a cached response.
658
+ * This handles the race condition where auth may not be hydrated yet on page refresh.
659
+ * @param {string} cacheKey - The cache key to look for
660
+ * @returns {Promise<Response|null>} - Cached response or null if not found
661
+ */
662
+ function _switchCachePartition() {
663
+ _switchCachePartition = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee15(isAuthenticated) {
664
+ var oldCacheName, partitionToDelete, deleted, newCacheName, _t1, _t10;
665
+ return _regenerator().w(function (_context15) {
666
+ while (1) switch (_context15.n) {
667
+ case 0:
668
+ _context15.p = 0;
669
+ console.log("\uD83D\uDD04 Switching cache partition (authenticated: ".concat(isAuthenticated, ")..."));
670
+ _context15.n = 1;
671
+ return cacheManager.getCacheName();
672
+ case 1:
673
+ oldCacheName = _context15.v;
674
+ // Reset identity to force re-evaluation of cache name
675
+ cacheManager.resetIdentity();
676
+ partitionToDelete = null;
677
+ if (isAuthenticated) {
678
+ // User logged in - delete anonymous cache
679
+ partitionToDelete = "".concat(CACHE_NAME_PREFIX, "-anonymous");
680
+ } else {
681
+ // User logged out - delete their user-specific cache
682
+ partitionToDelete = oldCacheName;
683
+ }
684
+ if (!partitionToDelete) {
685
+ _context15.n = 3;
686
+ break;
687
+ }
688
+ _context15.n = 2;
689
+ return cacheManager.deleteCache(partitionToDelete);
690
+ case 2:
691
+ _t1 = _context15.v;
692
+ _context15.n = 4;
693
+ break;
694
+ case 3:
695
+ _t1 = false;
696
+ case 4:
697
+ deleted = _t1;
698
+ _context15.n = 5;
699
+ return cacheManager.getCacheName();
700
+ case 5:
701
+ newCacheName = _context15.v;
702
+ console.log("\u2705 Cache partition switched:", {
703
+ from: oldCacheName,
704
+ to: newCacheName,
705
+ deleted: partitionToDelete
706
+ });
707
+ return _context15.a(2, {
708
+ oldCacheName: oldCacheName,
709
+ newCacheName: newCacheName,
710
+ deletedPartition: partitionToDelete,
711
+ deleted: deleted
712
+ });
713
+ case 6:
714
+ _context15.p = 6;
715
+ _t10 = _context15.v;
716
+ console.error('Failed to switch cache partition:', _t10);
717
+ return _context15.a(2, {
718
+ error: _t10.message
719
+ });
720
+ }
721
+ }, _callee15, null, [[0, 6]]);
722
+ }));
723
+ return _switchCachePartition.apply(this, arguments);
724
+ }
725
+ function findInAllCachePartitions(_x3) {
726
+ return _findInAllCachePartitions.apply(this, arguments);
727
+ }
728
+ function _findInAllCachePartitions() {
729
+ _findInAllCachePartitions = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee16(cacheKey) {
730
+ var allCacheNames, appCaches, encodedCacheKey, _iterator2, _step2, cacheName, cache, response, _t11;
731
+ return _regenerator().w(function (_context16) {
732
+ while (1) switch (_context16.n) {
733
+ case 0:
734
+ if ('caches' in window) {
735
+ _context16.n = 1;
736
+ break;
737
+ }
738
+ return _context16.a(2, null);
739
+ case 1:
740
+ _context16.n = 2;
741
+ return caches.keys();
742
+ case 2:
743
+ allCacheNames = _context16.v;
744
+ appCaches = allCacheNames.filter(function (name) {
745
+ return name.startsWith(CACHE_NAME_PREFIX);
746
+ }); // Also try URL-encoded version of the key in case spaces/special chars were encoded
747
+ encodedCacheKey = encodeURI(cacheKey);
748
+ _iterator2 = _createForOfIteratorHelper(appCaches);
749
+ _context16.p = 3;
750
+ _iterator2.s();
751
+ case 4:
752
+ if ((_step2 = _iterator2.n()).done) {
753
+ _context16.n = 10;
754
+ break;
755
+ }
756
+ cacheName = _step2.value;
757
+ _context16.n = 5;
758
+ return caches.open(cacheName);
759
+ case 5:
760
+ cache = _context16.v;
761
+ _context16.n = 6;
762
+ return cache.match(cacheKey);
763
+ case 6:
764
+ response = _context16.v;
765
+ if (!response) {
766
+ _context16.n = 7;
767
+ break;
768
+ }
769
+ console.log("\u2705 [findInAllCachePartitions] Found in ".concat(cacheName, " (exact match): ").concat(cacheKey.substring(0, 80), "..."));
770
+ return _context16.a(2, response);
771
+ case 7:
772
+ if (!(encodedCacheKey !== cacheKey)) {
773
+ _context16.n = 9;
774
+ break;
775
+ }
776
+ _context16.n = 8;
777
+ return cache.match(encodedCacheKey);
778
+ case 8:
779
+ response = _context16.v;
780
+ if (!response) {
781
+ _context16.n = 9;
782
+ break;
783
+ }
784
+ console.log("\u2705 [findInAllCachePartitions] Found in ".concat(cacheName, " (encoded match): ").concat(encodedCacheKey.substring(0, 80), "..."));
785
+ return _context16.a(2, response);
786
+ case 9:
787
+ _context16.n = 4;
788
+ break;
789
+ case 10:
790
+ _context16.n = 12;
791
+ break;
792
+ case 11:
793
+ _context16.p = 11;
794
+ _t11 = _context16.v;
795
+ _iterator2.e(_t11);
796
+ case 12:
797
+ _context16.p = 12;
798
+ _iterator2.f();
799
+ return _context16.f(12);
800
+ case 13:
801
+ return _context16.a(2, null);
802
+ }
803
+ }, _callee16, null, [[3, 11, 12, 13]]);
804
+ }));
805
+ return _findInAllCachePartitions.apply(this, arguments);
806
+ }
807
+ function getCachedS3Object(_x4) {
808
+ return _getCachedS3Object.apply(this, arguments);
809
+ }
810
+ function _getCachedS3Object() {
811
+ _getCachedS3Object = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee19(s3Key) {
812
+ var storageOptions,
813
+ expiryMs,
814
+ cacheKey,
815
+ expiry,
816
+ cachedResponse,
817
+ cachedTime,
818
+ age,
819
+ _allKeys,
820
+ matchingKeys,
821
+ _args19 = arguments;
822
+ return _regenerator().w(function (_context19) {
823
+ while (1) switch (_context19.n) {
824
+ case 0:
825
+ storageOptions = _args19.length > 1 && _args19[1] !== undefined ? _args19[1] : {};
826
+ expiryMs = _args19.length > 2 && _args19[2] !== undefined ? _args19[2] : null;
827
+ cacheKey = "https://s3.cache/".concat(s3Key);
828
+ expiry = expiryMs || cacheManager.getExpiryForPath(s3Key);
829
+ console.log("\uD83D\uDD0D [S3Cache] Checking for cached S3 object:", {
830
+ s3Key: s3Key,
831
+ cacheKey: cacheKey
832
+ });
833
+
834
+ // First, check ALL app cache partitions for a hit (handles auth race condition)
835
+ _context19.n = 1;
836
+ return findInAllCachePartitions(cacheKey);
837
+ case 1:
838
+ cachedResponse = _context19.v;
839
+ if (!cachedResponse) {
840
+ _context19.n = 4;
841
+ break;
842
+ }
843
+ cachedTime = cachedResponse.headers.get('x-cached-time');
844
+ age = Date.now() - parseInt(cachedTime || '0');
845
+ console.log("\u2705 [S3Cache] Found in cache partition, age: ".concat(Math.round(age / 1000), "s, expiry: ").concat(Math.round(expiry / 1000), "s"));
846
+ if (!(age < expiry)) {
847
+ _context19.n = 3;
848
+ break;
849
+ }
850
+ console.log("\u2705 [S3Cache] Returning cached blob for: ".concat(s3Key));
851
+ _context19.n = 2;
852
+ return cachedResponse.blob();
853
+ case 2:
854
+ return _context19.a(2, _context19.v);
855
+ case 3:
856
+ console.log("\u23F0 [S3Cache] Cache entry expired, will re-fetch");
857
+ _context19.n = 6;
858
+ break;
859
+ case 4:
860
+ console.log("\u274C [S3Cache] Not found in any cache partition");
861
+ _context19.n = 5;
862
+ return getAllCacheKeys();
863
+ case 5:
864
+ _allKeys = _context19.v;
865
+ matchingKeys = _toConsumableArray(_allKeys).filter(function (k) {
866
+ return k.includes('24dd6a7a') || k.includes('.glb');
867
+ });
868
+ if (matchingKeys.length > 0) {
869
+ console.log("\uD83D\uDD0D [S3Cache] Available GLB cache keys:", matchingKeys.slice(0, 5));
870
+ }
871
+ case 6:
872
+ return _context19.a(2, cacheManager.execute({
873
+ cacheKey: cacheKey,
874
+ sourceKey: s3Key,
875
+ expiryMs: expiry,
876
+ fetcher: function () {
877
+ var _fetcher = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee17() {
878
+ var Storage, Auth, result, _result, _t12;
879
+ return _regenerator().w(function (_context17) {
880
+ while (1) switch (_context17.n) {
881
+ case 0:
882
+ _context17.n = 1;
883
+ return requireAuthentication();
884
+ case 1:
885
+ Storage = getStorage();
886
+ Auth = getAuth();
887
+ _context17.p = 2;
888
+ _context17.n = 3;
889
+ return Storage.get(s3Key, _objectSpread2(_objectSpread2({}, storageOptions), {}, {
890
+ download: true
891
+ }));
892
+ case 3:
893
+ result = _context17.v;
894
+ return _context17.a(2, result.Body);
895
+ case 4:
896
+ _context17.p = 4;
897
+ _t12 = _context17.v;
898
+ if (!(_t12.code === 'ExpiredToken' || _t12.message && _t12.message.includes('ExpiredToken'))) {
899
+ _context17.n = 7;
900
+ break;
901
+ }
902
+ console.warn("\u26A0\uFE0F Token expired fetching S3 object. Refreshing credentials and retrying...");
903
+ _context17.n = 5;
904
+ return Auth.currentCredentials({
905
+ bypassCache: true
906
+ });
907
+ case 5:
908
+ _context17.n = 6;
909
+ return Storage.get(s3Key, _objectSpread2(_objectSpread2({}, storageOptions), {}, {
910
+ download: true
911
+ }));
912
+ case 6:
913
+ _result = _context17.v;
914
+ return _context17.a(2, _result.Body);
915
+ case 7:
916
+ throw _t12;
917
+ case 8:
918
+ return _context17.a(2);
919
+ }
920
+ }, _callee17, null, [[2, 4]]);
921
+ }));
922
+ function fetcher() {
923
+ return _fetcher.apply(this, arguments);
924
+ }
925
+ return fetcher;
926
+ }(),
927
+ processor: function () {
928
+ var _processor = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee18(res) {
929
+ return _regenerator().w(function (_context18) {
930
+ while (1) switch (_context18.n) {
931
+ case 0:
932
+ if (!(res instanceof Response)) {
933
+ _context18.n = 2;
934
+ break;
935
+ }
936
+ _context18.n = 1;
937
+ return res.blob();
938
+ case 1:
939
+ return _context18.a(2, _context18.v);
940
+ case 2:
941
+ return _context18.a(2, res);
942
+ }
943
+ }, _callee18);
944
+ }));
945
+ function processor(_x18) {
946
+ return _processor.apply(this, arguments);
947
+ }
948
+ return processor;
949
+ }(),
950
+ logType: 'S3 Object'
951
+ }));
952
+ }
953
+ }, _callee19);
954
+ }));
955
+ return _getCachedS3Object.apply(this, arguments);
956
+ }
957
+ function getCachedS3ObjectURL(_x5) {
958
+ return _getCachedS3ObjectURL.apply(this, arguments);
959
+ }
960
+ function _getCachedS3ObjectURL() {
961
+ _getCachedS3ObjectURL = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee20(s3Key) {
962
+ var storageOptions,
963
+ expiryMs,
964
+ blob,
965
+ _args20 = arguments;
966
+ return _regenerator().w(function (_context20) {
967
+ while (1) switch (_context20.n) {
968
+ case 0:
969
+ storageOptions = _args20.length > 1 && _args20[1] !== undefined ? _args20[1] : {};
970
+ expiryMs = _args20.length > 2 && _args20[2] !== undefined ? _args20[2] : null;
971
+ _context20.n = 1;
972
+ return getCachedS3Object(s3Key, storageOptions, expiryMs);
973
+ case 1:
974
+ blob = _context20.v;
975
+ return _context20.a(2, URL.createObjectURL(blob));
976
+ }
977
+ }, _callee20);
978
+ }));
979
+ return _getCachedS3ObjectURL.apply(this, arguments);
980
+ }
981
+ function preloadS3Objects(_x6) {
982
+ return _preloadS3Objects.apply(this, arguments);
983
+ }
984
+ function _preloadS3Objects() {
985
+ _preloadS3Objects = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee22(s3Keys) {
986
+ var storageOptions,
987
+ results,
988
+ _args22 = arguments;
989
+ return _regenerator().w(function (_context22) {
990
+ while (1) switch (_context22.n) {
991
+ case 0:
992
+ storageOptions = _args22.length > 1 && _args22[1] !== undefined ? _args22[1] : {};
993
+ results = {
994
+ success: 0,
995
+ failed: 0,
996
+ errors: []
997
+ };
998
+ _context22.n = 1;
999
+ return Promise.all(s3Keys.map(/*#__PURE__*/function () {
1000
+ var _ref4 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee21(key) {
1001
+ var _t13;
1002
+ return _regenerator().w(function (_context21) {
1003
+ while (1) switch (_context21.n) {
1004
+ case 0:
1005
+ _context21.p = 0;
1006
+ _context21.n = 1;
1007
+ return getCachedS3Object(key, storageOptions);
1008
+ case 1:
1009
+ results.success++;
1010
+ _context21.n = 3;
1011
+ break;
1012
+ case 2:
1013
+ _context21.p = 2;
1014
+ _t13 = _context21.v;
1015
+ results.failed++;
1016
+ results.errors.push({
1017
+ key: key,
1018
+ error: _t13.message
1019
+ });
1020
+ case 3:
1021
+ return _context21.a(2);
1022
+ }
1023
+ }, _callee21, null, [[0, 2]]);
1024
+ }));
1025
+ return function (_x19) {
1026
+ return _ref4.apply(this, arguments);
1027
+ };
1028
+ }()));
1029
+ case 1:
1030
+ console.log("\u2705 Preload complete: ".concat(results.success, " success, ").concat(results.failed, " failed"));
1031
+ return _context22.a(2, results);
1032
+ }
1033
+ }, _callee22);
1034
+ }));
1035
+ return _preloadS3Objects.apply(this, arguments);
1036
+ }
1037
+ function getCachedS3Json(_x7) {
1038
+ return _getCachedS3Json.apply(this, arguments);
1039
+ }
1040
+ function _getCachedS3Json() {
1041
+ _getCachedS3Json = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee24(s3Key) {
1042
+ var storageOptions,
1043
+ expiryMs,
1044
+ _args24 = arguments;
1045
+ return _regenerator().w(function (_context24) {
1046
+ while (1) switch (_context24.n) {
1047
+ case 0:
1048
+ storageOptions = _args24.length > 1 && _args24[1] !== undefined ? _args24[1] : {};
1049
+ expiryMs = _args24.length > 2 && _args24[2] !== undefined ? _args24[2] : null;
1050
+ return _context24.a(2, cacheManager.execute({
1051
+ cacheKey: "https://s3.cache/".concat(s3Key),
1052
+ sourceKey: s3Key,
1053
+ expiryMs: expiryMs || cacheManager.getExpiryForPath(s3Key),
1054
+ fetcher: function () {
1055
+ var _fetcher2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee23() {
1056
+ var Storage, Auth, signedUrl, _signedUrl, _t14;
1057
+ return _regenerator().w(function (_context23) {
1058
+ while (1) switch (_context23.n) {
1059
+ case 0:
1060
+ _context23.n = 1;
1061
+ return requireAuthentication();
1062
+ case 1:
1063
+ Storage = getStorage();
1064
+ Auth = getAuth();
1065
+ _context23.p = 2;
1066
+ _context23.n = 3;
1067
+ return Storage.get(s3Key, _objectSpread2(_objectSpread2({}, storageOptions), {}, {
1068
+ customPrefix: {
1069
+ public: ''
1070
+ }
1071
+ }));
1072
+ case 3:
1073
+ signedUrl = _context23.v;
1074
+ return _context23.a(2, fetch(signedUrl));
1075
+ case 4:
1076
+ _context23.p = 4;
1077
+ _t14 = _context23.v;
1078
+ if (!(_t14.code === 'ExpiredToken' || _t14.message && _t14.message.includes('ExpiredToken'))) {
1079
+ _context23.n = 7;
1080
+ break;
1081
+ }
1082
+ console.warn("\u26A0\uFE0F Token expired fetching S3 JSON. Refreshing credentials and retrying...");
1083
+ _context23.n = 5;
1084
+ return Auth.currentCredentials({
1085
+ bypassCache: true
1086
+ });
1087
+ case 5:
1088
+ _context23.n = 6;
1089
+ return Storage.get(s3Key, _objectSpread2(_objectSpread2({}, storageOptions), {}, {
1090
+ customPrefix: {
1091
+ public: ''
1092
+ }
1093
+ }));
1094
+ case 6:
1095
+ _signedUrl = _context23.v;
1096
+ return _context23.a(2, fetch(_signedUrl));
1097
+ case 7:
1098
+ throw _t14;
1099
+ case 8:
1100
+ return _context23.a(2);
1101
+ }
1102
+ }, _callee23, null, [[2, 4]]);
1103
+ }));
1104
+ function fetcher() {
1105
+ return _fetcher2.apply(this, arguments);
1106
+ }
1107
+ return fetcher;
1108
+ }(),
1109
+ processor: function processor(res) {
1110
+ return res.json();
1111
+ },
1112
+ logType: 'S3 JSON'
1113
+ }));
1114
+ }
1115
+ }, _callee24);
1116
+ }));
1117
+ return _getCachedS3Json.apply(this, arguments);
1118
+ }
1119
+ function getCachedLocalJson(_x8) {
1120
+ return _getCachedLocalJson.apply(this, arguments);
1121
+ }
1122
+ function _getCachedLocalJson() {
1123
+ _getCachedLocalJson = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee25(localPath) {
1124
+ return _regenerator().w(function (_context25) {
1125
+ while (1) switch (_context25.n) {
1126
+ case 0:
1127
+ return _context25.a(2, executeGlobal({
1128
+ cacheKey: "https://local.cache".concat(localPath),
1129
+ fetcher: function fetcher() {
1130
+ return fetch(localPath);
1131
+ },
1132
+ processor: function processor(res) {
1133
+ return res.json();
1134
+ },
1135
+ metadata: {
1136
+ 'x-local-path': localPath
1137
+ }
1138
+ }));
1139
+ }
1140
+ }, _callee25);
1141
+ }));
1142
+ return _getCachedLocalJson.apply(this, arguments);
1143
+ }
1144
+ function getCachedLocalFile(_x9) {
1145
+ return _getCachedLocalFile.apply(this, arguments);
1146
+ }
1147
+ function _getCachedLocalFile() {
1148
+ _getCachedLocalFile = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee26(url) {
1149
+ return _regenerator().w(function (_context26) {
1150
+ while (1) switch (_context26.n) {
1151
+ case 0:
1152
+ if (!(!url || url.includes('undefined') || url.includes('null'))) {
1153
+ _context26.n = 1;
1154
+ break;
1155
+ }
1156
+ throw new Error("Invalid URL: ".concat(url));
1157
+ case 1:
1158
+ return _context26.a(2, executeGlobal({
1159
+ cacheKey: "https://local.cache".concat(url),
1160
+ fetcher: function fetcher() {
1161
+ return fetch(url);
1162
+ },
1163
+ processor: function processor(res) {
1164
+ return res.blob();
1165
+ },
1166
+ metadata: {
1167
+ 'x-local-path': url
1168
+ }
1169
+ }));
1170
+ }
1171
+ }, _callee26);
1172
+ }));
1173
+ return _getCachedLocalFile.apply(this, arguments);
1174
+ }
1175
+ function getCachedLocalFileURL(_x0) {
1176
+ return _getCachedLocalFileURL.apply(this, arguments);
1177
+ }
1178
+ function _getCachedLocalFileURL() {
1179
+ _getCachedLocalFileURL = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee27(url) {
1180
+ var expiryMs,
1181
+ blob,
1182
+ _args27 = arguments;
1183
+ return _regenerator().w(function (_context27) {
1184
+ while (1) switch (_context27.n) {
1185
+ case 0:
1186
+ expiryMs = _args27.length > 1 && _args27[1] !== undefined ? _args27[1] : null;
1187
+ _context27.n = 1;
1188
+ return getCachedLocalFile(url, expiryMs);
1189
+ case 1:
1190
+ blob = _context27.v;
1191
+ return _context27.a(2, URL.createObjectURL(blob));
1192
+ }
1193
+ }, _callee27);
1194
+ }));
1195
+ return _getCachedLocalFileURL.apply(this, arguments);
1196
+ }
1197
+ function preloadLocalFiles(_x1) {
1198
+ return _preloadLocalFiles.apply(this, arguments);
1199
+ }
1200
+
1201
+ // --- Arbitrary JSON Helpers ---
1202
+ function _preloadLocalFiles() {
1203
+ _preloadLocalFiles = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee29(urls) {
1204
+ var results;
1205
+ return _regenerator().w(function (_context29) {
1206
+ while (1) switch (_context29.n) {
1207
+ case 0:
1208
+ console.log("preloadLocalFiles started");
1209
+ results = {
1210
+ success: 0,
1211
+ failed: 0,
1212
+ errors: []
1213
+ };
1214
+ _context29.n = 1;
1215
+ return Promise.all(urls.map(/*#__PURE__*/function () {
1216
+ var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee28(url) {
1217
+ var _t15;
1218
+ return _regenerator().w(function (_context28) {
1219
+ while (1) switch (_context28.n) {
1220
+ case 0:
1221
+ _context28.p = 0;
1222
+ _context28.n = 1;
1223
+ return getCachedLocalFile(url);
1224
+ case 1:
1225
+ results.success++;
1226
+ _context28.n = 3;
1227
+ break;
1228
+ case 2:
1229
+ _context28.p = 2;
1230
+ _t15 = _context28.v;
1231
+ console.error("\u274C preloadLocalFiles Preload failed for ".concat(url, ":"), _t15);
1232
+ results.failed++;
1233
+ results.errors.push({
1234
+ url: url,
1235
+ error: _t15.message
1236
+ });
1237
+ case 3:
1238
+ return _context28.a(2);
1239
+ }
1240
+ }, _callee28, null, [[0, 2]]);
1241
+ }));
1242
+ return function (_x20) {
1243
+ return _ref5.apply(this, arguments);
1244
+ };
1245
+ }()));
1246
+ case 1:
1247
+ console.log("preloadLocalFiles finished");
1248
+ if (results.failed > 0) {
1249
+ console.warn("\u26A0\uFE0F preloadLocalFiles Cache priming finished with errors: ".concat(results.failed, " failed"), results.errors);
1250
+ }
1251
+ return _context29.a(2, results);
1252
+ }
1253
+ }, _callee29);
1254
+ }));
1255
+ return _preloadLocalFiles.apply(this, arguments);
1256
+ }
1257
+ function cacheJsonData(_x10, _x11) {
1258
+ return _cacheJsonData.apply(this, arguments);
1259
+ }
1260
+ function _cacheJsonData() {
1261
+ _cacheJsonData = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee32(key, data) {
1262
+ var expiryMs,
1263
+ cacheKey,
1264
+ _args32 = arguments;
1265
+ return _regenerator().w(function (_context32) {
1266
+ while (1) switch (_context32.n) {
1267
+ case 0:
1268
+ expiryMs = _args32.length > 2 && _args32[2] !== undefined ? _args32[2] : CACHE_EXPIRY.JSON;
1269
+ cacheKey = "https://json.cache/".concat(key); // Delete existing entry first to ensure we overwrite with new data
1270
+ _context32.n = 1;
1271
+ return cacheManager.delete(cacheKey);
1272
+ case 1:
1273
+ return _context32.a(2, cacheManager.execute({
1274
+ cacheKey: cacheKey,
1275
+ sourceKey: key,
1276
+ expiryMs: expiryMs,
1277
+ fetcher: function () {
1278
+ var _fetcher3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee30() {
1279
+ return _regenerator().w(function (_context30) {
1280
+ while (1) switch (_context30.n) {
1281
+ case 0:
1282
+ return _context30.a(2, new Response(JSON.stringify(data), {
1283
+ headers: {
1284
+ 'Content-Type': 'application/json'
1285
+ }
1286
+ }));
1287
+ }
1288
+ }, _callee30);
1289
+ }));
1290
+ function fetcher() {
1291
+ return _fetcher3.apply(this, arguments);
1292
+ }
1293
+ return fetcher;
1294
+ }(),
1295
+ processor: function () {
1296
+ var _processor2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee31(res) {
1297
+ return _regenerator().w(function (_context31) {
1298
+ while (1) switch (_context31.n) {
1299
+ case 0:
1300
+ return _context31.a(2, data);
1301
+ }
1302
+ }, _callee31);
1303
+ }));
1304
+ function processor(_x21) {
1305
+ return _processor2.apply(this, arguments);
1306
+ }
1307
+ return processor;
1308
+ }(),
1309
+ // Return the original data to the caller
1310
+ logType: 'Arbitrary JSON'
1311
+ }).then(function () {
1312
+ return true;
1313
+ }).catch(function (e) {
1314
+ console.error("Failed to cache JSON data ".concat(key, ":"), e);
1315
+ return false;
1316
+ }));
1317
+ }
1318
+ }, _callee32);
1319
+ }));
1320
+ return _cacheJsonData.apply(this, arguments);
1321
+ }
1322
+ function getCachedJsonData(_x12) {
1323
+ return _getCachedJsonData.apply(this, arguments);
1324
+ }
1325
+ function _getCachedJsonData() {
1326
+ _getCachedJsonData = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee34(key) {
1327
+ var expiryMs,
1328
+ _args34 = arguments;
1329
+ return _regenerator().w(function (_context34) {
1330
+ while (1) switch (_context34.n) {
1331
+ case 0:
1332
+ expiryMs = _args34.length > 1 && _args34[1] !== undefined ? _args34[1] : CACHE_EXPIRY.JSON;
1333
+ return _context34.a(2, cacheManager.get("https://json.cache/".concat(key), expiryMs, /*#__PURE__*/function () {
1334
+ var _ref6 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee33(r) {
1335
+ return _regenerator().w(function (_context33) {
1336
+ while (1) switch (_context33.n) {
1337
+ case 0:
1338
+ return _context33.a(2, r.json());
1339
+ }
1340
+ }, _callee33);
1341
+ }));
1342
+ return function (_x22) {
1343
+ return _ref6.apply(this, arguments);
1344
+ };
1345
+ }()));
1346
+ }
1347
+ }, _callee34);
1348
+ }));
1349
+ return _getCachedJsonData.apply(this, arguments);
1350
+ }
1351
+ function removeCachedJsonData(_x13) {
1352
+ return _removeCachedJsonData.apply(this, arguments);
1353
+ }
1354
+
1355
+ // --- Thumbnails ---
1356
+ function _removeCachedJsonData() {
1357
+ _removeCachedJsonData = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee35(key) {
1358
+ return _regenerator().w(function (_context35) {
1359
+ while (1) switch (_context35.n) {
1360
+ case 0:
1361
+ return _context35.a(2, cacheManager.delete("https://json.cache/".concat(key)));
1362
+ }
1363
+ }, _callee35);
1364
+ }));
1365
+ return _removeCachedJsonData.apply(this, arguments);
1366
+ }
1367
+ function getThumbnailKey(asset) {
1368
+ var thumbnailKey = asset.thumbnailS3Path;
1369
+ if (!thumbnailKey && asset.s3Path) {
1370
+ var uuid = asset.uuid || asset.id;
1371
+ var thumbnailFilename = "".concat(uuid, ".png");
1372
+ thumbnailKey = "".concat(asset.s3Path, "/").concat(thumbnailFilename);
1373
+ }
1374
+ return thumbnailKey || null;
1375
+ }
1376
+ function isThumbnailCached(_x14) {
1377
+ return _isThumbnailCached.apply(this, arguments);
1378
+ }
1379
+ function _isThumbnailCached() {
1380
+ _isThumbnailCached = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee36(asset) {
1381
+ var isS3Asset, key;
1382
+ return _regenerator().w(function (_context36) {
1383
+ while (1) switch (_context36.n) {
1384
+ case 0:
1385
+ // Match the isS3Component logic: isS3Component || source === 's3' || scope === 'user'
1386
+ isS3Asset = asset.isS3Component || asset.source === 's3' || asset.scope === 'user';
1387
+ if (isS3Asset) {
1388
+ _context36.n = 1;
1389
+ break;
1390
+ }
1391
+ return _context36.a(2, true);
1392
+ case 1:
1393
+ key = getThumbnailKey(asset);
1394
+ if (key) {
1395
+ _context36.n = 2;
1396
+ break;
1397
+ }
1398
+ return _context36.a(2, false);
1399
+ case 2:
1400
+ return _context36.a(2, isCached(key));
1401
+ }
1402
+ }, _callee36);
1403
+ }));
1404
+ return _isThumbnailCached.apply(this, arguments);
1405
+ }
1406
+ function isCached(_x15) {
1407
+ return _isCached.apply(this, arguments);
1408
+ }
1409
+
1410
+ // --- Path/Expiry Helpers ---
1411
+ function _isCached() {
1412
+ _isCached = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee37(s3Key) {
1413
+ var expiryMs,
1414
+ _args37 = arguments;
1415
+ return _regenerator().w(function (_context37) {
1416
+ while (1) switch (_context37.n) {
1417
+ case 0:
1418
+ expiryMs = _args37.length > 1 && _args37[1] !== undefined ? _args37[1] : null;
1419
+ return _context37.a(2, cacheManager.has("https://s3.cache/".concat(s3Key), expiryMs));
1420
+ }
1421
+ }, _callee37);
1422
+ }));
1423
+ return _isCached.apply(this, arguments);
1424
+ }
1425
+ function getCacheExpiryForPath(path) {
1426
+ var assetType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
1427
+ return cacheManager.getExpiryForPath(path, assetType);
1428
+ }
1429
+ function formatCacheExpiry(ms) {
1430
+ var SECOND = 1000;
1431
+ var MINUTE = 60 * SECOND;
1432
+ var HOUR = 60 * MINUTE;
1433
+ var DAY = 24 * HOUR;
1434
+ var WEEK = 7 * DAY;
1435
+ if (ms >= WEEK) return "".concat((ms / WEEK).toFixed(1), " week(s)");
1436
+ if (ms >= DAY) return "".concat((ms / DAY).toFixed(1), " day(s)");
1437
+ if (ms >= HOUR) return "".concat((ms / HOUR).toFixed(1), " hour(s)");
1438
+ return "".concat((ms / 1000 / 60).toFixed(1), " minute(s)");
1439
+ }
1440
+
1441
+ export { GLOBAL_CACHE_NAME, cacheJsonData, cleanExpiredCache, cleanInvalidCacheEntries, clearS3Cache, formatCacheExpiry, getAllCacheKeys, getCacheExpiryForPath, getCacheStats, getCachedJsonData, getCachedLocalFile, getCachedLocalFileURL, getCachedLocalJson, getCachedS3Json, getCachedS3Object, getCachedS3ObjectURL, getCurrentCacheName, getGlobalCacheStats, getGlobalOnlyCacheStats, getThumbnailKey, getUserOnlyCacheStats, isCached, isThumbnailCached, preloadLocalFiles, preloadS3Objects, removeCachedJsonData, resetCacheIdentity, resetGlobalCacheStats, switchCachePartition };