@modern-js/prod-server 2.42.2 → 2.44.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 (90) hide show
  1. package/dist/cjs/libs/{render/cache/page-caches/index.js → hook-api/afterRenderForStream.js} +12 -14
  2. package/dist/cjs/libs/hook-api/index.js +12 -0
  3. package/dist/cjs/libs/render/index.js +4 -14
  4. package/dist/cjs/libs/render/ssr.js +15 -4
  5. package/dist/cjs/libs/render/{cache/page-caches/lru.js → ssrCache/cacheMod.js} +21 -31
  6. package/dist/cjs/libs/render/ssrCache/index.js +81 -0
  7. package/dist/cjs/libs/render/ssrCache/manager.js +104 -0
  8. package/dist/cjs/libs/render/static.js +5 -5
  9. package/dist/cjs/server/index.js +2 -0
  10. package/dist/cjs/server/modernServer.js +2 -9
  11. package/dist/esm/libs/hook-api/afterRenderForStream.js +33 -0
  12. package/dist/esm/libs/hook-api/index.js +10 -0
  13. package/dist/esm/libs/render/index.js +4 -14
  14. package/dist/esm/libs/render/ssr.js +19 -5
  15. package/dist/esm/libs/render/ssrCache/cacheMod.js +33 -0
  16. package/dist/esm/libs/render/ssrCache/index.js +146 -0
  17. package/dist/esm/libs/render/ssrCache/manager.js +159 -0
  18. package/dist/esm/libs/render/static.js +5 -5
  19. package/dist/esm/server/index.js +14 -4
  20. package/dist/esm/server/modernServer.js +32 -42
  21. package/dist/esm-node/libs/hook-api/afterRenderForStream.js +10 -0
  22. package/dist/esm-node/libs/hook-api/index.js +11 -0
  23. package/dist/esm-node/libs/render/index.js +4 -14
  24. package/dist/esm-node/libs/render/ssr.js +15 -4
  25. package/dist/esm-node/libs/render/ssrCache/cacheMod.js +22 -0
  26. package/dist/esm-node/libs/render/ssrCache/index.js +57 -0
  27. package/dist/esm-node/libs/render/ssrCache/manager.js +80 -0
  28. package/dist/esm-node/libs/render/static.js +5 -5
  29. package/dist/esm-node/server/index.js +2 -0
  30. package/dist/esm-node/server/modernServer.js +2 -9
  31. package/dist/types/libs/context/index.d.ts +2 -0
  32. package/dist/types/libs/hook-api/afterRenderForStream.d.ts +4 -0
  33. package/dist/types/libs/hook-api/index.d.ts +2 -1
  34. package/dist/types/libs/preload/flushServerHeader.d.ts +2 -0
  35. package/dist/types/libs/proxy.d.ts +2 -0
  36. package/dist/types/libs/render/ssr.d.ts +5 -5
  37. package/dist/types/libs/render/ssrCache/cacheMod.d.ts +8 -0
  38. package/dist/types/libs/render/ssrCache/index.d.ts +7 -0
  39. package/dist/types/libs/render/ssrCache/manager.d.ts +14 -0
  40. package/dist/types/renderHtml.d.ts +2 -0
  41. package/dist/types/server/index.d.ts +2 -0
  42. package/dist/types/server/modernServer.d.ts +2 -3
  43. package/dist/types/type.d.ts +2 -0
  44. package/package.json +8 -11
  45. package/dist/cjs/libs/render/cache/__tests__/cache.fun.test.js +0 -95
  46. package/dist/cjs/libs/render/cache/__tests__/cache.test.js +0 -223
  47. package/dist/cjs/libs/render/cache/__tests__/cacheable.js +0 -93
  48. package/dist/cjs/libs/render/cache/__tests__/error-configuration.js +0 -71
  49. package/dist/cjs/libs/render/cache/__tests__/matched-cache.js +0 -173
  50. package/dist/cjs/libs/render/cache/index.js +0 -92
  51. package/dist/cjs/libs/render/cache/spr.js +0 -247
  52. package/dist/cjs/libs/render/cache/type.js +0 -16
  53. package/dist/cjs/libs/render/cache/util.js +0 -121
  54. package/dist/cjs/libs/render/reader.js +0 -131
  55. package/dist/esm/libs/render/cache/__tests__/cache.fun.test.js +0 -124
  56. package/dist/esm/libs/render/cache/__tests__/cache.test.js +0 -612
  57. package/dist/esm/libs/render/cache/__tests__/cacheable.js +0 -69
  58. package/dist/esm/libs/render/cache/__tests__/error-configuration.js +0 -47
  59. package/dist/esm/libs/render/cache/__tests__/matched-cache.js +0 -149
  60. package/dist/esm/libs/render/cache/index.js +0 -204
  61. package/dist/esm/libs/render/cache/page-caches/index.js +0 -34
  62. package/dist/esm/libs/render/cache/page-caches/lru.js +0 -57
  63. package/dist/esm/libs/render/cache/spr.js +0 -345
  64. package/dist/esm/libs/render/cache/type.js +0 -0
  65. package/dist/esm/libs/render/cache/util.js +0 -105
  66. package/dist/esm/libs/render/reader.js +0 -196
  67. package/dist/esm-node/libs/render/cache/__tests__/cache.fun.test.js +0 -72
  68. package/dist/esm-node/libs/render/cache/__tests__/cache.test.js +0 -222
  69. package/dist/esm-node/libs/render/cache/__tests__/cacheable.js +0 -69
  70. package/dist/esm-node/libs/render/cache/__tests__/error-configuration.js +0 -47
  71. package/dist/esm-node/libs/render/cache/__tests__/matched-cache.js +0 -149
  72. package/dist/esm-node/libs/render/cache/index.js +0 -72
  73. package/dist/esm-node/libs/render/cache/page-caches/index.js +0 -12
  74. package/dist/esm-node/libs/render/cache/page-caches/lru.js +0 -32
  75. package/dist/esm-node/libs/render/cache/spr.js +0 -212
  76. package/dist/esm-node/libs/render/cache/type.js +0 -0
  77. package/dist/esm-node/libs/render/cache/util.js +0 -80
  78. package/dist/esm-node/libs/render/reader.js +0 -93
  79. package/dist/types/libs/render/cache/__tests__/cache.fun.test.d.ts +0 -1
  80. package/dist/types/libs/render/cache/__tests__/cache.test.d.ts +0 -1
  81. package/dist/types/libs/render/cache/__tests__/cacheable.d.ts +0 -62
  82. package/dist/types/libs/render/cache/__tests__/error-configuration.d.ts +0 -28
  83. package/dist/types/libs/render/cache/__tests__/matched-cache.d.ts +0 -124
  84. package/dist/types/libs/render/cache/index.d.ts +0 -6
  85. package/dist/types/libs/render/cache/page-caches/index.d.ts +0 -2
  86. package/dist/types/libs/render/cache/page-caches/lru.d.ts +0 -15
  87. package/dist/types/libs/render/cache/spr.d.ts +0 -22
  88. package/dist/types/libs/render/cache/type.d.ts +0 -48
  89. package/dist/types/libs/render/cache/util.d.ts +0 -18
  90. package/dist/types/libs/render/reader.d.ts +0 -20
