@modern-js/prod-server 2.0.0-beta.2 → 2.0.0-beta.4

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 (130) hide show
  1. package/CHANGELOG.md +132 -0
  2. package/dist/js/modern/constants.js +32 -25
  3. package/dist/js/modern/index.js +11 -6
  4. package/dist/js/modern/libs/context/context.js +52 -84
  5. package/dist/js/modern/libs/context/index.js +5 -2
  6. package/dist/js/modern/libs/hook-api/index.js +42 -35
  7. package/dist/js/modern/libs/hook-api/route.js +6 -7
  8. package/dist/js/modern/libs/hook-api/template.js +20 -34
  9. package/dist/js/modern/libs/loadConfig.js +45 -24
  10. package/dist/js/modern/libs/metrics.js +3 -4
  11. package/dist/js/modern/libs/proxy.js +68 -37
  12. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +112 -67
  13. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +246 -216
  14. package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +43 -49
  15. package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +36 -34
  16. package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +83 -113
  17. package/dist/js/modern/libs/render/cache/index.js +88 -54
  18. package/dist/js/modern/libs/render/cache/page-caches/index.js +31 -8
  19. package/dist/js/modern/libs/render/cache/page-caches/lru.js +6 -6
  20. package/dist/js/modern/libs/render/cache/spr.js +133 -117
  21. package/dist/js/modern/libs/render/cache/type.js +0 -1
  22. package/dist/js/modern/libs/render/cache/util.js +71 -39
  23. package/dist/js/modern/libs/render/index.js +76 -56
  24. package/dist/js/modern/libs/render/measure.js +38 -27
  25. package/dist/js/modern/libs/render/reader.js +65 -62
  26. package/dist/js/modern/libs/render/ssr.js +50 -32
  27. package/dist/js/modern/libs/render/static.js +50 -33
  28. package/dist/js/modern/libs/render/type.js +9 -6
  29. package/dist/js/modern/libs/route/index.js +8 -15
  30. package/dist/js/modern/libs/route/matcher.js +20 -34
  31. package/dist/js/modern/libs/route/route.js +9 -18
  32. package/dist/js/modern/libs/serve-file.js +33 -20
  33. package/dist/js/modern/server/index.js +144 -145
  34. package/dist/js/modern/server/modern-server-split.js +46 -12
  35. package/dist/js/modern/server/modern-server.js +377 -419
  36. package/dist/js/modern/type.js +0 -1
  37. package/dist/js/modern/utils.js +62 -43
  38. package/dist/js/modern/worker-server.js +34 -14
  39. package/dist/js/node/constants.js +45 -30
  40. package/dist/js/node/index.js +31 -57
  41. package/dist/js/node/libs/context/context.js +79 -94
  42. package/dist/js/node/libs/context/index.js +23 -13
  43. package/dist/js/node/libs/hook-api/index.js +69 -48
  44. package/dist/js/node/libs/hook-api/route.js +22 -11
  45. package/dist/js/node/libs/hook-api/template.js +36 -39
  46. package/dist/js/node/libs/loadConfig.js +69 -35
  47. package/dist/js/node/libs/metrics.js +21 -9
  48. package/dist/js/node/libs/proxy.js +86 -44
  49. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +132 -70
  50. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +268 -218
  51. package/dist/js/node/libs/render/cache/__tests__/cacheable.js +61 -55
  52. package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +54 -40
  53. package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +101 -119
  54. package/dist/js/node/libs/render/cache/index.js +110 -64
  55. package/dist/js/node/libs/render/cache/page-caches/index.js +50 -14
  56. package/dist/js/node/libs/render/cache/page-caches/lru.js +29 -12
  57. package/dist/js/node/libs/render/cache/spr.js +156 -129
  58. package/dist/js/node/libs/render/cache/type.js +0 -5
  59. package/dist/js/node/libs/render/cache/util.js +88 -45
  60. package/dist/js/node/libs/render/index.js +102 -67
  61. package/dist/js/node/libs/render/measure.js +55 -31
  62. package/dist/js/node/libs/render/reader.js +87 -70
  63. package/dist/js/node/libs/render/ssr.js +76 -47
  64. package/dist/js/node/libs/render/static.js +75 -40
  65. package/dist/js/node/libs/render/type.js +27 -12
  66. package/dist/js/node/libs/route/index.js +26 -26
  67. package/dist/js/node/libs/route/matcher.js +36 -41
  68. package/dist/js/node/libs/route/route.js +25 -22
  69. package/dist/js/node/libs/serve-file.js +61 -32
  70. package/dist/js/node/server/index.js +160 -160
  71. package/dist/js/node/server/modern-server-split.js +68 -22
  72. package/dist/js/node/server/modern-server.js +395 -443
  73. package/dist/js/node/type.js +0 -3
  74. package/dist/js/node/utils.js +74 -52
  75. package/dist/js/node/worker-server.js +53 -21
  76. package/dist/js/treeshaking/constants.js +26 -25
  77. package/dist/js/treeshaking/index.js +10 -10
  78. package/dist/js/treeshaking/libs/context/context.js +268 -237
  79. package/dist/js/treeshaking/libs/context/index.js +3 -3
  80. package/dist/js/treeshaking/libs/hook-api/index.js +265 -143
  81. package/dist/js/treeshaking/libs/hook-api/route.js +65 -30
  82. package/dist/js/treeshaking/libs/hook-api/template.js +121 -85
  83. package/dist/js/treeshaking/libs/loadConfig.js +80 -37
  84. package/dist/js/treeshaking/libs/metrics.js +4 -10
  85. package/dist/js/treeshaking/libs/proxy.js +240 -76
  86. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +288 -121
  87. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +772 -455
  88. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +65 -51
  89. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -35
  90. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +144 -118
  91. package/dist/js/treeshaking/libs/render/cache/index.js +337 -175
  92. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +151 -27
  93. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +80 -42
  94. package/dist/js/treeshaking/libs/render/cache/spr.js +470 -340
  95. package/dist/js/treeshaking/libs/render/cache/type.js +1 -1
  96. package/dist/js/treeshaking/libs/render/cache/util.js +271 -92
  97. package/dist/js/treeshaking/libs/render/index.js +228 -95
  98. package/dist/js/treeshaking/libs/render/measure.js +142 -57
  99. package/dist/js/treeshaking/libs/render/reader.js +325 -177
  100. package/dist/js/treeshaking/libs/render/ssr.js +220 -95
  101. package/dist/js/treeshaking/libs/render/static.js +210 -78
  102. package/dist/js/treeshaking/libs/render/type.js +7 -6
  103. package/dist/js/treeshaking/libs/route/index.js +125 -89
  104. package/dist/js/treeshaking/libs/route/matcher.js +132 -107
  105. package/dist/js/treeshaking/libs/route/route.js +40 -26
  106. package/dist/js/treeshaking/libs/serve-file.js +177 -68
  107. package/dist/js/treeshaking/server/index.js +468 -327
  108. package/dist/js/treeshaking/server/modern-server-split.js +352 -144
  109. package/dist/js/treeshaking/server/modern-server.js +1046 -911
  110. package/dist/js/treeshaking/type.js +1 -1
  111. package/dist/js/treeshaking/utils.js +138 -81
  112. package/dist/js/treeshaking/worker-server.js +176 -55
  113. package/dist/types/index.d.ts +2 -0
  114. package/dist/types/libs/context/context.d.ts +4 -1
  115. package/dist/types/libs/loadConfig.d.ts +1 -0
  116. package/dist/types/libs/render/cache/index.d.ts +2 -0
  117. package/dist/types/libs/render/cache/spr.d.ts +2 -0
  118. package/dist/types/libs/route/route.d.ts +0 -1
  119. package/dist/types/server/index.d.ts +3 -0
  120. package/dist/types/utils.d.ts +1 -1
  121. package/dist/types/worker-server.d.ts +1 -2
  122. package/package.json +7 -14
  123. package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
  124. package/dist/js/modern/libs/render/modern/index.js +0 -37
  125. package/dist/js/node/libs/render/modern/browser-list.js +0 -14
  126. package/dist/js/node/libs/render/modern/index.js +0 -46
  127. package/dist/js/treeshaking/libs/render/modern/browser-list.js +0 -7
  128. package/dist/js/treeshaking/libs/render/modern/index.js +0 -39
  129. package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
  130. package/dist/types/libs/render/modern/index.d.ts +0 -3
