@modern-js/server 1.4.3 → 1.4.4-beta.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 (143) hide show
  1. package/dist/js/modern/dev-tools/mock/index.js +1 -1
  2. package/dist/js/modern/dev-tools/socket-server.js +2 -2
  3. package/dist/js/modern/index.js +1 -1
  4. package/dist/js/modern/server/{dev-server/dev-server-split.js → dev-server-split.js} +0 -0
  5. package/dist/js/modern/server/{dev-server/dev-server.js → dev-server.js} +15 -14
  6. package/dist/js/modern/server/index.js +15 -138
  7. package/dist/js/modern/{libs/render/cache/type.js → types.js} +0 -0
  8. package/dist/js/node/dev-tools/mock/index.js +2 -2
  9. package/dist/js/node/dev-tools/socket-server.js +2 -3
  10. package/dist/js/node/index.js +2 -2
  11. package/dist/js/node/server/{dev-server/dev-server-split.js → dev-server-split.js} +0 -0
  12. package/dist/js/node/server/{dev-server/dev-server.js → dev-server.js} +18 -24
  13. package/dist/js/node/server/index.js +17 -152
  14. package/dist/js/node/{libs/render/cache/type.js → types.js} +0 -0
  15. package/dist/types/dev-tools/babel/register.d.ts +1 -2
  16. package/dist/types/dev-tools/dev-server-plugin.d.ts +1 -1
  17. package/dist/types/dev-tools/launch-editor/index.d.ts +1 -2
  18. package/dist/types/dev-tools/mock/getMockData.d.ts +1 -2
  19. package/dist/types/dev-tools/mock/index.d.ts +1 -2
  20. package/dist/types/dev-tools/socket-server.d.ts +1 -1
  21. package/dist/types/index.d.ts +4 -5
  22. package/dist/types/server/{dev-server/dev-server-split.d.ts → dev-server-split.d.ts} +6 -5
  23. package/dist/types/server/{dev-server/dev-server.d.ts → dev-server.d.ts} +4 -3
  24. package/dist/types/server/index.d.ts +4 -19
  25. package/dist/types/types.d.ts +28 -0
  26. package/modern.config.js +5 -1
  27. package/package.json +11 -41
  28. package/tests/server.test.ts +10 -3
  29. package/dist/js/modern/constants.js +0 -26
  30. package/dist/js/modern/libs/context/context.js +0 -180
  31. package/dist/js/modern/libs/context/index.js +0 -3
  32. package/dist/js/modern/libs/hook-api/route.js +0 -39
  33. package/dist/js/modern/libs/hook-api/template.js +0 -61
  34. package/dist/js/modern/libs/metrics.js +0 -12
  35. package/dist/js/modern/libs/proxy.js +0 -33
  36. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +0 -70
  37. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +0 -233
  38. package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +0 -53
  39. package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +0 -35
  40. package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +0 -121
  41. package/dist/js/modern/libs/render/cache/index.js +0 -74
  42. package/dist/js/modern/libs/render/cache/page-caches/index.js +0 -9
  43. package/dist/js/modern/libs/render/cache/page-caches/lru.js +0 -35
  44. package/dist/js/modern/libs/render/cache/spr.js +0 -280
  45. package/dist/js/modern/libs/render/cache/util.js +0 -79
  46. package/dist/js/modern/libs/render/index.js +0 -65
  47. package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
  48. package/dist/js/modern/libs/render/modern/index.js +0 -42
  49. package/dist/js/modern/libs/render/reader.js +0 -112
  50. package/dist/js/modern/libs/render/ssr.js +0 -58
  51. package/dist/js/modern/libs/render/static.js +0 -46
  52. package/dist/js/modern/libs/render/type.js +0 -7
  53. package/dist/js/modern/libs/route/index.js +0 -68
  54. package/dist/js/modern/libs/route/matcher.js +0 -94
  55. package/dist/js/modern/libs/route/route.js +0 -24
  56. package/dist/js/modern/libs/serve-file.js +0 -28
  57. package/dist/js/modern/server/dev-server/index.js +0 -2
  58. package/dist/js/modern/server/modern-server-split.js +0 -81
  59. package/dist/js/modern/server/modern-server.js +0 -584
  60. package/dist/js/modern/type.js +0 -1
  61. package/dist/js/modern/utils.js +0 -112
  62. package/dist/js/node/constants.js +0 -36
  63. package/dist/js/node/libs/context/context.js +0 -194
  64. package/dist/js/node/libs/context/index.js +0 -18
  65. package/dist/js/node/libs/hook-api/route.js +0 -48
  66. package/dist/js/node/libs/hook-api/template.js +0 -69
  67. package/dist/js/node/libs/metrics.js +0 -18
  68. package/dist/js/node/libs/proxy.js +0 -44
  69. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +0 -77
  70. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +0 -238
  71. package/dist/js/node/libs/render/cache/__tests__/cacheable.js +0 -60
  72. package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +0 -42
  73. package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +0 -128
  74. package/dist/js/node/libs/render/cache/index.js +0 -86
  75. package/dist/js/node/libs/render/cache/page-caches/index.js +0 -17
  76. package/dist/js/node/libs/render/cache/page-caches/lru.js +0 -47
  77. package/dist/js/node/libs/render/cache/spr.js +0 -298
  78. package/dist/js/node/libs/render/cache/util.js +0 -105
  79. package/dist/js/node/libs/render/index.js +0 -91
  80. package/dist/js/node/libs/render/modern/browser-list.js +0 -14
  81. package/dist/js/node/libs/render/modern/index.js +0 -58
  82. package/dist/js/node/libs/render/reader.js +0 -139
  83. package/dist/js/node/libs/render/ssr.js +0 -76
  84. package/dist/js/node/libs/render/static.js +0 -62
  85. package/dist/js/node/libs/render/type.js +0 -14
  86. package/dist/js/node/libs/route/index.js +0 -83
  87. package/dist/js/node/libs/route/matcher.js +0 -108
  88. package/dist/js/node/libs/route/route.js +0 -33
  89. package/dist/js/node/libs/serve-file.js +0 -41
  90. package/dist/js/node/server/dev-server/index.js +0 -27
  91. package/dist/js/node/server/modern-server-split.js +0 -97
  92. package/dist/js/node/server/modern-server.js +0 -622
  93. package/dist/js/node/type.js +0 -5
  94. package/dist/js/node/utils.js +0 -143
  95. package/dist/types/constants.d.ts +0 -20
  96. package/dist/types/libs/context/context.d.ts +0 -61
  97. package/dist/types/libs/context/index.d.ts +0 -4
  98. package/dist/types/libs/hook-api/route.d.ts +0 -14
  99. package/dist/types/libs/hook-api/template.d.ts +0 -14
  100. package/dist/types/libs/metrics.d.ts +0 -3
  101. package/dist/types/libs/proxy.d.ts +0 -4
  102. package/dist/types/libs/render/cache/__tests__/cache.fun.test.d.ts +0 -1
  103. package/dist/types/libs/render/cache/__tests__/cache.test.d.ts +0 -1
  104. package/dist/types/libs/render/cache/__tests__/cacheable.d.ts +0 -62
  105. package/dist/types/libs/render/cache/__tests__/error-configuration.d.ts +0 -28
  106. package/dist/types/libs/render/cache/__tests__/matched-cache.d.ts +0 -124
  107. package/dist/types/libs/render/cache/index.d.ts +0 -6
  108. package/dist/types/libs/render/cache/page-caches/index.d.ts +0 -2
  109. package/dist/types/libs/render/cache/page-caches/lru.d.ts +0 -15
  110. package/dist/types/libs/render/cache/spr.d.ts +0 -24
  111. package/dist/types/libs/render/cache/type.d.ts +0 -48
  112. package/dist/types/libs/render/cache/util.d.ts +0 -17
  113. package/dist/types/libs/render/index.d.ts +0 -18
  114. package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
  115. package/dist/types/libs/render/modern/index.d.ts +0 -3
  116. package/dist/types/libs/render/reader.d.ts +0 -18
  117. package/dist/types/libs/render/ssr.d.ts +0 -10
  118. package/dist/types/libs/render/static.d.ts +0 -3
  119. package/dist/types/libs/render/type.d.ts +0 -34
  120. package/dist/types/libs/route/index.d.ts +0 -15
  121. package/dist/types/libs/route/matcher.d.ts +0 -15
  122. package/dist/types/libs/route/route.d.ts +0 -14
  123. package/dist/types/libs/serve-file.d.ts +0 -8
  124. package/dist/types/server/dev-server/index.d.ts +0 -2
  125. package/dist/types/server/modern-server-split.d.ts +0 -26
  126. package/dist/types/server/modern-server.d.ts +0 -72
  127. package/dist/types/type.d.ts +0 -80
  128. package/dist/types/utils.d.ts +0 -19
  129. package/tests/context.test.ts +0 -52
  130. package/tests/fixtures/hosting-files/static/index.js +0 -1
  131. package/tests/fixtures/reader/index.ts +0 -3
  132. package/tests/fixtures/route-spec/dynamic.json +0 -13
  133. package/tests/fixtures/route-spec/index.json +0 -29
  134. package/tests/fixtures/ssr/bundle.js +0 -5
  135. package/tests/fixtures/static-dir/bar.html +0 -11
  136. package/tests/fixtures/static-dir/baz/index.html +0 -11
  137. package/tests/fixtures/static-dir/foo/index.html +0 -11
  138. package/tests/helper.ts +0 -8
  139. package/tests/hook.test.ts +0 -44
  140. package/tests/middleware.test.ts +0 -179
  141. package/tests/render.test.ts +0 -102
  142. package/tests/route.test.ts +0 -77
  143. package/tests/utils.test.ts +0 -106
