@modern-js/server-core 2.55.0 → 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/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/index.js +5 -3
- package/dist/cjs/plugins/render/render.js +4 -11
- package/dist/cjs/utils/error.js +3 -3
- package/dist/esm/adapters/node/plugins/resource.js +1 -1
- package/dist/esm/constants.js +10 -8
- 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/index.js +5 -3
- package/dist/esm/plugins/render/render.js +5 -12
- package/dist/esm/utils/error.js +3 -3
- package/dist/esm-node/adapters/node/plugins/resource.js +1 -1
- package/dist/esm-node/constants.js +10 -8
- 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/index.js +4 -2
- package/dist/esm-node/plugins/render/render.js +4 -11
- package/dist/esm-node/utils/error.js +3 -3
- package/dist/types/constants.d.ts +2 -1
- package/dist/types/plugins/index.d.ts +1 -1
- package/dist/types/plugins/monitors.d.ts +6 -0
- package/dist/types/types/render.d.ts +7 -4
- package/dist/types/types/requestHandler.d.ts +0 -2
- package/dist/types/types/server.d.ts +6 -2
- package/dist/types/utils/error.d.ts +2 -2
- package/package.json +7 -7
- package/dist/cjs/plugins/monitor.js +0 -87
- package/dist/cjs/plugins/render/serverTiming.js +0 -42
- package/dist/esm/plugins/monitor.js +0 -120
- package/dist/esm/plugins/render/serverTiming.js +0 -29
- package/dist/esm-node/plugins/monitor.js +0 -62
- package/dist/esm-node/plugins/render/serverTiming.js +0 -18
- package/dist/types/plugins/monitor.d.ts +0 -9
- package/dist/types/plugins/render/serverTiming.d.ts +0 -8
|
@@ -77,7 +77,7 @@ const loadBundle = async (filepath, logger) => {
|
|
|
77
77
|
return void 0;
|
|
78
78
|
}
|
|
79
79
|
return dynamicImport(filepath).catch((e) => {
|
|
80
|
-
logger.error(`Load ${filepath} bundle failed, error = %s`, e instanceof Error ? e.stack || e.message : e);
|
|
80
|
+
logger === null || logger === void 0 ? void 0 : logger.error(`Load ${filepath} bundle failed, error = %s`, e instanceof Error ? e.stack || e.message : e);
|
|
81
81
|
return void 0;
|
|
82
82
|
});
|
|
83
83
|
};
|
package/dist/cjs/constants.js
CHANGED
|
@@ -20,7 +20,8 @@ var constants_exports = {};
|
|
|
20
20
|
__export(constants_exports, {
|
|
21
21
|
AGGRED_DIR: () => AGGRED_DIR,
|
|
22
22
|
REPLACE_REG: () => REPLACE_REG,
|
|
23
|
-
|
|
23
|
+
SERVER_TIMING: () => SERVER_TIMING,
|
|
24
|
+
ServerTimings: () => ServerTimings,
|
|
24
25
|
X_MODERNJS_RENDER: () => X_MODERNJS_RENDER,
|
|
25
26
|
X_RENDER_CACHE: () => X_RENDER_CACHE
|
|
26
27
|
});
|
|
@@ -42,20 +43,22 @@ const REPLACE_REG = {
|
|
|
42
43
|
body: "</body>"
|
|
43
44
|
}
|
|
44
45
|
};
|
|
45
|
-
var
|
|
46
|
-
(function(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
})(
|
|
46
|
+
var ServerTimings;
|
|
47
|
+
(function(ServerTimings2) {
|
|
48
|
+
ServerTimings2["SERVER_HANDLE_REQUEST"] = "server-handle-request";
|
|
49
|
+
ServerTimings2["SERVER_MIDDLEWARE"] = "server-middleware";
|
|
50
|
+
ServerTimings2["SERVER_HOOK_AFTER_RENDER"] = "server-hook-after-render";
|
|
51
|
+
ServerTimings2["SERVER_HOOK_AFTER_MATCH"] = "server-hook-after-match";
|
|
52
|
+
})(ServerTimings || (ServerTimings = {}));
|
|
53
|
+
const SERVER_TIMING = "Server-Timing";
|
|
52
54
|
const X_RENDER_CACHE = "x-render-cache";
|
|
53
55
|
const X_MODERNJS_RENDER = "x-modernjs-render";
|
|
54
56
|
// Annotate the CommonJS export names for ESM import in node:
|
|
55
57
|
0 && (module.exports = {
|
|
56
58
|
AGGRED_DIR,
|
|
57
59
|
REPLACE_REG,
|
|
58
|
-
|
|
60
|
+
SERVER_TIMING,
|
|
61
|
+
ServerTimings,
|
|
59
62
|
X_MODERNJS_RENDER,
|
|
60
63
|
X_RENDER_CACHE
|
|
61
64
|
});
|
|
@@ -40,7 +40,7 @@ class CustomServer {
|
|
|
40
40
|
getHookMiddleware(entryName, routes) {
|
|
41
41
|
return async (c, next) => {
|
|
42
42
|
const routeInfo = routes.find((route) => route.entryName === entryName);
|
|
43
|
-
const
|
|
43
|
+
const monitors = c.get("monitors");
|
|
44
44
|
const baseHookCtx = (0, import_base.createBaseHookContext)(c);
|
|
45
45
|
const afterMatchCtx = (0, import_context.getAfterMatchCtx)(entryName, baseHookCtx);
|
|
46
46
|
const getCost = (0, import_time.time)();
|
|
@@ -48,7 +48,7 @@ class CustomServer {
|
|
|
48
48
|
onLast: noop
|
|
49
49
|
});
|
|
50
50
|
const cost = getCost();
|
|
51
|
-
cost && (
|
|
51
|
+
cost && (monitors === null || monitors === void 0 ? void 0 : monitors.timing(import_constants.ServerTimings.SERVER_HOOK_AFTER_MATCH, cost));
|
|
52
52
|
const { url, status } = afterMatchCtx.router;
|
|
53
53
|
if (url) {
|
|
54
54
|
return c.redirect(url, status);
|
|
@@ -88,7 +88,7 @@ class CustomServer {
|
|
|
88
88
|
onLast: noop
|
|
89
89
|
});
|
|
90
90
|
const cost2 = getCost2();
|
|
91
|
-
cost2 && (
|
|
91
|
+
cost2 && (monitors === null || monitors === void 0 ? void 0 : monitors.timing(import_constants.ServerTimings.SERVER_HOOK_AFTER_RENDER, cost2));
|
|
92
92
|
if (afterRenderCtx.response.private_overrided) {
|
|
93
93
|
return void 0;
|
|
94
94
|
}
|
|
@@ -107,7 +107,7 @@ class CustomServer {
|
|
|
107
107
|
}
|
|
108
108
|
return async (c, next) => {
|
|
109
109
|
var _c_env_node_res, _c_env_node, _c_env;
|
|
110
|
-
const
|
|
110
|
+
const monitors = c.get("monitors");
|
|
111
111
|
const locals = {};
|
|
112
112
|
const resArgs = {
|
|
113
113
|
headers: new Headers()
|
|
@@ -116,7 +116,7 @@ class CustomServer {
|
|
|
116
116
|
const getCost = (0, import_time.time)();
|
|
117
117
|
await serverMiddleware(customMiddlewareCtx);
|
|
118
118
|
const cost = getCost();
|
|
119
|
-
cost && (
|
|
119
|
+
cost && (monitors === null || monitors === void 0 ? void 0 : monitors.timing(import_constants.ServerTimings.SERVER_MIDDLEWARE, cost));
|
|
120
120
|
c.set("locals", locals);
|
|
121
121
|
if (isRedirect(resArgs.headers, resArgs.status)) {
|
|
122
122
|
return c.redirect(resArgs.headers.get("Location") || "", resArgs.status || 302);
|
|
@@ -21,8 +21,10 @@ __export(plugins_exports, {
|
|
|
21
21
|
faviconPlugin: () => import_favicon.faviconPlugin,
|
|
22
22
|
getLoaderCtx: () => import_customServer.getLoaderCtx,
|
|
23
23
|
getRenderHandler: () => import_render.getRenderHandler,
|
|
24
|
+
initMonitorsPlugin: () => import_monitors.initMonitorsPlugin,
|
|
25
|
+
injectServerTiming: () => import_monitors.injectServerTiming,
|
|
26
|
+
injectloggerPluigin: () => import_monitors.injectloggerPluigin,
|
|
24
27
|
logPlugin: () => import_log.logPlugin,
|
|
25
|
-
monitorPlugin: () => import_monitor.monitorPlugin,
|
|
26
28
|
processedByPlugin: () => import_processedBy.processedByPlugin,
|
|
27
29
|
renderPlugin: () => import_render.renderPlugin
|
|
28
30
|
});
|
|
@@ -32,14 +34,16 @@ var import_favicon = require("./favicon");
|
|
|
32
34
|
var import_processedBy = require("./processedBy");
|
|
33
35
|
var import_customServer = require("./customServer");
|
|
34
36
|
var import_log = require("./log");
|
|
35
|
-
var
|
|
37
|
+
var import_monitors = require("./monitors");
|
|
36
38
|
// Annotate the CommonJS export names for ESM import in node:
|
|
37
39
|
0 && (module.exports = {
|
|
38
40
|
faviconPlugin,
|
|
39
41
|
getLoaderCtx,
|
|
40
42
|
getRenderHandler,
|
|
43
|
+
initMonitorsPlugin,
|
|
44
|
+
injectServerTiming,
|
|
45
|
+
injectloggerPluigin,
|
|
41
46
|
logPlugin,
|
|
42
|
-
monitorPlugin,
|
|
43
47
|
processedByPlugin,
|
|
44
48
|
renderPlugin
|
|
45
49
|
});
|
package/dist/cjs/plugins/log.js
CHANGED
|
@@ -62,13 +62,13 @@ function log(fn, prefix, method, path, status = 0, elapsed) {
|
|
|
62
62
|
function logHandler() {
|
|
63
63
|
return async function logger(c, next) {
|
|
64
64
|
const { method } = c.req;
|
|
65
|
-
const
|
|
66
|
-
if (!
|
|
65
|
+
const monitors = c.get("monitors");
|
|
66
|
+
if (!monitors) {
|
|
67
67
|
await next();
|
|
68
68
|
return;
|
|
69
69
|
}
|
|
70
70
|
const path = (0, import_utils.getPathname)(c.req.raw);
|
|
71
|
-
const logFn =
|
|
71
|
+
const logFn = monitors.debug;
|
|
72
72
|
log(logFn, "<--", method, path);
|
|
73
73
|
const start = Date.now();
|
|
74
74
|
await next();
|
|
@@ -0,0 +1,183 @@
|
|
|
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 monitors_exports = {};
|
|
20
|
+
__export(monitors_exports, {
|
|
21
|
+
initMonitorsPlugin: () => initMonitorsPlugin,
|
|
22
|
+
initReporter: () => initReporter,
|
|
23
|
+
injectServerTiming: () => injectServerTiming,
|
|
24
|
+
injectloggerPluigin: () => injectloggerPluigin
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(monitors_exports);
|
|
27
|
+
var import_time = require("@modern-js/runtime-utils/time");
|
|
28
|
+
var import_constants = require("../constants");
|
|
29
|
+
function createMonitors() {
|
|
30
|
+
const coreMonitors = [];
|
|
31
|
+
const log = (level, message, args) => {
|
|
32
|
+
const event = {
|
|
33
|
+
type: "log",
|
|
34
|
+
payload: {
|
|
35
|
+
level,
|
|
36
|
+
message,
|
|
37
|
+
args
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
coreMonitors.forEach((monitor) => monitor(event));
|
|
41
|
+
};
|
|
42
|
+
const mointors = {
|
|
43
|
+
push(monitor) {
|
|
44
|
+
coreMonitors.push(monitor);
|
|
45
|
+
},
|
|
46
|
+
error(message, ...args) {
|
|
47
|
+
log("error", message, args);
|
|
48
|
+
},
|
|
49
|
+
warn(message, ...args) {
|
|
50
|
+
log("warn", message, args);
|
|
51
|
+
},
|
|
52
|
+
debug(message, ...args) {
|
|
53
|
+
log("debug", message, args);
|
|
54
|
+
},
|
|
55
|
+
info(message, ...args) {
|
|
56
|
+
log("info", message, args);
|
|
57
|
+
},
|
|
58
|
+
timing(name, dur, desc) {
|
|
59
|
+
const event = {
|
|
60
|
+
type: "timing",
|
|
61
|
+
payload: {
|
|
62
|
+
name,
|
|
63
|
+
dur,
|
|
64
|
+
desc
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
coreMonitors.forEach((monitor) => monitor(event));
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
return mointors;
|
|
71
|
+
}
|
|
72
|
+
const initMonitorsPlugin = () => ({
|
|
73
|
+
name: "@modern-js/init-mointor",
|
|
74
|
+
setup(api) {
|
|
75
|
+
return {
|
|
76
|
+
prepare() {
|
|
77
|
+
const { middlewares } = api.useAppContext();
|
|
78
|
+
middlewares.push({
|
|
79
|
+
name: "init-monitor",
|
|
80
|
+
handler: async (c, next) => {
|
|
81
|
+
if (!c.get("monitors")) {
|
|
82
|
+
const monitors = createMonitors();
|
|
83
|
+
c.set("monitors", monitors);
|
|
84
|
+
}
|
|
85
|
+
return next();
|
|
86
|
+
},
|
|
87
|
+
order: "pre"
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
const injectloggerPluigin = (logger) => ({
|
|
94
|
+
name: "@modern-js/inject-logger",
|
|
95
|
+
setup(api) {
|
|
96
|
+
return {
|
|
97
|
+
prepare() {
|
|
98
|
+
const { middlewares } = api.useAppContext();
|
|
99
|
+
middlewares.push({
|
|
100
|
+
name: "inject-logger",
|
|
101
|
+
handler: async (c, next) => {
|
|
102
|
+
if (!c.get("logger")) {
|
|
103
|
+
c.set("logger", logger);
|
|
104
|
+
}
|
|
105
|
+
const pathname = c.req.path;
|
|
106
|
+
const loggerMonitor = (event) => {
|
|
107
|
+
if (event.type === "log") {
|
|
108
|
+
const { level, message, args } = event.payload;
|
|
109
|
+
logger[level](message, ...args || []);
|
|
110
|
+
}
|
|
111
|
+
if (event.type === "timing") {
|
|
112
|
+
const { name, dur, desc } = event.payload;
|
|
113
|
+
if (desc) {
|
|
114
|
+
logger.debug(`%s Debug - ${name}, cost: %s, req.url = %s `, desc, dur, pathname);
|
|
115
|
+
} else {
|
|
116
|
+
logger.debug(`Debug - ${name}, cost: %s, req.url = %s`, dur, pathname);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
const monitors = c.get("monitors");
|
|
121
|
+
monitors === null || monitors === void 0 ? void 0 : monitors.push(loggerMonitor);
|
|
122
|
+
return next();
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
const injectServerTiming = (metaName = "modern-js") => ({
|
|
130
|
+
name: "@modern-js/inject-server-timing",
|
|
131
|
+
setup(api) {
|
|
132
|
+
return {
|
|
133
|
+
prepare() {
|
|
134
|
+
const { middlewares } = api.useAppContext();
|
|
135
|
+
middlewares.push({
|
|
136
|
+
name: "inject-server-timing",
|
|
137
|
+
handler: async (c, next) => {
|
|
138
|
+
const serverTimings = [];
|
|
139
|
+
const timingMonitor = (event) => {
|
|
140
|
+
if (event.type === "timing") {
|
|
141
|
+
serverTimings.push(event.payload);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
const monitors = c.get("monitors");
|
|
145
|
+
monitors === null || monitors === void 0 ? void 0 : monitors.push(timingMonitor);
|
|
146
|
+
await next();
|
|
147
|
+
serverTimings.forEach((serverTiming) => {
|
|
148
|
+
const { name, desc, dur } = serverTiming;
|
|
149
|
+
const _name = `bd-${metaName}-${name}`;
|
|
150
|
+
const value = `${_name};${desc ? `decs="${desc}";` : ""} dur=${dur}`;
|
|
151
|
+
c.header(import_constants.SERVER_TIMING, value, {
|
|
152
|
+
append: true
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
function initReporter(entryName) {
|
|
162
|
+
return async (c, next) => {
|
|
163
|
+
const reporter = c.get("reporter");
|
|
164
|
+
if (!reporter) {
|
|
165
|
+
await next();
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
await reporter.init({
|
|
169
|
+
entryName
|
|
170
|
+
});
|
|
171
|
+
const getCost = (0, import_time.time)();
|
|
172
|
+
await next();
|
|
173
|
+
const cost = getCost();
|
|
174
|
+
reporter.reportTiming(import_constants.ServerTimings.SERVER_HANDLE_REQUEST, cost);
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
178
|
+
0 && (module.exports = {
|
|
179
|
+
initMonitorsPlugin,
|
|
180
|
+
initReporter,
|
|
181
|
+
injectServerTiming,
|
|
182
|
+
injectloggerPluigin
|
|
183
|
+
});
|
|
@@ -23,7 +23,7 @@ __export(render_exports, {
|
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(render_exports);
|
|
25
25
|
var import_constants = require("@modern-js/utils/universal/constants");
|
|
26
|
-
var
|
|
26
|
+
var import_monitors = require("../monitors");
|
|
27
27
|
var import_utils = require("../../utils");
|
|
28
28
|
var import_customServer = require("../customServer");
|
|
29
29
|
var import_render = require("./render");
|
|
@@ -56,7 +56,7 @@ const renderPlugin = (options = {}) => ({
|
|
|
56
56
|
const urlPath = originUrlPath.endsWith("/") ? `${originUrlPath}*` : `${originUrlPath}/*`;
|
|
57
57
|
middlewares.push({
|
|
58
58
|
name: "init-reporter",
|
|
59
|
-
handler: (0,
|
|
59
|
+
handler: (0, import_monitors.initReporter)(entryName || import_constants.MAIN_ENTRY_NAME)
|
|
60
60
|
});
|
|
61
61
|
const customServerHookMiddleware = customServer.getHookMiddleware(entryName || "main", routes);
|
|
62
62
|
middlewares.push({
|
|
@@ -88,6 +88,7 @@ function createRenderHandler(render) {
|
|
|
88
88
|
var _c_env_node;
|
|
89
89
|
const logger = c.get("logger");
|
|
90
90
|
const reporter = c.get("reporter");
|
|
91
|
+
const monitors = c.get("monitors");
|
|
91
92
|
const templates = c.get("templates") || {};
|
|
92
93
|
const serverManifest = c.get("serverManifest") || {};
|
|
93
94
|
const locals = c.get("locals");
|
|
@@ -96,8 +97,9 @@ function createRenderHandler(render) {
|
|
|
96
97
|
const request = c.req.raw;
|
|
97
98
|
const nodeReq = (_c_env_node = c.env.node) === null || _c_env_node === void 0 ? void 0 : _c_env_node.req;
|
|
98
99
|
const res = await render(request, {
|
|
99
|
-
logger,
|
|
100
100
|
nodeReq,
|
|
101
|
+
monitors,
|
|
102
|
+
logger,
|
|
101
103
|
reporter,
|
|
102
104
|
templates,
|
|
103
105
|
metrics,
|
|
@@ -27,7 +27,6 @@ var import_utils = require("../../utils");
|
|
|
27
27
|
var import_constants = require("../../constants");
|
|
28
28
|
var import_dataHandler = require("./dataHandler");
|
|
29
29
|
var import_ssrRender = require("./ssrRender");
|
|
30
|
-
var import_serverTiming = require("./serverTiming");
|
|
31
30
|
const DYNAMIC_ROUTE_REG = /\/:./;
|
|
32
31
|
function getRouter(routes) {
|
|
33
32
|
const dynamicRoutes = [];
|
|
@@ -65,10 +64,9 @@ function getHeadersWithoutCookie(headers) {
|
|
|
65
64
|
delete _headers.cookie;
|
|
66
65
|
return _headers;
|
|
67
66
|
}
|
|
68
|
-
const SERVER_TIMING = "Server-Timing";
|
|
69
67
|
async function createRender({ routes, pwd, metaName, staticGenerate, cacheConfig, forceCSR, config, onFallback: onFallbackFn }) {
|
|
70
68
|
const router = getRouter(routes);
|
|
71
|
-
return async (req, { logger,
|
|
69
|
+
return async (req, { logger, reporter, metrics, monitors, nodeReq, templates, serverManifest, locals, loaderContext }) => {
|
|
72
70
|
const [routeInfo, params] = matchRoute(router, req);
|
|
73
71
|
const onFallback = async (reason, error) => {
|
|
74
72
|
return onFallbackFn === null || onFallbackFn === void 0 ? void 0 : onFallbackFn(reason, {
|
|
@@ -97,16 +95,14 @@ async function createRender({ routes, pwd, metaName, staticGenerate, cacheConfig
|
|
|
97
95
|
const renderMode = await getRenderMode(req, metaName || "modern-js", routeInfo.isSSR, forceCSR, nodeReq, onFallback);
|
|
98
96
|
const pathname = (0, import_utils.getPathname)(req);
|
|
99
97
|
const headerData = (0, import_utils.parseHeaders)(req);
|
|
100
|
-
const serverTimingInstance = new import_serverTiming.ServerTiming(metaName || "modern");
|
|
101
98
|
const onError = (e) => {
|
|
102
|
-
|
|
99
|
+
monitors === null || monitors === void 0 ? void 0 : monitors.error(`SSR Error - ${e instanceof Error ? e.name : e}, error = %s, req.url = %s, req.headers = %o`, e instanceof Error ? e.stack || e.message : e, pathname, getHeadersWithoutCookie(headerData));
|
|
103
100
|
};
|
|
104
101
|
const onTiming = (name, dur) => {
|
|
105
|
-
|
|
106
|
-
serverTimingInstance.addServeTiming(name, dur);
|
|
102
|
+
monitors === null || monitors === void 0 ? void 0 : monitors.timing(name, dur, "SSR");
|
|
107
103
|
};
|
|
108
104
|
const onBoundError = async (e) => {
|
|
109
|
-
(0, import_utils.onError)(import_utils.ErrorDigest.ERENDER, e,
|
|
105
|
+
(0, import_utils.onError)(import_utils.ErrorDigest.ERENDER, e, monitors, req);
|
|
110
106
|
await (onFallback === null || onFallback === void 0 ? void 0 : onFallback("error", e));
|
|
111
107
|
};
|
|
112
108
|
const renderOptions = {
|
|
@@ -140,9 +136,6 @@ async function createRender({ routes, pwd, metaName, staticGenerate, cacheConfig
|
|
|
140
136
|
default:
|
|
141
137
|
throw new Error(`Unknown render mode: ${renderMode}`);
|
|
142
138
|
}
|
|
143
|
-
serverTimingInstance.headers.forEach((value) => {
|
|
144
|
-
response.headers.append(SERVER_TIMING, value);
|
|
145
|
-
});
|
|
146
139
|
return response;
|
|
147
140
|
};
|
|
148
141
|
}
|
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 {
|
|
@@ -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
|
};
|
|
@@ -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 [
|