@modern-js/server 1.3.0 → 1.4.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 (121) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/dist/js/modern/dev-tools/babel/register.js +1 -0
  3. package/dist/js/modern/dev-tools/dev-server-plugin.js +1 -2
  4. package/dist/js/modern/dev-tools/mock/getMockData.js +24 -1
  5. package/dist/js/modern/dev-tools/mock/index.js +1 -26
  6. package/dist/js/modern/dev-tools/socket-server.js +4 -2
  7. package/dist/js/modern/dev-tools/watcher/index.js +6 -9
  8. package/dist/js/modern/dev-tools/watcher/stats-cache.js +32 -20
  9. package/dist/js/modern/libs/context/context.js +6 -0
  10. package/dist/js/modern/libs/hook-api/route.js +6 -4
  11. package/dist/js/modern/libs/render/index.js +1 -0
  12. package/dist/js/modern/libs/render/ssr.js +7 -2
  13. package/dist/js/modern/libs/route/index.js +0 -1
  14. package/dist/js/modern/libs/route/matcher.js +15 -3
  15. package/dist/js/modern/libs/route/route.js +1 -0
  16. package/dist/js/modern/server/dev-server/dev-server.js +18 -5
  17. package/dist/js/modern/server/index.js +5 -4
  18. package/dist/js/modern/server/modern-server-split.js +1 -1
  19. package/dist/js/modern/server/modern-server.js +9 -5
  20. package/dist/js/modern/utils.js +7 -0
  21. package/dist/js/node/dev-tools/babel/register.js +1 -0
  22. package/dist/js/node/dev-tools/dev-server-plugin.js +1 -2
  23. package/dist/js/node/dev-tools/mock/getMockData.js +29 -2
  24. package/dist/js/node/dev-tools/mock/index.js +5 -26
  25. package/dist/js/node/dev-tools/socket-server.js +4 -2
  26. package/dist/js/node/dev-tools/watcher/index.js +9 -7
  27. package/dist/js/node/dev-tools/watcher/stats-cache.js +33 -20
  28. package/dist/js/node/libs/context/context.js +6 -0
  29. package/dist/js/node/libs/hook-api/route.js +6 -4
  30. package/dist/js/node/libs/render/index.js +1 -0
  31. package/dist/js/node/libs/render/ssr.js +8 -2
  32. package/dist/js/node/libs/route/index.js +0 -1
  33. package/dist/js/node/libs/route/matcher.js +16 -3
  34. package/dist/js/node/libs/route/route.js +1 -0
  35. package/dist/js/node/server/dev-server/dev-server.js +20 -5
  36. package/dist/js/node/server/index.js +9 -6
  37. package/dist/js/node/server/modern-server-split.js +1 -1
  38. package/dist/js/node/server/modern-server.js +9 -5
  39. package/dist/js/node/utils.js +13 -2
  40. package/dist/types/dev-tools/mock/getMockData.d.ts +2 -1
  41. package/dist/types/dev-tools/socket-server.d.ts +1 -2
  42. package/dist/types/dev-tools/watcher/index.d.ts +3 -1
  43. package/dist/types/dev-tools/watcher/stats-cache.d.ts +3 -2
  44. package/dist/types/libs/context/context.d.ts +2 -0
  45. package/dist/types/libs/hook-api/route.d.ts +3 -2
  46. package/dist/types/libs/render/ssr.d.ts +1 -0
  47. package/dist/types/libs/route/matcher.d.ts +1 -1
  48. package/dist/types/libs/route/route.d.ts +1 -0
  49. package/dist/types/server/dev-server/dev-server-split.d.ts +3 -3
  50. package/dist/types/server/modern-server-split.d.ts +3 -3
  51. package/dist/types/server/modern-server.d.ts +1 -1
  52. package/dist/types/type.d.ts +6 -4
  53. package/dist/types/utils.d.ts +2 -1
  54. package/jest.config.js +1 -0
  55. package/package.json +12 -10
  56. package/tests/context.test.ts +12 -1
  57. package/tests/dev.test.ts +306 -7
  58. package/tests/fixtures/mock/exist/config/mock/index.ts +11 -0
  59. package/tests/fixtures/mock/zero/config/mock/index.ts +1 -0
  60. package/tests/fixtures/pure/tsconfig.json +0 -1
  61. package/tests/fixtures/route-spec/dynamic.json +13 -0
  62. package/tests/fixtures/ssr/bundle.js +5 -0
  63. package/tests/fixtures/watch/a.ts +3 -0
  64. package/tests/fixtures/watch/index.ts +5 -0
  65. package/tests/fixtures/watch/stats.txt +1 -0
  66. package/tests/hook.test.ts +1 -1
  67. package/tests/route.test.ts +26 -3
  68. package/tests/server.test.ts +19 -0
  69. package/tests/ssr.test.ts +34 -0
  70. package/tests/utils.test.ts +6 -0
  71. package/tests/watcher.test.ts +98 -0
  72. package/src/constants.ts +0 -26
  73. package/src/dev-tools/babel/register.ts +0 -37
  74. package/src/dev-tools/dev-server-plugin.ts +0 -48
  75. package/src/dev-tools/https/global.d.ts +0 -3
  76. package/src/dev-tools/https/index.ts +0 -12
  77. package/src/dev-tools/launch-editor/index.ts +0 -29
  78. package/src/dev-tools/mock/getMockData.ts +0 -109
  79. package/src/dev-tools/mock/index.ts +0 -63
  80. package/src/dev-tools/socket-server.ts +0 -192
  81. package/src/dev-tools/watcher/dependency-tree.ts +0 -94
  82. package/src/dev-tools/watcher/index.ts +0 -77
  83. package/src/dev-tools/watcher/stats-cache.ts +0 -53
  84. package/src/index.ts +0 -16
  85. package/src/libs/context/context.ts +0 -176
  86. package/src/libs/context/index.ts +0 -7
  87. package/src/libs/hook-api/route.ts +0 -38
  88. package/src/libs/hook-api/template.ts +0 -53
  89. package/src/libs/metrics.ts +0 -15
  90. package/src/libs/proxy.ts +0 -85
  91. package/src/libs/render/cache/__tests__/cache.fun.test.ts +0 -94
  92. package/src/libs/render/cache/__tests__/cache.test.ts +0 -240
  93. package/src/libs/render/cache/__tests__/cacheable.ts +0 -44
  94. package/src/libs/render/cache/__tests__/error-configuration.ts +0 -34
  95. package/src/libs/render/cache/__tests__/matched-cache.ts +0 -88
  96. package/src/libs/render/cache/index.ts +0 -75
  97. package/src/libs/render/cache/page-caches/index.ts +0 -11
  98. package/src/libs/render/cache/page-caches/lru.ts +0 -38
  99. package/src/libs/render/cache/spr.ts +0 -301
  100. package/src/libs/render/cache/type.ts +0 -59
  101. package/src/libs/render/cache/util.ts +0 -97
  102. package/src/libs/render/index.ts +0 -78
  103. package/src/libs/render/modern/browser-list.ts +0 -7
  104. package/src/libs/render/modern/index.ts +0 -41
  105. package/src/libs/render/modern/module.d.ts +0 -4
  106. package/src/libs/render/reader.ts +0 -119
  107. package/src/libs/render/ssr.ts +0 -62
  108. package/src/libs/render/static.ts +0 -52
  109. package/src/libs/render/type.ts +0 -38
  110. package/src/libs/route/index.ts +0 -77
  111. package/src/libs/route/matcher.ts +0 -93
  112. package/src/libs/route/route.ts +0 -32
  113. package/src/libs/serve-file.ts +0 -34
  114. package/src/server/dev-server/dev-server-split.ts +0 -41
  115. package/src/server/dev-server/dev-server.ts +0 -284
  116. package/src/server/dev-server/index.ts +0 -2
  117. package/src/server/index.ts +0 -163
  118. package/src/server/modern-server-split.ts +0 -97
  119. package/src/server/modern-server.ts +0 -636
  120. package/src/type.ts +0 -88
  121. package/src/utils.ts +0 -79
