@2112-lab/central-plant 0.2.10 → 0.3.0

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,1070 @@
1
+ import { createClass as _createClass, objectSpread2 as _objectSpread2, classCallCheck as _classCallCheck, asyncToGenerator as _asyncToGenerator, regenerator as _regenerator, createForOfIteratorHelper as _createForOfIteratorHelper } from '../../../_virtual/_rollupPluginBabelHelpers.js';
2
+
3
+ /**
4
+ * Generic Cache Management using Browser Cache API
5
+ * Framework-agnostic implementation of caching logic
6
+ */
7
+
8
+ // Time constants
9
+ var SECOND = 1000;
10
+ var MINUTE = 60 * SECOND;
11
+ var HOUR = 60 * MINUTE;
12
+ var DAY = 24 * HOUR;
13
+ var CACHE_EXPIRY = {
14
+ MODELS: 7 * DAY,
15
+ TEXTURES: 7 * DAY,
16
+ JSON: 24 * HOUR,
17
+ COMPONENT_DICTIONARY: 24 * HOUR,
18
+ SCENE_DATA: 12 * HOUR,
19
+ THUMBNAILS: Infinity,
20
+ SKYBOXES: 7 * DAY,
21
+ USER_CONTENT: 6 * HOUR,
22
+ TEMPORARY: 1 * HOUR,
23
+ DEFAULT: 24 * HOUR
24
+ };
25
+ var CACHE_NAME_PREFIX = 'central-plant-s3-cache-v1';
26
+ var CacheManager = /*#__PURE__*/function () {
27
+ function CacheManager() {
28
+ _classCallCheck(this, CacheManager);
29
+ this.stats = {
30
+ hits: 0,
31
+ misses: 0,
32
+ errors: 0,
33
+ totalBytesServed: 0,
34
+ totalBytesCached: 0,
35
+ startTime: Date.now()
36
+ };
37
+ this.config = {
38
+ // Default user provider returns null (anonymous)
39
+ getUser: function () {
40
+ var _getUser = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
41
+ return _regenerator().w(function (_context) {
42
+ while (1) switch (_context.n) {
43
+ case 0:
44
+ return _context.a(2, null);
45
+ }
46
+ }, _callee);
47
+ }));
48
+ function getUser() {
49
+ return _getUser.apply(this, arguments);
50
+ }
51
+ return getUser;
52
+ }(),
53
+ // Default error logger
54
+ onError: function onError(error) {
55
+ return console.error('CacheManager Error:', error);
56
+ }
57
+ };
58
+ }
59
+
60
+ /**
61
+ * Configure the cache manager
62
+ */
63
+ return _createClass(CacheManager, [{
64
+ key: "configure",
65
+ value: function configure() {
66
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
67
+ this.config = _objectSpread2(_objectSpread2({}, this.config), options);
68
+ }
69
+
70
+ /**
71
+ * Get cache expiry for a specific file path
72
+ */
73
+ }, {
74
+ key: "getExpiryForPath",
75
+ value: function getExpiryForPath(path) {
76
+ var assetType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
77
+ if (assetType && CACHE_EXPIRY[assetType.toUpperCase()]) {
78
+ return CACHE_EXPIRY[assetType.toUpperCase()];
79
+ }
80
+ var lowerPath = path ? path.toLowerCase() : '';
81
+ if (lowerPath.includes('/models/') || lowerPath.endsWith('.glb') || lowerPath.endsWith('.gltf')) {
82
+ return CACHE_EXPIRY.MODELS;
83
+ }
84
+ if (lowerPath.includes('/textures/') || lowerPath.endsWith('.jpg') || lowerPath.endsWith('.png') || lowerPath.endsWith('.webp')) {
85
+ return CACHE_EXPIRY.TEXTURES;
86
+ }
87
+ if (lowerPath.includes('/thumbnails/')) {
88
+ return CACHE_EXPIRY.THUMBNAILS;
89
+ }
90
+ if (lowerPath.includes('/skyboxes/') || lowerPath.endsWith('.hdr')) {
91
+ return CACHE_EXPIRY.SKYBOXES;
92
+ }
93
+ if (lowerPath.includes('component-dictionary')) {
94
+ return CACHE_EXPIRY.COMPONENT_DICTIONARY;
95
+ }
96
+ if (lowerPath.endsWith('.json')) {
97
+ return CACHE_EXPIRY.JSON;
98
+ }
99
+ return CACHE_EXPIRY.DEFAULT;
100
+ }
101
+
102
+ /**
103
+ * Get or open the cache handle efficiently
104
+ */
105
+ }, {
106
+ key: "_getCacheHandle",
107
+ value: (function () {
108
+ var _getCacheHandle2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(cacheName) {
109
+ return _regenerator().w(function (_context2) {
110
+ while (1) switch (_context2.n) {
111
+ case 0:
112
+ if (!(this._activeCacheName === cacheName && this._activeCacheHandle)) {
113
+ _context2.n = 1;
114
+ break;
115
+ }
116
+ return _context2.a(2, this._activeCacheHandle);
117
+ case 1:
118
+ // If we are switching caches, close/drop reference to the old one (GC handles it, but good practice)
119
+ this._activeCacheName = cacheName;
120
+ _context2.n = 2;
121
+ return caches.open(cacheName);
122
+ case 2:
123
+ this._activeCacheHandle = _context2.v;
124
+ return _context2.a(2, this._activeCacheHandle);
125
+ }
126
+ }, _callee2, this);
127
+ }));
128
+ function _getCacheHandle(_x) {
129
+ return _getCacheHandle2.apply(this, arguments);
130
+ }
131
+ return _getCacheHandle;
132
+ }()
133
+ /**
134
+ * Resets the identity, forcing a re-fetch of the user/cache name on next access
135
+ */
136
+ )
137
+ }, {
138
+ key: "resetIdentity",
139
+ value: function resetIdentity() {
140
+ this._cachedCacheName = null;
141
+ }
142
+
143
+ /**
144
+ * Get the cache name for the current user
145
+ * Memoized indefinitely until resetIdentity() is called
146
+ */
147
+ }, {
148
+ key: "getCacheName",
149
+ value: (function () {
150
+ var _getCacheName = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() {
151
+ var user, cacheName, _user$attributes, userId, sanitizedUserId, _t;
152
+ return _regenerator().w(function (_context3) {
153
+ while (1) switch (_context3.n) {
154
+ case 0:
155
+ if (!this._cachedCacheName) {
156
+ _context3.n = 1;
157
+ break;
158
+ }
159
+ return _context3.a(2, this._cachedCacheName);
160
+ case 1:
161
+ _context3.p = 1;
162
+ _context3.n = 2;
163
+ return this.config.getUser();
164
+ case 2:
165
+ user = _context3.v;
166
+ if (!user) {
167
+ cacheName = "".concat(CACHE_NAME_PREFIX, "-anonymous");
168
+ } else {
169
+ userId = typeof user === 'string' ? user : user.username || ((_user$attributes = user.attributes) === null || _user$attributes === void 0 ? void 0 : _user$attributes.sub) || 'anonymous';
170
+ sanitizedUserId = userId.replace(/[^a-zA-Z0-9-]/g, '-');
171
+ cacheName = "".concat(CACHE_NAME_PREFIX, "-user-").concat(sanitizedUserId);
172
+ }
173
+ this._cachedCacheName = cacheName;
174
+ return _context3.a(2, cacheName);
175
+ case 3:
176
+ _context3.p = 3;
177
+ _t = _context3.v;
178
+ console.warn('Failed to get user for cache partition, using anonymous:', _t);
179
+ return _context3.a(2, "".concat(CACHE_NAME_PREFIX, "-anonymous"));
180
+ }
181
+ }, _callee3, this, [[1, 3]]);
182
+ }));
183
+ function getCacheName() {
184
+ return _getCacheName.apply(this, arguments);
185
+ }
186
+ return getCacheName;
187
+ }()
188
+ /**
189
+ * Execute a cache operation
190
+ */
191
+ )
192
+ }, {
193
+ key: "execute",
194
+ value: (function () {
195
+ var _execute = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(_ref) {
196
+ var cacheKey, sourceKey, expiryMs, fetcher, processor, _ref$metadata, metadata, cacheName, cache, cachedResponse, cachedTime, age, result, fetchResult, blobOrJson, contentType, contentSize, responseBody, headers, cacheResponse, fallbackResult, _t2;
197
+ return _regenerator().w(function (_context4) {
198
+ while (1) switch (_context4.n) {
199
+ case 0:
200
+ cacheKey = _ref.cacheKey, sourceKey = _ref.sourceKey, expiryMs = _ref.expiryMs, fetcher = _ref.fetcher, processor = _ref.processor, _ref$metadata = _ref.metadata, metadata = _ref$metadata === void 0 ? {} : _ref$metadata, _ref.logType;
201
+ _context4.p = 1;
202
+ _context4.n = 2;
203
+ return this.getCacheName();
204
+ case 2:
205
+ cacheName = _context4.v;
206
+ _context4.n = 3;
207
+ return this._getCacheHandle(cacheName);
208
+ case 3:
209
+ cache = _context4.v;
210
+ _context4.n = 4;
211
+ return cache.match(cacheKey);
212
+ case 4:
213
+ cachedResponse = _context4.v;
214
+ if (!cachedResponse) {
215
+ _context4.n = 7;
216
+ break;
217
+ }
218
+ cachedTime = cachedResponse.headers.get('x-cached-time');
219
+ age = Date.now() - parseInt(cachedTime || '0');
220
+ if (!(age < expiryMs)) {
221
+ _context4.n = 6;
222
+ break;
223
+ }
224
+ _context4.n = 5;
225
+ return processor(cachedResponse.clone());
226
+ case 5:
227
+ result = _context4.v;
228
+ this.stats.hits++;
229
+ if (result instanceof Blob) this.stats.totalBytesServed += result.size;
230
+ return _context4.a(2, result);
231
+ case 6:
232
+ _context4.n = 7;
233
+ return cache.delete(cacheKey);
234
+ case 7:
235
+ this.stats.misses++;
236
+ _context4.n = 8;
237
+ return fetcher();
238
+ case 8:
239
+ fetchResult = _context4.v;
240
+ blobOrJson = fetchResult;
241
+ if (!(fetchResult instanceof Response)) {
242
+ _context4.n = 11;
243
+ break;
244
+ }
245
+ if (fetchResult.ok) {
246
+ _context4.n = 9;
247
+ break;
248
+ }
249
+ throw new Error("Fetch failed: ".concat(fetchResult.status));
250
+ case 9:
251
+ _context4.n = 10;
252
+ return processor(fetchResult.clone());
253
+ case 10:
254
+ blobOrJson = _context4.v;
255
+ case 11:
256
+ contentType = 'application/octet-stream';
257
+ contentSize = 0;
258
+ responseBody = blobOrJson;
259
+ if (blobOrJson instanceof Blob) {
260
+ contentType = blobOrJson.type;
261
+ contentSize = blobOrJson.size;
262
+ this.stats.totalBytesCached += contentSize;
263
+ this.stats.totalBytesServed += contentSize;
264
+ } else {
265
+ contentType = 'application/json';
266
+ responseBody = JSON.stringify(blobOrJson);
267
+ contentSize = responseBody.length;
268
+ }
269
+ headers = new Headers(_objectSpread2({
270
+ 'Content-Type': contentType,
271
+ 'x-cached-time': Date.now().toString(),
272
+ 'x-source-key': sourceKey || ''
273
+ }, metadata));
274
+ cacheResponse = new Response(responseBody, {
275
+ headers: headers
276
+ });
277
+ _context4.n = 12;
278
+ return cache.put(cacheKey, cacheResponse);
279
+ case 12:
280
+ return _context4.a(2, blobOrJson);
281
+ case 13:
282
+ _context4.p = 13;
283
+ _t2 = _context4.v;
284
+ this.stats.errors++;
285
+ this.config.onError(_t2);
286
+
287
+ // Fallback
288
+ _context4.p = 14;
289
+ _context4.n = 15;
290
+ return fetcher();
291
+ case 15:
292
+ fallbackResult = _context4.v;
293
+ if (!(fallbackResult instanceof Response)) {
294
+ _context4.n = 16;
295
+ break;
296
+ }
297
+ return _context4.a(2, processor(fallbackResult));
298
+ case 16:
299
+ return _context4.a(2, fallbackResult);
300
+ case 17:
301
+ _context4.p = 17;
302
+ _context4.v;
303
+ throw _t2;
304
+ case 18:
305
+ return _context4.a(2);
306
+ }
307
+ }, _callee4, this, [[14, 17], [1, 13]]);
308
+ }));
309
+ function execute(_x2) {
310
+ return _execute.apply(this, arguments);
311
+ }
312
+ return execute;
313
+ }())
314
+ }, {
315
+ key: "getGlobalStats",
316
+ value: function getGlobalStats() {
317
+ var uptime = Date.now() - this.stats.startTime;
318
+ return _objectSpread2(_objectSpread2({}, this.stats), {}, {
319
+ hitRate: this.stats.hits + this.stats.misses > 0 ? (this.stats.hits / (this.stats.hits + this.stats.misses) * 100).toFixed(1) + '%' : '0%',
320
+ totalMBServed: (this.stats.totalBytesServed / 1024 / 1024).toFixed(2),
321
+ totalMBCached: (this.stats.totalBytesCached / 1024 / 1024).toFixed(2),
322
+ uptimeMinutes: (uptime / 1000 / 60).toFixed(1)
323
+ });
324
+ }
325
+ }, {
326
+ key: "resetStats",
327
+ value: function resetStats() {
328
+ this.stats = {
329
+ hits: 0,
330
+ misses: 0,
331
+ errors: 0,
332
+ totalBytesServed: 0,
333
+ totalBytesCached: 0,
334
+ startTime: Date.now()
335
+ };
336
+ }
337
+ }, {
338
+ key: "clearCache",
339
+ value: function () {
340
+ var _clearCache = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5() {
341
+ var specificKey,
342
+ clearAllUsers,
343
+ cacheName,
344
+ cache,
345
+ keys,
346
+ _iterator,
347
+ _step,
348
+ req,
349
+ cacheNames,
350
+ appCaches,
351
+ _iterator2,
352
+ _step2,
353
+ name,
354
+ _cacheName,
355
+ _args5 = arguments,
356
+ _t4,
357
+ _t5,
358
+ _t6;
359
+ return _regenerator().w(function (_context5) {
360
+ while (1) switch (_context5.n) {
361
+ case 0:
362
+ specificKey = _args5.length > 0 && _args5[0] !== undefined ? _args5[0] : null;
363
+ clearAllUsers = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : false;
364
+ _context5.p = 1;
365
+ if (!specificKey) {
366
+ _context5.n = 12;
367
+ break;
368
+ }
369
+ _context5.n = 2;
370
+ return this.getCacheName();
371
+ case 2:
372
+ cacheName = _context5.v;
373
+ _context5.n = 3;
374
+ return caches.open(cacheName);
375
+ case 3:
376
+ cache = _context5.v;
377
+ _context5.n = 4;
378
+ return cache.keys();
379
+ case 4:
380
+ keys = _context5.v;
381
+ _iterator = _createForOfIteratorHelper(keys);
382
+ _context5.p = 5;
383
+ _iterator.s();
384
+ case 6:
385
+ if ((_step = _iterator.n()).done) {
386
+ _context5.n = 8;
387
+ break;
388
+ }
389
+ req = _step.value;
390
+ if (!req.url.includes(specificKey)) {
391
+ _context5.n = 7;
392
+ break;
393
+ }
394
+ _context5.n = 7;
395
+ return cache.delete(req);
396
+ case 7:
397
+ _context5.n = 6;
398
+ break;
399
+ case 8:
400
+ _context5.n = 10;
401
+ break;
402
+ case 9:
403
+ _context5.p = 9;
404
+ _t4 = _context5.v;
405
+ _iterator.e(_t4);
406
+ case 10:
407
+ _context5.p = 10;
408
+ _iterator.f();
409
+ return _context5.f(10);
410
+ case 11:
411
+ _context5.n = 23;
412
+ break;
413
+ case 12:
414
+ if (!clearAllUsers) {
415
+ _context5.n = 21;
416
+ break;
417
+ }
418
+ _context5.n = 13;
419
+ return caches.keys();
420
+ case 13:
421
+ cacheNames = _context5.v;
422
+ appCaches = cacheNames.filter(function (name) {
423
+ return name.startsWith(CACHE_NAME_PREFIX);
424
+ });
425
+ _iterator2 = _createForOfIteratorHelper(appCaches);
426
+ _context5.p = 14;
427
+ _iterator2.s();
428
+ case 15:
429
+ if ((_step2 = _iterator2.n()).done) {
430
+ _context5.n = 17;
431
+ break;
432
+ }
433
+ name = _step2.value;
434
+ _context5.n = 16;
435
+ return caches.delete(name);
436
+ case 16:
437
+ _context5.n = 15;
438
+ break;
439
+ case 17:
440
+ _context5.n = 19;
441
+ break;
442
+ case 18:
443
+ _context5.p = 18;
444
+ _t5 = _context5.v;
445
+ _iterator2.e(_t5);
446
+ case 19:
447
+ _context5.p = 19;
448
+ _iterator2.f();
449
+ return _context5.f(19);
450
+ case 20:
451
+ _context5.n = 23;
452
+ break;
453
+ case 21:
454
+ _context5.n = 22;
455
+ return this.getCacheName();
456
+ case 22:
457
+ _cacheName = _context5.v;
458
+ _context5.n = 23;
459
+ return caches.delete(_cacheName);
460
+ case 23:
461
+ return _context5.a(2, true);
462
+ case 24:
463
+ _context5.p = 24;
464
+ _t6 = _context5.v;
465
+ this.config.onError(_t6);
466
+ return _context5.a(2, false);
467
+ }
468
+ }, _callee5, this, [[14, 18, 19, 20], [5, 9, 10, 11], [1, 24]]);
469
+ }));
470
+ function clearCache() {
471
+ return _clearCache.apply(this, arguments);
472
+ }
473
+ return clearCache;
474
+ }()
475
+ }, {
476
+ key: "cleanExpired",
477
+ value: function () {
478
+ var _cleanExpired = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6() {
479
+ var cacheName, cache, keys, count, _iterator3, _step3, request, response, cachedTime, age, sourceKey, expiryMs, _t7, _t8;
480
+ return _regenerator().w(function (_context6) {
481
+ while (1) switch (_context6.n) {
482
+ case 0:
483
+ _context6.p = 0;
484
+ _context6.n = 1;
485
+ return this.getCacheName();
486
+ case 1:
487
+ cacheName = _context6.v;
488
+ _context6.n = 2;
489
+ return this._getCacheHandle(cacheName);
490
+ case 2:
491
+ cache = _context6.v;
492
+ _context6.n = 3;
493
+ return cache.keys();
494
+ case 3:
495
+ keys = _context6.v;
496
+ count = 0;
497
+ _iterator3 = _createForOfIteratorHelper(keys);
498
+ _context6.p = 4;
499
+ _iterator3.s();
500
+ case 5:
501
+ if ((_step3 = _iterator3.n()).done) {
502
+ _context6.n = 9;
503
+ break;
504
+ }
505
+ request = _step3.value;
506
+ _context6.n = 6;
507
+ return cache.match(request);
508
+ case 6:
509
+ response = _context6.v;
510
+ cachedTime = response.headers.get('x-cached-time');
511
+ age = Date.now() - parseInt(cachedTime || '0');
512
+ sourceKey = response.headers.get('x-source-key') || request.url;
513
+ expiryMs = this.getExpiryForPath(sourceKey);
514
+ if (!(age > expiryMs)) {
515
+ _context6.n = 8;
516
+ break;
517
+ }
518
+ _context6.n = 7;
519
+ return cache.delete(request);
520
+ case 7:
521
+ count++;
522
+ case 8:
523
+ _context6.n = 5;
524
+ break;
525
+ case 9:
526
+ _context6.n = 11;
527
+ break;
528
+ case 10:
529
+ _context6.p = 10;
530
+ _t7 = _context6.v;
531
+ _iterator3.e(_t7);
532
+ case 11:
533
+ _context6.p = 11;
534
+ _iterator3.f();
535
+ return _context6.f(11);
536
+ case 12:
537
+ return _context6.a(2, count);
538
+ case 13:
539
+ _context6.p = 13;
540
+ _t8 = _context6.v;
541
+ this.config.onError(_t8);
542
+ return _context6.a(2, 0);
543
+ }
544
+ }, _callee6, this, [[4, 10, 11, 12], [0, 13]]);
545
+ }));
546
+ function cleanExpired() {
547
+ return _cleanExpired.apply(this, arguments);
548
+ }
549
+ return cleanExpired;
550
+ }()
551
+ }, {
552
+ key: "cleanInvalid",
553
+ value: function () {
554
+ var _cleanInvalid = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7() {
555
+ var cacheName, cache, keys, cleanedCount, _iterator4, _step4, request, url, _t9, _t0;
556
+ return _regenerator().w(function (_context7) {
557
+ while (1) switch (_context7.n) {
558
+ case 0:
559
+ _context7.p = 0;
560
+ _context7.n = 1;
561
+ return this.getCacheName();
562
+ case 1:
563
+ cacheName = _context7.v;
564
+ _context7.n = 2;
565
+ return this._getCacheHandle(cacheName);
566
+ case 2:
567
+ cache = _context7.v;
568
+ _context7.n = 3;
569
+ return cache.keys();
570
+ case 3:
571
+ keys = _context7.v;
572
+ cleanedCount = 0;
573
+ _iterator4 = _createForOfIteratorHelper(keys);
574
+ _context7.p = 4;
575
+ _iterator4.s();
576
+ case 5:
577
+ if ((_step4 = _iterator4.n()).done) {
578
+ _context7.n = 8;
579
+ break;
580
+ }
581
+ request = _step4.value;
582
+ url = request.url;
583
+ if (!(url.includes('undefined') || url.includes('null') || url.includes('/library/models//'))) {
584
+ _context7.n = 7;
585
+ break;
586
+ }
587
+ _context7.n = 6;
588
+ return cache.delete(request);
589
+ case 6:
590
+ cleanedCount++;
591
+ case 7:
592
+ _context7.n = 5;
593
+ break;
594
+ case 8:
595
+ _context7.n = 10;
596
+ break;
597
+ case 9:
598
+ _context7.p = 9;
599
+ _t9 = _context7.v;
600
+ _iterator4.e(_t9);
601
+ case 10:
602
+ _context7.p = 10;
603
+ _iterator4.f();
604
+ return _context7.f(10);
605
+ case 11:
606
+ return _context7.a(2, cleanedCount);
607
+ case 12:
608
+ _context7.p = 12;
609
+ _t0 = _context7.v;
610
+ this.config.onError(_t0);
611
+ return _context7.a(2, 0);
612
+ }
613
+ }, _callee7, this, [[4, 9, 10, 11], [0, 12]]);
614
+ }));
615
+ function cleanInvalid() {
616
+ return _cleanInvalid.apply(this, arguments);
617
+ }
618
+ return cleanInvalid;
619
+ }()
620
+ /**
621
+ * Check if a key exists in cache and is valid
622
+ */
623
+ }, {
624
+ key: "has",
625
+ value: (function () {
626
+ var _has = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(cacheKey) {
627
+ var expiryMs,
628
+ cacheName,
629
+ cache,
630
+ response,
631
+ cachedTime,
632
+ age,
633
+ expiry,
634
+ _args8 = arguments,
635
+ _t1;
636
+ return _regenerator().w(function (_context8) {
637
+ while (1) switch (_context8.n) {
638
+ case 0:
639
+ expiryMs = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : null;
640
+ _context8.p = 1;
641
+ _context8.n = 2;
642
+ return this.getCacheName();
643
+ case 2:
644
+ cacheName = _context8.v;
645
+ _context8.n = 3;
646
+ return this._getCacheHandle(cacheName);
647
+ case 3:
648
+ cache = _context8.v;
649
+ _context8.n = 4;
650
+ return cache.match(cacheKey);
651
+ case 4:
652
+ response = _context8.v;
653
+ if (response) {
654
+ _context8.n = 5;
655
+ break;
656
+ }
657
+ return _context8.a(2, false);
658
+ case 5:
659
+ cachedTime = response.headers.get('x-cached-time');
660
+ age = Date.now() - parseInt(cachedTime || '0');
661
+ expiry = expiryMs || this.getExpiryForPath(cacheKey);
662
+ if (!(age < expiry)) {
663
+ _context8.n = 6;
664
+ break;
665
+ }
666
+ return _context8.a(2, true);
667
+ case 6:
668
+ _context8.n = 7;
669
+ return cache.delete(cacheKey);
670
+ case 7:
671
+ return _context8.a(2, false);
672
+ case 8:
673
+ _context8.n = 10;
674
+ break;
675
+ case 9:
676
+ _context8.p = 9;
677
+ _t1 = _context8.v;
678
+ this.config.onError(_t1);
679
+ return _context8.a(2, false);
680
+ case 10:
681
+ return _context8.a(2);
682
+ }
683
+ }, _callee8, this, [[1, 9]]);
684
+ }));
685
+ function has(_x3) {
686
+ return _has.apply(this, arguments);
687
+ }
688
+ return has;
689
+ }()
690
+ /**
691
+ * Get a value from cache without fetching (returns null on miss/expiry)
692
+ */
693
+ )
694
+ }, {
695
+ key: "get",
696
+ value: (function () {
697
+ var _get = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0(cacheKey) {
698
+ var expiryMs,
699
+ processor,
700
+ cacheName,
701
+ cache,
702
+ response,
703
+ cachedTime,
704
+ age,
705
+ expiry,
706
+ _args0 = arguments,
707
+ _t10;
708
+ return _regenerator().w(function (_context0) {
709
+ while (1) switch (_context0.n) {
710
+ case 0:
711
+ expiryMs = _args0.length > 1 && _args0[1] !== undefined ? _args0[1] : null;
712
+ processor = _args0.length > 2 && _args0[2] !== undefined ? _args0[2] : (/*#__PURE__*/function () {
713
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9(r) {
714
+ return _regenerator().w(function (_context9) {
715
+ while (1) switch (_context9.n) {
716
+ case 0:
717
+ return _context9.a(2, r.json());
718
+ }
719
+ }, _callee9);
720
+ }));
721
+ return function (_x5) {
722
+ return _ref2.apply(this, arguments);
723
+ };
724
+ }());
725
+ _context0.p = 1;
726
+ _context0.n = 2;
727
+ return this.getCacheName();
728
+ case 2:
729
+ cacheName = _context0.v;
730
+ _context0.n = 3;
731
+ return this._getCacheHandle(cacheName);
732
+ case 3:
733
+ cache = _context0.v;
734
+ _context0.n = 4;
735
+ return cache.match(cacheKey);
736
+ case 4:
737
+ response = _context0.v;
738
+ if (response) {
739
+ _context0.n = 5;
740
+ break;
741
+ }
742
+ return _context0.a(2, null);
743
+ case 5:
744
+ cachedTime = response.headers.get('x-cached-time');
745
+ age = Date.now() - parseInt(cachedTime || '0');
746
+ expiry = expiryMs || this.getExpiryForPath(cacheKey);
747
+ if (!(age < expiry)) {
748
+ _context0.n = 7;
749
+ break;
750
+ }
751
+ this.stats.hits++;
752
+ _context0.n = 6;
753
+ return processor(response);
754
+ case 6:
755
+ return _context0.a(2, _context0.v);
756
+ case 7:
757
+ _context0.n = 8;
758
+ return cache.delete(cacheKey);
759
+ case 8:
760
+ return _context0.a(2, null);
761
+ case 9:
762
+ _context0.n = 11;
763
+ break;
764
+ case 10:
765
+ _context0.p = 10;
766
+ _t10 = _context0.v;
767
+ this.config.onError(_t10);
768
+ return _context0.a(2, null);
769
+ case 11:
770
+ return _context0.a(2);
771
+ }
772
+ }, _callee0, this, [[1, 10]]);
773
+ }));
774
+ function get(_x4) {
775
+ return _get.apply(this, arguments);
776
+ }
777
+ return get;
778
+ }()
779
+ /**
780
+ * Delete a specific key from the cache
781
+ */
782
+ )
783
+ }, {
784
+ key: "delete",
785
+ value: (function () {
786
+ var _delete2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1(cacheKey) {
787
+ var cacheName, cache, _t11;
788
+ return _regenerator().w(function (_context1) {
789
+ while (1) switch (_context1.n) {
790
+ case 0:
791
+ _context1.p = 0;
792
+ _context1.n = 1;
793
+ return this.getCacheName();
794
+ case 1:
795
+ cacheName = _context1.v;
796
+ _context1.n = 2;
797
+ return this._getCacheHandle(cacheName);
798
+ case 2:
799
+ cache = _context1.v;
800
+ _context1.n = 3;
801
+ return cache.delete(cacheKey);
802
+ case 3:
803
+ return _context1.a(2, _context1.v);
804
+ case 4:
805
+ _context1.p = 4;
806
+ _t11 = _context1.v;
807
+ this.config.onError(_t11);
808
+ return _context1.a(2, false);
809
+ }
810
+ }, _callee1, this, [[0, 4]]);
811
+ }));
812
+ function _delete(_x6) {
813
+ return _delete2.apply(this, arguments);
814
+ }
815
+ return _delete;
816
+ }()
817
+ /**
818
+ * Delete a specific named cache (partition)
819
+ */
820
+ )
821
+ }, {
822
+ key: "deleteCache",
823
+ value: (function () {
824
+ var _deleteCache = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee10(cacheName) {
825
+ var _t12;
826
+ return _regenerator().w(function (_context10) {
827
+ while (1) switch (_context10.n) {
828
+ case 0:
829
+ _context10.p = 0;
830
+ _context10.n = 1;
831
+ return caches.delete(cacheName);
832
+ case 1:
833
+ return _context10.a(2, _context10.v);
834
+ case 2:
835
+ _context10.p = 2;
836
+ _t12 = _context10.v;
837
+ this.config.onError(_t12);
838
+ return _context10.a(2, false);
839
+ }
840
+ }, _callee10, this, [[0, 2]]);
841
+ }));
842
+ function deleteCache(_x7) {
843
+ return _deleteCache.apply(this, arguments);
844
+ }
845
+ return deleteCache;
846
+ }())
847
+ }, {
848
+ key: "getDetailedStats",
849
+ value: function () {
850
+ var _getDetailedStats = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee11() {
851
+ var allUsers,
852
+ cacheNames,
853
+ plantCaches,
854
+ allStats,
855
+ _iterator5,
856
+ _step5,
857
+ _cacheName2,
858
+ _cache,
859
+ _keys,
860
+ _totalSize,
861
+ _iterator6,
862
+ _step6,
863
+ request,
864
+ response,
865
+ blob,
866
+ grandTotal,
867
+ cacheName,
868
+ cache,
869
+ keys,
870
+ totalSize,
871
+ items,
872
+ _iterator7,
873
+ _step7,
874
+ _request,
875
+ _response,
876
+ _blob,
877
+ cachedTime,
878
+ sourceKey,
879
+ _args11 = arguments,
880
+ _t13,
881
+ _t14,
882
+ _t15,
883
+ _t16;
884
+ return _regenerator().w(function (_context11) {
885
+ while (1) switch (_context11.n) {
886
+ case 0:
887
+ allUsers = _args11.length > 0 && _args11[0] !== undefined ? _args11[0] : false;
888
+ _context11.p = 1;
889
+ if (!allUsers) {
890
+ _context11.n = 21;
891
+ break;
892
+ }
893
+ _context11.n = 2;
894
+ return caches.keys();
895
+ case 2:
896
+ cacheNames = _context11.v;
897
+ plantCaches = cacheNames.filter(function (name) {
898
+ return name.startsWith(CACHE_NAME_PREFIX);
899
+ });
900
+ allStats = [];
901
+ _iterator5 = _createForOfIteratorHelper(plantCaches);
902
+ _context11.p = 3;
903
+ _iterator5.s();
904
+ case 4:
905
+ if ((_step5 = _iterator5.n()).done) {
906
+ _context11.n = 17;
907
+ break;
908
+ }
909
+ _cacheName2 = _step5.value;
910
+ _context11.n = 5;
911
+ return caches.open(_cacheName2);
912
+ case 5:
913
+ _cache = _context11.v;
914
+ _context11.n = 6;
915
+ return _cache.keys();
916
+ case 6:
917
+ _keys = _context11.v;
918
+ _totalSize = 0;
919
+ _iterator6 = _createForOfIteratorHelper(_keys);
920
+ _context11.p = 7;
921
+ _iterator6.s();
922
+ case 8:
923
+ if ((_step6 = _iterator6.n()).done) {
924
+ _context11.n = 12;
925
+ break;
926
+ }
927
+ request = _step6.value;
928
+ _context11.n = 9;
929
+ return _cache.match(request);
930
+ case 9:
931
+ response = _context11.v;
932
+ _context11.n = 10;
933
+ return response.blob();
934
+ case 10:
935
+ blob = _context11.v;
936
+ _totalSize += blob.size;
937
+ case 11:
938
+ _context11.n = 8;
939
+ break;
940
+ case 12:
941
+ _context11.n = 14;
942
+ break;
943
+ case 13:
944
+ _context11.p = 13;
945
+ _t13 = _context11.v;
946
+ _iterator6.e(_t13);
947
+ case 14:
948
+ _context11.p = 14;
949
+ _iterator6.f();
950
+ return _context11.f(14);
951
+ case 15:
952
+ allStats.push({
953
+ cacheName: _cacheName2,
954
+ count: _keys.length,
955
+ totalSize: _totalSize,
956
+ totalSizeMB: (_totalSize / 1024 / 1024).toFixed(2)
957
+ });
958
+ case 16:
959
+ _context11.n = 4;
960
+ break;
961
+ case 17:
962
+ _context11.n = 19;
963
+ break;
964
+ case 18:
965
+ _context11.p = 18;
966
+ _t14 = _context11.v;
967
+ _iterator5.e(_t14);
968
+ case 19:
969
+ _context11.p = 19;
970
+ _iterator5.f();
971
+ return _context11.f(19);
972
+ case 20:
973
+ grandTotal = allStats.reduce(function (sum, stat) {
974
+ return sum + stat.totalSize;
975
+ }, 0);
976
+ return _context11.a(2, {
977
+ caches: allStats,
978
+ totalCaches: allStats.length,
979
+ grandTotalSizeMB: (grandTotal / 1024 / 1024).toFixed(2)
980
+ });
981
+ case 21:
982
+ _context11.n = 22;
983
+ return this.getCacheName();
984
+ case 22:
985
+ cacheName = _context11.v;
986
+ _context11.n = 23;
987
+ return caches.open(cacheName);
988
+ case 23:
989
+ cache = _context11.v;
990
+ _context11.n = 24;
991
+ return cache.keys();
992
+ case 24:
993
+ keys = _context11.v;
994
+ totalSize = 0;
995
+ items = [];
996
+ _iterator7 = _createForOfIteratorHelper(keys);
997
+ _context11.p = 25;
998
+ _iterator7.s();
999
+ case 26:
1000
+ if ((_step7 = _iterator7.n()).done) {
1001
+ _context11.n = 30;
1002
+ break;
1003
+ }
1004
+ _request = _step7.value;
1005
+ _context11.n = 27;
1006
+ return cache.match(_request);
1007
+ case 27:
1008
+ _response = _context11.v;
1009
+ _context11.n = 28;
1010
+ return _response.blob();
1011
+ case 28:
1012
+ _blob = _context11.v;
1013
+ cachedTime = _response.headers.get('x-cached-time');
1014
+ sourceKey = _response.headers.get('x-source-key') || _response.headers.get('x-s3-key') || _response.headers.get('x-local-path') || _request.url;
1015
+ totalSize += _blob.size;
1016
+ items.push({
1017
+ url: _request.url,
1018
+ sourceKey: sourceKey,
1019
+ size: _blob.size,
1020
+ sizeMB: (_blob.size / 1024 / 1024).toFixed(2),
1021
+ type: _blob.type,
1022
+ cachedTime: parseInt(cachedTime || '0'),
1023
+ age: Date.now() - parseInt(cachedTime || '0')
1024
+ });
1025
+ case 29:
1026
+ _context11.n = 26;
1027
+ break;
1028
+ case 30:
1029
+ _context11.n = 32;
1030
+ break;
1031
+ case 31:
1032
+ _context11.p = 31;
1033
+ _t15 = _context11.v;
1034
+ _iterator7.e(_t15);
1035
+ case 32:
1036
+ _context11.p = 32;
1037
+ _iterator7.f();
1038
+ return _context11.f(32);
1039
+ case 33:
1040
+ return _context11.a(2, {
1041
+ count: keys.length,
1042
+ totalSize: totalSize,
1043
+ totalSizeMB: (totalSize / 1024 / 1024).toFixed(2),
1044
+ items: items.sort(function (a, b) {
1045
+ return b.size - a.size;
1046
+ })
1047
+ });
1048
+ case 34:
1049
+ _context11.p = 34;
1050
+ _t16 = _context11.v;
1051
+ this.config.onError(_t16);
1052
+ return _context11.a(2, {
1053
+ count: 0,
1054
+ totalSize: 0,
1055
+ totalSizeMB: '0',
1056
+ items: []
1057
+ });
1058
+ }
1059
+ }, _callee11, this, [[25, 31, 32, 33], [7, 13, 14, 15], [3, 18, 19, 20], [1, 34]]);
1060
+ }));
1061
+ function getDetailedStats() {
1062
+ return _getDetailedStats.apply(this, arguments);
1063
+ }
1064
+ return getDetailedStats;
1065
+ }()
1066
+ }]);
1067
+ }();
1068
+ var cacheManager = new CacheManager();
1069
+
1070
+ export { CACHE_EXPIRY, CACHE_NAME_PREFIX, CacheManager, cacheManager, cacheManager as default };