@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
|
@@ -53,12 +53,16 @@ async function loadServerConfigOld(pwd, configFile) {
|
|
|
53
53
|
const serverConfig = await requireConfig(serverConfigPath);
|
|
54
54
|
return serverConfig;
|
|
55
55
|
}
|
|
56
|
-
async function loadServerRuntimeConfig(pwd, oldServerFile = import_utils.DEFAULT_SERVER_CONFIG, newServerConfigPath) {
|
|
56
|
+
async function loadServerRuntimeConfig(pwd, oldServerFile = import_utils.DEFAULT_SERVER_CONFIG, newServerConfigPath, metaName) {
|
|
57
57
|
const newServerConfig = newServerConfigPath && await loadServerConfigNew(newServerConfigPath);
|
|
58
58
|
if (newServerConfig) {
|
|
59
59
|
return newServerConfig;
|
|
60
60
|
}
|
|
61
61
|
const oldServerConfig = await loadServerConfigOld(pwd, oldServerFile);
|
|
62
|
+
if (oldServerConfig) {
|
|
63
|
+
const meta = (0, import_utils.getMeta)(metaName);
|
|
64
|
+
console.warn(`${import_utils.chalk.red("\n[Warning]")} ${import_utils.chalk.yellow.bold(`\`${oldServerFile}\``)} is no longer maintained. To extend the server, please migrate to ${import_utils.chalk.yellow.bold(`\`server/${meta}.server.ts\``)};`);
|
|
65
|
+
}
|
|
62
66
|
return oldServerConfig;
|
|
63
67
|
}
|
|
64
68
|
function loadServerCliConfig(pwd, defaultConfig = {}) {
|
|
@@ -35,6 +35,7 @@ __export(node_exports, {
|
|
|
35
35
|
});
|
|
36
36
|
module.exports = __toCommonJS(node_exports);
|
|
37
37
|
var import_node_http = require("node:http");
|
|
38
|
+
var import_cloneable_readable = __toESM(require("cloneable-readable"));
|
|
38
39
|
var import_helper = require("./helper");
|
|
39
40
|
var import_install = require("./polyfills/install");
|
|
40
41
|
var import_stream = require("./polyfills/stream");
|
|
@@ -42,14 +43,26 @@ var import_polyfills = require("./polyfills");
|
|
|
42
43
|
(0, import_install.installGlobals)();
|
|
43
44
|
const createWebRequest = (req, res, body) => {
|
|
44
45
|
const headerRecord = [];
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
47
|
+
if (key.startsWith(":")) {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (Array.isArray(value)) {
|
|
51
|
+
for (const item of value) {
|
|
52
|
+
if (item !== void 0) {
|
|
53
|
+
headerRecord.push([
|
|
54
|
+
key,
|
|
55
|
+
item
|
|
56
|
+
]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
} else if (value !== void 0) {
|
|
60
|
+
if (typeof value === "string") {
|
|
61
|
+
headerRecord.push([
|
|
62
|
+
key,
|
|
63
|
+
value
|
|
64
|
+
]);
|
|
65
|
+
}
|
|
53
66
|
}
|
|
54
67
|
}
|
|
55
68
|
const { method } = req;
|
|
@@ -61,13 +74,36 @@ const createWebRequest = (req, res, body) => {
|
|
|
61
74
|
};
|
|
62
75
|
res.on("close", () => controller.abort("res closed"));
|
|
63
76
|
const url = `http://${req.headers.host}${req.url}`;
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
77
|
+
const needsRequestBody = body || !(method === "GET" || method === "HEAD");
|
|
78
|
+
const cloneableReq = needsRequestBody ? (0, import_cloneable_readable.default)(req) : null;
|
|
79
|
+
if (needsRequestBody) {
|
|
80
|
+
if (body) {
|
|
81
|
+
init.body = body;
|
|
82
|
+
} else {
|
|
83
|
+
const stream = cloneableReq.clone();
|
|
84
|
+
init.body = (0, import_stream.createReadableStreamFromReadable)(stream);
|
|
85
|
+
}
|
|
67
86
|
init.duplex = "half";
|
|
68
87
|
}
|
|
69
|
-
const
|
|
70
|
-
|
|
88
|
+
const originalRequest = new Request(url, init);
|
|
89
|
+
if (needsRequestBody) {
|
|
90
|
+
return new Proxy(originalRequest, {
|
|
91
|
+
get(target, prop) {
|
|
92
|
+
if ([
|
|
93
|
+
"json",
|
|
94
|
+
"text",
|
|
95
|
+
"blob",
|
|
96
|
+
"arrayBuffer",
|
|
97
|
+
"formData",
|
|
98
|
+
"body"
|
|
99
|
+
].includes(prop)) {
|
|
100
|
+
cloneableReq.resume();
|
|
101
|
+
}
|
|
102
|
+
return target[prop];
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return originalRequest;
|
|
71
107
|
};
|
|
72
108
|
const sendResponse = async (response, res) => {
|
|
73
109
|
var _response_headers_get;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var context_exports = {};
|
|
20
|
+
__export(context_exports, {
|
|
21
|
+
run: () => run,
|
|
22
|
+
useHonoContext: () => useHonoContext
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(context_exports);
|
|
25
|
+
var import_storage = require("./utils/storage");
|
|
26
|
+
const { run, useHonoContext } = (0, import_storage.createStorage)();
|
|
27
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
28
|
+
0 && (module.exports = {
|
|
29
|
+
run,
|
|
30
|
+
useHonoContext
|
|
31
|
+
});
|
package/dist/cjs/index.js
CHANGED
|
@@ -21,15 +21,19 @@ var src_exports = {};
|
|
|
21
21
|
__export(src_exports, {
|
|
22
22
|
AGGRED_DIR: () => import_constants.AGGRED_DIR,
|
|
23
23
|
ErrorDigest: () => import_utils.ErrorDigest,
|
|
24
|
+
Hono: () => import_hono.Hono,
|
|
24
25
|
createErrorHtml: () => import_utils.createErrorHtml,
|
|
25
26
|
createServerBase: () => import_serverBase.createServerBase,
|
|
26
27
|
getLoaderCtx: () => import_helper.getLoaderCtx,
|
|
27
|
-
onError: () => import_utils.onError
|
|
28
|
+
onError: () => import_utils.onError,
|
|
29
|
+
useHonoContext: () => import_context.useHonoContext
|
|
28
30
|
});
|
|
29
31
|
module.exports = __toCommonJS(src_exports);
|
|
30
32
|
var import_utils = require("./utils");
|
|
31
33
|
var import_constants = require("./constants");
|
|
32
34
|
var import_serverBase = require("./serverBase");
|
|
35
|
+
var import_context = require("./context");
|
|
36
|
+
var import_hono = require("hono");
|
|
33
37
|
var import_helper = require("./helper");
|
|
34
38
|
__reExport(src_exports, require("./plugins"), module.exports);
|
|
35
39
|
__reExport(src_exports, require("./types/plugins"), module.exports);
|
|
@@ -41,10 +45,12 @@ __reExport(src_exports, require("./types/requestHandler"), module.exports);
|
|
|
41
45
|
0 && (module.exports = {
|
|
42
46
|
AGGRED_DIR,
|
|
43
47
|
ErrorDigest,
|
|
48
|
+
Hono,
|
|
44
49
|
createErrorHtml,
|
|
45
50
|
createServerBase,
|
|
46
51
|
getLoaderCtx,
|
|
47
52
|
onError,
|
|
53
|
+
useHonoContext,
|
|
48
54
|
...require("./plugins"),
|
|
49
55
|
...require("./types/plugins"),
|
|
50
56
|
...require("./types/render"),
|
|
@@ -92,19 +92,14 @@ class CustomServer {
|
|
|
92
92
|
}
|
|
93
93
|
};
|
|
94
94
|
}
|
|
95
|
-
async getServerMiddleware(
|
|
95
|
+
async getServerMiddleware() {
|
|
96
96
|
const serverMiddleware = await this.serverMiddlewarePromise;
|
|
97
97
|
if (!serverMiddleware || !(0, import_utils.isFunction)(serverMiddleware) && !(0, import_utils.isArray)(serverMiddleware)) {
|
|
98
|
-
return
|
|
98
|
+
return;
|
|
99
99
|
}
|
|
100
100
|
if (Array.isArray(serverMiddleware)) {
|
|
101
101
|
const unstableMiddlewares = getServerMidFromUnstableMid(serverMiddleware);
|
|
102
|
-
return
|
|
103
|
-
...renderMiddlewares || [],
|
|
104
|
-
...unstableMiddlewares
|
|
105
|
-
];
|
|
106
|
-
} else if (renderMiddlewares) {
|
|
107
|
-
return renderMiddlewares;
|
|
102
|
+
return unstableMiddlewares;
|
|
108
103
|
}
|
|
109
104
|
return async (c, next) => {
|
|
110
105
|
var _c_env_node_res, _c_env_node, _c_env;
|
|
@@ -130,9 +125,8 @@ class CustomServer {
|
|
|
130
125
|
}
|
|
131
126
|
};
|
|
132
127
|
}
|
|
133
|
-
constructor(hooks,
|
|
128
|
+
constructor(hooks, pwd) {
|
|
134
129
|
this.hooks = hooks;
|
|
135
|
-
this.serverBase = serverBase;
|
|
136
130
|
const webExtension = [];
|
|
137
131
|
this.serverMiddlewarePromise = hooks.prepareWebServer.call({
|
|
138
132
|
pwd,
|
|
@@ -23,6 +23,7 @@ __export(plugins_exports, {
|
|
|
23
23
|
faviconPlugin: () => import_favicon.faviconPlugin,
|
|
24
24
|
getRenderHandler: () => import_render.getRenderHandler,
|
|
25
25
|
handleSetupResult: () => import_compat.handleSetupResult,
|
|
26
|
+
injectConfigMiddlewarePlugin: () => import_middlewares.injectConfigMiddlewarePlugin,
|
|
26
27
|
injectRenderHandlerPlugin: () => import_render.injectRenderHandlerPlugin,
|
|
27
28
|
injectServerTiming: () => import_monitors.injectServerTiming,
|
|
28
29
|
injectloggerPlugin: () => import_monitors.injectloggerPlugin,
|
|
@@ -38,6 +39,7 @@ var import_processedBy = require("./processedBy");
|
|
|
38
39
|
var import_log = require("./log");
|
|
39
40
|
var import_default = require("./default");
|
|
40
41
|
var import_compat = require("./compat");
|
|
42
|
+
var import_middlewares = require("./middlewares");
|
|
41
43
|
// Annotate the CommonJS export names for ESM import in node:
|
|
42
44
|
0 && (module.exports = {
|
|
43
45
|
compatPlugin,
|
|
@@ -45,6 +47,7 @@ var import_compat = require("./compat");
|
|
|
45
47
|
faviconPlugin,
|
|
46
48
|
getRenderHandler,
|
|
47
49
|
handleSetupResult,
|
|
50
|
+
injectConfigMiddlewarePlugin,
|
|
48
51
|
injectRenderHandlerPlugin,
|
|
49
52
|
injectServerTiming,
|
|
50
53
|
injectloggerPlugin,
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var middlewares_exports = {};
|
|
20
|
+
__export(middlewares_exports, {
|
|
21
|
+
injectConfigMiddlewarePlugin: () => injectConfigMiddlewarePlugin
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(middlewares_exports);
|
|
24
|
+
const injectConfigMiddlewarePlugin = (configMiddlewares = [], configRenderMiddlewares = []) => ({
|
|
25
|
+
name: "@modern-js/plugin-inject-config-middleware",
|
|
26
|
+
setup(api) {
|
|
27
|
+
return {
|
|
28
|
+
prepare() {
|
|
29
|
+
const { middlewares, renderMiddlewares } = api.useAppContext();
|
|
30
|
+
configMiddlewares.forEach((m) => {
|
|
31
|
+
middlewares.push(m);
|
|
32
|
+
});
|
|
33
|
+
configRenderMiddlewares.forEach((m) => {
|
|
34
|
+
renderMiddlewares.push(m);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
41
|
+
0 && (module.exports = {
|
|
42
|
+
injectConfigMiddlewarePlugin
|
|
43
|
+
});
|
|
@@ -33,15 +33,13 @@ const renderPlugin = () => ({
|
|
|
33
33
|
setup(api) {
|
|
34
34
|
return {
|
|
35
35
|
async prepare() {
|
|
36
|
-
|
|
37
|
-
const { middlewares, routes, render, distDirectory: pwd, serverBase } = api.useAppContext();
|
|
36
|
+
const { middlewares, routes, render, distDirectory: pwd, renderMiddlewares } = api.useAppContext();
|
|
38
37
|
const hooks = api.getHooks();
|
|
39
38
|
const config = api.useConfigContext();
|
|
40
39
|
if (!routes) {
|
|
41
40
|
return;
|
|
42
41
|
}
|
|
43
|
-
const customServer = new import_customServer.CustomServer(hooks,
|
|
44
|
-
const serverMiddleware = ((_config_render = config.render) === null || _config_render === void 0 ? void 0 : _config_render.middleware) && (0, import_customServer.getServerMidFromUnstableMid)(config.render.middleware);
|
|
42
|
+
const customServer = new import_customServer.CustomServer(hooks, pwd);
|
|
45
43
|
const pageRoutes = getPageRoutes(routes);
|
|
46
44
|
middlewares.push({
|
|
47
45
|
name: "page-latency",
|
|
@@ -59,7 +57,14 @@ const renderPlugin = () => ({
|
|
|
59
57
|
handler: customServerHookMiddleware
|
|
60
58
|
});
|
|
61
59
|
}
|
|
62
|
-
|
|
60
|
+
renderMiddlewares === null || renderMiddlewares === void 0 ? void 0 : renderMiddlewares.forEach((m) => {
|
|
61
|
+
middlewares.push({
|
|
62
|
+
name: m.name,
|
|
63
|
+
path: urlPath,
|
|
64
|
+
handler: m.handler
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
const customServerMiddleware = await customServer.getServerMiddleware();
|
|
63
68
|
customServerMiddleware && middlewares.push({
|
|
64
69
|
name: "custom-server-middleware",
|
|
65
70
|
path: urlPath,
|
|
@@ -28,7 +28,6 @@ const injectRenderHandlerPlugin = ({ staticGenerate, cacheConfig }) => ({
|
|
|
28
28
|
setup(api) {
|
|
29
29
|
return {
|
|
30
30
|
async prepare() {
|
|
31
|
-
var _config_render;
|
|
32
31
|
const { distDirectory: pwd, routes, metaName } = api.useAppContext();
|
|
33
32
|
const config = api.useConfigContext();
|
|
34
33
|
const hookRunner = api.useHookRunners();
|
|
@@ -47,7 +46,8 @@ const injectRenderHandlerPlugin = ({ staticGenerate, cacheConfig }) => ({
|
|
|
47
46
|
routes,
|
|
48
47
|
config,
|
|
49
48
|
metaName,
|
|
50
|
-
|
|
49
|
+
// TODO: support modern.server.ts cache config
|
|
50
|
+
cacheConfig,
|
|
51
51
|
staticGenerate,
|
|
52
52
|
onFallback
|
|
53
53
|
};
|
package/dist/cjs/serverBase.js
CHANGED
|
@@ -26,6 +26,7 @@ var import_class_private_method_get = require("@swc/helpers/_/_class_private_met
|
|
|
26
26
|
var import_class_private_method_init = require("@swc/helpers/_/_class_private_method_init");
|
|
27
27
|
var import_server = require("@modern-js/plugin-v2/server");
|
|
28
28
|
var import_hono = require("hono");
|
|
29
|
+
var import_context = require("./context");
|
|
29
30
|
var import_hooks = require("./plugins/compat/hooks");
|
|
30
31
|
var import_utils = require("./utils");
|
|
31
32
|
var _applyMiddlewares = /* @__PURE__ */ new WeakSet();
|
|
@@ -41,7 +42,6 @@ class ServerBase {
|
|
|
41
42
|
cliConfig,
|
|
42
43
|
serverConfig: serverConfig || {}
|
|
43
44
|
});
|
|
44
|
-
this.addPlugins(mergedConfig.plugins || []);
|
|
45
45
|
const { serverContext } = await import_server.server.run({
|
|
46
46
|
plugins: this.plugins,
|
|
47
47
|
options: this.options,
|
|
@@ -99,6 +99,7 @@ class ServerBase {
|
|
|
99
99
|
this.serverContext = null;
|
|
100
100
|
this.options = options;
|
|
101
101
|
this.app = new import_hono.Hono();
|
|
102
|
+
this.app.use("*", import_context.run);
|
|
102
103
|
}
|
|
103
104
|
}
|
|
104
105
|
function applyMiddlewares() {
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var storage_exports = {};
|
|
30
|
+
__export(storage_exports, {
|
|
31
|
+
createStorage: () => createStorage
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(storage_exports);
|
|
34
|
+
var ah = __toESM(require("async_hooks"));
|
|
35
|
+
const createStorage = () => {
|
|
36
|
+
let storage;
|
|
37
|
+
if (typeof ah.AsyncLocalStorage !== "undefined") {
|
|
38
|
+
storage = new ah.AsyncLocalStorage();
|
|
39
|
+
}
|
|
40
|
+
const run = (context, cb) => {
|
|
41
|
+
if (!storage) {
|
|
42
|
+
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
43
|
+
`);
|
|
44
|
+
}
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
46
|
+
storage.run(context, () => {
|
|
47
|
+
try {
|
|
48
|
+
return resolve(cb());
|
|
49
|
+
} catch (error) {
|
|
50
|
+
return reject(error);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
const useHonoContext = () => {
|
|
56
|
+
if (!storage) {
|
|
57
|
+
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
58
|
+
`);
|
|
59
|
+
}
|
|
60
|
+
const context = storage.getStore();
|
|
61
|
+
if (!context) {
|
|
62
|
+
throw new Error(`Can't call useContext out of server scope`);
|
|
63
|
+
}
|
|
64
|
+
return context;
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
run,
|
|
68
|
+
useHonoContext
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
72
|
+
0 && (module.exports = {
|
|
73
|
+
createStorage
|
|
74
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
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
3
|
import path from "path";
|
|
4
|
-
import { fs, DEFAULT_SERVER_CONFIG, OUTPUT_CONFIG_FILE, lodash as _, compatibleRequire, ensureAbsolutePath, requireExistModule } from "@modern-js/utils";
|
|
4
|
+
import { fs, DEFAULT_SERVER_CONFIG, OUTPUT_CONFIG_FILE, lodash as _, chalk, compatibleRequire, ensureAbsolutePath, getMeta, requireExistModule } from "@modern-js/utils";
|
|
5
5
|
import { parse } from "flatted";
|
|
6
6
|
var requireConfig = function() {
|
|
7
7
|
var _ref = _async_to_generator(function(serverConfigPath) {
|
|
@@ -82,12 +82,12 @@ function loadServerRuntimeConfig(pwd) {
|
|
|
82
82
|
}
|
|
83
83
|
function _loadServerRuntimeConfig() {
|
|
84
84
|
_loadServerRuntimeConfig = _async_to_generator(function(pwd) {
|
|
85
|
-
var oldServerFile, newServerConfigPath, newServerConfig, _tmp, oldServerConfig;
|
|
85
|
+
var oldServerFile, newServerConfigPath, metaName, newServerConfig, _tmp, oldServerConfig, meta;
|
|
86
86
|
var _arguments = arguments;
|
|
87
87
|
return _ts_generator(this, function(_state) {
|
|
88
88
|
switch (_state.label) {
|
|
89
89
|
case 0:
|
|
90
|
-
oldServerFile = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : DEFAULT_SERVER_CONFIG, newServerConfigPath = _arguments.length > 2 ? _arguments[2] : void 0;
|
|
90
|
+
oldServerFile = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : DEFAULT_SERVER_CONFIG, newServerConfigPath = _arguments.length > 2 ? _arguments[2] : void 0, metaName = _arguments.length > 3 ? _arguments[3] : void 0;
|
|
91
91
|
_tmp = newServerConfigPath;
|
|
92
92
|
if (!_tmp)
|
|
93
93
|
return [
|
|
@@ -115,6 +115,10 @@ function _loadServerRuntimeConfig() {
|
|
|
115
115
|
];
|
|
116
116
|
case 3:
|
|
117
117
|
oldServerConfig = _state.sent();
|
|
118
|
+
if (oldServerConfig) {
|
|
119
|
+
meta = getMeta(metaName);
|
|
120
|
+
console.warn("".concat(chalk.red("\n[Warning]"), " ").concat(chalk.yellow.bold("`".concat(oldServerFile, "`")), " is no longer maintained. To extend the server, please migrate to ").concat(chalk.yellow.bold("`server/".concat(meta, ".server.ts`")), ";"));
|
|
121
|
+
}
|
|
118
122
|
return [
|
|
119
123
|
2,
|
|
120
124
|
oldServerConfig
|
|
@@ -4,6 +4,7 @@ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
|
4
4
|
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
5
5
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
6
6
|
import { ServerResponse } from "node:http";
|
|
7
|
+
import cloneable from "cloneable-readable";
|
|
7
8
|
import { isResFinalized } from "./helper";
|
|
8
9
|
import { installGlobals } from "./polyfills/install";
|
|
9
10
|
import { createReadableStreamFromReadable, writeReadableStreamToWritable } from "./polyfills/stream";
|
|
@@ -11,14 +12,60 @@ import { writeReadableStreamToWritable as writeReadableStreamToWritable2 } from
|
|
|
11
12
|
installGlobals();
|
|
12
13
|
var createWebRequest = function(req, res, body) {
|
|
13
14
|
var headerRecord = [];
|
|
14
|
-
var
|
|
15
|
-
|
|
16
|
-
var
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
16
|
+
try {
|
|
17
|
+
for (var _iterator = Object.entries(req.headers)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
18
|
+
var _step_value = _sliced_to_array(_step.value, 2), key = _step_value[0], value = _step_value[1];
|
|
19
|
+
if (key.startsWith(":")) {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (Array.isArray(value)) {
|
|
23
|
+
var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = void 0;
|
|
24
|
+
try {
|
|
25
|
+
for (var _iterator1 = value[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true) {
|
|
26
|
+
var item = _step1.value;
|
|
27
|
+
if (item !== void 0) {
|
|
28
|
+
headerRecord.push([
|
|
29
|
+
key,
|
|
30
|
+
item
|
|
31
|
+
]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
} catch (err) {
|
|
35
|
+
_didIteratorError1 = true;
|
|
36
|
+
_iteratorError1 = err;
|
|
37
|
+
} finally {
|
|
38
|
+
try {
|
|
39
|
+
if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
|
|
40
|
+
_iterator1.return();
|
|
41
|
+
}
|
|
42
|
+
} finally {
|
|
43
|
+
if (_didIteratorError1) {
|
|
44
|
+
throw _iteratorError1;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
} else if (value !== void 0) {
|
|
49
|
+
if (typeof value === "string") {
|
|
50
|
+
headerRecord.push([
|
|
51
|
+
key,
|
|
52
|
+
value
|
|
53
|
+
]);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
} catch (err) {
|
|
58
|
+
_didIteratorError = true;
|
|
59
|
+
_iteratorError = err;
|
|
60
|
+
} finally {
|
|
61
|
+
try {
|
|
62
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
63
|
+
_iterator.return();
|
|
64
|
+
}
|
|
65
|
+
} finally {
|
|
66
|
+
if (_didIteratorError) {
|
|
67
|
+
throw _iteratorError;
|
|
68
|
+
}
|
|
22
69
|
}
|
|
23
70
|
}
|
|
24
71
|
var method = req.method;
|
|
@@ -32,13 +79,36 @@ var createWebRequest = function(req, res, body) {
|
|
|
32
79
|
return controller.abort("res closed");
|
|
33
80
|
});
|
|
34
81
|
var url = "http://".concat(req.headers.host).concat(req.url);
|
|
35
|
-
var
|
|
36
|
-
|
|
37
|
-
|
|
82
|
+
var needsRequestBody = body || !(method === "GET" || method === "HEAD");
|
|
83
|
+
var cloneableReq = needsRequestBody ? cloneable(req) : null;
|
|
84
|
+
if (needsRequestBody) {
|
|
85
|
+
if (body) {
|
|
86
|
+
init.body = body;
|
|
87
|
+
} else {
|
|
88
|
+
var stream = cloneableReq.clone();
|
|
89
|
+
init.body = createReadableStreamFromReadable(stream);
|
|
90
|
+
}
|
|
38
91
|
init.duplex = "half";
|
|
39
92
|
}
|
|
40
|
-
var
|
|
41
|
-
|
|
93
|
+
var originalRequest = new Request(url, init);
|
|
94
|
+
if (needsRequestBody) {
|
|
95
|
+
return new Proxy(originalRequest, {
|
|
96
|
+
get: function get(target, prop) {
|
|
97
|
+
if ([
|
|
98
|
+
"json",
|
|
99
|
+
"text",
|
|
100
|
+
"blob",
|
|
101
|
+
"arrayBuffer",
|
|
102
|
+
"formData",
|
|
103
|
+
"body"
|
|
104
|
+
].includes(prop)) {
|
|
105
|
+
cloneableReq.resume();
|
|
106
|
+
}
|
|
107
|
+
return target[prop];
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
return originalRequest;
|
|
42
112
|
};
|
|
43
113
|
var sendResponse = function() {
|
|
44
114
|
var _ref = _async_to_generator(function(response, res) {
|
package/dist/esm/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
|
};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
2
|
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
3
|
-
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
4
3
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
5
4
|
import { time } from "@modern-js/runtime-utils/time";
|
|
6
5
|
import { isArray, isFunction } from "@modern-js/utils";
|
|
@@ -17,10 +16,9 @@ var isHtmlResponse = function(response) {
|
|
|
17
16
|
};
|
|
18
17
|
var CustomServer = /* @__PURE__ */ function() {
|
|
19
18
|
"use strict";
|
|
20
|
-
function CustomServer2(hooks,
|
|
19
|
+
function CustomServer2(hooks, pwd) {
|
|
21
20
|
_class_call_check(this, CustomServer2);
|
|
22
21
|
this.hooks = hooks;
|
|
23
|
-
this.serverBase = serverBase;
|
|
24
22
|
var webExtension = [];
|
|
25
23
|
this.serverMiddlewarePromise = hooks.prepareWebServer.call({
|
|
26
24
|
pwd,
|
|
@@ -165,7 +163,7 @@ var CustomServer = /* @__PURE__ */ function() {
|
|
|
165
163
|
};
|
|
166
164
|
}();
|
|
167
165
|
};
|
|
168
|
-
_proto.getServerMiddleware = function getServerMiddleware(
|
|
166
|
+
_proto.getServerMiddleware = function getServerMiddleware() {
|
|
169
167
|
var _this = this;
|
|
170
168
|
return _async_to_generator(function() {
|
|
171
169
|
var serverMiddleware, unstableMiddlewares;
|
|
@@ -180,20 +178,14 @@ var CustomServer = /* @__PURE__ */ function() {
|
|
|
180
178
|
serverMiddleware = _state.sent();
|
|
181
179
|
if (!serverMiddleware || !isFunction(serverMiddleware) && !isArray(serverMiddleware)) {
|
|
182
180
|
return [
|
|
183
|
-
2
|
|
184
|
-
renderMiddlewares
|
|
181
|
+
2
|
|
185
182
|
];
|
|
186
183
|
}
|
|
187
184
|
if (Array.isArray(serverMiddleware)) {
|
|
188
185
|
unstableMiddlewares = getServerMidFromUnstableMid(serverMiddleware);
|
|
189
186
|
return [
|
|
190
187
|
2,
|
|
191
|
-
|
|
192
|
-
];
|
|
193
|
-
} else if (renderMiddlewares) {
|
|
194
|
-
return [
|
|
195
|
-
2,
|
|
196
|
-
renderMiddlewares
|
|
188
|
+
unstableMiddlewares
|
|
197
189
|
];
|
|
198
190
|
}
|
|
199
191
|
return [
|