@modern-js/server 1.2.2-beta.0 → 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 (61) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/dist/js/modern/server/dev-server/dev-server.js +7 -3
  3. package/dist/js/node/server/dev-server/dev-server.js +7 -3
  4. package/dist/types/type.d.ts +1 -1
  5. package/jest.config.js +1 -0
  6. package/package.json +10 -10
  7. package/tests/dev.test.ts +7 -2
  8. package/tests/fixtures/pure/tsconfig.json +0 -1
  9. package/tests/server.test.ts +19 -0
  10. package/tests/watcher.test.ts +98 -0
  11. package/dist.zip +0 -0
  12. package/src/constants.ts +0 -26
  13. package/src/dev-tools/babel/register.ts +0 -37
  14. package/src/dev-tools/dev-server-plugin.ts +0 -48
  15. package/src/dev-tools/https/global.d.ts +0 -3
  16. package/src/dev-tools/https/index.ts +0 -12
  17. package/src/dev-tools/launch-editor/index.ts +0 -29
  18. package/src/dev-tools/mock/getMockData.ts +0 -109
  19. package/src/dev-tools/mock/index.ts +0 -63
  20. package/src/dev-tools/socket-server.ts +0 -192
  21. package/src/dev-tools/watcher/dependency-tree.ts +0 -94
  22. package/src/dev-tools/watcher/index.ts +0 -80
  23. package/src/dev-tools/watcher/stats-cache.ts +0 -53
  24. package/src/index.ts +0 -16
  25. package/src/libs/context/context.ts +0 -176
  26. package/src/libs/context/index.ts +0 -7
  27. package/src/libs/hook-api/route.ts +0 -38
  28. package/src/libs/hook-api/template.ts +0 -53
  29. package/src/libs/metrics.ts +0 -15
  30. package/src/libs/proxy.ts +0 -85
  31. package/src/libs/render/cache/__tests__/cache.fun.test.ts +0 -94
  32. package/src/libs/render/cache/__tests__/cache.test.ts +0 -240
  33. package/src/libs/render/cache/__tests__/cacheable.ts +0 -44
  34. package/src/libs/render/cache/__tests__/error-configuration.ts +0 -34
  35. package/src/libs/render/cache/__tests__/matched-cache.ts +0 -88
  36. package/src/libs/render/cache/index.ts +0 -75
  37. package/src/libs/render/cache/page-caches/index.ts +0 -11
  38. package/src/libs/render/cache/page-caches/lru.ts +0 -38
  39. package/src/libs/render/cache/spr.ts +0 -301
  40. package/src/libs/render/cache/type.ts +0 -59
  41. package/src/libs/render/cache/util.ts +0 -97
  42. package/src/libs/render/index.ts +0 -78
  43. package/src/libs/render/modern/browser-list.ts +0 -7
  44. package/src/libs/render/modern/index.ts +0 -41
  45. package/src/libs/render/modern/module.d.ts +0 -4
  46. package/src/libs/render/reader.ts +0 -119
  47. package/src/libs/render/ssr.ts +0 -62
  48. package/src/libs/render/static.ts +0 -52
  49. package/src/libs/render/type.ts +0 -38
  50. package/src/libs/route/index.ts +0 -77
  51. package/src/libs/route/matcher.ts +0 -93
  52. package/src/libs/route/route.ts +0 -32
  53. package/src/libs/serve-file.ts +0 -34
  54. package/src/server/dev-server/dev-server-split.ts +0 -41
  55. package/src/server/dev-server/dev-server.ts +0 -297
  56. package/src/server/dev-server/index.ts +0 -2
  57. package/src/server/index.ts +0 -163
  58. package/src/server/modern-server-split.ts +0 -97
  59. package/src/server/modern-server.ts +0 -636
  60. package/src/type.ts +0 -88
  61. package/src/utils.ts +0 -79