@@ -1,226 +1,276 @@
1
- "use strict";
2
-
3
- var _spr = require("../spr");
4
- var _errorConfiguration = require("./error-configuration");
5
- var _cacheable = require("./cacheable");
6
- var _matchedCache = require("./matched-cache");
7
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
8
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
9
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
10
- const createCacheConfig = (config = {}) => _objectSpread({
11
- excludes: null,
12
- includes: null,
13
- interval: 10,
14
- staleLimit: false,
15
- level: 0,
16
- fallback: false,
17
- matches: null
18
- }, config);
19
- jest.setTimeout(60000);
20
- describe('cache', () => {
21
- it('should cache correctly', async () => {
22
- (0, _spr.destroyCache)();
23
- const cache = (0, _spr.createCache)();
24
- const context = {
25
- entry: '',
26
- pathname: '',
27
- query: {},
28
- headers: {}
29
- };
30
- const content = 'hello';
31
- const cacheConfig = createCacheConfig();
32
- await cache.set(context, content, cacheConfig, true);
33
- const cacheResult = await cache.get(context);
34
- expect(cacheResult).not.toBe(null);
35
- expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe('hello');
36
- });
37
- it('should ignore cache set when cache config not exist', async () => {
38
- (0, _spr.destroyCache)();
39
- (0, _spr.destroyCache)();
40
- const cache = (0, _spr.createCache)();
41
- const context = {
42
- entry: '',
43
- pathname: '',
44
- query: {},
45
- headers: {}
46
- };
47
- const content = 'hello';
48
- const shouldCache = await cache.set(context, content, null, true);
49
- expect(shouldCache).toBe(false);
50
- });
51
- it('should calcual cache key error', async () => {
52
- (0, _spr.destroyCache)();
53
- const cache = (0, _spr.createCache)();
54
- const content = 'hello';
55
- for (const config of _errorConfiguration.errorConfiguration) {
56
- const cacheConfig = createCacheConfig(config);
57
- const tmpEntry = Math.random().toString();
58
- const context = {
59
- entry: tmpEntry,
60
- pathname: '',
61
- query: {},
62
- headers: {}
63
- };
64
- const shouldCache = await cache.set(context, content, cacheConfig);
65
- expect(shouldCache).toBe(false);
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var stdin_exports = {};
19
+ __export(stdin_exports, {
20
+ default: () => stdin_default
21
+ });
22
+ module.exports = __toCommonJS(stdin_exports);
23
+ var import_spr = require("../spr");
24
+ var import_error_configuration = require("./error-configuration");
25
+ var import_cacheable = require("./cacheable");
26
+ var import_matched_cache = require("./matched-cache");
27
+ var __defProp2 = Object.defineProperty;
28
+ var __getOwnPropNames2 = Object.getOwnPropertyNames;
29
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
30
+ var __hasOwnProp2 = Object.prototype.hasOwnProperty;
31
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
32
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
33
+ var __spreadValues = (a, b) => {
34
+ for (var prop in b || (b = {}))
35
+ if (__hasOwnProp2.call(b, prop))
36
+ __defNormalProp(a, prop, b[prop]);
37
+ if (__getOwnPropSymbols)
38
+ for (var prop of __getOwnPropSymbols(b)) {
39
+ if (__propIsEnum.call(b, prop))
40
+ __defNormalProp(a, prop, b[prop]);
66
41
  }
67
- });
68
- it('should get nothing for diff requestKey', async () => {
69
- (0, _spr.destroyCache)();
70
- const cache = (0, _spr.createCache)();
71
- const context = {
72
- entry: '',
73
- pathname: '',
74
- query: {},
75
- headers: {}
76
- };
77
- const content = 'hello';
78
- const cacheConfig = createCacheConfig({
79
- level: 1,
80
- includes: {
81
- query: ['name']
42
+ return a;
43
+ };
44
+ var __commonJS = (cb, mod) => function __require() {
45
+ return mod || (0, cb[__getOwnPropNames2(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
46
+ };
47
+ var __async = (__this, __arguments, generator) => {
48
+ return new Promise((resolve, reject) => {
49
+ var fulfilled = (value) => {
50
+ try {
51
+ step(generator.next(value));
52
+ } catch (e) {
53
+ reject(e);
82
54
  }
83
- });
84
- await cache.set(context, content, cacheConfig, true);
85
- const context_req = {
86
- entry: '',
87
- pathname: '/home',
88
- query: {},
89
- headers: {}
90
55
  };
91
- const cacheResult = await cache.get(context_req);
92
- expect(cacheResult).toBe(null);
93
- });
94
- it('should get nothing for diff cacheHash', async () => {
95
- (0, _spr.destroyCache)();
96
- const cache = (0, _spr.createCache)();
97
- const context = {
98
- entry: '',
99
- pathname: '',
100
- query: {},
101
- headers: {}
102
- };
103
- const content = 'hello';
104
- const cacheConfig = createCacheConfig({
105
- level: 1,
106
- includes: {
107
- query: ['name']
56
+ var rejected = (value) => {
57
+ try {
58
+ step(generator.throw(value));
59
+ } catch (e) {
60
+ reject(e);
108
61
  }
109
- });
110
- await cache.set(context, content, cacheConfig, true);
111
- const context_req = {
112
- entry: '',
113
- pathname: '',
114
- query: {
115
- name: 'zll'
116
- },
117
- headers: {}
118
62
  };
119
- const cacheResult = await cache.get(context_req);
120
- expect(cacheResult).toBe(null);
121
- });
122
- it('should get cache correctly', async () => {
123
- (0, _spr.destroyCache)();
124
- const cache = (0, _spr.createCache)();
125
- for (const cacheable of _cacheable.cacheabelAry) {
126
- const context = {
127
- entry: '',
128
- pathname: cacheable.requestOpt.url,
129
- query: cacheable.requestOpt.query || {},
130
- headers: cacheable.requestOpt.headers || {}
131
- };
132
- const cacheConfig = createCacheConfig(cacheable.cacheConfig || {});
133
- await cache.set(context, cacheable.content, cacheConfig, true);
134
- const cacheResult = await cache.get(context);
135
- expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe(cacheable.content);
136
- }
63
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
64
+ step((generator = generator.apply(__this, __arguments)).next());
137
65
  });
138
- it('should match cache correctly', async () => {
139
- (0, _spr.destroyCache)();
140
- const cache = (0, _spr.createCache)();
141
- for (const cacheable of _matchedCache.matchedCacheableAry) {
142
- const [baseCacheable, matchOne, ...other] = cacheable;
143
- const {
144
- requestOpt = {},
145
- cacheConfig,
146
- content
147
- } = baseCacheable;
148
- const context = {
149
- entry: '',
150
- pathname: requestOpt.url,
151
- query: requestOpt.query,
152
- headers: requestOpt.headers
153
- };
154
- await cache.set(context, content, createCacheConfig(cacheConfig), true);
155
- const matchContext = {
156
- entry: '',
157
- pathname: matchOne.url,
158
- query: matchOne.query,
159
- headers: matchOne.headers
160
- };
161
- const cacheResult = await cache.get(matchContext);
162
- expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe(content);
163
- for (const notMatch of other) {
164
- const notMatchContext = {
165
- entry: '',
166
- pathname: notMatch.url,
167
- query: notMatch.query,
168
- headers: notMatch.headers
66
+ };
67
+ var require_cache_test = __commonJS({
68
+ "src/libs/render/cache/__tests__/cache.test.ts"(exports) {
69
+ const createCacheConfig = (config = {}) => __spreadValues({
70
+ excludes: null,
71
+ includes: null,
72
+ interval: 10,
73
+ staleLimit: false,
74
+ level: 0,
75
+ fallback: false,
76
+ matches: null
77
+ }, config);
78
+ jest.setTimeout(6e4);
79
+ describe("cache", () => {
80
+ it("should cache correctly", () => __async(exports, null, function* () {
81
+ (0, import_spr.destroyCache)();
82
+ const cache = (0, import_spr.createCache)();
83
+ const context = {
84
+ entry: "",
85
+ pathname: "",
86
+ query: {},
87
+ headers: {}
169
88
  };
170
- const nothing = await cache.get(notMatchContext);
171
- expect(nothing).toBe(null);
172
- }
173
- }
174
- });
175
- it('should stale cache correctly', async () => {
176
- (0, _spr.destroyCache)();
177
- const cache = (0, _spr.createCache)();
178
- const context = {
179
- entry: '',
180
- pathname: '',
181
- query: {},
182
- headers: {}
183
- };
184
- const config = createCacheConfig({
185
- interval: 5
186
- });
187
- const content = 'hello';
188
- const shouldCache = await cache.set(context, content, config, true);
189
- expect(shouldCache.value).toBe(true);
190
- const freshResult = await cache.get(context);
191
- expect(freshResult === null || freshResult === void 0 ? void 0 : freshResult.isStale).toBe(false);
192
- await new Promise(resolve => {
193
- setTimeout(() => {
194
- resolve();
195
- }, 6000);
196
- });
197
- const staleResult = await cache.get(context);
198
- expect(staleResult === null || staleResult === void 0 ? void 0 : staleResult.isStale).toBe(true);
199
- });
200
- it('should garbage cache correctly', async () => {
201
- (0, _spr.destroyCache)();
202
- const cache = (0, _spr.createCache)();
203
- const context = {
204
- entry: '',
205
- pathname: '',
206
- query: {},
207
- headers: {}
208
- };
209
- const config = createCacheConfig({
210
- interval: 3,
211
- staleLimit: 8
212
- });
213
- const content = 'hello';
214
- const shouldCache = await cache.set(context, content, config, true);
215
- expect(shouldCache.value).toBe(true);
216
- const freshResult = await cache.get(context);
217
- expect(freshResult === null || freshResult === void 0 ? void 0 : freshResult.isGarbage).toBe(false);
218
- await new Promise(resolve => {
219
- setTimeout(() => {
220
- resolve();
221
- }, 10000);
89
+ const content = "hello";
90
+ const cacheConfig = createCacheConfig();
91
+ yield cache.set(context, content, cacheConfig, true);
92
+ const cacheResult = yield cache.get(context);
93
+ expect(cacheResult).not.toBe(null);
94
+ expect(cacheResult == null ? void 0 : cacheResult.content).toBe("hello");
95
+ }));
96
+ it("should ignore cache set when cache config not exist", () => __async(exports, null, function* () {
97
+ (0, import_spr.destroyCache)();
98
+ (0, import_spr.destroyCache)();
99
+ const cache = (0, import_spr.createCache)();
100
+ const context = {
101
+ entry: "",
102
+ pathname: "",
103
+ query: {},
104
+ headers: {}
105
+ };
106
+ const content = "hello";
107
+ const shouldCache = yield cache.set(context, content, null, true);
108
+ expect(shouldCache).toBe(false);
109
+ }));
110
+ it("should calcual cache key error", () => __async(exports, null, function* () {
111
+ (0, import_spr.destroyCache)();
112
+ const cache = (0, import_spr.createCache)();
113
+ const content = "hello";
114
+ for (const config of import_error_configuration.errorConfiguration) {
115
+ const cacheConfig = createCacheConfig(config);
116
+ const tmpEntry = Math.random().toString();
117
+ const context = {
118
+ entry: tmpEntry,
119
+ pathname: "",
120
+ query: {},
121
+ headers: {}
122
+ };
123
+ const shouldCache = yield cache.set(context, content, cacheConfig);
124
+ expect(shouldCache).toBe(false);
125
+ }
126
+ }));
127
+ it("should get nothing for diff requestKey", () => __async(exports, null, function* () {
128
+ (0, import_spr.destroyCache)();
129
+ const cache = (0, import_spr.createCache)();
130
+ const context = {
131
+ entry: "",
132
+ pathname: "",
133
+ query: {},
134
+ headers: {}
135
+ };
136
+ const content = "hello";
137
+ const cacheConfig = createCacheConfig({
138
+ level: 1,
139
+ includes: { query: ["name"] }
140
+ });
141
+ yield cache.set(context, content, cacheConfig, true);
142
+ const context_req = {
143
+ entry: "",
144
+ pathname: "/home",
145
+ query: {},
146
+ headers: {}
147
+ };
148
+ const cacheResult = yield cache.get(context_req);
149
+ expect(cacheResult).toBe(null);
150
+ }));
151
+ it("should get nothing for diff cacheHash", () => __async(exports, null, function* () {
152
+ (0, import_spr.destroyCache)();
153
+ const cache = (0, import_spr.createCache)();
154
+ const context = {
155
+ entry: "",
156
+ pathname: "",
157
+ query: {},
158
+ headers: {}
159
+ };
160
+ const content = "hello";
161
+ const cacheConfig = createCacheConfig({
162
+ level: 1,
163
+ includes: { query: ["name"] }
164
+ });
165
+ yield cache.set(context, content, cacheConfig, true);
166
+ const context_req = {
167
+ entry: "",
168
+ pathname: "",
169
+ query: { name: "zll" },
170
+ headers: {}
171
+ };
172
+ const cacheResult = yield cache.get(context_req);
173
+ expect(cacheResult).toBe(null);
174
+ }));
175
+ it("should get cache correctly", () => __async(exports, null, function* () {
176
+ (0, import_spr.destroyCache)();
177
+ const cache = (0, import_spr.createCache)();
178
+ for (const cacheable of import_cacheable.cacheabelAry) {
179
+ const context = {
180
+ entry: "",
181
+ pathname: cacheable.requestOpt.url,
182
+ query: cacheable.requestOpt.query || {},
183
+ headers: cacheable.requestOpt.headers || {}
184
+ };
185
+ const cacheConfig = createCacheConfig(cacheable.cacheConfig || {});
186
+ yield cache.set(context, cacheable.content, cacheConfig, true);
187
+ const cacheResult = yield cache.get(context);
188
+ expect(cacheResult == null ? void 0 : cacheResult.content).toBe(cacheable.content);
189
+ }
190
+ }));
191
+ it("should match cache correctly", () => __async(exports, null, function* () {
192
+ (0, import_spr.destroyCache)();
193
+ const cache = (0, import_spr.createCache)();
194
+ for (const cacheable of import_matched_cache.matchedCacheableAry) {
195
+ const [baseCacheable, matchOne, ...other] = cacheable;
196
+ const { requestOpt = {}, cacheConfig, content } = baseCacheable;
197
+ const context = {
198
+ entry: "",
199
+ pathname: requestOpt.url,
200
+ query: requestOpt.query,
201
+ headers: requestOpt.headers
202
+ };
203
+ yield cache.set(context, content, createCacheConfig(cacheConfig), true);
204
+ const matchContext = {
205
+ entry: "",
206
+ pathname: matchOne.url,
207
+ query: matchOne.query,
208
+ headers: matchOne.headers
209
+ };
210
+ const cacheResult = yield cache.get(matchContext);
211
+ expect(cacheResult == null ? void 0 : cacheResult.content).toBe(content);
212
+ for (const notMatch of other) {
213
+ const notMatchContext = {
214
+ entry: "",
215
+ pathname: notMatch.url,
216
+ query: notMatch.query,
217
+ headers: notMatch.headers
218
+ };
219
+ const nothing = yield cache.get(notMatchContext);
220
+ expect(nothing).toBe(null);
221
+ }
222
+ }
223
+ }));
224
+ it("should stale cache correctly", () => __async(exports, null, function* () {
225
+ (0, import_spr.destroyCache)();
226
+ const cache = (0, import_spr.createCache)();
227
+ const context = {
228
+ entry: "",
229
+ pathname: "",
230
+ query: {},
231
+ headers: {}
232
+ };
233
+ const config = createCacheConfig({ interval: 5 });
234
+ const content = "hello";
235
+ const shouldCache = yield cache.set(context, content, config, true);
236
+ expect(shouldCache.value).toBe(true);
237
+ const freshResult = yield cache.get(context);
238
+ expect(freshResult == null ? void 0 : freshResult.isStale).toBe(false);
239
+ yield new Promise((resolve) => {
240
+ setTimeout(() => {
241
+ resolve();
242
+ }, 6e3);
243
+ });
244
+ const staleResult = yield cache.get(context);
245
+ expect(staleResult == null ? void 0 : staleResult.isStale).toBe(true);
246
+ }));
247
+ it("should garbage cache correctly", () => __async(exports, null, function* () {
248
+ (0, import_spr.destroyCache)();
249
+ const cache = (0, import_spr.createCache)();
250
+ const context = {
251
+ entry: "",
252
+ pathname: "",
253
+ query: {},
254
+ headers: {}
255
+ };
256
+ const config = createCacheConfig({
257
+ interval: 3,
258
+ staleLimit: 8
259
+ });
260
+ const content = "hello";
261
+ const shouldCache = yield cache.set(context, content, config, true);
262
+ expect(shouldCache.value).toBe(true);
263
+ const freshResult = yield cache.get(context);
264
+ expect(freshResult == null ? void 0 : freshResult.isGarbage).toBe(false);
265
+ yield new Promise((resolve) => {
266
+ setTimeout(() => {
267
+ resolve();
268
+ }, 1e4);
269
+ });
270
+ const staleResult = yield cache.get(context);
271
+ expect(staleResult == null ? void 0 : staleResult.isGarbage).toBe(true);
272
+ }));
222
273
  });
223
- const staleResult = await cache.get(context);
224
- expect(staleResult === null || staleResult === void 0 ? void 0 : staleResult.isGarbage).toBe(true);
225
- });
226
- });
274
+ }
275
+ });
276
+ var stdin_default = require_cache_test();
@@ -1,60 +1,66 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var stdin_exports = {};
19
+ __export(stdin_exports, {
20
+ cacheabelAry: () => cacheabelAry
5
21
  });
6
- exports.cacheabelAry = void 0;
7
- const cacheabelAry = [{
8
- requestOpt: {
9
- url: '/'
22
+ module.exports = __toCommonJS(stdin_exports);
23
+ const cacheabelAry = [
24
+ {
25
+ requestOpt: { url: "/" },
26
+ cacheConfig: {},
27
+ content: "level0"
10
28
  },
11
- cacheConfig: {},
12
- content: 'level0'
13
- }, {
14
- requestOpt: {
15
- url: '/level-one',
16
- query: {
17
- name: 'modern'
18
- }
19
- },
20
- cacheConfig: {
21
- level: 1,
22
- includes: {
23
- query: ['name']
24
- }
25
- },
26
- content: 'level1'
27
- }, {
28
- requestOpt: {
29
- url: '/level-two',
30
- headers: {
31
- age: '18'
32
- }
33
- },
34
- cacheConfig: {
35
- level: 2,
36
- includes: {
37
- header: ['age']
38
- }
39
- },
40
- content: 'level2'
41
- }, {
42
- requestOpt: {
43
- url: '/level-three',
44
- query: {
45
- name: 'modern'
29
+ {
30
+ requestOpt: {
31
+ url: "/level-one",
32
+ query: { name: "modern" }
33
+ },
34
+ cacheConfig: {
35
+ level: 1,
36
+ includes: { query: ["name"] }
46
37
  },
47
- headers: {
48
- age: '18'
49
- }
38
+ content: "level1"
50
39
  },
51
- cacheConfig: {
52
- level: 3,
53
- includes: {
54
- query: ['name'],
55
- header: ['age']
56
- }
40
+ {
41
+ requestOpt: {
42
+ url: "/level-two",
43
+ headers: { age: "18" }
44
+ },
45
+ cacheConfig: {
46
+ level: 2,
47
+ includes: { header: ["age"] }
48
+ },
49
+ content: "level2"
57
50
  },
58
- content: 'level3'
59
- }];
60
- exports.cacheabelAry = cacheabelAry;
51
+ {
52
+ requestOpt: {
53
+ url: "/level-three",
54
+ query: { name: "modern" },
55
+ headers: { age: "18" }
56
+ },
57
+ cacheConfig: {
58
+ level: 3,
59
+ includes: {
60
+ query: ["name"],
61
+ header: ["age"]
62
+ }
63
+ },
64
+ content: "level3"
65
+ }
66
+ ];