@modern-js/server 1.2.1-beta.1 → 1.2.2-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.
- package/CHANGELOG.md +10 -0
- package/dist/js/modern/dev-tools/mock/index.js +1 -1
- package/dist/js/modern/dev-tools/watcher/index.js +3 -5
- package/dist/js/modern/libs/render/cache/index.js +1 -1
- package/dist/js/modern/libs/render/index.js +1 -1
- package/dist/js/modern/libs/render/modern/index.js +3 -1
- package/dist/js/modern/server/dev-server/dev-server.js +9 -3
- package/dist/js/modern/server/index.js +3 -3
- package/dist/js/modern/server/modern-server.js +3 -2
- package/dist/js/modern/utils.js +23 -0
- package/dist/js/node/dev-tools/mock/index.js +2 -2
- package/dist/js/node/dev-tools/watcher/index.js +3 -5
- package/dist/js/node/libs/render/cache/index.js +2 -2
- package/dist/js/node/libs/render/index.js +2 -2
- package/dist/js/node/libs/render/modern/index.js +3 -3
- package/dist/js/node/server/dev-server/dev-server.js +11 -3
- package/dist/js/node/server/index.js +4 -4
- package/dist/js/node/server/modern-server.js +4 -3
- package/dist/js/node/utils.js +28 -2
- package/dist/types/dev-tools/watcher/index.d.ts +2 -1
- package/dist/types/libs/metrics.d.ts +1 -1
- package/dist/types/libs/render/index.d.ts +1 -2
- package/dist/types/libs/render/ssr.d.ts +1 -2
- package/dist/types/server/dev-server/dev-server-split.d.ts +3 -3
- package/dist/types/server/dev-server/dev-server.d.ts +1 -1
- package/dist/types/server/index.d.ts +2 -2
- package/dist/types/server/modern-server-split.d.ts +4 -4
- package/dist/types/server/modern-server.d.ts +10 -7
- package/dist/types/utils.d.ts +10 -1
- package/dist.zip +0 -0
- package/jest.config.js +8 -0
- package/package.json +16 -10
- package/src/dev-tools/mock/index.ts +1 -1
- package/src/dev-tools/socket-server.ts +1 -1
- package/src/dev-tools/watcher/index.ts +9 -6
- package/src/libs/metrics.ts +1 -1
- package/src/libs/render/cache/index.ts +1 -1
- package/src/libs/render/index.ts +2 -3
- package/src/libs/render/modern/index.ts +2 -1
- package/src/libs/render/ssr.ts +1 -2
- package/src/server/dev-server/dev-server-split.ts +3 -3
- package/src/server/dev-server/dev-server.ts +44 -31
- package/src/server/index.ts +4 -4
- package/src/server/modern-server-split.ts +4 -4
- package/src/server/modern-server.ts +23 -14
- package/src/utils.ts +28 -0
- package/tests/dev.test.ts +39 -0
- package/tests/fixtures/pure/tsconfig.json +1 -1
- package/tests/middleware.test.ts +12 -11
- package/tests/server.test.ts +13 -1
- package/tests/tsconfig.json +1 -3
- package/tests/utils.test.ts +31 -0
- package/tsconfig.json +1 -3
|
@@ -3,8 +3,8 @@ import { fs } from '@modern-js/utils';
|
|
|
3
3
|
import { match } from 'path-to-regexp';
|
|
4
4
|
import { NextFunction } from '../../type';
|
|
5
5
|
import { ModernServerContext } from '../../libs/context';
|
|
6
|
+
import { AGGRED_DIR } from '../../constants';
|
|
6
7
|
import getMockData, { MockApi } from './getMockData';
|
|
7
|
-
import { AGGRED_DIR } from '@/constants';
|
|
8
8
|
|
|
9
9
|
const getMatched = (context: ModernServerContext, mockApiList: MockApi[]) => {
|
|
10
10
|
const { path: targetPathname, method: targetMethod } = context;
|
|
@@ -4,7 +4,7 @@ import ws from 'ws';
|
|
|
4
4
|
import type { Stats } from 'webpack';
|
|
5
5
|
import { logger } from '@modern-js/utils';
|
|
6
6
|
import { DevServerOptions } from '../type';
|
|
7
|
-
import { noop } from '
|
|
7
|
+
import { noop } from '../utils';
|
|
8
8
|
|
|
9
9
|
interface ExtWebSocket extends ws {
|
|
10
10
|
isAlive: boolean;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import chokidar, { FSWatcher, WatchOptions } from 'chokidar';
|
|
2
3
|
import { DependencyTree } from './dependency-tree';
|
|
3
4
|
import { StatsCache } from './stats-cache';
|
|
4
5
|
|
|
@@ -18,14 +19,16 @@ export default class Watcher {
|
|
|
18
19
|
|
|
19
20
|
private watcher!: FSWatcher;
|
|
20
21
|
|
|
21
|
-
public listen(
|
|
22
|
+
public listen(
|
|
23
|
+
files: string[],
|
|
24
|
+
options: WatchOptions,
|
|
25
|
+
callback: (changed: string) => void,
|
|
26
|
+
) {
|
|
22
27
|
const watched = files.filter(Boolean);
|
|
28
|
+
const filenames = watched.map(filename => filename.replace(/\\/g, '/'));
|
|
23
29
|
|
|
24
30
|
const cache = new StatsCache();
|
|
25
|
-
const watcher = chokidar.watch(
|
|
26
|
-
// 初始化的时候不触发 add、addDir 事件
|
|
27
|
-
ignoreInitial: true,
|
|
28
|
-
});
|
|
31
|
+
const watcher = chokidar.watch(filenames, options);
|
|
29
32
|
|
|
30
33
|
watcher.on('ready', () => {
|
|
31
34
|
cache.add(getWatchedFiles(watcher));
|
package/src/libs/metrics.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { ModernServerContext } from '../../context';
|
|
2
2
|
import { RenderFunction, SSRServerContext } from '../type';
|
|
3
|
+
import { ERROR_DIGEST } from '../../../constants';
|
|
3
4
|
import { createCache } from './spr';
|
|
4
5
|
import { namespaceHash, withCoalescedInvoke } from './util';
|
|
5
6
|
import { CacheContext } from './type';
|
|
6
|
-
import { ERROR_DIGEST } from '@/constants';
|
|
7
7
|
|
|
8
8
|
export default (renderFn: RenderFunction, ctx: ModernServerContext) => {
|
|
9
9
|
const sprCache = createCache();
|
package/src/libs/render/index.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import { fs } from '@modern-js/utils';
|
|
3
3
|
import mime from 'mime-types';
|
|
4
|
-
import { RenderResult } from '../../type';
|
|
4
|
+
import { RenderResult, ServerHookRunner } from '../../type';
|
|
5
5
|
import { ModernRoute } from '../route';
|
|
6
6
|
import { ModernServerContext } from '../context';
|
|
7
|
+
import { ERROR_DIGEST } from '../../constants';
|
|
7
8
|
import { handleDirectory } from './static';
|
|
8
9
|
import { readFile } from './reader';
|
|
9
10
|
import * as ssr from './ssr';
|
|
10
11
|
import { supportModern, getModernEntry } from './modern';
|
|
11
|
-
import { ERROR_DIGEST } from '@/constants';
|
|
12
|
-
import { ServerHookRunner } from '@/type';
|
|
13
12
|
|
|
14
13
|
export const createRenderHandler = ({
|
|
15
14
|
distDir,
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import Parser from 'ua-parser-js';
|
|
2
|
-
import nativeModules from '@babel/compat-data/native-modules';
|
|
3
2
|
import compareVersions from 'compare-versions';
|
|
4
3
|
import { ModernServerContext } from '../../context';
|
|
5
4
|
import { NativeModuleNameMap } from './browser-list';
|
|
6
5
|
|
|
6
|
+
const nativeModules = require('@babel/compat-data/native-modules');
|
|
7
|
+
|
|
7
8
|
export const supportModern = (context: ModernServerContext) => {
|
|
8
9
|
if (context.query.modern_es6) {
|
|
9
10
|
return true;
|
package/src/libs/render/ssr.ts
CHANGED
|
@@ -2,10 +2,9 @@ import path from 'path';
|
|
|
2
2
|
import { SERVER_RENDER_FUNCTION_NAME } from '@modern-js/utils';
|
|
3
3
|
import mime from 'mime-types';
|
|
4
4
|
import { ModernServerContext } from '../context';
|
|
5
|
-
import { RenderResult } from '../../type';
|
|
5
|
+
import { RenderResult, ServerHookRunner } from '../../type';
|
|
6
6
|
import cache from './cache';
|
|
7
7
|
import { SSRServerContext } from './type';
|
|
8
|
-
import { ServerHookRunner } from '@/type';
|
|
9
8
|
|
|
10
9
|
export const render = async (
|
|
11
10
|
ctx: ModernServerContext,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { APIServerStartInput } from '@modern-js/server-plugin';
|
|
2
|
+
import { mergeExtension } from '../../utils';
|
|
3
|
+
import { ModernRouteInterface } from '../../libs/route';
|
|
4
|
+
import { ApiServerMode } from '../../constants';
|
|
2
5
|
import { ModernDevServer } from './dev-server';
|
|
3
|
-
import { mergeExtension } from '@/utils';
|
|
4
|
-
import { ModernRouteInterface } from '@/libs/route';
|
|
5
|
-
import { ApiServerMode } from '@/constants';
|
|
6
6
|
|
|
7
7
|
export class ModernSSRDevServer extends ModernDevServer {
|
|
8
8
|
protected prepareAPIHandler(
|
|
@@ -15,23 +15,23 @@ import {
|
|
|
15
15
|
import type { MultiCompiler, Compiler } from 'webpack';
|
|
16
16
|
import webpackDevMiddleware from 'webpack-dev-middleware';
|
|
17
17
|
import { ModernServer } from '../modern-server';
|
|
18
|
-
import { createMockHandler } from '
|
|
19
|
-
import { createProxyHandler, ProxyOptions } from '
|
|
18
|
+
import { createMockHandler } from '../../dev-tools/mock';
|
|
19
|
+
import { createProxyHandler, ProxyOptions } from '../../libs/proxy';
|
|
20
20
|
import {
|
|
21
21
|
DevServerOptions,
|
|
22
22
|
ModernServerOptions,
|
|
23
23
|
NextFunction,
|
|
24
24
|
ServerHookRunner,
|
|
25
25
|
ReadyOptions,
|
|
26
|
-
} from '
|
|
27
|
-
import SocketServer from '
|
|
28
|
-
import DevServerPlugin from '
|
|
29
|
-
import { ModernServerContext } from '
|
|
30
|
-
import { createLaunchEditorHandler } from '
|
|
31
|
-
import { enableRegister } from '
|
|
32
|
-
import * as reader from '
|
|
33
|
-
import Watcher from '
|
|
34
|
-
import { AGGRED_DIR } from '
|
|
26
|
+
} from '../../type';
|
|
27
|
+
import SocketServer from '../../dev-tools/socket-server';
|
|
28
|
+
import DevServerPlugin from '../../dev-tools/dev-server-plugin';
|
|
29
|
+
import { ModernServerContext } from '../../libs/context';
|
|
30
|
+
import { createLaunchEditorHandler } from '../../dev-tools/launch-editor';
|
|
31
|
+
import { enableRegister } from '../../dev-tools/babel/register';
|
|
32
|
+
import * as reader from '../../libs/render/reader';
|
|
33
|
+
import Watcher from '../../dev-tools/watcher';
|
|
34
|
+
import { AGGRED_DIR } from '../../constants';
|
|
35
35
|
|
|
36
36
|
const DEFAULT_DEV_OPTIONS: DevServerOptions = {
|
|
37
37
|
client: {
|
|
@@ -166,7 +166,7 @@ export class ModernDevServer extends ModernServer {
|
|
|
166
166
|
const { dev } = this;
|
|
167
167
|
const devHttpsOption = typeof dev === 'object' && dev.https;
|
|
168
168
|
if (devHttpsOption) {
|
|
169
|
-
const { genHttpsOptions } = require('
|
|
169
|
+
const { genHttpsOptions } = require('../../dev-tools/https');
|
|
170
170
|
const httpsOptions = await genHttpsOptions(devHttpsOption);
|
|
171
171
|
return createHttpsServer(httpsOptions, handler);
|
|
172
172
|
} else {
|
|
@@ -252,32 +252,45 @@ export class ModernDevServer extends ModernServer {
|
|
|
252
252
|
const { pwd } = this;
|
|
253
253
|
const { mock } = AGGRED_DIR;
|
|
254
254
|
const defaultWatched = [
|
|
255
|
-
`${
|
|
256
|
-
`${
|
|
257
|
-
`${
|
|
258
|
-
`${
|
|
255
|
+
`${mock}/**/*`,
|
|
256
|
+
`${SERVER_DIR}/**/*`,
|
|
257
|
+
`${API_DIR}/**`,
|
|
258
|
+
`${SHARED_DIR}/**/*`,
|
|
259
259
|
];
|
|
260
260
|
|
|
261
|
+
const defaultWatchedPaths = defaultWatched.map(p =>
|
|
262
|
+
path.normalize(path.join(pwd, p)),
|
|
263
|
+
);
|
|
264
|
+
const mockPath = path.normalize(path.join(pwd, mock));
|
|
265
|
+
|
|
261
266
|
const watcher = new Watcher();
|
|
262
267
|
watcher.createDepTree();
|
|
263
268
|
|
|
264
269
|
// 监听文件变动,如果有变动则给 client,也就是 start 启动的插件发消息
|
|
265
|
-
watcher.listen(
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
270
|
+
watcher.listen(
|
|
271
|
+
defaultWatchedPaths,
|
|
272
|
+
{
|
|
273
|
+
// 初始化的时候不触发 add、addDir 事件
|
|
274
|
+
ignoreInitial: true,
|
|
275
|
+
ignored: /api\/typings\/**/,
|
|
276
|
+
},
|
|
277
|
+
(filepath: string) => {
|
|
278
|
+
watcher.updateDepTree();
|
|
279
|
+
watcher.cleanDepCache(filepath);
|
|
280
|
+
|
|
281
|
+
this.runner.reset();
|
|
282
|
+
|
|
283
|
+
if (filepath.startsWith(mockPath)) {
|
|
284
|
+
this.mockHandler = createMockHandler({ pwd });
|
|
285
|
+
} else {
|
|
286
|
+
try {
|
|
287
|
+
this.prepareFrameHandler();
|
|
288
|
+
} catch (e) {
|
|
289
|
+
this.logger.error(e as Error);
|
|
290
|
+
}
|
|
278
291
|
}
|
|
279
|
-
}
|
|
280
|
-
|
|
292
|
+
},
|
|
293
|
+
);
|
|
281
294
|
|
|
282
295
|
this.watcher = watcher;
|
|
283
296
|
}
|
package/src/server/index.ts
CHANGED
|
@@ -10,6 +10,8 @@ import {
|
|
|
10
10
|
ConfigContext,
|
|
11
11
|
UserConfig,
|
|
12
12
|
} from '@modern-js/core';
|
|
13
|
+
import { ModernServerOptions, ServerHookRunner, ReadyOptions } from '../type';
|
|
14
|
+
import { metrics as defaultMetrics } from '../libs/metrics';
|
|
13
15
|
import { ModernServer } from './modern-server';
|
|
14
16
|
import type { ModernDevServer } from './dev-server';
|
|
15
17
|
import {
|
|
@@ -17,8 +19,6 @@ import {
|
|
|
17
19
|
ModernSSRServer,
|
|
18
20
|
ModernWebServer,
|
|
19
21
|
} from './modern-server-split';
|
|
20
|
-
import { ModernServerOptions, ServerHookRunner, ReadyOptions } from '@/type';
|
|
21
|
-
import { metrics as defaultMetrics } from '@/libs/metrics';
|
|
22
22
|
|
|
23
23
|
export class Server {
|
|
24
24
|
public options: ModernServerOptions;
|
|
@@ -67,8 +67,8 @@ export class Server {
|
|
|
67
67
|
return this;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
public listen(port
|
|
71
|
-
this.app.listen(
|
|
70
|
+
public listen(port = 8080, listener: any) {
|
|
71
|
+
this.app.listen(process.env.PORT || port, () => {
|
|
72
72
|
if (listener) {
|
|
73
73
|
listener();
|
|
74
74
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { APIServerStartInput } from '@modern-js/server-plugin';
|
|
2
|
+
import { mergeExtension } from '../utils';
|
|
3
|
+
import { ModernRoute, ModernRouteInterface, RouteMatcher } from '../libs/route';
|
|
4
|
+
import { ApiServerMode } from '../constants';
|
|
5
|
+
import { ModernServerContext } from '../libs/context';
|
|
2
6
|
import { ModernServer } from './modern-server';
|
|
3
|
-
import { mergeExtension } from '@/utils';
|
|
4
|
-
import { ModernRoute, ModernRouteInterface, RouteMatcher } from '@/libs/route';
|
|
5
|
-
import { ApiServerMode } from '@/constants';
|
|
6
|
-
import { ModernServerContext } from '@/libs/context';
|
|
7
7
|
|
|
8
8
|
export class ModernSSRServer extends ModernServer {
|
|
9
9
|
// Todo should not invoke any route hook in modernSSRServer
|
|
@@ -4,10 +4,10 @@ import util from 'util';
|
|
|
4
4
|
import path from 'path';
|
|
5
5
|
import { fs, ROUTE_SPEC_FILE } from '@modern-js/utils';
|
|
6
6
|
import { Adapter, APIServerStartInput } from '@modern-js/server-plugin';
|
|
7
|
-
import { createMiddlewareCollecter } from '@modern-js/server-utils';
|
|
8
7
|
import type { NormalizedConfig } from '@modern-js/core';
|
|
9
8
|
import mime from 'mime-types';
|
|
10
9
|
import axios from 'axios';
|
|
10
|
+
import clone from 'lodash.clone';
|
|
11
11
|
import {
|
|
12
12
|
ModernServerOptions,
|
|
13
13
|
NextFunction,
|
|
@@ -16,27 +16,32 @@ import {
|
|
|
16
16
|
Logger,
|
|
17
17
|
ReadyOptions,
|
|
18
18
|
ConfWithBFF,
|
|
19
|
-
} from '
|
|
19
|
+
} from '../type';
|
|
20
20
|
import {
|
|
21
21
|
RouteMatchManager,
|
|
22
22
|
ModernRouteInterface,
|
|
23
23
|
ModernRoute,
|
|
24
24
|
RouteMatcher,
|
|
25
|
-
} from '
|
|
26
|
-
import { createRenderHandler } from '
|
|
27
|
-
import { createStaticFileHandler } from '
|
|
28
|
-
import {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
} from '../libs/route';
|
|
26
|
+
import { createRenderHandler } from '../libs/render';
|
|
27
|
+
import { createStaticFileHandler } from '../libs/serve-file';
|
|
28
|
+
import {
|
|
29
|
+
createErrorDocument,
|
|
30
|
+
createMiddlewareCollecter,
|
|
31
|
+
mergeExtension,
|
|
32
|
+
noop,
|
|
33
|
+
} from '../utils';
|
|
34
|
+
import * as reader from '../libs/render/reader';
|
|
35
|
+
import { createProxyHandler, ProxyOptions } from '../libs/proxy';
|
|
36
|
+
import { createContext, ModernServerContext } from '../libs/context';
|
|
32
37
|
import {
|
|
33
38
|
AGGRED_DIR,
|
|
34
39
|
ApiServerMode,
|
|
35
40
|
ERROR_DIGEST,
|
|
36
41
|
ERROR_PAGE_TEXT,
|
|
37
|
-
} from '
|
|
38
|
-
import { createTemplateAPI } from '
|
|
39
|
-
import { createRouteAPI } from '
|
|
42
|
+
} from '../constants';
|
|
43
|
+
import { createTemplateAPI } from '../libs/hook-api/template';
|
|
44
|
+
import { createRouteAPI } from '../libs/hook-api/route';
|
|
40
45
|
|
|
41
46
|
type ModernServerHandler = (
|
|
42
47
|
context: ModernServerContext,
|
|
@@ -319,9 +324,13 @@ export class ModernServer {
|
|
|
319
324
|
|
|
320
325
|
protected async emitRouteHook(
|
|
321
326
|
eventName: 'beforeMatch' | 'afterMatch' | 'beforeRender' | 'afterRender',
|
|
322
|
-
input:
|
|
327
|
+
input: {
|
|
328
|
+
context: ModernServerContext;
|
|
329
|
+
[propsName: string]: any;
|
|
330
|
+
},
|
|
323
331
|
) {
|
|
324
|
-
|
|
332
|
+
input.context = clone(input.context);
|
|
333
|
+
return this.runner[eventName](input as any, { onLast: noop as any });
|
|
325
334
|
}
|
|
326
335
|
|
|
327
336
|
// warmup ssr function
|
package/src/utils.ts
CHANGED
|
@@ -49,3 +49,31 @@ export const createErrorDocument = (status: number, text: string) => {
|
|
|
49
49
|
</html>
|
|
50
50
|
`;
|
|
51
51
|
};
|
|
52
|
+
|
|
53
|
+
export type CollectMiddlewaresResult = {
|
|
54
|
+
web: any[];
|
|
55
|
+
api: any[];
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const createMiddlewareCollecter = () => {
|
|
59
|
+
const webMiddlewares: any[] = [];
|
|
60
|
+
const apiMiddlewares: any[] = [];
|
|
61
|
+
|
|
62
|
+
const addWebMiddleware = (input: any) => {
|
|
63
|
+
webMiddlewares.push(input);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const addAPIMiddleware = (input: any) => {
|
|
67
|
+
apiMiddlewares.push(input);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const getMiddlewares = (): CollectMiddlewaresResult => ({
|
|
71
|
+
web: webMiddlewares,
|
|
72
|
+
api: apiMiddlewares,
|
|
73
|
+
});
|
|
74
|
+
return {
|
|
75
|
+
getMiddlewares,
|
|
76
|
+
addWebMiddleware,
|
|
77
|
+
addAPIMiddleware,
|
|
78
|
+
};
|
|
79
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
import SocketServer from '../src/dev-tools/socket-server';
|
|
3
|
+
|
|
4
|
+
describe('test dev tools', () => {
|
|
5
|
+
test('should socket server work correctly', () => {
|
|
6
|
+
const socketServer: any = new SocketServer({
|
|
7
|
+
client: {
|
|
8
|
+
port: '8080',
|
|
9
|
+
overlay: false,
|
|
10
|
+
logging: 'error',
|
|
11
|
+
path: '/',
|
|
12
|
+
host: '127.0.0.1',
|
|
13
|
+
},
|
|
14
|
+
dev: {
|
|
15
|
+
writeToDisk: false,
|
|
16
|
+
},
|
|
17
|
+
watch: true,
|
|
18
|
+
hot: true,
|
|
19
|
+
liveReload: true,
|
|
20
|
+
});
|
|
21
|
+
const app = http
|
|
22
|
+
.createServer((req, res) => {
|
|
23
|
+
res.end();
|
|
24
|
+
})
|
|
25
|
+
.listen(8080);
|
|
26
|
+
|
|
27
|
+
socketServer.prepare(app);
|
|
28
|
+
expect(socketServer.app).toBe(app);
|
|
29
|
+
expect(socketServer.stats).toBeUndefined();
|
|
30
|
+
|
|
31
|
+
const mockStats = {
|
|
32
|
+
toJson: () => ({}),
|
|
33
|
+
};
|
|
34
|
+
socketServer.updateStats(mockStats);
|
|
35
|
+
expect(socketServer.stats).toBe(mockStats);
|
|
36
|
+
|
|
37
|
+
app.close();
|
|
38
|
+
});
|
|
39
|
+
});
|
package/tests/middleware.test.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/* eslint-disable max-nested-callbacks */
|
|
2
1
|
import path from 'path';
|
|
3
2
|
import EventEmitter from 'events';
|
|
4
3
|
import { Readable } from 'stream';
|
|
@@ -66,12 +65,15 @@ describe('test middleware create factory', () => {
|
|
|
66
65
|
let sourceServerPort = 8080;
|
|
67
66
|
let sourceServer: Server | null = null;
|
|
68
67
|
beforeAll(async () => {
|
|
68
|
+
let done: any;
|
|
69
|
+
const promise = new Promise(resolve => (done = resolve));
|
|
69
70
|
sourceServerPort = await portfinder.getPortPromise();
|
|
70
71
|
sourceServer = createServer((req, res) => {
|
|
71
72
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
72
73
|
res.write(req.url?.slice(1));
|
|
73
74
|
res.end();
|
|
74
|
-
}).listen(sourceServerPort);
|
|
75
|
+
}).listen(sourceServerPort, done);
|
|
76
|
+
return promise;
|
|
75
77
|
});
|
|
76
78
|
|
|
77
79
|
afterAll(() => {
|
|
@@ -83,7 +85,7 @@ describe('test middleware create factory', () => {
|
|
|
83
85
|
test('should proxy correctly use simply options', async () => {
|
|
84
86
|
const port = await portfinder.getPortPromise();
|
|
85
87
|
const middlewares = createProxyHandler({
|
|
86
|
-
'/simple': `http://
|
|
88
|
+
'/simple': `http://localhost:${sourceServerPort}`,
|
|
87
89
|
});
|
|
88
90
|
const proxyHandler = middlewares![0];
|
|
89
91
|
|
|
@@ -95,7 +97,7 @@ describe('test middleware create factory', () => {
|
|
|
95
97
|
}).listen(port);
|
|
96
98
|
|
|
97
99
|
try {
|
|
98
|
-
const { data } = await axios.get(`http://
|
|
100
|
+
const { data } = await axios.get(`http://localhost:${port}/simple`);
|
|
99
101
|
expect(data).toBe('simple');
|
|
100
102
|
} finally {
|
|
101
103
|
server.close();
|
|
@@ -106,7 +108,7 @@ describe('test middleware create factory', () => {
|
|
|
106
108
|
const port = await portfinder.getPortPromise();
|
|
107
109
|
const middlewares = createProxyHandler({
|
|
108
110
|
'/simple-obj': {
|
|
109
|
-
target: `http://
|
|
111
|
+
target: `http://localhost:${sourceServerPort}`,
|
|
110
112
|
},
|
|
111
113
|
});
|
|
112
114
|
const proxyHandler = middlewares![0];
|
|
@@ -119,7 +121,7 @@ describe('test middleware create factory', () => {
|
|
|
119
121
|
}).listen(port);
|
|
120
122
|
|
|
121
123
|
try {
|
|
122
|
-
const { data } = await axios.get(`http://
|
|
124
|
+
const { data } = await axios.get(`http://localhost:${port}/simple-obj`);
|
|
123
125
|
expect(data).toBe('simple-obj');
|
|
124
126
|
} finally {
|
|
125
127
|
server.close();
|
|
@@ -130,7 +132,7 @@ describe('test middleware create factory', () => {
|
|
|
130
132
|
const port = await portfinder.getPortPromise();
|
|
131
133
|
const middlewares = createProxyHandler({
|
|
132
134
|
context: '/context',
|
|
133
|
-
target: `http://
|
|
135
|
+
target: `http://localhost:${sourceServerPort}`,
|
|
134
136
|
});
|
|
135
137
|
const proxyHandler = middlewares![0];
|
|
136
138
|
|
|
@@ -142,7 +144,7 @@ describe('test middleware create factory', () => {
|
|
|
142
144
|
}).listen(port);
|
|
143
145
|
|
|
144
146
|
try {
|
|
145
|
-
const { data } = await axios.get(`http://
|
|
147
|
+
const { data } = await axios.get(`http://localhost:${port}/context`);
|
|
146
148
|
expect(data).toBe('context');
|
|
147
149
|
} finally {
|
|
148
150
|
server.close();
|
|
@@ -154,7 +156,7 @@ describe('test middleware create factory', () => {
|
|
|
154
156
|
const middlewares = createProxyHandler([
|
|
155
157
|
{
|
|
156
158
|
context: '/array',
|
|
157
|
-
target: `http://
|
|
159
|
+
target: `http://localhost:${sourceServerPort}`,
|
|
158
160
|
},
|
|
159
161
|
]);
|
|
160
162
|
const proxyHandler = middlewares![0];
|
|
@@ -167,7 +169,7 @@ describe('test middleware create factory', () => {
|
|
|
167
169
|
}).listen(port);
|
|
168
170
|
|
|
169
171
|
try {
|
|
170
|
-
const { data } = await axios.get(`http://
|
|
172
|
+
const { data } = await axios.get(`http://localhost:${port}/array`);
|
|
171
173
|
expect(data).toBe('array');
|
|
172
174
|
} finally {
|
|
173
175
|
server.close();
|
|
@@ -175,4 +177,3 @@ describe('test middleware create factory', () => {
|
|
|
175
177
|
});
|
|
176
178
|
});
|
|
177
179
|
});
|
|
178
|
-
/* eslint-enable max-nested-callbacks */
|
package/tests/server.test.ts
CHANGED
|
@@ -2,6 +2,7 @@ import path from 'path';
|
|
|
2
2
|
import { defaultsConfig, NormalizedConfig } from '@modern-js/core';
|
|
3
3
|
import { ModernServerContext, NextFunction } from '@modern-js/types';
|
|
4
4
|
import createServer, { Server } from '../src';
|
|
5
|
+
import { ModernServer } from '../src/server/modern-server';
|
|
5
6
|
|
|
6
7
|
describe('test server', () => {
|
|
7
8
|
test('should throw error when ', resolve => {
|
|
@@ -58,7 +59,7 @@ describe('test server', () => {
|
|
|
58
59
|
config: defaultsConfig as NormalizedConfig,
|
|
59
60
|
pwd: appDirectory,
|
|
60
61
|
});
|
|
61
|
-
const modernServer = server
|
|
62
|
+
const modernServer = (server as any).server;
|
|
62
63
|
|
|
63
64
|
const len = modernServer.handlers.length;
|
|
64
65
|
|
|
@@ -85,5 +86,16 @@ describe('test server', () => {
|
|
|
85
86
|
expect(newLen + 1).toBe(nextLen);
|
|
86
87
|
expect(modernServer.handlers[nextLen - 1]).toBe(asyncHandler);
|
|
87
88
|
});
|
|
89
|
+
|
|
90
|
+
test('should get request handler correctly', async () => {
|
|
91
|
+
const server = await createServer({
|
|
92
|
+
config: defaultsConfig as NormalizedConfig,
|
|
93
|
+
pwd: appDirectory,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const modernServer: ModernServer = (server as any).server;
|
|
97
|
+
const handler = modernServer.getRequestHandler();
|
|
98
|
+
expect(typeof handler === 'function').toBeTruthy();
|
|
99
|
+
});
|
|
88
100
|
});
|
|
89
101
|
});
|
package/tests/tsconfig.json
CHANGED
package/tests/utils.test.ts
CHANGED
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
mergeExtension,
|
|
4
4
|
toMessage,
|
|
5
5
|
createErrorDocument,
|
|
6
|
+
createMiddlewareCollecter,
|
|
6
7
|
} from '../src/utils';
|
|
7
8
|
|
|
8
9
|
describe('test server utils', () => {
|
|
@@ -37,4 +38,34 @@ describe('test server utils', () => {
|
|
|
37
38
|
expect(doc).toMatch('redirect');
|
|
38
39
|
expect(doc).toMatch('302: redirect');
|
|
39
40
|
});
|
|
41
|
+
|
|
42
|
+
describe('test middleware collector', () => {
|
|
43
|
+
test('shoule return web middleware correctly', () => {
|
|
44
|
+
const { addWebMiddleware, getMiddlewares } = createMiddlewareCollecter();
|
|
45
|
+
|
|
46
|
+
const before = getMiddlewares();
|
|
47
|
+
expect(before.web).toEqual([]);
|
|
48
|
+
|
|
49
|
+
const middleware = async () => {
|
|
50
|
+
// empty
|
|
51
|
+
};
|
|
52
|
+
addWebMiddleware(middleware);
|
|
53
|
+
const after = getMiddlewares();
|
|
54
|
+
expect(after.web).toEqual([middleware]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('shoule return api middleware correctly', () => {
|
|
58
|
+
const { addAPIMiddleware, getMiddlewares } = createMiddlewareCollecter();
|
|
59
|
+
|
|
60
|
+
const before = getMiddlewares();
|
|
61
|
+
expect(before.web).toEqual([]);
|
|
62
|
+
|
|
63
|
+
const middleware = async () => {
|
|
64
|
+
// empty
|
|
65
|
+
};
|
|
66
|
+
addAPIMiddleware(middleware);
|
|
67
|
+
const after = getMiddlewares();
|
|
68
|
+
expect(after.api).toEqual([middleware]);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
40
71
|
});
|