@modern-js/prod-server 1.22.1 → 2.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 (132) hide show
  1. package/CHANGELOG.md +271 -17
  2. package/dist/js/modern/constants.js +32 -27
  3. package/dist/js/modern/index.js +11 -7
  4. package/dist/js/modern/libs/context/context.js +54 -132
  5. package/dist/js/modern/libs/context/index.js +5 -2
  6. package/dist/js/modern/libs/hook-api/index.js +134 -0
  7. package/dist/js/modern/libs/hook-api/route.js +13 -37
  8. package/dist/js/modern/libs/hook-api/template.js +41 -32
  9. package/dist/js/modern/libs/loadConfig.js +46 -32
  10. package/dist/js/modern/libs/metrics.js +6 -7
  11. package/dist/js/modern/libs/proxy.js +70 -44
  12. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +112 -68
  13. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +246 -225
  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 +93 -50
  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 -16
  20. package/dist/js/modern/libs/render/cache/spr.js +133 -167
  21. package/dist/js/modern/libs/render/cache/type.js +0 -1
  22. package/dist/js/modern/libs/render/cache/util.js +71 -45
  23. package/dist/js/modern/libs/render/index.js +76 -61
  24. package/dist/js/modern/libs/render/measure.js +42 -34
  25. package/dist/js/modern/libs/render/reader.js +68 -76
  26. package/dist/js/modern/libs/render/ssr.js +63 -33
  27. package/dist/js/modern/libs/render/static.js +51 -37
  28. package/dist/js/modern/libs/render/type.js +9 -7
  29. package/dist/js/modern/libs/route/index.js +12 -30
  30. package/dist/js/modern/libs/route/matcher.js +28 -50
  31. package/dist/js/modern/libs/route/route.js +9 -31
  32. package/dist/js/modern/libs/serve-file.js +40 -20
  33. package/dist/js/modern/server/index.js +152 -168
  34. package/dist/js/modern/server/modern-server-split.js +44 -60
  35. package/dist/js/modern/server/modern-server.js +416 -526
  36. package/dist/js/modern/type.js +0 -1
  37. package/dist/js/modern/utils.js +62 -58
  38. package/dist/js/modern/worker-server.js +54 -0
  39. package/dist/js/node/constants.js +53 -32
  40. package/dist/js/node/index.js +37 -67
  41. package/dist/js/node/libs/context/context.js +84 -150
  42. package/dist/js/node/libs/context/index.js +28 -16
  43. package/dist/js/node/libs/hook-api/index.js +164 -0
  44. package/dist/js/node/libs/hook-api/route.js +35 -45
  45. package/dist/js/node/libs/hook-api/template.js +64 -40
  46. package/dist/js/node/libs/loadConfig.js +74 -55
  47. package/dist/js/node/libs/metrics.js +28 -12
  48. package/dist/js/node/libs/proxy.js +90 -55
  49. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +99 -61
  50. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +147 -140
  51. package/dist/js/node/libs/render/cache/__tests__/cacheable.js +65 -55
  52. package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +58 -40
  53. package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +105 -119
  54. package/dist/js/node/libs/render/cache/index.js +115 -65
  55. package/dist/js/node/libs/render/cache/page-caches/index.js +54 -16
  56. package/dist/js/node/libs/render/cache/page-caches/lru.js +33 -26
  57. package/dist/js/node/libs/render/cache/spr.js +161 -188
  58. package/dist/js/node/libs/render/cache/type.js +15 -5
  59. package/dist/js/node/libs/render/cache/util.js +99 -63
  60. package/dist/js/node/libs/render/index.js +106 -87
  61. package/dist/js/node/libs/render/measure.js +61 -44
  62. package/dist/js/node/libs/render/reader.js +98 -100
  63. package/dist/js/node/libs/render/ssr.js +92 -58
  64. package/dist/js/node/libs/render/static.js +80 -53
  65. package/dist/js/node/libs/render/type.js +31 -13
  66. package/dist/js/node/libs/route/index.js +35 -44
  67. package/dist/js/node/libs/route/matcher.js +48 -65
  68. package/dist/js/node/libs/route/route.js +29 -37
  69. package/dist/js/node/libs/serve-file.js +74 -37
  70. package/dist/js/node/server/index.js +170 -203
  71. package/dist/js/node/server/modern-server-split.js +70 -73
  72. package/dist/js/node/server/modern-server.js +432 -577
  73. package/dist/js/node/type.js +15 -3
  74. package/dist/js/node/utils.js +87 -93
  75. package/dist/js/node/worker-server.js +77 -0
  76. package/dist/js/treeshaking/constants.js +29 -0
  77. package/dist/js/treeshaking/index.js +13 -0
  78. package/dist/js/treeshaking/libs/context/context.js +274 -0
  79. package/dist/js/treeshaking/libs/context/index.js +5 -0
  80. package/dist/js/treeshaking/libs/hook-api/index.js +281 -0
  81. package/dist/js/treeshaking/libs/hook-api/route.js +68 -0
  82. package/dist/js/treeshaking/libs/hook-api/template.js +127 -0
  83. package/dist/js/treeshaking/libs/loadConfig.js +82 -0
  84. package/dist/js/treeshaking/libs/metrics.js +6 -0
  85. package/dist/js/treeshaking/libs/proxy.js +244 -0
  86. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +291 -0
  87. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +781 -0
  88. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +67 -0
  89. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -0
  90. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +147 -0
  91. package/dist/js/treeshaking/libs/render/cache/index.js +346 -0
  92. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +154 -0
  93. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +84 -0
  94. package/dist/js/treeshaking/libs/render/cache/spr.js +492 -0
  95. package/dist/js/treeshaking/libs/render/cache/type.js +1 -0
  96. package/dist/js/treeshaking/libs/render/cache/util.js +280 -0
  97. package/dist/js/treeshaking/libs/render/index.js +233 -0
  98. package/dist/js/treeshaking/libs/render/measure.js +146 -0
  99. package/dist/js/treeshaking/libs/render/reader.js +339 -0
  100. package/dist/js/treeshaking/libs/render/ssr.js +223 -0
  101. package/dist/js/treeshaking/libs/render/static.js +216 -0
  102. package/dist/js/treeshaking/libs/render/type.js +7 -0
  103. package/dist/js/treeshaking/libs/route/index.js +130 -0
  104. package/dist/js/treeshaking/libs/route/matcher.js +138 -0
  105. package/dist/js/treeshaking/libs/route/route.js +40 -0
  106. package/dist/js/treeshaking/libs/serve-file.js +184 -0
  107. package/dist/js/treeshaking/server/index.js +505 -0
  108. package/dist/js/treeshaking/server/modern-server-split.js +360 -0
  109. package/dist/js/treeshaking/server/modern-server.js +1083 -0
  110. package/dist/js/treeshaking/type.js +1 -0
  111. package/dist/js/treeshaking/utils.js +147 -0
  112. package/dist/js/treeshaking/worker-server.js +177 -0
  113. package/dist/types/libs/context/context.d.ts +1 -1
  114. package/dist/types/libs/hook-api/index.d.ts +5 -0
  115. package/dist/types/libs/hook-api/route.d.ts +9 -14
  116. package/dist/types/libs/hook-api/template.d.ts +19 -9
  117. package/dist/types/libs/render/cache/index.d.ts +4 -2
  118. package/dist/types/libs/render/type.d.ts +3 -1
  119. package/dist/types/libs/route/route.d.ts +0 -1
  120. package/dist/types/libs/serve-file.d.ts +2 -1
  121. package/dist/types/server/index.d.ts +2 -0
  122. package/dist/types/server/modern-server.d.ts +11 -11
  123. package/dist/types/type.d.ts +8 -10
  124. package/dist/types/utils.d.ts +3 -4
  125. package/dist/types/worker-server.d.ts +15 -0
  126. package/package.json +31 -45
  127. package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
  128. package/dist/js/modern/libs/render/modern/index.js +0 -48
  129. package/dist/js/node/libs/render/modern/browser-list.js +0 -14
  130. package/dist/js/node/libs/render/modern/index.js +0 -64
  131. package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
  132. package/dist/types/libs/render/modern/index.d.ts +0 -3
