@modern-js/runtime-utils 2.69.5 → 3.0.0-alpha.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.
Files changed (135) hide show
  1. package/dist/cjs/browser/deferreds.js +141 -145
  2. package/dist/cjs/browser/index.js +62 -21
  3. package/dist/cjs/browser/nestedRoutes.js +121 -147
  4. package/dist/cjs/merge.js +51 -44
  5. package/dist/cjs/node/fileReader.js +88 -97
  6. package/dist/cjs/node/index.js +87 -33
  7. package/dist/cjs/node/loaderContext/createLoaderCtx.js +43 -36
  8. package/dist/cjs/node/loaderContext/createRequestCtx.js +43 -41
  9. package/dist/cjs/node/loaderContext/index.js +38 -29
  10. package/dist/cjs/node/serialize.js +46 -40
  11. package/dist/cjs/node/storer/container.js +62 -58
  12. package/dist/cjs/node/storer/index.js +39 -31
  13. package/dist/cjs/node/storer/storage.js +82 -92
  14. package/dist/cjs/node/stream.js +89 -91
  15. package/dist/cjs/parsed.js +40 -38
  16. package/dist/cjs/router.js +88 -19
  17. package/dist/cjs/rsc.js +58 -0
  18. package/dist/cjs/server/index.js +55 -19
  19. package/dist/cjs/server/nestedRoutes.js +43 -39
  20. package/dist/cjs/time.js +53 -47
  21. package/dist/cjs/universal/async_storage.js +53 -37
  22. package/dist/cjs/universal/async_storage.server.js +63 -74
  23. package/dist/cjs/universal/cache.js +330 -381
  24. package/dist/cjs/universal/request.js +73 -65
  25. package/dist/cjs/url.js +33 -28
  26. package/dist/esm/browser/deferreds.mjs +118 -0
  27. package/dist/esm/browser/nestedRoutes.mjs +104 -0
  28. package/dist/esm/merge.mjs +26 -0
  29. package/dist/esm/node/fileReader.mjs +47 -0
  30. package/dist/{esm-node/node/index.js → esm/node/index.mjs} +2 -5
  31. package/dist/esm/node/loaderContext/createLoaderCtx.mjs +14 -0
  32. package/dist/esm/node/loaderContext/createRequestCtx.mjs +16 -0
  33. package/dist/{esm-node/node/loaderContext/index.js → esm/node/loaderContext/index.mjs} +1 -4
  34. package/dist/esm/node/serialize.mjs +5 -0
  35. package/dist/esm/node/storer/container.mjs +38 -0
  36. package/dist/{esm-node/node/storer/index.js → esm/node/storer/index.mjs} +2 -6
  37. package/dist/esm/node/storer/storage.mjs +53 -0
  38. package/dist/esm/node/stream.mjs +68 -0
  39. package/dist/esm/parsed.mjs +12 -0
  40. package/dist/esm/router.mjs +18 -0
  41. package/dist/esm/rsc.mjs +1 -0
  42. package/dist/esm/server/nestedRoutes.mjs +15 -0
  43. package/dist/esm/time.mjs +27 -0
  44. package/dist/esm/universal/async_storage.mjs +7 -0
  45. package/dist/esm/universal/async_storage.server.mjs +32 -0
  46. package/dist/esm/universal/cache.mjs +326 -0
  47. package/dist/esm/universal/request.mjs +40 -0
  48. package/dist/esm/url.mjs +5 -0
  49. package/dist/esm-node/browser/deferreds.mjs +118 -0
  50. package/dist/esm-node/browser/index.mjs +2 -0
  51. package/dist/esm-node/browser/nestedRoutes.mjs +104 -0
  52. package/dist/esm-node/merge.mjs +26 -0
  53. package/dist/esm-node/node/fileReader.mjs +47 -0
  54. package/dist/esm-node/node/index.mjs +5 -0
  55. package/dist/esm-node/node/loaderContext/createLoaderCtx.mjs +14 -0
  56. package/dist/esm-node/node/loaderContext/createRequestCtx.mjs +16 -0
  57. package/dist/esm-node/node/loaderContext/index.mjs +4 -0
  58. package/dist/esm-node/node/serialize.mjs +5 -0
  59. package/dist/esm-node/node/storer/container.mjs +38 -0
  60. package/dist/esm-node/node/storer/index.mjs +7 -0
  61. package/dist/esm-node/node/storer/storage.mjs +53 -0
  62. package/dist/esm-node/node/stream.mjs +68 -0
  63. package/dist/esm-node/parsed.mjs +12 -0
  64. package/dist/esm-node/router.mjs +18 -0
  65. package/dist/esm-node/rsc.mjs +1 -0
  66. package/dist/esm-node/server/index.mjs +1 -0
  67. package/dist/esm-node/server/nestedRoutes.mjs +15 -0
  68. package/dist/esm-node/time.mjs +27 -0
  69. package/dist/esm-node/universal/async_storage.mjs +24 -0
  70. package/dist/esm-node/universal/async_storage.server.mjs +32 -0
  71. package/dist/esm-node/universal/cache.mjs +326 -0
  72. package/dist/esm-node/universal/request.mjs +40 -0
  73. package/dist/esm-node/url.mjs +5 -0
  74. package/dist/types/browser/nestedRoutes.d.ts +2 -1
  75. package/dist/types/node/stream.d.ts +1 -1
  76. package/dist/types/router.d.ts +6 -1
  77. package/dist/types/rsc.d.ts +1 -0
  78. package/dist/types/universal/async_storage.d.ts +1 -1
  79. package/dist/types/universal/async_storage.server.d.ts +1 -1
  80. package/package.json +36 -46
  81. package/rslib.config.mts +27 -0
  82. package/rstest.config.ts +26 -0
  83. package/dist/cjs/node/router.js +0 -22
  84. package/dist/cjs/remixRouter.js +0 -43
  85. package/dist/esm/browser/deferreds.js +0 -227
  86. package/dist/esm/browser/index.js +0 -2
  87. package/dist/esm/browser/nestedRoutes.js +0 -177
  88. package/dist/esm/merge.js +0 -33
  89. package/dist/esm/node/fileReader.js +0 -140
  90. package/dist/esm/node/index.js +0 -8
  91. package/dist/esm/node/loaderContext/createLoaderCtx.js +0 -24
  92. package/dist/esm/node/loaderContext/createRequestCtx.js +0 -24
  93. package/dist/esm/node/loaderContext/index.js +0 -7
  94. package/dist/esm/node/router.js +0 -1
  95. package/dist/esm/node/serialize.js +0 -9
  96. package/dist/esm/node/storer/container.js +0 -94
  97. package/dist/esm/node/storer/index.js +0 -11
  98. package/dist/esm/node/storer/storage.js +0 -135
  99. package/dist/esm/node/stream.js +0 -87
  100. package/dist/esm/parsed.js +0 -20
  101. package/dist/esm/remixRouter.js +0 -7
  102. package/dist/esm/router.js +0 -1
  103. package/dist/esm/server/index.js +0 -1
  104. package/dist/esm/server/nestedRoutes.js +0 -23
  105. package/dist/esm/time.js +0 -32
  106. package/dist/esm/universal/async_storage.js +0 -18
  107. package/dist/esm/universal/async_storage.server.js +0 -43
  108. package/dist/esm/universal/cache.js +0 -1032
  109. package/dist/esm/universal/request.js +0 -51
  110. package/dist/esm/url.js +0 -10
  111. package/dist/esm-node/browser/deferreds.js +0 -138
  112. package/dist/esm-node/browser/nestedRoutes.js +0 -142
  113. package/dist/esm-node/merge.js +0 -29
  114. package/dist/esm-node/node/fileReader.js +0 -68
  115. package/dist/esm-node/node/loaderContext/createLoaderCtx.js +0 -19
  116. package/dist/esm-node/node/loaderContext/createRequestCtx.js +0 -24
  117. package/dist/esm-node/node/router.js +0 -1
  118. package/dist/esm-node/node/serialize.js +0 -9
  119. package/dist/esm-node/node/storer/container.js +0 -44
  120. package/dist/esm-node/node/storer/storage.js +0 -73
  121. package/dist/esm-node/node/stream.js +0 -80
  122. package/dist/esm-node/parsed.js +0 -20
  123. package/dist/esm-node/remixRouter.js +0 -7
  124. package/dist/esm-node/router.js +0 -1
  125. package/dist/esm-node/server/nestedRoutes.js +0 -21
  126. package/dist/esm-node/time.js +0 -31
  127. package/dist/esm-node/universal/async_storage.js +0 -18
  128. package/dist/esm-node/universal/async_storage.server.js +0 -45
  129. package/dist/esm-node/universal/cache.js +0 -401
  130. package/dist/esm-node/universal/request.js +0 -50
  131. package/dist/esm-node/url.js +0 -10
  132. package/dist/types/node/router.d.ts +0 -1
  133. package/dist/types/remixRouter.d.ts +0 -2
  134. /package/dist/{esm-node/browser/index.js → esm/browser/index.mjs} +0 -0
  135. /package/dist/{esm-node/server/index.js → esm/server/index.mjs} +0 -0
