@modern-js/server 1.3.1 → 1.3.2

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 (52) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/package.json +9 -9
  3. package/src/constants.ts +0 -26
  4. package/src/dev-tools/babel/register.ts +0 -37
  5. package/src/dev-tools/dev-server-plugin.ts +0 -48
  6. package/src/dev-tools/https/global.d.ts +0 -3
  7. package/src/dev-tools/https/index.ts +0 -12
  8. package/src/dev-tools/launch-editor/index.ts +0 -29
  9. package/src/dev-tools/mock/getMockData.ts +0 -109
  10. package/src/dev-tools/mock/index.ts +0 -63
  11. package/src/dev-tools/socket-server.ts +0 -192
  12. package/src/dev-tools/watcher/dependency-tree.ts +0 -94
  13. package/src/dev-tools/watcher/index.ts +0 -79
  14. package/src/dev-tools/watcher/stats-cache.ts +0 -53
  15. package/src/index.ts +0 -16
  16. package/src/libs/context/context.ts +0 -176
  17. package/src/libs/context/index.ts +0 -7
  18. package/src/libs/hook-api/route.ts +0 -38
  19. package/src/libs/hook-api/template.ts +0 -53
  20. package/src/libs/metrics.ts +0 -15
  21. package/src/libs/proxy.ts +0 -85
  22. package/src/libs/render/cache/__tests__/cache.fun.test.ts +0 -94
  23. package/src/libs/render/cache/__tests__/cache.test.ts +0 -240
  24. package/src/libs/render/cache/__tests__/cacheable.ts +0 -44
  25. package/src/libs/render/cache/__tests__/error-configuration.ts +0 -34
  26. package/src/libs/render/cache/__tests__/matched-cache.ts +0 -88
  27. package/src/libs/render/cache/index.ts +0 -75
  28. package/src/libs/render/cache/page-caches/index.ts +0 -11
  29. package/src/libs/render/cache/page-caches/lru.ts +0 -38
  30. package/src/libs/render/cache/spr.ts +0 -301
  31. package/src/libs/render/cache/type.ts +0 -59
  32. package/src/libs/render/cache/util.ts +0 -97
  33. package/src/libs/render/index.ts +0 -78
  34. package/src/libs/render/modern/browser-list.ts +0 -7
  35. package/src/libs/render/modern/index.ts +0 -41
  36. package/src/libs/render/modern/module.d.ts +0 -4
  37. package/src/libs/render/reader.ts +0 -119
  38. package/src/libs/render/ssr.ts +0 -62
  39. package/src/libs/render/static.ts +0 -52
  40. package/src/libs/render/type.ts +0 -38
  41. package/src/libs/route/index.ts +0 -77
  42. package/src/libs/route/matcher.ts +0 -93
  43. package/src/libs/route/route.ts +0 -32
  44. package/src/libs/serve-file.ts +0 -34
  45. package/src/server/dev-server/dev-server-split.ts +0 -41
  46. package/src/server/dev-server/dev-server.ts +0 -300
  47. package/src/server/dev-server/index.ts +0 -2
  48. package/src/server/index.ts +0 -163
  49. package/src/server/modern-server-split.ts +0 -97
  50. package/src/server/modern-server.ts +0 -636
  51. package/src/type.ts +0 -88
  52. package/src/utils.ts +0 -79
