@modern-js/prod-server 1.22.1 → 2.0.0-alpha.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 (132) hide show
  1. package/CHANGELOG.md +271 -17
  2. package/dist/js/modern/constants.js +32 -27
  3. package/dist/js/modern/index.js +11 -7
  4. package/dist/js/modern/libs/context/context.js +54 -132
  5. package/dist/js/modern/libs/context/index.js +5 -2
  6. package/dist/js/modern/libs/hook-api/index.js +134 -0
  7. package/dist/js/modern/libs/hook-api/route.js +13 -37
  8. package/dist/js/modern/libs/hook-api/template.js +41 -32
  9. package/dist/js/modern/libs/loadConfig.js +46 -32
  10. package/dist/js/modern/libs/metrics.js +6 -7
  11. package/dist/js/modern/libs/proxy.js +70 -44
  12. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +112 -68
  13. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +246 -225
  14. package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +43 -49
  15. package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +36 -34
  16. package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +83 -113
  17. package/dist/js/modern/libs/render/cache/index.js +93 -50
  18. package/dist/js/modern/libs/render/cache/page-caches/index.js +31 -8
  19. package/dist/js/modern/libs/render/cache/page-caches/lru.js +6 -16
  20. package/dist/js/modern/libs/render/cache/spr.js +133 -167
  21. package/dist/js/modern/libs/render/cache/type.js +0 -1
  22. package/dist/js/modern/libs/render/cache/util.js +71 -45
  23. package/dist/js/modern/libs/render/index.js +76 -61
  24. package/dist/js/modern/libs/render/measure.js +42 -34
  25. package/dist/js/modern/libs/render/reader.js +68 -76
  26. package/dist/js/modern/libs/render/ssr.js +63 -33
  27. package/dist/js/modern/libs/render/static.js +51 -37
  28. package/dist/js/modern/libs/render/type.js +9 -7
  29. package/dist/js/modern/libs/route/index.js +12 -30
  30. package/dist/js/modern/libs/route/matcher.js +28 -50
  31. package/dist/js/modern/libs/route/route.js +9 -31
  32. package/dist/js/modern/libs/serve-file.js +40 -20
  33. package/dist/js/modern/server/index.js +152 -168
  34. package/dist/js/modern/server/modern-server-split.js +44 -60
  35. package/dist/js/modern/server/modern-server.js +416 -526
  36. package/dist/js/modern/type.js +0 -1
  37. package/dist/js/modern/utils.js +62 -58
  38. package/dist/js/modern/worker-server.js +54 -0
  39. package/dist/js/node/constants.js +53 -32
  40. package/dist/js/node/index.js +37 -67
  41. package/dist/js/node/libs/context/context.js +84 -150
  42. package/dist/js/node/libs/context/index.js +28 -16
  43. package/dist/js/node/libs/hook-api/index.js +164 -0
  44. package/dist/js/node/libs/hook-api/route.js +35 -45
  45. package/dist/js/node/libs/hook-api/template.js +64 -40
  46. package/dist/js/node/libs/loadConfig.js +74 -55
  47. package/dist/js/node/libs/metrics.js +28 -12
  48. package/dist/js/node/libs/proxy.js +90 -55
  49. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +99 -61
  50. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +147 -140
  51. package/dist/js/node/libs/render/cache/__tests__/cacheable.js +65 -55
  52. package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +58 -40
  53. package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +105 -119
  54. package/dist/js/node/libs/render/cache/index.js +115 -65
  55. package/dist/js/node/libs/render/cache/page-caches/index.js +54 -16
  56. package/dist/js/node/libs/render/cache/page-caches/lru.js +33 -26
  57. package/dist/js/node/libs/render/cache/spr.js +161 -188
  58. package/dist/js/node/libs/render/cache/type.js +15 -5
  59. package/dist/js/node/libs/render/cache/util.js +99 -63
  60. package/dist/js/node/libs/render/index.js +106 -87
  61. package/dist/js/node/libs/render/measure.js +61 -44
  62. package/dist/js/node/libs/render/reader.js +98 -100
  63. package/dist/js/node/libs/render/ssr.js +92 -58
  64. package/dist/js/node/libs/render/static.js +80 -53
  65. package/dist/js/node/libs/render/type.js +31 -13
  66. package/dist/js/node/libs/route/index.js +35 -44
  67. package/dist/js/node/libs/route/matcher.js +48 -65
  68. package/dist/js/node/libs/route/route.js +29 -37
  69. package/dist/js/node/libs/serve-file.js +74 -37
  70. package/dist/js/node/server/index.js +170 -203
  71. package/dist/js/node/server/modern-server-split.js +70 -73
  72. package/dist/js/node/server/modern-server.js +432 -577
  73. package/dist/js/node/type.js +15 -3
  74. package/dist/js/node/utils.js +87 -93
  75. package/dist/js/node/worker-server.js +77 -0
  76. package/dist/js/treeshaking/constants.js +29 -0
  77. package/dist/js/treeshaking/index.js +13 -0
  78. package/dist/js/treeshaking/libs/context/context.js +274 -0
  79. package/dist/js/treeshaking/libs/context/index.js +5 -0
  80. package/dist/js/treeshaking/libs/hook-api/index.js +281 -0
  81. package/dist/js/treeshaking/libs/hook-api/route.js +68 -0
  82. package/dist/js/treeshaking/libs/hook-api/template.js +127 -0
  83. package/dist/js/treeshaking/libs/loadConfig.js +82 -0
  84. package/dist/js/treeshaking/libs/metrics.js +6 -0
  85. package/dist/js/treeshaking/libs/proxy.js +244 -0
  86. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +291 -0
  87. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +781 -0
  88. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +67 -0
  89. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -0
  90. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +147 -0
  91. package/dist/js/treeshaking/libs/render/cache/index.js +346 -0
  92. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +154 -0
  93. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +84 -0
  94. package/dist/js/treeshaking/libs/render/cache/spr.js +492 -0
  95. package/dist/js/treeshaking/libs/render/cache/type.js +1 -0
  96. package/dist/js/treeshaking/libs/render/cache/util.js +280 -0
  97. package/dist/js/treeshaking/libs/render/index.js +233 -0
  98. package/dist/js/treeshaking/libs/render/measure.js +146 -0
  99. package/dist/js/treeshaking/libs/render/reader.js +339 -0
  100. package/dist/js/treeshaking/libs/render/ssr.js +223 -0
  101. package/dist/js/treeshaking/libs/render/static.js +216 -0
  102. package/dist/js/treeshaking/libs/render/type.js +7 -0
  103. package/dist/js/treeshaking/libs/route/index.js +130 -0
  104. package/dist/js/treeshaking/libs/route/matcher.js +138 -0
  105. package/dist/js/treeshaking/libs/route/route.js +40 -0
  106. package/dist/js/treeshaking/libs/serve-file.js +184 -0
  107. package/dist/js/treeshaking/server/index.js +505 -0
  108. package/dist/js/treeshaking/server/modern-server-split.js +360 -0
  109. package/dist/js/treeshaking/server/modern-server.js +1083 -0
  110. package/dist/js/treeshaking/type.js +1 -0
  111. package/dist/js/treeshaking/utils.js +147 -0
  112. package/dist/js/treeshaking/worker-server.js +177 -0
  113. package/dist/types/libs/context/context.d.ts +1 -1
  114. package/dist/types/libs/hook-api/index.d.ts +5 -0
  115. package/dist/types/libs/hook-api/route.d.ts +9 -14
  116. package/dist/types/libs/hook-api/template.d.ts +19 -9
  117. package/dist/types/libs/render/cache/index.d.ts +4 -2
  118. package/dist/types/libs/render/type.d.ts +3 -1
  119. package/dist/types/libs/route/route.d.ts +0 -1
  120. package/dist/types/libs/serve-file.d.ts +2 -1
  121. package/dist/types/server/index.d.ts +2 -0
  122. package/dist/types/server/modern-server.d.ts +11 -11
  123. package/dist/types/type.d.ts +8 -10
  124. package/dist/types/utils.d.ts +3 -4
  125. package/dist/types/worker-server.d.ts +15 -0
  126. package/package.json +31 -45
  127. package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
  128. package/dist/js/modern/libs/render/modern/index.js +0 -48
  129. package/dist/js/node/libs/render/modern/browser-list.js +0 -14
  130. package/dist/js/node/libs/render/modern/index.js +0 -64
  131. package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
  132. package/dist/types/libs/render/modern/index.d.ts +0 -3