@@ -1,1032 +0,0 @@
1
- import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
- import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3
- import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
4
- import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
5
- import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
6
- import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
7
- import { _ as _type_of } from "@swc/helpers/_/_type_of";
8
- import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
9
- import { LRUCache } from "lru-cache";
10
- import { getAsyncLocalStorage } from "./async_storage";
11
- var CacheSize = {
12
- KB: 1024,
13
- MB: 1024 * 1024,
14
- GB: 1024 * 1024 * 1024
15
- };
16
- var CacheTime = {
17
- SECOND: 1e3,
18
- MINUTE: 60 * 1e3,
19
- HOUR: 60 * 60 * 1e3,
20
- DAY: 24 * 60 * 60 * 1e3,
21
- WEEK: 7 * 24 * 60 * 60 * 1e3,
22
- MONTH: 30 * 24 * 60 * 60 * 1e3
23
- };
24
- function estimateObjectSize(data) {
25
- var type = typeof data === "undefined" ? "undefined" : _type_of(data);
26
- if (type === "number")
27
- return 8;
28
- if (type === "boolean")
29
- return 4;
30
- if (type === "string")
31
- return Math.max(data.length * 2, 1);
32
- if (data === null || data === void 0)
33
- return 1;
34
- if (ArrayBuffer.isView(data)) {
35
- return Math.max(data.byteLength, 1);
36
- }
37
- if (Array.isArray(data)) {
38
- return Math.max(data.reduce(function(acc, item) {
39
- return acc + estimateObjectSize(item);
40
- }, 0), 1);
41
- }
42
- if (_instanceof(data, Map) || _instanceof(data, Set)) {
43
- return 1024;
44
- }
45
- if (_instanceof(data, Date)) {
46
- return 8;
47
- }
48
- if (type === "object") {
49
- return Math.max(Object.entries(data).reduce(function(acc, param) {
50
- var _param = _sliced_to_array(param, 2), key = _param[0], value = _param[1];
51
- return acc + key.length * 2 + estimateObjectSize(value);
52
- }, 0), 1);
53
- }
54
- return 1;
55
- }
56
- var MemoryContainer = /* @__PURE__ */ function() {
57
- "use strict";
58
- function MemoryContainer2(options) {
59
- _class_call_check(this, MemoryContainer2);
60
- var _options_maxSize;
61
- this.lru = new LRUCache({
62
- maxSize: (_options_maxSize = options === null || options === void 0 ? void 0 : options.maxSize) !== null && _options_maxSize !== void 0 ? _options_maxSize : CacheSize.GB,
63
- sizeCalculation: estimateObjectSize,
64
- updateAgeOnGet: true,
65
- updateAgeOnHas: true
66
- });
67
- }
68
- var _proto = MemoryContainer2.prototype;
69
- _proto.get = function get(key) {
70
- var _this = this;
71
- return _async_to_generator(function() {
72
- return _ts_generator(this, function(_state) {
73
- return [
74
- 2,
75
- _this.lru.get(key)
76
- ];
77
- });
78
- })();
79
- };
80
- _proto.set = function set(key, value, options) {
81
- var _this = this;
82
- return _async_to_generator(function() {
83
- return _ts_generator(this, function(_state) {
84
- if (options === null || options === void 0 ? void 0 : options.ttl) {
85
- _this.lru.set(key, value, {
86
- ttl: options.ttl * 1e3
87
- });
88
- } else {
89
- _this.lru.set(key, value);
90
- }
91
- return [
92
- 2
93
- ];
94
- });
95
- })();
96
- };
97
- _proto.has = function has(key) {
98
- var _this = this;
99
- return _async_to_generator(function() {
100
- return _ts_generator(this, function(_state) {
101
- return [
102
- 2,
103
- _this.lru.has(key)
104
- ];
105
- });
106
- })();
107
- };
108
- _proto.delete = function _delete(key) {
109
- var _this = this;
110
- return _async_to_generator(function() {
111
- return _ts_generator(this, function(_state) {
112
- return [
113
- 2,
114
- _this.lru.delete(key)
115
- ];
116
- });
117
- })();
118
- };
119
- _proto.clear = function clear() {
120
- var _this = this;
121
- return _async_to_generator(function() {
122
- return _ts_generator(this, function(_state) {
123
- _this.lru.clear();
124
- return [
125
- 2
126
- ];
127
- });
128
- })();
129
- };
130
- return MemoryContainer2;
131
- }();
132
- var isServer = typeof window === "undefined";
133
- var requestCacheMap = /* @__PURE__ */ new WeakMap();
134
- var TAG_PREFIX = "tag:";
135
- var CACHE_PREFIX = "modernjs_cache:";
136
- var ongoingRevalidations = /* @__PURE__ */ new Map();
137
- var storage;
138
- var cacheConfig = {
139
- maxSize: CacheSize.GB
140
- };
141
- function getStorage() {
142
- if (storage) {
143
- return storage;
144
- }
145
- if (cacheConfig.container) {
146
- storage = cacheConfig.container;
147
- } else {
148
- storage = new MemoryContainer({
149
- maxSize: cacheConfig.maxSize
150
- });
151
- }
152
- return storage;
153
- }
154
- function configureCache(config) {
155
- cacheConfig = _object_spread({}, cacheConfig, config);
156
- storage = void 0;
157
- }
158
- function generateKey(args) {
159
- return JSON.stringify(args, function(_, value) {
160
- if (value && (typeof value === "undefined" ? "undefined" : _type_of(value)) === "object" && !Array.isArray(value)) {
161
- return Object.keys(value).sort().reduce(function(result, key) {
162
- result[key] = value[key];
163
- return result;
164
- }, {});
165
- }
166
- return value;
167
- });
168
- }
169
- function generateStableFunctionId(fn) {
170
- var fnString = fn.toString();
171
- var hash = 0;
172
- for (var i = 0; i < fnString.length; i++) {
173
- var char = fnString.charCodeAt(i);
174
- hash = (hash << 5) - hash + char;
175
- hash = hash & hash;
176
- }
177
- return "fn_".concat(fn.name || "anonymous", "_").concat(Math.abs(hash).toString(36));
178
- }
179
- function cache(fn, options) {
180
- return /* @__PURE__ */ _async_to_generator(function() {
181
- var _len, args, _key, _storage_useContext, storage2, request, shouldDisableCaching, requestCache, fnCache, key, promise, data, error, tag, _options_maxAge, maxAge, _options_revalidate, revalidate, customKey, onCache, getKey, unstable_shouldCache, missReason, currentStorage, now, tags, genKey, finalKey, functionId, storageKey, shouldDisableCaching1, _asyncStorage_useContext, asyncStorage, request1, cached, cacheItem, age, revalidationPromise, data1, shouldCache, error1, data2, onCache1;
182
- var _arguments = arguments;
183
- return _ts_generator(this, function(_state) {
184
- switch (_state.label) {
185
- case 0:
186
- for (_len = _arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
187
- args[_key] = _arguments[_key];
188
- }
189
- if (!(isServer && typeof options === "undefined"))
190
- return [
191
- 3,
192
- 7
193
- ];
194
- storage2 = getAsyncLocalStorage();
195
- request = storage2 === null || storage2 === void 0 ? void 0 : (_storage_useContext = storage2.useContext()) === null || _storage_useContext === void 0 ? void 0 : _storage_useContext.request;
196
- if (!request)
197
- return [
198
- 3,
199
- 6
200
- ];
201
- shouldDisableCaching = false;
202
- if (!cacheConfig.unstable_shouldDisable)
203
- return [
204
- 3,
205
- 2
206
- ];
207
- return [
208
- 4,
209
- cacheConfig.unstable_shouldDisable({
210
- request
211
- })
212
- ];
213
- case 1:
214
- shouldDisableCaching = _state.sent();
215
- _state.label = 2;
216
- case 2:
217
- if (shouldDisableCaching) {
218
- return [
219
- 2,
220
- fn.apply(void 0, _to_consumable_array(args))
221
- ];
222
- }
223
- requestCache = requestCacheMap.get(request);
224
- if (!requestCache) {
225
- requestCache = /* @__PURE__ */ new Map();
226
- requestCacheMap.set(request, requestCache);
227
- }
228
- fnCache = requestCache.get(fn);
229
- if (!fnCache) {
230
- fnCache = /* @__PURE__ */ new Map();
231
- requestCache.set(fn, fnCache);
232
- }
233
- key = generateKey(args);
234
- if (fnCache.has(key)) {
235
- return [
236
- 2,
237
- fnCache.get(key)
238
- ];
239
- }
240
- promise = fn.apply(void 0, _to_consumable_array(args));
241
- fnCache.set(key, promise);
242
- _state.label = 3;
243
- case 3:
244
- _state.trys.push([
245
- 3,
246
- 5,
247
- ,
248
- 6
249
- ]);
250
- return [
251
- 4,
252
- promise
253
- ];
254
- case 4:
255
- data = _state.sent();
256
- return [
257
- 2,
258
- data
259
- ];
260
- case 5:
261
- error = _state.sent();
262
- fnCache.delete(key);
263
- throw error;
264
- case 6:
265
- return [
266
- 3,
267
- 23
268
- ];
269
- case 7:
270
- if (!(typeof options !== "undefined"))
271
- return [
272
- 3,
273
- 22
274
- ];
275
- _state.label = 8;
276
- case 8:
277
- _state.trys.push([
278
- 8,
279
- 19,
280
- ,
281
- 21
282
- ]);
283
- tag = options.tag, _options_maxAge = options.maxAge, maxAge = _options_maxAge === void 0 ? CacheTime.MINUTE * 5 : _options_maxAge, _options_revalidate = options.revalidate, revalidate = _options_revalidate === void 0 ? 0 : _options_revalidate, customKey = options.customKey, onCache = options.onCache, getKey = options.getKey, unstable_shouldCache = options.unstable_shouldCache;
284
- currentStorage = getStorage();
285
- now = Date.now();
286
- tags = tag ? Array.isArray(tag) ? tag : [
287
- tag
288
- ] : [];
289
- genKey = getKey ? getKey.apply(void 0, _to_consumable_array(args)) : generateKey(args);
290
- if (customKey) {
291
- finalKey = customKey({
292
- params: args,
293
- fn,
294
- generatedKey: genKey
295
- });
296
- } else {
297
- functionId = generateStableFunctionId(fn);
298
- finalKey = "".concat(functionId, ":").concat(genKey);
299
- }
300
- storageKey = "".concat(CACHE_PREFIX).concat(finalKey);
301
- shouldDisableCaching1 = false;
302
- if (!(isServer && cacheConfig.unstable_shouldDisable))
303
- return [
304
- 3,
305
- 10
306
- ];
307
- asyncStorage = getAsyncLocalStorage();
308
- request1 = asyncStorage === null || asyncStorage === void 0 ? void 0 : (_asyncStorage_useContext = asyncStorage.useContext()) === null || _asyncStorage_useContext === void 0 ? void 0 : _asyncStorage_useContext.request;
309
- if (!request1)
310
- return [
311
- 3,
312
- 10
313
- ];
314
- return [
315
- 4,
316
- cacheConfig.unstable_shouldDisable({
317
- request: request1
318
- })
319
- ];
320
- case 9:
321
- shouldDisableCaching1 = _state.sent();
322
- _state.label = 10;
323
- case 10:
324
- if (!!shouldDisableCaching1)
325
- return [
326
- 3,
327
- 12
328
- ];
329
- return [
330
- 4,
331
- currentStorage.get(storageKey)
332
- ];
333
- case 11:
334
- cached = _state.sent();
335
- if (cached) {
336
- try {
337
- cacheItem = cached;
338
- age = now - cacheItem.timestamp;
339
- if (age < maxAge) {
340
- onCache === null || onCache === void 0 ? void 0 : onCache({
341
- status: "hit",
342
- key: finalKey,
343
- params: args,
344
- result: cacheItem.data
345
- });
346
- return [
347
- 2,
348
- cacheItem.data
349
- ];
350
- }
351
- if (revalidate > 0 && age < maxAge + revalidate) {
352
- onCache === null || onCache === void 0 ? void 0 : onCache({
353
- status: "stale",
354
- key: finalKey,
355
- params: args,
356
- result: cacheItem.data
357
- });
358
- if (!ongoingRevalidations.has(storageKey)) {
359
- revalidationPromise = _async_to_generator(function() {
360
- var newData, shouldCache2, error2, _asyncStorage_useContext_monitors, _asyncStorage_useContext2, asyncStorage2;
361
- return _ts_generator(this, function(_state2) {
362
- switch (_state2.label) {
363
- case 0:
364
- _state2.trys.push([
365
- 0,
366
- 6,
367
- 7,
368
- 8
369
- ]);
370
- return [
371
- 4,
372
- fn.apply(void 0, _to_consumable_array(args))
373
- ];
374
- case 1:
375
- newData = _state2.sent();
376
- shouldCache2 = true;
377
- if (!unstable_shouldCache)
378
- return [
379
- 3,
380
- 3
381
- ];
382
- return [
383
- 4,
384
- unstable_shouldCache({
385
- params: args,
386
- result: newData
387
- })
388
- ];
389
- case 2:
390
- shouldCache2 = _state2.sent();
391
- _state2.label = 3;
392
- case 3:
393
- if (!shouldCache2)
394
- return [
395
- 3,
396
- 5
397
- ];
398
- return [
399
- 4,
400
- setCacheItem(currentStorage, storageKey, newData, tags, maxAge, revalidate)
401
- ];
402
- case 4:
403
- _state2.sent();
404
- _state2.label = 5;
405
- case 5:
406
- return [
407
- 3,
408
- 8
409
- ];
410
- case 6:
411
- error2 = _state2.sent();
412
- if (isServer) {
413
- ;
414
- asyncStorage2 = getAsyncLocalStorage();
415
- asyncStorage2 === null || asyncStorage2 === void 0 ? void 0 : (_asyncStorage_useContext2 = asyncStorage2.useContext()) === null || _asyncStorage_useContext2 === void 0 ? void 0 : (_asyncStorage_useContext_monitors = _asyncStorage_useContext2.monitors) === null || _asyncStorage_useContext_monitors === void 0 ? void 0 : _asyncStorage_useContext_monitors.error(error2.message);
416
- } else {
417
- console.error("Background revalidation failed:", error2);
418
- }
419
- return [
420
- 3,
421
- 8
422
- ];
423
- case 7:
424
- ongoingRevalidations.delete(storageKey);
425
- return [
426
- 7
427
- ];
428
- case 8:
429
- return [
430
- 2
431
- ];
432
- }
433
- });
434
- })();
435
- ongoingRevalidations.set(storageKey, revalidationPromise);
436
- }
437
- return [
438
- 2,
439
- cacheItem.data
440
- ];
441
- }
442
- missReason = 3;
443
- } catch (error2) {
444
- console.warn("Failed to parse cached data:", error2);
445
- missReason = 4;
446
- }
447
- } else {
448
- missReason = 2;
449
- }
450
- return [
451
- 3,
452
- 13
453
- ];
454
- case 12:
455
- missReason = 1;
456
- _state.label = 13;
457
- case 13:
458
- return [
459
- 4,
460
- fn.apply(void 0, _to_consumable_array(args))
461
- ];
462
- case 14:
463
- data1 = _state.sent();
464
- if (!!shouldDisableCaching1)
465
- return [
466
- 3,
467
- 18
468
- ];
469
- shouldCache = true;
470
- if (!unstable_shouldCache)
471
- return [
472
- 3,
473
- 16
474
- ];
475
- return [
476
- 4,
477
- unstable_shouldCache({
478
- params: args,
479
- result: data1
480
- })
481
- ];
482
- case 15:
483
- shouldCache = _state.sent();
484
- _state.label = 16;
485
- case 16:
486
- if (!shouldCache)
487
- return [
488
- 3,
489
- 18
490
- ];
491
- return [
492
- 4,
493
- setCacheItem(currentStorage, storageKey, data1, tags, maxAge, revalidate)
494
- ];
495
- case 17:
496
- _state.sent();
497
- _state.label = 18;
498
- case 18:
499
- onCache === null || onCache === void 0 ? void 0 : onCache({
500
- status: "miss",
501
- key: finalKey,
502
- params: args,
503
- result: data1,
504
- reason: missReason
505
- });
506
- return [
507
- 2,
508
- data1
509
- ];
510
- case 19:
511
- error1 = _state.sent();
512
- console.warn("Cache operation failed, falling back to direct execution:", error1);
513
- return [
514
- 4,
515
- fn.apply(void 0, _to_consumable_array(args))
516
- ];
517
- case 20:
518
- data2 = _state.sent();
519
- onCache1 = options.onCache;
520
- try {
521
- onCache1 === null || onCache1 === void 0 ? void 0 : onCache1({
522
- status: "miss",
523
- key: "cache_failed",
524
- params: args,
525
- result: data2,
526
- reason: 5
527
- });
528
- } catch (callbackError) {
529
- console.warn("Failed to call onCache callback:", callbackError);
530
- }
531
- return [
532
- 2,
533
- data2
534
- ];
535
- case 21:
536
- return [
537
- 3,
538
- 23
539
- ];
540
- case 22:
541
- console.warn("The cache function will not work because it runs on the browser and there are no options are provided.");
542
- return [
543
- 2,
544
- fn.apply(void 0, _to_consumable_array(args))
545
- ];
546
- case 23:
547
- return [
548
- 2
549
- ];
550
- }
551
- });
552
- });
553
- }
554
- function setCacheItem(storage2, storageKey, data, tags, maxAge, revalidate) {
555
- return _setCacheItem.apply(this, arguments);
556
- }
557
- function _setCacheItem() {
558
- _setCacheItem = _async_to_generator(function(storage2, storageKey, data, tags, maxAge, revalidate) {
559
- var newItem, ttl;
560
- return _ts_generator(this, function(_state) {
561
- switch (_state.label) {
562
- case 0:
563
- newItem = {
564
- data,
565
- timestamp: Date.now(),
566
- tags: tags.length > 0 ? tags : void 0
567
- };
568
- ttl = (maxAge + revalidate) / 1e3;
569
- return [
570
- 4,
571
- storage2.set(storageKey, newItem, {
572
- ttl: ttl > 0 ? ttl : void 0
573
- })
574
- ];
575
- case 1:
576
- _state.sent();
577
- return [
578
- 4,
579
- updateTagRelationships(storage2, storageKey, tags)
580
- ];
581
- case 2:
582
- _state.sent();
583
- return [
584
- 2
585
- ];
586
- }
587
- });
588
- });
589
- return _setCacheItem.apply(this, arguments);
590
- }
591
- function updateTagRelationships(storage2, storageKey, tags) {
592
- return _updateTagRelationships.apply(this, arguments);
593
- }
594
- function _updateTagRelationships() {
595
- _updateTagRelationships = _async_to_generator(function(storage2, storageKey, tags) {
596
- var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, tag, tagStoreKey, keyList, keyArray, err;
597
- return _ts_generator(this, function(_state) {
598
- switch (_state.label) {
599
- case 0:
600
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
601
- _state.label = 1;
602
- case 1:
603
- _state.trys.push([
604
- 1,
605
- 7,
606
- 8,
607
- 9
608
- ]);
609
- _iterator = tags[Symbol.iterator]();
610
- _state.label = 2;
611
- case 2:
612
- if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done))
613
- return [
614
- 3,
615
- 6
616
- ];
617
- tag = _step.value;
618
- tagStoreKey = "".concat(TAG_PREFIX).concat(tag);
619
- return [
620
- 4,
621
- storage2.get(tagStoreKey)
622
- ];
623
- case 3:
624
- keyList = _state.sent();
625
- keyArray = keyList || [];
626
- if (!keyArray.includes(storageKey)) {
627
- keyArray.push(storageKey);
628
- }
629
- return [
630
- 4,
631
- storage2.set(tagStoreKey, keyArray)
632
- ];
633
- case 4:
634
- _state.sent();
635
- _state.label = 5;
636
- case 5:
637
- _iteratorNormalCompletion = true;
638
- return [
639
- 3,
640
- 2
641
- ];
642
- case 6:
643
- return [
644
- 3,
645
- 9
646
- ];
647
- case 7:
648
- err = _state.sent();
649
- _didIteratorError = true;
650
- _iteratorError = err;
651
- return [
652
- 3,
653
- 9
654
- ];
655
- case 8:
656
- try {
657
- if (!_iteratorNormalCompletion && _iterator.return != null) {
658
- _iterator.return();
659
- }
660
- } finally {
661
- if (_didIteratorError) {
662
- throw _iteratorError;
663
- }
664
- }
665
- return [
666
- 7
667
- ];
668
- case 9:
669
- return [
670
- 2
671
- ];
672
- }
673
- });
674
- });
675
- return _updateTagRelationships.apply(this, arguments);
676
- }
677
- function removeKeyFromTags(storage2, storageKey, tags) {
678
- return _removeKeyFromTags.apply(this, arguments);
679
- }
680
- function _removeKeyFromTags() {
681
- _removeKeyFromTags = _async_to_generator(function(storage2, storageKey, tags) {
682
- var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, tag, tagStoreKey, keyList, keyArray, updatedKeyList, error, err;
683
- return _ts_generator(this, function(_state) {
684
- switch (_state.label) {
685
- case 0:
686
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
687
- _state.label = 1;
688
- case 1:
689
- _state.trys.push([
690
- 1,
691
- 12,
692
- 13,
693
- 14
694
- ]);
695
- _iterator = tags[Symbol.iterator]();
696
- _state.label = 2;
697
- case 2:
698
- if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done))
699
- return [
700
- 3,
701
- 11
702
- ];
703
- tag = _step.value;
704
- tagStoreKey = "".concat(TAG_PREFIX).concat(tag);
705
- return [
706
- 4,
707
- storage2.get(tagStoreKey)
708
- ];
709
- case 3:
710
- keyList = _state.sent();
711
- if (!keyList)
712
- return [
713
- 3,
714
- 10
715
- ];
716
- _state.label = 4;
717
- case 4:
718
- _state.trys.push([
719
- 4,
720
- 9,
721
- ,
722
- 10
723
- ]);
724
- keyArray = Array.isArray(keyList) ? keyList : [];
725
- updatedKeyList = keyArray.filter(function(key) {
726
- return key !== storageKey;
727
- });
728
- if (!(updatedKeyList.length > 0))
729
- return [
730
- 3,
731
- 6
732
- ];
733
- return [
734
- 4,
735
- storage2.set(tagStoreKey, updatedKeyList)
736
- ];
737
- case 5:
738
- _state.sent();
739
- return [
740
- 3,
741
- 8
742
- ];
743
- case 6:
744
- return [
745
- 4,
746
- storage2.delete(tagStoreKey)
747
- ];
748
- case 7:
749
- _state.sent();
750
- _state.label = 8;
751
- case 8:
752
- return [
753
- 3,
754
- 10
755
- ];
756
- case 9:
757
- error = _state.sent();
758
- console.warn("Failed to process tag key list for tag ".concat(tag, ":"), error);
759
- return [
760
- 3,
761
- 10
762
- ];
763
- case 10:
764
- _iteratorNormalCompletion = true;
765
- return [
766
- 3,
767
- 2
768
- ];
769
- case 11:
770
- return [
771
- 3,
772
- 14
773
- ];
774
- case 12:
775
- err = _state.sent();
776
- _didIteratorError = true;
777
- _iteratorError = err;
778
- return [
779
- 3,
780
- 14
781
- ];
782
- case 13:
783
- try {
784
- if (!_iteratorNormalCompletion && _iterator.return != null) {
785
- _iterator.return();
786
- }
787
- } finally {
788
- if (_didIteratorError) {
789
- throw _iteratorError;
790
- }
791
- }
792
- return [
793
- 7
794
- ];
795
- case 14:
796
- return [
797
- 2
798
- ];
799
- }
800
- });
801
- });
802
- return _removeKeyFromTags.apply(this, arguments);
803
- }
804
- function withRequestCache(handler) {
805
- if (!isServer) {
806
- return handler;
807
- }
808
- return function() {
809
- var _ref = _async_to_generator(function(req) {
810
- var _len, args, _key, storage2;
811
- var _arguments = arguments;
812
- return _ts_generator(this, function(_state) {
813
- for (_len = _arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
814
- args[_key - 1] = _arguments[_key];
815
- }
816
- storage2 = getAsyncLocalStorage();
817
- return [
818
- 2,
819
- storage2.run({
820
- request: req
821
- }, function() {
822
- return handler.apply(void 0, [
823
- req
824
- ].concat(_to_consumable_array(args)));
825
- })
826
- ];
827
- });
828
- });
829
- return function(req) {
830
- return _ref.apply(this, arguments);
831
- };
832
- }();
833
- }
834
- function revalidateTag(tag) {
835
- return _revalidateTag.apply(this, arguments);
836
- }
837
- function _revalidateTag() {
838
- _revalidateTag = _async_to_generator(function(tag) {
839
- var currentStorage, tagStoreKey, keyList, keyArray, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, cacheKey, cached, cacheItem, otherTags, error, err, error1;
840
- return _ts_generator(this, function(_state) {
841
- switch (_state.label) {
842
- case 0:
843
- currentStorage = getStorage();
844
- tagStoreKey = "".concat(TAG_PREFIX).concat(tag);
845
- return [
846
- 4,
847
- currentStorage.get(tagStoreKey)
848
- ];
849
- case 1:
850
- keyList = _state.sent();
851
- if (!keyList)
852
- return [
853
- 3,
854
- 19
855
- ];
856
- _state.label = 2;
857
- case 2:
858
- _state.trys.push([
859
- 2,
860
- 18,
861
- ,
862
- 19
863
- ]);
864
- keyArray = Array.isArray(keyList) ? keyList : [];
865
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
866
- _state.label = 3;
867
- case 3:
868
- _state.trys.push([
869
- 3,
870
- 14,
871
- 15,
872
- 16
873
- ]);
874
- _iterator = keyArray[Symbol.iterator]();
875
- _state.label = 4;
876
- case 4:
877
- if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done))
878
- return [
879
- 3,
880
- 13
881
- ];
882
- cacheKey = _step.value;
883
- return [
884
- 4,
885
- currentStorage.get(cacheKey)
886
- ];
887
- case 5:
888
- cached = _state.sent();
889
- if (!cached)
890
- return [
891
- 3,
892
- 10
893
- ];
894
- _state.label = 6;
895
- case 6:
896
- _state.trys.push([
897
- 6,
898
- 9,
899
- ,
900
- 10
901
- ]);
902
- cacheItem = cached;
903
- if (!cacheItem.tags)
904
- return [
905
- 3,
906
- 8
907
- ];
908
- otherTags = cacheItem.tags.filter(function(t) {
909
- return t !== tag;
910
- });
911
- return [
912
- 4,
913
- removeKeyFromTags(currentStorage, cacheKey, otherTags)
914
- ];
915
- case 7:
916
- _state.sent();
917
- _state.label = 8;
918
- case 8:
919
- return [
920
- 3,
921
- 10
922
- ];
923
- case 9:
924
- error = _state.sent();
925
- console.warn("Failed to parse cached data while revalidating:", error);
926
- return [
927
- 3,
928
- 10
929
- ];
930
- case 10:
931
- return [
932
- 4,
933
- currentStorage.delete(cacheKey)
934
- ];
935
- case 11:
936
- _state.sent();
937
- _state.label = 12;
938
- case 12:
939
- _iteratorNormalCompletion = true;
940
- return [
941
- 3,
942
- 4
943
- ];
944
- case 13:
945
- return [
946
- 3,
947
- 16
948
- ];
949
- case 14:
950
- err = _state.sent();
951
- _didIteratorError = true;
952
- _iteratorError = err;
953
- return [
954
- 3,
955
- 16
956
- ];
957
- case 15:
958
- try {
959
- if (!_iteratorNormalCompletion && _iterator.return != null) {
960
- _iterator.return();
961
- }
962
- } finally {
963
- if (_didIteratorError) {
964
- throw _iteratorError;
965
- }
966
- }
967
- return [
968
- 7
969
- ];
970
- case 16:
971
- return [
972
- 4,
973
- currentStorage.delete(tagStoreKey)
974
- ];
975
- case 17:
976
- _state.sent();
977
- return [
978
- 3,
979
- 19
980
- ];
981
- case 18:
982
- error1 = _state.sent();
983
- console.warn("Failed to process tag key list:", error1);
984
- return [
985
- 3,
986
- 19
987
- ];
988
- case 19:
989
- return [
990
- 2
991
- ];
992
- }
993
- });
994
- });
995
- return _revalidateTag.apply(this, arguments);
996
- }
997
- function clearStore() {
998
- return _clearStore.apply(this, arguments);
999
- }
1000
- function _clearStore() {
1001
- _clearStore = _async_to_generator(function() {
1002
- var currentStorage;
1003
- return _ts_generator(this, function(_state) {
1004
- switch (_state.label) {
1005
- case 0:
1006
- currentStorage = getStorage();
1007
- return [
1008
- 4,
1009
- currentStorage.clear()
1010
- ];
1011
- case 1:
1012
- _state.sent();
1013
- storage = void 0;
1014
- ongoingRevalidations.clear();
1015
- return [
1016
- 2
1017
- ];
1018
- }
1019
- });
1020
- });
1021
- return _clearStore.apply(this, arguments);
1022
- }
1023
- export {
1024
- CacheSize,
1025
- CacheTime,
1026
- cache,
1027
- clearStore,
1028
- configureCache,
1029
- generateKey,
1030
- revalidateTag,
1031
- withRequestCache
1032
- };