@modern-js/server-core 2.54.6 → 2.56.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/adapters/node/plugins/resource.js +1 -1
- package/dist/cjs/constants.js +12 -9
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/plugins/customServer/index.js +5 -5
- package/dist/cjs/plugins/index.js +7 -3
- package/dist/cjs/plugins/log.js +3 -3
- package/dist/cjs/plugins/monitors.js +183 -0
- package/dist/cjs/plugins/render/dataHandler.js +3 -2
- package/dist/cjs/plugins/render/index.js +6 -4
- package/dist/cjs/plugins/render/render.js +32 -15
- package/dist/cjs/plugins/render/ssrCache.js +72 -83
- package/dist/cjs/plugins/render/ssrRender.js +40 -145
- package/dist/cjs/types/requestHandler.js +16 -0
- package/dist/cjs/utils/error.js +3 -3
- package/dist/cjs/utils/transformStream.js +1 -1
- package/dist/esm/adapters/node/plugins/resource.js +1 -1
- package/dist/esm/constants.js +10 -8
- package/dist/esm/index.js +1 -0
- package/dist/esm/plugins/customServer/index.js +8 -8
- package/dist/esm/plugins/index.js +4 -2
- package/dist/esm/plugins/log.js +4 -4
- package/dist/esm/plugins/monitors.js +261 -0
- package/dist/esm/plugins/render/dataHandler.js +4 -3
- package/dist/esm/plugins/render/index.js +6 -4
- package/dist/esm/plugins/render/render.js +51 -24
- package/dist/esm/plugins/render/ssrCache.js +101 -131
- package/dist/esm/plugins/render/ssrRender.js +55 -175
- package/dist/esm/types/requestHandler.js +0 -0
- package/dist/esm/utils/error.js +3 -3
- package/dist/esm/utils/transformStream.js +16 -2
- package/dist/esm-node/adapters/node/plugins/resource.js +1 -1
- package/dist/esm-node/constants.js +10 -8
- package/dist/esm-node/index.js +1 -0
- package/dist/esm-node/plugins/customServer/index.js +6 -6
- package/dist/esm-node/plugins/index.js +4 -2
- package/dist/esm-node/plugins/log.js +3 -3
- package/dist/esm-node/plugins/monitors.js +156 -0
- package/dist/esm-node/plugins/render/dataHandler.js +3 -2
- package/dist/esm-node/plugins/render/index.js +5 -3
- package/dist/esm-node/plugins/render/render.js +33 -16
- package/dist/esm-node/plugins/render/ssrCache.js +73 -74
- package/dist/esm-node/plugins/render/ssrRender.js +43 -137
- package/dist/esm-node/types/requestHandler.js +0 -0
- package/dist/esm-node/utils/error.js +3 -3
- package/dist/esm-node/utils/transformStream.js +1 -1
- package/dist/types/constants.d.ts +2 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/plugins/index.d.ts +1 -1
- package/dist/types/plugins/monitors.d.ts +6 -0
- package/dist/types/plugins/render/dataHandler.d.ts +1 -1
- package/dist/types/plugins/render/render.d.ts +3 -2
- package/dist/types/plugins/render/ssrCache.d.ts +7 -11
- package/dist/types/plugins/render/ssrRender.d.ts +10 -10
- package/dist/types/types/config/html.d.ts +11 -0
- package/dist/types/types/config/output.d.ts +8 -0
- package/dist/types/types/render.d.ts +7 -4
- package/dist/types/types/requestHandler.d.ts +41 -0
- package/dist/types/types/server.d.ts +13 -4
- package/dist/types/utils/error.d.ts +2 -2
- package/dist/types/utils/transformStream.d.ts +1 -1
- package/package.json +7 -8
- package/dist/cjs/plugins/monitor.js +0 -87
- package/dist/cjs/plugins/render/serverTiming.js +0 -40
- package/dist/esm/plugins/monitor.js +0 -120
- package/dist/esm/plugins/render/serverTiming.js +0 -21
- package/dist/esm-node/plugins/monitor.js +0 -62
- package/dist/esm-node/plugins/render/serverTiming.js +0 -16
- package/dist/types/plugins/monitor.d.ts +0 -9
- package/dist/types/plugins/render/serverTiming.d.ts +0 -7
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
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
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
var ssrCache_exports = {};
|
|
30
20
|
__export(ssrCache_exports, {
|
|
@@ -33,72 +23,42 @@ __export(ssrCache_exports, {
|
|
|
33
23
|
});
|
|
34
24
|
module.exports = __toCommonJS(ssrCache_exports);
|
|
35
25
|
var import_storer = require("@modern-js/runtime-utils/storer");
|
|
26
|
+
var import_constants = require("../../constants");
|
|
36
27
|
var import_utils = require("../../utils");
|
|
37
|
-
async function processCache(key,
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const current = Date.now();
|
|
45
|
-
const cache = {
|
|
46
|
-
val: renderResult,
|
|
47
|
-
cursor: current
|
|
48
|
-
};
|
|
49
|
-
await container.set(key, JSON.stringify(cache), {
|
|
50
|
-
ttl
|
|
51
|
-
});
|
|
52
|
-
return {
|
|
53
|
-
data: renderResult,
|
|
54
|
-
status
|
|
55
|
-
};
|
|
56
|
-
} else {
|
|
57
|
-
const { Readable } = await Promise.resolve().then(() => __toESM(require("stream"))).catch((_) => ({
|
|
58
|
-
Readable: void 0
|
|
59
|
-
}));
|
|
60
|
-
const runtimeEnv = (0, import_utils.getRuntimeEnv)();
|
|
61
|
-
const streamModule = "../../adapters/node/polyfills/stream";
|
|
62
|
-
const { createReadableStreamFromReadable } = runtimeEnv === "node" ? await Promise.resolve().then(() => __toESM(require(streamModule))).catch((_) => ({
|
|
63
|
-
createReadableStreamFromReadable: void 0
|
|
64
|
-
})) : {
|
|
65
|
-
createReadableStreamFromReadable: void 0
|
|
66
|
-
};
|
|
67
|
-
const body = (
|
|
68
|
-
// TODO: remove node:stream, move it to ssr entry.
|
|
69
|
-
Readable && renderResult instanceof Readable ? createReadableStreamFromReadable === null || createReadableStreamFromReadable === void 0 ? void 0 : createReadableStreamFromReadable(renderResult) : renderResult
|
|
70
|
-
);
|
|
28
|
+
async function processCache({ request, key, requestHandler, requestHandlerOptions, ttl, container, cacheStatus }) {
|
|
29
|
+
const response = await requestHandler(request, requestHandlerOptions);
|
|
30
|
+
const decoder = new TextDecoder();
|
|
31
|
+
if (response.body) {
|
|
32
|
+
const stream = (0, import_utils.createTransformStream)();
|
|
33
|
+
const reader = response.body.getReader();
|
|
34
|
+
const writer = stream.writable.getWriter();
|
|
71
35
|
let html = "";
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
36
|
+
const push = () => reader.read().then(({ done, value }) => {
|
|
37
|
+
if (done) {
|
|
38
|
+
const current = Date.now();
|
|
39
|
+
const cache = {
|
|
40
|
+
val: html,
|
|
41
|
+
cursor: current
|
|
42
|
+
};
|
|
43
|
+
container.set(key, JSON.stringify(cache), {
|
|
44
|
+
ttl
|
|
45
|
+
});
|
|
46
|
+
writer.close();
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const content = decoder.decode(value);
|
|
50
|
+
html += content;
|
|
51
|
+
writer.write(value);
|
|
52
|
+
push();
|
|
75
53
|
});
|
|
76
|
-
const reader = body.getReader();
|
|
77
|
-
const writer = stream.writable.getWriter();
|
|
78
|
-
const push = () => {
|
|
79
|
-
reader.read().then(({ done, value }) => {
|
|
80
|
-
if (done) {
|
|
81
|
-
const current = Date.now();
|
|
82
|
-
const cache = {
|
|
83
|
-
val: html,
|
|
84
|
-
cursor: current
|
|
85
|
-
};
|
|
86
|
-
container.set(key, JSON.stringify(cache), {
|
|
87
|
-
ttl
|
|
88
|
-
});
|
|
89
|
-
writer.close();
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
writer.write(value);
|
|
93
|
-
push();
|
|
94
|
-
});
|
|
95
|
-
};
|
|
96
54
|
push();
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
status
|
|
100
|
-
|
|
55
|
+
cacheStatus && response.headers.set(import_constants.X_RENDER_CACHE, cacheStatus);
|
|
56
|
+
return new Response(stream.readable, {
|
|
57
|
+
status: response.status,
|
|
58
|
+
headers: response.headers
|
|
59
|
+
});
|
|
101
60
|
}
|
|
61
|
+
return response;
|
|
102
62
|
}
|
|
103
63
|
const CACHE_NAMESPACE = "__ssr__cache";
|
|
104
64
|
const storage = (0, import_storer.createMemoryStorage)(CACHE_NAMESPACE);
|
|
@@ -145,7 +105,7 @@ function matchCacheControl(cacheOption, req) {
|
|
|
145
105
|
}
|
|
146
106
|
}
|
|
147
107
|
async function getCacheResult(request, options) {
|
|
148
|
-
const { cacheControl,
|
|
108
|
+
const { cacheControl, container = storage, requestHandler, requestHandlerOptions } = options;
|
|
149
109
|
const key = computedKey(request, cacheControl);
|
|
150
110
|
const value = await container.get(key);
|
|
151
111
|
const { maxAge, staleWhileRevalidate } = cacheControl;
|
|
@@ -154,21 +114,50 @@ async function getCacheResult(request, options) {
|
|
|
154
114
|
const cache = JSON.parse(value);
|
|
155
115
|
const interval = Date.now() - cache.cursor;
|
|
156
116
|
if (interval <= maxAge) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
117
|
+
const cacheStatus = "hit";
|
|
118
|
+
return new Response(cache.val, {
|
|
119
|
+
headers: {
|
|
120
|
+
[import_constants.X_RENDER_CACHE]: cacheStatus
|
|
121
|
+
}
|
|
122
|
+
});
|
|
161
123
|
} else if (interval <= staleWhileRevalidate + maxAge) {
|
|
162
|
-
processCache(
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
124
|
+
processCache({
|
|
125
|
+
key,
|
|
126
|
+
request,
|
|
127
|
+
requestHandler,
|
|
128
|
+
requestHandlerOptions,
|
|
129
|
+
ttl,
|
|
130
|
+
container
|
|
131
|
+
}).then(async (response) => {
|
|
132
|
+
await response.text();
|
|
133
|
+
});
|
|
134
|
+
const cacheStatus = "stale";
|
|
135
|
+
return new Response(cache.val, {
|
|
136
|
+
headers: {
|
|
137
|
+
[import_constants.X_RENDER_CACHE]: cacheStatus
|
|
138
|
+
}
|
|
139
|
+
});
|
|
167
140
|
} else {
|
|
168
|
-
return processCache(
|
|
141
|
+
return processCache({
|
|
142
|
+
key,
|
|
143
|
+
request,
|
|
144
|
+
requestHandler,
|
|
145
|
+
requestHandlerOptions,
|
|
146
|
+
ttl,
|
|
147
|
+
container,
|
|
148
|
+
cacheStatus: "expired"
|
|
149
|
+
});
|
|
169
150
|
}
|
|
170
151
|
} else {
|
|
171
|
-
return processCache(
|
|
152
|
+
return processCache({
|
|
153
|
+
key,
|
|
154
|
+
request,
|
|
155
|
+
requestHandler,
|
|
156
|
+
requestHandlerOptions,
|
|
157
|
+
ttl,
|
|
158
|
+
container,
|
|
159
|
+
cacheStatus: "miss"
|
|
160
|
+
});
|
|
172
161
|
}
|
|
173
162
|
}
|
|
174
163
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,48 +15,22 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
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
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
var ssrRender_exports = {};
|
|
30
20
|
__export(ssrRender_exports, {
|
|
31
|
-
getPathnameFromNodeReq: () => getPathnameFromNodeReq,
|
|
32
21
|
ssrRender: () => ssrRender
|
|
33
22
|
});
|
|
34
23
|
module.exports = __toCommonJS(ssrRender_exports);
|
|
35
24
|
var import_constants = require("@modern-js/utils/universal/constants");
|
|
36
|
-
var isbot = __toESM(require("isbot"));
|
|
37
25
|
var import_utils = require("../../utils");
|
|
38
26
|
var import_constants2 = require("../../constants");
|
|
39
|
-
var import_serverTiming = require("./serverTiming");
|
|
40
27
|
var import_ssrCache = require("./ssrCache");
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
},
|
|
44
|
-
reportError() {
|
|
45
|
-
},
|
|
46
|
-
reportTiming() {
|
|
47
|
-
},
|
|
48
|
-
reportInfo() {
|
|
49
|
-
},
|
|
50
|
-
reportWarn() {
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
async function ssrRender(request, { routeInfo, html, staticGenerate, nonce, metaName, reporter, logger, nodeReq, serverManifest, locals, params, metrics, loaderContext, cacheConfig }) {
|
|
28
|
+
const SERVER_RUNTIME_ENTRY = "requestHandler";
|
|
29
|
+
async function ssrRender(request, { routeInfo, html, config: userConfig, staticGenerate, nodeReq, serverManifest, locals, params, loaderContext, reporter, cacheConfig, logger, metrics, onError, onTiming }) {
|
|
54
30
|
var _serverManifest_renderBundles;
|
|
55
31
|
const { entryName } = routeInfo;
|
|
56
32
|
const loadableStats = serverManifest.loadableStats || {};
|
|
57
33
|
const routeManifest = serverManifest.routeManifest || {};
|
|
58
|
-
const host = (0, import_utils.getHost)(request);
|
|
59
|
-
const isSpider = isbot.default(request.headers.get("user-agent"));
|
|
60
|
-
const responseProxy = new ResponseProxy();
|
|
61
|
-
const query = (0, import_utils.parseQuery)(request);
|
|
62
34
|
const headers = (0, import_utils.parseHeaders)(request);
|
|
63
35
|
if (nodeReq) {
|
|
64
36
|
for (const key in nodeReq.headers) {
|
|
@@ -67,103 +39,46 @@ async function ssrRender(request, { routeInfo, html, staticGenerate, nonce, meta
|
|
|
67
39
|
}
|
|
68
40
|
}
|
|
69
41
|
}
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
},
|
|
84
|
-
status(code) {
|
|
85
|
-
responseProxy.status = code;
|
|
86
|
-
},
|
|
87
|
-
locals: locals || {}
|
|
42
|
+
const renderBundle = (_serverManifest_renderBundles = serverManifest.renderBundles) === null || _serverManifest_renderBundles === void 0 ? void 0 : _serverManifest_renderBundles[entryName || import_constants.MAIN_ENTRY_NAME];
|
|
43
|
+
if (!renderBundle) {
|
|
44
|
+
throw new Error(`Can't found renderBundle ${entryName || import_constants.MAIN_ENTRY_NAME}`);
|
|
45
|
+
}
|
|
46
|
+
const requestHandler = await renderBundle[SERVER_RUNTIME_ENTRY];
|
|
47
|
+
const config = createRequestHandlerConfig(userConfig);
|
|
48
|
+
const requestHandlerOptions = {
|
|
49
|
+
resource: {
|
|
50
|
+
route: routeInfo,
|
|
51
|
+
loadableStats,
|
|
52
|
+
routeManifest,
|
|
53
|
+
htmlTemplate: html,
|
|
54
|
+
entryName: entryName || import_constants.MAIN_ENTRY_NAME
|
|
88
55
|
},
|
|
89
|
-
|
|
90
|
-
template: html,
|
|
91
|
-
loadableStats,
|
|
56
|
+
params,
|
|
92
57
|
loaderContext,
|
|
93
|
-
|
|
94
|
-
|
|
58
|
+
config,
|
|
59
|
+
locals,
|
|
60
|
+
reporter,
|
|
95
61
|
staticGenerate,
|
|
96
62
|
logger,
|
|
97
63
|
metrics,
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
/** @deprecated node req */
|
|
101
|
-
req: nodeReq || request,
|
|
102
|
-
/** @deprecated node res */
|
|
103
|
-
res: void 0,
|
|
104
|
-
isSpider,
|
|
105
|
-
nonce
|
|
64
|
+
onError,
|
|
65
|
+
onTiming
|
|
106
66
|
};
|
|
107
|
-
const renderBundle = (_serverManifest_renderBundles = serverManifest.renderBundles) === null || _serverManifest_renderBundles === void 0 ? void 0 : _serverManifest_renderBundles[entryName || import_constants.MAIN_ENTRY_NAME];
|
|
108
|
-
if (!renderBundle) {
|
|
109
|
-
throw new Error(`Can't found renderBundle ${entryName || import_constants.MAIN_ENTRY_NAME}`);
|
|
110
|
-
}
|
|
111
|
-
const runtimeEnv = (0, import_utils.getRuntimeEnv)();
|
|
112
|
-
let ssrResult;
|
|
113
|
-
let cacheStatus;
|
|
114
|
-
const render = renderBundle[import_constants.SERVER_RENDER_FUNCTION_NAME];
|
|
115
67
|
const cacheControl = await (0, import_ssrCache.matchCacheControl)(cacheConfig === null || cacheConfig === void 0 ? void 0 : cacheConfig.strategy, nodeReq || new IncomingMessgeProxy(request));
|
|
68
|
+
let response;
|
|
116
69
|
if (cacheControl) {
|
|
117
|
-
|
|
70
|
+
response = await (0, import_ssrCache.getCacheResult)(request, {
|
|
118
71
|
cacheControl,
|
|
119
72
|
container: cacheConfig === null || cacheConfig === void 0 ? void 0 : cacheConfig.container,
|
|
120
|
-
|
|
121
|
-
|
|
73
|
+
requestHandler,
|
|
74
|
+
requestHandlerOptions
|
|
122
75
|
});
|
|
123
|
-
ssrResult = data2;
|
|
124
|
-
cacheStatus = status;
|
|
125
76
|
} else {
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
const { redirection } = ssrContext;
|
|
129
|
-
if (cacheStatus) {
|
|
130
|
-
responseProxy.headers.set(import_constants2.X_RENDER_CACHE, cacheStatus);
|
|
131
|
-
}
|
|
132
|
-
responseProxy.headers.set(import_constants2.X_MODERNJS_RENDER, "server");
|
|
133
|
-
if (redirection.url) {
|
|
134
|
-
const { headers: headers2 } = responseProxy;
|
|
135
|
-
headers2.set("Location", redirection.url);
|
|
136
|
-
return new Response(null, {
|
|
137
|
-
status: redirection.status || 302,
|
|
138
|
-
headers: {
|
|
139
|
-
Location: redirection.url
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
const { Readable } = await Promise.resolve().then(() => __toESM(require("stream"))).catch((_) => ({
|
|
144
|
-
Readable: void 0
|
|
145
|
-
}));
|
|
146
|
-
const streamModule = "../../adapters/node/polyfills/stream";
|
|
147
|
-
const { createReadableStreamFromReadable } = runtimeEnv === "node" ? await Promise.resolve().then(() => __toESM(require(streamModule))).catch((_) => ({
|
|
148
|
-
createReadableStreamFromReadable: void 0
|
|
149
|
-
})) : {
|
|
150
|
-
createReadableStreamFromReadable: void 0
|
|
151
|
-
};
|
|
152
|
-
const data = Readable && ssrResult instanceof Readable ? (createReadableStreamFromReadable === null || createReadableStreamFromReadable === void 0 ? void 0 : createReadableStreamFromReadable(ssrResult)) || "" : ssrResult;
|
|
153
|
-
if (typeof data !== "string") {
|
|
154
|
-
responseProxy.headers.set("transfer-encoding", "chunked");
|
|
155
|
-
}
|
|
156
|
-
return new Response(data, {
|
|
157
|
-
status: responseProxy.status,
|
|
158
|
-
headers: responseProxy.headers
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
class ResponseProxy {
|
|
162
|
-
constructor() {
|
|
163
|
-
this.headers = new Headers();
|
|
164
|
-
this.status = 200;
|
|
165
|
-
this.headers.set("content-type", "text/html; charset=UTF-8");
|
|
77
|
+
response = await requestHandler(request, requestHandlerOptions);
|
|
166
78
|
}
|
|
79
|
+
response.headers.set(import_constants2.X_MODERNJS_RENDER, "server");
|
|
80
|
+
response.headers.set("content-type", "text/html; charset=UTF-8");
|
|
81
|
+
return response;
|
|
167
82
|
}
|
|
168
83
|
class IncomingMessgeProxy {
|
|
169
84
|
constructor(req) {
|
|
@@ -175,39 +90,19 @@ class IncomingMessgeProxy {
|
|
|
175
90
|
this.url = (0, import_utils.getPathname)(req);
|
|
176
91
|
}
|
|
177
92
|
}
|
|
178
|
-
function
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
host = nodeReq.headers.host;
|
|
190
|
-
}
|
|
191
|
-
host = host.split(/\s*,\s*/, 1)[0] || "undefined";
|
|
192
|
-
return host;
|
|
193
|
-
}
|
|
194
|
-
const href = `${getProtocal()}://${getHost2()}${nodeReq.url || ""}`;
|
|
195
|
-
return href;
|
|
196
|
-
}
|
|
197
|
-
function getPathnameFromNodeReq(nodeReq) {
|
|
198
|
-
const { url } = nodeReq;
|
|
199
|
-
if (!url) {
|
|
200
|
-
return "/";
|
|
201
|
-
}
|
|
202
|
-
const match = url.match(/\/[^?]*/);
|
|
203
|
-
let pathname = match ? match[0] : "/";
|
|
204
|
-
if (pathname !== "/" && pathname.endsWith("/")) {
|
|
205
|
-
pathname = pathname.slice(0, -1);
|
|
206
|
-
}
|
|
207
|
-
return pathname;
|
|
93
|
+
function createRequestHandlerConfig(userConfig) {
|
|
94
|
+
const { output, server, security, html } = userConfig;
|
|
95
|
+
return {
|
|
96
|
+
ssr: server === null || server === void 0 ? void 0 : server.ssr,
|
|
97
|
+
ssrByEntries: server === null || server === void 0 ? void 0 : server.ssrByEntries,
|
|
98
|
+
nonce: security === null || security === void 0 ? void 0 : security.nonce,
|
|
99
|
+
enableInlineScripts: output === null || output === void 0 ? void 0 : output.enableInlineScripts,
|
|
100
|
+
enableInlineStyles: output === null || output === void 0 ? void 0 : output.enableInlineStyles,
|
|
101
|
+
crossorigin: html === null || html === void 0 ? void 0 : html.crossorigin,
|
|
102
|
+
scriptLoading: html === null || html === void 0 ? void 0 : html.scriptLoading
|
|
103
|
+
};
|
|
208
104
|
}
|
|
209
105
|
// Annotate the CommonJS export names for ESM import in node:
|
|
210
106
|
0 && (module.exports = {
|
|
211
|
-
getPathnameFromNodeReq,
|
|
212
107
|
ssrRender
|
|
213
108
|
});
|
|
@@ -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 requestHandler_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(requestHandler_exports);
|
package/dist/cjs/utils/error.js
CHANGED
|
@@ -68,11 +68,11 @@ var ErrorDigest;
|
|
|
68
68
|
ErrorDigest2["EINTER"] = "Internal server error";
|
|
69
69
|
ErrorDigest2["ERENDER"] = "SSR render failed";
|
|
70
70
|
})(ErrorDigest || (ErrorDigest = {}));
|
|
71
|
-
function onError(digest, error,
|
|
71
|
+
function onError(digest, error, monitors, req) {
|
|
72
72
|
const headerData = req && (0, import_request.parseHeaders)(req);
|
|
73
73
|
headerData && delete headerData.cookie;
|
|
74
|
-
if (
|
|
75
|
-
|
|
74
|
+
if (monitors) {
|
|
75
|
+
monitors.error(req ? `Server Error - ${digest}, error = %s, req.url = %s, req.headers = %o` : `Server Error - ${digest}, error = %s`, error instanceof Error ? error.stack || error.message : error, req === null || req === void 0 ? void 0 : req.url, headerData);
|
|
76
76
|
} else if (req) {
|
|
77
77
|
console.error(`Server Error - ${digest}, error = ${error instanceof Error ? error.stack || error.message : error}, req.url = ${req.url}, req.headers = ${JSON.stringify(headerData)}`);
|
|
78
78
|
} else {
|
|
@@ -28,7 +28,7 @@ function createTransformStream(fn) {
|
|
|
28
28
|
return new TransformStream({
|
|
29
29
|
async transform(chunk, controller) {
|
|
30
30
|
const content = decoder.decode(chunk);
|
|
31
|
-
const newContent = await fn(content);
|
|
31
|
+
const newContent = fn ? await fn(content) : content;
|
|
32
32
|
controller.enqueue(encoder.encode(newContent));
|
|
33
33
|
}
|
|
34
34
|
});
|
|
@@ -137,7 +137,7 @@ var loadBundle = function() {
|
|
|
137
137
|
return [
|
|
138
138
|
2,
|
|
139
139
|
dynamicImport(filepath).catch(function(e) {
|
|
140
|
-
logger.error("Load ".concat(filepath, " bundle failed, error = %s"), _instanceof(e, Error) ? e.stack || e.message : e);
|
|
140
|
+
logger === null || logger === void 0 ? void 0 : logger.error("Load ".concat(filepath, " bundle failed, error = %s"), _instanceof(e, Error) ? e.stack || e.message : e);
|
|
141
141
|
return void 0;
|
|
142
142
|
})
|
|
143
143
|
];
|
package/dist/esm/constants.js
CHANGED
|
@@ -15,19 +15,21 @@ var REPLACE_REG = {
|
|
|
15
15
|
body: "</body>"
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
|
-
var
|
|
19
|
-
(function(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
})(
|
|
18
|
+
var ServerTimings;
|
|
19
|
+
(function(ServerTimings2) {
|
|
20
|
+
ServerTimings2["SERVER_HANDLE_REQUEST"] = "server-handle-request";
|
|
21
|
+
ServerTimings2["SERVER_MIDDLEWARE"] = "server-middleware";
|
|
22
|
+
ServerTimings2["SERVER_HOOK_AFTER_RENDER"] = "server-hook-after-render";
|
|
23
|
+
ServerTimings2["SERVER_HOOK_AFTER_MATCH"] = "server-hook-after-match";
|
|
24
|
+
})(ServerTimings || (ServerTimings = {}));
|
|
25
|
+
var SERVER_TIMING = "Server-Timing";
|
|
25
26
|
var X_RENDER_CACHE = "x-render-cache";
|
|
26
27
|
var X_MODERNJS_RENDER = "x-modernjs-render";
|
|
27
28
|
export {
|
|
28
29
|
AGGRED_DIR,
|
|
29
30
|
REPLACE_REG,
|
|
30
|
-
|
|
31
|
+
SERVER_TIMING,
|
|
32
|
+
ServerTimings,
|
|
31
33
|
X_MODERNJS_RENDER,
|
|
32
34
|
X_RENDER_CACHE
|
|
33
35
|
};
|
package/dist/esm/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
|
3
3
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
4
4
|
import { time } from "@modern-js/runtime-utils/time";
|
|
5
5
|
import { transformResponse } from "../../utils";
|
|
6
|
-
import {
|
|
6
|
+
import { ServerTimings } from "../../constants";
|
|
7
7
|
import { getLoaderCtx } from "./loader";
|
|
8
8
|
import { getAfterMatchCtx, getAfterRenderCtx, createCustomMiddlewaresCtx, createAfterStreamingRenderContext } from "./context";
|
|
9
9
|
import { createBaseHookContext } from "./base";
|
|
@@ -37,14 +37,14 @@ var CustomServer = /* @__PURE__ */ function() {
|
|
|
37
37
|
var _this = this;
|
|
38
38
|
return function() {
|
|
39
39
|
var _ref = _async_to_generator(function(c, next) {
|
|
40
|
-
var routeInfo,
|
|
40
|
+
var routeInfo, monitors, baseHookCtx, afterMatchCtx, getCost, cost, _afterMatchCtx_router, url, status, current, rewriteRoute, afterStreamingRenderContext, afterRenderCtx, getCost1, cost1, newBody;
|
|
41
41
|
return _ts_generator(this, function(_state) {
|
|
42
42
|
switch (_state.label) {
|
|
43
43
|
case 0:
|
|
44
44
|
routeInfo = routes.find(function(route) {
|
|
45
45
|
return route.entryName === entryName;
|
|
46
46
|
});
|
|
47
|
-
|
|
47
|
+
monitors = c.get("monitors");
|
|
48
48
|
baseHookCtx = createBaseHookContext(c);
|
|
49
49
|
afterMatchCtx = getAfterMatchCtx(entryName, baseHookCtx);
|
|
50
50
|
getCost = time();
|
|
@@ -57,7 +57,7 @@ var CustomServer = /* @__PURE__ */ function() {
|
|
|
57
57
|
case 1:
|
|
58
58
|
_state.sent();
|
|
59
59
|
cost = getCost();
|
|
60
|
-
cost && (
|
|
60
|
+
cost && (monitors === null || monitors === void 0 ? void 0 : monitors.timing(ServerTimings.SERVER_HOOK_AFTER_MATCH, cost));
|
|
61
61
|
_afterMatchCtx_router = afterMatchCtx.router, url = _afterMatchCtx_router.url, status = _afterMatchCtx_router.status;
|
|
62
62
|
if (url) {
|
|
63
63
|
return [
|
|
@@ -137,7 +137,7 @@ var CustomServer = /* @__PURE__ */ function() {
|
|
|
137
137
|
case 5:
|
|
138
138
|
_state.sent();
|
|
139
139
|
cost1 = getCost1();
|
|
140
|
-
cost1 && (
|
|
140
|
+
cost1 && (monitors === null || monitors === void 0 ? void 0 : monitors.timing(ServerTimings.SERVER_HOOK_AFTER_RENDER, cost1));
|
|
141
141
|
if (afterRenderCtx.response.private_overrided) {
|
|
142
142
|
return [
|
|
143
143
|
2,
|
|
@@ -187,11 +187,11 @@ var CustomServer = /* @__PURE__ */ function() {
|
|
|
187
187
|
2,
|
|
188
188
|
function() {
|
|
189
189
|
var _ref = _async_to_generator(function(c, next) {
|
|
190
|
-
var _c_env_node_res, _c_env_node, _c_env,
|
|
190
|
+
var _c_env_node_res, _c_env_node, _c_env, monitors, locals, resArgs, customMiddlewareCtx, getCost, cost;
|
|
191
191
|
return _ts_generator(this, function(_state2) {
|
|
192
192
|
switch (_state2.label) {
|
|
193
193
|
case 0:
|
|
194
|
-
|
|
194
|
+
monitors = c.get("monitors");
|
|
195
195
|
locals = {};
|
|
196
196
|
resArgs = {
|
|
197
197
|
headers: new Headers()
|
|
@@ -205,7 +205,7 @@ var CustomServer = /* @__PURE__ */ function() {
|
|
|
205
205
|
case 1:
|
|
206
206
|
_state2.sent();
|
|
207
207
|
cost = getCost();
|
|
208
|
-
cost && (
|
|
208
|
+
cost && (monitors === null || monitors === void 0 ? void 0 : monitors.timing(ServerTimings.SERVER_MIDDLEWARE, cost));
|
|
209
209
|
c.set("locals", locals);
|
|
210
210
|
if (isRedirect(resArgs.headers, resArgs.status)) {
|
|
211
211
|
return [
|
|
@@ -3,13 +3,15 @@ import { faviconPlugin } from "./favicon";
|
|
|
3
3
|
import { processedByPlugin } from "./processedBy";
|
|
4
4
|
import { getLoaderCtx } from "./customServer";
|
|
5
5
|
import { logPlugin } from "./log";
|
|
6
|
-
import {
|
|
6
|
+
import { initMonitorsPlugin, injectServerTiming, injectloggerPluigin } from "./monitors";
|
|
7
7
|
export {
|
|
8
8
|
faviconPlugin,
|
|
9
9
|
getLoaderCtx,
|
|
10
10
|
getRenderHandler,
|
|
11
|
+
initMonitorsPlugin,
|
|
12
|
+
injectServerTiming,
|
|
13
|
+
injectloggerPluigin,
|
|
11
14
|
logPlugin,
|
|
12
|
-
monitorPlugin,
|
|
13
15
|
processedByPlugin,
|
|
14
16
|
renderPlugin
|
|
15
17
|
};
|
package/dist/esm/plugins/log.js
CHANGED
|
@@ -41,13 +41,13 @@ function log(fn, prefix, method, path) {
|
|
|
41
41
|
function logHandler() {
|
|
42
42
|
return function() {
|
|
43
43
|
var _logger = _async_to_generator(function(c, next) {
|
|
44
|
-
var method,
|
|
44
|
+
var method, monitors, path, logFn, start;
|
|
45
45
|
return _ts_generator(this, function(_state) {
|
|
46
46
|
switch (_state.label) {
|
|
47
47
|
case 0:
|
|
48
48
|
method = c.req.method;
|
|
49
|
-
|
|
50
|
-
if (!!
|
|
49
|
+
monitors = c.get("monitors");
|
|
50
|
+
if (!!monitors)
|
|
51
51
|
return [
|
|
52
52
|
3,
|
|
53
53
|
2
|
|
@@ -63,7 +63,7 @@ function logHandler() {
|
|
|
63
63
|
];
|
|
64
64
|
case 2:
|
|
65
65
|
path = getPathname(c.req.raw);
|
|
66
|
-
logFn =
|
|
66
|
+
logFn = monitors.debug;
|
|
67
67
|
log(logFn, "<--", method, path);
|
|
68
68
|
start = Date.now();
|
|
69
69
|
return [
|