@@ -1,70 +1,114 @@
1
- import url from 'url';
2
- import { createCache, destroyCache } from "../spr";
3
- import { cacheAddition, connectFactor, fname, maybeSync, namespaceHash, valueFactory, withCoalescedInvoke } from "../util";
4
- describe('test spr util functions', () => {
5
- it('should return value correctly', () => {
6
- expect(connectFactor('bar', 'foo')).toBe('bar-foo');
7
- expect(fname(1)).toBe('f1');
8
- expect(namespaceHash('modern', '!@#$%^&')).toBe('modern/!@#$%^&');
9
- });
10
- it('should create or destroy instance correctly', () => {
11
- const ins1 = createCache();
12
- const ins2 = createCache();
13
- expect(ins1 === ins2).toBe(true);
14
- destroyCache();
15
- const ins3 = createCache();
16
- expect(ins1 === ins3).toBe(false);
17
- expect(ins2 === ins3).toBe(false);
18
- });
19
- it('should return function correctly', () => {
20
- const urlParams = (() => new url.URLSearchParams())();
21
-
22
- urlParams.set('name', 'modern');
23
- const getParam = valueFactory(urlParams);
24
- expect(getParam('name')).toBe('modern');
25
- const headers = {
26
- age: '12345'
1
+ var __getOwnPropNames = Object.getOwnPropertyNames;
2
+ var __commonJS = (cb, mod) => function __require() {
3
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
4
+ };
5
+ var __async = (__this, __arguments, generator) => {
6
+ return new Promise((resolve, reject) => {
7
+ var fulfilled = (value) => {
8
+ try {
9
+ step(generator.next(value));
10
+ } catch (e) {
11
+ reject(e);
12
+ }
27
13
  };
28
- const getHeader = valueFactory(headers);
29
- expect(getHeader('age')).toBe('12345');
30
- });
31
- it('should add target html content', () => {
32
- const contentNoHead = '<div>123</div>';
33
- const html = cacheAddition(contentNoHead, Math.random().toString());
34
- expect(html).toBe(contentNoHead);
35
- const contentWithHead = '<head></head><div>123</div>';
36
- const hash = Math.random().toString();
37
- const htmlWithHead = cacheAddition(contentWithHead, hash);
38
- expect(htmlWithHead).toBe(`<head><meta name="x-moden-spr" content="${hash}"></head><div>123</div>`);
39
- });
40
- it('should only invoke func one time', async () => {
41
- let index = 0;
42
- const fn = withCoalescedInvoke(async () => new Promise(resolve => {
43
- setTimeout(() => {
44
- index += 1;
45
- resolve(index);
46
- }, 500);
47
- }));
48
- const key = 'test';
49
- const [res1, res2] = await Promise.all([fn(key, []), fn(key, [])]);
50
- expect(res1.isOrigin && res2.isOrigin).toBe(false);
51
- expect(res1.isOrigin || res2.isOrigin).toBe(true);
52
- expect(res1.value).toBe(1);
53
- expect(res2.value).toBe(1);
54
- });
55
- it('should invoke sync or async', async () => {
56
- const foo = '';
57
- const async = await maybeSync(() => new Promise(resolve => {
58
- setTimeout(() => {
59
- resolve(foo);
60
- }, 100);
61
- }))(false);
62
- expect(async).toBeUndefined();
63
- const sync = await maybeSync(() => new Promise(resolve => {
64
- setTimeout(() => {
65
- resolve(foo);
66
- }, 100);
67
- }))(true);
68
- expect(sync).toBe(foo);
14
+ var rejected = (value) => {
15
+ try {
16
+ step(generator.throw(value));
17
+ } catch (e) {
18
+ reject(e);
19
+ }
20
+ };
21
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
22
+ step((generator = generator.apply(__this, __arguments)).next());
69
23
  });
70
- });
24
+ };
25
+ import url from "url";
26
+ import { createCache, destroyCache } from "../spr";
27
+ import {
28
+ cacheAddition,
29
+ connectFactor,
30
+ fname,
31
+ maybeSync,
32
+ namespaceHash,
33
+ valueFactory,
34
+ withCoalescedInvoke
35
+ } from "../util";
36
+ var require_cache_fun_test = __commonJS({
37
+ "src/libs/render/cache/__tests__/cache.fun.test.ts"(exports) {
38
+ describe("test spr util functions", () => {
39
+ it("should return value correctly", () => {
40
+ expect(connectFactor("bar", "foo")).toBe("bar-foo");
41
+ expect(fname(1)).toBe("f1");
42
+ expect(namespaceHash("modern", "!@#$%^&")).toBe("modern/!@#$%^&");
43
+ });
44
+ it("should create or destroy instance correctly", () => {
45
+ const ins1 = createCache();
46
+ const ins2 = createCache();
47
+ expect(ins1 === ins2).toBe(true);
48
+ destroyCache();
49
+ const ins3 = createCache();
50
+ expect(ins1 === ins3).toBe(false);
51
+ expect(ins2 === ins3).toBe(false);
52
+ });
53
+ it("should return function correctly", () => {
54
+ const urlParams = (() => new url.URLSearchParams())();
55
+ urlParams.set("name", "modern");
56
+ const getParam = valueFactory(urlParams);
57
+ expect(getParam("name")).toBe("modern");
58
+ const headers = { age: "12345" };
59
+ const getHeader = valueFactory(headers);
60
+ expect(getHeader("age")).toBe("12345");
61
+ });
62
+ it("should add target html content", () => {
63
+ const contentNoHead = "<div>123</div>";
64
+ const html = cacheAddition(contentNoHead, Math.random().toString());
65
+ expect(html).toBe(contentNoHead);
66
+ const contentWithHead = "<head></head><div>123</div>";
67
+ const hash = Math.random().toString();
68
+ const htmlWithHead = cacheAddition(contentWithHead, hash);
69
+ expect(htmlWithHead).toBe(
70
+ `<head><meta name="x-moden-spr" content="${hash}"></head><div>123</div>`
71
+ );
72
+ });
73
+ it("should only invoke func one time", () => __async(exports, null, function* () {
74
+ let index = 0;
75
+ const fn = withCoalescedInvoke(
76
+ () => __async(exports, null, function* () {
77
+ return new Promise((resolve) => {
78
+ setTimeout(() => {
79
+ index += 1;
80
+ resolve(index);
81
+ }, 500);
82
+ });
83
+ })
84
+ );
85
+ const key = "test";
86
+ const [res1, res2] = yield Promise.all([fn(key, []), fn(key, [])]);
87
+ expect(res1.isOrigin && res2.isOrigin).toBe(false);
88
+ expect(res1.isOrigin || res2.isOrigin).toBe(true);
89
+ expect(res1.value).toBe(1);
90
+ expect(res2.value).toBe(1);
91
+ }));
92
+ it("should invoke sync or async", () => __async(exports, null, function* () {
93
+ const foo = "";
94
+ const async = yield maybeSync(
95
+ () => new Promise((resolve) => {
96
+ setTimeout(() => {
97
+ resolve(foo);
98
+ }, 100);
99
+ })
100
+ )(false);
101
+ expect(async).toBeUndefined();
102
+ const sync = yield maybeSync(
103
+ () => new Promise((resolve) => {
104
+ setTimeout(() => {
105
+ resolve(foo);
106
+ }, 100);
107
+ })
108
+ )(true);
109
+ expect(sync).toBe(foo);
110
+ }));
111
+ });
112
+ }
113
+ });
114
+ export default require_cache_fun_test();
@@ -1,233 +1,254 @@
1
- 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; }
2
-
3
- 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; }
4
-
5
- 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; }
6
-
7
- import { createCache, destroyCache } from "../spr";
8
- import { errorConfiguration } from "./error-configuration";
9
- import { cacheabelAry } from "./cacheable";
10
- import { matchedCacheableAry } from "./matched-cache";
11
-
12
- const createCacheConfig = (config = {}) => _objectSpread({
13
- excludes: null,
14
- includes: null,
15
- interval: 10,
16
- staleLimit: false,
17
- level: 0,
18
- fallback: false,
19
- matches: null
20
- }, config);
21
-
22
- jest.setTimeout(60000);
23
- describe('cache', () => {
24
- it('should cache correctly', async () => {
25
- destroyCache();
26
- const cache = createCache();
27
- const context = {
28
- entry: '',
29
- pathname: '',
30
- query: {},
31
- headers: {}
32
- };
33
- const content = 'hello';
34
- const cacheConfig = createCacheConfig();
35
- await cache.set(context, content, cacheConfig, true);
36
- const cacheResult = await cache.get(context);
37
- expect(cacheResult).not.toBe(null);
38
- expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe('hello');
39
- });
40
- it('should ignore cache set when cache config not exist', async () => {
41
- destroyCache();
42
- destroyCache();
43
- const cache = createCache();
44
- const context = {
45
- entry: '',
46
- pathname: '',
47
- query: {},
48
- headers: {}
49
- };
50
- const content = 'hello';
51
- const shouldCache = await cache.set(context, content, null, true);
52
- expect(shouldCache).toBe(false);
53
- });
54
- it('should calcual cache key error', async () => {
55
- destroyCache();
56
- const cache = createCache();
57
- const content = 'hello';
58
-
59
- for (const config of errorConfiguration) {
60
- const cacheConfig = createCacheConfig(config);
61
- const tmpEntry = Math.random().toString();
62
- const context = {
63
- entry: tmpEntry,
64
- pathname: '',
65
- query: {},
66
- headers: {}
67
- };
68
- const shouldCache = await cache.set(context, content, cacheConfig);
69
- expect(shouldCache).toBe(false);
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
6
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
+ var __spreadValues = (a, b) => {
8
+ for (var prop in b || (b = {}))
9
+ if (__hasOwnProp.call(b, prop))
10
+ __defNormalProp(a, prop, b[prop]);
11
+ if (__getOwnPropSymbols)
12
+ for (var prop of __getOwnPropSymbols(b)) {
13
+ if (__propIsEnum.call(b, prop))
14
+ __defNormalProp(a, prop, b[prop]);
70
15
  }
71
- });
72
- it('should get nothing for diff requestKey', async () => {
73
- destroyCache();
74
- const cache = createCache();
75
- const context = {
76
- entry: '',
77
- pathname: '',
78
- query: {},
79
- headers: {}
80
- };
81
- const content = 'hello';
82
- const cacheConfig = createCacheConfig({
83
- level: 1,
84
- includes: {
85
- query: ['name']
16
+ return a;
17
+ };
18
+ var __commonJS = (cb, mod) => function __require() {
19
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
20
+ };
21
+ var __async = (__this, __arguments, generator) => {
22
+ return new Promise((resolve, reject) => {
23
+ var fulfilled = (value) => {
24
+ try {
25
+ step(generator.next(value));
26
+ } catch (e) {
27
+ reject(e);
86
28
  }
87
- });
88
- await cache.set(context, content, cacheConfig, true);
89
- const context_req = {
90
- entry: '',
91
- pathname: '/home',
92
- query: {},
93
- headers: {}
94
- };
95
- const cacheResult = await cache.get(context_req);
96
- expect(cacheResult).toBe(null);
97
- });
98
- it('should get nothing for diff cacheHash', async () => {
99
- destroyCache();
100
- const cache = createCache();
101
- const context = {
102
- entry: '',
103
- pathname: '',
104
- query: {},
105
- headers: {}
106
29
  };
107
- const content = 'hello';
108
- const cacheConfig = createCacheConfig({
109
- level: 1,
110
- includes: {
111
- query: ['name']
30
+ var rejected = (value) => {
31
+ try {
32
+ step(generator.throw(value));
33
+ } catch (e) {
34
+ reject(e);
112
35
  }
113
- });
114
- await cache.set(context, content, cacheConfig, true);
115
- const context_req = {
116
- entry: '',
117
- pathname: '',
118
- query: {
119
- name: 'zll'
120
- },
121
- headers: {}
122
36
  };
123
- const cacheResult = await cache.get(context_req);
124
- expect(cacheResult).toBe(null);
37
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
38
+ step((generator = generator.apply(__this, __arguments)).next());
125
39
  });
126
- it('should get cache correctly', async () => {
127
- destroyCache();
128
- const cache = createCache();
129
-
130
- for (const cacheable of cacheabelAry) {
131
- const context = {
132
- entry: '',
133
- pathname: cacheable.requestOpt.url,
134
- query: cacheable.requestOpt.query || {},
135
- headers: cacheable.requestOpt.headers || {}
136
- };
137
- const cacheConfig = createCacheConfig(cacheable.cacheConfig || {});
138
- await cache.set(context, cacheable.content, cacheConfig, true);
139
- const cacheResult = await cache.get(context);
140
- expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe(cacheable.content);
141
- }
142
- });
143
- it('should match cache correctly', async () => {
144
- destroyCache();
145
- const cache = createCache();
146
-
147
- for (const cacheable of matchedCacheableAry) {
148
- const [baseCacheable, matchOne, ...other] = cacheable;
149
- const {
150
- requestOpt = {},
151
- cacheConfig,
152
- content
153
- } = baseCacheable;
154
- const context = {
155
- entry: '',
156
- pathname: requestOpt.url,
157
- query: requestOpt.query,
158
- headers: requestOpt.headers
159
- };
160
- await cache.set(context, content, createCacheConfig(cacheConfig), true);
161
- const matchContext = {
162
- entry: '',
163
- pathname: matchOne.url,
164
- query: matchOne.query,
165
- headers: matchOne.headers
166
- };
167
- const cacheResult = await cache.get(matchContext);
168
- expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe(content);
169
-
170
- for (const notMatch of other) {
171
- const notMatchContext = {
172
- entry: '',
173
- pathname: notMatch.url,
174
- query: notMatch.query,
175
- headers: notMatch.headers
40
+ };
41
+ import { createCache, destroyCache } from "../spr";
42
+ import { errorConfiguration } from "./error-configuration";
43
+ import { cacheabelAry } from "./cacheable";
44
+ import { matchedCacheableAry } from "./matched-cache";
45
+ var require_cache_test = __commonJS({
46
+ "src/libs/render/cache/__tests__/cache.test.ts"(exports) {
47
+ const createCacheConfig = (config = {}) => __spreadValues({
48
+ excludes: null,
49
+ includes: null,
50
+ interval: 10,
51
+ staleLimit: false,
52
+ level: 0,
53
+ fallback: false,
54
+ matches: null
55
+ }, config);
56
+ jest.setTimeout(6e4);
57
+ describe("cache", () => {
58
+ it("should cache correctly", () => __async(exports, null, function* () {
59
+ destroyCache();
60
+ const cache = createCache();
61
+ const context = {
62
+ entry: "",
63
+ pathname: "",
64
+ query: {},
65
+ headers: {}
176
66
  };
177
- const nothing = await cache.get(notMatchContext);
178
- expect(nothing).toBe(null);
179
- }
180
- }
181
- });
182
- it('should stale cache correctly', async () => {
183
- destroyCache();
184
- const cache = createCache();
185
- const context = {
186
- entry: '',
187
- pathname: '',
188
- query: {},
189
- headers: {}
190
- };
191
- const config = createCacheConfig({
192
- interval: 5
193
- });
194
- const content = 'hello';
195
- const shouldCache = await cache.set(context, content, config, true);
196
- expect(shouldCache.value).toBe(true);
197
- const freshResult = await cache.get(context);
198
- expect(freshResult === null || freshResult === void 0 ? void 0 : freshResult.isStale).toBe(false);
199
- await new Promise(resolve => {
200
- setTimeout(() => {
201
- resolve();
202
- }, 6000);
203
- });
204
- const staleResult = await cache.get(context);
205
- expect(staleResult === null || staleResult === void 0 ? void 0 : staleResult.isStale).toBe(true);
206
- });
207
- it('should garbage cache correctly', async () => {
208
- destroyCache();
209
- const cache = createCache();
210
- const context = {
211
- entry: '',
212
- pathname: '',
213
- query: {},
214
- headers: {}
215
- };
216
- const config = createCacheConfig({
217
- interval: 3,
218
- staleLimit: 8
219
- });
220
- const content = 'hello';
221
- const shouldCache = await cache.set(context, content, config, true);
222
- expect(shouldCache.value).toBe(true);
223
- const freshResult = await cache.get(context);
224
- expect(freshResult === null || freshResult === void 0 ? void 0 : freshResult.isGarbage).toBe(false);
225
- await new Promise(resolve => {
226
- setTimeout(() => {
227
- resolve();
228
- }, 10000);
67
+ const content = "hello";
68
+ const cacheConfig = createCacheConfig();
69
+ yield cache.set(context, content, cacheConfig, true);
70
+ const cacheResult = yield cache.get(context);
71
+ expect(cacheResult).not.toBe(null);
72
+ expect(cacheResult == null ? void 0 : cacheResult.content).toBe("hello");
73
+ }));
74
+ it("should ignore cache set when cache config not exist", () => __async(exports, null, function* () {
75
+ destroyCache();
76
+ destroyCache();
77
+ const cache = createCache();
78
+ const context = {
79
+ entry: "",
80
+ pathname: "",
81
+ query: {},
82
+ headers: {}
83
+ };
84
+ const content = "hello";
85
+ const shouldCache = yield cache.set(context, content, null, true);
86
+ expect(shouldCache).toBe(false);
87
+ }));
88
+ it("should calcual cache key error", () => __async(exports, null, function* () {
89
+ destroyCache();
90
+ const cache = createCache();
91
+ const content = "hello";
92
+ for (const config of errorConfiguration) {
93
+ const cacheConfig = createCacheConfig(config);
94
+ const tmpEntry = Math.random().toString();
95
+ const context = {
96
+ entry: tmpEntry,
97
+ pathname: "",
98
+ query: {},
99
+ headers: {}
100
+ };
101
+ const shouldCache = yield cache.set(context, content, cacheConfig);
102
+ expect(shouldCache).toBe(false);
103
+ }
104
+ }));
105
+ it("should get nothing for diff requestKey", () => __async(exports, null, function* () {
106
+ destroyCache();
107
+ const cache = createCache();
108
+ const context = {
109
+ entry: "",
110
+ pathname: "",
111
+ query: {},
112
+ headers: {}
113
+ };
114
+ const content = "hello";
115
+ const cacheConfig = createCacheConfig({
116
+ level: 1,
117
+ includes: { query: ["name"] }
118
+ });
119
+ yield cache.set(context, content, cacheConfig, true);
120
+ const context_req = {
121
+ entry: "",
122
+ pathname: "/home",
123
+ query: {},
124
+ headers: {}
125
+ };
126
+ const cacheResult = yield cache.get(context_req);
127
+ expect(cacheResult).toBe(null);
128
+ }));
129
+ it("should get nothing for diff cacheHash", () => __async(exports, null, function* () {
130
+ destroyCache();
131
+ const cache = createCache();
132
+ const context = {
133
+ entry: "",
134
+ pathname: "",
135
+ query: {},
136
+ headers: {}
137
+ };
138
+ const content = "hello";
139
+ const cacheConfig = createCacheConfig({
140
+ level: 1,
141
+ includes: { query: ["name"] }
142
+ });
143
+ yield cache.set(context, content, cacheConfig, true);
144
+ const context_req = {
145
+ entry: "",
146
+ pathname: "",
147
+ query: { name: "zll" },
148
+ headers: {}
149
+ };
150
+ const cacheResult = yield cache.get(context_req);
151
+ expect(cacheResult).toBe(null);
152
+ }));
153
+ it("should get cache correctly", () => __async(exports, null, function* () {
154
+ destroyCache();
155
+ const cache = createCache();
156
+ for (const cacheable of cacheabelAry) {
157
+ const context = {
158
+ entry: "",
159
+ pathname: cacheable.requestOpt.url,
160
+ query: cacheable.requestOpt.query || {},
161
+ headers: cacheable.requestOpt.headers || {}
162
+ };
163
+ const cacheConfig = createCacheConfig(cacheable.cacheConfig || {});
164
+ yield cache.set(context, cacheable.content, cacheConfig, true);
165
+ const cacheResult = yield cache.get(context);
166
+ expect(cacheResult == null ? void 0 : cacheResult.content).toBe(cacheable.content);
167
+ }
168
+ }));
169
+ it("should match cache correctly", () => __async(exports, null, function* () {
170
+ destroyCache();
171
+ const cache = createCache();
172
+ for (const cacheable of matchedCacheableAry) {
173
+ const [baseCacheable, matchOne, ...other] = cacheable;
174
+ const { requestOpt = {}, cacheConfig, content } = baseCacheable;
175
+ const context = {
176
+ entry: "",
177
+ pathname: requestOpt.url,
178
+ query: requestOpt.query,
179
+ headers: requestOpt.headers
180
+ };
181
+ yield cache.set(context, content, createCacheConfig(cacheConfig), true);
182
+ const matchContext = {
183
+ entry: "",
184
+ pathname: matchOne.url,
185
+ query: matchOne.query,
186
+ headers: matchOne.headers
187
+ };
188
+ const cacheResult = yield cache.get(matchContext);
189
+ expect(cacheResult == null ? void 0 : cacheResult.content).toBe(content);
190
+ for (const notMatch of other) {
191
+ const notMatchContext = {
192
+ entry: "",
193
+ pathname: notMatch.url,
194
+ query: notMatch.query,
195
+ headers: notMatch.headers
196
+ };
197
+ const nothing = yield cache.get(notMatchContext);
198
+ expect(nothing).toBe(null);
199
+ }
200
+ }
201
+ }));
202
+ it("should stale cache correctly", () => __async(exports, null, function* () {
203
+ destroyCache();
204
+ const cache = createCache();
205
+ const context = {
206
+ entry: "",
207
+ pathname: "",
208
+ query: {},
209
+ headers: {}
210
+ };
211
+ const config = createCacheConfig({ interval: 5 });
212
+ const content = "hello";
213
+ const shouldCache = yield cache.set(context, content, config, true);
214
+ expect(shouldCache.value).toBe(true);
215
+ const freshResult = yield cache.get(context);
216
+ expect(freshResult == null ? void 0 : freshResult.isStale).toBe(false);
217
+ yield new Promise((resolve) => {
218
+ setTimeout(() => {
219
+ resolve();
220
+ }, 6e3);
221
+ });
222
+ const staleResult = yield cache.get(context);
223
+ expect(staleResult == null ? void 0 : staleResult.isStale).toBe(true);
224
+ }));
225
+ it("should garbage cache correctly", () => __async(exports, null, function* () {
226
+ destroyCache();
227
+ const cache = createCache();
228
+ const context = {
229
+ entry: "",
230
+ pathname: "",
231
+ query: {},
232
+ headers: {}
233
+ };
234
+ const config = createCacheConfig({
235
+ interval: 3,
236
+ staleLimit: 8
237
+ });
238
+ const content = "hello";
239
+ const shouldCache = yield cache.set(context, content, config, true);
240
+ expect(shouldCache.value).toBe(true);
241
+ const freshResult = yield cache.get(context);
242
+ expect(freshResult == null ? void 0 : freshResult.isGarbage).toBe(false);
243
+ yield new Promise((resolve) => {
244
+ setTimeout(() => {
245
+ resolve();
246
+ }, 1e4);
247
+ });
248
+ const staleResult = yield cache.get(context);
249
+ expect(staleResult == null ? void 0 : staleResult.isGarbage).toBe(true);
250
+ }));
229
251
  });
230
- const staleResult = await cache.get(context);
231
- expect(staleResult === null || staleResult === void 0 ? void 0 : staleResult.isGarbage).toBe(true);
232
- });
233
- });
252
+ }
253
+ });
254
+ export default require_cache_test();