@@ -1,3 +1,15 @@
1
- "use strict";
2
-
3
- var _http = require("http");
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from === "object" || typeof from === "function") {
7
+ for (let key of __getOwnPropNames(from))
8
+ if (!__hasOwnProp.call(to, key) && key !== except)
9
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
+ }
11
+ return to;
12
+ };
13
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
14
+ var type_exports = {};
15
+ module.exports = __toCommonJS(type_exports);
@@ -1,37 +1,56 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __export = (target, all) => {
20
+ for (var name in all)
21
+ __defProp(target, name, { get: all[name], enumerable: true });
22
+ };
23
+ var __copyProps = (to, from, except, desc) => {
24
+ if (from && typeof from === "object" || typeof from === "function") {
25
+ for (let key of __getOwnPropNames(from))
26
+ if (!__hasOwnProp.call(to, key) && key !== except)
27
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
28
+ }
29
+ return to;
30
+ };
31
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
+ var utils_exports = {};
33
+ __export(utils_exports, {
34
+ createErrorDocument: () => createErrorDocument,
35
+ createMiddlewareCollecter: () => createMiddlewareCollecter,
36
+ debug: () => debug,
37
+ getStaticReg: () => getStaticReg,
38
+ headersWithoutCookie: () => headersWithoutCookie,
39
+ isRedirect: () => isRedirect,
40
+ mergeExtension: () => mergeExtension,
41
+ noop: () => noop,
42
+ prepareFavicons: () => prepareFavicons,
43
+ useLocalPrefix: () => useLocalPrefix
5
44
  });