@@ -1,70 +0,0 @@
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'
27
- };
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);
69
- });
70
- });
@@ -1,233 +0,0 @@
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);
70
- }
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']
86
- }
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
- };
107
- const content = 'hello';
108
- const cacheConfig = createCacheConfig({
109
- level: 1,
110
- includes: {
111
- query: ['name']
112
- }
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
- };
123
- const cacheResult = await cache.get(context_req);
124
- expect(cacheResult).toBe(null);
125
- });
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
176
- };
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);
229
- });
230
- const staleResult = await cache.get(context);
231
- expect(staleResult === null || staleResult === void 0 ? void 0 : staleResult.isGarbage).toBe(true);
232
- });
233
- });
@@ -1,53 +0,0 @@
1
- export const cacheabelAry = [{
2
- requestOpt: {
3
- url: '/'
4
- },
5
- cacheConfig: {},
6
- content: 'level0'
7
- }, {
8
- requestOpt: {
9
- url: '/level-one',
10
- query: {
11
- name: 'modern'
12
- }
13
- },
14
- cacheConfig: {
15
- level: 1,
16
- includes: {
17
- query: ['name']
18
- }
19
- },
20
- content: 'level1'
21
- }, {
22
- requestOpt: {
23
- url: '/level-two',
24
- headers: {
25
- age: '18'
26
- }
27
- },
28
- cacheConfig: {
29
- level: 2,
30
- includes: {
31
- header: ['age']
32
- }
33
- },
34
- content: 'level2'
35
- }, {
36
- requestOpt: {
37
- url: '/level-three',
38
- query: {
39
- name: 'modern'
40
- },
41
- headers: {
42
- age: '18'
43
- }
44
- },
45
- cacheConfig: {
46
- level: 3,
47
- includes: {
48
- query: ['name'],
49
- header: ['age']
50
- }
51
- },
52
- content: 'level3'
53
- }];
@@ -1,35 +0,0 @@
1
- export const errorConfiguration = [{
2
- level: 1
3
- }, {
4
- level: 1,
5
- includes: {}
6
- }, {
7
- level: 1,
8
- includes: {
9
- query: []
10
- }
11
- }, {
12
- level: 2,
13
- includes: {}
14
- }, {
15
- level: 2,
16
- includes: {
17
- header: []
18
- }
19
- }, {
20
- level: 3,
21
- includes: {
22
- header: []
23
- }
24
- }, {
25
- level: 3,
26
- includes: {
27
- query: []
28
- }
29
- }, {
30
- level: 3,
31
- includes: {
32
- query: [],
33
- header: []
34
- }
35
- }];
@@ -1,121 +0,0 @@
1
- export const matchedCacheableAry = [[{
2
- requestOpt: {
3
- url: '/level-one',
4
- headers: {},
5
- query: {
6
- name: 'byted'
7
- }
8
- },
9
- cacheConfig: {
10
- level: 1,
11
- includes: {
12
- query: ['name']
13
- },
14
- matches: {
15
- query: {
16
- name: {
17
- weixin: '^byted'
18
- }
19
- }
20
- }
21
- },
22
- content: 'level1'
23
- }, {
24
- url: '/level-one',
25
- headers: {},
26
- query: {
27
- name: 'byted_likely'
28
- }
29
- }, {
30
- url: '/level-one',
31
- headers: {},
32
- query: {
33
- name: 'not_byted'
34
- }
35
- }], [{
36
- requestOpt: {
37
- url: '/level-two',
38
- query: {},
39
- headers: {
40
- age: '17'
41
- }
42
- },
43
- cacheConfig: {
44
- level: 2,
45
- includes: {
46
- header: ['age']
47
- },
48
- matches: {
49
- header: {
50
- age: {
51
- one: '^1'
52
- }
53
- }
54
- }
55
- },
56
- content: 'level2'
57
- }, {
58
- url: '/level-two',
59
- query: {},
60
- headers: {
61
- age: '11'
62
- }
63
- }, {
64
- url: '/level-two',
65
- query: {},
66
- headers: {
67
- age: '22'
68
- }
69
- }], [{
70
- requestOpt: {
71
- url: '/level-three',
72
- headers: {
73
- age: '17'
74
- },
75
- query: {
76
- name: 'byted'
77
- }
78
- },
79
- cacheConfig: {
80
- level: 3,
81
- includes: {
82
- query: ['name'],
83
- header: ['age']
84
- },
85
- matches: {
86
- query: {
87
- name: {
88
- weixin: '^byted'
89
- }
90
- },
91
- header: {
92
- age: {
93
- one: '^1'
94
- }
95
- }
96
- }
97
- },
98
- content: 'level3'
99
- }, {
100
- url: '/level-three',
101
- query: {
102
- name: 'byted_likely'
103
- },
104
- headers: {
105
- age: '19'
106
- }
107
- }, {
108
- url: '/level-three',
109
- query: {
110
- name: 'byted_likely'
111
- },
112
- headers: {}
113
- }, {
114
- url: '/level-three',
115
- query: {
116
- name: 'not_byted'
117
- },
118
- headers: {
119
- age: '19'
120
- }
121
- }]];
@@ -1,74 +0,0 @@
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 { ERROR_DIGEST } from "../../../constants";
8
- import { createCache } from "./spr";
9
- import { namespaceHash, withCoalescedInvoke } from "./util";
10
- export default ((renderFn, ctx) => {
11
- const sprCache = createCache();
12
-
13
- const doRender = async context => {
14
- const cacheContext = _objectSpread({
15
- entry: context.entryName
16
- }, context.request);
17
-
18
- const cacheFile = await sprCache.get(cacheContext); // no cache, render sync
19
-
20
- if (!cacheFile) {
21
- const html = await renderFn(context);
22
- const {
23
- cacheConfig
24
- } = context;
25
-
26
- if (html && cacheConfig) {
27
- await sprCache.set(cacheContext, html, cacheConfig);
28
- }
29
-
30
- return html;
31
- }
32
-
33
- const cacheHash = cacheFile === null || cacheFile === void 0 ? void 0 : cacheFile.hash; // completely expired
34
-
35
- if (cacheFile.isGarbage) {
36
- const html = await renderFn(context);
37
- const {
38
- cacheConfig
39
- } = context;
40
-
41
- if (html && cacheConfig) {
42
- await sprCache.set(cacheContext, html, cacheConfig);
43
- }
44
-
45
- return html;
46
- } else if (cacheFile.isStale) {
47
- // if file is stale, request async
48
- const render = withCoalescedInvoke(() => renderFn(context)).bind(null, namespaceHash('render', cacheFile.hash), []);
49
- render() // eslint-disable-next-line promise/prefer-await-to-then
50
- .then(res => {
51
- if (res.value && res.isOrigin) {
52
- const {
53
- cacheConfig
54
- } = context;
55
-
56
- if (cacheConfig) {
57
- sprCache.set(cacheContext, res.value, cacheConfig);
58
- } else {
59
- sprCache.del(cacheContext, cacheHash);
60
- }
61
- }
62
- }) // eslint-disable-next-line promise/prefer-await-to-then
63
- .catch(e => {
64
- sprCache.del(cacheContext, cacheHash);
65
- ctx.error(ERROR_DIGEST.ERENDER, e);
66
- });
67
- }
68
-
69
- ctx.res.setHeader('x-modern-spr', '1');
70
- return cacheFile.content;
71
- };
72
-
73
- return doRender;
74
- });
@@ -1,9 +0,0 @@
1
- import { LRUCaches } from "./lru";
2
- export async function createPageCaches(max) {
3
- const constructorOptions = {
4
- max
5
- };
6
- const cacheInstance = new LRUCaches(constructorOptions);
7
- await cacheInstance.init();
8
- return cacheInstance;
9
- }
@@ -1,35 +0,0 @@
1
- import LRU from 'lru-cache';
2
- export class LRUCaches {
3
- constructor(options) {
4
- this.caches = void 0;
5
- this.max = void 0;
6
- this.max = options.max;
7
- this.caches = new LRU(this.max);
8
- }
9
-
10
- init() {
11
- return Promise.resolve();
12
- }
13
-
14
- keys() {
15
- return this.caches.keys();
16
- }
17
-
18
- get(key) {
19
- return Promise.resolve(this.caches.get(key) || null);
20
- }
21
-
22
- peek(key) {
23
- return this.caches.peek(key) || null;
24
- }
25
-
26
- set(key, cache) {
27
- this.caches.set(key, cache);
28
- return Promise.resolve();
29
- }
30
-
31
- del(key) {
32
- this.caches.del(key);
33
- }
34
-
35
- }