@modern-js/prod-server 2.47.0 → 2.47.1-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 (169) hide show
  1. package/README.md +20 -16
  2. package/dist/cjs/{libs/hook-api/route.js → error.js} +16 -25
  3. package/dist/cjs/index.js +46 -24
  4. package/dist/esm/error.js +15 -0
  5. package/dist/esm/index.js +105 -19
  6. package/dist/esm-node/error.js +14 -0
  7. package/dist/esm-node/index.js +44 -17
  8. package/dist/types/error.d.ts +6 -0
  9. package/dist/types/index.d.ts +21 -11
  10. package/package.json +18 -92
  11. package/dist/cjs/constants.js +0 -72
  12. package/dist/cjs/libs/context/context.js +0 -240
  13. package/dist/cjs/libs/context/index.js +0 -31
  14. package/dist/cjs/libs/hook-api/afterRenderForStream.js +0 -34
  15. package/dist/cjs/libs/hook-api/base.js +0 -107
  16. package/dist/cjs/libs/hook-api/index.js +0 -85
  17. package/dist/cjs/libs/hook-api/index.worker.js +0 -119
  18. package/dist/cjs/libs/hook-api/template.js +0 -74
  19. package/dist/cjs/libs/hook-api/templateForStream.js +0 -52
  20. package/dist/cjs/libs/loadConfig.js +0 -70
  21. package/dist/cjs/libs/metrics.js +0 -33
  22. package/dist/cjs/libs/preload/flushServerHeader.js +0 -49
  23. package/dist/cjs/libs/preload/index.js +0 -24
  24. package/dist/cjs/libs/preload/parseLinks.js +0 -124
  25. package/dist/cjs/libs/preload/shouldFlushServerHeader.js +0 -52
  26. package/dist/cjs/libs/preload/transformLinks2String.js +0 -150
  27. package/dist/cjs/libs/proxy.js +0 -99
  28. package/dist/cjs/libs/render/index.js +0 -109
  29. package/dist/cjs/libs/render/measure.js +0 -74
  30. package/dist/cjs/libs/render/ssr.js +0 -126
  31. package/dist/cjs/libs/render/ssrCache/cacheMod.js +0 -56
  32. package/dist/cjs/libs/render/ssrCache/index.js +0 -83
  33. package/dist/cjs/libs/render/ssrCache/manager.js +0 -106
  34. package/dist/cjs/libs/render/static.js +0 -72
  35. package/dist/cjs/libs/render/type.js +0 -33
  36. package/dist/cjs/libs/render/utils.js +0 -41
  37. package/dist/cjs/libs/reporter.js +0 -39
  38. package/dist/cjs/libs/route/index.js +0 -85
  39. package/dist/cjs/libs/route/matcher.js +0 -117
  40. package/dist/cjs/libs/route/route.js +0 -49
  41. package/dist/cjs/libs/serveFile.js +0 -84
  42. package/dist/cjs/libs/serverTiming.js +0 -44
  43. package/dist/cjs/renderHtml.js +0 -72
  44. package/dist/cjs/server/index.js +0 -229
  45. package/dist/cjs/server/modernServer.js +0 -532
  46. package/dist/cjs/server/modernServerSplit.js +0 -67
  47. package/dist/cjs/type.js +0 -16
  48. package/dist/cjs/utils.js +0 -164
  49. package/dist/cjs/workerServer.js +0 -264
  50. package/dist/esm/constants.js +0 -43
  51. package/dist/esm/libs/context/context.js +0 -292
  52. package/dist/esm/libs/context/index.js +0 -8
  53. package/dist/esm/libs/hook-api/afterRenderForStream.js +0 -33
  54. package/dist/esm/libs/hook-api/base.js +0 -108
  55. package/dist/esm/libs/hook-api/index.js +0 -55
  56. package/dist/esm/libs/hook-api/index.worker.js +0 -113
  57. package/dist/esm/libs/hook-api/route.js +0 -41
  58. package/dist/esm/libs/hook-api/template.js +0 -84
  59. package/dist/esm/libs/hook-api/templateForStream.js +0 -35
  60. package/dist/esm/libs/loadConfig.js +0 -36
  61. package/dist/esm/libs/metrics.js +0 -9
  62. package/dist/esm/libs/preload/flushServerHeader.js +0 -50
  63. package/dist/esm/libs/preload/index.js +0 -2
  64. package/dist/esm/libs/preload/parseLinks.js +0 -172
  65. package/dist/esm/libs/preload/shouldFlushServerHeader.js +0 -27
  66. package/dist/esm/libs/preload/transformLinks2String.js +0 -140
  67. package/dist/esm/libs/proxy.js +0 -160
  68. package/dist/esm/libs/render/index.js +0 -175
  69. package/dist/esm/libs/render/measure.js +0 -69
  70. package/dist/esm/libs/render/ssr.js +0 -129
  71. package/dist/esm/libs/render/ssrCache/cacheMod.js +0 -33
  72. package/dist/esm/libs/render/ssrCache/index.js +0 -151
  73. package/dist/esm/libs/render/ssrCache/manager.js +0 -170
  74. package/dist/esm/libs/render/static.js +0 -100
  75. package/dist/esm/libs/render/type.js +0 -9
  76. package/dist/esm/libs/render/utils.js +0 -16
  77. package/dist/esm/libs/reporter.js +0 -15
  78. package/dist/esm/libs/route/index.js +0 -116
  79. package/dist/esm/libs/route/matcher.js +0 -128
  80. package/dist/esm/libs/route/route.js +0 -26
  81. package/dist/esm/libs/serveFile.js +0 -68
  82. package/dist/esm/libs/serverTiming.js +0 -30
  83. package/dist/esm/renderHtml.js +0 -150
  84. package/dist/esm/server/index.js +0 -426
  85. package/dist/esm/server/modernServer.js +0 -1018
  86. package/dist/esm/server/modernServerSplit.js +0 -138
  87. package/dist/esm/type.js +0 -0
  88. package/dist/esm/utils.js +0 -103
  89. package/dist/esm/workerServer.js +0 -347
  90. package/dist/esm-node/constants.js +0 -43
  91. package/dist/esm-node/libs/context/context.js +0 -206
  92. package/dist/esm-node/libs/context/index.js +0 -6
  93. package/dist/esm-node/libs/hook-api/afterRenderForStream.js +0 -10
  94. package/dist/esm-node/libs/hook-api/base.js +0 -72
  95. package/dist/esm-node/libs/hook-api/index.js +0 -57
  96. package/dist/esm-node/libs/hook-api/index.worker.js +0 -92
  97. package/dist/esm-node/libs/hook-api/route.js +0 -24
  98. package/dist/esm-node/libs/hook-api/template.js +0 -49
  99. package/dist/esm-node/libs/hook-api/templateForStream.js +0 -28
  100. package/dist/esm-node/libs/loadConfig.js +0 -34
  101. package/dist/esm-node/libs/metrics.js +0 -9
  102. package/dist/esm-node/libs/preload/flushServerHeader.js +0 -25
  103. package/dist/esm-node/libs/preload/index.js +0 -2
  104. package/dist/esm-node/libs/preload/parseLinks.js +0 -90
  105. package/dist/esm-node/libs/preload/shouldFlushServerHeader.js +0 -27
  106. package/dist/esm-node/libs/preload/transformLinks2String.js +0 -126
  107. package/dist/esm-node/libs/proxy.js +0 -74
  108. package/dist/esm-node/libs/render/index.js +0 -75
  109. package/dist/esm-node/libs/render/measure.js +0 -49
  110. package/dist/esm-node/libs/render/ssr.js +0 -92
  111. package/dist/esm-node/libs/render/ssrCache/cacheMod.js +0 -22
  112. package/dist/esm-node/libs/render/ssrCache/index.js +0 -59
  113. package/dist/esm-node/libs/render/ssrCache/manager.js +0 -82
  114. package/dist/esm-node/libs/render/static.js +0 -38
  115. package/dist/esm-node/libs/render/type.js +0 -9
  116. package/dist/esm-node/libs/render/utils.js +0 -16
  117. package/dist/esm-node/libs/reporter.js +0 -15
  118. package/dist/esm-node/libs/route/index.js +0 -60
  119. package/dist/esm-node/libs/route/matcher.js +0 -93
  120. package/dist/esm-node/libs/route/route.js +0 -25
  121. package/dist/esm-node/libs/serveFile.js +0 -49
  122. package/dist/esm-node/libs/serverTiming.js +0 -20
  123. package/dist/esm-node/renderHtml.js +0 -80
  124. package/dist/esm-node/server/index.js +0 -195
  125. package/dist/esm-node/server/modernServer.js +0 -498
  126. package/dist/esm-node/server/modernServerSplit.js +0 -43
  127. package/dist/esm-node/type.js +0 -0
  128. package/dist/esm-node/utils.js +0 -132
  129. package/dist/esm-node/workerServer.js +0 -239
  130. package/dist/types/constants.d.ts +0 -30
  131. package/dist/types/libs/context/context.d.ts +0 -62
  132. package/dist/types/libs/context/index.d.ts +0 -7
  133. package/dist/types/libs/hook-api/afterRenderForStream.d.ts +0 -4
  134. package/dist/types/libs/hook-api/base.d.ts +0 -53
  135. package/dist/types/libs/hook-api/index.d.ts +0 -6
  136. package/dist/types/libs/hook-api/index.worker.d.ts +0 -19
  137. package/dist/types/libs/hook-api/route.d.ts +0 -9
  138. package/dist/types/libs/hook-api/template.d.ts +0 -22
  139. package/dist/types/libs/hook-api/templateForStream.d.ts +0 -8
  140. package/dist/types/libs/loadConfig.d.ts +0 -13
  141. package/dist/types/libs/metrics.d.ts +0 -3
  142. package/dist/types/libs/preload/flushServerHeader.d.ts +0 -14
  143. package/dist/types/libs/preload/index.d.ts +0 -2
  144. package/dist/types/libs/preload/parseLinks.d.ts +0 -13
  145. package/dist/types/libs/preload/shouldFlushServerHeader.d.ts +0 -3
  146. package/dist/types/libs/preload/transformLinks2String.d.ts +0 -3
  147. package/dist/types/libs/proxy.d.ts +0 -13
  148. package/dist/types/libs/render/index.d.ts +0 -21
  149. package/dist/types/libs/render/measure.d.ts +0 -10
  150. package/dist/types/libs/render/ssr.d.ts +0 -12
  151. package/dist/types/libs/render/ssrCache/cacheMod.d.ts +0 -8
  152. package/dist/types/libs/render/ssrCache/index.d.ts +0 -7
  153. package/dist/types/libs/render/ssrCache/manager.d.ts +0 -14
  154. package/dist/types/libs/render/static.d.ts +0 -3
  155. package/dist/types/libs/render/type.d.ts +0 -36
  156. package/dist/types/libs/render/utils.d.ts +0 -5
  157. package/dist/types/libs/reporter.d.ts +0 -2
  158. package/dist/types/libs/route/index.d.ts +0 -15
  159. package/dist/types/libs/route/matcher.d.ts +0 -15
  160. package/dist/types/libs/route/route.d.ts +0 -14
  161. package/dist/types/libs/serveFile.d.ts +0 -9
  162. package/dist/types/libs/serverTiming.d.ts +0 -13
  163. package/dist/types/renderHtml.d.ts +0 -23
  164. package/dist/types/server/index.d.ts +0 -55
  165. package/dist/types/server/modernServer.d.ts +0 -71
  166. package/dist/types/server/modernServerSplit.d.ts +0 -2
  167. package/dist/types/type.d.ts +0 -77
  168. package/dist/types/utils.d.ts +0 -24
  169. package/dist/types/workerServer.d.ts +0 -59
