@modern-js/server-core 2.65.5 → 2.66.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +2 -5
- package/dist/cjs/plugins/compat/hooks.js +92 -0
- package/dist/cjs/plugins/compat/index.js +65 -0
- package/dist/cjs/plugins/customServer/index.js +11 -17
- package/dist/cjs/plugins/default.js +2 -0
- package/dist/cjs/plugins/index.js +5 -0
- package/dist/cjs/plugins/render/index.js +2 -2
- package/dist/cjs/serverBase.js +31 -40
- package/dist/cjs/types/index.js +2 -2
- package/dist/cjs/types/{plugin.js → plugins/base.js} +2 -2
- package/dist/cjs/types/plugins/index.js +24 -0
- package/dist/cjs/types/plugins/new.js +16 -0
- package/dist/cjs/types/plugins/old.js +16 -0
- package/dist/esm/index.js +1 -3
- package/dist/esm/plugins/compat/hooks.js +70 -0
- package/dist/esm/plugins/compat/index.js +45 -0
- package/dist/esm/plugins/customServer/index.js +30 -22
- package/dist/esm/plugins/default.js +2 -0
- package/dist/esm/plugins/index.js +3 -0
- package/dist/esm/plugins/render/index.js +3 -3
- package/dist/esm/serverBase.js +34 -38
- package/dist/esm/types/index.js +1 -1
- package/dist/esm/types/plugins/base.js +0 -0
- package/dist/esm/types/plugins/index.js +2 -0
- package/dist/esm/types/plugins/new.js +0 -0
- package/dist/esm/types/plugins/old.js +0 -0
- package/dist/esm-node/index.js +1 -3
- package/dist/esm-node/plugins/compat/hooks.js +66 -0
- package/dist/esm-node/plugins/compat/index.js +40 -0
- package/dist/esm-node/plugins/customServer/index.js +10 -16
- package/dist/esm-node/plugins/default.js +2 -0
- package/dist/esm-node/plugins/index.js +3 -0
- package/dist/esm-node/plugins/render/index.js +2 -2
- package/dist/esm-node/serverBase.js +31 -40
- package/dist/esm-node/types/index.js +1 -1
- package/dist/esm-node/types/plugins/index.js +2 -0
- package/dist/esm-node/types/plugins/old.js +0 -0
- package/dist/types/adapters/node/node.d.ts +0 -2
- package/dist/types/adapters/node/plugins/nodeServer.d.ts +3 -2
- package/dist/types/adapters/node/plugins/resource.d.ts +3 -3
- package/dist/types/adapters/node/plugins/static.d.ts +2 -2
- package/dist/types/adapters/node/polyfills/stream.d.ts +0 -2
- package/dist/types/index.d.ts +1 -2
- package/dist/types/plugins/compat/hooks.d.ts +8 -0
- package/dist/types/plugins/compat/index.d.ts +3 -0
- package/dist/types/plugins/customServer/index.d.ts +3 -3
- package/dist/types/plugins/default.d.ts +2 -2
- package/dist/types/plugins/favicon.d.ts +2 -2
- package/dist/types/plugins/index.d.ts +1 -0
- package/dist/types/plugins/log.d.ts +2 -2
- package/dist/types/plugins/monitors.d.ts +4 -4
- package/dist/types/plugins/processedBy.d.ts +2 -2
- package/dist/types/plugins/render/index.d.ts +2 -2
- package/dist/types/plugins/render/inject.d.ts +2 -2
- package/dist/types/plugins/render/ssrCache.d.ts +0 -1
- package/dist/types/plugins/render/ssrRender.d.ts +0 -1
- package/dist/types/plugins/route.d.ts +2 -2
- package/dist/types/serverBase.d.ts +13 -24
- package/dist/types/types/index.d.ts +1 -1
- package/dist/types/types/plugins/base.d.ts +79 -0
- package/dist/types/types/plugins/index.d.ts +3 -0
- package/dist/types/types/plugins/new.d.ts +34 -0
- package/dist/types/types/plugins/old.d.ts +56 -0
- package/dist/types/types/render.d.ts +0 -1
- package/package.json +8 -7
- package/dist/cjs/pluginManager.js +0 -106
- package/dist/esm/pluginManager.js +0 -140
- package/dist/esm-node/pluginManager.js +0 -82
- package/dist/types/pluginManager.d.ts +0 -31
- package/dist/types/types/plugin.d.ts +0 -150
- /package/dist/esm-node/types/{plugin.js → plugins/base.js} +0 -0
- /package/dist/{esm/types/plugin.js → esm-node/types/plugins/new.js} +0 -0
package/dist/cjs/index.js
CHANGED
|
@@ -21,7 +21,6 @@ var src_exports = {};
|
|
|
21
21
|
__export(src_exports, {
|
|
22
22
|
AGGRED_DIR: () => import_constants.AGGRED_DIR,
|
|
23
23
|
ErrorDigest: () => import_utils.ErrorDigest,
|
|
24
|
-
PluginManager: () => import_pluginManager.PluginManager,
|
|
25
24
|
createErrorHtml: () => import_utils.createErrorHtml,
|
|
26
25
|
createServerBase: () => import_serverBase.createServerBase,
|
|
27
26
|
getLoaderCtx: () => import_helper.getLoaderCtx,
|
|
@@ -31,10 +30,9 @@ module.exports = __toCommonJS(src_exports);
|
|
|
31
30
|
var import_utils = require("./utils");
|
|
32
31
|
var import_constants = require("./constants");
|
|
33
32
|
var import_serverBase = require("./serverBase");
|
|
34
|
-
var import_pluginManager = require("./pluginManager");
|
|
35
33
|
var import_helper = require("./helper");
|
|
36
34
|
__reExport(src_exports, require("./plugins"), module.exports);
|
|
37
|
-
__reExport(src_exports, require("./types/
|
|
35
|
+
__reExport(src_exports, require("./types/plugins"), module.exports);
|
|
38
36
|
__reExport(src_exports, require("./types/render"), module.exports);
|
|
39
37
|
__reExport(src_exports, require("@modern-js/plugin"), module.exports);
|
|
40
38
|
__reExport(src_exports, require("./types/config"), module.exports);
|
|
@@ -43,13 +41,12 @@ __reExport(src_exports, require("./types/requestHandler"), module.exports);
|
|
|
43
41
|
0 && (module.exports = {
|
|
44
42
|
AGGRED_DIR,
|
|
45
43
|
ErrorDigest,
|
|
46
|
-
PluginManager,
|
|
47
44
|
createErrorHtml,
|
|
48
45
|
createServerBase,
|
|
49
46
|
getLoaderCtx,
|
|
50
47
|
onError,
|
|
51
48
|
...require("./plugins"),
|
|
52
|
-
...require("./types/
|
|
49
|
+
...require("./types/plugins"),
|
|
53
50
|
...require("./types/render"),
|
|
54
51
|
...require("@modern-js/plugin"),
|
|
55
52
|
...require("./types/config"),
|
|
@@ -0,0 +1,92 @@
|
|
|
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 hooks_exports = {};
|
|
20
|
+
__export(hooks_exports, {
|
|
21
|
+
getHookRunners: () => getHookRunners,
|
|
22
|
+
handleSetupResult: () => handleSetupResult,
|
|
23
|
+
transformHookRunner: () => transformHookRunner
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(hooks_exports);
|
|
26
|
+
function getHookRunners(context) {
|
|
27
|
+
const { hooks } = context;
|
|
28
|
+
return {
|
|
29
|
+
config: (params) => {
|
|
30
|
+
return hooks.modifyConfig.call(params);
|
|
31
|
+
},
|
|
32
|
+
prepare: () => {
|
|
33
|
+
return hooks.onPrepare.call();
|
|
34
|
+
},
|
|
35
|
+
reset: (params) => {
|
|
36
|
+
return hooks.onReset.call(params);
|
|
37
|
+
},
|
|
38
|
+
fallback: (input) => {
|
|
39
|
+
return hooks.fallback.call(input);
|
|
40
|
+
},
|
|
41
|
+
prepareWebServer: (input) => {
|
|
42
|
+
return hooks.prepareWebServer.call(input);
|
|
43
|
+
},
|
|
44
|
+
prepareApiServer: (input) => {
|
|
45
|
+
return hooks.prepareApiServer.call(input);
|
|
46
|
+
},
|
|
47
|
+
afterMatch: (ctx) => {
|
|
48
|
+
return hooks.afterMatch.call(ctx);
|
|
49
|
+
},
|
|
50
|
+
afterRender: (ctx) => {
|
|
51
|
+
return hooks.afterRender.call(ctx);
|
|
52
|
+
},
|
|
53
|
+
afterStreamingRender: (ctx) => {
|
|
54
|
+
return hooks.afterStreamingRender.call(ctx);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function transformHookRunner(hookRunnerName) {
|
|
59
|
+
switch (hookRunnerName) {
|
|
60
|
+
case "config":
|
|
61
|
+
return "modifyConfig";
|
|
62
|
+
case "prepare":
|
|
63
|
+
return "onPrepare";
|
|
64
|
+
case "reset":
|
|
65
|
+
return "onReset";
|
|
66
|
+
default:
|
|
67
|
+
return hookRunnerName;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function handleSetupResult(setupResult, api) {
|
|
71
|
+
if (!setupResult) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
Object.keys(setupResult).forEach((key) => {
|
|
75
|
+
const fn = setupResult[key];
|
|
76
|
+
if (typeof fn === "function") {
|
|
77
|
+
const newAPI = transformHookRunner(key);
|
|
78
|
+
if (api[newAPI]) {
|
|
79
|
+
api[newAPI]((...params) => {
|
|
80
|
+
const res = fn(...params);
|
|
81
|
+
return res;
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
88
|
+
0 && (module.exports = {
|
|
89
|
+
getHookRunners,
|
|
90
|
+
handleSetupResult,
|
|
91
|
+
transformHookRunner
|
|
92
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
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 compat_exports = {};
|
|
20
|
+
__export(compat_exports, {
|
|
21
|
+
compatPlugin: () => compatPlugin,
|
|
22
|
+
handleSetupResult: () => import_hooks2.handleSetupResult
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(compat_exports);
|
|
25
|
+
var import_plugin_v2 = require("@modern-js/plugin-v2");
|
|
26
|
+
var import_hooks = require("./hooks");
|
|
27
|
+
var import_hooks2 = require("./hooks");
|
|
28
|
+
const compatPlugin = () => ({
|
|
29
|
+
name: "@modern-js/server-compat",
|
|
30
|
+
registryHooks: {
|
|
31
|
+
fallback: (0, import_plugin_v2.createAsyncHook)(),
|
|
32
|
+
prepareWebServer: (0, import_plugin_v2.createAsyncPipelineHook)(),
|
|
33
|
+
prepareApiServer: (0, import_plugin_v2.createAsyncPipelineHook)(),
|
|
34
|
+
afterMatch: (0, import_plugin_v2.createAsyncPipelineHook)(),
|
|
35
|
+
afterRender: (0, import_plugin_v2.createAsyncPipelineHook)(),
|
|
36
|
+
afterStreamingRender: (0, import_plugin_v2.createAsyncPipelineHook)()
|
|
37
|
+
},
|
|
38
|
+
_registryApi: (getServerContext, updateServerContext) => {
|
|
39
|
+
const getInternalContext = () => {
|
|
40
|
+
return getServerContext()._internalContext;
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
useConfigContext: () => {
|
|
44
|
+
return getInternalContext().config;
|
|
45
|
+
},
|
|
46
|
+
useAppContext: () => {
|
|
47
|
+
const { _internalContext, ...serverContext } = getServerContext();
|
|
48
|
+
return serverContext;
|
|
49
|
+
},
|
|
50
|
+
setAppContext: (context) => {
|
|
51
|
+
return updateServerContext(context);
|
|
52
|
+
},
|
|
53
|
+
useHookRunners: () => {
|
|
54
|
+
return (0, import_hooks.getHookRunners)(getInternalContext());
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
setup: () => {
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
62
|
+
0 && (module.exports = {
|
|
63
|
+
compatPlugin,
|
|
64
|
+
handleSetupResult
|
|
65
|
+
});
|
|
@@ -23,9 +23,10 @@ __export(customServer_exports, {
|
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(customServer_exports);
|
|
25
25
|
var import_time = require("@modern-js/runtime-utils/time");
|
|
26
|
+
var import_utils = require("@modern-js/utils");
|
|
26
27
|
var import_constants = require("../../constants");
|
|
27
28
|
var import_helper = require("../../helper");
|
|
28
|
-
var
|
|
29
|
+
var import_utils2 = require("../../utils");
|
|
29
30
|
var import_base = require("./base");
|
|
30
31
|
var import_context = require("./context");
|
|
31
32
|
const noop = () => {
|
|
@@ -42,9 +43,7 @@ class CustomServer {
|
|
|
42
43
|
const baseHookCtx = (0, import_base.createBaseHookContext)(c);
|
|
43
44
|
const afterMatchCtx = (0, import_context.getAfterMatchCtx)(entryName, baseHookCtx);
|
|
44
45
|
const getCost = (0, import_time.time)();
|
|
45
|
-
await this.
|
|
46
|
-
onLast: noop
|
|
47
|
-
});
|
|
46
|
+
await this.hooks.afterMatch.call(afterMatchCtx);
|
|
48
47
|
const cost = getCost();
|
|
49
48
|
cost && (monitors === null || monitors === void 0 ? void 0 : monitors.timing(import_constants.ServerTimings.SERVER_HOOK_AFTER_MATCH, cost));
|
|
50
49
|
const { url, status } = afterMatchCtx.router;
|
|
@@ -74,18 +73,15 @@ class CustomServer {
|
|
|
74
73
|
}
|
|
75
74
|
if (routeInfo.isStream) {
|
|
76
75
|
const afterStreamingRenderContext = (0, import_context.createAfterStreamingRenderContext)(baseHookCtx, routeInfo);
|
|
77
|
-
c.res = (0,
|
|
76
|
+
c.res = (0, import_utils2.transformResponse)(c.res, async (chunk) => {
|
|
78
77
|
const context = afterStreamingRenderContext(chunk);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
});
|
|
78
|
+
const { chunk: newChunk } = await this.hooks.afterStreamingRender.call(context);
|
|
79
|
+
return newChunk;
|
|
82
80
|
});
|
|
83
81
|
} else {
|
|
84
82
|
const afterRenderCtx = await (0, import_context.getAfterRenderCtx)(c, baseHookCtx, routeInfo);
|
|
85
83
|
const getCost2 = (0, import_time.time)();
|
|
86
|
-
await this.
|
|
87
|
-
onLast: noop
|
|
88
|
-
});
|
|
84
|
+
await this.hooks.afterRender.call(afterRenderCtx);
|
|
89
85
|
const cost2 = getCost2();
|
|
90
86
|
cost2 && (monitors === null || monitors === void 0 ? void 0 : monitors.timing(import_constants.ServerTimings.SERVER_HOOK_AFTER_RENDER, cost2));
|
|
91
87
|
if (afterRenderCtx.response.private_overrided) {
|
|
@@ -98,7 +94,7 @@ class CustomServer {
|
|
|
98
94
|
}
|
|
99
95
|
async getServerMiddleware(renderMiddlewares) {
|
|
100
96
|
const serverMiddleware = await this.serverMiddlewarePromise;
|
|
101
|
-
if (!serverMiddleware) {
|
|
97
|
+
if (!serverMiddleware || !(0, import_utils.isFunction)(serverMiddleware) && !(0, import_utils.isArray)(serverMiddleware)) {
|
|
102
98
|
return renderMiddlewares;
|
|
103
99
|
}
|
|
104
100
|
if (Array.isArray(serverMiddleware)) {
|
|
@@ -134,17 +130,15 @@ class CustomServer {
|
|
|
134
130
|
}
|
|
135
131
|
};
|
|
136
132
|
}
|
|
137
|
-
constructor(
|
|
138
|
-
this.
|
|
133
|
+
constructor(hooks, serverBase, pwd) {
|
|
134
|
+
this.hooks = hooks;
|
|
139
135
|
this.serverBase = serverBase;
|
|
140
136
|
const webExtension = [];
|
|
141
|
-
this.serverMiddlewarePromise =
|
|
137
|
+
this.serverMiddlewarePromise = hooks.prepareWebServer.call({
|
|
142
138
|
pwd,
|
|
143
139
|
config: {
|
|
144
140
|
middleware: webExtension
|
|
145
141
|
}
|
|
146
|
-
}, {
|
|
147
|
-
onLast: () => []
|
|
148
142
|
});
|
|
149
143
|
}
|
|
150
144
|
}
|
|
@@ -21,6 +21,7 @@ __export(default_exports, {
|
|
|
21
21
|
createDefaultPlugins: () => createDefaultPlugins
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(default_exports);
|
|
24
|
+
var import_compat = require("./compat");
|
|
24
25
|
var import_log = require("./log");
|
|
25
26
|
var import_monitors = require("./monitors");
|
|
26
27
|
var import_processedBy = require("./processedBy");
|
|
@@ -36,6 +37,7 @@ function createSilenceLogger() {
|
|
|
36
37
|
}
|
|
37
38
|
function createDefaultPlugins(options = {}) {
|
|
38
39
|
const plugins = [
|
|
40
|
+
(0, import_compat.compatPlugin)(),
|
|
39
41
|
(0, import_log.logPlugin)(),
|
|
40
42
|
(0, import_monitors.initMonitorsPlugin)(),
|
|
41
43
|
(0, import_render.injectRenderHandlerPlugin)(options),
|
|
@@ -18,9 +18,11 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var plugins_exports = {};
|
|
20
20
|
__export(plugins_exports, {
|
|
21
|
+
compatPlugin: () => import_compat.compatPlugin,
|
|
21
22
|
createDefaultPlugins: () => import_default.createDefaultPlugins,
|
|
22
23
|
faviconPlugin: () => import_favicon.faviconPlugin,
|
|
23
24
|
getRenderHandler: () => import_render.getRenderHandler,
|
|
25
|
+
handleSetupResult: () => import_compat.handleSetupResult,
|
|
24
26
|
injectRenderHandlerPlugin: () => import_render.injectRenderHandlerPlugin,
|
|
25
27
|
injectServerTiming: () => import_monitors.injectServerTiming,
|
|
26
28
|
injectloggerPlugin: () => import_monitors.injectloggerPlugin,
|
|
@@ -35,11 +37,14 @@ var import_monitors = require("./monitors");
|
|
|
35
37
|
var import_processedBy = require("./processedBy");
|
|
36
38
|
var import_log = require("./log");
|
|
37
39
|
var import_default = require("./default");
|
|
40
|
+
var import_compat = require("./compat");
|
|
38
41
|
// Annotate the CommonJS export names for ESM import in node:
|
|
39
42
|
0 && (module.exports = {
|
|
43
|
+
compatPlugin,
|
|
40
44
|
createDefaultPlugins,
|
|
41
45
|
faviconPlugin,
|
|
42
46
|
getRenderHandler,
|
|
47
|
+
handleSetupResult,
|
|
43
48
|
injectRenderHandlerPlugin,
|
|
44
49
|
injectServerTiming,
|
|
45
50
|
injectloggerPlugin,
|
|
@@ -35,12 +35,12 @@ const renderPlugin = () => ({
|
|
|
35
35
|
async prepare() {
|
|
36
36
|
var _config_render;
|
|
37
37
|
const { middlewares, routes, render, distDirectory: pwd, serverBase } = api.useAppContext();
|
|
38
|
-
const
|
|
38
|
+
const hooks = api.getHooks();
|
|
39
39
|
const config = api.useConfigContext();
|
|
40
40
|
if (!routes) {
|
|
41
41
|
return;
|
|
42
42
|
}
|
|
43
|
-
const customServer = new import_customServer.CustomServer(
|
|
43
|
+
const customServer = new import_customServer.CustomServer(hooks, serverBase, pwd);
|
|
44
44
|
const serverMiddleware = ((_config_render = config.render) === null || _config_render === void 0 ? void 0 : _config_render.middleware) && (0, import_customServer.getServerMidFromUnstableMid)(config.render.middleware);
|
|
45
45
|
const pageRoutes = getPageRoutes(routes);
|
|
46
46
|
middlewares.push({
|
package/dist/cjs/serverBase.js
CHANGED
|
@@ -24,26 +24,41 @@ __export(serverBase_exports, {
|
|
|
24
24
|
module.exports = __toCommonJS(serverBase_exports);
|
|
25
25
|
var import_class_private_method_get = require("@swc/helpers/_/_class_private_method_get");
|
|
26
26
|
var import_class_private_method_init = require("@swc/helpers/_/_class_private_method_init");
|
|
27
|
-
var
|
|
27
|
+
var import_server = require("@modern-js/plugin-v2/server");
|
|
28
28
|
var import_hono = require("hono");
|
|
29
|
-
var
|
|
30
|
-
var
|
|
29
|
+
var import_hooks = require("./plugins/compat/hooks");
|
|
30
|
+
var import_utils = require("./utils");
|
|
31
|
+
var _applyMiddlewares = /* @__PURE__ */ new WeakSet();
|
|
31
32
|
class ServerBase {
|
|
32
33
|
/**
|
|
33
|
-
*
|
|
34
|
-
* -
|
|
35
|
-
* -
|
|
36
|
-
* - 应用 middlewares
|
|
34
|
+
* Order
|
|
35
|
+
* - server runner
|
|
36
|
+
* - apply middlewares
|
|
37
37
|
*/
|
|
38
38
|
async init() {
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
const { serverConfig, config: cliConfig } = this.options;
|
|
40
|
+
const mergedConfig = (0, import_utils.loadConfig)({
|
|
41
|
+
cliConfig,
|
|
42
|
+
serverConfig: serverConfig || {}
|
|
43
|
+
});
|
|
44
|
+
this.addPlugins(mergedConfig.plugins || []);
|
|
45
|
+
const { serverContext } = await import_server.server.run({
|
|
46
|
+
plugins: this.plugins,
|
|
47
|
+
options: this.options,
|
|
48
|
+
config: mergedConfig,
|
|
49
|
+
handleSetupResult: import_hooks.handleSetupResult
|
|
50
|
+
});
|
|
51
|
+
serverContext.serverBase = this;
|
|
52
|
+
await serverContext.hooks.onPrepare.call();
|
|
53
|
+
this.serverContext = serverContext;
|
|
42
54
|
(0, import_class_private_method_get._)(this, _applyMiddlewares, applyMiddlewares).call(this);
|
|
43
55
|
return this;
|
|
44
56
|
}
|
|
45
57
|
addPlugins(plugins) {
|
|
46
|
-
this.
|
|
58
|
+
this.plugins.push(...plugins);
|
|
59
|
+
}
|
|
60
|
+
get hooks() {
|
|
61
|
+
return this.serverContext.hooks;
|
|
47
62
|
}
|
|
48
63
|
get all() {
|
|
49
64
|
return this.app.all.bind(this.app);
|
|
@@ -79,39 +94,15 @@ class ServerBase {
|
|
|
79
94
|
return this.app.onError.bind(this.app);
|
|
80
95
|
}
|
|
81
96
|
constructor(options) {
|
|
82
|
-
(0, import_class_private_method_init._)(this, _getAppContext);
|
|
83
97
|
(0, import_class_private_method_init._)(this, _applyMiddlewares);
|
|
98
|
+
this.plugins = [];
|
|
99
|
+
this.serverContext = null;
|
|
84
100
|
this.options = options;
|
|
85
|
-
const { config, serverConfig } = options;
|
|
86
|
-
const appContext = (0, import_class_private_method_get._)(this, _getAppContext, getAppContext).call(this);
|
|
87
|
-
this.appContext = appContext;
|
|
88
|
-
this.pluginManager = new import_pluginManager.PluginManager({
|
|
89
|
-
cliConfig: config,
|
|
90
|
-
appContext,
|
|
91
|
-
serverConfig
|
|
92
|
-
});
|
|
93
101
|
this.app = new import_hono.Hono();
|
|
94
102
|
}
|
|
95
103
|
}
|
|
96
|
-
function getAppContext() {
|
|
97
|
-
const { appContext: context, pwd, routes, metaName } = this.options;
|
|
98
|
-
const appContext = {
|
|
99
|
-
routes,
|
|
100
|
-
middlewares: [],
|
|
101
|
-
appDirectory: (context === null || context === void 0 ? void 0 : context.appDirectory) || "",
|
|
102
|
-
apiDirectory: context === null || context === void 0 ? void 0 : context.apiDirectory,
|
|
103
|
-
internalDirectory: (context === null || context === void 0 ? void 0 : context.internalDirectory) || "",
|
|
104
|
-
lambdaDirectory: context === null || context === void 0 ? void 0 : context.lambdaDirectory,
|
|
105
|
-
sharedDirectory: (context === null || context === void 0 ? void 0 : context.sharedDirectory) || "",
|
|
106
|
-
distDirectory: pwd,
|
|
107
|
-
plugins: [],
|
|
108
|
-
metaName: metaName || "modern-js",
|
|
109
|
-
serverBase: this
|
|
110
|
-
};
|
|
111
|
-
return (0, import_plugin.createContext)(appContext);
|
|
112
|
-
}
|
|
113
104
|
function applyMiddlewares() {
|
|
114
|
-
const { middlewares } = this.
|
|
105
|
+
const { middlewares } = this.serverContext;
|
|
115
106
|
const preMiddlewares = [];
|
|
116
107
|
const defaultMiddlewares = [];
|
|
117
108
|
const postMiddlewares = [];
|
|
@@ -169,8 +160,8 @@ function createServerBase(options) {
|
|
|
169
160
|
if (options == null) {
|
|
170
161
|
throw new Error("can not start server without options");
|
|
171
162
|
}
|
|
172
|
-
const
|
|
173
|
-
return
|
|
163
|
+
const server2 = new ServerBase(options);
|
|
164
|
+
return server2;
|
|
174
165
|
}
|
|
175
166
|
// Annotate the CommonJS export names for ESM import in node:
|
|
176
167
|
0 && (module.exports = {
|
package/dist/cjs/types/index.js
CHANGED
|
@@ -16,13 +16,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
16
16
|
var types_exports = {};
|
|
17
17
|
module.exports = __toCommonJS(types_exports);
|
|
18
18
|
__reExport(types_exports, require("./config"), module.exports);
|
|
19
|
-
__reExport(types_exports, require("./
|
|
19
|
+
__reExport(types_exports, require("./plugins"), module.exports);
|
|
20
20
|
__reExport(types_exports, require("./render"), module.exports);
|
|
21
21
|
__reExport(types_exports, require("./server"), module.exports);
|
|
22
22
|
// Annotate the CommonJS export names for ESM import in node:
|
|
23
23
|
0 && (module.exports = {
|
|
24
24
|
...require("./config"),
|
|
25
|
-
...require("./
|
|
25
|
+
...require("./plugins"),
|
|
26
26
|
...require("./render"),
|
|
27
27
|
...require("./server")
|
|
28
28
|
});
|
|
@@ -12,5 +12,5 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
12
12
|
return to;
|
|
13
13
|
};
|
|
14
14
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
-
var
|
|
16
|
-
module.exports = __toCommonJS(
|
|
15
|
+
var base_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(base_exports);
|
|
@@ -0,0 +1,24 @@
|
|
|
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 __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var plugins_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(plugins_exports);
|
|
18
|
+
__reExport(plugins_exports, require("./new"), module.exports);
|
|
19
|
+
__reExport(plugins_exports, require("./old"), module.exports);
|
|
20
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
21
|
+
0 && (module.exports = {
|
|
22
|
+
...require("./new"),
|
|
23
|
+
...require("./old")
|
|
24
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
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 __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var new_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(new_exports);
|
|
@@ -0,0 +1,16 @@
|
|
|
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 __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var old_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(old_exports);
|
package/dist/esm/index.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { createErrorHtml, onError, ErrorDigest } from "./utils";
|
|
2
2
|
import { AGGRED_DIR } from "./constants";
|
|
3
3
|
import { createServerBase } from "./serverBase";
|
|
4
|
-
import { PluginManager } from "./pluginManager";
|
|
5
4
|
import { getLoaderCtx } from "./helper";
|
|
6
5
|
export * from "./plugins";
|
|
7
|
-
export * from "./types/
|
|
6
|
+
export * from "./types/plugins";
|
|
8
7
|
export * from "./types/render";
|
|
9
8
|
export * from "@modern-js/plugin";
|
|
10
9
|
export * from "./types/config";
|
|
@@ -12,7 +11,6 @@ export * from "./types/requestHandler";
|
|
|
12
11
|
export {
|
|
13
12
|
AGGRED_DIR,
|
|
14
13
|
ErrorDigest,
|
|
15
|
-
PluginManager,
|
|
16
14
|
createErrorHtml,
|
|
17
15
|
createServerBase,
|
|
18
16
|
getLoaderCtx,
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
2
|
+
function getHookRunners(context) {
|
|
3
|
+
var hooks = context.hooks;
|
|
4
|
+
return {
|
|
5
|
+
config: function(params) {
|
|
6
|
+
return hooks.modifyConfig.call(params);
|
|
7
|
+
},
|
|
8
|
+
prepare: function() {
|
|
9
|
+
return hooks.onPrepare.call();
|
|
10
|
+
},
|
|
11
|
+
reset: function(params) {
|
|
12
|
+
return hooks.onReset.call(params);
|
|
13
|
+
},
|
|
14
|
+
fallback: function(input) {
|
|
15
|
+
return hooks.fallback.call(input);
|
|
16
|
+
},
|
|
17
|
+
prepareWebServer: function(input) {
|
|
18
|
+
return hooks.prepareWebServer.call(input);
|
|
19
|
+
},
|
|
20
|
+
prepareApiServer: function(input) {
|
|
21
|
+
return hooks.prepareApiServer.call(input);
|
|
22
|
+
},
|
|
23
|
+
afterMatch: function(ctx) {
|
|
24
|
+
return hooks.afterMatch.call(ctx);
|
|
25
|
+
},
|
|
26
|
+
afterRender: function(ctx) {
|
|
27
|
+
return hooks.afterRender.call(ctx);
|
|
28
|
+
},
|
|
29
|
+
afterStreamingRender: function(ctx) {
|
|
30
|
+
return hooks.afterStreamingRender.call(ctx);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function transformHookRunner(hookRunnerName) {
|
|
35
|
+
switch (hookRunnerName) {
|
|
36
|
+
case "config":
|
|
37
|
+
return "modifyConfig";
|
|
38
|
+
case "prepare":
|
|
39
|
+
return "onPrepare";
|
|
40
|
+
case "reset":
|
|
41
|
+
return "onReset";
|
|
42
|
+
default:
|
|
43
|
+
return hookRunnerName;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function handleSetupResult(setupResult, api) {
|
|
47
|
+
if (!setupResult) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
Object.keys(setupResult).forEach(function(key) {
|
|
51
|
+
var fn = setupResult[key];
|
|
52
|
+
if (typeof fn === "function") {
|
|
53
|
+
var newAPI = transformHookRunner(key);
|
|
54
|
+
if (api[newAPI]) {
|
|
55
|
+
api[newAPI](function() {
|
|
56
|
+
for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
57
|
+
params[_key] = arguments[_key];
|
|
58
|
+
}
|
|
59
|
+
var res = fn.apply(void 0, _to_consumable_array(params));
|
|
60
|
+
return res;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
export {
|
|
67
|
+
getHookRunners,
|
|
68
|
+
handleSetupResult,
|
|
69
|
+
transformHookRunner
|
|
70
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
|
|
2
|
+
import { createAsyncHook, createAsyncPipelineHook } from "@modern-js/plugin-v2";
|
|
3
|
+
import { getHookRunners } from "./hooks";
|
|
4
|
+
import { handleSetupResult } from "./hooks";
|
|
5
|
+
var compatPlugin = function() {
|
|
6
|
+
return {
|
|
7
|
+
name: "@modern-js/server-compat",
|
|
8
|
+
registryHooks: {
|
|
9
|
+
fallback: createAsyncHook(),
|
|
10
|
+
prepareWebServer: createAsyncPipelineHook(),
|
|
11
|
+
prepareApiServer: createAsyncPipelineHook(),
|
|
12
|
+
afterMatch: createAsyncPipelineHook(),
|
|
13
|
+
afterRender: createAsyncPipelineHook(),
|
|
14
|
+
afterStreamingRender: createAsyncPipelineHook()
|
|
15
|
+
},
|
|
16
|
+
_registryApi: function(getServerContext, updateServerContext) {
|
|
17
|
+
var getInternalContext = function() {
|
|
18
|
+
return getServerContext()._internalContext;
|
|
19
|
+
};
|
|
20
|
+
return {
|
|
21
|
+
useConfigContext: function() {
|
|
22
|
+
return getInternalContext().config;
|
|
23
|
+
},
|
|
24
|
+
useAppContext: function() {
|
|
25
|
+
var _getServerContext = getServerContext(), _internalContext = _getServerContext._internalContext, serverContext = _object_without_properties(_getServerContext, [
|
|
26
|
+
"_internalContext"
|
|
27
|
+
]);
|
|
28
|
+
return serverContext;
|
|
29
|
+
},
|
|
30
|
+
setAppContext: function(context) {
|
|
31
|
+
return updateServerContext(context);
|
|
32
|
+
},
|
|
33
|
+
useHookRunners: function() {
|
|
34
|
+
return getHookRunners(getInternalContext());
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
setup: function() {
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export {
|
|
43
|
+
compatPlugin,
|
|
44
|
+
handleSetupResult
|
|
45
|
+
};
|