@modern-js/prod-server 1.0.3 → 1.0.6
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 +39 -0
- package/dist/js/modern/libs/render/cache/index.js +2 -4
- package/dist/js/modern/libs/render/cache/spr.js +1 -2
- package/dist/js/modern/libs/render/cache/util.js +2 -5
- package/dist/js/modern/server/index.js +13 -10
- package/dist/js/modern/server/modern-server.js +1 -2
- package/dist/js/modern/utils.js +1 -1
- package/dist/js/node/libs/render/cache/index.js +2 -4
- package/dist/js/node/libs/render/cache/spr.js +1 -2
- package/dist/js/node/libs/render/cache/util.js +2 -5
- package/dist/js/node/server/index.js +11 -8
- package/dist/js/node/server/modern-server.js +1 -2
- package/dist/js/node/utils.js +1 -1
- package/dist/types/libs/proxy.d.ts +3 -3
- package/dist/types/type.d.ts +3 -11
- package/jest.config.js +0 -1
- package/package.json +5 -5
- package/src/libs/proxy.ts +3 -3
- package/src/libs/render/cache/index.ts +0 -2
- package/src/libs/render/cache/spr.ts +0 -1
- package/src/libs/render/cache/util.ts +0 -3
- package/src/server/index.ts +8 -13
- package/src/server/modern-server.ts +2 -3
- package/src/type.ts +3 -10
- package/src/utils.ts +1 -1
- package/tests/fixtures/pure/package.json +1 -1
- package/tests/utils.test.ts +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,44 @@
|
|
|
1
1
|
# @modern-js/prod-server
|
|
2
2
|
|
|
3
|
+
## 1.0.6
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- bebb39b6: chore: improve devDependencies and peerDependencies
|
|
8
|
+
- 132f7b53: feat: move config declarations to @modern-js/core
|
|
9
|
+
- Updated dependencies [bebb39b6]
|
|
10
|
+
- Updated dependencies [132f7b53]
|
|
11
|
+
- @modern-js/server-core@1.2.5
|
|
12
|
+
- @modern-js/utils@1.3.7
|
|
13
|
+
|
|
14
|
+
## 1.0.5
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- d2d0fa11: fix: missing devServer.proxy typing
|
|
19
|
+
- Updated dependencies [a8df060e]
|
|
20
|
+
- Updated dependencies [c2046f37]
|
|
21
|
+
- Updated dependencies [18db013a]
|
|
22
|
+
- @modern-js/server-core@1.2.4
|
|
23
|
+
- @modern-js/utils@1.3.6
|
|
24
|
+
|
|
25
|
+
## 1.0.4
|
|
26
|
+
|
|
27
|
+
### Patch Changes
|
|
28
|
+
|
|
29
|
+
- d95f28c3: should enable babel register before server plugin require
|
|
30
|
+
- d95f28c3: remove server hook when api only
|
|
31
|
+
- 0923c182: fix static handler when static file not exist
|
|
32
|
+
- 2008fdbd: convert two packages server part, support server load plugin itself
|
|
33
|
+
- ca0bcf13: publish prod-server local
|
|
34
|
+
- Updated dependencies [5bf5868d]
|
|
35
|
+
- Updated dependencies [d95f28c3]
|
|
36
|
+
- Updated dependencies [2e8dec93]
|
|
37
|
+
- Updated dependencies [2008fdbd]
|
|
38
|
+
- Updated dependencies [2e8dec93]
|
|
39
|
+
- @modern-js/utils@1.3.5
|
|
40
|
+
- @modern-js/server-core@1.2.3
|
|
41
|
+
|
|
3
42
|
## 1.0.2
|
|
4
43
|
|
|
5
44
|
### Patch Changes
|
|
@@ -46,8 +46,7 @@ export default ((renderFn, ctx) => {
|
|
|
46
46
|
} else if (cacheFile.isStale) {
|
|
47
47
|
// if file is stale, request async
|
|
48
48
|
const render = withCoalescedInvoke(() => renderFn(context)).bind(null, namespaceHash('render', cacheFile.hash), []);
|
|
49
|
-
render()
|
|
50
|
-
.then(res => {
|
|
49
|
+
render().then(res => {
|
|
51
50
|
if (res.value && res.isOrigin) {
|
|
52
51
|
const {
|
|
53
52
|
cacheConfig
|
|
@@ -59,8 +58,7 @@ export default ((renderFn, ctx) => {
|
|
|
59
58
|
sprCache.del(cacheContext, cacheHash);
|
|
60
59
|
}
|
|
61
60
|
}
|
|
62
|
-
})
|
|
63
|
-
.catch(e => {
|
|
61
|
+
}).catch(e => {
|
|
64
62
|
sprCache.del(cacheContext, cacheHash);
|
|
65
63
|
ctx.error(ERROR_DIGEST.ERENDER, e);
|
|
66
64
|
});
|
|
@@ -221,8 +221,7 @@ class CacheManager {
|
|
|
221
221
|
let data = this.cache.get(cacheKey);
|
|
222
222
|
|
|
223
223
|
if (!data) {
|
|
224
|
-
const caches = await createPageCaches(MAX_CACHE_EACH_REQ);
|
|
225
|
-
|
|
224
|
+
const caches = await createPageCaches(MAX_CACHE_EACH_REQ);
|
|
226
225
|
data = this.createCacheContent(cacheConfig, caches);
|
|
227
226
|
}
|
|
228
227
|
|
|
@@ -39,7 +39,6 @@ export function withCoalescedInvoke(func) {
|
|
|
39
39
|
const entry = globalInvokeCache.get(key);
|
|
40
40
|
|
|
41
41
|
if (entry) {
|
|
42
|
-
// eslint-disable-next-line promise/prefer-await-to-then
|
|
43
42
|
return entry.then(res => ({
|
|
44
43
|
isOrigin: false,
|
|
45
44
|
value: res.value
|
|
@@ -50,15 +49,13 @@ export function withCoalescedInvoke(func) {
|
|
|
50
49
|
return func(...args);
|
|
51
50
|
}
|
|
52
51
|
|
|
53
|
-
const future = __wrapper()
|
|
54
|
-
.then(res => {
|
|
52
|
+
const future = __wrapper().then(res => {
|
|
55
53
|
globalInvokeCache.delete(key);
|
|
56
54
|
return {
|
|
57
55
|
isOrigin: true,
|
|
58
56
|
value: res
|
|
59
57
|
};
|
|
60
|
-
})
|
|
61
|
-
.catch(err => {
|
|
58
|
+
}).catch(err => {
|
|
62
59
|
globalInvokeCache.delete(key);
|
|
63
60
|
throw err;
|
|
64
61
|
});
|
|
@@ -5,8 +5,8 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
5
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
6
|
|
|
7
7
|
import path from 'path';
|
|
8
|
-
import { serverManager, AppContext, ConfigContext } from '@modern-js/server-core';
|
|
9
|
-
import {
|
|
8
|
+
import { serverManager, AppContext, ConfigContext, loadPlugins } from '@modern-js/server-core';
|
|
9
|
+
import { logger as defaultLogger, SHARED_DIR } from '@modern-js/utils';
|
|
10
10
|
import { metrics as defaultMetrics } from "../libs/metrics";
|
|
11
11
|
import { createProdServer } from "./modern-server-split";
|
|
12
12
|
export class Server {
|
|
@@ -67,26 +67,29 @@ export class Server {
|
|
|
67
67
|
options
|
|
68
68
|
} = this;
|
|
69
69
|
const {
|
|
70
|
-
plugins = []
|
|
70
|
+
plugins = [],
|
|
71
|
+
pwd,
|
|
72
|
+
config
|
|
71
73
|
} = options; // server app context for serve plugin
|
|
72
74
|
|
|
73
|
-
plugins
|
|
74
|
-
|
|
75
|
+
const loadedPlugins = loadPlugins(plugins, pwd);
|
|
76
|
+
loadedPlugins.forEach(p => {
|
|
77
|
+
serverManager.usePlugin(p);
|
|
75
78
|
});
|
|
76
79
|
const appContext = this.initAppContext();
|
|
77
80
|
serverManager.run(() => {
|
|
78
|
-
var
|
|
81
|
+
var _config$output;
|
|
79
82
|
|
|
80
|
-
ConfigContext.set(
|
|
83
|
+
ConfigContext.set(config);
|
|
81
84
|
AppContext.set(_objectSpread(_objectSpread({}, appContext), {}, {
|
|
82
|
-
distDirectory: path.join(
|
|
85
|
+
distDirectory: path.join(pwd, ((_config$output = config.output) === null || _config$output === void 0 ? void 0 : _config$output.path) || 'dist')
|
|
83
86
|
}));
|
|
84
87
|
});
|
|
85
88
|
return serverManager.init({});
|
|
86
89
|
}
|
|
87
90
|
|
|
88
91
|
initAppContext() {
|
|
89
|
-
var _config$
|
|
92
|
+
var _config$output2;
|
|
90
93
|
|
|
91
94
|
const {
|
|
92
95
|
options
|
|
@@ -101,7 +104,7 @@ export class Server {
|
|
|
101
104
|
}));
|
|
102
105
|
return {
|
|
103
106
|
appDirectory,
|
|
104
|
-
distDirectory: path.join(appDirectory, ((_config$
|
|
107
|
+
distDirectory: path.join(appDirectory, ((_config$output2 = config.output) === null || _config$output2 === void 0 ? void 0 : _config$output2.path) || 'dist'),
|
|
105
108
|
sharedDirectory: path.resolve(appDirectory, SHARED_DIR),
|
|
106
109
|
plugins: serverPlugins
|
|
107
110
|
};
|
package/dist/js/modern/utils.js
CHANGED
|
@@ -88,7 +88,7 @@ export const getStaticReg = (output = {}) => {
|
|
|
88
88
|
} = output;
|
|
89
89
|
const favicons = prepareFavicons(favicon, faviconByEntries);
|
|
90
90
|
const staticFiles = [cssPath, jsPath, mediaPath].filter(v => Boolean(v));
|
|
91
|
-
const staticPathRegExp = new RegExp(`^/(static/|upload/|favicon.ico|icon.png${favicons.length > 0 ? `|${favicons.join('|')}` : ''}
|
|
91
|
+
const staticPathRegExp = new RegExp(`^/(static/|upload/|favicon.ico|icon.png${favicons.length > 0 ? `|${favicons.join('|')}` : ''}${staticFiles.length > 0 ? `|${staticFiles.join('|')}` : ''})`);
|
|
92
92
|
return staticPathRegExp;
|
|
93
93
|
};
|
|
94
94
|
export const prepareFavicons = (favicon, faviconByEntries) => {
|
|
@@ -56,8 +56,7 @@ var _default = (renderFn, ctx) => {
|
|
|
56
56
|
} else if (cacheFile.isStale) {
|
|
57
57
|
// if file is stale, request async
|
|
58
58
|
const render = (0, _util.withCoalescedInvoke)(() => renderFn(context)).bind(null, (0, _util.namespaceHash)('render', cacheFile.hash), []);
|
|
59
|
-
render()
|
|
60
|
-
.then(res => {
|
|
59
|
+
render().then(res => {
|
|
61
60
|
if (res.value && res.isOrigin) {
|
|
62
61
|
const {
|
|
63
62
|
cacheConfig
|
|
@@ -69,8 +68,7 @@ var _default = (renderFn, ctx) => {
|
|
|
69
68
|
sprCache.del(cacheContext, cacheHash);
|
|
70
69
|
}
|
|
71
70
|
}
|
|
72
|
-
})
|
|
73
|
-
.catch(e => {
|
|
71
|
+
}).catch(e => {
|
|
74
72
|
sprCache.del(cacheContext, cacheHash);
|
|
75
73
|
ctx.error(_constants.ERROR_DIGEST.ERENDER, e);
|
|
76
74
|
});
|
|
@@ -237,8 +237,7 @@ class CacheManager {
|
|
|
237
237
|
let data = this.cache.get(cacheKey);
|
|
238
238
|
|
|
239
239
|
if (!data) {
|
|
240
|
-
const caches = await (0, _pageCaches.createPageCaches)(MAX_CACHE_EACH_REQ);
|
|
241
|
-
|
|
240
|
+
const caches = await (0, _pageCaches.createPageCaches)(MAX_CACHE_EACH_REQ);
|
|
242
241
|
data = this.createCacheContent(cacheConfig, caches);
|
|
243
242
|
}
|
|
244
243
|
|
|
@@ -64,7 +64,6 @@ function withCoalescedInvoke(func) {
|
|
|
64
64
|
const entry = globalInvokeCache.get(key);
|
|
65
65
|
|
|
66
66
|
if (entry) {
|
|
67
|
-
// eslint-disable-next-line promise/prefer-await-to-then
|
|
68
67
|
return entry.then(res => ({
|
|
69
68
|
isOrigin: false,
|
|
70
69
|
value: res.value
|
|
@@ -75,15 +74,13 @@ function withCoalescedInvoke(func) {
|
|
|
75
74
|
return func(...args);
|
|
76
75
|
}
|
|
77
76
|
|
|
78
|
-
const future = __wrapper()
|
|
79
|
-
.then(res => {
|
|
77
|
+
const future = __wrapper().then(res => {
|
|
80
78
|
globalInvokeCache.delete(key);
|
|
81
79
|
return {
|
|
82
80
|
isOrigin: true,
|
|
83
81
|
value: res
|
|
84
82
|
};
|
|
85
|
-
})
|
|
86
|
-
.catch(err => {
|
|
83
|
+
}).catch(err => {
|
|
87
84
|
globalInvokeCache.delete(key);
|
|
88
85
|
throw err;
|
|
89
86
|
});
|
|
@@ -82,21 +82,24 @@ class Server {
|
|
|
82
82
|
options
|
|
83
83
|
} = this;
|
|
84
84
|
const {
|
|
85
|
-
plugins = []
|
|
85
|
+
plugins = [],
|
|
86
|
+
pwd,
|
|
87
|
+
config
|
|
86
88
|
} = options; // server app context for serve plugin
|
|
87
89
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
+
const loadedPlugins = (0, _serverCore.loadPlugins)(plugins, pwd);
|
|
91
|
+
loadedPlugins.forEach(p => {
|
|
92
|
+
_serverCore.serverManager.usePlugin(p);
|
|
90
93
|
});
|
|
91
94
|
const appContext = this.initAppContext();
|
|
92
95
|
|
|
93
96
|
_serverCore.serverManager.run(() => {
|
|
94
|
-
var
|
|
97
|
+
var _config$output;
|
|
95
98
|
|
|
96
|
-
_serverCore.ConfigContext.set(
|
|
99
|
+
_serverCore.ConfigContext.set(config);
|
|
97
100
|
|
|
98
101
|
_serverCore.AppContext.set(_objectSpread(_objectSpread({}, appContext), {}, {
|
|
99
|
-
distDirectory: _path.default.join(
|
|
102
|
+
distDirectory: _path.default.join(pwd, ((_config$output = config.output) === null || _config$output === void 0 ? void 0 : _config$output.path) || 'dist')
|
|
100
103
|
}));
|
|
101
104
|
});
|
|
102
105
|
|
|
@@ -104,7 +107,7 @@ class Server {
|
|
|
104
107
|
}
|
|
105
108
|
|
|
106
109
|
initAppContext() {
|
|
107
|
-
var _config$
|
|
110
|
+
var _config$output2;
|
|
108
111
|
|
|
109
112
|
const {
|
|
110
113
|
options
|
|
@@ -119,7 +122,7 @@ class Server {
|
|
|
119
122
|
}));
|
|
120
123
|
return {
|
|
121
124
|
appDirectory,
|
|
122
|
-
distDirectory: _path.default.join(appDirectory, ((_config$
|
|
125
|
+
distDirectory: _path.default.join(appDirectory, ((_config$output2 = config.output) === null || _config$output2 === void 0 ? void 0 : _config$output2.path) || 'dist'),
|
|
123
126
|
sharedDirectory: _path.default.resolve(appDirectory, _utils.SHARED_DIR),
|
|
124
127
|
plugins: serverPlugins
|
|
125
128
|
};
|
package/dist/js/node/utils.js
CHANGED
|
@@ -114,7 +114,7 @@ const getStaticReg = (output = {}) => {
|
|
|
114
114
|
} = output;
|
|
115
115
|
const favicons = prepareFavicons(favicon, faviconByEntries);
|
|
116
116
|
const staticFiles = [cssPath, jsPath, mediaPath].filter(v => Boolean(v));
|
|
117
|
-
const staticPathRegExp = new RegExp(`^/(static/|upload/|favicon.ico|icon.png${favicons.length > 0 ? `|${favicons.join('|')}` : ''}
|
|
117
|
+
const staticPathRegExp = new RegExp(`^/(static/|upload/|favicon.ico|icon.png${favicons.length > 0 ? `|${favicons.join('|')}` : ''}${staticFiles.length > 0 ? `|${staticFiles.join('|')}` : ''})`);
|
|
118
118
|
return staticPathRegExp;
|
|
119
119
|
};
|
|
120
120
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NextFunction,
|
|
1
|
+
import { NextFunction, BffProxyOptions } from '@modern-js/types';
|
|
2
2
|
import { ModernServerContext } from './context';
|
|
3
|
-
export type {
|
|
4
|
-
export declare const createProxyHandler: (proxyOptions
|
|
3
|
+
export type { BffProxyOptions };
|
|
4
|
+
export declare const createProxyHandler: (proxyOptions?: BffProxyOptions | undefined) => ((ctx: ModernServerContext, next: NextFunction) => Promise<void>)[] | null;
|
package/dist/types/type.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { IncomingMessage, Server, ServerResponse } from 'http';
|
|
3
|
-
import { serverManager } from '@modern-js/server-core';
|
|
3
|
+
import { serverManager, ServerPlugin } from '@modern-js/server-core';
|
|
4
4
|
import type { NormalizedConfig } from '@modern-js/core';
|
|
5
5
|
import type { Metrics, Logger, NextFunction } from '@modern-js/types/server';
|
|
6
6
|
import { ModernRouteInterface } from './libs/route';
|
|
@@ -10,19 +10,11 @@ declare module 'http' {
|
|
|
10
10
|
metrics: Metrics;
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
|
-
declare
|
|
14
|
-
interface UserConfig {
|
|
15
|
-
bff?: {
|
|
16
|
-
proxy: Record<string, any>;
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
}
|
|
13
|
+
declare type Plugin = string | [string, any] | ServerPlugin;
|
|
20
14
|
export declare type ModernServerOptions = {
|
|
21
15
|
pwd: string;
|
|
22
16
|
config: NormalizedConfig;
|
|
23
|
-
plugins?:
|
|
24
|
-
pluginPath: string;
|
|
25
|
-
}[];
|
|
17
|
+
plugins?: Plugin[];
|
|
26
18
|
routes?: ModernRouteInterface[];
|
|
27
19
|
staticGenerate?: boolean;
|
|
28
20
|
loggerOptions?: Record<string, string>;
|
package/jest.config.js
CHANGED
|
@@ -2,7 +2,6 @@ const sharedConfig = require('@scripts/jest-config');
|
|
|
2
2
|
|
|
3
3
|
/** @type {import('@jest/types').Config.InitialOptions} */
|
|
4
4
|
module.exports = {
|
|
5
|
-
// eslint-disable-next-line node/no-unsupported-features/es-syntax
|
|
6
5
|
...sharedConfig,
|
|
7
6
|
testEnvironment: 'node',
|
|
8
7
|
rootDir: __dirname,
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.0.
|
|
14
|
+
"version": "1.0.6",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -28,9 +28,9 @@
|
|
|
28
28
|
}
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@modern-js/utils": "^1.3.
|
|
31
|
+
"@modern-js/utils": "^1.3.7",
|
|
32
32
|
"@babel/compat-data": "^7.17.0",
|
|
33
|
-
"@modern-js/server-core": "^1.2.
|
|
33
|
+
"@modern-js/server-core": "^1.2.5",
|
|
34
34
|
"axios": "^0.24.0",
|
|
35
35
|
"compare-versions": "^3.6.0",
|
|
36
36
|
"http-proxy-middleware": "^2.0.1",
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"cookie": "^0.4.2"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@modern-js/types": "^1.3.
|
|
48
|
-
"@modern-js/core": "
|
|
47
|
+
"@modern-js/types": "^1.3.6",
|
|
48
|
+
"@modern-js/core": "1.6.1",
|
|
49
49
|
"@scripts/jest-config": "0.0.0",
|
|
50
50
|
"@scripts/build": "0.0.0",
|
|
51
51
|
"del-cli": "^4.0.1",
|
package/src/libs/proxy.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
2
|
-
import { NextFunction,
|
|
2
|
+
import { NextFunction, BffProxyOptions } from '@modern-js/types';
|
|
3
3
|
import { formatProxyOptions } from '@modern-js/utils';
|
|
4
4
|
import { ModernServerContext } from './context';
|
|
5
5
|
|
|
6
|
-
export type {
|
|
6
|
+
export type { BffProxyOptions };
|
|
7
7
|
|
|
8
|
-
export const createProxyHandler = (proxyOptions
|
|
8
|
+
export const createProxyHandler = (proxyOptions?: BffProxyOptions) => {
|
|
9
9
|
if (!proxyOptions) {
|
|
10
10
|
return null;
|
|
11
11
|
}
|
|
@@ -49,7 +49,6 @@ export default (renderFn: RenderFunction, ctx: ModernServerContext) => {
|
|
|
49
49
|
);
|
|
50
50
|
|
|
51
51
|
render()
|
|
52
|
-
// eslint-disable-next-line promise/prefer-await-to-then
|
|
53
52
|
.then(res => {
|
|
54
53
|
if (res.value && res.isOrigin) {
|
|
55
54
|
const { cacheConfig } = context;
|
|
@@ -60,7 +59,6 @@ export default (renderFn: RenderFunction, ctx: ModernServerContext) => {
|
|
|
60
59
|
}
|
|
61
60
|
}
|
|
62
61
|
})
|
|
63
|
-
// eslint-disable-next-line promise/prefer-await-to-then
|
|
64
62
|
.catch(e => {
|
|
65
63
|
sprCache.del(cacheContext, cacheHash);
|
|
66
64
|
ctx.error(ERROR_DIGEST.ERENDER, e);
|
|
@@ -236,7 +236,6 @@ class CacheManager {
|
|
|
236
236
|
let data = this.cache.get(cacheKey);
|
|
237
237
|
if (!data) {
|
|
238
238
|
const caches = await createPageCaches(MAX_CACHE_EACH_REQ);
|
|
239
|
-
// eslint-disable-next-line require-atomic-updates
|
|
240
239
|
data = this.createCacheContent(cacheConfig, caches);
|
|
241
240
|
}
|
|
242
241
|
|
|
@@ -58,7 +58,6 @@ export function withCoalescedInvoke<F extends (...args: any[]) => Promise<any>>(
|
|
|
58
58
|
return async function (key: string, args: Parameters<F>) {
|
|
59
59
|
const entry = globalInvokeCache.get(key);
|
|
60
60
|
if (entry) {
|
|
61
|
-
// eslint-disable-next-line promise/prefer-await-to-then
|
|
62
61
|
return entry.then(res => ({
|
|
63
62
|
isOrigin: false,
|
|
64
63
|
value: res.value as UnwrapPromise<ReturnType<F>>,
|
|
@@ -70,12 +69,10 @@ export function withCoalescedInvoke<F extends (...args: any[]) => Promise<any>>(
|
|
|
70
69
|
}
|
|
71
70
|
|
|
72
71
|
const future = __wrapper()
|
|
73
|
-
// eslint-disable-next-line promise/prefer-await-to-then
|
|
74
72
|
.then(res => {
|
|
75
73
|
globalInvokeCache.delete(key);
|
|
76
74
|
return { isOrigin: true, value: res as UnwrapPromise<ReturnType<F>> };
|
|
77
75
|
})
|
|
78
|
-
// eslint-disable-next-line promise/prefer-await-to-then
|
|
79
76
|
.catch(err => {
|
|
80
77
|
globalInvokeCache.delete(key);
|
|
81
78
|
throw err;
|
package/src/server/index.ts
CHANGED
|
@@ -4,12 +4,9 @@ import {
|
|
|
4
4
|
serverManager,
|
|
5
5
|
AppContext,
|
|
6
6
|
ConfigContext,
|
|
7
|
+
loadPlugins,
|
|
7
8
|
} from '@modern-js/server-core';
|
|
8
|
-
import {
|
|
9
|
-
compatRequire,
|
|
10
|
-
logger as defaultLogger,
|
|
11
|
-
SHARED_DIR,
|
|
12
|
-
} from '@modern-js/utils';
|
|
9
|
+
import { logger as defaultLogger, SHARED_DIR } from '@modern-js/utils';
|
|
13
10
|
import type { UserConfig } from '@modern-js/core';
|
|
14
11
|
import { ISAppContext } from '@modern-js/types';
|
|
15
12
|
import {
|
|
@@ -88,22 +85,20 @@ export class Server {
|
|
|
88
85
|
serverManager.clear();
|
|
89
86
|
|
|
90
87
|
const { options } = this;
|
|
91
|
-
const { plugins = [] } = options;
|
|
88
|
+
const { plugins = [], pwd, config } = options;
|
|
92
89
|
|
|
93
90
|
// server app context for serve plugin
|
|
94
|
-
plugins
|
|
95
|
-
|
|
91
|
+
const loadedPlugins = loadPlugins(plugins, pwd);
|
|
92
|
+
loadedPlugins.forEach(p => {
|
|
93
|
+
serverManager.usePlugin(p);
|
|
96
94
|
});
|
|
97
95
|
|
|
98
96
|
const appContext = this.initAppContext();
|
|
99
97
|
serverManager.run(() => {
|
|
100
|
-
ConfigContext.set(
|
|
98
|
+
ConfigContext.set(config as UserConfig);
|
|
101
99
|
AppContext.set({
|
|
102
100
|
...appContext,
|
|
103
|
-
distDirectory: path.join(
|
|
104
|
-
options.pwd,
|
|
105
|
-
options.config.output?.path || 'dist',
|
|
106
|
-
),
|
|
101
|
+
distDirectory: path.join(pwd, config.output?.path || 'dist'),
|
|
107
102
|
});
|
|
108
103
|
});
|
|
109
104
|
|
|
@@ -35,7 +35,7 @@ import {
|
|
|
35
35
|
noop,
|
|
36
36
|
} from '../utils';
|
|
37
37
|
import * as reader from '../libs/render/reader';
|
|
38
|
-
import { createProxyHandler,
|
|
38
|
+
import { createProxyHandler, BffProxyOptions } from '../libs/proxy';
|
|
39
39
|
import { createContext, ModernServerContext } from '../libs/context';
|
|
40
40
|
import {
|
|
41
41
|
AGGRED_DIR,
|
|
@@ -144,7 +144,7 @@ export class ModernServer implements ModernServerInterface {
|
|
|
144
144
|
});
|
|
145
145
|
|
|
146
146
|
// proxy handler, each proxy has own handler
|
|
147
|
-
this.proxyHandler = createProxyHandler(conf.bff?.proxy as
|
|
147
|
+
this.proxyHandler = createProxyHandler(conf.bff?.proxy as BffProxyOptions);
|
|
148
148
|
if (this.proxyHandler) {
|
|
149
149
|
this.proxyHandler.forEach(handler => {
|
|
150
150
|
this.addHandler(handler);
|
|
@@ -547,7 +547,6 @@ export class ModernServer implements ModernServerInterface {
|
|
|
547
547
|
return next();
|
|
548
548
|
}
|
|
549
549
|
|
|
550
|
-
// eslint-disable-next-line promise/prefer-await-to-then
|
|
551
550
|
return handler(context, dispatch as NextFunction).catch(onError);
|
|
552
551
|
};
|
|
553
552
|
|
package/src/type.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Buffer } from 'buffer';
|
|
2
2
|
import { IncomingMessage, Server, ServerResponse } from 'http';
|
|
3
|
-
import { serverManager } from '@modern-js/server-core';
|
|
3
|
+
import { serverManager, ServerPlugin } from '@modern-js/server-core';
|
|
4
4
|
import type { NormalizedConfig } from '@modern-js/core';
|
|
5
5
|
import type { Metrics, Logger, NextFunction } from '@modern-js/types/server';
|
|
6
6
|
import { ModernRouteInterface } from './libs/route';
|
|
@@ -12,18 +12,11 @@ declare module 'http' {
|
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
interface UserConfig {
|
|
17
|
-
bff?: {
|
|
18
|
-
proxy: Record<string, any>;
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
15
|
+
type Plugin = string | [string, any] | ServerPlugin;
|
|
23
16
|
export type ModernServerOptions = {
|
|
24
17
|
pwd: string;
|
|
25
18
|
config: NormalizedConfig;
|
|
26
|
-
plugins?:
|
|
19
|
+
plugins?: Plugin[];
|
|
27
20
|
routes?: ModernRouteInterface[];
|
|
28
21
|
staticGenerate?: boolean;
|
|
29
22
|
loggerOptions?: Record<string, string>;
|
package/src/utils.ts
CHANGED
|
@@ -94,7 +94,7 @@ export const getStaticReg = (output: NormalizedConfig['output'] = {}) => {
|
|
|
94
94
|
const staticPathRegExp = new RegExp(
|
|
95
95
|
`^/(static/|upload/|favicon.ico|icon.png${
|
|
96
96
|
favicons.length > 0 ? `|${favicons.join('|')}` : ''
|
|
97
|
-
}
|
|
97
|
+
}${staticFiles.length > 0 ? `|${staticFiles.join('|')}` : ''})`,
|
|
98
98
|
);
|
|
99
99
|
|
|
100
100
|
return staticPathRegExp;
|
package/tests/utils.test.ts
CHANGED
|
@@ -79,8 +79,8 @@ describe('test server utils', () => {
|
|
|
79
79
|
describe('test create static reg', () => {
|
|
80
80
|
test('should test static path correctly', () => {
|
|
81
81
|
const reg = getStaticReg();
|
|
82
|
-
expect(reg.test('/static')).toBeTruthy();
|
|
83
|
-
expect(reg.test('/upload')).toBeTruthy();
|
|
82
|
+
expect(reg.test('/static/')).toBeTruthy();
|
|
83
|
+
expect(reg.test('/upload/')).toBeTruthy();
|
|
84
84
|
});
|
|
85
85
|
|
|
86
86
|
test('should test custom static path correctly', () => {
|