@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.
Files changed (43) hide show
  1. package/dist/cjs/adapters/node/plugins/resource.js +1 -1
  2. package/dist/cjs/constants.js +12 -9
  3. package/dist/cjs/plugins/customServer/index.js +5 -5
  4. package/dist/cjs/plugins/index.js +7 -3
  5. package/dist/cjs/plugins/log.js +3 -3
  6. package/dist/cjs/plugins/monitors.js +183 -0
  7. package/dist/cjs/plugins/render/index.js +5 -3
  8. package/dist/cjs/plugins/render/render.js +4 -11
  9. package/dist/cjs/utils/error.js +3 -3
  10. package/dist/esm/adapters/node/plugins/resource.js +1 -1
  11. package/dist/esm/constants.js +10 -8
  12. package/dist/esm/plugins/customServer/index.js +8 -8
  13. package/dist/esm/plugins/index.js +4 -2
  14. package/dist/esm/plugins/log.js +4 -4
  15. package/dist/esm/plugins/monitors.js +261 -0
  16. package/dist/esm/plugins/render/index.js +5 -3
  17. package/dist/esm/plugins/render/render.js +5 -12
  18. package/dist/esm/utils/error.js +3 -3
  19. package/dist/esm-node/adapters/node/plugins/resource.js +1 -1
  20. package/dist/esm-node/constants.js +10 -8
  21. package/dist/esm-node/plugins/customServer/index.js +6 -6
  22. package/dist/esm-node/plugins/index.js +4 -2
  23. package/dist/esm-node/plugins/log.js +3 -3
  24. package/dist/esm-node/plugins/monitors.js +156 -0
  25. package/dist/esm-node/plugins/render/index.js +4 -2
  26. package/dist/esm-node/plugins/render/render.js +4 -11
  27. package/dist/esm-node/utils/error.js +3 -3
  28. package/dist/types/constants.d.ts +2 -1
  29. package/dist/types/plugins/index.d.ts +1 -1
  30. package/dist/types/plugins/monitors.d.ts +6 -0
  31. package/dist/types/types/render.d.ts +7 -4
  32. package/dist/types/types/requestHandler.d.ts +0 -2
  33. package/dist/types/types/server.d.ts +6 -2
  34. package/dist/types/utils/error.d.ts +2 -2
  35. package/package.json +7 -7
  36. package/dist/cjs/plugins/monitor.js +0 -87
  37. package/dist/cjs/plugins/render/serverTiming.js +0 -42
  38. package/dist/esm/plugins/monitor.js +0 -120
  39. package/dist/esm/plugins/render/serverTiming.js +0 -29
  40. package/dist/esm-node/plugins/monitor.js +0 -62
  41. package/dist/esm-node/plugins/render/serverTiming.js +0 -18
  42. package/dist/types/plugins/monitor.d.ts +0 -9
  43. package/dist/types/plugins/render/serverTiming.d.ts +0 -8
@@ -1,5 +1,5 @@
1
1
  import { MAIN_ENTRY_NAME } from "@modern-js/utils/universal/constants";
