@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.
@@ -37840,7 +37840,7 @@ var CentralPlant = /*#__PURE__*/function (_BaseDisposable) {
37840
37840
  * Initialize the CentralPlant manager
37841
37841
  *
37842
37842
  * @constructor
37843
- * @version 0.2.12
37843
+ * @version 0.3.1
37844
37844
  * @updated 2025-10-22
37845
37845
  *
37846
37846
  * @description Creates a new CentralPlant instance and initializes internal managers and utilities.
@@ -42101,9 +42101,14 @@ var CacheManager = /*#__PURE__*/function () {
42101
42101
  }
42102
42102
  return getUser;
42103
42103
  }(),
42104
- // Default error logger
42104
+ // Injected Amplify Auth instance (set by consuming app via configure())
42105
+ auth: null,
42106
+ // Injected Amplify Storage instance (set by consuming app via configure())
42107
+ storage: null,
42108
+ // Default error logger — suppresses expected not-authenticated errors
42105
42109
  onError: function onError(error) {
42106
- return console.error('CacheManager Error:', error);
42110
+ if ((error === null || error === void 0 ? void 0 : error.code) === 'S3CacheNotAuthenticated') return;
42111
+ console.error('CacheManager Error:', error);
42107
42112
  }
42108
42113
  };
42109
42114
  }
@@ -42181,10 +42186,6 @@ var CacheManager = /*#__PURE__*/function () {
42181
42186
  }
42182
42187
  return _getCacheHandle;
42183
42188
  }()
42184
- /**
42185
- * Get the cache name for the current user
42186
- * Memoized to prevent excessive Auth calls
42187
- */
42188
42189
  /**
42189
42190
  * Resets the identity, forcing a re-fetch of the user/cache name on next access
42190
42191
  */
