@modern-js/server-core 2.55.0 → 2.56.1
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 +35 -8
- 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 +86 -36
- 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 +26 -9
- 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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { time } from "@modern-js/runtime-utils/time";
|
|
2
2
|
import { transformResponse } from "../../utils";
|
|
3
|
-
import {
|
|
3
|
+
import { ServerTimings } from "../../constants";
|
|
4
4
|
import { getLoaderCtx } from "./loader";
|
|
5
5
|
import { getAfterMatchCtx, getAfterRenderCtx, createCustomMiddlewaresCtx, createAfterStreamingRenderContext } from "./context";
|
|
6
6
|
import { createBaseHookContext } from "./base";
|
|
@@ -15,7 +15,7 @@ class CustomServer {
|
|
|
15
15
|
getHookMiddleware(entryName, routes) {
|
|
16
16
|
return async (c, next) => {
|
|
17
17
|
const routeInfo = routes.find((route) => route.entryName === entryName);
|
|
18
|
-
const
|
|
18
|
+
const monitors = c.get("monitors");
|
|
19
19
|
const baseHookCtx = createBaseHookContext(c);
|
|
20
20
|
const afterMatchCtx = getAfterMatchCtx(entryName, baseHookCtx);
|
|
21
21
|
const getCost = time();
|
|
@@ -23,7 +23,7 @@ class CustomServer {
|
|
|
23
23
|
onLast: noop
|
|
24
24
|
});
|
|
25
25
|
const cost = getCost();
|
|
26
|
-
cost && (
|
|
26
|
+
cost && (monitors === null || monitors === void 0 ? void 0 : monitors.timing(ServerTimings.SERVER_HOOK_AFTER_MATCH, cost));
|
|
27
27
|
const { url, status } = afterMatchCtx.router;
|
|
28
28
|
if (url) {
|
|
29
29
|
return c.redirect(url, status);
|
|
@@ -63,7 +63,7 @@ class CustomServer {
|
|
|
63
63
|
onLast: noop
|
|
64
64
|
});
|
|
65
65
|
const cost2 = getCost2();
|
|
66
|
-
cost2 && (
|
|
66
|
+
cost2 && (monitors === null || monitors === void 0 ? void 0 : monitors.timing(ServerTimings.SERVER_HOOK_AFTER_RENDER, cost2));
|
|
67
67
|
if (afterRenderCtx.response.private_overrided) {
|
|
68
68
|
return void 0;
|
|
69
69
|
}
|
|
@@ -82,7 +82,7 @@ class CustomServer {
|
|
|
82
82
|
}
|
|
83
83
|
return async (c, next) => {
|
|
84
84
|
var _c_env_node_res, _c_env_node, _c_env;
|
|
85
|
-
const
|
|
85
|
+
const monitors = c.get("monitors");
|
|
86
86
|
const locals = {};
|
|
87
87
|
const resArgs = {
|
|
88
88
|
headers: new Headers()
|
|
@@ -91,7 +91,7 @@ class CustomServer {
|
|
|
91
91
|
const getCost = time();
|
|
92
92
|
await serverMiddleware(customMiddlewareCtx);
|
|
93
93
|
const cost = getCost();
|
|
94
|
-
cost && (
|
|
94
|
+
cost && (monitors === null || monitors === void 0 ? void 0 : monitors.timing(ServerTimings.SERVER_MIDDLEWARE, cost));
|
|
95
95
|
c.set("locals", locals);
|
|
96
96
|
if (isRedirect(resArgs.headers, resArgs.status)) {
|
|
97
97
|
return c.redirect(resArgs.headers.get("Location") || "", resArgs.status || 302);
|
|
@@ -121,7 +121,7 @@ class CustomServer {
|
|
|
121
121
|
function getServerMidFromUnstableMid(serverMiddleware) {
|
|
122
122
|
return serverMiddleware.map((middleware) => {
|
|
123
123
|
return async (c, next) => {
|
|
124
|
-
const context = createMiddlewareContextFromHono(c);
|
|
124
|
+
const context = await createMiddlewareContextFromHono(c);
|
|
125
125
|
return middleware(context, next);
|
|
126
126
|
};
|
|
127
127
|
});
|
|
@@ -134,11 +134,28 @@ function isRedirect(headers, code) {
|
|
|
134
134
|
308
|
|
135
135
|
].includes(code || 0) || headers.get("Location");
|
|
136
136
|
}
|
|
137
|
-
function createMiddlewareContextFromHono(c) {
|
|
137
|
+
async function createMiddlewareContextFromHono(c) {
|
|
138
138
|
const loaderContext = getLoaderCtx(c);
|
|
139
|
+
let rawRequest = c.req.raw;
|
|
140
|
+
const method = rawRequest.method.toUpperCase();
|
|
141
|
+
if (![
|
|
142
|
+
"GET",
|
|
143
|
+
"HEAD"
|
|
144
|
+
].includes(method) && !rawRequest.body && c.env.node.req) {
|
|
145
|
+
const streamPath = "../../adapters/node/polyfills/stream";
|
|
146
|
+
const { createReadableStreamFromReadable } = await import(streamPath);
|
|
147
|
+
const init = {
|
|
148
|
+
body: createReadableStreamFromReadable(c.env.node.req),
|
|
149
|
+
headers: rawRequest.headers,
|
|
150
|
+
signal: rawRequest.signal,
|
|
151
|
+
method: rawRequest.method
|
|
152
|
+
};
|
|
153
|
+
init.duplex = "half";
|
|
154
|
+
rawRequest = new Request(rawRequest.url, init);
|
|
155
|
+
}
|
|
139
156
|
return {
|
|
140
157
|
get request() {
|
|
141
|
-
return
|
|
158
|
+
return rawRequest;
|
|
142
159
|
},
|
|
143
160
|
get response() {
|
|
144
161
|
return c.res;
|
|
@@ -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
|
};
|
|
@@ -39,13 +39,13 @@ function log(fn, prefix, method, path, status = 0, elapsed) {
|
|
|
39
39
|
function logHandler() {
|
|
40
40
|
return async function logger(c, next) {
|
|
41
41
|
const { method } = c.req;
|
|
42
|
-
const
|
|
43
|
-
if (!
|
|
42
|
+
const monitors = c.get("monitors");
|
|
43
|
+
if (!monitors) {
|
|
44
44
|
await next();
|
|
45
45
|
return;
|
|
46
46
|
}
|
|
47
47
|
const path = getPathname(c.req.raw);
|
|
48
|
-
const logFn =
|
|
48
|
+
const logFn = monitors.debug;
|
|
49
49
|
log(logFn, "<--", method, path);
|
|
50
50
|
const start = Date.now();
|
|
51
51
|
await next();
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { time } from "@modern-js/runtime-utils/time";
|
|
2
|
+
import { SERVER_TIMING, ServerTimings } from "../constants";
|
|
3
|
+
function createMonitors() {
|
|
4
|
+
const coreMonitors = [];
|
|
5
|
+
const log = (level, message, args) => {
|
|
6
|
+
const event = {
|
|
7
|
+
type: "log",
|
|
8
|
+
payload: {
|
|
9
|
+
level,
|
|
10
|
+
message,
|
|
11
|
+
args
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
coreMonitors.forEach((monitor) => monitor(event));
|
|
15
|
+
};
|
|
16
|
+
const mointors = {
|
|
17
|
+
push(monitor) {
|
|
18
|
+
coreMonitors.push(monitor);
|
|
19
|
+
},
|
|
20
|
+
error(message, ...args) {
|
|
21
|
+
log("error", message, args);
|
|
22
|
+
},
|
|
23
|
+
warn(message, ...args) {
|
|
24
|
+
log("warn", message, args);
|
|
25
|
+
},
|
|
26
|
+
debug(message, ...args) {
|
|
27
|
+
log("debug", message, args);
|
|
28
|
+
},
|
|
29
|
+
info(message, ...args) {
|
|
30
|
+
log("info", message, args);
|
|
31
|
+
},
|
|
32
|
+
timing(name, dur, desc) {
|
|
33
|
+
const event = {
|
|
34
|
+
type: "timing",
|
|
35
|
+
payload: {
|
|
36
|
+
name,
|
|
37
|
+
dur,
|
|
38
|
+
desc
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
coreMonitors.forEach((monitor) => monitor(event));
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
return mointors;
|
|
45
|
+
}
|
|
46
|
+
const initMonitorsPlugin = () => ({
|
|
47
|
+
name: "@modern-js/init-mointor",
|
|
48
|
+
setup(api) {
|
|
49
|
+
return {
|
|
50
|
+
prepare() {
|
|
51
|
+
const { middlewares } = api.useAppContext();
|
|
52
|
+
middlewares.push({
|
|
53
|
+
name: "init-monitor",
|
|
54
|
+
handler: async (c, next) => {
|
|
55
|
+
if (!c.get("monitors")) {
|
|
56
|
+
const monitors = createMonitors();
|
|
57
|
+
c.set("monitors", monitors);
|
|
58
|
+
}
|
|
59
|
+
return next();
|
|
60
|
+
},
|
|
61
|
+
order: "pre"
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
const injectloggerPluigin = (logger) => ({
|
|
68
|
+
name: "@modern-js/inject-logger",
|
|
69
|
+
setup(api) {
|
|
70
|
+
return {
|
|
71
|
+
prepare() {
|
|
72
|
+
const { middlewares } = api.useAppContext();
|
|
73
|
+
middlewares.push({
|
|
74
|
+
name: "inject-logger",
|
|
75
|
+
handler: async (c, next) => {
|
|
76
|
+
if (!c.get("logger")) {
|
|
77
|
+
c.set("logger", logger);
|
|
78
|
+
}
|
|
79
|
+
const pathname = c.req.path;
|
|
80
|
+
const loggerMonitor = (event) => {
|
|
81
|
+
if (event.type === "log") {
|
|
82
|
+
const { level, message, args } = event.payload;
|
|
83
|
+
logger[level](message, ...args || []);
|
|
84
|
+
}
|
|
85
|
+
if (event.type === "timing") {
|
|
86
|
+
const { name, dur, desc } = event.payload;
|
|
87
|
+
if (desc) {
|
|
88
|
+
logger.debug(`%s Debug - ${name}, cost: %s, req.url = %s `, desc, dur, pathname);
|
|
89
|
+
} else {
|
|
90
|
+
logger.debug(`Debug - ${name}, cost: %s, req.url = %s`, dur, pathname);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const monitors = c.get("monitors");
|
|
95
|
+
monitors === null || monitors === void 0 ? void 0 : monitors.push(loggerMonitor);
|
|
96
|
+
return next();
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
const injectServerTiming = (metaName = "modern-js") => ({
|
|
104
|
+
name: "@modern-js/inject-server-timing",
|
|
105
|
+
setup(api) {
|
|
106
|
+
return {
|
|
107
|
+
prepare() {
|
|
108
|
+
const { middlewares } = api.useAppContext();
|
|
109
|
+
middlewares.push({
|
|
110
|
+
name: "inject-server-timing",
|
|
111
|
+
handler: async (c, next) => {
|
|
112
|
+
const serverTimings = [];
|
|
113
|
+
const timingMonitor = (event) => {
|
|
114
|
+
if (event.type === "timing") {
|
|
115
|
+
serverTimings.push(event.payload);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const monitors = c.get("monitors");
|
|
119
|
+
monitors === null || monitors === void 0 ? void 0 : monitors.push(timingMonitor);
|
|
120
|
+
await next();
|
|
121
|
+
serverTimings.forEach((serverTiming) => {
|
|
122
|
+
const { name, desc, dur } = serverTiming;
|
|
123
|
+
const _name = `bd-${metaName}-${name}`;
|
|
124
|
+
const value = `${_name};${desc ? `decs="${desc}";` : ""} dur=${dur}`;
|
|
125
|
+
c.header(SERVER_TIMING, value, {
|
|
126
|
+
append: true
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
function initReporter(entryName) {
|
|
136
|
+
return async (c, next) => {
|
|
137
|
+
const reporter = c.get("reporter");
|
|
138
|
+
if (!reporter) {
|
|
139
|
+
await next();
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
await reporter.init({
|
|
143
|
+
entryName
|
|
144
|
+
});
|
|
145
|
+
const getCost = time();
|
|
146
|
+
await next();
|
|
147
|
+
const cost = getCost();
|
|
148
|
+
reporter.reportTiming(ServerTimings.SERVER_HANDLE_REQUEST, cost);
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
export {
|
|
152
|
+
initMonitorsPlugin,
|
|
153
|
+
initReporter,
|
|
154
|
+
injectServerTiming,
|
|
155
|
+
injectloggerPluigin
|
|
156
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { MAIN_ENTRY_NAME } from "@modern-js/utils/universal/constants";
|
|
2
|
-
import { initReporter } from "../
|
|
2
|
+
import { initReporter } from "../monitors";
|
|
3
3
|
import { sortRoutes } from "../../utils";
|
|
4
4
|
import { getLoaderCtx, CustomServer, getServerMidFromUnstableMid } from "../customServer";
|
|
5
5
|
import { createRender } from "./render";
|
|
@@ -64,6 +64,7 @@ function createRenderHandler(render) {
|
|
|
64
64
|
var _c_env_node;
|
|
65
65
|
const logger = c.get("logger");
|
|
66
66
|
const reporter = c.get("reporter");
|
|
67
|
+
const monitors = c.get("monitors");
|
|
67
68
|
const templates = c.get("templates") || {};
|
|
68
69
|
const serverManifest = c.get("serverManifest") || {};
|
|
69
70
|
const locals = c.get("locals");
|
|
@@ -72,8 +73,9 @@ function createRenderHandler(render) {
|
|
|
72
73
|
const request = c.req.raw;
|
|
73
74
|
const nodeReq = (_c_env_node = c.env.node) === null || _c_env_node === void 0 ? void 0 : _c_env_node.req;
|
|
74
75
|
const res = await render(request, {
|
|
75
|
-
logger,
|
|
76
76
|
nodeReq,
|
|
77
|
+
monitors,
|
|
78
|
+
logger,
|
|
77
79
|
reporter,
|
|
78
80
|
templates,
|
|
79
81
|
metrics,
|
|
@@ -4,7 +4,6 @@ import { parseQuery, getPathname, createErrorHtml, sortRoutes, transformResponse
|
|
|
4
4
|
import { REPLACE_REG, X_MODERNJS_RENDER } from "../../constants";
|
|
5
5
|
import { dataHandler } from "./dataHandler";
|
|
6
6
|
import { ssrRender } from "./ssrRender";
|
|
7
|
-
import { ServerTiming } from "./serverTiming";
|
|
8
7
|
const DYNAMIC_ROUTE_REG = /\/:./;
|
|
9
8
|
function getRouter(routes) {
|
|
10
9
|
const dynamicRoutes = [];
|
|
@@ -42,10 +41,9 @@ function getHeadersWithoutCookie(headers) {
|
|
|
42
41
|
delete _headers.cookie;
|
|
43
42
|
return _headers;
|
|
44
43
|
}
|
|
45
|
-
const SERVER_TIMING = "Server-Timing";
|
|
46
44
|
async function createRender({ routes, pwd, metaName, staticGenerate, cacheConfig, forceCSR, config, onFallback: onFallbackFn }) {
|
|
47
45
|
const router = getRouter(routes);
|
|
48
|
-
return async (req, { logger,
|
|
46
|
+
return async (req, { logger, reporter, metrics, monitors, nodeReq, templates, serverManifest, locals, loaderContext }) => {
|
|
49
47
|
const [routeInfo, params] = matchRoute(router, req);
|
|
50
48
|
const onFallback = async (reason, error) => {
|
|
51
49
|
return onFallbackFn === null || onFallbackFn === void 0 ? void 0 : onFallbackFn(reason, {
|
|
@@ -74,16 +72,14 @@ async function createRender({ routes, pwd, metaName, staticGenerate, cacheConfig
|
|
|
74
72
|
const renderMode = await getRenderMode(req, metaName || "modern-js", routeInfo.isSSR, forceCSR, nodeReq, onFallback);
|
|
75
73
|
const pathname = getPathname(req);
|
|
76
74
|
const headerData = parseHeaders(req);
|
|
77
|
-
const serverTimingInstance = new ServerTiming(metaName || "modern");
|
|
78
75
|
const onError = (e) => {
|
|
79
|
-
|
|
76
|
+
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));
|
|
80
77
|
};
|
|
81
78
|
const onTiming = (name, dur) => {
|
|
82
|
-
|
|
83
|
-
serverTimingInstance.addServeTiming(name, dur);
|
|
79
|
+
monitors === null || monitors === void 0 ? void 0 : monitors.timing(name, dur, "SSR");
|
|
84
80
|
};
|
|
85
81
|
const onBoundError = async (e) => {
|
|
86
|
-
onErrorFn(ErrorDigest.ERENDER, e,
|
|
82
|
+
onErrorFn(ErrorDigest.ERENDER, e, monitors, req);
|
|
87
83
|
await (onFallback === null || onFallback === void 0 ? void 0 : onFallback("error", e));
|
|
88
84
|
};
|
|
89
85
|
const renderOptions = {
|
|
@@ -117,9 +113,6 @@ async function createRender({ routes, pwd, metaName, staticGenerate, cacheConfig
|
|
|
117
113
|
default:
|
|
118
114
|
throw new Error(`Unknown render mode: ${renderMode}`);
|
|
119
115
|
}
|
|
120
|
-
serverTimingInstance.headers.forEach((value) => {
|
|
121
|
-
response.headers.append(SERVER_TIMING, value);
|
|
122
|
-
});
|
|
123
116
|
return response;
|
|
124
117
|
};
|
|
125
118
|
}
|
|
@@ -43,11 +43,11 @@ var ErrorDigest;
|
|
|
43
43
|
ErrorDigest2["EINTER"] = "Internal server error";
|
|
44
44
|
ErrorDigest2["ERENDER"] = "SSR render failed";
|
|
45
45
|
})(ErrorDigest || (ErrorDigest = {}));
|
|
46
|
-
function onError(digest, error,
|
|
46
|
+
function onError(digest, error, monitors, req) {
|
|
47
47
|
const headerData = req && parseHeaders(req);
|
|
48
48
|
headerData && delete headerData.cookie;
|
|
49
|
-
if (
|
|
50
|
-
|
|
49
|
+
if (monitors) {
|
|
50
|
+
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);
|
|
51
51
|
} else if (req) {
|
|
52
52
|
console.error(`Server Error - ${digest}, error = ${error instanceof Error ? error.stack || error.message : error}, req.url = ${req.url}, req.headers = ${JSON.stringify(headerData)}`);
|
|
53
53
|
} else {
|
|
@@ -15,11 +15,12 @@ export declare const REPLACE_REG: {
|
|
|
15
15
|
body: string;
|
|
16
16
|
};
|
|
17
17
|
};
|
|
18
|
-
export declare enum
|
|
18
|
+
export declare enum ServerTimings {
|
|
19
19
|
SERVER_HANDLE_REQUEST = "server-handle-request",
|
|
20
20
|
SERVER_MIDDLEWARE = "server-middleware",
|
|
21
21
|
SERVER_HOOK_AFTER_RENDER = "server-hook-after-render",
|
|
22
22
|
SERVER_HOOK_AFTER_MATCH = "server-hook-after-match"
|
|
23
23
|
}
|
|
24
|
+
export declare const SERVER_TIMING = "Server-Timing";
|
|
24
25
|
export declare const X_RENDER_CACHE = "x-render-cache";
|
|
25
26
|
export declare const X_MODERNJS_RENDER = "x-modernjs-render";
|
|
@@ -3,4 +3,4 @@ export { faviconPlugin } from './favicon';
|
|
|
3
3
|
export { processedByPlugin } from './processedBy';
|
|
4
4
|
export { getLoaderCtx } from './customServer';
|
|
5
5
|
export { logPlugin } from './log';
|
|
6
|
-
export {
|
|
6
|
+
export { initMonitorsPlugin, injectServerTiming, injectloggerPluigin, } from './monitors';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Logger } from '@modern-js/types';
|
|
2
|
+
import type { Context, Next, ServerEnv, ServerPlugin } from '../types';
|
|
3
|
+
export declare const initMonitorsPlugin: () => ServerPlugin;
|
|
4
|
+
export declare const injectloggerPluigin: (logger: Logger) => ServerPlugin;
|
|
5
|
+
export declare const injectServerTiming: (metaName?: string) => ServerPlugin;
|
|
6
|
+
export declare function initReporter(entryName: string): (c: Context<ServerEnv>, next: Next) => Promise<void>;
|
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import type { IncomingMessage } from 'node:http';
|
|
3
|
-
import type { Logger, Metrics, Reporter } from '@modern-js/types';
|
|
3
|
+
import type { Logger, Metrics, Monitors, Reporter } from '@modern-js/types';
|
|
4
4
|
import type { ServerManifest } from './server';
|
|
5
5
|
export interface RenderOptions {
|
|
6
|
-
logger: Logger;
|
|
7
6
|
loaderContext?: Map<string, unknown>;
|
|
8
7
|
/** ssr render html templates */
|
|
9
8
|
templates: Record<string, string>;
|
|
10
9
|
/** Communicating with custom server hook & modern ssr runtime. */
|
|
11
10
|
locals?: Record<string, any>;
|
|
12
|
-
/**
|
|
11
|
+
/** @deprecated */
|
|
12
|
+
logger: Logger;
|
|
13
|
+
/** @deprecated */
|
|
13
14
|
metrics?: Metrics;
|
|
14
|
-
|
|
15
|
+
/** @deprecated */
|
|
15
16
|
reporter?: Reporter;
|
|
17
|
+
monitors?: Monitors;
|
|
18
|
+
serverManifest: ServerManifest;
|
|
16
19
|
nodeReq?: IncomingMessage;
|
|
17
20
|
}
|
|
18
21
|
export type Render = (request: Request, options: RenderOptions) => Promise<Response>;
|
|
@@ -14,8 +14,6 @@ export type RequestHandlerConfig = {
|
|
|
14
14
|
scriptLoading?: 'defer' | 'blocking' | 'module' | 'async';
|
|
15
15
|
enableInlineStyles?: boolean | RegExp;
|
|
16
16
|
enableInlineScripts?: boolean | RegExp;
|
|
17
|
-
disablePrerender?: boolean;
|
|
18
|
-
chunkLoadingGlobal?: string;
|
|
19
17
|
ssr?: ServerUserConfig['ssr'];
|
|
20
18
|
ssrByEntries?: ServerUserConfig['ssrByEntries'];
|
|
21
19
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import type { Readable } from 'node:stream';
|
|
3
|
-
import type { Metrics, Logger, Reporter, BaseSSRServerContext, ServerRoute, NestedRoute } from '@modern-js/types';
|
|
3
|
+
import type { Metrics, Logger, Reporter, BaseSSRServerContext, ServerRoute, NestedRoute, Monitors } from '@modern-js/types';
|
|
4
4
|
import { RequestHandler as BundleRequestHandler, OnError, OnTiming } from './requestHandler';
|
|
5
5
|
export type SSRServerContext = BaseSSRServerContext & {
|
|
6
6
|
staticGenerate?: boolean;
|
|
@@ -30,8 +30,13 @@ export type ServerManifest = {
|
|
|
30
30
|
routeManifest?: Record<string, any>;
|
|
31
31
|
};
|
|
32
32
|
type ServerVariables = {
|
|
33
|
+
/** @deprecated */
|
|
33
34
|
logger: Logger;
|
|
35
|
+
/** @deprecated */
|
|
34
36
|
reporter?: Reporter;
|
|
37
|
+
/** @deprecated */
|
|
38
|
+
metrics?: Metrics;
|
|
39
|
+
monitors: Monitors;
|
|
35
40
|
serverManifest?: ServerManifest;
|
|
36
41
|
templates?: Record<string, string>;
|
|
37
42
|
/**
|
|
@@ -41,7 +46,6 @@ type ServerVariables = {
|
|
|
41
46
|
* Custom by ssrRuntime.
|
|
42
47
|
*/
|
|
43
48
|
locals?: Record<string, any>;
|
|
44
|
-
metrics?: Metrics;
|
|
45
49
|
};
|
|
46
50
|
export type ServerEnv = {
|
|
47
51
|
Variables: ServerVariables;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Monitors } from '@modern-js/types';
|
|
2
2
|
export declare const createErrorHtml: (status: number) => string;
|
|
3
3
|
export declare enum ErrorDigest {
|
|
4
4
|
ENOTF = "Page could not be found",
|
|
5
5
|
EINTER = "Internal server error",
|
|
6
6
|
ERENDER = "SSR render failed"
|
|
7
7
|
}
|
|
8
|
-
export declare function onError(digest: ErrorDigest, error: Error | string,
|
|
8
|
+
export declare function onError(digest: ErrorDigest, error: Error | string, monitors?: Monitors, req?: Request): void;
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "2.
|
|
18
|
+
"version": "2.56.1",
|
|
19
19
|
"jsnext:source": "./src/index.ts",
|
|
20
20
|
"types": "./dist/types/index.d.ts",
|
|
21
21
|
"main": "./dist/cjs/index.js",
|
|
@@ -52,9 +52,9 @@
|
|
|
52
52
|
"@web-std/file": "^3.0.3",
|
|
53
53
|
"hono": "^3.12.2",
|
|
54
54
|
"ts-deepmerge": "7.0.0",
|
|
55
|
-
"@modern-js/plugin": "2.
|
|
56
|
-
"@modern-js/runtime-utils": "2.
|
|
57
|
-
"@modern-js/utils": "2.
|
|
55
|
+
"@modern-js/plugin": "2.56.1",
|
|
56
|
+
"@modern-js/runtime-utils": "2.56.1",
|
|
57
|
+
"@modern-js/utils": "2.56.1"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
60
|
"@types/jest": "^29",
|
|
@@ -64,9 +64,9 @@
|
|
|
64
64
|
"jest": "^29",
|
|
65
65
|
"ts-jest": "^29.1.0",
|
|
66
66
|
"typescript": "^5",
|
|
67
|
-
"@modern-js/types": "2.
|
|
68
|
-
"@scripts/jest-config": "2.
|
|
69
|
-
"@scripts/build": "2.
|
|
67
|
+
"@modern-js/types": "2.56.1",
|
|
68
|
+
"@scripts/jest-config": "2.56.1",
|
|
69
|
+
"@scripts/build": "2.56.1"
|
|
70
70
|
},
|
|
71
71
|
"sideEffects": false,
|
|
72
72
|
"publishConfig": {
|
|
@@ -1,87 +0,0 @@
|
|
|
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 monitor_exports = {};
|
|
20
|
-
__export(monitor_exports, {
|
|
21
|
-
initReporter: () => initReporter,
|
|
22
|
-
monitorPlugin: () => monitorPlugin
|
|
23
|
-
});
|
|
24
|
-
module.exports = __toCommonJS(monitor_exports);
|
|
25
|
-
var import_time = require("@modern-js/runtime-utils/time");
|
|
26
|
-
var import_constants = require("../constants");
|
|
27
|
-
const defaultReporter = {
|
|
28
|
-
init() {
|
|
29
|
-
},
|
|
30
|
-
reportError() {
|
|
31
|
-
},
|
|
32
|
-
reportTiming() {
|
|
33
|
-
},
|
|
34
|
-
reportInfo() {
|
|
35
|
-
},
|
|
36
|
-
reportWarn() {
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
function initReporter(entryName) {
|
|
40
|
-
return async (c, next) => {
|
|
41
|
-
const reporter = c.get("reporter");
|
|
42
|
-
if (!reporter) {
|
|
43
|
-
await next();
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
await reporter.init({
|
|
47
|
-
entryName
|
|
48
|
-
});
|
|
49
|
-
const getCost = (0, import_time.time)();
|
|
50
|
-
await next();
|
|
51
|
-
const cost = getCost();
|
|
52
|
-
reporter.reportTiming(import_constants.ServerReportTimings.SERVER_HANDLE_REQUEST, cost);
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
const monitorPlugin = (options) => ({
|
|
56
|
-
name: "@modern-js/plugin-monitor",
|
|
57
|
-
setup(api) {
|
|
58
|
-
return {
|
|
59
|
-
prepare() {
|
|
60
|
-
const { middlewares } = api.useAppContext();
|
|
61
|
-
middlewares.push({
|
|
62
|
-
name: "monitor",
|
|
63
|
-
handler: async (c, next) => {
|
|
64
|
-
const logger = c.get("logger");
|
|
65
|
-
if (!logger && options.logger) {
|
|
66
|
-
c.set("logger", options.logger);
|
|
67
|
-
}
|
|
68
|
-
const metrics = c.get("metrics");
|
|
69
|
-
if (!metrics && options.metrics) {
|
|
70
|
-
c.set("metrics", metrics);
|
|
71
|
-
}
|
|
72
|
-
const reporter = c.get("reporter");
|
|
73
|
-
if (!reporter) {
|
|
74
|
-
c.set("reporter", reporter || defaultReporter);
|
|
75
|
-
}
|
|
76
|
-
await next();
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
84
|
-
0 && (module.exports = {
|
|
85
|
-
initReporter,
|
|
86
|
-
monitorPlugin
|
|
87
|
-
});
|
|
@@ -1,42 +0,0 @@
|
|
|
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 serverTiming_exports = {};
|
|
20
|
-
__export(serverTiming_exports, {
|
|
21
|
-
ServerTiming: () => ServerTiming
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(serverTiming_exports);
|
|
24
|
-
class ServerTiming {
|
|
25
|
-
get headers() {
|
|
26
|
-
return this.headerList;
|
|
27
|
-
}
|
|
28
|
-
addServeTiming(name, dur, desc) {
|
|
29
|
-
const _name = `bd-${this.meta}-${name}`;
|
|
30
|
-
const value = `${_name};${desc ? `decs="${desc}";` : ""} dur=${dur}`;
|
|
31
|
-
this.headerList.push(value);
|
|
32
|
-
return this;
|
|
33
|
-
}
|
|
34
|
-
constructor(meta) {
|
|
35
|
-
this.headerList = [];
|
|
36
|
-
this.meta = meta;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
40
|
-
0 && (module.exports = {
|
|
41
|
-
ServerTiming
|
|
42
|
-
});
|