@@ -1,345 +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 _create_class } from "@swc/helpers/_/_create_class";
4
- import { _ as _define_property } from "@swc/helpers/_/_define_property";
5
- import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
6
- import crypto from "crypto";
7
- import LRUCache from "lru-cache";
8
- import { mime } from "@modern-js/utils";
9
- import { cacheAddition, connectFactor, fname, maybeSync, namespaceHash, valueFactory, withCoalescedInvoke } from "./util";
10
- import { createPageCaches } from "./page-caches";
11
- var MAX_CACHE_EACH_REQ = Number(process.env.ROUTE_CACHE_LIMIT) || 10;
12
- var MAX_SIZE_EACH_CLUSTER = Number(process.env.CLUSTER_CACHE_LIMIT) || 100;
13
- var BASE_LEVEL = 0;
14
- var QUERY_LEVEL = 1;
15
- var HEADER_LEVEL = 2;
16
- var QUERY_HEADER_LEVEL = 3;
17
- var CacheManager = /* @__PURE__ */ function() {
18
- "use strict";
19
- function CacheManager2(cacheOptions) {
20
- var _this = this;
21
- _class_call_check(this, CacheManager2);
22
- _define_property(this, "cache", void 0);
23
- _define_property(this, "cacheOptions", void 0);
24
- _define_property(this, "find", function() {
25
- {
26
- var _this1 = _this;
27
- var _obj;
28
- return _obj = {}, _define_property(_obj, fname(BASE_LEVEL), function(context, cacheKey) {
29
- return _this1.md5(cacheKey);
30
- }), _define_property(_obj, fname(QUERY_LEVEL), function(context, cacheKey, data) {
31
- var queryFactor = _this1.queryFactor(context, data);
32
- if (!queryFactor) {
33
- return null;
34
- }
35
- return _this1.md5(connectFactor(cacheKey, queryFactor));
36
- }), _define_property(_obj, fname(HEADER_LEVEL), function(context, cacheKey, data) {
37
- var headerFactor = _this1.headerFactor(context, data);
38
- if (!headerFactor) {
39
- return null;
40
- }
41
- return _this1.md5(connectFactor(cacheKey, headerFactor));
42
- }), _define_property(_obj, fname(QUERY_HEADER_LEVEL), function(context, cacheKey, data) {
43
- var queryFactor = _this1.queryFactor(context, data);
44
- var headerFactor = _this1.headerFactor(context, data);
45
- if (!queryFactor || !headerFactor) {
46
- return null;
47
- }
48
- return _this1.md5(connectFactor(cacheKey, headerFactor, queryFactor));
49
- }), _obj;
50
- }
51
- }());
52
- this.cacheOptions = cacheOptions;
53
- this.cache = new LRUCache({
54
- max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
55
- length: function length(n) {
56
- var len = n.caches.keys().reduce(function(total, cur) {
57
- var _n_caches_peek;
58
- return total + (((_n_caches_peek = n.caches.peek(cur)) === null || _n_caches_peek === void 0 ? void 0 : _n_caches_peek.size) || 0);
59
- }, 1);
60
- return len;
61
- }
62
- });
63
- }
64
- _create_class(CacheManager2, [
65
- {
66
- key: "md5",
67
- value: function md5(content) {
68
- var md52 = crypto.createHash("md5");
69
- return md52.update(content).digest("hex");
70
- }
71
- },
72
- {
73
- key: "generateRequestKey",
74
- value: function generateRequestKey(context) {
75
- var pathname = context.pathname, entry = context.entry;
76
- return this.md5("".concat(pathname, "_").concat(entry));
77
- }
78
- },
79
- {
80
- key: "replaceValue",
81
- value: function replaceValue(value, matcher) {
82
- var final = value;
83
- Object.keys(matcher).some(function(replacer) {
84
- var reg = new RegExp(matcher[replacer]);
85
- if (reg.test(value)) {
86
- final = replacer;
87
- return true;
88
- }
89
- return false;
90
- });
91
- return final;
92
- }
93
- },
94
- {
95
- key: "factor",
96
- value: function factor(keys, obj) {
97
- var matches = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
98
- var _this = this;
99
- keys.sort();
100
- var getValue = valueFactory(obj);
101
- var factorAry = keys.reduce(function(ary, key) {
102
- var value = getValue(key) || "";
103
- var matcher = matches[key];
104
- if (matcher) {
105
- value = _this.replaceValue(value, matcher);
106
- }
107
- return ary.concat([
108
- key,
109
- value
110
- ]);
111
- }, []);
112
- return factorAry.join(",");
113
- }
114
- },
115
- {
116
- key: "queryFactor",
117
- value: function queryFactor(context, data) {
118
- var _data_includes, _data_matches;
119
- var queryKeys = (_data_includes = data.includes) === null || _data_includes === void 0 ? void 0 : _data_includes.query;
120
- var queryMatches = (_data_matches = data.matches) === null || _data_matches === void 0 ? void 0 : _data_matches.query;
121
- if (!queryKeys || queryKeys.length === 0) {
122
- return null;
123
- }
124
- var requestQuery = context.query;
125
- var queryFactor2 = this.factor(queryKeys, requestQuery, queryMatches);
126
- return queryFactor2;
127
- }
128
- },
129
- {
130
- key: "headerFactor",
131
- value: function headerFactor(context, data) {
132
- var _data_includes, _data_matches;
133
- var headerKeys = (_data_includes = data.includes) === null || _data_includes === void 0 ? void 0 : _data_includes.header;
134
- var headerMatches = (_data_matches = data.matches) === null || _data_matches === void 0 ? void 0 : _data_matches.header;
135
- if (!headerKeys || headerKeys.length === 0) {
136
- return null;
137
- }
138
- var requestHeader = context.headers;
139
- var headerFactor2 = this.factor(headerKeys, requestHeader, headerMatches);
140
- return headerFactor2;
141
- }
142
- },
143
- {
144
- key: "best",
145
- value: function best(context, cacheKey, data) {
146
- var _this = this;
147
- return _async_to_generator(function() {
148
- var level, cacheHash;
149
- return _ts_generator(this, function(_state) {
150
- level = data.level;
151
- cacheHash = _this.find[fname(level)](context, cacheKey, data);
152
- if (!cacheHash) {
153
- return [
154
- 2,
155
- null
156
- ];
157
- }
158
- return [
159
- 2,
160
- data.caches.get(cacheHash)
161
- ];
162
- });
163
- })();
164
- }
165
- },
166
- {
167
- key: "createCacheContent",
168
- value: function createCacheContent(config, caches) {
169
- return {
170
- level: config.level,
171
- interval: config.interval,
172
- includes: config.includes || null,
173
- limit: config.staleLimit,
174
- matches: config.matches || null,
175
- caches
176
- };
177
- }
178
- },
179
- {
180
- key: "get",
181
- value: function get(context) {
182
- var _this = this;
183
- return _async_to_generator(function() {
184
- var cacheKey, data, dest, expireTime, limitTime, html, cacheHash, isStale, isGarbage;
185
- return _ts_generator(this, function(_state) {
186
- switch (_state.label) {
187
- case 0:
188
- cacheKey = _this.generateRequestKey(context);
189
- data = _this.cache.get(cacheKey);
190
- if (!data) {
191
- return [
192
- 2,
193
- null
194
- ];
195
- }
196
- return [
197
- 4,
198
- _this.best(context, cacheKey, data)
199
- ];
200
- case 1:
201
- dest = _state.sent();
202
- if (!dest) {
203
- return [
204
- 2,
205
- null
206
- ];
207
- }
208
- expireTime = dest.expireTime, limitTime = dest.limitTime, html = dest.html, cacheHash = dest.cacheHash;
209
- isStale = Date.now() - expireTime > 0;
210
- isGarbage = limitTime ? Date.now() - limitTime > 0 : false;
211
- return [
212
- 2,
213
- {
214
- content: html || "",
215
- contentType: mime.contentType("html"),
216
- isStale,
217
- isGarbage,
218
- hash: cacheHash
219
- }
220
- ];
221
- }
222
- });
223
- })();
224
- }
225
- },
226
- {
227
- key: "set",
228
- value: function set(context, html, cacheConfig) {
229
- var sync = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : false;
230
- var _this = this;
231
- return _async_to_generator(function() {
232
- var cacheKey, data, caches, cacheHash, cacheSyncOrAsync, doCache;
233
- return _ts_generator(this, function(_state) {
234
- switch (_state.label) {
235
- case 0:
236
- if (!cacheConfig) {
237
- return [
238
- 2,
239
- false
240
- ];
241
- }
242
- cacheKey = _this.generateRequestKey(context);
243
- data = _this.cache.get(cacheKey);
244
- if (!!data)
245
- return [
246
- 3,
247
- 2
248
- ];
249
- return [
250
- 4,
251
- createPageCaches(MAX_CACHE_EACH_REQ)
252
- ];
253
- case 1:
254
- caches = _state.sent();
255
- data = _this.createCacheContent(cacheConfig, caches);
256
- _state.label = 2;
257
- case 2:
258
- cacheHash = _this.find[fname(cacheConfig.level)](context, cacheKey, data);
259
- if (!cacheHash) {
260
- return [
261
- 2,
262
- false
263
- ];
264
- }
265
- cacheSyncOrAsync = function() {
266
- var _ref = _async_to_generator(function() {
267
- var next, limit, storeHTML, size;
268
- return _ts_generator(this, function(_state2) {
269
- switch (_state2.label) {
270
- case 0:
271
- next = data;
272
- limit = cacheConfig.staleLimit;
273
- storeHTML = cacheAddition(html, cacheHash);
274
- size = storeHTML.length;
275
- return [
276
- 4,
277
- next.caches.set(cacheHash, {
278
- expireTime: Date.now() + cacheConfig.interval * 1e3,
279
- limitTime: typeof limit === "number" ? Date.now() + limit * 1e3 : null,
280
- cacheHash,
281
- html: storeHTML,
282
- size
283
- })
284
- ];
285
- case 1:
286
- _state2.sent();
287
- _this.cache.set(cacheKey, next);
288
- return [
289
- 2,
290
- true
291
- ];
292
- }
293
- });
294
- });
295
- return function cacheSyncOrAsync2() {
296
- return _ref.apply(this, arguments);
297
- };
298
- }();
299
- doCache = withCoalescedInvoke(cacheSyncOrAsync).bind(null, namespaceHash("stream", cacheHash), []);
300
- return [
301
- 2,
302
- maybeSync(doCache)(sync)
303
- ];
304
- }
305
- });
306
- })();
307
- }
308
- },
309
- {
310
- key: "del",
311
- value: function del(context, cacheHash) {
312
- var _this = this;
313
- return _async_to_generator(function() {
314
- var cacheKey, data;
315
- return _ts_generator(this, function(_state) {
316
- cacheKey = _this.generateRequestKey(context);
317
- data = _this.cache.get(cacheKey);
318
- data === null || data === void 0 ? void 0 : data.caches.del(cacheHash);
319
- return [
320
- 2
321
- ];
322
- });
323
- })();
324
- }
325
- }
326
- ]);
327
- return CacheManager2;
328
- }();
329
- var manager;
330
- function createCache() {
331
- if (manager) {
332
- return manager;
333
- }
334
- manager = new CacheManager({
335
- max: 0
336
- });
337
- return manager;
338
- }
339
- function destroyCache() {
340
- manager = null;
341
- }
342
- export {
343
- createCache,
344
- destroyCache
345
- };
File without changes
@@ -1,105 +0,0 @@
1
- import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
- import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
3
- import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
4
- import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
5
- import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
6
- import url from "url";
7
- function namespaceHash(namespace, hash) {
8
- return "".concat(namespace, "/").concat(hash);
9
- }
10
- function fname(lv) {
11
- return "f".concat(lv);
12
- }
13
- function connectFactor() {
14
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
15
- args[_key] = arguments[_key];
16
- }
17
- return args.join("-");
18
- }
19
- function valueFactory(obj) {
20
- if (_instanceof(obj, url.URLSearchParams)) {
21
- return function(key) {
22
- return obj.get(key);
23
- };
24
- } else {
25
- return function(key) {
26
- var value = obj[key];
27
- if (Array.isArray(value)) {
28
- return value.join(",");
29
- }
30
- return value;
31
- };
32
- }
33
- }
34
- function getTime(param) {
35
- var _param = _sliced_to_array(param, 2), s = _param[0], ns = _param[1];
36
- return Math.floor(s * 1e3 + ns / 1e6);
37
- }
38
- var RE_START_IN_HEAD = /<head>/;
39
- function cacheAddition(html, hash) {
40
- var additionHtml = html.replace(RE_START_IN_HEAD, '<head><meta name="x-moden-spr" content="'.concat(hash, '">'));
41
- return additionHtml;
42
- }
43
- var globalInvokeCache = /* @__PURE__ */ new Map();
44
- function withCoalescedInvoke(func) {
45
- return function() {
46
- var _ref = _async_to_generator(function(key, args) {
47
- var entry, future;
48
- function __wrapper() {
49
- return func.apply(void 0, _to_consumable_array(args));
50
- }
51
- return _ts_generator(this, function(_state) {
52
- entry = globalInvokeCache.get(key);
53
- if (entry) {
54
- return [
55
- 2,
56
- entry.then(function(res) {
57
- return {
58
- isOrigin: false,
59
- value: res.value
60
- };
61
- })
62
- ];
63
- }
64
- future = __wrapper().then(function(res) {
65
- globalInvokeCache.delete(key);
66
- return {
67
- isOrigin: true,
68
- value: res
69
- };
70
- }).catch(function(err) {
71
- globalInvokeCache.delete(key);
72
- throw err;
73
- });
74
- globalInvokeCache.set(key, future);
75
- return [
76
- 2,
77
- future
78
- ];
79
- });
80
- });
81
- return function(key, args) {
82
- return _ref.apply(this, arguments);
83
- };
84
- }();
85
- }
86
- function maybeSync(fn) {
87
- return function(sync) {
88
- if (sync) {
89
- return fn();
90
- } else {
91
- fn();
92
- return Promise.resolve();
93
- }
94
- };
95
- }
96
- export {
97
- cacheAddition,
98
- connectFactor,
99
- fname,
100
- getTime,
101
- maybeSync,
102
- namespaceHash,
103
- valueFactory,
104
- withCoalescedInvoke
105
- };
@@ -1,196 +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 _create_class } from "@swc/helpers/_/_create_class";
4
- import { _ as _define_property } from "@swc/helpers/_/_define_property";
5
- import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
6
- import { fs as extraFS } from "@modern-js/utils";
7
- import LRU from "lru-cache";
8
- var Byte = 1;
9
- var KB = 1024 * Byte;
10
- var MB = 1024 * KB;
11
- var getContentLength = function(cache) {
12
- return cache.content.length;
13
- };
14
- var createCacheItem = function() {
15
- var _ref = _async_to_generator(function(content, mtime) {
16
- return _ts_generator(this, function(_state) {
17
- return [
18
- 2,
19
- {
20
- content,
21
- mtime
22
- }
23
- ];
24
- });
25
- });
26
- return function createCacheItem2(content, mtime) {
27
- return _ref.apply(this, arguments);
28
- };
29
- }();
30
- var LruReader = /* @__PURE__ */ function() {
31
- "use strict";
32
- function LruReader2() {
33
- _class_call_check(this, LruReader2);
34
- _define_property(this, "cache", void 0);
35
- _define_property(this, "fs", void 0);
36
- this.cache = new LRU({
37
- max: 256 * MB,
38
- length: getContentLength,
39
- maxAge: 5 * 60 * 5e3
40
- });
41
- this.fs = extraFS;
42
- }
43
- _create_class(LruReader2, [
44
- {
45
- key: "init",
46
- value: function init2(fs) {
47
- this.fs = fs || extraFS;
48
- }
49
- },
50
- {
51
- key: "close",
52
- value: function close2() {
53
- }
54
- },
55
- {
56
- key: "read",
57
- value: function read(filepath) {
58
- var _this = this;
59
- return _async_to_generator(function() {
60
- var fs, content, stat, content1, item;
61
- return _ts_generator(this, function(_state) {
62
- switch (_state.label) {
63
- case 0:
64
- fs = _this.fs;
65
- if (_this.cache.has(filepath)) {
66
- content = _this.cache.get(filepath).content;
67
- return [
68
- 2,
69
- {
70
- content
71
- }
72
- ];
73
- }
74
- if (!fs.existsSync(filepath)) {
75
- return [
76
- 2,
77
- null
78
- ];
79
- }
80
- stat = fs.statSync(filepath);
81
- if (stat.isDirectory()) {
82
- return [
83
- 2,
84
- null
85
- ];
86
- }
87
- if (stat.size > 20 * MB) {
88
- return [
89
- 2,
90
- null
91
- ];
92
- }
93
- return [
94
- 4,
95
- fs.promises.readFile(filepath)
96
- ];
97
- case 1:
98
- content1 = _state.sent();
99
- return [
100
- 4,
101
- createCacheItem(content1, stat.mtime)
102
- ];
103
- case 2:
104
- item = _state.sent();
105
- _this.cache.set(filepath, item);
106
- return [
107
- 2,
108
- item
109
- ];
110
- }
111
- });
112
- })();
113
- }
114
- },
115
- {
116
- key: "update",
117
- value: function update() {
118
- var _this = this, cache = _this.cache, fs = _this.fs;
119
- var files = cache.keys();
120
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
121
- try {
122
- for (var _iterator = files[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
123
- var filepath = _step.value;
124
- if (!fs.existsSync(filepath)) {
125
- cache.del(filepath);
126
- }
127
- try {
128
- var item = cache.get(filepath);
129
- var stat = fs.statSync(filepath);
130
- var mtime = stat.mtime;
131
- if (item.mtime < mtime) {
132
- cache.del(filepath);
133
- }
134
- } catch (e) {
135
- cache.del(filepath);
136
- }
137
- }
138
- } catch (err) {
139
- _didIteratorError = true;
140
- _iteratorError = err;
141
- } finally {
142
- try {
143
- if (!_iteratorNormalCompletion && _iterator.return != null) {
144
- _iterator.return();
145
- }
146
- } finally {
147
- if (_didIteratorError) {
148
- throw _iteratorError;
149
- }
150
- }
151
- }
152
- }
153
- }
154
- ]);
155
- return LruReader2;
156
- }();
157
- var reader = new LruReader();
158
- var readFile = function() {
159
- var _ref = _async_to_generator(function(filepath) {
160
- var file;
161
- return _ts_generator(this, function(_state) {
162
- switch (_state.label) {
163
- case 0:
164
- return [
165
- 4,
166
- reader.read(filepath)
167
- ];
168
- case 1:
169
- file = _state.sent();
170
- return [
171
- 2,
172
- file === null || file === void 0 ? void 0 : file.content
173
- ];
174
- }
175
- });
176
- });
177
- return function readFile2(filepath) {
178
- return _ref.apply(this, arguments);
179
- };
180
- }();
181
- var updateFile = function() {
182
- reader.update();
183
- };
184
- var init = function(fs) {
185
- reader.init(fs);
186
- };
187
- var close = function() {
188
- reader.close();
189
- };
190
- export {
191
- LruReader,
192
- close,
193
- init,
194
- readFile,
195
- updateFile
196
- };