@@ -1,94 +0,0 @@
1
- import minimatch from 'minimatch';
2
-
3
- export const defaultIgnores = [
4
- '**/bower_components/**',
5
- '**/coverage/**',
6
- '**/node_modules/**',
7
- '**/.*/**',
8
- '**/*.d.ts',
9
- '**/*.log',
10
- ];
11
-
12
- export interface DependencyTreeOptions {
13
- root: string;
14
- ignore?: string[];
15
- }
16
-
17
- export interface TreeNode {
18
- module: NodeModule;
19
- parent: Set<TreeNode>;
20
- children: Set<TreeNode>;
21
- }
22
-
23
- /**
24
- * `require.cache` already is a dependency tree, however require cache's
25
- * `module.parent` is the module that first required. so we have to implement
26
- * a new tree which revisit the cache tree to find all parent node
27
- */
28
- export class DependencyTree {
29
- private readonly tree: Map<string, TreeNode>;
30
-
31
- private readonly ignore: string[];
32
-
33
- constructor() {
34
- this.tree = new Map<string, TreeNode>();
35
- this.ignore = [...defaultIgnores];
36
- }
37
-
38
- public getNode(path: string) {
39
- return this.tree.get(path);
40
- }
41
-
42
- /**
43
- * update dependency tree
44
- *
45
- * @param cache
46
- */
47
- public update(cache: any) {
48
- this.tree.clear();
49
-
50
- // insert all module that not ignored
51
- Object.keys(cache).forEach(path => {
52
- if (!this.shouldIgnore(path)) {
53
- const module = cache[path];
54
- this.tree.set(module.filename, {
55
- module,
56
- parent: new Set<TreeNode>(),
57
- children: new Set<TreeNode>(),
58
- });
59
- }
60
- });
61
-
62
- // update treeNode parent and children
63
- for (const treeNode of this.tree.values()) {
64
- const { parent } = treeNode.module;
65
- const { children } = treeNode.module;
66
-
67
- if (parent && !this.shouldIgnore(parent.filename)) {
68
- const parentTreeNode = this.tree.get(parent.filename)!;
69
- if (parentTreeNode) {
70
- treeNode.parent.add(parentTreeNode);
71
- }
72
- }
73
-
74
- children.forEach(child => {
75
- if (!this.shouldIgnore(child.filename)) {
76
- const childTreeNode = this.tree.get(child.filename)!;
77
- if (childTreeNode) {
78
- treeNode.children.add(childTreeNode);
79
- childTreeNode.parent.add(treeNode);
80
- }
81
- }
82
- });
83
- }
84
- }
85
-
86
- private shouldIgnore(path: string): boolean {
87
- return (
88
- !path ||
89
- Boolean(
90
- this.ignore.find(rule => minimatch.match([path], rule).length > 0),
91
- )
92
- );
93
- }
94
- }
@@ -1,77 +0,0 @@
1
- import chokidar, { FSWatcher } from 'chokidar';
2
- import { DependencyTree } from './dependency-tree';
3
- import { StatsCache } from './stats-cache';
4
-
5
- const getWatchedFiles = (watcher: chokidar.FSWatcher) => {
6
- const watched = watcher.getWatched();
7
- const files: string[] = [];
8
- Object.keys(watched).forEach(dir => {
9
- watched[dir].forEach((fileName: string) => {
10
- files.push(`${dir}/${fileName}`);
11
- });
12
- });
13
- return files;
14
- };
15
-
16
- export default class Watcher {
17
- private dependencyTree: DependencyTree | null = null;
18
-
19
- private watcher!: FSWatcher;
20
-
21
- public listen(files: string[], callback: (changed: string) => void) {
22
- const watched = files.filter(Boolean);
23
-
24
- const cache = new StatsCache();
25
- const watcher = chokidar.watch(watched, {
26
- // 初始化的时候不触发 add、addDir 事件
27
- ignoreInitial: true,
28
- });
29
-
30
- watcher.on('ready', () => {
31
- cache.add(getWatchedFiles(watcher));
32
- });
33
-
34
- watcher.on('change', changed => {
35
- if (cache.isDiff(changed)) {
36
- cache.refresh(changed);
37
- callback(changed);
38
- }
39
- });
40
-
41
- watcher.on('add', changed => {
42
- if (!cache.has(changed)) {
43
- cache.add([changed]);
44
- callback(changed);
45
- }
46
- });
47
-
48
- watcher.on('unlink', changed => {
49
- cache.del(changed);
50
- callback(changed);
51
- });
52
-
53
- this.watcher = watcher;
54
- }
55
-
56
- public createDepTree() {
57
- this.dependencyTree = new DependencyTree();
58
- }
59
-
60
- public updateDepTree() {
61
- this.dependencyTree?.update(require.cache);
62
- }
63
-
64
- public cleanDepCache(filepath: string) {
65
- const node = this.dependencyTree?.getNode(filepath);
66
- if (node && require.cache[filepath]) {
67
- delete require.cache[filepath];
68
- for (const parentNode of node.parent.values()) {
69
- this.cleanDepCache(parentNode.module.filename);
70
- }
71
- }
72
- }
73
-
74
- public close() {
75
- return this.watcher.close();
76
- }
77
- }
@@ -1,53 +0,0 @@
1
- import fs from 'fs';
2
-
3
- export class StatsCache {
4
- private readonly cachedStats: Record<string, number> = {};
5
-
6
- public add(files: string[]) {
7
- const { cachedStats } = this;
8
- for (const filename of files) {
9
- if (fs.existsSync(filename)) {
10
- const stat = fs.statSync(filename);
11
- if (stat.isFile() && !cachedStats[filename]) {
12
- cachedStats[filename] = this.sign(stat);
13
- }
14
- }
15
- }
16
- }
17
-
18
- public refresh(filename: string) {
19
- const { cachedStats } = this;
20
- if (fs.existsSync(filename)) {
21
- const stat = fs.statSync(filename);
22
- if (stat.isFile()) {
23
- cachedStats[filename] = this.sign(stat);
24
- }
25
- }
26
- }
27
-
28
- public del(filename: string) {
29
- if (this.cachedStats[filename]) {
30
- delete this.cachedStats[filename];
31
- }
32
- }
33
-
34
- public isDiff(filename: string) {
35
- const { cachedStats } = this;
36
- const stat = fs.statSync(filename);
37
- const cachedStat = cachedStats[filename];
38
-
39
- if (this.sign(stat) !== cachedStat) {
40
- return true;
41
- }
42
- return false;
43
- }
44
-
45
- public has(filename: string) {
46
- return Boolean(this.cachedStats[filename]);
47
- }
48
-
49
- // Todo size 其实有点问题,修改单个字符会导致触发不了 change
50
- private sign(stat: fs.Stats) {
51
- return stat.size;
52
- }
53
- }
package/src/index.ts DELETED
@@ -1,16 +0,0 @@
1
- import { ModernServerOptions } from './type';
2
- import { Server } from './server';
3
-
4
- export type { SSRServerContext } from './libs/render/type';
5
- export { Server };
6
- export type { ModernServerOptions };
7
-
8
- export default (options: ModernServerOptions): Promise<Server> => {
9
- if (options == null) {
10
- throw new Error('can not start mserver without options');
11
- }
12
-
13
- const server = new Server(options);
14
-
15
- return server.init();
16
- };
@@ -1,176 +0,0 @@
1
- import { IncomingMessage, ServerResponse } from 'http';
2
- import { URL } from 'url';
3
- import qs from 'querystring';
4
- import type {
5
- ModernServerContext as ModernServerContextInterface,
6
- Metrics,
7
- Logger,
8
- } from '@modern-js/types/server';
9
- import { toMessage } from '../../utils';
10
-
11
- export type ContextOptions = {
12
- logger?: Logger;
13
- metrics?: Metrics;
14
- };
15
-
16
- export class ModernServerContext implements ModernServerContextInterface {
17
- /**
18
- * http request
19
- */
20
- public req: IncomingMessage;
21
-
22
- /**
23
- * http response
24
- */
25
- public res: ServerResponse;
26
-
27
- /**
28
- * url params
29
- */
30
- public params: Record<string, string> = {};
31
-
32
- public logger: Logger;
33
-
34
- public metrics?: Metrics;
35
-
36
- constructor(req: IncomingMessage, res: ServerResponse) {
37
- this.req = req;
38
- this.res = res;
39
- this.logger = req.logger;
40
- this.metrics = req.metrics;
41
-
42
- this.bind();
43
- }
44
-
45
- private bind() {
46
- const { req, res } = this as any;
47
- req.get = (key: string) => this.getReqHeader(key);
48
- res.set = (key: string, value: any) => this.res.setHeader(key, value);
49
- }
50
-
51
- public setParams(params: Record<string, string>) {
52
- this.params = params;
53
- }
54
-
55
- public getReqHeader(key: string) {
56
- const { req } = this;
57
- const field = key.toLowerCase();
58
- switch (field) {
59
- case 'referer':
60
- case 'referrer':
61
- return req.headers.referrer || req.headers.referer || '';
62
- default:
63
- return req.headers[field] || '';
64
- }
65
- }
66
-
67
- /* request property */
68
- public get headers() {
69
- return this.req.headers;
70
- }
71
-
72
- public get method(): string {
73
- return this.req.method!;
74
- }
75
-
76
- public get url() {
77
- return this.req.url || '';
78
- }
79
-
80
- public set url(val) {
81
- this.req.url = val;
82
- }
83
-
84
- public get host() {
85
- let host = this.getReqHeader('X-Forwarded-Host');
86
- if (!host) {
87
- host = this.getReqHeader('Host');
88
- }
89
- return (host as string).split(/\s*,\s*/, 1)[0] || '';
90
- }
91
-
92
- public get protocol() {
93
- if ((this.req.socket as any).encrypted) {
94
- return 'https';
95
- }
96
-
97
- const proto = this.getReqHeader('X-Forwarded-Proto');
98
- return proto ? (proto as string).split(/\s*,\s*/, 1)[0] : 'http';
99
- }
100
-
101
- public get origin() {
102
- return `${this.protocol}://${this.host}`;
103
- }
104
-
105
- public get href() {
106
- return this.origin + this.url;
107
- }
108
-
109
- public get parsedURL() {
110
- const url = new URL(this.req.url!, this.origin);
111
- return url;
112
- }
113
-
114
- public get path() {
115
- return this.parsedURL.pathname;
116
- }
117
-
118
- public set path(p) {
119
- const url = new URL(this.req.url!, this.origin);
120
- // this should never happend
121
- if (!url || !p) {
122
- return;
123
- }
124
-
125
- if (url.pathname === p) {
126
- return;
127
- }
128
-
129
- url.pathname = p;
130
-
131
- this.url = url.toString();
132
- }
133
-
134
- public get querystring() {
135
- if (!this.req) {
136
- return '';
137
- }
138
- return this.parsedURL.search.replace(/^\?/, '') || '';
139
- }
140
-
141
- public get query() {
142
- const str = this.querystring;
143
- return qs.parse(str);
144
- }
145
-
146
- /* response property */
147
- public get status() {
148
- return this.res.statusCode;
149
- }
150
-
151
- public set status(statusCode) {
152
- this.res.statusCode = statusCode;
153
- }
154
-
155
- /**
156
- * 判断链接是否已经关闭
157
- */
158
- public resHasHandled(): boolean {
159
- return this.res.writableEnded;
160
- }
161
-
162
- public logInfo() {
163
- return {
164
- headers: this.headers,
165
- href: this.href,
166
- url: this.url,
167
- };
168
- }
169
-
170
- public error(dig: string, err: Error | string = '') {
171
- const message = toMessage(dig, err);
172
- const reqInfo = this.logInfo();
173
-
174
- this.logger.error(`${reqInfo.url} - ${message}`, reqInfo);
175
- }
176
- }
@@ -1,7 +0,0 @@
1
- import { IncomingMessage, ServerResponse } from 'http';
2
- import { ModernServerContext } from './context';
3
-
4
- export const createContext = (req: IncomingMessage, res: ServerResponse) =>
5
- new ModernServerContext(req, res);
6
-
7
- export { ModernServerContext };
@@ -1,38 +0,0 @@
1
- import { RouteMatchManager, RouteMatcher } from '../route';
2
-
3
- class RouteAPI {
4
- private readonly router: RouteMatchManager;
5
-
6
- private current: RouteMatcher;
7
-
8
- constructor(matched: RouteMatcher, router: RouteMatchManager) {
9
- this.current = matched;
10
- this.router = router;
11
- }
12
-
13
- public cur() {
14
- return this.current.generate();
15
- }
16
-
17
- public get(entryName: string) {
18
- const { router } = this;
19
- const matched = router.matchEntry(entryName);
20
- return matched ? matched.generate() : null;
21
- }
22
-
23
- public use(entryName: string) {
24
- const { router } = this;
25
- const matched = router.matchEntry(entryName);
26
- if (matched) {
27
- this.current = matched;
28
- return true;
29
- } else {
30
- return false;
31
- }
32
- }
33
- }
34
-
35
- export const createRouteAPI = (
36
- matched: RouteMatcher,
37
- router: RouteMatchManager,
38
- ) => new RouteAPI(matched, router);
@@ -1,53 +0,0 @@
1
- const RegList = {
2
- before: {
3
- head: '<head>',
4
- body: '<body>',
5
- },
6
- after: {
7
- head: '</head>',
8
- body: '</body>',
9
- },
10
- };
11
-
12
- class TemplateAPI {
13
- private content: string;
14
-
15
- constructor(content: string) {
16
- this.content = content;
17
- }
18
-
19
- public get() {
20
- return this.content;
21
- }
22
-
23
- public set(content: string) {
24
- this.content = content;
25
- }
26
-
27
- public prependHead(fragment: string) {
28
- const { head } = RegList.before;
29
- return this.replace(head, `${head}${fragment}`);
30
- }
31
-
32
- public appendHead(fragment: string) {
33
- const { head } = RegList.after;
34
- return this.replace(head, `${fragment}${head}`);
35
- }
36
-
37
- public prependBody(fragment: string) {
38
- const { body } = RegList.before;
39
- return this.replace(body, `${body}${fragment}`);
40
- }
41
-
42
- public appendBody(fragment: string) {
43
- const { body } = RegList.after;
44
- return this.replace(body, `${fragment}${body}`);
45
- }
46
-
47
- public replace(reg: RegExp | string, text: string) {
48
- this.content = this.content.replace(reg, text);
49
- return this;
50
- }
51
- }
52
-
53
- export const createTemplateAPI = (content: string) => new TemplateAPI(content);
@@ -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
- };