@@ -1,26 +0,0 @@
1
- import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
2
- import { _ as _define_property } from "@swc/helpers/_/_define_property";
3
- var ModernRoute = function ModernRoute2(routeSpec) {
4
- "use strict";
5
- _class_call_check(this, ModernRoute2);
6
- _define_property(this, "entryName", void 0);
7
- _define_property(this, "urlPath", void 0);
8
- _define_property(this, "entryPath", void 0);
9
- _define_property(this, "bundle", void 0);
10
- _define_property(this, "isApi", void 0);
11
- _define_property(this, "isSSR", void 0);
12
- _define_property(this, "isSPA", void 0);
13
- _define_property(this, "params", {});
14
- _define_property(this, "responseHeaders", void 0);
15
- this.entryName = routeSpec.entryName || "";
16
- this.urlPath = routeSpec.urlPath;
17
- this.entryPath = routeSpec.entryPath || "";
18
- this.isSSR = routeSpec.isSSR || false;
19
- this.isSPA = routeSpec.isSPA || false;
20
- this.isApi = routeSpec.isApi || false;
21
- this.bundle = routeSpec.bundle || "";
22
- this.responseHeaders = routeSpec.responseHeaders;
23
- };
24
- export {
25
- ModernRoute
26
- };
@@ -1,68 +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 serve from "serve-static";
4
- import { isString, isRegExp } from "@modern-js/utils";
5
- var removedPrefix = function(req, prefix) {
6
- var useLocalPrefix = !prefix.includes(".");
7
- if (useLocalPrefix) {
8
- req.url = req.url.slice(prefix.length);
9
- return function() {
10
- req.url = prefix + req.url;
11
- };
12
- } else {
13
- return function() {
14
- };
15
- }
16
- };
17
- var faviconFallbackHandler = function(context, next) {
18
- if (context.url === "/favicon.ico") {
19
- context.res.statusCode = 204;
20
- context.res.end();
21
- } else {
22
- next();
23
- }
24
- };
25
- var createStaticFileHandler = function(rules) {
26
- var assetPrefix = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "/";
27
- return function() {
28
- var _ref = (
29
- // eslint-disable-next-line consistent-return
30
- _async_to_generator(function(context, next) {
31
- var requestUrl, req, res, hitRule, resume;
32
- return _ts_generator(this, function(_state) {
33
- requestUrl = context.url, req = context.req, res = context.res;
34
- hitRule = rules.find(function(item) {
35
- if (isString(item.path) && requestUrl.startsWith(item.path)) {
36
- return true;
37
- } else if (isRegExp(item.path) && item.path.test(requestUrl)) {
38
- return true;
39
- }
40
- return false;
41
- });
42
- if (hitRule) {
43
- resume = removedPrefix(req, assetPrefix);
44
- serve(hitRule.target)(req, res, function() {
45
- resume();
46
- next();
47
- });
48
- } else {
49
- return [
50
- 2,
51
- next()
52
- ];
53
- }
54
- return [
55
- 2
56
- ];
57
- });
58
- })
59
- );
60
- return function(context, next) {
61
- return _ref.apply(this, arguments);
62
- };
63
- }();
64
- };
65
- export {
66
- createStaticFileHandler,
67
- faviconFallbackHandler
68
- };
@@ -1,30 +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
- import { _ as _define_property } from "@swc/helpers/_/_define_property";
4
- var SERVER_TIMING = "Server-Timing";
5
- var ServerTiming = /* @__PURE__ */ function() {
6
- "use strict";
7
- function ServerTiming2(res, meta) {
8
- _class_call_check(this, ServerTiming2);
9
- _define_property(this, "meta", void 0);
10
- _define_property(this, "res", void 0);
11
- this.meta = meta;
12
- this.res = res;
13
- }
14
- _create_class(ServerTiming2, [
15
- {
16
- key: "addServeTiming",
17
- value: function addServeTiming(name, dur, desc) {
18
- var _name = "bd-".concat(this.meta, "-").concat(name);
19
- var serverTiming = this.res.getHeader(SERVER_TIMING) || this.res.getHeader(SERVER_TIMING.toLocaleLowerCase());
20
- var value = "".concat(_name, ";").concat(desc ? 'decs="'.concat(desc, '";') : "", " dur=").concat(dur);
21
- this.res.set(SERVER_TIMING, serverTiming ? "".concat(serverTiming, ", ").concat(value) : value);
22
- return this;
23
- }
24
- }
25
- ]);
26
- return ServerTiming2;
27
- }();
28
- export {
29
- ServerTiming
30
- };
@@ -1,150 +0,0 @@
1
- var __getOwnPropNames = Object.getOwnPropertyNames;
2
- var __commonJS = (cb, mod) => function __require() {
3
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
4
- };
5
- import { _ as _assert_this_initialized } from "@swc/helpers/_/_assert_this_initialized";
6
- import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
7
- import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
8
- import { _ as _create_class } from "@swc/helpers/_/_create_class";
9
- import { _ as _define_property } from "@swc/helpers/_/_define_property";
10
- import { _ as _inherits } from "@swc/helpers/_/_inherits";
11
- import { _ as _create_super } from "@swc/helpers/_/_create_super";
12
- import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
13
- import { OutgoingMessage } from "http";
14
- import { Readable } from "stream";
15
- import { Socket } from "net";
16
- import { Server } from "./server";
17
- import { isRedirect } from "./utils";
18
- var require_renderHtml = __commonJS({
19
- "src/renderHtml.ts"(exports, module) {
20
- var IncomingMessageLike = /* @__PURE__ */ function(Readable2) {
21
- "use strict";
22
- _inherits(IncomingMessageLike2, Readable2);
23
- var _super = _create_super(IncomingMessageLike2);
24
- function IncomingMessageLike2(param) {
25
- var method = param.method, url = param.url, headers = param.headers;
26
- _class_call_check(this, IncomingMessageLike2);
27
- var _this;
28
- _this = _super.call(this);
29
- _define_property(_assert_this_initialized(_this), "headers", void 0);
30
- _define_property(_assert_this_initialized(_this), "method", void 0);
31
- _define_property(_assert_this_initialized(_this), "url", void 0);
32
- _define_property(_assert_this_initialized(_this), "socket", void 0);
33
- _this.socket = new Socket();
34
- _this.headers = headers || {};
35
- _this.headers.host = "localhost:8080";
36
- _this.method = method || "get";
37
- _this.url = url;
38
- return _this;
39
- }
40
- return IncomingMessageLike2;
41
- }(Readable);
42
- var ServerResponseLike = /* @__PURE__ */ function(OutgoingMessage2) {
43
- "use strict";
44
- _inherits(ServerResponseLike2, OutgoingMessage2);
45
- var _super = _create_super(ServerResponseLike2);
46
- function ServerResponseLike2() {
47
- _class_call_check(this, ServerResponseLike2);
48
- var _this;
49
- _this = _super.call(this);
50
- _define_property(_assert_this_initialized(_this), "statusCode", void 0);
51
- _define_property(_assert_this_initialized(_this), "data", void 0);
52
- _this.statusCode = 200;
53
- _this.data = [];
54
- return _this;
55
- }
56
- _create_class(ServerResponseLike2, [
57
- {
58
- key: "end",
59
- value: function end(chunk, _encoding, cb) {
60
- this.data.push(chunk.toString());
61
- cb && cb();
62
- this.emit("finish");
63
- return this;
64
- }
65
- }
66
- ]);
67
- return ServerResponseLike2;
68
- }(OutgoingMessage);
69
- var CustomServer = /* @__PURE__ */ function(Server2) {
70
- "use strict";
71
- _inherits(CustomServer2, Server2);
72
- var _super = _create_super(CustomServer2);
73
- function CustomServer2() {
74
- _class_call_check(this, CustomServer2);
75
- return _super.apply(this, arguments);
76
- }
77
- _create_class(CustomServer2, [
78
- {
79
- key: "render",
80
- value: function render(req, res, _url) {
81
- var _this = this;
82
- return _async_to_generator(function() {
83
- var handler;
84
- return _ts_generator(this, function(_state) {
85
- handler = _this.getRequestHandler();
86
- handler(req, res);
87
- return [
88
- 2,
89
- null
90
- ];
91
- });
92
- })();
93
- }
94
- }
95
- ]);
96
- return CustomServer2;
97
- }(Server);
98
- function renderHtml(_) {
99
- return _renderHtml.apply(this, arguments);
100
- }
101
- function _renderHtml() {
102
- _renderHtml = _async_to_generator(function(param) {
103
- var url, method, headers, body, serverOptions, req, res, customServer;
104
- return _ts_generator(this, function(_state) {
105
- switch (_state.label) {
106
- case 0:
107
- url = param.url, method = param.method, headers = param.headers, body = param.body, serverOptions = param.serverOptions;
108
- req = new IncomingMessageLike({
109
- method,
110
- url,
111
- headers
112
- });
113
- if (body) {
114
- req.push(body);
115
- req.push(null);
116
- }
117
- res = new ServerResponseLike();
118
- customServer = new CustomServer(serverOptions);
119
- return [
120
- 4,
121
- customServer.init({
122
- disableHttpServer: true
123
- })
124
- ];
125
- case 1:
126
- _state.sent();
127
- customServer.render(req, res);
128
- return [
129
- 2,
130
- new Promise(function(resolve) {
131
- res.addListener("finish", function() {
132
- resolve({
133
- headers: res.getHeaders(),
134
- redirected: isRedirect(res.statusCode),
135
- status: res.statusCode,
136
- url,
137
- body: res.data.join()
138
- });
139
- });
140
- })
141
- ];
142
- }
143
- });
144
- });
145
- return _renderHtml.apply(this, arguments);
146
- }
147
- module.exports = renderHtml;
148
- }
149
- });
150
- export default require_renderHtml();
@@ -1,426 +0,0 @@
1
- import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
- import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3
- import { _ as _create_class } from "@swc/helpers/_/_create_class";
4
- import { _ as _define_property } from "@swc/helpers/_/_define_property";
5
- import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
6
- import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
7
- import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
8
- import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
9
- import path from "path";
10
- import { fs, createLogger, SHARED_DIR, OUTPUT_CONFIG_FILE, dotenv, dotenvExpand, INTERNAL_SERVER_PLUGINS, ensureAbsolutePath } from "@modern-js/utils";
11
- import { serverManager, AppContext, ConfigContext, loadPlugins } from "@modern-js/server-core";
12
- import { metrics as defaultMetrics } from "../libs/metrics";
13
- import { loadConfig, getServerConfigPath, requireConfig } from "../libs/loadConfig";
14
- import { debug } from "../utils";
15
- import { createProdServer } from "./modernServerSplit";
16
- var Server = /* @__PURE__ */ function() {
17
- "use strict";
18
- function Server2(options) {
19
- _class_call_check(this, Server2);
20
- _define_property(this, "options", void 0);
21
- _define_property(this, "serverImpl", createProdServer);
22
- _define_property(this, "server", void 0);
23
- _define_property(this, "app", void 0);
24
- _define_property(this, "runner", void 0);
25
- _define_property(this, "serverConfig", void 0);
26
- options.logger = options.logger || createLogger({
27
- level: "warn"
28
- });
29
- options.metrics = options.metrics || defaultMetrics;
30
- this.options = options;
31
- this.serverConfig = {};
32
- }
33
- _create_class(Server2, [
34
- {
35
- key: "init",
36
- value: (
37
- /**
38
- * 初始化顺序
39
- * - 读取 .env.{process.env.MODERN_ENV} 文件,加载环境变量
40
- * - 获取 server runtime config
41
- * - 设置 context
42
- * - 创建 hooksRunner
43
- * - 合并插件,内置插件和 serverConfig 中配置的插件
44
- * - 执行 config hook
45
- * - 获取最终的配置
46
- * - 设置配置到 context
47
- * - 初始化 server
48
- * - 执行 prepare hook
49
- * - 执行 server init
50
- */
51
- function init() {
52
- var _ref = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {
53
- disableHttpServer: false
54
- }, _ref_disableHttpServer = _ref.disableHttpServer, disableHttpServer = _ref_disableHttpServer === void 0 ? false : _ref_disableHttpServer;
55
- var _this = this;
56
- return _async_to_generator(function() {
57
- var options, result, ref, ref1, result1, ref2, ref3;
58
- return _ts_generator(this, function(_state) {
59
- switch (_state.label) {
60
- case 0:
61
- options = _this.options;
62
- return [
63
- 4,
64
- _this.loadServerEnv(options)
65
- ];
66
- case 1:
67
- _state.sent();
68
- _this.initServerConfig(options);
69
- return [
70
- 4,
71
- _this.injectContext(_this.runner, options)
72
- ];
73
- case 2:
74
- _state.sent();
75
- return [
76
- 4,
77
- _this.createHookRunner()
78
- ];
79
- case 3:
80
- _this.runner = _state.sent();
81
- return [
82
- 4,
83
- _this.initConfig(_this.runner, options)
84
- ];
85
- case 4:
86
- _state.sent();
87
- return [
88
- 4,
89
- _this.injectContext(_this.runner, options)
90
- ];
91
- case 5:
92
- _state.sent();
93
- _this.server = _this.serverImpl(options);
94
- return [
95
- 4,
96
- _this.runPrepareHook(_this.runner)
97
- ];
98
- case 6:
99
- _state.sent();
100
- if (!!disableHttpServer)
101
- return [
102
- 3,
103
- 8
104
- ];
105
- return [
106
- 4,
107
- _this.server.createHTTPServer(_this.getRequestHandler())
108
- ];
109
- case 7:
110
- _this.app = _state.sent();
111
- _state.label = 8;
112
- case 8:
113
- return [
114
- 4,
115
- _this.runner.beforeServerInit({
116
- app: _this.app,
117
- server: _this.server
118
- })
119
- ];
120
- case 9:
121
- result = _state.sent();
122
- ref = result, ref1 = ref.app, _this.app = ref1 === void 0 ? _this.app : ref1, _this.server = ref.server, ref;
123
- return [
124
- 4,
125
- _this.server.onInit(_this.runner, _this.app)
126
- ];
127
- case 10:
128
- _state.sent();
129
- return [
130
- 4,
131
- _this.runner.afterServerInit({
132
- app: _this.app,
133
- server: _this.server
134
- })
135
- ];
136
- case 11:
137
- result1 = _state.sent();
138
- ref2 = result1, ref3 = ref2.app, _this.app = ref3 === void 0 ? _this.app : ref3, _this.server = ref2.server, ref2;
139
- return [
140
- 2,
141
- _this
142
- ];
143
- }
144
- });
145
- })();
146
- }
147
- )
148
- },
149
- {
150
- key: "runConfigHook",
151
- value: (
152
- /**
153
- * Execute config hooks
154
- * @param runner
155
- * @param options
156
- */
157
- function runConfigHook(runner, serverConfig) {
158
- var newServerConfig = runner.config(serverConfig || {});
159
- return newServerConfig;
160
- }
161
- )
162
- },
163
- {
164
- key: "runPrepareHook",
165
- value: function runPrepareHook(runner) {
166
- return _async_to_generator(function() {
167
- return _ts_generator(this, function(_state) {
168
- runner.prepare();
169
- return [
170
- 2
171
- ];
172
- });
173
- })();
174
- }
175
- },
176
- {
177
- key: "initServerConfig",
178
- value: function initServerConfig(options) {
179
- var pwd = options.pwd, serverConfigFile = options.serverConfigFile;
180
- var distDirectory = path.join(pwd, options.config.output.path || "dist");
181
- var serverConfigPath = getServerConfigPath(distDirectory, serverConfigFile);
182
- var serverConfig = requireConfig(serverConfigPath);
183
- this.serverConfig = serverConfig;
184
- }
185
- },
186
- {
187
- key: "initConfig",
188
- value: (
189
- /**
190
- *
191
- * merge cliConfig and serverConfig
192
- */
193
- function initConfig(runner, options) {
194
- var _this = this;
195
- return _async_to_generator(function() {
196
- var pwd, config, serverConfig, finalServerConfig, resolvedConfigPath;
197
- return _ts_generator(this, function(_state) {
198
- pwd = options.pwd, config = options.config;
199
- serverConfig = _this.serverConfig;
200
- finalServerConfig = _this.runConfigHook(runner, serverConfig);
201
- resolvedConfigPath = ensureAbsolutePath(pwd, path.join(config.output.path || "dist", OUTPUT_CONFIG_FILE));
202
- options.config = loadConfig({
203
- cliConfig: config,
204
- serverConfig: finalServerConfig,
205
- resolvedConfigPath
206
- });
207
- return [
208
- 2
209
- ];
210
- });
211
- })();
212
- }
213
- )
214
- },
215
- {
216
- key: "close",
217
- value: function close() {
218
- var _this = this;
219
- return _async_to_generator(function() {
220
- var _this_server_close, _this_server;
221
- return _ts_generator(this, function(_state) {
222
- switch (_state.label) {
223
- case 0:
224
- return [
225
- 4,
226
- (_this_server_close = (_this_server = _this.server).close) === null || _this_server_close === void 0 ? void 0 : _this_server_close.call(_this_server)
227
- ];
228
- case 1:
229
- _state.sent();
230
- _this.app.close();
231
- return [
232
- 2
233
- ];
234
- }
235
- });
236
- })();
237
- }
238
- },
239
- {
240
- key: "listen",
241
- value: function listen(options, listener) {
242
- var callback = function() {
243
- listener === null || listener === void 0 ? void 0 : listener();
244
- };
245
- if (typeof options === "object") {
246
- if (process.env.PORT) {
247
- Object.assign(options, {
248
- port: process.env.PORT
249
- });
250
- }
251
- this.app.listen(options, callback);
252
- } else {
253
- this.app.listen(process.env.PORT || options || 8080, callback);
254
- }
255
- }
256
- },
257
- {
258
- key: "getRequestHandler",
259
- value: function getRequestHandler() {
260
- var _this = this;
261
- return function(req, res, next) {
262
- var requestHandler = _this.server.getRequestHandler();
263
- return requestHandler(req, res, next);
264
- };
265
- }
266
- },
267
- {
268
- key: "render",
269
- value: function render(req, res, url) {
270
- var _this = this;
271
- return _async_to_generator(function() {
272
- return _ts_generator(this, function(_state) {
273
- return [
274
- 2,
275
- _this.server.render(req, res, url)
276
- ];
277
- });
278
- })();
279
- }
280
- },
281
- {
282
- key: "createHookRunner",
283
- value: function createHookRunner() {
284
- var _this = this;
285
- return _async_to_generator(function() {
286
- var options, _options_internalPlugins, internalPlugins, pwd, _options_plugins, plugins, serverPlugins, loadedPlugins, hooksRunner;
287
- return _ts_generator(this, function(_state) {
288
- switch (_state.label) {
289
- case 0:
290
- serverManager.clear();
291
- options = _this.options;
292
- _options_internalPlugins = options.internalPlugins, internalPlugins = _options_internalPlugins === void 0 ? INTERNAL_SERVER_PLUGINS : _options_internalPlugins, pwd = options.pwd, _options_plugins = options.plugins, plugins = _options_plugins === void 0 ? [] : _options_plugins;
293
- serverPlugins = _this.serverConfig.plugins || [];
294
- loadedPlugins = loadPlugins(pwd, _to_consumable_array(serverPlugins).concat(_to_consumable_array(plugins)), {
295
- internalPlugins
296
- });
297
- debug("plugins", loadedPlugins);
298
- loadedPlugins.forEach(function(p) {
299
- serverManager.usePlugin(p);
300
- });
301
- return [
302
- 4,
303
- serverManager.init()
304
- ];
305
- case 1:
306
- hooksRunner = _state.sent();
307
- return [
308
- 2,
309
- hooksRunner
310
- ];
311
- }
312
- });
313
- })();
314
- }
315
- },
316
- {
317
- key: "injectContext",
318
- value: function injectContext(runner, options) {
319
- var _this = this;
320
- return _async_to_generator(function() {
321
- var appContext, config, pwd;
322
- return _ts_generator(this, function(_state) {
323
- appContext = _this.initAppContext();
324
- config = options.config, pwd = options.pwd;
325
- ConfigContext.set(config);
326
- AppContext.set(_object_spread_props(_object_spread({}, appContext), {
327
- distDirectory: path.join(pwd, config.output.path || "dist")
328
- }));
329
- return [
330
- 2
331
- ];
332
- });
333
- })();
334
- }
335
- },
336
- {
337
- key: "initAppContext",
338
- value: function initAppContext() {
339
- var options = this.options;
340
- var appDirectory = options.pwd, _options_plugins = options.plugins, plugins = _options_plugins === void 0 ? [] : _options_plugins, config = options.config, appContext = options.appContext;
341
- var serverPlugins = plugins.map(function(p) {
342
- return {
343
- server: p
344
- };
345
- });
346
- return {
347
- appDirectory,
348
- apiDirectory: appContext === null || appContext === void 0 ? void 0 : appContext.apiDirectory,
349
- lambdaDirectory: appContext === null || appContext === void 0 ? void 0 : appContext.lambdaDirectory,
350
- sharedDirectory: (appContext === null || appContext === void 0 ? void 0 : appContext.sharedDirectory) || path.resolve(appDirectory, SHARED_DIR),
351
- distDirectory: path.join(appDirectory, config.output.path || "dist"),
352
- plugins: serverPlugins
353
- };
354
- }
355
- },
356
- {
357
- key: "loadServerEnv",
358
- value: function loadServerEnv(options) {
359
- return _async_to_generator(function() {
360
- var appDirectory, serverEnv, defaultEnvPath, serverEnvPath, _i, _iter, envPath, _tmp, envConfig;
361
- return _ts_generator(this, function(_state) {
362
- switch (_state.label) {
363
- case 0:
364
- appDirectory = options.pwd;
365
- serverEnv = process.env.MODERN_ENV;
366
- defaultEnvPath = path.resolve(appDirectory, ".env");
367
- serverEnvPath = path.resolve(appDirectory, ".env.".concat(serverEnv));
368
- _i = 0, _iter = [
369
- serverEnvPath,
370
- defaultEnvPath
371
- ];
372
- _state.label = 1;
373
- case 1:
374
- if (!(_i < _iter.length))
375
- return [
376
- 3,
377
- 6
378
- ];
379
- envPath = _iter[_i];
380
- return [
381
- 4,
382
- fs.pathExists(envPath)
383
- ];
384
- case 2:
385
- _tmp = _state.sent();
386
- if (!_tmp)
387
- return [
388
- 3,
389
- 4
390
- ];
391
- return [
392
- 4,
393
- fs.stat(envPath)
394
- ];
395
- case 3:
396
- _tmp = !_state.sent().isDirectory();
397
- _state.label = 4;
398
- case 4:
399
- if (_tmp) {
400
- envConfig = dotenv.config({
401
- path: envPath
402
- });
403
- dotenvExpand(envConfig);
404
- }
405
- _state.label = 5;
406
- case 5:
407
- _i++;
408
- return [
409
- 3,
410
- 1
411
- ];
412
- case 6:
413
- return [
414
- 2
415
- ];
416
- }
417
- });
418
- })();
419
- }
420
- }
421
- ]);
422
- return Server2;
423
- }();
424
- export {
425
- Server
426
- };