@modern-js/server-core 2.67.2 → 2.67.4
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/dist/cjs/adapters/node/helper/loadConfig.js +5 -1
- package/dist/cjs/adapters/node/node.js +49 -13
- package/dist/cjs/context.js +31 -0
- package/dist/cjs/index.js +7 -1
- package/dist/cjs/plugins/customServer/index.js +4 -10
- package/dist/cjs/plugins/index.js +3 -0
- package/dist/cjs/plugins/middlewares.js +43 -0
- package/dist/cjs/plugins/render/index.js +10 -5
- package/dist/cjs/plugins/render/inject.js +2 -2
- package/dist/cjs/serverBase.js +2 -1
- package/dist/cjs/utils/storage.js +74 -0
- package/dist/esm/adapters/node/helper/loadConfig.js +7 -3
- package/dist/esm/adapters/node/node.js +83 -13
- package/dist/esm/context.js +6 -0
- package/dist/esm/index.js +5 -1
- package/dist/esm/plugins/customServer/index.js +4 -12
- package/dist/esm/plugins/index.js +2 -0
- package/dist/esm/plugins/middlewares.js +22 -0
- package/dist/esm/plugins/render/index.js +53 -29
- package/dist/esm/plugins/render/inject.js +3 -2
- package/dist/esm/serverBase.js +2 -1
- package/dist/esm/utils/storage.js +38 -0
- package/dist/esm-node/adapters/node/helper/loadConfig.js +6 -2
- package/dist/esm-node/adapters/node/node.js +49 -13
- package/dist/esm-node/context.js +6 -0
- package/dist/esm-node/index.js +5 -1
- package/dist/esm-node/plugins/customServer/index.js +4 -10
- package/dist/esm-node/plugins/index.js +2 -0
- package/dist/esm-node/plugins/middlewares.js +19 -0
- package/dist/esm-node/plugins/render/index.js +11 -6
- package/dist/esm-node/plugins/render/inject.js +2 -2
- package/dist/esm-node/serverBase.js +2 -1
- package/dist/esm-node/utils/storage.js +40 -0
- package/dist/types/adapters/node/helper/loadConfig.d.ts +1 -1
- package/dist/types/context.d.ts +3 -0
- package/dist/types/index.d.ts +3 -1
- package/dist/types/plugins/customServer/index.d.ts +2 -4
- package/dist/types/plugins/index.d.ts +1 -0
- package/dist/types/plugins/middlewares.d.ts +2 -0
- package/dist/types/types/plugins/base.d.ts +8 -10
- package/dist/types/types/plugins/index.d.ts +1 -1
- package/dist/types/types/plugins/new.d.ts +2 -2
- package/dist/types/types/server.d.ts +1 -1
- package/dist/types/utils/storage.d.ts +5 -0
- package/package.json +9 -7
|
@@ -5,12 +5,14 @@ import { processedByPlugin } from "./processedBy";
|
|
|
5
5
|
import { logPlugin } from "./log";
|
|
6
6
|
import { createDefaultPlugins } from "./default";
|
|
7
7
|
import { compatPlugin, handleSetupResult } from "./compat";
|
|
8
|
+
import { injectConfigMiddlewarePlugin } from "./middlewares";
|
|
8
9
|
export {
|
|
9
10
|
compatPlugin,
|
|
10
11
|
createDefaultPlugins,
|
|
11
12
|
faviconPlugin,
|
|
12
13
|
getRenderHandler,
|
|
13
14
|
handleSetupResult,
|
|
15
|
+
injectConfigMiddlewarePlugin,
|
|
14
16
|
injectRenderHandlerPlugin,
|
|
15
17
|
injectServerTiming,
|
|
16
18
|
injectloggerPlugin,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
var injectConfigMiddlewarePlugin = function() {
|
|
2
|
+
var configMiddlewares = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [], configRenderMiddlewares = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
|
|
3
|
+
return {
|
|
4
|
+
name: "@modern-js/plugin-inject-config-middleware",
|
|
5
|
+
setup: function setup(api) {
|
|
6
|
+
return {
|
|
7
|
+
prepare: function prepare() {
|
|
8
|
+
var _api_useAppContext = api.useAppContext(), middlewares = _api_useAppContext.middlewares, renderMiddlewares = _api_useAppContext.renderMiddlewares;
|
|
9
|
+
configMiddlewares.forEach(function(m) {
|
|
10
|
+
middlewares.push(m);
|
|
11
|
+
});
|
|
12
|
+
configRenderMiddlewares.forEach(function(m) {
|
|
13
|
+
renderMiddlewares.push(m);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export {
|
|
21
|
+
injectConfigMiddlewarePlugin
|
|
22
|
+
};
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
2
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
3
|
+
import { _ as _ts_values } from "@swc/helpers/_/_ts_values";
|
|
3
4
|
import { MAIN_ENTRY_NAME } from "@modern-js/utils/universal/constants";
|
|
4
5
|
import { getLoaderCtx } from "../../helper";
|
|
5
6
|
import { sortRoutes } from "../../utils";
|
|
6
|
-
import { CustomServer
|
|
7
|
+
import { CustomServer } from "../customServer";
|
|
7
8
|
import { requestLatencyMiddleware } from "../monitors";
|
|
8
9
|
export * from "./inject";
|
|
9
10
|
var renderPlugin = function() {
|
|
@@ -13,11 +14,11 @@ var renderPlugin = function() {
|
|
|
13
14
|
return {
|
|
14
15
|
prepare: function prepare() {
|
|
15
16
|
return _async_to_generator(function() {
|
|
16
|
-
var
|
|
17
|
+
var _api_useAppContext, middlewares, routes, render, pwd, renderMiddlewares, hooks, config, customServer, pageRoutes, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, err;
|
|
17
18
|
return _ts_generator(this, function(_state) {
|
|
18
19
|
switch (_state.label) {
|
|
19
20
|
case 0:
|
|
20
|
-
_api_useAppContext = api.useAppContext(), middlewares = _api_useAppContext.middlewares, routes = _api_useAppContext.routes, render = _api_useAppContext.render, pwd = _api_useAppContext.distDirectory,
|
|
21
|
+
_api_useAppContext = api.useAppContext(), middlewares = _api_useAppContext.middlewares, routes = _api_useAppContext.routes, render = _api_useAppContext.render, pwd = _api_useAppContext.distDirectory, renderMiddlewares = _api_useAppContext.renderMiddlewares;
|
|
21
22
|
hooks = api.getHooks();
|
|
22
23
|
config = api.useConfigContext();
|
|
23
24
|
if (!routes) {
|
|
@@ -25,8 +26,7 @@ var renderPlugin = function() {
|
|
|
25
26
|
2
|
|
26
27
|
];
|
|
27
28
|
}
|
|
28
|
-
customServer = new CustomServer(hooks,
|
|
29
|
-
serverMiddleware = ((_config_render = config.render) === null || _config_render === void 0 ? void 0 : _config_render.middleware) && getServerMidFromUnstableMid(config.render.middleware);
|
|
29
|
+
customServer = new CustomServer(hooks, pwd);
|
|
30
30
|
pageRoutes = getPageRoutes(routes);
|
|
31
31
|
middlewares.push({
|
|
32
32
|
name: "page-latency",
|
|
@@ -41,6 +41,51 @@ var renderPlugin = function() {
|
|
|
41
41
|
7,
|
|
42
42
|
8
|
|
43
43
|
]);
|
|
44
|
+
_loop = function() {
|
|
45
|
+
var route, _config_server, originUrlPath, _route_entryName, entryName, urlPath, customServerHookMiddleware, customServerMiddleware;
|
|
46
|
+
return _ts_generator(this, function(_state2) {
|
|
47
|
+
switch (_state2.label) {
|
|
48
|
+
case 0:
|
|
49
|
+
route = _step.value;
|
|
50
|
+
originUrlPath = route.urlPath, _route_entryName = route.entryName, entryName = _route_entryName === void 0 ? MAIN_ENTRY_NAME : _route_entryName;
|
|
51
|
+
urlPath = originUrlPath.endsWith("/") ? "".concat(originUrlPath, "*") : "".concat(originUrlPath, "/*");
|
|
52
|
+
if (((_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.disableHook) !== true) {
|
|
53
|
+
customServerHookMiddleware = customServer.getHookMiddleware(entryName, routes);
|
|
54
|
+
middlewares.push({
|
|
55
|
+
name: "custom-server-hook",
|
|
56
|
+
path: urlPath,
|
|
57
|
+
handler: customServerHookMiddleware
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
renderMiddlewares === null || renderMiddlewares === void 0 ? void 0 : renderMiddlewares.forEach(function(m) {
|
|
61
|
+
middlewares.push({
|
|
62
|
+
name: m.name,
|
|
63
|
+
path: urlPath,
|
|
64
|
+
handler: m.handler
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
return [
|
|
68
|
+
4,
|
|
69
|
+
customServer.getServerMiddleware()
|
|
70
|
+
];
|
|
71
|
+
case 1:
|
|
72
|
+
customServerMiddleware = _state2.sent();
|
|
73
|
+
customServerMiddleware && middlewares.push({
|
|
74
|
+
name: "custom-server-middleware",
|
|
75
|
+
path: urlPath,
|
|
76
|
+
handler: customServerMiddleware
|
|
77
|
+
});
|
|
78
|
+
render && middlewares.push({
|
|
79
|
+
name: "render",
|
|
80
|
+
path: urlPath,
|
|
81
|
+
handler: createRenderHandler(render)
|
|
82
|
+
});
|
|
83
|
+
return [
|
|
84
|
+
2
|
|
85
|
+
];
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
};
|
|
44
89
|
_iterator = pageRoutes[Symbol.iterator]();
|
|
45
90
|
_state.label = 2;
|
|
46
91
|
case 2:
|
|
@@ -49,33 +94,12 @@ var renderPlugin = function() {
|
|
|
49
94
|
3,
|
|
50
95
|
5
|
|
51
96
|
];
|
|
52
|
-
route = _step.value;
|
|
53
|
-
originUrlPath = route.urlPath, _route_entryName = route.entryName, entryName = _route_entryName === void 0 ? MAIN_ENTRY_NAME : _route_entryName;
|
|
54
|
-
urlPath = originUrlPath.endsWith("/") ? "".concat(originUrlPath, "*") : "".concat(originUrlPath, "/*");
|
|
55
|
-
if (((_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.disableHook) !== true) {
|
|
56
|
-
customServerHookMiddleware = customServer.getHookMiddleware(entryName, routes);
|
|
57
|
-
middlewares.push({
|
|
58
|
-
name: "custom-server-hook",
|
|
59
|
-
path: urlPath,
|
|
60
|
-
handler: customServerHookMiddleware
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
97
|
return [
|
|
64
|
-
|
|
65
|
-
|
|
98
|
+
5,
|
|
99
|
+
_ts_values(_loop())
|
|
66
100
|
];
|
|
67
101
|
case 3:
|
|
68
|
-
|
|
69
|
-
customServerMiddleware && middlewares.push({
|
|
70
|
-
name: "custom-server-middleware",
|
|
71
|
-
path: urlPath,
|
|
72
|
-
handler: customServerMiddleware
|
|
73
|
-
});
|
|
74
|
-
render && middlewares.push({
|
|
75
|
-
name: "render",
|
|
76
|
-
path: urlPath,
|
|
77
|
-
handler: createRenderHandler(render)
|
|
78
|
-
});
|
|
102
|
+
_state.sent();
|
|
79
103
|
_state.label = 4;
|
|
80
104
|
case 4:
|
|
81
105
|
_iteratorNormalCompletion = true;
|
|
@@ -12,7 +12,7 @@ var injectRenderHandlerPlugin = function(param) {
|
|
|
12
12
|
return {
|
|
13
13
|
prepare: function prepare() {
|
|
14
14
|
return _async_to_generator(function() {
|
|
15
|
-
var
|
|
15
|
+
var _api_useAppContext, pwd, routes, metaName, config, hookRunner, onFallback, getRenderHandlerOptions, render;
|
|
16
16
|
return _ts_generator(this, function(_state) {
|
|
17
17
|
switch (_state.label) {
|
|
18
18
|
case 0:
|
|
@@ -54,7 +54,8 @@ var injectRenderHandlerPlugin = function(param) {
|
|
|
54
54
|
routes,
|
|
55
55
|
config,
|
|
56
56
|
metaName,
|
|
57
|
-
|
|
57
|
+
// TODO: support modern.server.ts cache config
|
|
58
|
+
cacheConfig,
|
|
58
59
|
staticGenerate,
|
|
59
60
|
onFallback
|
|
60
61
|
};
|
package/dist/esm/serverBase.js
CHANGED
|
@@ -7,6 +7,7 @@ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
|
7
7
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
8
8
|
import { server } from "@modern-js/plugin-v2/server";
|
|
9
9
|
import { Hono } from "hono";
|
|
10
|
+
import { run } from "./context";
|
|
10
11
|
import { handleSetupResult } from "./plugins/compat/hooks";
|
|
11
12
|
import { loadConfig } from "./utils";
|
|
12
13
|
var _applyMiddlewares = /* @__PURE__ */ new WeakSet();
|
|
@@ -19,6 +20,7 @@ var ServerBase = /* @__PURE__ */ function() {
|
|
|
19
20
|
this.serverContext = null;
|
|
20
21
|
this.options = options;
|
|
21
22
|
this.app = new Hono();
|
|
23
|
+
this.app.use("*", run);
|
|
22
24
|
}
|
|
23
25
|
var _proto = ServerBase2.prototype;
|
|
24
26
|
_proto.init = function init() {
|
|
@@ -33,7 +35,6 @@ var ServerBase = /* @__PURE__ */ function() {
|
|
|
33
35
|
cliConfig,
|
|
34
36
|
serverConfig: serverConfig || {}
|
|
35
37
|
});
|
|
36
|
-
_this.addPlugins(mergedConfig.plugins || []);
|
|
37
38
|
return [
|
|
38
39
|
4,
|
|
39
40
|
server.run({
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import * as ah from "async_hooks";
|
|
2
|
+
var createStorage = function() {
|
|
3
|
+
var storage;
|
|
4
|
+
if (typeof ah.AsyncLocalStorage !== "undefined") {
|
|
5
|
+
storage = new ah.AsyncLocalStorage();
|
|
6
|
+
}
|
|
7
|
+
var run = function(context, cb) {
|
|
8
|
+
if (!storage) {
|
|
9
|
+
throw new Error("Unable to use async_hook, please confirm the node version >= 12.17\n ");
|
|
10
|
+
}
|
|
11
|
+
return new Promise(function(resolve, reject) {
|
|
12
|
+
storage.run(context, function() {
|
|
13
|
+
try {
|
|
14
|
+
return resolve(cb());
|
|
15
|
+
} catch (error) {
|
|
16
|
+
return reject(error);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
var useHonoContext = function() {
|
|
22
|
+
if (!storage) {
|
|
23
|
+
throw new Error("Unable to use async_hook, please confirm the node version >= 12.17\n ");
|
|
24
|
+
}
|
|
25
|
+
var context = storage.getStore();
|
|
26
|
+
if (!context) {
|
|
27
|
+
throw new Error("Can't call useContext out of server scope");
|
|
28
|
+
}
|
|
29
|
+
return context;
|
|
30
|
+
};
|
|
31
|
+
return {
|
|
32
|
+
run,
|
|
33
|
+
useHonoContext
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
export {
|
|
37
|
+
createStorage
|
|
38
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import path from "path";
|
|
2
|
-
import { fs, DEFAULT_SERVER_CONFIG, OUTPUT_CONFIG_FILE, lodash as _, compatibleRequire, ensureAbsolutePath, requireExistModule } from "@modern-js/utils";
|
|
2
|
+
import { fs, DEFAULT_SERVER_CONFIG, OUTPUT_CONFIG_FILE, lodash as _, chalk, compatibleRequire, ensureAbsolutePath, getMeta, requireExistModule } from "@modern-js/utils";
|
|
3
3
|
import { parse } from "flatted";
|
|
4
4
|
const requireConfig = async (serverConfigPath) => {
|
|
5
5
|
if (fs.pathExistsSync(serverConfigPath)) {
|
|
@@ -19,12 +19,16 @@ async function loadServerConfigOld(pwd, configFile) {
|
|
|
19
19
|
const serverConfig = await requireConfig(serverConfigPath);
|
|
20
20
|
return serverConfig;
|
|
21
21
|
}
|
|
22
|
-
async function loadServerRuntimeConfig(pwd, oldServerFile = DEFAULT_SERVER_CONFIG, newServerConfigPath) {
|
|
22
|
+
async function loadServerRuntimeConfig(pwd, oldServerFile = DEFAULT_SERVER_CONFIG, newServerConfigPath, metaName) {
|
|
23
23
|
const newServerConfig = newServerConfigPath && await loadServerConfigNew(newServerConfigPath);
|
|
24
24
|
if (newServerConfig) {
|
|
25
25
|
return newServerConfig;
|
|
26
26
|
}
|
|
27
27
|
const oldServerConfig = await loadServerConfigOld(pwd, oldServerFile);
|
|
28
|
+
if (oldServerConfig) {
|
|
29
|
+
const meta = getMeta(metaName);
|
|
30
|
+
console.warn(`${chalk.red("\n[Warning]")} ${chalk.yellow.bold(`\`${oldServerFile}\``)} is no longer maintained. To extend the server, please migrate to ${chalk.yellow.bold(`\`server/${meta}.server.ts\``)};`);
|
|
31
|
+
}
|
|
28
32
|
return oldServerConfig;
|
|
29
33
|
}
|
|
30
34
|
function loadServerCliConfig(pwd, defaultConfig = {}) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ServerResponse } from "node:http";
|
|
2
|
+
import cloneable from "cloneable-readable";
|
|
2
3
|
import { isResFinalized } from "./helper";
|
|
3
4
|
import { installGlobals } from "./polyfills/install";
|
|
4
5
|
import { createReadableStreamFromReadable, writeReadableStreamToWritable } from "./polyfills/stream";
|
|
@@ -6,14 +7,26 @@ import { writeReadableStreamToWritable as writeReadableStreamToWritable2 } from
|
|
|
6
7
|
installGlobals();
|
|
7
8
|
const createWebRequest = (req, res, body) => {
|
|
8
9
|
const headerRecord = [];
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
11
|
+
if (key.startsWith(":")) {
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
if (Array.isArray(value)) {
|
|
15
|
+
for (const item of value) {
|
|
16
|
+
if (item !== void 0) {
|
|
17
|
+
headerRecord.push([
|
|
18
|
+
key,
|
|
19
|
+
item
|
|
20
|
+
]);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
} else if (value !== void 0) {
|
|
24
|
+
if (typeof value === "string") {
|
|
25
|
+
headerRecord.push([
|
|
26
|
+
key,
|
|
27
|
+
value
|
|
28
|
+
]);
|
|
29
|
+
}
|
|
17
30
|
}
|
|
18
31
|
}
|
|
19
32
|
const { method } = req;
|
|
@@ -25,13 +38,36 @@ const createWebRequest = (req, res, body) => {
|
|
|
25
38
|
};
|
|
26
39
|
res.on("close", () => controller.abort("res closed"));
|
|
27
40
|
const url = `http://${req.headers.host}${req.url}`;
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
41
|
+
const needsRequestBody = body || !(method === "GET" || method === "HEAD");
|
|
42
|
+
const cloneableReq = needsRequestBody ? cloneable(req) : null;
|
|
43
|
+
if (needsRequestBody) {
|
|
44
|
+
if (body) {
|
|
45
|
+
init.body = body;
|
|
46
|
+
} else {
|
|
47
|
+
const stream = cloneableReq.clone();
|
|
48
|
+
init.body = createReadableStreamFromReadable(stream);
|
|
49
|
+
}
|
|
31
50
|
init.duplex = "half";
|
|
32
51
|
}
|
|
33
|
-
const
|
|
34
|
-
|
|
52
|
+
const originalRequest = new Request(url, init);
|
|
53
|
+
if (needsRequestBody) {
|
|
54
|
+
return new Proxy(originalRequest, {
|
|
55
|
+
get(target, prop) {
|
|
56
|
+
if ([
|
|
57
|
+
"json",
|
|
58
|
+
"text",
|
|
59
|
+
"blob",
|
|
60
|
+
"arrayBuffer",
|
|
61
|
+
"formData",
|
|
62
|
+
"body"
|
|
63
|
+
].includes(prop)) {
|
|
64
|
+
cloneableReq.resume();
|
|
65
|
+
}
|
|
66
|
+
return target[prop];
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return originalRequest;
|
|
35
71
|
};
|
|
36
72
|
const sendResponse = async (response, res) => {
|
|
37
73
|
var _response_headers_get;
|
package/dist/esm-node/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { createErrorHtml, onError, ErrorDigest } from "./utils";
|
|
2
2
|
import { AGGRED_DIR } from "./constants";
|
|
3
3
|
import { createServerBase } from "./serverBase";
|
|
4
|
+
import { useHonoContext } from "./context";
|
|
5
|
+
import { Hono } from "hono";
|
|
4
6
|
import { getLoaderCtx } from "./helper";
|
|
5
7
|
export * from "./plugins";
|
|
6
8
|
export * from "./types/plugins";
|
|
@@ -11,8 +13,10 @@ export * from "./types/requestHandler";
|
|
|
11
13
|
export {
|
|
12
14
|
AGGRED_DIR,
|
|
13
15
|
ErrorDigest,
|
|
16
|
+
Hono,
|
|
14
17
|
createErrorHtml,
|
|
15
18
|
createServerBase,
|
|
16
19
|
getLoaderCtx,
|
|
17
|
-
onError
|
|
20
|
+
onError,
|
|
21
|
+
useHonoContext
|
|
18
22
|
};
|
|
@@ -68,19 +68,14 @@ class CustomServer {
|
|
|
68
68
|
}
|
|
69
69
|
};
|
|
70
70
|
}
|
|
71
|
-
async getServerMiddleware(
|
|
71
|
+
async getServerMiddleware() {
|
|
72
72
|
const serverMiddleware = await this.serverMiddlewarePromise;
|
|
73
73
|
if (!serverMiddleware || !isFunction(serverMiddleware) && !isArray(serverMiddleware)) {
|
|
74
|
-
return
|
|
74
|
+
return;
|
|
75
75
|
}
|
|
76
76
|
if (Array.isArray(serverMiddleware)) {
|
|
77
77
|
const unstableMiddlewares = getServerMidFromUnstableMid(serverMiddleware);
|
|
78
|
-
return
|
|
79
|
-
...renderMiddlewares || [],
|
|
80
|
-
...unstableMiddlewares
|
|
81
|
-
];
|
|
82
|
-
} else if (renderMiddlewares) {
|
|
83
|
-
return renderMiddlewares;
|
|
78
|
+
return unstableMiddlewares;
|
|
84
79
|
}
|
|
85
80
|
return async (c, next) => {
|
|
86
81
|
var _c_env_node_res, _c_env_node, _c_env;
|
|
@@ -106,9 +101,8 @@ class CustomServer {
|
|
|
106
101
|
}
|
|
107
102
|
};
|
|
108
103
|
}
|
|
109
|
-
constructor(hooks,
|
|
104
|
+
constructor(hooks, pwd) {
|
|
110
105
|
this.hooks = hooks;
|
|
111
|
-
this.serverBase = serverBase;
|
|
112
106
|
const webExtension = [];
|
|
113
107
|
this.serverMiddlewarePromise = hooks.prepareWebServer.call({
|
|
114
108
|
pwd,
|
|
@@ -5,12 +5,14 @@ import { processedByPlugin } from "./processedBy";
|
|
|
5
5
|
import { logPlugin } from "./log";
|
|
6
6
|
import { createDefaultPlugins } from "./default";
|
|
7
7
|
import { compatPlugin, handleSetupResult } from "./compat";
|
|
8
|
+
import { injectConfigMiddlewarePlugin } from "./middlewares";
|
|
8
9
|
export {
|
|
9
10
|
compatPlugin,
|
|
10
11
|
createDefaultPlugins,
|
|
11
12
|
faviconPlugin,
|
|
12
13
|
getRenderHandler,
|
|
13
14
|
handleSetupResult,
|
|
15
|
+
injectConfigMiddlewarePlugin,
|
|
14
16
|
injectRenderHandlerPlugin,
|
|
15
17
|
injectServerTiming,
|
|
16
18
|
injectloggerPlugin,
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const injectConfigMiddlewarePlugin = (configMiddlewares = [], configRenderMiddlewares = []) => ({
|
|
2
|
+
name: "@modern-js/plugin-inject-config-middleware",
|
|
3
|
+
setup(api) {
|
|
4
|
+
return {
|
|
5
|
+
prepare() {
|
|
6
|
+
const { middlewares, renderMiddlewares } = api.useAppContext();
|
|
7
|
+
configMiddlewares.forEach((m) => {
|
|
8
|
+
middlewares.push(m);
|
|
9
|
+
});
|
|
10
|
+
configRenderMiddlewares.forEach((m) => {
|
|
11
|
+
renderMiddlewares.push(m);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
export {
|
|
18
|
+
injectConfigMiddlewarePlugin
|
|
19
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { MAIN_ENTRY_NAME } from "@modern-js/utils/universal/constants";
|
|
2
2
|
import { getLoaderCtx } from "../../helper";
|
|
3
3
|
import { sortRoutes } from "../../utils";
|
|
4
|
-
import { CustomServer
|
|
4
|
+
import { CustomServer } from "../customServer";
|
|
5
5
|
import { requestLatencyMiddleware } from "../monitors";
|
|
6
6
|
export * from "./inject";
|
|
7
7
|
const renderPlugin = () => ({
|
|
@@ -9,15 +9,13 @@ const renderPlugin = () => ({
|
|
|
9
9
|
setup(api) {
|
|
10
10
|
return {
|
|
11
11
|
async prepare() {
|
|
12
|
-
|
|
13
|
-
const { middlewares, routes, render, distDirectory: pwd, serverBase } = api.useAppContext();
|
|
12
|
+
const { middlewares, routes, render, distDirectory: pwd, renderMiddlewares } = api.useAppContext();
|
|
14
13
|
const hooks = api.getHooks();
|
|
15
14
|
const config = api.useConfigContext();
|
|
16
15
|
if (!routes) {
|
|
17
16
|
return;
|
|
18
17
|
}
|
|
19
|
-
const customServer = new CustomServer(hooks,
|
|
20
|
-
const serverMiddleware = ((_config_render = config.render) === null || _config_render === void 0 ? void 0 : _config_render.middleware) && getServerMidFromUnstableMid(config.render.middleware);
|
|
18
|
+
const customServer = new CustomServer(hooks, pwd);
|
|
21
19
|
const pageRoutes = getPageRoutes(routes);
|
|
22
20
|
middlewares.push({
|
|
23
21
|
name: "page-latency",
|
|
@@ -35,7 +33,14 @@ const renderPlugin = () => ({
|
|
|
35
33
|
handler: customServerHookMiddleware
|
|
36
34
|
});
|
|
37
35
|
}
|
|
38
|
-
|
|
36
|
+
renderMiddlewares === null || renderMiddlewares === void 0 ? void 0 : renderMiddlewares.forEach((m) => {
|
|
37
|
+
middlewares.push({
|
|
38
|
+
name: m.name,
|
|
39
|
+
path: urlPath,
|
|
40
|
+
handler: m.handler
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
const customServerMiddleware = await customServer.getServerMiddleware();
|
|
39
44
|
customServerMiddleware && middlewares.push({
|
|
40
45
|
name: "custom-server-middleware",
|
|
41
46
|
path: urlPath,
|
|
@@ -4,7 +4,6 @@ const injectRenderHandlerPlugin = ({ staticGenerate, cacheConfig }) => ({
|
|
|
4
4
|
setup(api) {
|
|
5
5
|
return {
|
|
6
6
|
async prepare() {
|
|
7
|
-
var _config_render;
|
|
8
7
|
const { distDirectory: pwd, routes, metaName } = api.useAppContext();
|
|
9
8
|
const config = api.useConfigContext();
|
|
10
9
|
const hookRunner = api.useHookRunners();
|
|
@@ -23,7 +22,8 @@ const injectRenderHandlerPlugin = ({ staticGenerate, cacheConfig }) => ({
|
|
|
23
22
|
routes,
|
|
24
23
|
config,
|
|
25
24
|
metaName,
|
|
26
|
-
|
|
25
|
+
// TODO: support modern.server.ts cache config
|
|
26
|
+
cacheConfig,
|
|
27
27
|
staticGenerate,
|
|
28
28
|
onFallback
|
|
29
29
|
};
|
|
@@ -2,6 +2,7 @@ import { _ as _class_private_method_get } from "@swc/helpers/_/_class_private_me
|
|
|
2
2
|
import { _ as _class_private_method_init } from "@swc/helpers/_/_class_private_method_init";
|
|
3
3
|
import { server } from "@modern-js/plugin-v2/server";
|
|
4
4
|
import { Hono } from "hono";
|
|
5
|
+
import { run } from "./context";
|
|
5
6
|
import { handleSetupResult } from "./plugins/compat/hooks";
|
|
6
7
|
import { loadConfig } from "./utils";
|
|
7
8
|
var _applyMiddlewares = /* @__PURE__ */ new WeakSet();
|
|
@@ -17,7 +18,6 @@ class ServerBase {
|
|
|
17
18
|
cliConfig,
|
|
18
19
|
serverConfig: serverConfig || {}
|
|
19
20
|
});
|
|
20
|
-
this.addPlugins(mergedConfig.plugins || []);
|
|
21
21
|
const { serverContext } = await server.run({
|
|
22
22
|
plugins: this.plugins,
|
|
23
23
|
options: this.options,
|
|
@@ -75,6 +75,7 @@ class ServerBase {
|
|
|
75
75
|
this.serverContext = null;
|
|
76
76
|
this.options = options;
|
|
77
77
|
this.app = new Hono();
|
|
78
|
+
this.app.use("*", run);
|
|
78
79
|
}
|
|
79
80
|
}
|
|
80
81
|
function applyMiddlewares() {
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as ah from "async_hooks";
|
|
2
|
+
const createStorage = () => {
|
|
3
|
+
let storage;
|
|
4
|
+
if (typeof ah.AsyncLocalStorage !== "undefined") {
|
|
5
|
+
storage = new ah.AsyncLocalStorage();
|
|
6
|
+
}
|
|
7
|
+
const run = (context, cb) => {
|
|
8
|
+
if (!storage) {
|
|
9
|
+
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
10
|
+
`);
|
|
11
|
+
}
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
storage.run(context, () => {
|
|
14
|
+
try {
|
|
15
|
+
return resolve(cb());
|
|
16
|
+
} catch (error) {
|
|
17
|
+
return reject(error);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
const useHonoContext = () => {
|
|
23
|
+
if (!storage) {
|
|
24
|
+
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
25
|
+
`);
|
|
26
|
+
}
|
|
27
|
+
const context = storage.getStore();
|
|
28
|
+
if (!context) {
|
|
29
|
+
throw new Error(`Can't call useContext out of server scope`);
|
|
30
|
+
}
|
|
31
|
+
return context;
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
run,
|
|
35
|
+
useHonoContext
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
export {
|
|
39
|
+
createStorage
|
|
40
|
+
};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { CliConfig, ServerConfig, UserConfig } from '../../../types';
|
|
2
|
-
export declare function loadServerRuntimeConfig(pwd: string, oldServerFile?: string, newServerConfigPath?: string): Promise<ServerConfig | undefined>;
|
|
2
|
+
export declare function loadServerRuntimeConfig(pwd: string, oldServerFile?: string, newServerConfigPath?: string, metaName?: string): Promise<ServerConfig | undefined>;
|
|
3
3
|
export declare function loadServerCliConfig(pwd: string, defaultConfig?: UserConfig): CliConfig;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -2,7 +2,9 @@ export { createErrorHtml, onError, ErrorDigest } from './utils';
|
|
|
2
2
|
export { AGGRED_DIR } from './constants';
|
|
3
3
|
export type { ServerBase, ServerBaseOptions } from './serverBase';
|
|
4
4
|
export { createServerBase } from './serverBase';
|
|
5
|
-
export
|
|
5
|
+
export { useHonoContext } from './context';
|
|
6
|
+
export { Hono } from 'hono';
|
|
7
|
+
export type { Middleware, MiddlewareHandler, Context, Next, HonoRequest as InternalRequest, ServerEnv, ServerManifest, ServerLoaderBundle, } from './types';
|
|
6
8
|
export { getLoaderCtx } from './helper';
|
|
7
9
|
export * from './plugins';
|
|
8
10
|
export * from './types/plugins';
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import type { ServerRoute, UnstableMiddleware } from '@modern-js/types';
|
|
2
2
|
import type { ServerNodeEnv } from '../../adapters/node/hono';
|
|
3
|
-
import type { ServerBase } from '../../serverBase';
|
|
4
3
|
import type { Middleware, ServerEnv, ServerPluginHooks } from '../../types';
|
|
5
4
|
export declare class CustomServer {
|
|
6
5
|
private hooks;
|
|
7
6
|
private serverMiddlewarePromise;
|
|
8
|
-
|
|
9
|
-
constructor(hooks: ServerPluginHooks, serverBase: ServerBase, pwd: string);
|
|
7
|
+
constructor(hooks: ServerPluginHooks, pwd: string);
|
|
10
8
|
getHookMiddleware(entryName: string, routes: ServerRoute[]): Middleware<ServerEnv>;
|
|
11
|
-
getServerMiddleware(
|
|
9
|
+
getServerMiddleware(): Promise<Middleware<ServerNodeEnv & ServerEnv> | Array<Middleware<ServerNodeEnv & ServerEnv>> | undefined>;
|
|
12
10
|
}
|
|
13
11
|
export declare function getServerMidFromUnstableMid(serverMiddleware: UnstableMiddleware[]): Array<Middleware<ServerNodeEnv & ServerEnv>>;
|
|
@@ -5,3 +5,4 @@ export { processedByPlugin } from './processedBy';
|
|
|
5
5
|
export { logPlugin } from './log';
|
|
6
6
|
export { createDefaultPlugins, type CreateDefaultPluginsOptions, } from './default';
|
|
7
7
|
export { compatPlugin, handleSetupResult } from './compat';
|
|
8
|
+
export { injectConfigMiddlewarePlugin } from './middlewares';
|