@modern-js/server-core 2.47.1-alpha.0 → 2.48.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 +4 -6
- package/dist/cjs/{core/loadPlugins.js → loadPlugins.js} +1 -1
- package/dist/cjs/{core/plugin.js → plugin.js} +3 -1
- package/dist/esm/index.js +2 -3
- package/dist/esm/{core/loadPlugins.js → loadPlugins.js} +1 -1
- package/dist/esm/{core/plugin.js → plugin.js} +3 -1
- package/dist/types/index.d.ts +2 -3
- package/dist/types/{core/loadPlugins.d.ts → loadPlugins.d.ts} +1 -1
- package/dist/types/{core/plugin.d.ts → plugin.d.ts} +35 -32
- package/package.json +6 -29
- package/dist/cjs/base/adapters/node/bff.js +0 -69
- package/dist/cjs/base/adapters/node/hono.js +0 -62
- package/dist/cjs/base/adapters/node/index.js +0 -32
- package/dist/cjs/base/adapters/node/loadServer.js +0 -56
- package/dist/cjs/base/adapters/node/middlewares/index.js +0 -26
- package/dist/cjs/base/adapters/node/middlewares/mock.js +0 -113
- package/dist/cjs/base/adapters/node/middlewares/serverStatic.js +0 -105
- package/dist/cjs/base/adapters/node/middlewares/templates.js +0 -65
- package/dist/cjs/base/adapters/node/node.js +0 -121
- package/dist/cjs/base/adapters/node/polyfills/index.js +0 -24
- package/dist/cjs/base/adapters/node/polyfills/install.js +0 -57
- package/dist/cjs/base/adapters/node/polyfills/stream.js +0 -159
- package/dist/cjs/base/constants.js +0 -55
- package/dist/cjs/base/index.js +0 -73
- package/dist/cjs/base/middlewares/customServer/base.js +0 -115
- package/dist/cjs/base/middlewares/customServer/context.js +0 -78
- package/dist/cjs/base/middlewares/customServer/index.js +0 -138
- package/dist/cjs/base/middlewares/customServer/routerApi.js +0 -48
- package/dist/cjs/base/middlewares/customServer/template.js +0 -60
- package/dist/cjs/base/middlewares/dataHandler.js +0 -69
- package/dist/cjs/base/middlewares/faviconFallback.js +0 -34
- package/dist/cjs/base/middlewares/index.js +0 -28
- package/dist/cjs/base/middlewares/monitor.js +0 -75
- package/dist/cjs/base/middlewares/renderHandler/index.js +0 -92
- package/dist/cjs/base/middlewares/renderHandler/render.js +0 -90
- package/dist/cjs/base/middlewares/renderHandler/serverTiming.js +0 -43
- package/dist/cjs/base/middlewares/renderHandler/ssrCache.js +0 -173
- package/dist/cjs/base/middlewares/renderHandler/ssrRender.js +0 -183
- package/dist/cjs/base/serverBase.js +0 -167
- package/dist/cjs/base/utils/debug.js +0 -29
- package/dist/cjs/base/utils/entry.js +0 -30
- package/dist/cjs/base/utils/env.js +0 -45
- package/dist/cjs/base/utils/error.js +0 -65
- package/dist/cjs/base/utils/index.js +0 -40
- package/dist/cjs/base/utils/middlewareCollector.js +0 -54
- package/dist/cjs/base/utils/path.js +0 -46
- package/dist/cjs/base/utils/request.js +0 -65
- package/dist/cjs/base/utils/serverConfig.js +0 -71
- package/dist/cjs/base/utils/transformStream.js +0 -38
- package/dist/cjs/base/utils/warmup.js +0 -43
- package/dist/cjs/core/hono.js +0 -16
- package/dist/cjs/core/index.js +0 -24
- package/dist/cjs/core/render.js +0 -16
- package/dist/cjs/core/server.js +0 -22
- package/dist/esm/base/adapters/node/bff.js +0 -45
- package/dist/esm/base/adapters/node/hono.js +0 -37
- package/dist/esm/base/adapters/node/index.js +0 -6
- package/dist/esm/base/adapters/node/loadServer.js +0 -22
- package/dist/esm/base/adapters/node/middlewares/index.js +0 -3
- package/dist/esm/base/adapters/node/middlewares/mock.js +0 -79
- package/dist/esm/base/adapters/node/middlewares/serverStatic.js +0 -71
- package/dist/esm/base/adapters/node/middlewares/templates.js +0 -31
- package/dist/esm/base/adapters/node/node.js +0 -95
- package/dist/esm/base/adapters/node/polyfills/index.js +0 -2
- package/dist/esm/base/adapters/node/polyfills/install.js +0 -33
- package/dist/esm/base/adapters/node/polyfills/stream.js +0 -132
- package/dist/esm/base/constants.js +0 -29
- package/dist/esm/base/index.js +0 -32
- package/dist/esm/base/middlewares/customServer/base.js +0 -91
- package/dist/esm/base/middlewares/customServer/context.js +0 -51
- package/dist/esm/base/middlewares/customServer/index.js +0 -114
- package/dist/esm/base/middlewares/customServer/routerApi.js +0 -24
- package/dist/esm/base/middlewares/customServer/template.js +0 -36
- package/dist/esm/base/middlewares/dataHandler.js +0 -35
- package/dist/esm/base/middlewares/faviconFallback.js +0 -10
- package/dist/esm/base/middlewares/index.js +0 -4
- package/dist/esm/base/middlewares/monitor.js +0 -49
- package/dist/esm/base/middlewares/renderHandler/index.js +0 -67
- package/dist/esm/base/middlewares/renderHandler/render.js +0 -66
- package/dist/esm/base/middlewares/renderHandler/serverTiming.js +0 -19
- package/dist/esm/base/middlewares/renderHandler/ssrCache.js +0 -149
- package/dist/esm/base/middlewares/renderHandler/ssrRender.js +0 -149
- package/dist/esm/base/serverBase.js +0 -143
- package/dist/esm/base/utils/debug.js +0 -5
- package/dist/esm/base/utils/entry.js +0 -6
- package/dist/esm/base/utils/env.js +0 -20
- package/dist/esm/base/utils/error.js +0 -41
- package/dist/esm/base/utils/index.js +0 -10
- package/dist/esm/base/utils/middlewareCollector.js +0 -29
- package/dist/esm/base/utils/path.js +0 -12
- package/dist/esm/base/utils/request.js +0 -38
- package/dist/esm/base/utils/serverConfig.js +0 -35
- package/dist/esm/base/utils/transformStream.js +0 -14
- package/dist/esm/base/utils/warmup.js +0 -9
- package/dist/esm/core/hono.js +0 -0
- package/dist/esm/core/index.js +0 -2
- package/dist/esm/core/render.js +0 -0
- package/dist/esm/core/server.js +0 -1
- package/dist/types/base/adapters/node/bff.d.ts +0 -4
- package/dist/types/base/adapters/node/hono.d.ts +0 -30
- package/dist/types/base/adapters/node/index.d.ts +0 -6
- package/dist/types/base/adapters/node/loadServer.d.ts +0 -3
- package/dist/types/base/adapters/node/middlewares/index.d.ts +0 -3
- package/dist/types/base/adapters/node/middlewares/mock.d.ts +0 -5
- package/dist/types/base/adapters/node/middlewares/serverStatic.d.ts +0 -9
- package/dist/types/base/adapters/node/middlewares/templates.d.ts +0 -3
- package/dist/types/base/adapters/node/node.d.ts +0 -12
- package/dist/types/base/adapters/node/polyfills/index.d.ts +0 -2
- package/dist/types/base/adapters/node/polyfills/install.d.ts +0 -1
- package/dist/types/base/adapters/node/polyfills/stream.d.ts +0 -18
- package/dist/types/base/constants.d.ts +0 -23
- package/dist/types/base/index.d.ts +0 -9
- package/dist/types/base/middlewares/customServer/base.d.ts +0 -3
- package/dist/types/base/middlewares/customServer/context.d.ts +0 -7
- package/dist/types/base/middlewares/customServer/index.d.ts +0 -13
- package/dist/types/base/middlewares/customServer/routerApi.d.ts +0 -9
- package/dist/types/base/middlewares/customServer/template.d.ts +0 -11
- package/dist/types/base/middlewares/dataHandler.d.ts +0 -3
- package/dist/types/base/middlewares/faviconFallback.d.ts +0 -2
- package/dist/types/base/middlewares/index.d.ts +0 -4
- package/dist/types/base/middlewares/monitor.d.ts +0 -7
- package/dist/types/base/middlewares/renderHandler/index.d.ts +0 -9
- package/dist/types/base/middlewares/renderHandler/render.d.ts +0 -12
- package/dist/types/base/middlewares/renderHandler/serverTiming.d.ts +0 -7
- package/dist/types/base/middlewares/renderHandler/ssrCache.d.ts +0 -17
- package/dist/types/base/middlewares/renderHandler/ssrRender.d.ts +0 -18
- package/dist/types/base/serverBase.d.ts +0 -49
- package/dist/types/base/utils/debug.d.ts +0 -1
- package/dist/types/base/utils/entry.d.ts +0 -2
- package/dist/types/base/utils/env.d.ts +0 -2
- package/dist/types/base/utils/error.d.ts +0 -1
- package/dist/types/base/utils/index.d.ts +0 -10
- package/dist/types/base/utils/middlewareCollector.d.ts +0 -12
- package/dist/types/base/utils/path.d.ts +0 -3
- package/dist/types/base/utils/request.d.ts +0 -12
- package/dist/types/base/utils/serverConfig.d.ts +0 -14
- package/dist/types/base/utils/transformStream.d.ts +0 -2
- package/dist/types/base/utils/warmup.d.ts +0 -1
- package/dist/types/core/hono.d.ts +0 -1
- package/dist/types/core/index.d.ts +0 -2
- package/dist/types/core/render.d.ts +0 -11
- package/dist/types/core/server.d.ts +0 -71
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { cutNameByHyphen } from "@modern-js/utils";
|
|
2
|
-
import { parseQuery } from "../../utils/request";
|
|
3
|
-
import { createErrorHtml, sortRoutes } from "../../utils";
|
|
4
|
-
import { ssrRender } from "./ssrRender";
|
|
5
|
-
function createRender({ routes, pwd, metaName, staticGenerate, forceCSR, nonce }) {
|
|
6
|
-
return async (req, { logger, nodeReq, reporter, tpls }) => {
|
|
7
|
-
const routeInfo = matchRoute(req, routes);
|
|
8
|
-
if (!routeInfo) {
|
|
9
|
-
return new Response(createErrorHtml(404), {
|
|
10
|
-
status: 404,
|
|
11
|
-
headers: {
|
|
12
|
-
"content-type": "text/html; charset=UTF-8"
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
const html = tpls[routeInfo.entryName];
|
|
17
|
-
if (!html) {
|
|
18
|
-
throw new Error(`Can't found entry ${routeInfo.entryName} html `);
|
|
19
|
-
}
|
|
20
|
-
const renderMode = getRenderMode(req, metaName || "modern-js", routeInfo.isSSR, forceCSR);
|
|
21
|
-
return renderMode === "csr" ? csrRender(html) : ssrRender(req, {
|
|
22
|
-
pwd,
|
|
23
|
-
mode: "string",
|
|
24
|
-
html,
|
|
25
|
-
routeInfo,
|
|
26
|
-
staticGenerate: staticGenerate || false,
|
|
27
|
-
metaName: metaName || "modern-js",
|
|
28
|
-
nonce,
|
|
29
|
-
logger,
|
|
30
|
-
nodeReq,
|
|
31
|
-
reporter
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
function matchRoute(req, routes) {
|
|
36
|
-
const sorted = routes.sort(sortRoutes);
|
|
37
|
-
for (const route of sorted) {
|
|
38
|
-
const reg = new RegExp(route.urlPath);
|
|
39
|
-
if (reg.test(req.url)) {
|
|
40
|
-
return route;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return void 0;
|
|
44
|
-
}
|
|
45
|
-
function getRenderMode(req, framework, isSSR, forceCSR) {
|
|
46
|
-
const query = parseQuery(req);
|
|
47
|
-
if (isSSR) {
|
|
48
|
-
if (forceCSR && (query.csr || req.headers.get(`x-${cutNameByHyphen(framework)}-ssr-fallback`))) {
|
|
49
|
-
return "csr";
|
|
50
|
-
}
|
|
51
|
-
return "ssr";
|
|
52
|
-
} else {
|
|
53
|
-
return "csr";
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
function csrRender(html) {
|
|
57
|
-
return new Response(html, {
|
|
58
|
-
status: 200,
|
|
59
|
-
headers: new Headers({
|
|
60
|
-
"content-type": "text/html; charset=UTF-8"
|
|
61
|
-
})
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
export {
|
|
65
|
-
createRender
|
|
66
|
-
};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
2
|
-
const SERVER_TIMING = "Server-Timing";
|
|
3
|
-
class ServerTiming {
|
|
4
|
-
addServeTiming(name, dur, desc) {
|
|
5
|
-
const _name = `bd-${this.meta}-${name}`;
|
|
6
|
-
const value = `${_name};${desc ? `decs="${desc}";` : ""} dur=${dur}`;
|
|
7
|
-
this.headers.append(SERVER_TIMING, value);
|
|
8
|
-
return this;
|
|
9
|
-
}
|
|
10
|
-
constructor(headers, meta) {
|
|
11
|
-
_define_property(this, "headers", void 0);
|
|
12
|
-
_define_property(this, "meta", void 0);
|
|
13
|
-
this.meta = meta;
|
|
14
|
-
this.headers = headers;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
export {
|
|
18
|
-
ServerTiming
|
|
19
|
-
};
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
2
|
-
import { Readable } from "stream";
|
|
3
|
-
import { SERVER_DIR, requireExistModule } from "@modern-js/utils";
|
|
4
|
-
import { createMemoryStorage } from "@modern-js/runtime-utils/storer";
|
|
5
|
-
import { createReadableStreamFromReadable } from "../../adapters/node/polyfills/stream";
|
|
6
|
-
import { createTransformStream, getPathModule, getPathname } from "../../utils";
|
|
7
|
-
class CacheManager {
|
|
8
|
-
async getCacheResult(req, cacheControl, render, ssrContext) {
|
|
9
|
-
const key = this.computedKey(req, cacheControl);
|
|
10
|
-
const value = await this.container.get(key);
|
|
11
|
-
const { maxAge, staleWhileRevalidate } = cacheControl;
|
|
12
|
-
const ttl = maxAge + staleWhileRevalidate;
|
|
13
|
-
if (value) {
|
|
14
|
-
const cache = JSON.parse(value);
|
|
15
|
-
const interval = Date.now() - cache.cursor;
|
|
16
|
-
if (interval <= maxAge) {
|
|
17
|
-
return cache.val;
|
|
18
|
-
} else if (interval <= staleWhileRevalidate + maxAge) {
|
|
19
|
-
this.processCache(key, render, ssrContext, ttl);
|
|
20
|
-
return cache.val;
|
|
21
|
-
} else {
|
|
22
|
-
return this.processCache(key, render, ssrContext, ttl);
|
|
23
|
-
}
|
|
24
|
-
} else {
|
|
25
|
-
return this.processCache(key, render, ssrContext, ttl);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
async processCache(key, render, ssrContext, ttl) {
|
|
29
|
-
const renderResult = await render(ssrContext);
|
|
30
|
-
if (!renderResult) {
|
|
31
|
-
return "";
|
|
32
|
-
} else if (typeof renderResult === "string") {
|
|
33
|
-
const current = Date.now();
|
|
34
|
-
const cache = {
|
|
35
|
-
val: renderResult,
|
|
36
|
-
cursor: current
|
|
37
|
-
};
|
|
38
|
-
await this.container.set(key, JSON.stringify(cache), {
|
|
39
|
-
ttl
|
|
40
|
-
});
|
|
41
|
-
return renderResult;
|
|
42
|
-
} else {
|
|
43
|
-
const body = (
|
|
44
|
-
// TODO: remove node:stream, move it to ssr entry.
|
|
45
|
-
renderResult instanceof Readable ? createReadableStreamFromReadable(renderResult) : renderResult
|
|
46
|
-
);
|
|
47
|
-
let html = "";
|
|
48
|
-
const stream = createTransformStream((chunk) => {
|
|
49
|
-
html += chunk;
|
|
50
|
-
return chunk;
|
|
51
|
-
});
|
|
52
|
-
stream.readable.getReader().closed.then(() => {
|
|
53
|
-
const current = Date.now();
|
|
54
|
-
const cache = {
|
|
55
|
-
val: html,
|
|
56
|
-
cursor: current
|
|
57
|
-
};
|
|
58
|
-
this.container.set(key, JSON.stringify(cache), {
|
|
59
|
-
ttl
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
body.pipeThrough(stream);
|
|
63
|
-
return stream.readable;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
computedKey(req, cacheControl) {
|
|
67
|
-
const pathname = getPathname(req);
|
|
68
|
-
const { customKey } = cacheControl;
|
|
69
|
-
const defaultKey = pathname.replace(/.+\/+$/, "");
|
|
70
|
-
if (customKey) {
|
|
71
|
-
if (typeof customKey === "string") {
|
|
72
|
-
return customKey;
|
|
73
|
-
} else {
|
|
74
|
-
return customKey(defaultKey);
|
|
75
|
-
}
|
|
76
|
-
} else {
|
|
77
|
-
return defaultKey;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
constructor(container) {
|
|
81
|
-
_define_property(this, "container", void 0);
|
|
82
|
-
this.container = container;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
const CACHE_FILENAME = "cache";
|
|
86
|
-
class ServerCache {
|
|
87
|
-
async loadCacheMod(pwd = process.cwd()) {
|
|
88
|
-
const path = await getPathModule();
|
|
89
|
-
const serverCacheFilepath = path.resolve(pwd, SERVER_DIR, CACHE_FILENAME);
|
|
90
|
-
const mod = requireExistModule(serverCacheFilepath, {
|
|
91
|
-
interop: false
|
|
92
|
-
});
|
|
93
|
-
this.cacheOption = mod === null || mod === void 0 ? void 0 : mod.cacheOption;
|
|
94
|
-
if (this.cacheOption && !(mod === null || mod === void 0 ? void 0 : mod.customContainer)) {
|
|
95
|
-
const cacheStorage = createMemoryStorage("__ssr__cache");
|
|
96
|
-
this.customContainer = cacheStorage;
|
|
97
|
-
} else {
|
|
98
|
-
this.customContainer = mod === null || mod === void 0 ? void 0 : mod.customContainer;
|
|
99
|
-
}
|
|
100
|
-
if (this.customContainer) {
|
|
101
|
-
this.cacheManger = new CacheManager(this.customContainer);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
matchCacheControl(req) {
|
|
105
|
-
const { cacheOption } = this;
|
|
106
|
-
if (!cacheOption || !req) {
|
|
107
|
-
return void 0;
|
|
108
|
-
} else if (isCacheControl(cacheOption)) {
|
|
109
|
-
return cacheOption;
|
|
110
|
-
} else if (isCacheOptionProvider(cacheOption)) {
|
|
111
|
-
return cacheOption(req);
|
|
112
|
-
} else {
|
|
113
|
-
const url = req.url;
|
|
114
|
-
const options = Object.entries(cacheOption);
|
|
115
|
-
for (const [key, option] of options) {
|
|
116
|
-
if (key === "*" || new RegExp(key).test(url)) {
|
|
117
|
-
if (typeof option === "function") {
|
|
118
|
-
return option(req);
|
|
119
|
-
} else {
|
|
120
|
-
return option;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
return void 0;
|
|
125
|
-
}
|
|
126
|
-
function isCacheOptionProvider(option) {
|
|
127
|
-
return typeof option === "function";
|
|
128
|
-
}
|
|
129
|
-
function isCacheControl(option) {
|
|
130
|
-
return typeof option === "object" && option !== null && "maxAge" in option;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
getCache(req, cacheControl, render, ssrContext) {
|
|
134
|
-
if (this.cacheManger) {
|
|
135
|
-
return this.cacheManger.getCacheResult(req, cacheControl, render, ssrContext);
|
|
136
|
-
} else {
|
|
137
|
-
return render(ssrContext);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
constructor() {
|
|
141
|
-
_define_property(this, "customContainer", void 0);
|
|
142
|
-
_define_property(this, "cacheOption", void 0);
|
|
143
|
-
_define_property(this, "cacheManger", void 0);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
const ssrCache = new ServerCache();
|
|
147
|
-
export {
|
|
148
|
-
ssrCache
|
|
149
|
-
};
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
2
|
-
import { Readable } from "stream";
|
|
3
|
-
import { LOADABLE_STATS_FILE, ROUTE_MANIFEST_FILE, SERVER_RENDER_FUNCTION_NAME } from "@modern-js/utils";
|
|
4
|
-
import * as isbot from "isbot";
|
|
5
|
-
import { createTransformStream, getPathModule } from "../../utils";
|
|
6
|
-
import { REPLACE_REG } from "../../constants";
|
|
7
|
-
import { parseHeaders, parseQuery, getHost } from "../../utils/request";
|
|
8
|
-
import { createReadableStreamFromReadable } from "../../adapters/node/polyfills/stream";
|
|
9
|
-
import { ServerTiming } from "./serverTiming";
|
|
10
|
-
import { ssrCache } from "./ssrCache";
|
|
11
|
-
const defaultReporter = {
|
|
12
|
-
init() {
|
|
13
|
-
},
|
|
14
|
-
reportError() {
|
|
15
|
-
},
|
|
16
|
-
reportTiming() {
|
|
17
|
-
},
|
|
18
|
-
reportInfo() {
|
|
19
|
-
},
|
|
20
|
-
reportWarn() {
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
async function ssrRender(request, { routeInfo, pwd, html, staticGenerate, nonce, metaName, reporter, logger, nodeReq }) {
|
|
24
|
-
const { entryName } = routeInfo;
|
|
25
|
-
const path = await getPathModule();
|
|
26
|
-
const jsBundlePath = path.join(pwd, routeInfo.bundle);
|
|
27
|
-
const loadableUri = path.join(pwd, LOADABLE_STATS_FILE);
|
|
28
|
-
let loadableStats = {};
|
|
29
|
-
try {
|
|
30
|
-
loadableStats = await import(loadableUri);
|
|
31
|
-
} catch (_) {
|
|
32
|
-
}
|
|
33
|
-
const routesManifestUri = path.join(pwd, ROUTE_MANIFEST_FILE);
|
|
34
|
-
let routeManifest;
|
|
35
|
-
try {
|
|
36
|
-
routeManifest = await import(routesManifestUri);
|
|
37
|
-
} catch (_) {
|
|
38
|
-
}
|
|
39
|
-
const host = getHost(request);
|
|
40
|
-
const isSpider = isbot.default(request.headers.get("user-agent"));
|
|
41
|
-
const responseProxy = new ResponseProxy();
|
|
42
|
-
const query = parseQuery(request);
|
|
43
|
-
const headers = parseHeaders(request);
|
|
44
|
-
const ssrContext = {
|
|
45
|
-
request: {
|
|
46
|
-
baseUrl: routeInfo.urlPath,
|
|
47
|
-
params: {},
|
|
48
|
-
// eslint-disable-next-line node/no-unsupported-features/node-builtins, node/prefer-global/url
|
|
49
|
-
pathname: new URL(request.url).pathname,
|
|
50
|
-
host,
|
|
51
|
-
query,
|
|
52
|
-
url: request.url,
|
|
53
|
-
headers
|
|
54
|
-
},
|
|
55
|
-
response: {
|
|
56
|
-
setHeader(key, value) {
|
|
57
|
-
responseProxy.headers.set(key, value);
|
|
58
|
-
},
|
|
59
|
-
status(code) {
|
|
60
|
-
responseProxy.status = code;
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
redirection: {},
|
|
64
|
-
template: html,
|
|
65
|
-
loadableStats,
|
|
66
|
-
routeManifest,
|
|
67
|
-
entryName,
|
|
68
|
-
staticGenerate,
|
|
69
|
-
logger,
|
|
70
|
-
serverTiming: new ServerTiming(responseProxy.headers, metaName),
|
|
71
|
-
reporter: reporter || defaultReporter,
|
|
72
|
-
/** @deprecated node req */
|
|
73
|
-
req: void 0,
|
|
74
|
-
/** @deprecated node res */
|
|
75
|
-
res: void 0,
|
|
76
|
-
isSpider,
|
|
77
|
-
nonce
|
|
78
|
-
};
|
|
79
|
-
const jsBundle = await import(jsBundlePath);
|
|
80
|
-
const incomingMessage = nodeReq ? nodeReq : new IncomingMessgeProxy(request);
|
|
81
|
-
const cacheControl = await ssrCache.matchCacheControl(incomingMessage);
|
|
82
|
-
let ssrResult;
|
|
83
|
-
const render = jsBundle[SERVER_RENDER_FUNCTION_NAME];
|
|
84
|
-
if (cacheControl) {
|
|
85
|
-
ssrResult = await ssrCache.getCache(request, cacheControl, render, ssrContext);
|
|
86
|
-
} else {
|
|
87
|
-
ssrResult = await render(ssrContext);
|
|
88
|
-
}
|
|
89
|
-
const { redirection } = ssrContext;
|
|
90
|
-
if (redirection.url) {
|
|
91
|
-
const { headers: headers2 } = responseProxy;
|
|
92
|
-
headers2.set("Location", redirection.url);
|
|
93
|
-
return new Response(null, {
|
|
94
|
-
status: redirection.status || 302,
|
|
95
|
-
headers: {
|
|
96
|
-
Location: redirection.url
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
const serverData = {
|
|
101
|
-
router: {
|
|
102
|
-
baseUrl: routeInfo.urlPath,
|
|
103
|
-
// TODO: parse
|
|
104
|
-
params: {}
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
const data = ssrResult instanceof Readable ? createReadableStreamFromReadable(ssrResult) : ssrResult;
|
|
108
|
-
const body = injectServerData(data, serverData);
|
|
109
|
-
return new Response(body, {
|
|
110
|
-
status: responseProxy.status,
|
|
111
|
-
headers: responseProxy.headers
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
function injectServerData(data, serverData) {
|
|
115
|
-
const { head } = REPLACE_REG.before;
|
|
116
|
-
const searchValue = new RegExp(head);
|
|
117
|
-
const replcaeCb = (beforeHead) => `${beforeHead}<script type="application/json" id="__MODERN_SERVER_DATA__">${JSON.stringify(serverData)}</script>`;
|
|
118
|
-
if (typeof data === "string") {
|
|
119
|
-
return data.replace(searchValue, replcaeCb);
|
|
120
|
-
} else {
|
|
121
|
-
const stream = createTransformStream((before) => {
|
|
122
|
-
return before.replace(searchValue, replcaeCb);
|
|
123
|
-
});
|
|
124
|
-
data.pipeThrough(stream);
|
|
125
|
-
return stream.readable;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
class ResponseProxy {
|
|
129
|
-
constructor() {
|
|
130
|
-
_define_property(this, "headers", new Headers());
|
|
131
|
-
_define_property(this, "status", 200);
|
|
132
|
-
this.headers.set("content-type", "text/html; charset=UTF-8");
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
class IncomingMessgeProxy {
|
|
136
|
-
constructor(req) {
|
|
137
|
-
_define_property(this, "headers", {});
|
|
138
|
-
_define_property(this, "method", void 0);
|
|
139
|
-
_define_property(this, "url", void 0);
|
|
140
|
-
req.headers.forEach((value, key) => {
|
|
141
|
-
this.headers[key] = value;
|
|
142
|
-
});
|
|
143
|
-
this.method = req.method;
|
|
144
|
-
this.url = new URL(req.url).pathname;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
export {
|
|
148
|
-
ssrRender
|
|
149
|
-
};
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
2
|
-
import { INTERNAL_SERVER_PLUGINS, OUTPUT_CONFIG_FILE, SHARED_DIR, ensureAbsolutePath } from "@modern-js/utils";
|
|
3
|
-
import { Hono } from "hono";
|
|
4
|
-
import { AppContext, ConfigContext, loadPlugins, serverManager } from "../core";
|
|
5
|
-
import { debug, getPathModule, getServerConfigPath, loadConfig, requireConfig } from "./utils";
|
|
6
|
-
class ServerBase {
|
|
7
|
-
/**
|
|
8
|
-
* 初始化顺序
|
|
9
|
-
* - 获取 server runtime config
|
|
10
|
-
* - 设置 context
|
|
11
|
-
* - 创建 hooksRunner
|
|
12
|
-
* - 合并插件,内置插件和 serverConfig 中配置的插件
|
|
13
|
-
* - 执行 config hook
|
|
14
|
-
* - 获取最终的配置
|
|
15
|
-
* - 设置配置到 context
|
|
16
|
-
* - 执行 prepare hook
|
|
17
|
-
*/
|
|
18
|
-
async init() {
|
|
19
|
-
const { options } = this;
|
|
20
|
-
await this.initServerConfig(options);
|
|
21
|
-
await this.injectContext(options);
|
|
22
|
-
this.runner = await this.createHookRunner();
|
|
23
|
-
await this.initConfig(this.runner, options);
|
|
24
|
-
await this.injectContext(options);
|
|
25
|
-
await this.runner.prepare();
|
|
26
|
-
return this;
|
|
27
|
-
}
|
|
28
|
-
async createHookRunner() {
|
|
29
|
-
serverManager.clear();
|
|
30
|
-
const { options } = this;
|
|
31
|
-
const { internalPlugins = INTERNAL_SERVER_PLUGINS, pwd, plugins = [] } = options;
|
|
32
|
-
const serverPlugins = this.serverConfig.plugins || [];
|
|
33
|
-
const loadedPlugins = loadPlugins(options.appContext.appDirectory || pwd, [
|
|
34
|
-
...serverPlugins,
|
|
35
|
-
...plugins
|
|
36
|
-
], {
|
|
37
|
-
internalPlugins
|
|
38
|
-
});
|
|
39
|
-
debug("plugins", loadedPlugins);
|
|
40
|
-
loadedPlugins.forEach((p) => {
|
|
41
|
-
serverManager.usePlugin(p);
|
|
42
|
-
});
|
|
43
|
-
const hooksRunner = await serverManager.init();
|
|
44
|
-
return hooksRunner;
|
|
45
|
-
}
|
|
46
|
-
async initServerConfig(options) {
|
|
47
|
-
const { pwd, serverConfigFile } = options;
|
|
48
|
-
const serverConfigPath = await getServerConfigPath(pwd, serverConfigFile);
|
|
49
|
-
const serverConfig = requireConfig(serverConfigPath);
|
|
50
|
-
this.serverConfig = serverConfig;
|
|
51
|
-
}
|
|
52
|
-
async injectContext(options) {
|
|
53
|
-
const appContext = await this.initAppContext();
|
|
54
|
-
const { config, pwd } = options;
|
|
55
|
-
ConfigContext.set(config);
|
|
56
|
-
AppContext.set({
|
|
57
|
-
...appContext,
|
|
58
|
-
serverBase: this,
|
|
59
|
-
distDirectory: pwd
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
async initAppContext() {
|
|
63
|
-
const path = await getPathModule();
|
|
64
|
-
const { options } = this;
|
|
65
|
-
const { pwd, plugins = [], appContext } = options;
|
|
66
|
-
const serverPlugins = plugins.map((p) => ({
|
|
67
|
-
server: p
|
|
68
|
-
}));
|
|
69
|
-
return {
|
|
70
|
-
appDirectory: (appContext === null || appContext === void 0 ? void 0 : appContext.appDirectory) || "",
|
|
71
|
-
apiDirectory: appContext === null || appContext === void 0 ? void 0 : appContext.apiDirectory,
|
|
72
|
-
lambdaDirectory: appContext === null || appContext === void 0 ? void 0 : appContext.lambdaDirectory,
|
|
73
|
-
sharedDirectory: (appContext === null || appContext === void 0 ? void 0 : appContext.sharedDirectory) || path.resolve(appContext.appDirectory || "", SHARED_DIR),
|
|
74
|
-
distDirectory: pwd,
|
|
75
|
-
plugins: serverPlugins
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Execute config hooks
|
|
80
|
-
* @param runner
|
|
81
|
-
* @param options
|
|
82
|
-
*/
|
|
83
|
-
runConfigHook(runner, serverConfig) {
|
|
84
|
-
const newServerConfig = runner.config(serverConfig || {});
|
|
85
|
-
return newServerConfig;
|
|
86
|
-
}
|
|
87
|
-
async initConfig(runner, options) {
|
|
88
|
-
const path = await getPathModule();
|
|
89
|
-
const { pwd, config } = options;
|
|
90
|
-
const { serverConfig } = this;
|
|
91
|
-
const finalServerConfig = this.runConfigHook(runner, serverConfig);
|
|
92
|
-
const resolvedConfigPath = ensureAbsolutePath(pwd, path.join(config.output.path || "dist", OUTPUT_CONFIG_FILE));
|
|
93
|
-
options.config = loadConfig({
|
|
94
|
-
cliConfig: config,
|
|
95
|
-
serverConfig: finalServerConfig,
|
|
96
|
-
resolvedConfigPath
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
get all() {
|
|
100
|
-
return this.app.all;
|
|
101
|
-
}
|
|
102
|
-
get use() {
|
|
103
|
-
return this.app.use;
|
|
104
|
-
}
|
|
105
|
-
get get() {
|
|
106
|
-
return this.app.get;
|
|
107
|
-
}
|
|
108
|
-
get post() {
|
|
109
|
-
return this.app.post;
|
|
110
|
-
}
|
|
111
|
-
get put() {
|
|
112
|
-
return this.app.put;
|
|
113
|
-
}
|
|
114
|
-
get delete() {
|
|
115
|
-
return this.app.delete;
|
|
116
|
-
}
|
|
117
|
-
get patch() {
|
|
118
|
-
return this.app.patch;
|
|
119
|
-
}
|
|
120
|
-
get handle() {
|
|
121
|
-
return this.app.fetch;
|
|
122
|
-
}
|
|
123
|
-
get request() {
|
|
124
|
-
return this.app.request;
|
|
125
|
-
}
|
|
126
|
-
get notFound() {
|
|
127
|
-
return this.app.notFound;
|
|
128
|
-
}
|
|
129
|
-
get onError() {
|
|
130
|
-
return this.app.onError;
|
|
131
|
-
}
|
|
132
|
-
constructor(options) {
|
|
133
|
-
_define_property(this, "options", void 0);
|
|
134
|
-
_define_property(this, "runner", void 0);
|
|
135
|
-
_define_property(this, "app", void 0);
|
|
136
|
-
_define_property(this, "serverConfig", {});
|
|
137
|
-
this.options = options;
|
|
138
|
-
this.app = new Hono();
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
export {
|
|
142
|
-
ServerBase
|
|
143
|
-
};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
const getRuntimeEnv = () => {
|
|
2
|
-
var _global_process_release, _global_process, _global;
|
|
3
|
-
if (((_global = global) === null || _global === void 0 ? void 0 : (_global_process = _global.process) === null || _global_process === void 0 ? void 0 : (_global_process_release = _global_process.release) === null || _global_process_release === void 0 ? void 0 : _global_process_release.name) === "node") {
|
|
4
|
-
return "node";
|
|
5
|
-
}
|
|
6
|
-
return "other";
|
|
7
|
-
};
|
|
8
|
-
const checkIsProd = () => {
|
|
9
|
-
const env = getRuntimeEnv();
|
|
10
|
-
switch (env) {
|
|
11
|
-
case "node":
|
|
12
|
-
return process.env.NODE_ENV === "production";
|
|
13
|
-
default:
|
|
14
|
-
return false;
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
export {
|
|
18
|
-
checkIsProd,
|
|
19
|
-
getRuntimeEnv
|
|
20
|
-
};
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
const ERROR_PAGE_TEXT = {
|
|
2
|
-
404: "This page could not be found.",
|
|
3
|
-
500: "Internal Server Error."
|
|
4
|
-
};
|
|
5
|
-
const createErrorHtml = (status) => {
|
|
6
|
-
const text = ERROR_PAGE_TEXT[status] || "";
|
|
7
|
-
const title = `${status}: ${text}`;
|
|
8
|
-
return `<!DOCTYPE html>
|
|
9
|
-
<html lang="en">
|
|
10
|
-
<head>
|
|
11
|
-
<meta charset="utf-8">
|
|
12
|
-
<meta name="viewport" content="width=device-width">
|
|
13
|
-
<title>${title}</title>
|
|
14
|
-
<style>
|
|
15
|
-
html,body {
|
|
16
|
-
margin: 0;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
.page-container {
|
|
20
|
-
color: #000;
|
|
21
|
-
background: #fff;
|
|
22
|
-
height: 100vh;
|
|
23
|
-
text-align: center;
|
|
24
|
-
display: flex;
|
|
25
|
-
flex-direction: column;
|
|
26
|
-
align-items: center;
|
|
27
|
-
justify-content: center;
|
|
28
|
-
}
|
|
29
|
-
</style>
|
|
30
|
-
</head>
|
|
31
|
-
<body>
|
|
32
|
-
<div class="page-container">
|
|
33
|
-
<h1>${status}</h1>
|
|
34
|
-
<div>${text}</div>
|
|
35
|
-
</body>
|
|
36
|
-
</html>
|
|
37
|
-
`;
|
|
38
|
-
};
|
|
39
|
-
export {
|
|
40
|
-
createErrorHtml
|
|
41
|
-
};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export * from "./env";
|
|
2
|
-
export * from "./request";
|
|
3
|
-
export * from "./serverConfig";
|
|
4
|
-
export * from "./debug";
|
|
5
|
-
export * from "./transformStream";
|
|
6
|
-
export * from "./middlewareCollector";
|
|
7
|
-
export * from "./error";
|
|
8
|
-
export * from "./warmup";
|
|
9
|
-
export * from "./entry";
|
|
10
|
-
export * from "./path";
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
const mergeExtension = (users) => {
|
|
2
|
-
const output = [];
|
|
3
|
-
return {
|
|
4
|
-
middleware: output.concat(users)
|
|
5
|
-
};
|
|
6
|
-
};
|
|
7
|
-
const createMiddlewareCollecter = () => {
|
|
8
|
-
const webMiddlewares = [];
|
|
9
|
-
const apiMiddlewares = [];
|
|
10
|
-
const addWebMiddleware = (input) => {
|
|
11
|
-
webMiddlewares.push(input);
|
|
12
|
-
};
|
|
13
|
-
const addAPIMiddleware = (input) => {
|
|
14
|
-
apiMiddlewares.push(input);
|
|
15
|
-
};
|
|
16
|
-
const getMiddlewares = () => ({
|
|
17
|
-
web: webMiddlewares,
|
|
18
|
-
api: apiMiddlewares
|
|
19
|
-
});
|
|
20
|
-
return {
|
|
21
|
-
getMiddlewares,
|
|
22
|
-
addWebMiddleware,
|
|
23
|
-
addAPIMiddleware
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
export {
|
|
27
|
-
createMiddlewareCollecter,
|
|
28
|
-
mergeExtension
|
|
29
|
-
};
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
function parseQuery(request) {
|
|
2
|
-
const { url } = request;
|
|
3
|
-
const q = url.split("?")[1];
|
|
4
|
-
const query = {};
|
|
5
|
-
if (q) {
|
|
6
|
-
q.split("&").forEach((item) => {
|
|
7
|
-
const [key, value] = item.split("=");
|
|
8
|
-
query[key] = value;
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
return query;
|
|
12
|
-
}
|
|
13
|
-
function parseHeaders(request) {
|
|
14
|
-
const headersData = {};
|
|
15
|
-
request.headers.forEach((value, key) => {
|
|
16
|
-
headersData[key] = value;
|
|
17
|
-
});
|
|
18
|
-
return headersData;
|
|
19
|
-
}
|
|
20
|
-
function getPathname(request) {
|
|
21
|
-
const match = request.url.match(/^https?:\/\/[^/]+(\/[^?]*)/);
|
|
22
|
-
return match ? match[1] : "/";
|
|
23
|
-
}
|
|
24
|
-
function getHost(request) {
|
|
25
|
-
const { headers } = request;
|
|
26
|
-
let host = headers.get("X-Forwarded-Host");
|
|
27
|
-
if (!host) {
|
|
28
|
-
host = headers.get("Host");
|
|
29
|
-
}
|
|
30
|
-
host = (host === null || host === void 0 ? void 0 : host.split(/\s*,\s*/, 1)[0]) || "undefined";
|
|
31
|
-
return host;
|
|
32
|
-
}
|
|
33
|
-
export {
|
|
34
|
-
getHost,
|
|
35
|
-
getPathname,
|
|
36
|
-
parseHeaders,
|
|
37
|
-
parseQuery
|
|
38
|
-
};
|