@@ -1,15 +0,0 @@
1
- import { Metrics } from '../type';
2
-
3
- const metrics: Metrics = {
4
- gauges() {
5
- // no impl
6
- },
7
- counter() {
8
- // no impl
9
- },
10
- timer() {
11
- // no impl
12
- },
13
- };
14
-
15
- export { metrics };
package/src/libs/proxy.ts DELETED
@@ -1,85 +0,0 @@
1
- import { IncomingMessage, ServerResponse } from 'http';
2
- import { createProxyMiddleware } from 'http-proxy-middleware';
3
- import { NextFunction } from '../type';
4
- import { ModernServerContext } from './context';
5
-
6
- type ProxyDetail = {
7
- target: string;
8
- pathRewrite?: Record<string, string>;
9
- secure?: boolean;
10
- logLevel?: 'debug' | 'info' | 'warn' | 'error' | 'silent';
11
- bypass?: (
12
- req: IncomingMessage,
13
- res: ServerResponse,
14
- proxyOptions: ProxyOptions,
15
- ) => string | undefined | null | false;
16
- context?: string | string[];
17
- changeOrigin?: boolean;
18
- };
19
-
20
- export type ProxyOptions =
21
- | Record<string, string>
22
- | Record<string, ProxyDetail>
23
- | ProxyDetail[]
24
- | ProxyDetail;
25
-
26
- export const createProxyHandler = (proxyOptions: ProxyOptions) => {
27
- if (!proxyOptions) {
28
- return null;
29
- }
30
-
31
- // If it is not an array, it may be an object that uses the context attribute
32
- // or an object in the form of { source: ProxyDetail }
33
- const formatedProxy: ProxyDetail[] = [];
34
- if (!Array.isArray(proxyOptions)) {
35
- if ('target' in proxyOptions) {
36
- formatedProxy.push(proxyOptions as ProxyDetail);
37
- } else {
38
- Array.prototype.push.apply(
39
- formatedProxy,
40
- Object.keys(proxyOptions).reduce(
41
- (total: ProxyDetail[], source: string) => {
42
- const option = proxyOptions[source];
43
-
44
- total.push({
45
- context: source,
46
- changeOrigin: true,
47
- logLevel: 'warn',
48
- ...(typeof option === 'string' ? { target: option } : option),
49
- });
50
- return total;
51
- },
52
- [],
53
- ),
54
- );
55
- }
56
- } else {
57
- formatedProxy.push(...proxyOptions);
58
- }
59
-
60
- const middlewares = formatedProxy.map(option => {
61
- const middleware = createProxyMiddleware(option.context!, option);
62
-
63
- // eslint-disable-next-line consistent-return
64
- return async (ctx: ModernServerContext, next: NextFunction) => {
65
- const { req, res } = ctx;
66
- const bypassUrl =
67
- typeof option.bypass === 'function'
68
- ? option.bypass(req, res, option)
69
- : null;
70
-
71
- // only false, no true
72
- if (typeof bypassUrl === 'boolean') {
73
- ctx.status = 404;
74
- return next();
75
- } else if (typeof bypassUrl === 'string') {
76
- ctx.url = bypassUrl;
77
- return next();
78
- }
79
-
80
- (middleware as any)(req, res, next);
81
- };
82
- });
83
-
84
- return middlewares;
85
- };
@@ -1,94 +0,0 @@
1
- import url from 'url';
2
- import { createCache, destroyCache } from '../spr';
3
- import {
4
- cacheAddition,
5
- connectFactor,
6
- fname,
7
- maybeSync,
8
- namespaceHash,
9
- valueFactory,
10
- withCoalescedInvoke,
11
- } from '../util';
12
-
13
- describe('test spr util functions', () => {
14
- it('should return value correctly', () => {
15
- expect(connectFactor('bar', 'foo')).toBe('bar-foo');
16
- expect(fname(1)).toBe('f1');
17
- expect(namespaceHash('modern', '!@#$%^&')).toBe('modern/!@#$%^&');
18
- });
19
-
20
- it('should create or destroy instance correctly', () => {
21
- const ins1 = createCache();
22
- const ins2 = createCache();
23
- expect(ins1 === ins2).toBe(true);
24
- destroyCache();
25
- const ins3 = createCache();
26
- expect(ins1 === ins3).toBe(false);
27
- expect(ins2 === ins3).toBe(false);
28
- });
29
-
30
- it('should return function correctly', () => {
31
- const urlParams: any = (() => new url.URLSearchParams())();
32
- urlParams.set('name', 'modern');
33
- const getParam = valueFactory(urlParams);
34
- expect(getParam('name')).toBe('modern');
35
- const headers: any = { age: '12345' };
36
- const getHeader = valueFactory(headers);
37
- expect(getHeader('age')).toBe('12345');
38
- });
39
-
40
- it('should add target html content', () => {
41
- const contentNoHead = '<div>123</div>';
42
- const html = cacheAddition(contentNoHead, Math.random().toString());
43
- expect(html).toBe(contentNoHead);
44
-
45
- const contentWithHead = '<head></head><div>123</div>';
46
- const hash = Math.random().toString();
47
- const htmlWithHead = cacheAddition(contentWithHead, hash);
48
- expect(htmlWithHead).toBe(
49
- `<head><meta name="x-moden-spr" content="${hash}"></head><div>123</div>`,
50
- );
51
- });
52
-
53
- it('should only invoke func one time', async () => {
54
- let index = 0;
55
- const fn = withCoalescedInvoke(
56
- async () =>
57
- new Promise(resolve => {
58
- setTimeout(() => {
59
- index += 1;
60
- resolve(index);
61
- }, 500);
62
- }),
63
- );
64
- const key = 'test';
65
- const [res1, res2] = await Promise.all([fn(key, []), fn(key, [])]);
66
- expect(res1.isOrigin && res2.isOrigin).toBe(false);
67
- expect(res1.isOrigin || res2.isOrigin).toBe(true);
68
- expect(res1.value).toBe(1);
69
- expect(res2.value).toBe(1);
70
- });
71
-
72
- it('should invoke sync or async', async () => {
73
- const foo = '';
74
- const async = await maybeSync(
75
- () =>
76
- new Promise(resolve => {
77
- setTimeout(() => {
78
- resolve(foo);
79
- }, 100);
80
- }),
81
- )(false);
82
- expect(async).toBeUndefined();
83
-
84
- const sync = await maybeSync(
85
- () =>
86
- new Promise(resolve => {
87
- setTimeout(() => {
88
- resolve(foo);
89
- }, 100);
90
- }),
91
- )(true);
92
- expect(sync).toBe(foo);
93
- });
94
- });
@@ -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
- }