@@ -1,240 +0,0 @@
1
- import { createCache, destroyCache } from '../spr';
2
- import { CacheContext } from '../type';
3
- import { errorConfiguration } from './error-configuration';
4
- import { cacheabelAry } from './cacheable';
5
- import { matchedCacheableAry } from './matched-cache';
6
-
7
- const createCacheConfig = (config: any = {}) => ({
8
- excludes: null,
9
- includes: null,
10
- interval: 10,
11
- staleLimit: false,
12
- level: 0,
13
- fallback: false,
14
- matches: null,
15
- ...config,
16
- });
17
-
18
- jest.setTimeout(60000);
19
-
20
- describe('cache', () => {
21
- it('should cache correctly', async () => {
22
- destroyCache();
23
- const cache = createCache();
24
- const context: CacheContext = {
25
- entry: '',
26
- pathname: '',
27
- query: {},
28
- headers: {},
29
- };
30
- const content = 'hello';
31
- const cacheConfig = createCacheConfig();
32
-
33
- await cache.set(context, content, cacheConfig, true);
34
- const cacheResult = await cache.get(context);
35
- expect(cacheResult).not.toBe(null);
36
- expect(cacheResult?.content).toBe('hello');
37
- });
38
-
39
- it('should ignore cache set when cache config not exist', async () => {
40
- destroyCache();
41
- destroyCache();
42
- const cache = createCache();
43
- const context: CacheContext = {
44
- entry: '',
45
- pathname: '',
46
- query: {},
47
- headers: {},
48
- };
49
- const content = 'hello';
50
-
51
- const shouldCache = await cache.set(context, content, null as any, true);
52
- expect(shouldCache).toBe(false);
53
- });
54
-
55
- it('should calcual cache key error', async () => {
56
- destroyCache();
57
- const cache = createCache();
58
- const content = 'hello';
59
-
60
- for (const config of errorConfiguration) {
61
- const cacheConfig = createCacheConfig(config);
62
- const tmpEntry = Math.random().toString();
63
- const context: CacheContext = {
64
- entry: tmpEntry,
65
- pathname: '',
66
- query: {},
67
- headers: {},
68
- };
69
- const shouldCache = await cache.set(context, content, cacheConfig);
70
- expect(shouldCache).toBe(false);
71
- }
72
- });
73
-
74
- it('should get nothing for diff requestKey', async () => {
75
- destroyCache();
76
- const cache = createCache();
77
- const context: CacheContext = {
78
- entry: '',
79
- pathname: '',
80
- query: {},
81
- headers: {},
82
- };
83
- const content = 'hello';
84
- const cacheConfig = createCacheConfig({
85
- level: 1,
86
- includes: { query: ['name'] },
87
- });
88
-
89
- await cache.set(context, content, cacheConfig, true);
90
-
91
- const context_req: CacheContext = {
92
- entry: '',
93
- pathname: '/home',
94
- query: {},
95
- headers: {},
96
- };
97
-
98
- const cacheResult = await cache.get(context_req);
99
- expect(cacheResult).toBe(null);
100
- });
101
-
102
- it('should get nothing for diff cacheHash', async () => {
103
- destroyCache();
104
- const cache = createCache();
105
- const context: CacheContext = {
106
- entry: '',
107
- pathname: '',
108
- query: {},
109
- headers: {},
110
- };
111
- const content = 'hello';
112
- const cacheConfig = createCacheConfig({
113
- level: 1,
114
- includes: { query: ['name'] },
115
- });
116
-
117
- await cache.set(context, content, cacheConfig, true);
118
-
119
- const context_req: CacheContext = {
120
- entry: '',
121
- pathname: '',
122
- query: { name: 'zll' },
123
- headers: {},
124
- };
125
-
126
- const cacheResult = await cache.get(context_req);
127
- expect(cacheResult).toBe(null);
128
- });
129
-
130
- it('should get cache correctly', async () => {
131
- destroyCache();
132
- const cache = createCache();
133
- for (const cacheable of cacheabelAry) {
134
- const context: CacheContext = {
135
- entry: '',
136
- pathname: cacheable.requestOpt.url,
137
- query: cacheable.requestOpt.query || {},
138
- headers: cacheable.requestOpt.headers || {},
139
- };
140
- const cacheConfig = createCacheConfig(cacheable.cacheConfig || {});
141
-
142
- await cache.set(context, cacheable.content, cacheConfig, true);
143
- const cacheResult = await cache.get(context);
144
- expect(cacheResult?.content).toBe(cacheable.content);
145
- }
146
- });
147
-
148
- it('should match cache correctly', async () => {
149
- destroyCache();
150
- const cache = createCache();
151
- for (const cacheable of matchedCacheableAry) {
152
- const [baseCacheable, matchOne, ...other] = cacheable;
153
- const { requestOpt = {} as any, cacheConfig, content } = baseCacheable;
154
- const context: CacheContext = {
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
-
162
- const matchContext: CacheContext = {
163
- entry: '',
164
- pathname: matchOne.url!,
165
- query: matchOne.query!,
166
- headers: matchOne.headers!,
167
- };
168
- const cacheResult = await cache.get(matchContext);
169
- expect(cacheResult?.content).toBe(content);
170
-
171
- for (const notMatch of other) {
172
- const notMatchContext: CacheContext = {
173
- entry: '',
174
- pathname: notMatch.url!,
175
- query: notMatch.query!,
176
- headers: notMatch.headers!,
177
- };
178
- const nothing = await cache.get(notMatchContext);
179
- expect(nothing).toBe(null);
180
- }
181
- }
182
- });
183
-
184
- it('should stale cache correctly', async () => {
185
- destroyCache();
186
- const cache = createCache();
187
- const context: CacheContext = {
188
- entry: '',
189
- pathname: '',
190
- query: {},
191
- headers: {},
192
- };
193
- const config = createCacheConfig({ interval: 5 });
194
- const content = 'hello';
195
- const shouldCache = await cache.set(context, content, config, true);
196
- expect(shouldCache.value).toBe(true);
197
-
198
- const freshResult = await cache.get(context);
199
- expect(freshResult?.isStale).toBe(false);
200
-
201
- await new Promise<void>(resolve => {
202
- setTimeout(() => {
203
- resolve();
204
- }, 6000);
205
- });
206
-
207
- const staleResult = await cache.get(context);
208
- expect(staleResult?.isStale).toBe(true);
209
- });
210
-
211
- it('should garbage cache correctly', async () => {
212
- destroyCache();
213
- const cache = createCache();
214
- const context: CacheContext = {
215
- entry: '',
216
- pathname: '',
217
- query: {},
218
- headers: {},
219
- };
220
- const config = createCacheConfig({
221
- interval: 3,
222
- staleLimit: 8,
223
- });
224
- const content = 'hello';
225
- const shouldCache = await cache.set(context, content, config, true);
226
- expect(shouldCache.value).toBe(true);
227
-
228
- const freshResult = await cache.get(context);
229
- expect(freshResult?.isGarbage).toBe(false);
230
-
231
- await new Promise<void>(resolve => {
232
- setTimeout(() => {
233
- resolve();
234
- }, 10000);
235
- });
236
-
237
- const staleResult = await cache.get(context);
238
- expect(staleResult?.isGarbage).toBe(true);
239
- });
240
- });
@@ -1,44 +0,0 @@
1
- export const cacheabelAry = [
2
- {
3
- requestOpt: { url: '/' },
4
- cacheConfig: {},
5
- content: 'level0',
6
- },
7
- {
8
- requestOpt: {
9
- url: '/level-one',
10
- query: { name: 'modern' },
11
- },
12
- cacheConfig: {
13
- level: 1,
14
- includes: { query: ['name'] },
15
- },
16
- content: 'level1',
17
- },
18
- {
19
- requestOpt: {
20
- url: '/level-two',
21
- headers: { age: '18' },
22
- },
23
- cacheConfig: {
24
- level: 2,
25
- includes: { header: ['age'] },
26
- },
27
- content: 'level2',
28
- },
29
- {
30
- requestOpt: {
31
- url: '/level-three',
32
- query: { name: 'modern' },
33
- headers: { age: '18' },
34
- },
35
- cacheConfig: {
36
- level: 3,
37
- includes: {
38
- query: ['name'],
39
- header: ['age'],
40
- },
41
- },
42
- content: 'level3',
43
- },
44
- ];
@@ -1,34 +0,0 @@
1
- export const errorConfiguration = [
2
- { level: 1 },
3
- {
4
- level: 1,
5
- includes: {},
6
- },
7
- {
8
- level: 1,
9
- includes: { query: [] },
10
- },
11
- {
12
- level: 2,
13
- includes: {},
14
- },
15
- {
16
- level: 2,
17
- includes: { header: [] },
18
- },
19
- {
20
- level: 3,
21
- includes: { header: [] },
22
- },
23
- {
24
- level: 3,
25
- includes: { query: [] },
26
- },
27
- {
28
- level: 3,
29
- includes: {
30
- query: [],
31
- header: [],
32
- },
33
- },
34
- ];
@@ -1,88 +0,0 @@
1
- export const matchedCacheableAry = [
2
- [
3
- {
4
- requestOpt: {
5
- url: '/level-one',
6
- headers: {},
7
- query: { name: 'byted' },
8
- },
9
- cacheConfig: {
10
- level: 1,
11
- includes: { query: ['name'] },
12
- matches: { query: { name: { weixin: '^byted' } } },
13
- },
14
- content: 'level1',
15
- },
16
- {
17
- url: '/level-one',
18
- headers: {},
19
- query: { name: 'byted_likely' },
20
- },
21
- {
22
- url: '/level-one',
23
- headers: {},
24
- query: { name: 'not_byted' },
25
- },
26
- ],
27
- [
28
- {
29
- requestOpt: {
30
- url: '/level-two',
31
- query: {},
32
- headers: { age: '17' },
33
- },
34
- cacheConfig: {
35
- level: 2,
36
- includes: { header: ['age'] },
37
- matches: { header: { age: { one: '^1' } } },
38
- },
39
- content: 'level2',
40
- },
41
- {
42
- url: '/level-two',
43
- query: {},
44
- headers: { age: '11' },
45
- },
46
- {
47
- url: '/level-two',
48
- query: {},
49
- headers: { age: '22' },
50
- },
51
- ],
52
- [
53
- {
54
- requestOpt: {
55
- url: '/level-three',
56
- headers: { age: '17' },
57
- query: { name: 'byted' },
58
- },
59
- cacheConfig: {
60
- level: 3,
61
- includes: {
62
- query: ['name'],
63
- header: ['age'],
64
- },
65
- matches: {
66
- query: { name: { weixin: '^byted' } },
67
- header: { age: { one: '^1' } },
68
- },
69
- },
70
- content: 'level3',
71
- },
72
- {
73
- url: '/level-three',
74
- query: { name: 'byted_likely' },
75
- headers: { age: '19' },
76
- },
77
- {
78
- url: '/level-three',
79
- query: { name: 'byted_likely' },
80
- headers: {},
81
- },
82
- {
83
- url: '/level-three',
84
- query: { name: 'not_byted' },
85
- headers: { age: '19' },
86
- },
87
- ],
88
- ];
@@ -1,75 +0,0 @@
1
- import { ModernServerContext } from '../../context';
2
- import { RenderFunction, SSRServerContext } from '../type';
3
- import { ERROR_DIGEST } from '../../../constants';
4
- import { createCache } from './spr';
5
- import { namespaceHash, withCoalescedInvoke } from './util';
6
- import { CacheContext } from './type';
7
-
8
- export default (renderFn: RenderFunction, ctx: ModernServerContext) => {
9
- const sprCache = createCache();
10
-
11
- const doRender: RenderFunction = async (context: SSRServerContext) => {
12
- const cacheContext: CacheContext = {
13
- entry: context.entryName,
14
- ...context.request,
15
- };
16
-
17
- const cacheFile = await sprCache.get(cacheContext);
18
-
19
- // no cache, render sync
20
- if (!cacheFile) {
21
- const html = await renderFn(context);
22
- const { cacheConfig } = context;
23
-
24
- if (html && cacheConfig) {
25
- await sprCache.set(cacheContext, html, cacheConfig);
26
- }
27
-
28
- return html;
29
- }
30
-
31
- const cacheHash = cacheFile?.hash;
32
-
33
- // completely expired
34
- if (cacheFile.isGarbage) {
35
- const html = await renderFn(context);
36
- const { cacheConfig } = context;
37
-
38
- if (html && cacheConfig) {
39
- await sprCache.set(cacheContext, html, cacheConfig);
40
- }
41
-
42
- return html;
43
- } else if (cacheFile.isStale) {
44
- // if file is stale, request async
45
- const render = withCoalescedInvoke(() => renderFn(context)).bind(
46
- null,
47
- namespaceHash('render', cacheFile.hash),
48
- [],
49
- );
50
-
51
- render()
52
- // eslint-disable-next-line promise/prefer-await-to-then
53
- .then(res => {
54
- if (res.value && res.isOrigin) {
55
- const { cacheConfig } = context;
56
- if (cacheConfig) {
57
- sprCache.set(cacheContext, res.value, cacheConfig);
58
- } else {
59
- sprCache.del(cacheContext, cacheHash);
60
- }
61
- }
62
- })
63
- // eslint-disable-next-line promise/prefer-await-to-then
64
- .catch(e => {
65
- sprCache.del(cacheContext, cacheHash);
66
- ctx.error(ERROR_DIGEST.ERENDER, e);
67
- });
68
- }
69
-
70
- ctx.res.setHeader('x-modern-spr', '1');
71
- return cacheFile.content as string;
72
- };
73
-
74
- return doRender;
75
- };
@@ -1,11 +0,0 @@
1
- import { PageCachesInterface } from '../type';
2
- import { LRUCaches } from './lru';
3
-
4
- export async function createPageCaches(
5
- max: number,
6
- ): Promise<PageCachesInterface> {
7
- const constructorOptions = { max };
8
- const cacheInstance = new LRUCaches(constructorOptions);
9
- await cacheInstance.init();
10
- return cacheInstance;
11
- }
@@ -1,38 +0,0 @@
1
- import LRU from 'lru-cache';
2
- import { PageCache, PageCachesInterface } from '../type';
3
-
4
- export class LRUCaches implements PageCachesInterface {
5
- caches: LRU<string, PageCache>;
6
-
7
- private readonly max: number;
8
-
9
- constructor(options: { max: number }) {
10
- this.max = options.max;
11
- this.caches = new LRU(this.max);
12
- }
13
-
14
- init() {
15
- return Promise.resolve();
16
- }
17
-
18
- public keys(): string[] {
19
- return this.caches.keys();
20
- }
21
-
22
- public get(key: string) {
23
- return Promise.resolve(this.caches.get(key) || null);
24
- }
25
-
26
- public peek(key: string) {
27
- return this.caches.peek(key) || null;
28
- }
29
-
30
- public set(key: string, cache: PageCache) {
31
- this.caches.set(key, cache);
32
- return Promise.resolve();
33
- }
34
-
35
- public del(key: string) {
36
- this.caches.del(key);
37
- }
38
- }