2
- import { initReporter } from "../monitor";
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, nodeReq, reporter, templates, serverManifest, locals, metrics, loaderContext }) => {
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
- logger.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));
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
- logger.debug(`SSR Debug - ${name}, cost = %s, req.url = %s`, dur, pathname);
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, logger, req);
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, logger, req) {
46
+ function onError(digest, error, monitors, req) {
47
47
  const headerData = req && parseHeaders(req);
48
48
  headerData && delete headerData.cookie;
49
- if (logger) {
50
- logger.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);
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 ServerReportTimings {
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 { monitorPlugin } from './monitor';
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
- /** Metrics */
11
+ /** @deprecated */
12
+ logger: Logger;
13
+ /** @deprecated */
13
14
  metrics?: Metrics;
14
- serverManifest: ServerManifest;
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 { Logger } from '@modern-js/types';
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, logger?: Logger, req?: Request): void;
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.55.0",
18
+ "version": "2.56.0",
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.55.0",
56
- "@modern-js/runtime-utils": "2.55.0",
57
- "@modern-js/utils": "2.55.0"
55
+ "@modern-js/plugin": "2.56.0",
56
+ "@modern-js/runtime-utils": "2.56.0",
57
+ "@modern-js/utils": "2.56.0"
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.55.0",
68
- "@scripts/jest-config": "2.55.0",
69
- "@scripts/build": "2.55.0"
67
+ "@modern-js/types": "2.56.0",
68
+ "@scripts/build": "2.56.0",
69
+ "@scripts/jest-config": "2.56.0"
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
- });
@@ -1,120 +0,0 @@
1
- import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
- import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
- import { time } from "@modern-js/runtime-utils/time";
4
- import { ServerReportTimings } from "../constants";
5
- var defaultReporter = {
6
- init: function init() {
7
- },
8
- reportError: function reportError() {
9
- },
10
- reportTiming: function reportTiming() {
11
- },
12
- reportInfo: function reportInfo() {
13
- },
14
- reportWarn: function reportWarn() {
15
- }
16
- };
17
- function initReporter(entryName) {
18
- return function() {
19
- var _ref = _async_to_generator(function(c, next) {
20
- var reporter, getCost, cost;
21
- return _ts_generator(this, function(_state) {
22
- switch (_state.label) {
23
- case 0:
24
- reporter = c.get("reporter");
25
- if (!!reporter)
26
- return [
27
- 3,
28
- 2
29
- ];
30
- return [
31
- 4,
32
- next()
33
- ];
34
- case 1:
35
- _state.sent();
36
- return [
37
- 2
38
- ];
39
- case 2:
40
- return [
41
- 4,
42
- reporter.init({
43
- entryName
44
- })
45
- ];
46
- case 3:
47
- _state.sent();
48
- getCost = time();
49
- return [
50
- 4,
51
- next()
52
- ];
53
- case 4:
54
- _state.sent();
55
- cost = getCost();
56
- reporter.reportTiming(ServerReportTimings.SERVER_HANDLE_REQUEST, cost);
57
- return [
58
- 2
59
- ];
60
- }
61
- });
62
- });
63
- return function(c, next) {
64
- return _ref.apply(this, arguments);
65
- };
66
- }();
67
- }
68
- var monitorPlugin = function(options) {
69
- return {
70
- name: "@modern-js/plugin-monitor",
71
- setup: function setup(api) {
72
- return {
73
- prepare: function prepare() {
74
- var middlewares = api.useAppContext().middlewares;
75
- middlewares.push({
76
- name: "monitor",
77
- handler: function() {
78
- var _ref = _async_to_generator(function(c, next) {
79
- var logger, metrics, reporter;
80
- return _ts_generator(this, function(_state) {
81
- switch (_state.label) {
82
- case 0:
83
- logger = c.get("logger");
84
- if (!logger && options.logger) {
85
- c.set("logger", options.logger);
86
- }
87
- metrics = c.get("metrics");
88
- if (!metrics && options.metrics) {
89
- c.set("metrics", metrics);
90
- }
91
- reporter = c.get("reporter");
92
- if (!reporter) {
93
- c.set("reporter", reporter || defaultReporter);
94
- }
95
- return [
96
- 4,
97
- next()
98
- ];
99
- case 1:
100
- _state.sent();
101
- return [
102
- 2
103
- ];
104
- }
105
- });
106
- });
107
- return function(c, next) {
108
- return _ref.apply(this, arguments);
109
- };
110
- }()
111
- });
112
- }
113
- };
114
- }
115
- };
116
- };
117
- export {
118
- initReporter,
119
- monitorPlugin
120
- };
@@ -1,29 +0,0 @@
1
- import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
2
- import { _ as _create_class } from "@swc/helpers/_/_create_class";
3
- var ServerTiming = /* @__PURE__ */ function() {
4
- "use strict";
5
- function ServerTiming2(meta) {
6
- _class_call_check(this, ServerTiming2);
7
- this.headerList = [];
8
- this.meta = meta;
9
- }
10
- var _proto = ServerTiming2.prototype;
11
- _proto.addServeTiming = function addServeTiming(name, dur, desc) {
12
- var _name = "bd-".concat(this.meta, "-").concat(name);
13
- var value = "".concat(_name, ";").concat(desc ? 'decs="'.concat(desc, '";') : "", " dur=").concat(dur);
14
- this.headerList.push(value);
15
- return this;
16
- };
17
- _create_class(ServerTiming2, [
18
- {
19
- key: "headers",
20
- get: function get() {
21
- return this.headerList;
22
- }
23
- }
24
- ]);
25
- return ServerTiming2;
26
- }();
27
- export {
28
- ServerTiming
29
- };
@@ -1,62 +0,0 @@
1
- import { time } from "@modern-js/runtime-utils/time";
2
- import { ServerReportTimings } from "../constants";
3
- const defaultReporter = {
4
- init() {
5
- },
6
- reportError() {
7
- },
8
- reportTiming() {
9
- },
10
- reportInfo() {
11
- },
12
- reportWarn() {
13
- }
14
- };
15
- function initReporter(entryName) {
16
- return async (c, next) => {
17
- const reporter = c.get("reporter");
18
- if (!reporter) {
19
- await next();
20
- return;
21
- }
22
- await reporter.init({
23
- entryName
24
- });
25
- const getCost = time();
26
- await next();
27
- const cost = getCost();
28
- reporter.reportTiming(ServerReportTimings.SERVER_HANDLE_REQUEST, cost);
29
- };
30
- }
31
- const monitorPlugin = (options) => ({
32
- name: "@modern-js/plugin-monitor",
33
- setup(api) {
34
- return {
35
- prepare() {
36
- const { middlewares } = api.useAppContext();
37
- middlewares.push({
38
- name: "monitor",
39
- handler: async (c, next) => {
40
- const logger = c.get("logger");
41
- if (!logger && options.logger) {
42
- c.set("logger", options.logger);
43
- }
44
- const metrics = c.get("metrics");
45
- if (!metrics && options.metrics) {
46
- c.set("metrics", metrics);
47
- }
48
- const reporter = c.get("reporter");
49
- if (!reporter) {
50
- c.set("reporter", reporter || defaultReporter);
51
- }
52
- await next();
53
- }
54
- });
55
- }
56
- };
57
- }
58
- });
59
- export {
60
- initReporter,
61
- monitorPlugin
62
- };
@@ -1,18 +0,0 @@
1
- class ServerTiming {
2
- get headers() {
3
- return this.headerList;
4
- }
5
- addServeTiming(name, dur, desc) {
6
- const _name = `bd-${this.meta}-${name}`;
7
- const value = `${_name};${desc ? `decs="${desc}";` : ""} dur=${dur}`;
8
- this.headerList.push(value);
9
- return this;
10
- }
11
- constructor(meta) {
12
- this.headerList = [];
13
- this.meta = meta;
14
- }
15
- }
16
- export {
17
- ServerTiming
18
- };
@@ -1,9 +0,0 @@
1
- import { Logger, Metrics, Reporter } from '@modern-js/types';
2
- import { Context, Next, ServerEnv, ServerPlugin } from '../types';
3
- export interface MonitorOptions {
4
- logger?: Logger;
5
- metrics?: Metrics;
6
- reporter?: Reporter;
7
- }
8
- export declare function initReporter(entryName: string): (c: Context<ServerEnv>, next: Next) => Promise<void>;
9
- export declare const monitorPlugin: (options: MonitorOptions) => ServerPlugin;
@@ -1,8 +0,0 @@
1
- import { ServerTiming as ServerTimingInterafce } from '@modern-js/types';
2
- export declare class ServerTiming implements ServerTimingInterafce {
3
- private headerList;
4
- private meta;
5
- constructor(meta: string);
6
- get headers(): string[];
7
- addServeTiming(name: string, dur: number, desc?: string): this;
8
- }