6
- exports.useLocalPrefix = exports.toPath = exports.prepareFavicons = exports.noop = exports.mergeExtension = exports.isRedirect = exports.headersWithoutCookie = exports.getStaticReg = exports.debug = exports.createMiddlewareCollecter = exports.createErrorDocument = void 0;
7
-
8
- var _pathToRegexp = require("path-to-regexp");
9
-
10
- var _utils = require("@modern-js/utils");
11
-
12
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
13
-
14
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
15
-
16
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
17
-
18
- const debug = (0, _utils.createDebugger)('prod-server');
19
- exports.debug = debug;
20
-
21
- const mergeExtension = users => {
45
+ module.exports = __toCommonJS(utils_exports);
46
+ var import_utils = require("@modern-js/utils");
47
+ const debug = (0, import_utils.createDebugger)("prod-server");
48
+ const mergeExtension = (users) => {
22
49
  const output = [];
23
- return {
24
- middleware: output.concat(users)
25
- };
50
+ return { middleware: output.concat(users) };
26
51
  };
27
-
28
- exports.mergeExtension = mergeExtension;
29
-
30
- const noop = () => {// noop
52
+ const noop = () => {
31
53
  };
32
-
33
- exports.noop = noop;
34
-
35
54
  const createErrorDocument = (status, text) => {
36
55
  const title = `${status}: ${text}`;
37
56
  return `<!DOCTYPE html>
@@ -65,108 +84,83 @@ const createErrorDocument = (status, text) => {
65
84
  </html>
66
85
  `;
67
86
  };
68
-
69
- exports.createErrorDocument = createErrorDocument;
70
-
71
87
  const createMiddlewareCollecter = () => {
72
88
  const webMiddlewares = [];
73
89
  const apiMiddlewares = [];
74
-
75
- const addWebMiddleware = input => {
90
+ const addWebMiddleware = (input) => {
76
91
  webMiddlewares.push(input);
77
92
  };
78
-
79
- const addAPIMiddleware = input => {
93
+ const addAPIMiddleware = (input) => {
80
94
  apiMiddlewares.push(input);
81
95
  };
82
-
83
96
  const getMiddlewares = () => ({
84
97
  web: webMiddlewares,
85
98
  api: apiMiddlewares
86
99
  });
87
-
88
100
  return {
89
101
  getMiddlewares,
90
102
  addWebMiddleware,
91
103
  addAPIMiddleware
92
104
  };
93
105
  };
94
-
95
- exports.createMiddlewareCollecter = createMiddlewareCollecter;
96
-
97
- const toPath = (reg, params) => {
98
- const fn = (0, _pathToRegexp.compile)(reg, {
99
- encode: encodeURIComponent
100
- });
101
- return fn(params);
102
- };
103
-
104
- exports.toPath = toPath;
105
-
106
- const useLocalPrefix = url => {
107
- return (0, _utils.isProd)() && !url.includes('.');
106
+ const useLocalPrefix = (url) => {
107
+ return (0, import_utils.isProd)() && !url.includes(".");
108
108
  };
109
-
110
- exports.useLocalPrefix = useLocalPrefix;
111
-
112
- const getStaticReg = (output = {}) => {
109
+ const getStaticReg = (output = {}, html = {}) => {
113
110
  const {
114
- favicon,
115
- faviconByEntries,
116
- cssPath,
117
- jsPath,
118
- mediaPath,
119
- assetPrefix = '/'
111
+ distPath: { css: cssPath, js: jsPath, media: mediaPath } = {},
112
+ assetPrefix = "/"
120
113
  } = output;
121
- const prefix = useLocalPrefix(assetPrefix) ? assetPrefix : '';
114
+ const { favicon, faviconByEntries } = html;
115
+ const prefix = useLocalPrefix(assetPrefix) ? assetPrefix : "";
122
116
  const favicons = prepareFavicons(favicon, faviconByEntries);
123
- const staticFiles = [cssPath, jsPath, mediaPath].filter(v => Boolean(v));
124
- const staticReg = ['static/', 'upload/', ...staticFiles];
125
- const iconReg = ['favicon.ico', 'icon.png', ...favicons];
126
- const regPrefix = prefix.endsWith('/') ? prefix : `${prefix}/`;
127
- const staticPathRegExp = new RegExp(`^${regPrefix}(${[...staticReg, ...iconReg].join('|')})`);
117
+ const staticFiles = [cssPath, jsPath, mediaPath].filter((v) => Boolean(v));
118
+ const staticReg = ["static/", "upload/", ...staticFiles];
119
+ const iconReg = ["favicon.ico", "icon.png", ...favicons];
120
+ const regPrefix = prefix.endsWith("/") ? prefix : `${prefix}/`;
121
+ const staticPathRegExp = new RegExp(
122
+ `^${regPrefix}(${[...staticReg, ...iconReg].join("|")})`
123
+ );
128
124
  return staticPathRegExp;
129
125
  };
130
-
131
- exports.getStaticReg = getStaticReg;
132
-
133
126
  const prepareFavicons = (favicon, faviconByEntries) => {
134
127
  const faviconNames = [];
135
-
136
128
  if (favicon) {
137
- faviconNames.push(favicon.substring(favicon.lastIndexOf('/') + 1));
129
+ faviconNames.push(favicon.substring(favicon.lastIndexOf("/") + 1));
138
130
  }
139
-
140
131
  if (faviconByEntries) {
141
- Object.keys(faviconByEntries).forEach(f => {
132
+ Object.keys(faviconByEntries).forEach((f) => {
142
133
  const curFavicon = faviconByEntries[f];
143
-
144
134
  if (curFavicon) {
145
- faviconNames.push(curFavicon.substring(curFavicon.lastIndexOf('/') + 1));
135
+ faviconNames.push(
136
+ curFavicon.substring(curFavicon.lastIndexOf("/") + 1)
137
+ );
146
138
  }
147
139
  });
148
140
  }
149
-
150
141
  return faviconNames;
151
142
  };
152
-
153
- exports.prepareFavicons = prepareFavicons;
154
-
155
- const headersWithoutCookie = headers => {
156
- if (typeof headers.cookie !== 'undefined') {
157
- const safeHeaders = _objectSpread({}, headers);
158
-
143
+ const headersWithoutCookie = (headers) => {
144
+ if (typeof headers.cookie !== "undefined") {
145
+ const safeHeaders = __spreadValues({}, headers);
159
146
  delete safeHeaders.cookie;
160
147
  return safeHeaders;
161
148
  }
162
-
163
149
  return headers;
164
150
  };
165
-
166
- exports.headersWithoutCookie = headersWithoutCookie;
167
-
168
- const isRedirect = code => {
151
+ const isRedirect = (code) => {
169
152
  return [301, 302, 307, 308].includes(code);
170
153
  };
171
-
172
- exports.isRedirect = isRedirect;
154
+ // Annotate the CommonJS export names for ESM import in node:
155
+ 0 && (module.exports = {
156
+ createErrorDocument,
157
+ createMiddlewareCollecter,
158
+ debug,
159
+ getStaticReg,
160
+ headersWithoutCookie,
161
+ isRedirect,
162
+ mergeExtension,
163
+ noop,
164
+ prepareFavicons,
165
+ useLocalPrefix
166
+ });
@@ -0,0 +1,77 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var __async = (__this, __arguments, generator) => {
19
+ return new Promise((resolve, reject) => {
20
+ var fulfilled = (value) => {
21
+ try {
22
+ step(generator.next(value));
23
+ } catch (e) {
24
+ reject(e);
25
+ }
26
+ };
27
+ var rejected = (value) => {
28
+ try {
29
+ step(generator.throw(value));
30
+ } catch (e) {
31
+ reject(e);
32
+ }
33
+ };
34
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
35
+ step((generator = generator.apply(__this, __arguments)).next());
36
+ });
37
+ };
38
+ var worker_server_exports = {};
39
+ __export(worker_server_exports, {
40
+ createHandler: () => createHandler
41
+ });
42
+ module.exports = __toCommonJS(worker_server_exports);
43
+ var import_route = require("./libs/route");
44
+ const createHandler = (manifest) => {
45
+ const routeMgr = new import_route.RouteMatchManager();
46
+ const { pages, routes } = manifest;
47
+ routeMgr.reset(routes);
48
+ return (ctx) => __async(void 0, null, function* () {
49
+ var _a, _b, _c, _d, _e, _f;
50
+ const pageMatch = routeMgr.match(ctx.url);
51
+ if (!pageMatch) {
52
+ ctx.body = "404: Page not found";
53
+ ctx.status = 404;
54
+ return;
55
+ }
56
+ const page = pages[pageMatch.spec.urlPath];
57
+ (_b = (_a = ctx.request).query) != null ? _b : _a.query = ctx.query;
58
+ (_d = (_c = ctx.request).pathname) != null ? _d : _c.pathname = ctx.pathname;
59
+ (_f = (_e = ctx.request).params) != null ? _f : _e.params = ctx.params;
60
+ const params = pageMatch.parseURLParams(ctx.url);
61
+ ctx.body = yield page.serverRender({
62
+ template: page.template,
63
+ query: ctx.query,
64
+ request: ctx.request,
65
+ response: ctx.response,
66
+ pathname: ctx.pathname,
67
+ req: ctx.request,
68
+ res: ctx.response,
69
+ params: ctx.params || params || {}
70
+ });
71
+ ctx.status = 200;
72
+ });
73
+ };
74
+ // Annotate the CommonJS export names for ESM import in node:
75
+ 0 && (module.exports = {
76
+ createHandler
77
+ });
@@ -0,0 +1,29 @@
1
+ var AGGRED_DIR = {
2
+ mock: "config/mock",
3
+ server: "server",
4
+ api: "api",
5
+ shared: "shared",
6
+ lambda: "lambda"
7
+ };
8
+ var ApiServerMode = /* @__PURE__ */ function(ApiServerMode2) {
9
+ ApiServerMode2["func"] = "function";
10
+ ApiServerMode2["frame"] = "framework";
11
+ return ApiServerMode2;
12
+ }(ApiServerMode || {});
13
+ var ERROR_DIGEST = {
14
+ INIT: "Server init error",
15
+ ENOTF: "Page could not be found",
16
+ WARMUP: "SSR warmup failed",
17
+ EINTER: "Internal server error",
18
+ ERENDER: "SSR render failed",
19
+ EMICROINJ: "Get micro-frontend info failed"
20
+ };
21
+ var ERROR_PAGE_TEXT = {
22
+ 404: "This page could not be found.",
23
+ 500: "Internal Server Error."
24
+ };
25
+ var RUN_MODE = {
26
+ FULL: "full",
27
+ TYPE: "type"
28
+ };
29
+ export { AGGRED_DIR, ApiServerMode, ERROR_DIGEST, ERROR_PAGE_TEXT, RUN_MODE };
@@ -0,0 +1,13 @@
1
+ import { Server } from "./server";
2
+ import { ModernServer } from "./server/modern-server";
3
+ import { createProxyHandler } from "./libs/proxy";
4
+ export * from "./type";
5
+ export * from "./constants";
6
+ var src_default = function(options) {
7
+ if (options == null) {
8
+ throw new Error("can not start mserver without options");
9
+ }
10
+ var server = new Server(options);
11
+ return server.init();
12
+ };
13
+ export { ModernServer, Server, createProxyHandler, src_default as default };
@@ -0,0 +1,274 @@
1
+ function _classCallCheck(instance, Constructor) {
2
+ if (!(instance instanceof Constructor)) {
3
+ throw new TypeError("Cannot call a class as a function");
4
+ }
5
+ }
6
+ function _defineProperties(target, props) {
7
+ for(var i = 0; i < props.length; i++){
8
+ var descriptor = props[i];
9
+ descriptor.enumerable = descriptor.enumerable || false;
10
+ descriptor.configurable = true;
11
+ if ("value" in descriptor) descriptor.writable = true;
12
+ Object.defineProperty(target, descriptor.key, descriptor);
13
+ }
14
+ }
15
+ function _createClass(Constructor, protoProps, staticProps) {
16
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
17
+ if (staticProps) _defineProperties(Constructor, staticProps);
18
+ return Constructor;
19
+ }
20
+ function _defineProperty(obj, key, value) {
21
+ if (key in obj) {
22
+ Object.defineProperty(obj, key, {
23
+ value: value,
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true
27
+ });
28
+ } else {
29
+ obj[key] = value;
30
+ }
31
+ return obj;
32
+ }
33
+ function _instanceof(left, right) {
34
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
35
+ return !!right[Symbol.hasInstance](left);
36
+ } else {
37
+ return left instanceof right;
38
+ }
39
+ }
40
+ import { URL } from "url";
41
+ import qs from "querystring";
42
+ import { Buffer } from "buffer";
43
+ import createEtag from "etag";
44
+ import fresh from "fresh";
45
+ import { headersWithoutCookie } from "../../utils";
46
+ var ModernServerContext = /*#__PURE__*/ function() {
47
+ "use strict";
48
+ function ModernServerContext(req, res, options) {
49
+ _classCallCheck(this, ModernServerContext);
50
+ _defineProperty(this, "req", void 0);
51
+ _defineProperty(this, "res", void 0);
52
+ _defineProperty(this, "params", {});
53
+ _defineProperty(this, "serverData", void 0);
54
+ _defineProperty(this, "options", {});
55
+ this.req = req;
56
+ this.res = res;
57
+ this.options = options || {};
58
+ this.serverData = {};
59
+ this.bind();
60
+ }
61
+ _createClass(ModernServerContext, [
62
+ {
63
+ key: "logger",
64
+ get: function get() {
65
+ return this.req.logger;
66
+ }
67
+ },
68
+ {
69
+ key: "metrics",
70
+ get: function get() {
71
+ return this.req.metrics;
72
+ }
73
+ },
74
+ {
75
+ key: "bind",
76
+ value: function bind() {
77
+ var _this = this;
78
+ var ref = this, req = ref.req, res = ref.res;
79
+ req.get = function(key) {
80
+ return _this.getReqHeader(key);
81
+ };
82
+ res.set = function(key, value) {
83
+ return _this.res.setHeader(key, value);
84
+ };
85
+ res.send = function(body) {
86
+ _this.send(body);
87
+ };
88
+ }
89
+ },
90
+ {
91
+ key: "send",
92
+ value: function send(body) {
93
+ try {
94
+ var generateETag = !this.res.getHeader("ETag") && this.options.etag;
95
+ if (body !== void 0 && generateETag) {
96
+ var encoding = typeof body === "string" ? "utf-8" : void 0;
97
+ var buf = !Buffer.isBuffer(body) ? Buffer.from(body, encoding) : body;
98
+ var etag = createEtag(buf, {
99
+ weak: true
100
+ });
101
+ if (etag) {
102
+ this.res.setHeader("ETag", etag);
103
+ }
104
+ }
105
+ if (this.fresh) {
106
+ this.status = 304;
107
+ }
108
+ } catch (e) {
109
+ this.logger.error(e.message);
110
+ }
111
+ this.res.end(body);
112
+ }
113
+ },
114
+ {
115
+ key: "setParams",
116
+ value: function setParams(params) {
117
+ this.params = params;
118
+ }
119
+ },
120
+ {
121
+ key: "setServerData",
122
+ value: function setServerData(key, value) {
123
+ this.serverData[key] = value;
124
+ }
125
+ },
126
+ {
127
+ key: "getReqHeader",
128
+ value: function getReqHeader(key) {
129
+ var req = this.req;
130
+ var field = key.toLowerCase();
131
+ switch(field){
132
+ case "referer":
133
+ case "referrer":
134
+ return req.headers.referrer || req.headers.referer || "";
135
+ default:
136
+ return req.headers[field] || "";
137
+ }
138
+ }
139
+ },
140
+ {
141
+ key: "fresh",
142
+ get: function get() {
143
+ var ref = this, status = ref.status, res = ref.res, method = ref.method;
144
+ if ("GET" !== method && "HEAD" !== method) {
145
+ return false;
146
+ }
147
+ if (status >= 200 && status < 300 || 304 === status) {
148
+ return fresh(this.headers, {
149
+ etag: res.getHeader("ETag"),
150
+ "last-modified": res.getHeader("Last-Modified")
151
+ });
152
+ }
153
+ return false;
154
+ }
155
+ },
156
+ {
157
+ key: "headers",
158
+ get: function get() {
159
+ return this.req.headers;
160
+ }
161
+ },
162
+ {
163
+ key: "method",
164
+ get: function get() {
165
+ return this.req.method;
166
+ }
167
+ },
168
+ {
169
+ key: "url",
170
+ get: function get() {
171
+ return this.req.url || "";
172
+ },
173
+ set: function set(val) {
174
+ this.req.url = val;
175
+ }
176
+ },
177
+ {
178
+ key: "host",
179
+ get: function get() {
180
+ var host = this.getReqHeader("X-Forwarded-Host");
181
+ if (!host) {
182
+ host = this.getReqHeader("Host");
183
+ }
184
+ return host.split(/\s*,\s*/, 1)[0] || "";
185
+ }
186
+ },
187
+ {
188
+ key: "protocol",
189
+ get: function get() {
190
+ if (this.req.socket.encrypted) {
191
+ return "https";
192
+ }
193
+ var proto = this.getReqHeader("X-Forwarded-Proto");
194
+ return proto ? proto.split(/\s*,\s*/, 1)[0] : "http";
195
+ }
196
+ },
197
+ {
198
+ key: "origin",
199
+ get: function get() {
200
+ return "".concat(this.protocol, "://").concat(this.host);
201
+ }
202
+ },
203
+ {
204
+ key: "href",
205
+ get: function get() {
206
+ return this.origin + this.url;
207
+ }
208
+ },
209
+ {
210
+ key: "parsedURL",
211
+ get: function get() {
212
+ var url = new URL(this.req.url, this.origin);
213
+ return url;
214
+ }
215
+ },
216
+ {
217
+ key: "path",
218
+ get: function get() {
219
+ return this.parsedURL.pathname;
220
+ },
221
+ set: function set(p) {
222
+ var url = new URL(this.req.url, this.origin);
223
+ if (!url || !p) {
224
+ return;
225
+ }
226
+ if (url.pathname === p) {
227
+ return;
228
+ }
229
+ url.pathname = p;
230
+ this.url = url.toString();
231
+ }
232
+ },
233
+ {
234
+ key: "querystring",
235
+ get: function get() {
236
+ if (!this.req) {
237
+ return "";
238
+ }
239
+ return this.parsedURL.search.replace(/^\?/, "") || "";
240
+ }
241
+ },
242
+ {
243
+ key: "query",
244
+ get: function get() {
245
+ var str = this.querystring;
246
+ return qs.parse(str);
247
+ }
248
+ },
249
+ {
250
+ key: "status",
251
+ get: function get() {
252
+ return this.res.statusCode;
253
+ },
254
+ set: function set(statusCode) {
255
+ this.res.statusCode = statusCode;
256
+ }
257
+ },
258
+ {
259
+ key: "resHasHandled",
260
+ value: function resHasHandled() {
261
+ return this.res.writableEnded;
262
+ }
263
+ },
264
+ {
265
+ key: "error",
266
+ value: function error(dig) {
267
+ var e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "";
268
+ this.logger.error("Web Server Error - ".concat(dig, ", error = %s, req.url = %s, req.headers = %o"), _instanceof(e, Error) ? e.stack || e.message : e, this.path, headersWithoutCookie(this.headers));
269
+ }
270
+ }
271
+ ]);
272
+ return ModernServerContext;
273
+ }();
274
+ export { ModernServerContext };
@@ -0,0 +1,5 @@
1
+ import { ModernServerContext } from "./context";
2
+ var createContext = function(req, res, options) {
3
+ return new ModernServerContext(req, res, options);
4
+ };
5
+ export { ModernServerContext, createContext };