@@ -42281,7 +42282,6 @@ var CacheManager = /*#__PURE__*/function () {
42281
42282
  case 5:
42282
42283
  result = _context4.v;
42283
42284
  this.stats.hits++;
42284
- result instanceof Blob ? result.size : 0;
42285
42285
  if (result instanceof Blob) this.stats.totalBytesServed += result.size;
42286
42286
  return _context4.a(2, result);
42287
42287
  case 6:
@@ -42340,7 +42340,13 @@ var CacheManager = /*#__PURE__*/function () {
42340
42340
  this.stats.errors++;
42341
42341
  this.config.onError(_t2);
42342
42342
 
42343
- // Fallback
42343
+ // If the error is marked noRetry, propagate immediately without a second fetch attempt
42344
+ if (!_t2.noRetry) {
42345
+ _context4.n = 14;
42346
+ break;
42347
+ }
42348
+ throw _t2;
42349
+ case 14:
42344
42350
  _context4.p = 14;
42345
42351
  _context4.n = 15;
42346
42352
  return fetcher();
@@ -42833,7 +42839,7 @@ var CacheManager = /*#__PURE__*/function () {
42833
42839
  return get;
42834
42840
  }()
42835
42841
  /**
42836
- * Delete a specific key from value
42842
+ * Delete a specific key from the cache
42837
42843
  */
42838
42844
  )
42839
42845
  }, {
@@ -43066,7 +43072,7 @@ var CacheManager = /*#__PURE__*/function () {
43066
43072
  return _response.blob();
43067
43073
  case 28:
43068
43074
  _blob = _context11.v;
43069
- cachedTime = _response.headers.get('x-cached-time'); // Try to get key from headers or fallback
43075
+ cachedTime = _response.headers.get('x-cached-time');
43070
43076
  sourceKey = _response.headers.get('x-source-key') || _response.headers.get('x-s3-key') || _response.headers.get('x-local-path') || _request.url;
43071
43077
  totalSize += _blob.size;
43072
43078
  items.push({
@@ -43123,6 +43129,2052 @@ var CacheManager = /*#__PURE__*/function () {
43123
43129
  }();
43124
43130
  var cacheManager = new CacheManager();
43125
43131
 
43132
+ /**
43133
+ * Retrieve the injected Auth instance. Throws if not configured.
43134
+ */
43135
+ function getAuth$1() {
43136
+ var auth = cacheManager.config.auth;
43137
+ if (!auth) throw new Error('cacheManager.configure({ auth }) must be called before using S3 cache functions');
43138
+ return auth;
43139
+ }
43140
+
43141
+ /**
43142
+ * Retrieve the injected Storage instance. Throws if not configured.
43143
+ */
43144
+ function getStorage() {
43145
+ var storage = cacheManager.config.storage;
43146
+ if (!storage) throw new Error('cacheManager.configure({ storage }) must be called before using S3 cache functions');
43147
+ return storage;
43148
+ }
43149
+
43150
+ /**
43151
+ * Guard used inside S3 fetchers. Throws a noRetry error (suppressed by onError)
43152
+ * instead of letting Storage.get() fail with an Amplify credential warning.
43153
+ */
43154
+ function requireAuthentication() {
43155
+ return _requireAuthentication.apply(this, arguments);
43156
+ }
43157
+ /**
43158
+ * Reset the cache manager identity.
43159
+ * Call this when auth state changes to ensure the correct cache partition is used.
43160
+ */
43161
+ function _requireAuthentication() {
43162
+ _requireAuthentication = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
43163
+ var err;
43164
+ return _regenerator().w(function (_context) {
43165
+ while (1) switch (_context.n) {
43166
+ case 0:
43167
+ _context.p = 0;
43168
+ _context.n = 1;
43169
+ return getAuth$1().currentAuthenticatedUser();
43170
+ case 1:
43171
+ _context.n = 3;
43172
+ break;
43173
+ case 2:
43174
+ _context.p = 2;
43175
+ _context.v;
43176
+ err = new Error('S3 access requires authentication');
43177
+ err.code = 'S3CacheNotAuthenticated';
43178
+ err.noRetry = true;
43179
+ throw err;
43180
+ case 3:
43181
+ return _context.a(2);
43182
+ }
43183
+ }, _callee, null, [[0, 2]]);
43184
+ }));
43185
+ return _requireAuthentication.apply(this, arguments);
43186
+ }
43187
+ function resetCacheIdentity() {
43188
+ cacheManager.resetIdentity();
43189
+ console.log('🔄 Cache identity reset - will re-evaluate on next cache access');
43190
+ }
43191
+
43192
+ // --- Global Cache for Bundled/Static Access ---
43193
+ var GLOBAL_CACHE_NAME = 'asset-manager-global-v1';
43194
+ function getGlobalCache() {
43195
+ return _getGlobalCache.apply(this, arguments);
43196
+ }
43197
+ function _getGlobalCache() {
43198
+ _getGlobalCache = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
43199
+ return _regenerator().w(function (_context2) {
43200
+ while (1) switch (_context2.n) {
43201
+ case 0:
43202
+ if ('caches' in window) {
43203
+ _context2.n = 1;
43204
+ break;
43205
+ }
43206
+ return _context2.a(2, null);
43207
+ case 1:
43208
+ return _context2.a(2, caches.open(GLOBAL_CACHE_NAME));
43209
+ }
43210
+ }, _callee2);
43211
+ }));
43212
+ return _getGlobalCache.apply(this, arguments);
43213
+ }
43214
+ function executeGlobal(_x) {
43215
+ return _executeGlobal.apply(this, arguments);
43216
+ }
43217
+ /**
43218
+ * Clear the entire global cache (bundled models, thumbnails, etc.).
43219
+ */
43220
+ function _executeGlobal() {
43221
+ _executeGlobal = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(_ref) {
43222
+ var cacheKey, fetcher, processor, metadata, cache, cachedResponse, response, responseClone, headers, responseToCache, _response, _t2, _t3, _t4, _t5, _t6, _t7;
43223
+ return _regenerator().w(function (_context3) {
43224
+ while (1) switch (_context3.n) {
43225
+ case 0:
43226
+ cacheKey = _ref.cacheKey, fetcher = _ref.fetcher, processor = _ref.processor, metadata = _ref.metadata;
43227
+ _context3.p = 1;
43228
+ _context3.n = 2;
43229
+ return getGlobalCache();
43230
+ case 2:
43231
+ cache = _context3.v;
43232
+ if (cache) {
43233
+ _context3.n = 5;
43234
+ break;
43235
+ }
43236
+ _t2 = processor;
43237
+ _context3.n = 3;
43238
+ return fetcher();
43239
+ case 3:
43240
+ _t3 = _context3.v;
43241
+ _context3.n = 4;
43242
+ return _t2(_t3);
43243
+ case 4:
43244
+ return _context3.a(2, _context3.v);
43245
+ case 5:
43246
+ _context3.n = 6;
43247
+ return cache.match(cacheKey);
43248
+ case 6:
43249
+ cachedResponse = _context3.v;
43250
+ if (!cachedResponse) {
43251
+ _context3.n = 8;
43252
+ break;
43253
+ }
43254
+ _context3.n = 7;
43255
+ return processor(cachedResponse);
43256
+ case 7:
43257
+ return _context3.a(2, _context3.v);
43258
+ case 8:
43259
+ _context3.n = 9;
43260
+ return fetcher();
43261
+ case 9:
43262
+ response = _context3.v;
43263
+ // Clone for cache
43264
+ responseClone = response.clone(); // Store with metadata headers if possible
43265
+ headers = new Headers(responseClone.headers);
43266
+ headers.append('X-Cached-Time', Date.now().toString());
43267
+ if (metadata) {
43268
+ headers.append('X-Cache-Metadata', JSON.stringify(metadata));
43269
+ }
43270
+
43271
+ // We need to create a new Response with updated headers to store it
43272
+ _t4 = Response;
43273
+ _context3.n = 10;
43274
+ return responseClone.blob();
43275
+ case 10:
43276
+ _t5 = _context3.v;
43277
+ _t6 = {
43278
+ status: responseClone.status,
43279
+ statusText: responseClone.statusText,
43280
+ headers: headers
43281
+ };
43282
+ responseToCache = new _t4(_t5, _t6);
43283
+ _context3.n = 11;
43284
+ return cache.put(cacheKey, responseToCache);
43285
+ case 11:
43286
+ _context3.n = 12;
43287
+ return processor(response);
43288
+ case 12:
43289
+ return _context3.a(2, _context3.v);
43290
+ case 13:
43291
+ _context3.p = 13;
43292
+ _t7 = _context3.v;
43293
+ console.warn('Global Cache execution failed, falling back to network', _t7);
43294
+ _context3.n = 14;
43295
+ return fetcher();
43296
+ case 14:
43297
+ _response = _context3.v;
43298
+ _context3.n = 15;
43299
+ return processor(_response);
43300
+ case 15:
43301
+ return _context3.a(2, _context3.v);
43302
+ }
43303
+ }, _callee3, null, [[1, 13]]);
43304
+ }));
43305
+ return _executeGlobal.apply(this, arguments);
43306
+ }
43307
+ function clearGlobalCache() {
43308
+ return _clearGlobalCache.apply(this, arguments);
43309
+ }
43310
+ /**
43311
+ * Remove expired entries from the global cache.
43312
+ * Returns the number of entries removed.
43313
+ */
43314
+ function _clearGlobalCache() {
43315
+ _clearGlobalCache = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4() {
43316
+ var deleted, _t8;
43317
+ return _regenerator().w(function (_context4) {
43318
+ while (1) switch (_context4.n) {
43319
+ case 0:
43320
+ _context4.p = 0;
43321
+ if ('caches' in window) {
43322
+ _context4.n = 1;
43323
+ break;
43324
+ }
43325
+ return _context4.a(2, false);
43326
+ case 1:
43327
+ _context4.n = 2;
43328
+ return caches.delete(GLOBAL_CACHE_NAME);
43329
+ case 2:
43330
+ deleted = _context4.v;
43331
+ if (deleted) console.log('🗑️ Global cache cleared');
43332
+ return _context4.a(2, deleted);
43333
+ case 3:
43334
+ _context4.p = 3;
43335
+ _t8 = _context4.v;
43336
+ console.warn('Failed to clear global cache:', _t8);
43337
+ return _context4.a(2, false);
43338
+ }
43339
+ }, _callee4, null, [[0, 3]]);
43340
+ }));
43341
+ return _clearGlobalCache.apply(this, arguments);
43342
+ }
43343
+ function cleanExpiredGlobalCache() {
43344
+ return _cleanExpiredGlobalCache.apply(this, arguments);
43345
+ }
43346
+ function _cleanExpiredGlobalCache() {
43347
+ _cleanExpiredGlobalCache = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6() {
43348
+ var cache, keys, cleaned;
43349
+ return _regenerator().w(function (_context6) {
43350
+ while (1) switch (_context6.n) {
43351
+ case 0:
43352
+ _context6.n = 1;
43353
+ return getGlobalCache();
43354
+ case 1:
43355
+ cache = _context6.v;
43356
+ if (cache) {
43357
+ _context6.n = 2;
43358
+ break;
43359
+ }
43360
+ return _context6.a(2, 0);
43361
+ case 2:
43362
+ _context6.n = 3;
43363
+ return cache.keys();
43364
+ case 3:
43365
+ keys = _context6.v;
43366
+ cleaned = 0;
43367
+ _context6.n = 4;
43368
+ return Promise.all(keys.map(/*#__PURE__*/function () {
43369
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(request) {
43370
+ var response, cachedTime, age, s3Key, expiryMs, _t9;
43371
+ return _regenerator().w(function (_context5) {
43372
+ while (1) switch (_context5.n) {
43373
+ case 0:
43374
+ _context5.p = 0;
43375
+ _context5.n = 1;
43376
+ return cache.match(request);
43377
+ case 1:
43378
+ response = _context5.v;
43379
+ if (response) {
43380
+ _context5.n = 2;
43381
+ break;
43382
+ }
43383
+ return _context5.a(2);
43384
+ case 2:
43385
+ cachedTime = parseInt(response.headers.get('X-Cached-Time') || '0');
43386
+ age = Date.now() - cachedTime;
43387
+ s3Key = request.url.replace('https://local.cache', '');
43388
+ expiryMs = cacheManager.getExpiryForPath(s3Key);
43389
+ if (!(age > expiryMs)) {
43390
+ _context5.n = 4;
43391
+ break;
43392
+ }
43393
+ _context5.n = 3;
43394
+ return cache.delete(request);
43395
+ case 3:
43396
+ cleaned++;
43397
+ case 4:
43398
+ _context5.n = 6;
43399
+ break;
43400
+ case 5:
43401
+ _context5.p = 5;
43402
+ _t9 = _context5.v;
43403
+ console.warn('Failed to check global cache entry:', _t9);
43404
+ case 6:
43405
+ return _context5.a(2);
43406
+ }
43407
+ }, _callee5, null, [[0, 5]]);
43408
+ }));
43409
+ return function (_x16) {
43410
+ return _ref2.apply(this, arguments);
43411
+ };
43412
+ }()));
43413
+ case 4:
43414
+ if (cleaned > 0) console.log("\uD83E\uDDF9 Cleaned ".concat(cleaned, " expired global cache entries"));
43415
+ return _context6.a(2, cleaned);
43416
+ }
43417
+ }, _callee6);
43418
+ }));
43419
+ return _cleanExpiredGlobalCache.apply(this, arguments);
43420
+ }
43421
+ function getGlobalCacheStatsDetail() {
43422
+ return _getGlobalCacheStatsDetail.apply(this, arguments);
43423
+ } // --- Exports delegating to CacheManager ---
43424
+ function _getGlobalCacheStatsDetail() {
43425
+ _getGlobalCacheStatsDetail = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8() {
43426
+ var cache, keys, items, totalSize;
43427
+ return _regenerator().w(function (_context8) {
43428
+ while (1) switch (_context8.n) {
43429
+ case 0:
43430
+ _context8.n = 1;
43431
+ return getGlobalCache();
43432
+ case 1:
43433
+ cache = _context8.v;
43434
+ if (cache) {
43435
+ _context8.n = 2;
43436
+ break;
43437
+ }
43438
+ return _context8.a(2, {
43439
+ count: 0,
43440
+ totalSize: 0,
43441
+ totalSizeMB: '0.00',
43442
+ items: []
43443
+ });
43444
+ case 2:
43445
+ _context8.n = 3;
43446
+ return cache.keys();
43447
+ case 3:
43448
+ keys = _context8.v;
43449
+ items = [];
43450
+ totalSize = 0;
43451
+ _context8.n = 4;
43452
+ return Promise.all(keys.map(/*#__PURE__*/function () {
43453
+ var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(request) {
43454
+ var response, cachedTime, metadataHeader, metadata, size, cl;
43455
+ return _regenerator().w(function (_context7) {
43456
+ while (1) switch (_context7.n) {
43457
+ case 0:
43458
+ _context7.n = 1;
43459
+ return cache.match(request);
43460
+ case 1:
43461
+ response = _context7.v;
43462
+ if (response) {
43463
+ _context7.n = 2;
43464
+ break;
43465
+ }
43466
+ return _context7.a(2);
43467
+ case 2:
43468
+ cachedTime = parseInt(response.headers.get('X-Cached-Time') || '0');
43469
+ metadataHeader = response.headers.get('X-Cache-Metadata');
43470
+ metadata = metadataHeader ? JSON.parse(metadataHeader) : {};
43471
+ size = 0;
43472
+ cl = response.headers.get('Content-Length');
43473
+ if (cl) size = parseInt(cl);
43474
+ totalSize += size;
43475
+ items.push(_objectSpread2({
43476
+ url: request.url,
43477
+ s3Key: request.url.replace('https://local.cache', ''),
43478
+ size: size,
43479
+ sizeMB: (size / (1024 * 1024)).toFixed(2),
43480
+ cachedTime: cachedTime,
43481
+ age: Date.now() - cachedTime,
43482
+ isLocal: true,
43483
+ type: response.headers.get('Content-Type') || 'application/octet-stream'
43484
+ }, metadata));
43485
+ case 3:
43486
+ return _context7.a(2);
43487
+ }
43488
+ }, _callee7);
43489
+ }));
43490
+ return function (_x17) {
43491
+ return _ref3.apply(this, arguments);
43492
+ };
43493
+ }()));
43494
+ case 4:
43495
+ return _context8.a(2, {
43496
+ count: items.length,
43497
+ totalSize: totalSize,
43498
+ totalSizeMB: (totalSize / (1024 * 1024)).toFixed(2),
43499
+ items: items
43500
+ });
43501
+ }
43502
+ }, _callee8);
43503
+ }));
43504
+ return _getGlobalCacheStatsDetail.apply(this, arguments);
43505
+ }
43506
+ function getGlobalCacheStats() {
43507
+ return cacheManager.getGlobalStats();
43508
+ }
43509
+ function resetGlobalCacheStats() {
43510
+ cacheManager.resetStats();
43511
+ }
43512
+ function getCurrentCacheName() {
43513
+ return _getCurrentCacheName.apply(this, arguments);
43514
+ }
43515
+ function _getCurrentCacheName() {
43516
+ _getCurrentCacheName = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9() {
43517
+ return _regenerator().w(function (_context9) {
43518
+ while (1) switch (_context9.n) {
43519
+ case 0:
43520
+ return _context9.a(2, cacheManager.getCacheName());
43521
+ }
43522
+ }, _callee9);
43523
+ }));
43524
+ return _getCurrentCacheName.apply(this, arguments);
43525
+ }
43526
+ function cleanExpiredCache() {
43527
+ return _cleanExpiredCache.apply(this, arguments);
43528
+ }
43529
+ function _cleanExpiredCache() {
43530
+ _cleanExpiredCache = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0() {
43531
+ var userCount, globalCount, count;
43532
+ return _regenerator().w(function (_context0) {
43533
+ while (1) switch (_context0.n) {
43534
+ case 0:
43535
+ _context0.n = 1;
43536
+ return cacheManager.cleanExpired();
43537
+ case 1:
43538
+ userCount = _context0.v;
43539
+ _context0.n = 2;
43540
+ return cleanExpiredGlobalCache();
43541
+ case 2:
43542
+ globalCount = _context0.v;
43543
+ count = userCount + globalCount;
43544
+ if (count > 0) console.log("\uD83E\uDDF9 Cleaned ".concat(count, " expired cache entries (user: ").concat(userCount, ", global: ").concat(globalCount, ")"));
43545
+ return _context0.a(2, count);
43546
+ }
43547
+ }, _callee0);
43548
+ }));
43549
+ return _cleanExpiredCache.apply(this, arguments);
43550
+ }
43551
+ function cleanInvalidCacheEntries() {
43552
+ return _cleanInvalidCacheEntries.apply(this, arguments);
43553
+ }
43554
+ function _cleanInvalidCacheEntries() {
43555
+ _cleanInvalidCacheEntries = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1() {
43556
+ var count;
43557
+ return _regenerator().w(function (_context1) {
43558
+ while (1) switch (_context1.n) {
43559
+ case 0:
43560
+ _context1.n = 1;
43561
+ return cacheManager.cleanInvalid();
43562
+ case 1:
43563
+ count = _context1.v;
43564
+ if (count > 0) console.log("\uD83E\uDDF9 Cleaned ".concat(count, " invalid cache entries"));
43565
+ return _context1.a(2, count);
43566
+ }
43567
+ }, _callee1);
43568
+ }));
43569
+ return _cleanInvalidCacheEntries.apply(this, arguments);
43570
+ }
43571
+ function getCacheStats() {
43572
+ return _getCacheStats.apply(this, arguments);
43573
+ }
43574
+ function _getCacheStats() {
43575
+ _getCacheStats = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee10() {
43576
+ var allUsers,
43577
+ userStats,
43578
+ globalStats,
43579
+ userItems,
43580
+ _args10 = arguments;
43581
+ return _regenerator().w(function (_context10) {
43582
+ while (1) switch (_context10.n) {
43583
+ case 0:
43584
+ allUsers = _args10.length > 0 && _args10[0] !== undefined ? _args10[0] : false;
43585
+ _context10.n = 1;
43586
+ return cacheManager.getDetailedStats(allUsers);
43587
+ case 1:
43588
+ userStats = _context10.v;
43589
+ _context10.n = 2;
43590
+ return getGlobalCacheStatsDetail();
43591
+ case 2:
43592
+ globalStats = _context10.v;
43593
+ // Ensure user stats items have isLocal: false
43594
+ userItems = userStats.items.map(function (item) {
43595
+ return _objectSpread2(_objectSpread2({}, item), {}, {
43596
+ isLocal: false
43597
+ });
43598
+ }); // Merge stats
43599
+ return _context10.a(2, {
43600
+ count: userStats.count + globalStats.count,
43601
+ totalSize: userStats.totalSize + globalStats.totalSize,
43602
+ totalSizeMB: (parseFloat(userStats.totalSizeMB) + parseFloat(globalStats.totalSizeMB)).toFixed(2),
43603
+ items: [].concat(_toConsumableArray(userItems), _toConsumableArray(globalStats.items))
43604
+ });
43605
+ }
43606
+ }, _callee10);
43607
+ }));
43608
+ return _getCacheStats.apply(this, arguments);
43609
+ }
43610
+ function getUserOnlyCacheStats() {
43611
+ return _getUserOnlyCacheStats.apply(this, arguments);
43612
+ }
43613
+ function _getUserOnlyCacheStats() {
43614
+ _getUserOnlyCacheStats = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee11() {
43615
+ var allUsers,
43616
+ stats,
43617
+ _args11 = arguments;
43618
+ return _regenerator().w(function (_context11) {
43619
+ while (1) switch (_context11.n) {
43620
+ case 0:
43621
+ allUsers = _args11.length > 0 && _args11[0] !== undefined ? _args11[0] : false;
43622
+ _context11.n = 1;
43623
+ return cacheManager.getDetailedStats(allUsers);
43624
+ case 1:
43625
+ stats = _context11.v;
43626
+ // Ensure items have isLocal: false
43627
+ stats.items = stats.items.map(function (item) {
43628
+ return _objectSpread2(_objectSpread2({}, item), {}, {
43629
+ isLocal: false
43630
+ });
43631
+ });
43632
+ return _context11.a(2, stats);
43633
+ }
43634
+ }, _callee11);
43635
+ }));
43636
+ return _getUserOnlyCacheStats.apply(this, arguments);
43637
+ }
43638
+ function getGlobalOnlyCacheStats() {
43639
+ return _getGlobalOnlyCacheStats.apply(this, arguments);
43640
+ }
43641
+ function _getGlobalOnlyCacheStats() {
43642
+ _getGlobalOnlyCacheStats = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee12() {
43643
+ return _regenerator().w(function (_context12) {
43644
+ while (1) switch (_context12.n) {
43645
+ case 0:
43646
+ _context12.n = 1;
43647
+ return getGlobalCacheStatsDetail();
43648
+ case 1:
43649
+ return _context12.a(2, _context12.v);
43650
+ }
43651
+ }, _callee12);
43652
+ }));
43653
+ return _getGlobalOnlyCacheStats.apply(this, arguments);
43654
+ }
43655
+ function clearS3Cache() {
43656
+ return _clearS3Cache.apply(this, arguments);
43657
+ }
43658
+ function _clearS3Cache() {
43659
+ _clearS3Cache = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee13() {
43660
+ var specificKey,
43661
+ clearAllUsers,
43662
+ result,
43663
+ _args13 = arguments;
43664
+ return _regenerator().w(function (_context13) {
43665
+ while (1) switch (_context13.n) {
43666
+ case 0:
43667
+ specificKey = _args13.length > 0 && _args13[0] !== undefined ? _args13[0] : null;
43668
+ clearAllUsers = _args13.length > 1 && _args13[1] !== undefined ? _args13[1] : false;
43669
+ _context13.n = 1;
43670
+ return cacheManager.clearCache(specificKey, clearAllUsers);
43671
+ case 1:
43672
+ result = _context13.v;
43673
+ if (specificKey) {
43674
+ _context13.n = 2;
43675
+ break;
43676
+ }
43677
+ _context13.n = 2;
43678
+ return clearGlobalCache();
43679
+ case 2:
43680
+ if (result) console.log(specificKey ? "\uD83D\uDDD1\uFE0F Cleared cache for ".concat(specificKey) : "\uD83D\uDDD1\uFE0F Cache cleared (user + global)");
43681
+ return _context13.a(2, result);
43682
+ }
43683
+ }, _callee13);
43684
+ }));
43685
+ return _clearS3Cache.apply(this, arguments);
43686
+ }
43687
+ function getAllCacheKeys() {
43688
+ return _getAllCacheKeys.apply(this, arguments);
43689
+ }
43690
+ function _getAllCacheKeys() {
43691
+ _getAllCacheKeys = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee14() {
43692
+ var allKeys, addKey, globalCache, keys, allCacheNames, appCaches, _iterator, _step, cacheName, cache, _keys, _t0;
43693
+ return _regenerator().w(function (_context14) {
43694
+ while (1) switch (_context14.n) {
43695
+ case 0:
43696
+ allKeys = new Set(); // Helper to add both raw and decoded versions for robust matching
43697
+ addKey = function addKey(url) {
43698
+ allKeys.add(url);
43699
+ try {
43700
+ var decoded = decodeURI(url);
43701
+ if (decoded !== url) allKeys.add(decoded);
43702
+ } catch (e) {
43703
+ // Ignore encoding errors
43704
+ }
43705
+ }; // 1. Get Global Cache Keys (Custom Global Cache)
43706
+ _context14.n = 1;
43707
+ return getGlobalCache();
43708
+ case 1:
43709
+ globalCache = _context14.v;
43710
+ if (!globalCache) {
43711
+ _context14.n = 3;
43712
+ break;
43713
+ }
43714
+ _context14.n = 2;
43715
+ return globalCache.keys();
43716
+ case 2:
43717
+ keys = _context14.v;
43718
+ keys.forEach(function (request) {
43719
+ return addKey(request.url);
43720
+ });
43721
+ case 3:
43722
+ if (!('caches' in window)) {
43723
+ _context14.n = 13;
43724
+ break;
43725
+ }
43726
+ _context14.n = 4;
43727
+ return caches.keys();
43728
+ case 4:
43729
+ allCacheNames = _context14.v;
43730
+ appCaches = allCacheNames.filter(function (name) {
43731
+ return name.startsWith(CACHE_NAME_PREFIX);
43732
+ });
43733
+ _iterator = _createForOfIteratorHelper(appCaches);
43734
+ _context14.p = 5;
43735
+ _iterator.s();
43736
+ case 6:
43737
+ if ((_step = _iterator.n()).done) {
43738
+ _context14.n = 10;
43739
+ break;
43740
+ }
43741
+ cacheName = _step.value;
43742
+ _context14.n = 7;
43743
+ return caches.open(cacheName);
43744
+ case 7:
43745
+ cache = _context14.v;
43746
+ if (!cache) {
43747
+ _context14.n = 9;
43748
+ break;
43749
+ }
43750
+ _context14.n = 8;
43751
+ return cache.keys();
43752
+ case 8:
43753
+ _keys = _context14.v;
43754
+ _keys.forEach(function (request) {
43755
+ return addKey(request.url);
43756
+ });
43757
+ case 9:
43758
+ _context14.n = 6;
43759
+ break;
43760
+ case 10:
43761
+ _context14.n = 12;
43762
+ break;
43763
+ case 11:
43764
+ _context14.p = 11;
43765
+ _t0 = _context14.v;
43766
+ _iterator.e(_t0);
43767
+ case 12:
43768
+ _context14.p = 12;
43769
+ _iterator.f();
43770
+ return _context14.f(12);
43771
+ case 13:
43772
+ return _context14.a(2, allKeys);
43773
+ }
43774
+ }, _callee14, null, [[5, 11, 12, 13]]);
43775
+ }));
43776
+ return _getAllCacheKeys.apply(this, arguments);
43777
+ }
43778
+ function switchCachePartition(_x2) {
43779
+ return _switchCachePartition.apply(this, arguments);
43780
+ }
43781
+
43782
+ // --- Data Fetchers ---
43783
+
43784
+ /**
43785
+ * Check all app cache partitions for a cached response.
43786
+ * This handles the race condition where auth may not be hydrated yet on page refresh.
43787
+ * @param {string} cacheKey - The cache key to look for
43788
+ * @returns {Promise<Response|null>} - Cached response or null if not found
43789
+ */
43790
+ function _switchCachePartition() {
43791
+ _switchCachePartition = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee15(isAuthenticated) {
43792
+ var oldCacheName, partitionToDelete, deleted, newCacheName, _t1, _t10;
43793
+ return _regenerator().w(function (_context15) {
43794
+ while (1) switch (_context15.n) {
43795
+ case 0:
43796
+ _context15.p = 0;
43797
+ console.log("\uD83D\uDD04 Switching cache partition (authenticated: ".concat(isAuthenticated, ")..."));
43798
+ _context15.n = 1;
43799
+ return cacheManager.getCacheName();
43800
+ case 1:
43801
+ oldCacheName = _context15.v;
43802
+ // Reset identity to force re-evaluation of cache name
43803
+ cacheManager.resetIdentity();
43804
+ partitionToDelete = null;
43805
+ if (isAuthenticated) {
43806
+ // User logged in - delete anonymous cache
43807
+ partitionToDelete = "".concat(CACHE_NAME_PREFIX, "-anonymous");
43808
+ } else {
43809
+ // User logged out - delete their user-specific cache
43810
+ partitionToDelete = oldCacheName;
43811
+ }
43812
+ if (!partitionToDelete) {
43813
+ _context15.n = 3;
43814
+ break;
43815
+ }
43816
+ _context15.n = 2;
43817
+ return cacheManager.deleteCache(partitionToDelete);
43818
+ case 2:
43819
+ _t1 = _context15.v;
43820
+ _context15.n = 4;
43821
+ break;
43822
+ case 3:
43823
+ _t1 = false;
43824
+ case 4:
43825
+ deleted = _t1;
43826
+ _context15.n = 5;
43827
+ return cacheManager.getCacheName();
43828
+ case 5:
43829
+ newCacheName = _context15.v;
43830
+ console.log("\u2705 Cache partition switched:", {
43831
+ from: oldCacheName,
43832
+ to: newCacheName,
43833
+ deleted: partitionToDelete
43834
+ });
43835
+ return _context15.a(2, {
43836
+ oldCacheName: oldCacheName,
43837
+ newCacheName: newCacheName,
43838
+ deletedPartition: partitionToDelete,
43839
+ deleted: deleted
43840
+ });
43841
+ case 6:
43842
+ _context15.p = 6;
43843
+ _t10 = _context15.v;
43844
+ console.error('Failed to switch cache partition:', _t10);
43845
+ return _context15.a(2, {
43846
+ error: _t10.message
43847
+ });
43848
+ }
43849
+ }, _callee15, null, [[0, 6]]);
43850
+ }));
43851
+ return _switchCachePartition.apply(this, arguments);
43852
+ }
43853
+ function findInAllCachePartitions(_x3) {
43854
+ return _findInAllCachePartitions.apply(this, arguments);
43855
+ }
43856
+ function _findInAllCachePartitions() {
43857
+ _findInAllCachePartitions = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee16(cacheKey) {
43858
+ var allCacheNames, appCaches, encodedCacheKey, _iterator2, _step2, cacheName, cache, response, _t11;
43859
+ return _regenerator().w(function (_context16) {
43860
+ while (1) switch (_context16.n) {
43861
+ case 0:
43862
+ if ('caches' in window) {
43863
+ _context16.n = 1;
43864
+ break;
43865
+ }
43866
+ return _context16.a(2, null);
43867
+ case 1:
43868
+ _context16.n = 2;
43869
+ return caches.keys();
43870
+ case 2:
43871
+ allCacheNames = _context16.v;
43872
+ appCaches = allCacheNames.filter(function (name) {
43873
+ return name.startsWith(CACHE_NAME_PREFIX);
43874
+ }); // Also try URL-encoded version of the key in case spaces/special chars were encoded
43875
+ encodedCacheKey = encodeURI(cacheKey);
43876
+ _iterator2 = _createForOfIteratorHelper(appCaches);
43877
+ _context16.p = 3;
43878
+ _iterator2.s();
43879
+ case 4:
43880
+ if ((_step2 = _iterator2.n()).done) {
43881
+ _context16.n = 10;
43882
+ break;
43883
+ }
43884
+ cacheName = _step2.value;
43885
+ _context16.n = 5;
43886
+ return caches.open(cacheName);
43887
+ case 5:
43888
+ cache = _context16.v;
43889
+ _context16.n = 6;
43890
+ return cache.match(cacheKey);
43891
+ case 6:
43892
+ response = _context16.v;
43893
+ if (!response) {
43894
+ _context16.n = 7;
43895
+ break;
43896
+ }
43897
+ console.log("\u2705 [findInAllCachePartitions] Found in ".concat(cacheName, " (exact match): ").concat(cacheKey.substring(0, 80), "..."));
43898
+ return _context16.a(2, response);
43899
+ case 7:
43900
+ if (!(encodedCacheKey !== cacheKey)) {
43901
+ _context16.n = 9;
43902
+ break;
43903
+ }
43904
+ _context16.n = 8;
43905
+ return cache.match(encodedCacheKey);
43906
+ case 8:
43907
+ response = _context16.v;
43908
+ if (!response) {
43909
+ _context16.n = 9;
43910
+ break;
43911
+ }
43912
+ console.log("\u2705 [findInAllCachePartitions] Found in ".concat(cacheName, " (encoded match): ").concat(encodedCacheKey.substring(0, 80), "..."));
43913
+ return _context16.a(2, response);
43914
+ case 9:
43915
+ _context16.n = 4;
43916
+ break;
43917
+ case 10:
43918
+ _context16.n = 12;
43919
+ break;
43920
+ case 11:
43921
+ _context16.p = 11;
43922
+ _t11 = _context16.v;
43923
+ _iterator2.e(_t11);
43924
+ case 12:
43925
+ _context16.p = 12;
43926
+ _iterator2.f();
43927
+ return _context16.f(12);
43928
+ case 13:
43929
+ return _context16.a(2, null);
43930
+ }
43931
+ }, _callee16, null, [[3, 11, 12, 13]]);
43932
+ }));
43933
+ return _findInAllCachePartitions.apply(this, arguments);
43934
+ }
43935
+ function getCachedS3Object(_x4) {
43936
+ return _getCachedS3Object.apply(this, arguments);
43937
+ }
43938
+ function _getCachedS3Object() {
43939
+ _getCachedS3Object = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee19(s3Key) {
43940
+ var storageOptions,
43941
+ expiryMs,
43942
+ cacheKey,
43943
+ expiry,
43944
+ cachedResponse,
43945
+ cachedTime,
43946
+ age,
43947
+ _allKeys,
43948
+ matchingKeys,
43949
+ _args19 = arguments;
43950
+ return _regenerator().w(function (_context19) {
43951
+ while (1) switch (_context19.n) {
43952
+ case 0:
43953
+ storageOptions = _args19.length > 1 && _args19[1] !== undefined ? _args19[1] : {};
43954
+ expiryMs = _args19.length > 2 && _args19[2] !== undefined ? _args19[2] : null;
43955
+ cacheKey = "https://s3.cache/".concat(s3Key);
43956
+ expiry = expiryMs || cacheManager.getExpiryForPath(s3Key);
43957
+ console.log("\uD83D\uDD0D [S3Cache] Checking for cached S3 object:", {
43958
+ s3Key: s3Key,
43959
+ cacheKey: cacheKey
43960
+ });
43961
+
43962
+ // First, check ALL app cache partitions for a hit (handles auth race condition)
43963
+ _context19.n = 1;
43964
+ return findInAllCachePartitions(cacheKey);
43965
+ case 1:
43966
+ cachedResponse = _context19.v;
43967
+ if (!cachedResponse) {
43968
+ _context19.n = 4;
43969
+ break;
43970
+ }
43971
+ cachedTime = cachedResponse.headers.get('x-cached-time');
43972
+ age = Date.now() - parseInt(cachedTime || '0');
43973
+ console.log("\u2705 [S3Cache] Found in cache partition, age: ".concat(Math.round(age / 1000), "s, expiry: ").concat(Math.round(expiry / 1000), "s"));
43974
+ if (!(age < expiry)) {
43975
+ _context19.n = 3;
43976
+ break;
43977
+ }
43978
+ console.log("\u2705 [S3Cache] Returning cached blob for: ".concat(s3Key));
43979
+ _context19.n = 2;
43980
+ return cachedResponse.blob();
43981
+ case 2:
43982
+ return _context19.a(2, _context19.v);
43983
+ case 3:
43984
+ console.log("\u23F0 [S3Cache] Cache entry expired, will re-fetch");
43985
+ _context19.n = 6;
43986
+ break;
43987
+ case 4:
43988
+ console.log("\u274C [S3Cache] Not found in any cache partition");
43989
+ _context19.n = 5;
43990
+ return getAllCacheKeys();
43991
+ case 5:
43992
+ _allKeys = _context19.v;
43993
+ matchingKeys = _toConsumableArray(_allKeys).filter(function (k) {
43994
+ return k.includes('24dd6a7a') || k.includes('.glb');
43995
+ });
43996
+ if (matchingKeys.length > 0) {
43997
+ console.log("\uD83D\uDD0D [S3Cache] Available GLB cache keys:", matchingKeys.slice(0, 5));
43998
+ }
43999
+ case 6:
44000
+ return _context19.a(2, cacheManager.execute({
44001
+ cacheKey: cacheKey,
44002
+ sourceKey: s3Key,
44003
+ expiryMs: expiry,
44004
+ fetcher: function () {
44005
+ var _fetcher = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee17() {
44006
+ var Storage, Auth, result, _result, _t12;
44007
+ return _regenerator().w(function (_context17) {
44008
+ while (1) switch (_context17.n) {
44009
+ case 0:
44010
+ _context17.n = 1;
44011
+ return requireAuthentication();
44012
+ case 1:
44013
+ Storage = getStorage();
44014
+ Auth = getAuth$1();
44015
+ _context17.p = 2;
44016
+ _context17.n = 3;
44017
+ return Storage.get(s3Key, _objectSpread2(_objectSpread2({}, storageOptions), {}, {
44018
+ download: true
44019
+ }));
44020
+ case 3:
44021
+ result = _context17.v;
44022
+ return _context17.a(2, result.Body);
44023
+ case 4:
44024
+ _context17.p = 4;
44025
+ _t12 = _context17.v;
44026
+ if (!(_t12.code === 'ExpiredToken' || _t12.message && _t12.message.includes('ExpiredToken'))) {
44027
+ _context17.n = 7;
44028
+ break;
44029
+ }
44030
+ console.warn("\u26A0\uFE0F Token expired fetching S3 object. Refreshing credentials and retrying...");
44031
+ _context17.n = 5;
44032
+ return Auth.currentCredentials({
44033
+ bypassCache: true
44034
+ });
44035
+ case 5:
44036
+ _context17.n = 6;
44037
+ return Storage.get(s3Key, _objectSpread2(_objectSpread2({}, storageOptions), {}, {
44038
+ download: true
44039
+ }));
44040
+ case 6:
44041
+ _result = _context17.v;
44042
+ return _context17.a(2, _result.Body);
44043
+ case 7:
44044
+ throw _t12;
44045
+ case 8:
44046
+ return _context17.a(2);
44047
+ }
44048
+ }, _callee17, null, [[2, 4]]);
44049
+ }));
44050
+ function fetcher() {
44051
+ return _fetcher.apply(this, arguments);
44052
+ }
44053
+ return fetcher;
44054
+ }(),
44055
+ processor: function () {
44056
+ var _processor = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee18(res) {
44057
+ return _regenerator().w(function (_context18) {
44058
+ while (1) switch (_context18.n) {
44059
+ case 0:
44060
+ if (!(res instanceof Response)) {
44061
+ _context18.n = 2;
44062
+ break;
44063
+ }
44064
+ _context18.n = 1;
44065
+ return res.blob();
44066
+ case 1:
44067
+ return _context18.a(2, _context18.v);
44068
+ case 2:
44069
+ return _context18.a(2, res);
44070
+ }
44071
+ }, _callee18);
44072
+ }));
44073
+ function processor(_x18) {
44074
+ return _processor.apply(this, arguments);
44075
+ }
44076
+ return processor;
44077
+ }(),
44078
+ logType: 'S3 Object'
44079
+ }));
44080
+ }
44081
+ }, _callee19);
44082
+ }));
44083
+ return _getCachedS3Object.apply(this, arguments);
44084
+ }
44085
+ function getCachedS3ObjectURL(_x5) {
44086
+ return _getCachedS3ObjectURL.apply(this, arguments);
44087
+ }
44088
+ function _getCachedS3ObjectURL() {
44089
+ _getCachedS3ObjectURL = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee20(s3Key) {
44090
+ var storageOptions,
44091
+ expiryMs,
44092
+ blob,
44093
+ _args20 = arguments;
44094
+ return _regenerator().w(function (_context20) {
44095
+ while (1) switch (_context20.n) {
44096
+ case 0:
44097
+ storageOptions = _args20.length > 1 && _args20[1] !== undefined ? _args20[1] : {};
44098
+ expiryMs = _args20.length > 2 && _args20[2] !== undefined ? _args20[2] : null;
44099
+ _context20.n = 1;
44100
+ return getCachedS3Object(s3Key, storageOptions, expiryMs);
44101
+ case 1:
44102
+ blob = _context20.v;
44103
+ return _context20.a(2, URL.createObjectURL(blob));
44104
+ }
44105
+ }, _callee20);
44106
+ }));
44107
+ return _getCachedS3ObjectURL.apply(this, arguments);
44108
+ }
44109
+ function preloadS3Objects(_x6) {
44110
+ return _preloadS3Objects.apply(this, arguments);
44111
+ }
44112
+ function _preloadS3Objects() {
44113
+ _preloadS3Objects = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee22(s3Keys) {
44114
+ var storageOptions,
44115
+ results,
44116
+ _args22 = arguments;
44117
+ return _regenerator().w(function (_context22) {
44118
+ while (1) switch (_context22.n) {
44119
+ case 0:
44120
+ storageOptions = _args22.length > 1 && _args22[1] !== undefined ? _args22[1] : {};
44121
+ results = {
44122
+ success: 0,
44123
+ failed: 0,
44124
+ errors: []
44125
+ };
44126
+ _context22.n = 1;
44127
+ return Promise.all(s3Keys.map(/*#__PURE__*/function () {
44128
+ var _ref4 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee21(key) {
44129
+ var _t13;
44130
+ return _regenerator().w(function (_context21) {
44131
+ while (1) switch (_context21.n) {
44132
+ case 0:
44133
+ _context21.p = 0;
44134
+ _context21.n = 1;
44135
+ return getCachedS3Object(key, storageOptions);
44136
+ case 1:
44137
+ results.success++;
44138
+ _context21.n = 3;
44139
+ break;
44140
+ case 2:
44141
+ _context21.p = 2;
44142
+ _t13 = _context21.v;
44143
+ results.failed++;
44144
+ results.errors.push({
44145
+ key: key,
44146
+ error: _t13.message
44147
+ });
44148
+ case 3:
44149
+ return _context21.a(2);
44150
+ }
44151
+ }, _callee21, null, [[0, 2]]);
44152
+ }));
44153
+ return function (_x19) {
44154
+ return _ref4.apply(this, arguments);
44155
+ };
44156
+ }()));
44157
+ case 1:
44158
+ console.log("\u2705 Preload complete: ".concat(results.success, " success, ").concat(results.failed, " failed"));
44159
+ return _context22.a(2, results);
44160
+ }
44161
+ }, _callee22);
44162
+ }));
44163
+ return _preloadS3Objects.apply(this, arguments);
44164
+ }
44165
+ function getCachedS3Json(_x7) {
44166
+ return _getCachedS3Json.apply(this, arguments);
44167
+ }
44168
+ function _getCachedS3Json() {
44169
+ _getCachedS3Json = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee24(s3Key) {
44170
+ var storageOptions,
44171
+ expiryMs,
44172
+ _args24 = arguments;
44173
+ return _regenerator().w(function (_context24) {
44174
+ while (1) switch (_context24.n) {
44175
+ case 0:
44176
+ storageOptions = _args24.length > 1 && _args24[1] !== undefined ? _args24[1] : {};
44177
+ expiryMs = _args24.length > 2 && _args24[2] !== undefined ? _args24[2] : null;
44178
+ return _context24.a(2, cacheManager.execute({
44179
+ cacheKey: "https://s3.cache/".concat(s3Key),
44180
+ sourceKey: s3Key,
44181
+ expiryMs: expiryMs || cacheManager.getExpiryForPath(s3Key),
44182
+ fetcher: function () {
44183
+ var _fetcher2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee23() {
44184
+ var Storage, Auth, signedUrl, _signedUrl, _t14;
44185
+ return _regenerator().w(function (_context23) {
44186
+ while (1) switch (_context23.n) {
44187
+ case 0:
44188
+ _context23.n = 1;
44189
+ return requireAuthentication();
44190
+ case 1:
44191
+ Storage = getStorage();
44192
+ Auth = getAuth$1();
44193
+ _context23.p = 2;
44194
+ _context23.n = 3;
44195
+ return Storage.get(s3Key, _objectSpread2(_objectSpread2({}, storageOptions), {}, {
44196
+ customPrefix: {
44197
+ public: ''
44198
+ }
44199
+ }));
44200
+ case 3:
44201
+ signedUrl = _context23.v;
44202
+ return _context23.a(2, fetch(signedUrl));
44203
+ case 4:
44204
+ _context23.p = 4;
44205
+ _t14 = _context23.v;
44206
+ if (!(_t14.code === 'ExpiredToken' || _t14.message && _t14.message.includes('ExpiredToken'))) {
44207
+ _context23.n = 7;
44208
+ break;
44209
+ }
44210
+ console.warn("\u26A0\uFE0F Token expired fetching S3 JSON. Refreshing credentials and retrying...");
44211
+ _context23.n = 5;
44212
+ return Auth.currentCredentials({
44213
+ bypassCache: true
44214
+ });
44215
+ case 5:
44216
+ _context23.n = 6;
44217
+ return Storage.get(s3Key, _objectSpread2(_objectSpread2({}, storageOptions), {}, {
44218
+ customPrefix: {
44219
+ public: ''
44220
+ }
44221
+ }));
44222
+ case 6:
44223
+ _signedUrl = _context23.v;
44224
+ return _context23.a(2, fetch(_signedUrl));
44225
+ case 7:
44226
+ throw _t14;
44227
+ case 8:
44228
+ return _context23.a(2);
44229
+ }
44230
+ }, _callee23, null, [[2, 4]]);
44231
+ }));
44232
+ function fetcher() {
44233
+ return _fetcher2.apply(this, arguments);
44234
+ }
44235
+ return fetcher;
44236
+ }(),
44237
+ processor: function processor(res) {
44238
+ return res.json();
44239
+ },
44240
+ logType: 'S3 JSON'
44241
+ }));
44242
+ }
44243
+ }, _callee24);
44244
+ }));
44245
+ return _getCachedS3Json.apply(this, arguments);
44246
+ }
44247
+ function getCachedLocalJson(_x8) {
44248
+ return _getCachedLocalJson.apply(this, arguments);
44249
+ }
44250
+ function _getCachedLocalJson() {
44251
+ _getCachedLocalJson = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee25(localPath) {
44252
+ return _regenerator().w(function (_context25) {
44253
+ while (1) switch (_context25.n) {
44254
+ case 0:
44255
+ return _context25.a(2, executeGlobal({
44256
+ cacheKey: "https://local.cache".concat(localPath),
44257
+ fetcher: function fetcher() {
44258
+ return fetch(localPath);
44259
+ },
44260
+ processor: function processor(res) {
44261
+ return res.json();
44262
+ },
44263
+ metadata: {
44264
+ 'x-local-path': localPath
44265
+ }
44266
+ }));
44267
+ }
44268
+ }, _callee25);
44269
+ }));
44270
+ return _getCachedLocalJson.apply(this, arguments);
44271
+ }
44272
+ function getCachedLocalFile(_x9) {
44273
+ return _getCachedLocalFile.apply(this, arguments);
44274
+ }
44275
+ function _getCachedLocalFile() {
44276
+ _getCachedLocalFile = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee26(url) {
44277
+ return _regenerator().w(function (_context26) {
44278
+ while (1) switch (_context26.n) {
44279
+ case 0:
44280
+ if (!(!url || url.includes('undefined') || url.includes('null'))) {
44281
+ _context26.n = 1;
44282
+ break;
44283
+ }
44284
+ throw new Error("Invalid URL: ".concat(url));
44285
+ case 1:
44286
+ return _context26.a(2, executeGlobal({
44287
+ cacheKey: "https://local.cache".concat(url),
44288
+ fetcher: function fetcher() {
44289
+ return fetch(url);
44290
+ },
44291
+ processor: function processor(res) {
44292
+ return res.blob();
44293
+ },
44294
+ metadata: {
44295
+ 'x-local-path': url
44296
+ }
44297
+ }));
44298
+ }
44299
+ }, _callee26);
44300
+ }));
44301
+ return _getCachedLocalFile.apply(this, arguments);
44302
+ }
44303
+ function getCachedLocalFileURL(_x0) {
44304
+ return _getCachedLocalFileURL.apply(this, arguments);
44305
+ }
44306
+ function _getCachedLocalFileURL() {
44307
+ _getCachedLocalFileURL = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee27(url) {
44308
+ var expiryMs,
44309
+ blob,
44310
+ _args27 = arguments;
44311
+ return _regenerator().w(function (_context27) {
44312
+ while (1) switch (_context27.n) {
44313
+ case 0:
44314
+ expiryMs = _args27.length > 1 && _args27[1] !== undefined ? _args27[1] : null;
44315
+ _context27.n = 1;
44316
+ return getCachedLocalFile(url, expiryMs);
44317
+ case 1:
44318
+ blob = _context27.v;
44319
+ return _context27.a(2, URL.createObjectURL(blob));
44320
+ }
44321
+ }, _callee27);
44322
+ }));
44323
+ return _getCachedLocalFileURL.apply(this, arguments);
44324
+ }
44325
+ function preloadLocalFiles(_x1) {
44326
+ return _preloadLocalFiles.apply(this, arguments);
44327
+ }
44328
+
44329
+ // --- Arbitrary JSON Helpers ---
44330
+ function _preloadLocalFiles() {
44331
+ _preloadLocalFiles = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee29(urls) {
44332
+ var results;
44333
+ return _regenerator().w(function (_context29) {
44334
+ while (1) switch (_context29.n) {
44335
+ case 0:
44336
+ console.log("preloadLocalFiles started");
44337
+ results = {
44338
+ success: 0,
44339
+ failed: 0,
44340
+ errors: []
44341
+ };
44342
+ _context29.n = 1;
44343
+ return Promise.all(urls.map(/*#__PURE__*/function () {
44344
+ var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee28(url) {
44345
+ var _t15;
44346
+ return _regenerator().w(function (_context28) {
44347
+ while (1) switch (_context28.n) {
44348
+ case 0:
44349
+ _context28.p = 0;
44350
+ _context28.n = 1;
44351
+ return getCachedLocalFile(url);
44352
+ case 1:
44353
+ results.success++;
44354
+ _context28.n = 3;
44355
+ break;
44356
+ case 2:
44357
+ _context28.p = 2;
44358
+ _t15 = _context28.v;
44359
+ console.error("\u274C preloadLocalFiles Preload failed for ".concat(url, ":"), _t15);
44360
+ results.failed++;
44361
+ results.errors.push({
44362
+ url: url,
44363
+ error: _t15.message
44364
+ });
44365
+ case 3:
44366
+ return _context28.a(2);
44367
+ }
44368
+ }, _callee28, null, [[0, 2]]);
44369
+ }));
44370
+ return function (_x20) {
44371
+ return _ref5.apply(this, arguments);
44372
+ };
44373
+ }()));
44374
+ case 1:
44375
+ console.log("preloadLocalFiles finished");
44376
+ if (results.failed > 0) {
44377
+ console.warn("\u26A0\uFE0F preloadLocalFiles Cache priming finished with errors: ".concat(results.failed, " failed"), results.errors);
44378
+ }
44379
+ return _context29.a(2, results);
44380
+ }
44381
+ }, _callee29);
44382
+ }));
44383
+ return _preloadLocalFiles.apply(this, arguments);
44384
+ }
44385
+ function cacheJsonData(_x10, _x11) {
44386
+ return _cacheJsonData.apply(this, arguments);
44387
+ }
44388
+ function _cacheJsonData() {
44389
+ _cacheJsonData = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee32(key, data) {
44390
+ var expiryMs,
44391
+ cacheKey,
44392
+ _args32 = arguments;
44393
+ return _regenerator().w(function (_context32) {
44394
+ while (1) switch (_context32.n) {
44395
+ case 0:
44396
+ expiryMs = _args32.length > 2 && _args32[2] !== undefined ? _args32[2] : CACHE_EXPIRY.JSON;
44397
+ cacheKey = "https://json.cache/".concat(key); // Delete existing entry first to ensure we overwrite with new data
44398
+ _context32.n = 1;
44399
+ return cacheManager.delete(cacheKey);
44400
+ case 1:
44401
+ return _context32.a(2, cacheManager.execute({
44402
+ cacheKey: cacheKey,
44403
+ sourceKey: key,
44404
+ expiryMs: expiryMs,
44405
+ fetcher: function () {
44406
+ var _fetcher3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee30() {
44407
+ return _regenerator().w(function (_context30) {
44408
+ while (1) switch (_context30.n) {
44409
+ case 0:
44410
+ return _context30.a(2, new Response(JSON.stringify(data), {
44411
+ headers: {
44412
+ 'Content-Type': 'application/json'
44413
+ }
44414
+ }));
44415
+ }
44416
+ }, _callee30);
44417
+ }));
44418
+ function fetcher() {
44419
+ return _fetcher3.apply(this, arguments);
44420
+ }
44421
+ return fetcher;
44422
+ }(),
44423
+ processor: function () {
44424
+ var _processor2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee31(res) {
44425
+ return _regenerator().w(function (_context31) {
44426
+ while (1) switch (_context31.n) {
44427
+ case 0:
44428
+ return _context31.a(2, data);
44429
+ }
44430
+ }, _callee31);
44431
+ }));
44432
+ function processor(_x21) {
44433
+ return _processor2.apply(this, arguments);
44434
+ }
44435
+ return processor;
44436
+ }(),
44437
+ // Return the original data to the caller
44438
+ logType: 'Arbitrary JSON'
44439
+ }).then(function () {
44440
+ return true;
44441
+ }).catch(function (e) {
44442
+ console.error("Failed to cache JSON data ".concat(key, ":"), e);
44443
+ return false;
44444
+ }));
44445
+ }
44446
+ }, _callee32);
44447
+ }));
44448
+ return _cacheJsonData.apply(this, arguments);
44449
+ }
44450
+ function getCachedJsonData(_x12) {
44451
+ return _getCachedJsonData.apply(this, arguments);
44452
+ }
44453
+ function _getCachedJsonData() {
44454
+ _getCachedJsonData = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee34(key) {
44455
+ var expiryMs,
44456
+ _args34 = arguments;
44457
+ return _regenerator().w(function (_context34) {
44458
+ while (1) switch (_context34.n) {
44459
+ case 0:
44460
+ expiryMs = _args34.length > 1 && _args34[1] !== undefined ? _args34[1] : CACHE_EXPIRY.JSON;
44461
+ return _context34.a(2, cacheManager.get("https://json.cache/".concat(key), expiryMs, /*#__PURE__*/function () {
44462
+ var _ref6 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee33(r) {
44463
+ return _regenerator().w(function (_context33) {
44464
+ while (1) switch (_context33.n) {
44465
+ case 0:
44466
+ return _context33.a(2, r.json());
44467
+ }
44468
+ }, _callee33);
44469
+ }));
44470
+ return function (_x22) {
44471
+ return _ref6.apply(this, arguments);
44472
+ };
44473
+ }()));
44474
+ }
44475
+ }, _callee34);
44476
+ }));
44477
+ return _getCachedJsonData.apply(this, arguments);
44478
+ }
44479
+ function removeCachedJsonData(_x13) {
44480
+ return _removeCachedJsonData.apply(this, arguments);
44481
+ }
44482
+
44483
+ // --- Thumbnails ---
44484
+ function _removeCachedJsonData() {
44485
+ _removeCachedJsonData = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee35(key) {
44486
+ return _regenerator().w(function (_context35) {
44487
+ while (1) switch (_context35.n) {
44488
+ case 0:
44489
+ return _context35.a(2, cacheManager.delete("https://json.cache/".concat(key)));
44490
+ }
44491
+ }, _callee35);
44492
+ }));
44493
+ return _removeCachedJsonData.apply(this, arguments);
44494
+ }
44495
+ function getThumbnailKey(asset) {
44496
+ var thumbnailKey = asset.thumbnailS3Path;
44497
+ if (!thumbnailKey && asset.s3Path) {
44498
+ var uuid = asset.uuid || asset.id;
44499
+ var thumbnailFilename = "".concat(uuid, ".png");
44500
+ thumbnailKey = "".concat(asset.s3Path, "/").concat(thumbnailFilename);
44501
+ }
44502
+ return thumbnailKey || null;
44503
+ }
44504
+ function isThumbnailCached(_x14) {
44505
+ return _isThumbnailCached.apply(this, arguments);
44506
+ }
44507
+ function _isThumbnailCached() {
44508
+ _isThumbnailCached = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee36(asset) {
44509
+ var isS3Asset, key;
44510
+ return _regenerator().w(function (_context36) {
44511
+ while (1) switch (_context36.n) {
44512
+ case 0:
44513
+ // Match the isS3Component logic: isS3Component || source === 's3' || scope === 'user'
44514
+ isS3Asset = asset.isS3Component || asset.source === 's3' || asset.scope === 'user';
44515
+ if (isS3Asset) {
44516
+ _context36.n = 1;
44517
+ break;
44518
+ }
44519
+ return _context36.a(2, true);
44520
+ case 1:
44521
+ key = getThumbnailKey(asset);
44522
+ if (key) {
44523
+ _context36.n = 2;
44524
+ break;
44525
+ }
44526
+ return _context36.a(2, false);
44527
+ case 2:
44528
+ return _context36.a(2, isCached(key));
44529
+ }
44530
+ }, _callee36);
44531
+ }));
44532
+ return _isThumbnailCached.apply(this, arguments);
44533
+ }
44534
+ function isCached(_x15) {
44535
+ return _isCached.apply(this, arguments);
44536
+ }
44537
+
44538
+ // --- Path/Expiry Helpers ---
44539
+ function _isCached() {
44540
+ _isCached = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee37(s3Key) {
44541
+ var expiryMs,
44542
+ _args37 = arguments;
44543
+ return _regenerator().w(function (_context37) {
44544
+ while (1) switch (_context37.n) {
44545
+ case 0:
44546
+ expiryMs = _args37.length > 1 && _args37[1] !== undefined ? _args37[1] : null;
44547
+ return _context37.a(2, cacheManager.has("https://s3.cache/".concat(s3Key), expiryMs));
44548
+ }
44549
+ }, _callee37);
44550
+ }));
44551
+ return _isCached.apply(this, arguments);
44552
+ }
44553
+ function getCacheExpiryForPath(path) {
44554
+ var assetType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
44555
+ return cacheManager.getExpiryForPath(path, assetType);
44556
+ }
44557
+ function formatCacheExpiry(ms) {
44558
+ var SECOND = 1000;
44559
+ var MINUTE = 60 * SECOND;
44560
+ var HOUR = 60 * MINUTE;
44561
+ var DAY = 24 * HOUR;
44562
+ var WEEK = 7 * DAY;
44563
+ if (ms >= WEEK) return "".concat((ms / WEEK).toFixed(1), " week(s)");
44564
+ if (ms >= DAY) return "".concat((ms / DAY).toFixed(1), " day(s)");
44565
+ if (ms >= HOUR) return "".concat((ms / HOUR).toFixed(1), " hour(s)");
44566
+ return "".concat((ms / 1000 / 60).toFixed(1), " minute(s)");
44567
+ }
44568
+
44569
+ var CACHE_KEY = 's3-component-metadata';
44570
+ var CACHE_VERSION = 1;
44571
+
44572
+ /**
44573
+ * Simple mutex implementation for preventing concurrent read-modify-write operations
44574
+ */
44575
+ var SimpleMutex = /*#__PURE__*/function () {
44576
+ function SimpleMutex() {
44577
+ _classCallCheck(this, SimpleMutex);
44578
+ this._locked = false;
44579
+ this._queue = [];
44580
+ }
44581
+ return _createClass(SimpleMutex, [{
44582
+ key: "acquire",
44583
+ value: function () {
44584
+ var _acquire = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
44585
+ var _this = this;
44586
+ return _regenerator().w(function (_context) {
44587
+ while (1) switch (_context.n) {
44588
+ case 0:
44589
+ return _context.a(2, new Promise(function (resolve) {
44590
+ if (!_this._locked) {
44591
+ _this._locked = true;
44592
+ resolve();
44593
+ } else {
44594
+ _this._queue.push(resolve);
44595
+ }
44596
+ }));
44597
+ }
44598
+ }, _callee);
44599
+ }));
44600
+ function acquire() {
44601
+ return _acquire.apply(this, arguments);
44602
+ }
44603
+ return acquire;
44604
+ }()
44605
+ }, {
44606
+ key: "release",
44607
+ value: function release() {
44608
+ if (this._queue.length > 0) {
44609
+ var next = this._queue.shift();
44610
+ next();
44611
+ } else {
44612
+ this._locked = false;
44613
+ }
44614
+ }
44615
+ }]);
44616
+ }();
44617
+ var S3MetadataCacheService = /*#__PURE__*/function () {
44618
+ function S3MetadataCacheService() {
44619
+ _classCallCheck(this, S3MetadataCacheService);
44620
+ this._mutex = new SimpleMutex();
44621
+ this._memoryCache = null;
44622
+ this._memoryCacheTime = 0;
44623
+ this._memoryCacheTTL = 5000; // 5 seconds in-memory cache
44624
+ }
44625
+
44626
+ /**
44627
+ * Create empty metadata structure
44628
+ */
44629
+ return _createClass(S3MetadataCacheService, [{
44630
+ key: "_createEmptyMetadata",
44631
+ value: function _createEmptyMetadata() {
44632
+ return {
44633
+ version: CACHE_VERSION,
44634
+ components: [],
44635
+ timestamp: Date.now()
44636
+ };
44637
+ }
44638
+
44639
+ /**
44640
+ * Validate and migrate metadata if needed
44641
+ */
44642
+ }, {
44643
+ key: "_validateMetadata",
44644
+ value: function _validateMetadata(metadata) {
44645
+ if (!metadata) {
44646
+ return this._createEmptyMetadata();
44647
+ }
44648
+
44649
+ // Ensure components array exists
44650
+ if (!Array.isArray(metadata.components)) {
44651
+ metadata.components = [];
44652
+ }
44653
+
44654
+ // Add version if missing (for legacy cache)
44655
+ if (metadata.version === undefined) {
44656
+ metadata.version = CACHE_VERSION;
44657
+ }
44658
+ return metadata;
44659
+ }
44660
+
44661
+ /**
44662
+ * Check if memory cache is still valid
44663
+ */
44664
+ }, {
44665
+ key: "_isMemoryCacheValid",
44666
+ value: function _isMemoryCacheValid() {
44667
+ return this._memoryCache !== null && Date.now() - this._memoryCacheTime < this._memoryCacheTTL;
44668
+ }
44669
+
44670
+ /**
44671
+ * Invalidate the memory cache
44672
+ */
44673
+ }, {
44674
+ key: "_invalidateMemoryCache",
44675
+ value: function _invalidateMemoryCache() {
44676
+ this._memoryCache = null;
44677
+ this._memoryCacheTime = 0;
44678
+ }
44679
+
44680
+ /**
44681
+ * Get all cached components
44682
+ * @returns {Promise<Array>} Array of cached component metadata
44683
+ */
44684
+ }, {
44685
+ key: "getAll",
44686
+ value: (function () {
44687
+ var _getAll = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
44688
+ var metadata, validated, _t;
44689
+ return _regenerator().w(function (_context2) {
44690
+ while (1) switch (_context2.n) {
44691
+ case 0:
44692
+ if (!this._isMemoryCacheValid()) {
44693
+ _context2.n = 1;
44694
+ break;
44695
+ }
44696
+ return _context2.a(2, _toConsumableArray(this._memoryCache.components));
44697
+ case 1:
44698
+ _context2.p = 1;
44699
+ _context2.n = 2;
44700
+ return getCachedJsonData(CACHE_KEY);
44701
+ case 2:
44702
+ metadata = _context2.v;
44703
+ validated = this._validateMetadata(metadata); // Update memory cache
44704
+ this._memoryCache = validated;
44705
+ this._memoryCacheTime = Date.now();
44706
+ return _context2.a(2, _toConsumableArray(validated.components));
44707
+ case 3:
44708
+ _context2.p = 3;
44709
+ _t = _context2.v;
44710
+ console.warn('⚠️ S3MetadataCacheService: Failed to get components:', _t);
44711
+ return _context2.a(2, []);
44712
+ }
44713
+ }, _callee2, this, [[1, 3]]);
44714
+ }));
44715
+ function getAll() {
44716
+ return _getAll.apply(this, arguments);
44717
+ }
44718
+ return getAll;
44719
+ }()
44720
+ /**
44721
+ * Get the full metadata object (for advanced use cases)
44722
+ * @returns {Promise<Object>} Full metadata object with version, components, timestamp
44723
+ */
44724
+ )
44725
+ }, {
44726
+ key: "getMetadata",
44727
+ value: (function () {
44728
+ var _getMetadata = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() {
44729
+ var metadata, validated, _t2;
44730
+ return _regenerator().w(function (_context3) {
44731
+ while (1) switch (_context3.n) {
44732
+ case 0:
44733
+ if (!this._isMemoryCacheValid()) {
44734
+ _context3.n = 1;
44735
+ break;
44736
+ }
44737
+ return _context3.a(2, _objectSpread2({}, this._memoryCache));
44738
+ case 1:
44739
+ _context3.p = 1;
44740
+ _context3.n = 2;
44741
+ return getCachedJsonData(CACHE_KEY);
44742
+ case 2:
44743
+ metadata = _context3.v;
44744
+ validated = this._validateMetadata(metadata);
44745
+ this._memoryCache = validated;
44746
+ this._memoryCacheTime = Date.now();
44747
+ return _context3.a(2, _objectSpread2({}, validated));
44748
+ case 3:
44749
+ _context3.p = 3;
44750
+ _t2 = _context3.v;
44751
+ console.warn('⚠️ S3MetadataCacheService: Failed to get metadata:', _t2);
44752
+ return _context3.a(2, this._createEmptyMetadata());
44753
+ }
44754
+ }, _callee3, this, [[1, 3]]);
44755
+ }));
44756
+ function getMetadata() {
44757
+ return _getMetadata.apply(this, arguments);
44758
+ }
44759
+ return getMetadata;
44760
+ }()
44761
+ /**
44762
+ * Add or update a component in the cache
44763
+ * Uses mutex to prevent race conditions
44764
+ * @param {Object} component - Component metadata to add/update
44765
+ * @returns {Promise<boolean>} Success status
44766
+ */
44767
+ )
44768
+ }, {
44769
+ key: "addComponent",
44770
+ value: (function () {
44771
+ var _addComponent = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(component) {
44772
+ var metadata, validated, existingIndex, _t3;
44773
+ return _regenerator().w(function (_context4) {
44774
+ while (1) switch (_context4.n) {
44775
+ case 0:
44776
+ if (!(!component || !component.id && !component.uuid)) {
44777
+ _context4.n = 1;
44778
+ break;
44779
+ }
44780
+ console.warn('⚠️ S3MetadataCacheService: Cannot add component without id or uuid');
44781
+ return _context4.a(2, false);
44782
+ case 1:
44783
+ _context4.n = 2;
44784
+ return this._mutex.acquire();
44785
+ case 2:
44786
+ _context4.p = 2;
44787
+ _context4.n = 3;
44788
+ return getCachedJsonData(CACHE_KEY);
44789
+ case 3:
44790
+ metadata = _context4.v;
44791
+ validated = this._validateMetadata(metadata); // Remove existing entry if it exists (by id or uuid)
44792
+ existingIndex = validated.components.findIndex(function (c) {
44793
+ return c.id === component.id || c.uuid === component.uuid || component.id && c.uuid === component.id || component.uuid && c.id === component.uuid;
44794
+ });
44795
+ if (existingIndex !== -1) {
44796
+ validated.components.splice(existingIndex, 1);
44797
+ }
44798
+
44799
+ // Add new component
44800
+ validated.components.push(component);
44801
+ validated.timestamp = Date.now();
44802
+
44803
+ // Write back to cache
44804
+ _context4.n = 4;
44805
+ return cacheJsonData(CACHE_KEY, validated);
44806
+ case 4:
44807
+ // Update memory cache
44808
+ this._memoryCache = validated;
44809
+ this._memoryCacheTime = Date.now();
44810
+ console.log("\uD83D\uDCBE S3MetadataCacheService: Added/updated component ".concat(component.name || component.id));
44811
+ return _context4.a(2, true);
44812
+ case 5:
44813
+ _context4.p = 5;
44814
+ _t3 = _context4.v;
44815
+ console.error('❌ S3MetadataCacheService: Failed to add component:', _t3);
44816
+ return _context4.a(2, false);
44817
+ case 6:
44818
+ _context4.p = 6;
44819
+ this._mutex.release();
44820
+ return _context4.f(6);
44821
+ case 7:
44822
+ return _context4.a(2);
44823
+ }
44824
+ }, _callee4, this, [[2, 5, 6, 7]]);
44825
+ }));
44826
+ function addComponent(_x) {
44827
+ return _addComponent.apply(this, arguments);
44828
+ }
44829
+ return addComponent;
44830
+ }()
44831
+ /**
44832
+ * Remove a component from the cache
44833
+ * Uses mutex to prevent race conditions
44834
+ * @param {string} identifier - Component id or uuid to remove
44835
+ * @returns {Promise<boolean>} Success status
44836
+ */
44837
+ )
44838
+ }, {
44839
+ key: "removeComponent",
44840
+ value: (function () {
44841
+ var _removeComponent = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(identifier) {
44842
+ var metadata, validated, originalLength, removed, _t4;
44843
+ return _regenerator().w(function (_context5) {
44844
+ while (1) switch (_context5.n) {
44845
+ case 0:
44846
+ if (identifier) {
44847
+ _context5.n = 1;
44848
+ break;
44849
+ }
44850
+ console.warn('⚠️ S3MetadataCacheService: Cannot remove component without identifier');
44851
+ return _context5.a(2, false);
44852
+ case 1:
44853
+ _context5.n = 2;
44854
+ return this._mutex.acquire();
44855
+ case 2:
44856
+ _context5.p = 2;
44857
+ _context5.n = 3;
44858
+ return getCachedJsonData(CACHE_KEY);
44859
+ case 3:
44860
+ metadata = _context5.v;
44861
+ if (!(!metadata || !metadata.components)) {
44862
+ _context5.n = 4;
44863
+ break;
44864
+ }
44865
+ console.log('ℹ️ S3MetadataCacheService: No metadata to remove from');
44866
+ return _context5.a(2, true);
44867
+ case 4:
44868
+ validated = this._validateMetadata(metadata);
44869
+ originalLength = validated.components.length; // Filter out the component by both id and uuid
44870
+ validated.components = validated.components.filter(function (c) {
44871
+ return c.id !== identifier && c.uuid !== identifier;
44872
+ });
44873
+ removed = originalLength !== validated.components.length;
44874
+ if (!removed) {
44875
+ _context5.n = 6;
44876
+ break;
44877
+ }
44878
+ validated.timestamp = Date.now();
44879
+ _context5.n = 5;
44880
+ return cacheJsonData(CACHE_KEY, validated);
44881
+ case 5:
44882
+ // Update memory cache
44883
+ this._memoryCache = validated;
44884
+ this._memoryCacheTime = Date.now();
44885
+ console.log("\uD83D\uDDD1\uFE0F S3MetadataCacheService: Removed component ".concat(identifier));
44886
+ case 6:
44887
+ return _context5.a(2, true);
44888
+ case 7:
44889
+ _context5.p = 7;
44890
+ _t4 = _context5.v;
44891
+ console.error('❌ S3MetadataCacheService: Failed to remove component:', _t4);
44892
+ return _context5.a(2, false);
44893
+ case 8:
44894
+ _context5.p = 8;
44895
+ this._mutex.release();
44896
+ return _context5.f(8);
44897
+ case 9:
44898
+ return _context5.a(2);
44899
+ }
44900
+ }, _callee5, this, [[2, 7, 8, 9]]);
44901
+ }));
44902
+ function removeComponent(_x2) {
44903
+ return _removeComponent.apply(this, arguments);
44904
+ }
44905
+ return removeComponent;
44906
+ }()
44907
+ /**
44908
+ * Clear all cached metadata
44909
+ * @returns {Promise<boolean>} Success status
44910
+ */
44911
+ )
44912
+ }, {
44913
+ key: "clear",
44914
+ value: (function () {
44915
+ var _clear = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6() {
44916
+ var _t5;
44917
+ return _regenerator().w(function (_context6) {
44918
+ while (1) switch (_context6.n) {
44919
+ case 0:
44920
+ _context6.n = 1;
44921
+ return this._mutex.acquire();
44922
+ case 1:
44923
+ _context6.p = 1;
44924
+ _context6.n = 2;
44925
+ return removeCachedJsonData(CACHE_KEY);
44926
+ case 2:
44927
+ this._invalidateMemoryCache();
44928
+ console.log('🗑️ S3MetadataCacheService: Cleared all metadata');
44929
+ return _context6.a(2, true);
44930
+ case 3:
44931
+ _context6.p = 3;
44932
+ _t5 = _context6.v;
44933
+ console.error('❌ S3MetadataCacheService: Failed to clear metadata:', _t5);
44934
+ return _context6.a(2, false);
44935
+ case 4:
44936
+ _context6.p = 4;
44937
+ this._mutex.release();
44938
+ return _context6.f(4);
44939
+ case 5:
44940
+ return _context6.a(2);
44941
+ }
44942
+ }, _callee6, this, [[1, 3, 4, 5]]);
44943
+ }));
44944
+ function clear() {
44945
+ return _clear.apply(this, arguments);
44946
+ }
44947
+ return clear;
44948
+ }()
44949
+ /**
44950
+ * Check if a component exists in the cache
44951
+ * @param {string} identifier - Component id or uuid
44952
+ * @returns {Promise<boolean>} True if exists
44953
+ */
44954
+ )
44955
+ }, {
44956
+ key: "hasComponent",
44957
+ value: (function () {
44958
+ var _hasComponent = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(identifier) {
44959
+ var components;
44960
+ return _regenerator().w(function (_context7) {
44961
+ while (1) switch (_context7.n) {
44962
+ case 0:
44963
+ _context7.n = 1;
44964
+ return this.getAll();
44965
+ case 1:
44966
+ components = _context7.v;
44967
+ return _context7.a(2, components.some(function (c) {
44968
+ return c.id === identifier || c.uuid === identifier;
44969
+ }));
44970
+ }
44971
+ }, _callee7, this);
44972
+ }));
44973
+ function hasComponent(_x3) {
44974
+ return _hasComponent.apply(this, arguments);
44975
+ }
44976
+ return hasComponent;
44977
+ }()
44978
+ /**
44979
+ * Force refresh from persistent cache (bypass memory cache)
44980
+ * @returns {Promise<Array>} Array of cached components
44981
+ */
44982
+ )
44983
+ }, {
44984
+ key: "refresh",
44985
+ value: (function () {
44986
+ var _refresh = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8() {
44987
+ return _regenerator().w(function (_context8) {
44988
+ while (1) switch (_context8.n) {
44989
+ case 0:
44990
+ this._invalidateMemoryCache();
44991
+ return _context8.a(2, this.getAll());
44992
+ }
44993
+ }, _callee8, this);
44994
+ }));
44995
+ function refresh() {
44996
+ return _refresh.apply(this, arguments);
44997
+ }
44998
+ return refresh;
44999
+ }()
45000
+ /**
45001
+ * Replace all cached components with a new array
45002
+ * Used for bulk operations like initial fetch
45003
+ * @param {Array} components - Array of components to cache
45004
+ * @returns {Promise<boolean>} Success status
45005
+ */
45006
+ )
45007
+ }, {
45008
+ key: "setAll",
45009
+ value: (function () {
45010
+ var _setAll = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9(components) {
45011
+ var metadata, _t6;
45012
+ return _regenerator().w(function (_context9) {
45013
+ while (1) switch (_context9.n) {
45014
+ case 0:
45015
+ if (Array.isArray(components)) {
45016
+ _context9.n = 1;
45017
+ break;
45018
+ }
45019
+ console.warn('⚠️ S3MetadataCacheService: setAll requires an array');
45020
+ return _context9.a(2, false);
45021
+ case 1:
45022
+ _context9.n = 2;
45023
+ return this._mutex.acquire();
45024
+ case 2:
45025
+ _context9.p = 2;
45026
+ metadata = {
45027
+ version: CACHE_VERSION,
45028
+ components: components,
45029
+ timestamp: Date.now()
45030
+ };
45031
+ _context9.n = 3;
45032
+ return cacheJsonData(CACHE_KEY, metadata);
45033
+ case 3:
45034
+ // Update memory cache
45035
+ this._memoryCache = metadata;
45036
+ this._memoryCacheTime = Date.now();
45037
+ console.log("\uD83D\uDCBE S3MetadataCacheService: Set ".concat(components.length, " components"));
45038
+ return _context9.a(2, true);
45039
+ case 4:
45040
+ _context9.p = 4;
45041
+ _t6 = _context9.v;
45042
+ console.error('❌ S3MetadataCacheService: Failed to set components:', _t6);
45043
+ return _context9.a(2, false);
45044
+ case 5:
45045
+ _context9.p = 5;
45046
+ this._mutex.release();
45047
+ return _context9.f(5);
45048
+ case 6:
45049
+ return _context9.a(2);
45050
+ }
45051
+ }, _callee9, this, [[2, 4, 5, 6]]);
45052
+ }));
45053
+ function setAll(_x4) {
45054
+ return _setAll.apply(this, arguments);
45055
+ }
45056
+ return setAll;
45057
+ }())
45058
+ }]);
45059
+ }(); // Export singleton instance
45060
+ var s3MetadataCache = new S3MetadataCacheService();
45061
+
45062
+ /**
45063
+ * Retrieve the injected Auth instance from cacheManager config.
45064
+ * Falls back gracefully if not configured (skip credential refresh).
45065
+ */
45066
+ function getAuth() {
45067
+ return cacheManager.config.auth || null;
45068
+ }
45069
+
45070
+ /**
45071
+ * Measure S3 operation time and log results
45072
+ * @param {string} operation - Operation name (e.g., "Upload GLB", "Download JSON")
45073
+ * @param {Function} asyncFn - Async function to measure
45074
+ * @param {number} fileSize - Optional file size in bytes for speed calculation
45075
+ * @returns {Promise} - Result from asyncFn
45076
+ */
45077
+ function measureS3Transfer(_x, _x2) {
45078
+ return _measureS3Transfer.apply(this, arguments);
45079
+ }
45080
+ function _measureS3Transfer() {
45081
+ _measureS3Transfer = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(operation, asyncFn) {
45082
+ var fileSize,
45083
+ startTime,
45084
+ executeTransfer,
45085
+ Auth,
45086
+ endTime,
45087
+ duration,
45088
+ _args2 = arguments,
45089
+ _t,
45090
+ _t2;
45091
+ return _regenerator().w(function (_context2) {
45092
+ while (1) switch (_context2.n) {
45093
+ case 0:
45094
+ fileSize = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : null;
45095
+ startTime = performance.now();
45096
+ executeTransfer = /*#__PURE__*/function () {
45097
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
45098
+ var result, endTime, duration, logMessage, sizeMB, speedMBps;
45099
+ return _regenerator().w(function (_context) {
45100
+ while (1) switch (_context.n) {
45101
+ case 0:
45102
+ _context.n = 1;
45103
+ return asyncFn();
45104
+ case 1:
45105
+ result = _context.v;
45106
+ endTime = performance.now();
45107
+ duration = ((endTime - startTime) / 1000).toFixed(3); // Convert to seconds
45108
+ logMessage = "\u23F1\uFE0F S3 ".concat(operation, ": ").concat(duration, "s");
45109
+ if (fileSize) {
45110
+ sizeMB = (fileSize / (1024 * 1024)).toFixed(2);
45111
+ speedMBps = (sizeMB / duration).toFixed(2);
45112
+ logMessage += " (".concat(sizeMB, " MB @ ").concat(speedMBps, " MB/s)");
45113
+ }
45114
+ console.log(logMessage);
45115
+ return _context.a(2, result);
45116
+ }
45117
+ }, _callee);
45118
+ }));
45119
+ return function executeTransfer() {
45120
+ return _ref.apply(this, arguments);
45121
+ };
45122
+ }();
45123
+ _context2.p = 1;
45124
+ _context2.n = 2;
45125
+ return executeTransfer();
45126
+ case 2:
45127
+ return _context2.a(2, _context2.v);
45128
+ case 3:
45129
+ _context2.p = 3;
45130
+ _t = _context2.v;
45131
+ if (!(_t.code === 'ExpiredToken' || _t.message && _t.message.includes('ExpiredToken'))) {
45132
+ _context2.n = 8;
45133
+ break;
45134
+ }
45135
+ console.warn("\u26A0\uFE0F Token expired during ".concat(operation, ". Attempting session refresh and retry..."));
45136
+ Auth = getAuth();
45137
+ if (Auth) {
45138
+ _context2.n = 4;
45139
+ break;
45140
+ }
45141
+ console.error("\u274C Cannot refresh credentials: Auth not injected via cacheManager.configure({ auth })");
45142
+ throw _t;
45143
+ case 4:
45144
+ _context2.p = 4;
45145
+ _context2.n = 5;
45146
+ return Auth.currentCredentials({
45147
+ bypassCache: true
45148
+ });
45149
+ case 5:
45150
+ console.log("\u2705 Session refreshed. Retrying ".concat(operation, "..."));
45151
+
45152
+ // Reset timer for the retry
45153
+ startTime = performance.now();
45154
+ _context2.n = 6;
45155
+ return executeTransfer();
45156
+ case 6:
45157
+ return _context2.a(2, _context2.v);
45158
+ case 7:
45159
+ _context2.p = 7;
45160
+ _t2 = _context2.v;
45161
+ console.error("\u274C Retry failed for ".concat(operation, ":"), _t2);
45162
+ // If retry fails, throw the Retry Error (likely unrelated or persistent auth issue)
45163
+ throw _t2;
45164
+ case 8:
45165
+ // Standard error handling for non-expired tokens (or if logic above didn't catch it)
45166
+ endTime = performance.now();
45167
+ duration = ((endTime - startTime) / 1000).toFixed(3);
45168
+ console.error("\u274C S3 ".concat(operation, " failed after ").concat(duration, "s:"), _t);
45169
+ throw _t;
45170
+ case 9:
45171
+ return _context2.a(2);
45172
+ }
45173
+ }, _callee2, null, [[4, 7], [1, 3]]);
45174
+ }));
45175
+ return _measureS3Transfer.apply(this, arguments);
45176
+ }
45177
+
43126
45178
  var Rendering2D = /*#__PURE__*/function (_BaseDisposable) {
43127
45179
  function Rendering2D(_canvas) {
43128
45180
  var _this;
@@ -44474,6 +46526,7 @@ exports.ComponentManager = ComponentManager;
44474
46526
  exports.ComponentTooltipManager = ComponentTooltipManager;
44475
46527
  exports.EnvironmentManager = EnvironmentManager;
44476
46528
  exports.FLOW_ATTRIBUTE_KEYS = FLOW_ATTRIBUTE_KEYS;
46529
+ exports.GLOBAL_CACHE_NAME = GLOBAL_CACHE_NAME;
44477
46530
  exports.KeyboardControlsManager = KeyboardControlsManager;
44478
46531
  exports.ModelManager = ModelManager;
44479
46532
  exports.OperationHistoryManager = OperationHistoryManager;
@@ -44483,6 +46536,7 @@ exports.PathfindingManager = PathfindingManager;
44483
46536
  exports.PerformanceMonitorManager = PerformanceMonitorManager;
44484
46537
  exports.Rendering2D = rendering2D;
44485
46538
  exports.Rendering3D = rendering3D;
46539
+ exports.S3MetadataCacheService = S3MetadataCacheService;
44486
46540
  exports.SceneExportManager = SceneExportManager;
44487
46541
  exports.SceneHierarchyManager = SceneHierarchyManager;
44488
46542
  exports.SceneInitializationManager = SceneInitializationManager;
@@ -44490,15 +46544,36 @@ exports.SceneOperationsManager = SceneOperationsManager;
44490
46544
  exports.SceneTooltipsManager = SceneTooltipsManager;
44491
46545
  exports.SnapshotManager = SnapshotManager;
44492
46546
  exports.ThreeJSResourceManager = ThreeJSResourceManager;
46547
+ exports.cacheJsonData = cacheJsonData;
44493
46548
  exports.cacheManager = cacheManager;
46549
+ exports.cleanExpiredCache = cleanExpiredCache;
46550
+ exports.cleanInvalidCacheEntries = cleanInvalidCacheEntries;
46551
+ exports.clearS3Cache = clearS3Cache;
44494
46552
  exports.createPathfindingRequest = createPathfindingRequest;
44495
46553
  exports.createTransformControls = createTransformControls;
44496
46554
  exports.findObjectByHardcodedUuid = findObjectByHardcodedUuid;
46555
+ exports.formatCacheExpiry = formatCacheExpiry;
44497
46556
  exports.generateUniqueComponentId = generateUniqueComponentId;
44498
46557
  exports.generateUuidFromName = generateUuidFromName;
46558
+ exports.getAllCacheKeys = getAllCacheKeys;
46559
+ exports.getCacheExpiryForPath = getCacheExpiryForPath;
46560
+ exports.getCacheStats = getCacheStats;
46561
+ exports.getCachedJsonData = getCachedJsonData;
46562
+ exports.getCachedLocalFile = getCachedLocalFile;
46563
+ exports.getCachedLocalFileURL = getCachedLocalFileURL;
46564
+ exports.getCachedLocalJson = getCachedLocalJson;
46565
+ exports.getCachedS3Json = getCachedS3Json;
46566
+ exports.getCachedS3Object = getCachedS3Object;
46567
+ exports.getCachedS3ObjectURL = getCachedS3ObjectURL;
46568
+ exports.getCurrentCacheName = getCurrentCacheName;
46569
+ exports.getGlobalCacheStats = getGlobalCacheStats;
46570
+ exports.getGlobalOnlyCacheStats = getGlobalOnlyCacheStats;
44499
46571
  exports.getHardcodedUuid = getHardcodedUuid;
44500
46572
  exports.getObjectTypeName = getObjectTypeName;
44501
46573
  exports.getObjectsByType = getObjectsByType;
46574
+ exports.getThumbnailKey = getThumbnailKey;
46575
+ exports.getUserOnlyCacheStats = getUserOnlyCacheStats;
46576
+ exports.isCached = isCached;
44502
46577
  exports.isComponent = isComponent;
44503
46578
  exports.isComputed = isComputed;
44504
46579
  exports.isConnector = isConnector;
@@ -44506,9 +46581,18 @@ exports.isDeclared = isDeclared;
44506
46581
  exports.isExportable = isExportable;
44507
46582
  exports.isGateway = isGateway;
44508
46583
  exports.isSegment = isSegment;
46584
+ exports.isThumbnailCached = isThumbnailCached;
44509
46585
  exports.loadTextureSetAndCreateMaterial = loadTextureSetAndCreateMaterial;
44510
46586
  exports.markAsComputed = markAsComputed;
44511
46587
  exports.markAsDeclared = markAsDeclared;
46588
+ exports.measureS3Transfer = measureS3Transfer;
44512
46589
  exports.modelPreloader = modelPreloader;
46590
+ exports.preloadLocalFiles = preloadLocalFiles;
46591
+ exports.preloadS3Objects = preloadS3Objects;
46592
+ exports.removeCachedJsonData = removeCachedJsonData;
46593
+ exports.resetCacheIdentity = resetCacheIdentity;
46594
+ exports.resetGlobalCacheStats = resetGlobalCacheStats;
46595
+ exports.s3MetadataCache = s3MetadataCache;
44513
46596
  exports.sceneViewer = sceneViewer;
44514
46597
  exports.shouldRemoveOnRegeneration = shouldRemoveOnRegeneration;
46598
+ exports.switchCachePartition = switchCachePartition;