@modern-js/prod-server 2.8.0 → 2.9.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.
- package/CHANGELOG.md +9 -0
- package/dist/cjs/server/index.js +8 -6
- package/dist/esm/server/index.js +71 -18
- package/dist/esm-node/server/index.js +9 -6
- package/dist/js/modern/constants.js +35 -0
- package/dist/js/modern/index.js +18 -0
- package/dist/js/modern/libs/context/context.js +160 -0
- package/dist/js/modern/libs/context/index.js +6 -0
- package/dist/js/modern/libs/hook-api/index.js +134 -0
- package/dist/js/modern/libs/hook-api/route.js +20 -0
- package/dist/js/modern/libs/hook-api/template.js +73 -0
- package/dist/js/modern/libs/loadConfig.js +62 -0
- package/dist/js/modern/libs/logger.js +111 -0
- package/dist/js/modern/libs/metrics.js +11 -0
- package/dist/js/modern/libs/proxy.js +92 -0
- package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +114 -0
- package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +254 -0
- package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +47 -0
- package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +37 -0
- package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +91 -0
- package/dist/js/modern/libs/render/cache/index.js +115 -0
- package/dist/js/modern/libs/render/cache/page-caches/index.js +32 -0
- package/dist/js/modern/libs/render/cache/page-caches/lru.js +29 -0
- package/dist/js/modern/libs/render/cache/spr.js +248 -0
- package/dist/js/modern/libs/render/cache/type.js +0 -0
- package/dist/js/modern/libs/render/cache/util.js +102 -0
- package/dist/js/modern/libs/render/index.js +86 -0
- package/dist/js/modern/libs/render/measure.js +68 -0
- package/dist/js/modern/libs/render/reader.js +107 -0
- package/dist/js/modern/libs/render/ssr.js +100 -0
- package/dist/js/modern/libs/render/static.js +60 -0
- package/dist/js/modern/libs/render/type.js +9 -0
- package/dist/js/modern/libs/route/index.js +54 -0
- package/dist/js/modern/libs/route/matcher.js +87 -0
- package/dist/js/modern/libs/route/route.js +16 -0
- package/dist/js/modern/libs/serve-file.js +67 -0
- package/dist/js/modern/server/index.js +208 -0
- package/dist/js/modern/server/modern-server-split.js +74 -0
- package/dist/js/modern/server/modern-server.js +554 -0
- package/dist/js/modern/type.js +0 -0
- package/dist/js/modern/utils.js +136 -0
- package/dist/js/modern/worker-server.js +89 -0
- package/dist/js/node/constants.js +62 -0
- package/dist/js/node/index.js +44 -0
- package/dist/js/node/libs/context/context.js +189 -0
- package/dist/js/node/libs/context/index.js +30 -0
- package/dist/js/node/libs/hook-api/index.js +164 -0
- package/dist/js/node/libs/hook-api/route.js +43 -0
- package/dist/js/node/libs/hook-api/template.js +97 -0
- package/dist/js/node/libs/loadConfig.js +91 -0
- package/dist/js/node/libs/logger.js +133 -0
- package/dist/js/node/libs/metrics.js +34 -0
- package/dist/js/node/libs/proxy.js +114 -0
- package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +115 -0
- package/dist/js/node/libs/render/cache/__tests__/cache.test.js +245 -0
- package/dist/js/node/libs/render/cache/__tests__/cacheable.js +70 -0
- package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +60 -0
- package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +114 -0
- package/dist/js/node/libs/render/cache/index.js +134 -0
- package/dist/js/node/libs/render/cache/page-caches/index.js +55 -0
- package/dist/js/node/libs/render/cache/page-caches/lru.js +58 -0
- package/dist/js/node/libs/render/cache/spr.js +270 -0
- package/dist/js/node/libs/render/cache/type.js +15 -0
- package/dist/js/node/libs/render/cache/util.js +138 -0
- package/dist/js/node/libs/render/index.js +115 -0
- package/dist/js/node/libs/render/measure.js +90 -0
- package/dist/js/node/libs/render/reader.js +140 -0
- package/dist/js/node/libs/render/ssr.js +123 -0
- package/dist/js/node/libs/render/static.js +89 -0
- package/dist/js/node/libs/render/type.js +32 -0
- package/dist/js/node/libs/route/index.js +78 -0
- package/dist/js/node/libs/route/matcher.js +106 -0
- package/dist/js/node/libs/route/route.js +39 -0
- package/dist/js/node/libs/serve-file.js +97 -0
- package/dist/js/node/server/index.js +219 -0
- package/dist/js/node/server/modern-server-split.js +97 -0
- package/dist/js/node/server/modern-server.js +559 -0
- package/dist/js/node/type.js +15 -0
- package/dist/js/node/utils.js +166 -0
- package/dist/js/node/worker-server.js +113 -0
- package/dist/js/treeshaking/constants.js +29 -0
- package/dist/js/treeshaking/index.js +13 -0
- package/dist/js/treeshaking/libs/context/context.js +274 -0
- package/dist/js/treeshaking/libs/context/index.js +5 -0
- package/dist/js/treeshaking/libs/hook-api/index.js +281 -0
- package/dist/js/treeshaking/libs/hook-api/route.js +68 -0
- package/dist/js/treeshaking/libs/hook-api/template.js +127 -0
- package/dist/js/treeshaking/libs/loadConfig.js +82 -0
- package/dist/js/treeshaking/libs/logger.js +205 -0
- package/dist/js/treeshaking/libs/metrics.js +6 -0
- package/dist/js/treeshaking/libs/proxy.js +244 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +291 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +781 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +67 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +147 -0
- package/dist/js/treeshaking/libs/render/cache/index.js +346 -0
- package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +154 -0
- package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +84 -0
- package/dist/js/treeshaking/libs/render/cache/spr.js +492 -0
- package/dist/js/treeshaking/libs/render/cache/type.js +1 -0
- package/dist/js/treeshaking/libs/render/cache/util.js +280 -0
- package/dist/js/treeshaking/libs/render/index.js +234 -0
- package/dist/js/treeshaking/libs/render/measure.js +146 -0
- package/dist/js/treeshaking/libs/render/reader.js +339 -0
- package/dist/js/treeshaking/libs/render/ssr.js +223 -0
- package/dist/js/treeshaking/libs/render/static.js +216 -0
- package/dist/js/treeshaking/libs/render/type.js +7 -0
- package/dist/js/treeshaking/libs/route/index.js +130 -0
- package/dist/js/treeshaking/libs/route/matcher.js +143 -0
- package/dist/js/treeshaking/libs/route/route.js +40 -0
- package/dist/js/treeshaking/libs/serve-file.js +184 -0
- package/dist/js/treeshaking/server/index.js +505 -0
- package/dist/js/treeshaking/server/modern-server-split.js +360 -0
- package/dist/js/treeshaking/server/modern-server.js +1089 -0
- package/dist/js/treeshaking/type.js +1 -0
- package/dist/js/treeshaking/utils.js +147 -0
- package/dist/js/treeshaking/worker-server.js +233 -0
- package/dist/types/libs/context/context.d.ts +1 -1
- package/dist/types/utils.d.ts +1 -1
- package/package.json +9 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @modern-js/prod-server
|
|
2
2
|
|
|
3
|
+
## 2.9.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 49bb8cd0ef: fix: move chunkmap.css comment to bottom of head
|
|
8
|
+
fix: 将 chunkmap.css 标记移动到 head 底部
|
|
9
|
+
- @modern-js/server-core@2.9.0
|
|
10
|
+
- @modern-js/utils@2.9.0
|
|
11
|
+
|
|
3
12
|
## 2.8.0
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
package/dist/cjs/server/index.js
CHANGED
|
@@ -31,7 +31,6 @@ __export(server_exports, {
|
|
|
31
31
|
});
|
|
32
32
|
module.exports = __toCommonJS(server_exports);
|
|
33
33
|
var import_path = __toESM(require("path"));
|
|
34
|
-
var import_fs = __toESM(require("fs"));
|
|
35
34
|
var import_utils = require("@modern-js/utils");
|
|
36
35
|
var import_server_core = require("@modern-js/server-core");
|
|
37
36
|
var import_metrics = require("../libs/metrics");
|
|
@@ -64,7 +63,7 @@ class Server {
|
|
|
64
63
|
*/
|
|
65
64
|
async init() {
|
|
66
65
|
const { options } = this;
|
|
67
|
-
this.loadServerEnv(options);
|
|
66
|
+
await this.loadServerEnv(options);
|
|
68
67
|
this.initServerConfig(options);
|
|
69
68
|
await this.injectContext(this.runner, options);
|
|
70
69
|
this.runner = await this.createHookRunner();
|
|
@@ -185,13 +184,16 @@ class Server {
|
|
|
185
184
|
plugins: serverPlugins
|
|
186
185
|
};
|
|
187
186
|
}
|
|
188
|
-
loadServerEnv(options) {
|
|
187
|
+
async loadServerEnv(options) {
|
|
189
188
|
const { pwd: appDirectory } = options;
|
|
190
189
|
const serverEnv = process.env.MODERN_ENV;
|
|
190
|
+
const defaultEnvPath = import_path.default.resolve(appDirectory, `.env`);
|
|
191
191
|
const serverEnvPath = import_path.default.resolve(appDirectory, `.env.${serverEnv}`);
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
192
|
+
for (const envPath of [defaultEnvPath, serverEnvPath]) {
|
|
193
|
+
if (await import_utils.fs.pathExists(envPath) && !(await import_utils.fs.stat(envPath)).isDirectory()) {
|
|
194
|
+
const envConfig = import_utils.dotenv.config({ path: envPath });
|
|
195
|
+
(0, import_utils.dotenvExpand)(envConfig);
|
|
196
|
+
}
|
|
195
197
|
}
|
|
196
198
|
}
|
|
197
199
|
}
|
package/dist/esm/server/index.js
CHANGED
|
@@ -219,8 +219,7 @@ var __generator = this && this.__generator || function(thisArg, body) {
|
|
|
219
219
|
}
|
|
220
220
|
};
|
|
221
221
|
import path from "path";
|
|
222
|
-
import fs from "
|
|
223
|
-
import { Logger, SHARED_DIR, OUTPUT_CONFIG_FILE, dotenv, dotenvExpand, INTERNAL_SERVER_PLUGINS } from "@modern-js/utils";
|
|
222
|
+
import { fs, Logger, SHARED_DIR, OUTPUT_CONFIG_FILE, dotenv, dotenvExpand, INTERNAL_SERVER_PLUGINS } from "@modern-js/utils";
|
|
224
223
|
import { serverManager, AppContext, ConfigContext, loadPlugins } from "@modern-js/server-core";
|
|
225
224
|
import { metrics as defaultMetrics } from "../libs/metrics";
|
|
226
225
|
import { loadConfig, getServerConfigPath, requireConfig } from "../libs/loadConfig";
|
|
@@ -267,50 +266,55 @@ var Server = /*#__PURE__*/ function() {
|
|
|
267
266
|
switch(_state.label){
|
|
268
267
|
case 0:
|
|
269
268
|
options = _this.options;
|
|
270
|
-
|
|
269
|
+
return [
|
|
270
|
+
4,
|
|
271
|
+
_this.loadServerEnv(options)
|
|
272
|
+
];
|
|
273
|
+
case 1:
|
|
274
|
+
_state.sent();
|
|
271
275
|
_this.initServerConfig(options);
|
|
272
276
|
return [
|
|
273
277
|
4,
|
|
274
278
|
_this.injectContext(_this.runner, options)
|
|
275
279
|
];
|
|
276
|
-
case
|
|
280
|
+
case 2:
|
|
277
281
|
_state.sent();
|
|
278
282
|
return [
|
|
279
283
|
4,
|
|
280
284
|
_this.createHookRunner()
|
|
281
285
|
];
|
|
282
|
-
case
|
|
286
|
+
case 3:
|
|
283
287
|
_this.runner = _state.sent();
|
|
284
288
|
return [
|
|
285
289
|
4,
|
|
286
290
|
_this.initConfig(_this.runner, options)
|
|
287
291
|
];
|
|
288
|
-
case
|
|
292
|
+
case 4:
|
|
289
293
|
_state.sent();
|
|
290
294
|
return [
|
|
291
295
|
4,
|
|
292
296
|
_this.injectContext(_this.runner, options)
|
|
293
297
|
];
|
|
294
|
-
case
|
|
298
|
+
case 5:
|
|
295
299
|
_state.sent();
|
|
296
300
|
_this.server = _this.serverImpl(options);
|
|
297
301
|
return [
|
|
298
302
|
4,
|
|
299
303
|
_this.runPrepareHook(_this.runner)
|
|
300
304
|
];
|
|
301
|
-
case
|
|
305
|
+
case 6:
|
|
302
306
|
_state.sent();
|
|
303
307
|
return [
|
|
304
308
|
4,
|
|
305
309
|
_this.server.createHTTPServer(_this.getRequestHandler())
|
|
306
310
|
];
|
|
307
|
-
case
|
|
311
|
+
case 7:
|
|
308
312
|
_this.app = _state.sent();
|
|
309
313
|
return [
|
|
310
314
|
4,
|
|
311
315
|
_this.server.onInit(_this.runner, _this.app)
|
|
312
316
|
];
|
|
313
|
-
case
|
|
317
|
+
case 8:
|
|
314
318
|
_state.sent();
|
|
315
319
|
return [
|
|
316
320
|
2,
|
|
@@ -515,15 +519,64 @@ var Server = /*#__PURE__*/ function() {
|
|
|
515
519
|
{
|
|
516
520
|
key: "loadServerEnv",
|
|
517
521
|
value: function loadServerEnv(options) {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
522
|
+
return _asyncToGenerator(function() {
|
|
523
|
+
var appDirectory, serverEnv, defaultEnvPath, serverEnvPath, _i, _iter, envPath, _tmp, envConfig;
|
|
524
|
+
return __generator(this, function(_state) {
|
|
525
|
+
switch(_state.label){
|
|
526
|
+
case 0:
|
|
527
|
+
appDirectory = options.pwd;
|
|
528
|
+
serverEnv = process.env.MODERN_ENV;
|
|
529
|
+
defaultEnvPath = path.resolve(appDirectory, ".env");
|
|
530
|
+
serverEnvPath = path.resolve(appDirectory, ".env.".concat(serverEnv));
|
|
531
|
+
_i = 0, _iter = [
|
|
532
|
+
defaultEnvPath,
|
|
533
|
+
serverEnvPath
|
|
534
|
+
];
|
|
535
|
+
_state.label = 1;
|
|
536
|
+
case 1:
|
|
537
|
+
if (!(_i < _iter.length)) return [
|
|
538
|
+
3,
|
|
539
|
+
6
|
|
540
|
+
];
|
|
541
|
+
envPath = _iter[_i];
|
|
542
|
+
return [
|
|
543
|
+
4,
|
|
544
|
+
fs.pathExists(envPath)
|
|
545
|
+
];
|
|
546
|
+
case 2:
|
|
547
|
+
_tmp = _state.sent();
|
|
548
|
+
if (!_tmp) return [
|
|
549
|
+
3,
|
|
550
|
+
4
|
|
551
|
+
];
|
|
552
|
+
return [
|
|
553
|
+
4,
|
|
554
|
+
fs.stat(envPath)
|
|
555
|
+
];
|
|
556
|
+
case 3:
|
|
557
|
+
_tmp = !_state.sent().isDirectory();
|
|
558
|
+
_state.label = 4;
|
|
559
|
+
case 4:
|
|
560
|
+
if (_tmp) {
|
|
561
|
+
envConfig = dotenv.config({
|
|
562
|
+
path: envPath
|
|
563
|
+
});
|
|
564
|
+
dotenvExpand(envConfig);
|
|
565
|
+
}
|
|
566
|
+
_state.label = 5;
|
|
567
|
+
case 5:
|
|
568
|
+
_i++;
|
|
569
|
+
return [
|
|
570
|
+
3,
|
|
571
|
+
1
|
|
572
|
+
];
|
|
573
|
+
case 6:
|
|
574
|
+
return [
|
|
575
|
+
2
|
|
576
|
+
];
|
|
577
|
+
}
|
|
524
578
|
});
|
|
525
|
-
|
|
526
|
-
}
|
|
579
|
+
})();
|
|
527
580
|
}
|
|
528
581
|
}
|
|
529
582
|
]);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import path from "path";
|
|
2
|
-
import fs from "fs";
|
|
3
2
|
import {
|
|
3
|
+
fs,
|
|
4
4
|
Logger,
|
|
5
5
|
SHARED_DIR,
|
|
6
6
|
OUTPUT_CONFIG_FILE,
|
|
@@ -48,7 +48,7 @@ class Server {
|
|
|
48
48
|
*/
|
|
49
49
|
async init() {
|
|
50
50
|
const { options } = this;
|
|
51
|
-
this.loadServerEnv(options);
|
|
51
|
+
await this.loadServerEnv(options);
|
|
52
52
|
this.initServerConfig(options);
|
|
53
53
|
await this.injectContext(this.runner, options);
|
|
54
54
|
this.runner = await this.createHookRunner();
|
|
@@ -169,13 +169,16 @@ class Server {
|
|
|
169
169
|
plugins: serverPlugins
|
|
170
170
|
};
|
|
171
171
|
}
|
|
172
|
-
loadServerEnv(options) {
|
|
172
|
+
async loadServerEnv(options) {
|
|
173
173
|
const { pwd: appDirectory } = options;
|
|
174
174
|
const serverEnv = process.env.MODERN_ENV;
|
|
175
|
+
const defaultEnvPath = path.resolve(appDirectory, `.env`);
|
|
175
176
|
const serverEnvPath = path.resolve(appDirectory, `.env.${serverEnv}`);
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
177
|
+
for (const envPath of [defaultEnvPath, serverEnvPath]) {
|
|
178
|
+
if (await fs.pathExists(envPath) && !(await fs.stat(envPath)).isDirectory()) {
|
|
179
|
+
const envConfig = dotenv.config({ path: envPath });
|
|
180
|
+
dotenvExpand(envConfig);
|
|
181
|
+
}
|
|
179
182
|
}
|
|
180
183
|
}
|
|
181
184
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const AGGRED_DIR = {
|
|
2
|
+
mock: "config/mock",
|
|
3
|
+
server: "server",
|
|
4
|
+
api: "api",
|
|
5
|
+
shared: "shared",
|
|
6
|
+
lambda: "lambda"
|
|
7
|
+
};
|
|
8
|
+
var ApiServerMode = /* @__PURE__ */ ((ApiServerMode2) => {
|
|
9
|
+
ApiServerMode2["func"] = "function";
|
|
10
|
+
ApiServerMode2["frame"] = "framework";
|
|
11
|
+
return ApiServerMode2;
|
|
12
|
+
})(ApiServerMode || {});
|
|
13
|
+
const 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
|
+
const ERROR_PAGE_TEXT = {
|
|
22
|
+
404: "This page could not be found.",
|
|
23
|
+
500: "Internal Server Error."
|
|
24
|
+
};
|
|
25
|
+
const RUN_MODE = {
|
|
26
|
+
FULL: "full",
|
|
27
|
+
TYPE: "type"
|
|
28
|
+
};
|
|
29
|
+
export {
|
|
30
|
+
AGGRED_DIR,
|
|
31
|
+
ApiServerMode,
|
|
32
|
+
ERROR_DIGEST,
|
|
33
|
+
ERROR_PAGE_TEXT,
|
|
34
|
+
RUN_MODE
|
|
35
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
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 = (options) => {
|
|
7
|
+
if (options == null) {
|
|
8
|
+
throw new Error("can not start mserver without options");
|
|
9
|
+
}
|
|
10
|
+
const server = new Server(options);
|
|
11
|
+
return server.init();
|
|
12
|
+
};
|
|
13
|
+
export {
|
|
14
|
+
ModernServer,
|
|
15
|
+
Server,
|
|
16
|
+
createProxyHandler,
|
|
17
|
+
src_default as default
|
|
18
|
+
};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { URL } from "url";
|
|
2
|
+
import qs from "querystring";
|
|
3
|
+
import { Buffer } from "buffer";
|
|
4
|
+
import createEtag from "etag";
|
|
5
|
+
import fresh from "fresh";
|
|
6
|
+
import { headersWithoutCookie } from "../../utils";
|
|
7
|
+
class ModernServerContext {
|
|
8
|
+
constructor(req, res, options) {
|
|
9
|
+
this.params = {};
|
|
10
|
+
this.options = {};
|
|
11
|
+
this.req = req;
|
|
12
|
+
this.res = res;
|
|
13
|
+
this.options = options || {};
|
|
14
|
+
this.serverData = {};
|
|
15
|
+
this.bind();
|
|
16
|
+
}
|
|
17
|
+
get logger() {
|
|
18
|
+
return this.req.logger;
|
|
19
|
+
}
|
|
20
|
+
get metrics() {
|
|
21
|
+
return this.req.metrics;
|
|
22
|
+
}
|
|
23
|
+
bind() {
|
|
24
|
+
const { req, res } = this;
|
|
25
|
+
req.get = (key) => this.getReqHeader(key);
|
|
26
|
+
res.set = (key, value) => this.res.setHeader(key, value);
|
|
27
|
+
res.send = (body) => {
|
|
28
|
+
this.send(body);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
send(body) {
|
|
32
|
+
try {
|
|
33
|
+
const generateETag = !this.res.getHeader("ETag") && this.options.etag;
|
|
34
|
+
if (body !== void 0 && generateETag) {
|
|
35
|
+
const encoding = typeof body === "string" ? "utf-8" : void 0;
|
|
36
|
+
const buf = !Buffer.isBuffer(body) ? Buffer.from(body, encoding) : body;
|
|
37
|
+
const etag = createEtag(buf, { weak: true });
|
|
38
|
+
if (etag) {
|
|
39
|
+
this.res.setHeader("ETag", etag);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (this.fresh) {
|
|
43
|
+
this.status = 304;
|
|
44
|
+
}
|
|
45
|
+
} catch (e) {
|
|
46
|
+
this.logger.error(e.message);
|
|
47
|
+
}
|
|
48
|
+
this.res.end(body);
|
|
49
|
+
}
|
|
50
|
+
setParams(params) {
|
|
51
|
+
this.params = params;
|
|
52
|
+
}
|
|
53
|
+
setServerData(key, value) {
|
|
54
|
+
this.serverData[key] = value;
|
|
55
|
+
}
|
|
56
|
+
getReqHeader(key) {
|
|
57
|
+
const { req } = this;
|
|
58
|
+
const field = key.toLowerCase();
|
|
59
|
+
switch (field) {
|
|
60
|
+
case "referer":
|
|
61
|
+
case "referrer":
|
|
62
|
+
return req.headers.referrer || req.headers.referer || "";
|
|
63
|
+
default:
|
|
64
|
+
return req.headers[field] || "";
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
get fresh() {
|
|
68
|
+
const { status, res, method } = this;
|
|
69
|
+
if ("GET" !== method && "HEAD" !== method) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
if (status >= 200 && status < 300 || 304 === status) {
|
|
73
|
+
return fresh(this.headers, {
|
|
74
|
+
etag: res.getHeader("ETag"),
|
|
75
|
+
"last-modified": res.getHeader("Last-Modified")
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
get headers() {
|
|
81
|
+
return this.req.headers;
|
|
82
|
+
}
|
|
83
|
+
get method() {
|
|
84
|
+
return this.req.method;
|
|
85
|
+
}
|
|
86
|
+
get url() {
|
|
87
|
+
return this.req.url || "";
|
|
88
|
+
}
|
|
89
|
+
set url(val) {
|
|
90
|
+
this.req.url = val;
|
|
91
|
+
}
|
|
92
|
+
get host() {
|
|
93
|
+
let host = this.getReqHeader("X-Forwarded-Host");
|
|
94
|
+
if (!host) {
|
|
95
|
+
host = this.getReqHeader("Host");
|
|
96
|
+
}
|
|
97
|
+
return host.split(/\s*,\s*/, 1)[0] || "";
|
|
98
|
+
}
|
|
99
|
+
get protocol() {
|
|
100
|
+
if (this.req.socket.encrypted) {
|
|
101
|
+
return "https";
|
|
102
|
+
}
|
|
103
|
+
const proto = this.getReqHeader("X-Forwarded-Proto");
|
|
104
|
+
return proto ? proto.split(/\s*,\s*/, 1)[0] : "http";
|
|
105
|
+
}
|
|
106
|
+
get origin() {
|
|
107
|
+
return `${this.protocol}://${this.host}`;
|
|
108
|
+
}
|
|
109
|
+
get href() {
|
|
110
|
+
return this.origin + this.url;
|
|
111
|
+
}
|
|
112
|
+
get parsedURL() {
|
|
113
|
+
const url = new URL(this.req.url, this.origin);
|
|
114
|
+
return url;
|
|
115
|
+
}
|
|
116
|
+
get path() {
|
|
117
|
+
return this.parsedURL.pathname;
|
|
118
|
+
}
|
|
119
|
+
set path(p) {
|
|
120
|
+
const url = new URL(this.req.url, this.origin);
|
|
121
|
+
if (!url || !p) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (url.pathname === p) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
url.pathname = p;
|
|
128
|
+
this.url = url.toString();
|
|
129
|
+
}
|
|
130
|
+
get querystring() {
|
|
131
|
+
if (!this.req) {
|
|
132
|
+
return "";
|
|
133
|
+
}
|
|
134
|
+
return this.parsedURL.search.replace(/^\?/, "") || "";
|
|
135
|
+
}
|
|
136
|
+
get query() {
|
|
137
|
+
const str = this.querystring;
|
|
138
|
+
return qs.parse(str);
|
|
139
|
+
}
|
|
140
|
+
get status() {
|
|
141
|
+
return this.res.statusCode;
|
|
142
|
+
}
|
|
143
|
+
set status(statusCode) {
|
|
144
|
+
this.res.statusCode = statusCode;
|
|
145
|
+
}
|
|
146
|
+
resHasHandled() {
|
|
147
|
+
return this.res.writableEnded;
|
|
148
|
+
}
|
|
149
|
+
error(dig, e = "") {
|
|
150
|
+
this.logger.error(
|
|
151
|
+
`Web Server Error - ${dig}, error = %s, req.url = %s, req.headers = %o`,
|
|
152
|
+
e instanceof Error ? e.stack || e.message : e,
|
|
153
|
+
this.path,
|
|
154
|
+
headersWithoutCookie(this.headers)
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
export {
|
|
159
|
+
ModernServerContext
|
|
160
|
+
};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
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 __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
import cookie from "cookie";
|
|
21
|
+
import { RouteAPI } from "./route";
|
|
22
|
+
import { TemplateAPI } from "./template";
|
|
23
|
+
class Response {
|
|
24
|
+
constructor(res) {
|
|
25
|
+
this.res = res;
|
|
26
|
+
this._cookie = cookie.parse(res.getHeader("set-cookie") || "");
|
|
27
|
+
this.cookies = {
|
|
28
|
+
get: this.getCookie.bind(this),
|
|
29
|
+
set: this.setCookie.bind(this),
|
|
30
|
+
delete: this.deleteCookie.bind(this),
|
|
31
|
+
clear: this.clearCookie.bind(this),
|
|
32
|
+
apply: this.applyCookie.bind(this)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
get(key) {
|
|
36
|
+
return this.res.getHeader(key);
|
|
37
|
+
}
|
|
38
|
+
set(key, value) {
|
|
39
|
+
return this.res.setHeader(key, value);
|
|
40
|
+
}
|
|
41
|
+
status(code) {
|
|
42
|
+
this.res.statusCode = code;
|
|
43
|
+
}
|
|
44
|
+
getCookie(key) {
|
|
45
|
+
return this._cookie[key];
|
|
46
|
+
}
|
|
47
|
+
setCookie(key, value) {
|
|
48
|
+
this._cookie[key] = value;
|
|
49
|
+
}
|
|
50
|
+
deleteCookie(key) {
|
|
51
|
+
if (this._cookie[key]) {
|
|
52
|
+
delete this._cookie[key];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
clearCookie() {
|
|
56
|
+
this._cookie = {};
|
|
57
|
+
}
|
|
58
|
+
applyCookie() {
|
|
59
|
+
const str = Object.entries(this._cookie).map(([key, value]) => {
|
|
60
|
+
return cookie.serialize(key, value);
|
|
61
|
+
}).join("; ");
|
|
62
|
+
if (str) {
|
|
63
|
+
this.res.setHeader("set-cookie", str);
|
|
64
|
+
} else {
|
|
65
|
+
this.res.removeHeader("set-cookie");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
raw(body, options) {
|
|
69
|
+
const { status, headers = {} } = options || {};
|
|
70
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
71
|
+
this.res.setHeader(key, value);
|
|
72
|
+
});
|
|
73
|
+
if (status) {
|
|
74
|
+
this.res.statusCode = status;
|
|
75
|
+
}
|
|
76
|
+
this.res.end(body);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
class Request {
|
|
80
|
+
constructor(ctx) {
|
|
81
|
+
this.url = ctx.url;
|
|
82
|
+
this.host = ctx.host;
|
|
83
|
+
this.pathname = ctx.path;
|
|
84
|
+
this.query = ctx.query;
|
|
85
|
+
this.headers = ctx.headers;
|
|
86
|
+
this.cookie = ctx.headers.cookie || "";
|
|
87
|
+
this._cookie = cookie.parse(this.cookie);
|
|
88
|
+
this.cookies = {
|
|
89
|
+
get: this.getCookie.bind(this)
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
getCookie(key) {
|
|
93
|
+
return this._cookie[key];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const base = (context) => {
|
|
97
|
+
const { res } = context;
|
|
98
|
+
return {
|
|
99
|
+
response: new Response(res),
|
|
100
|
+
request: new Request(context),
|
|
101
|
+
logger: context.logger,
|
|
102
|
+
metrics: context.metrics
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
const createAfterMatchContext = (context, entryName) => {
|
|
106
|
+
const baseContext = base(context);
|
|
107
|
+
return __spreadProps(__spreadValues({}, baseContext), {
|
|
108
|
+
router: new RouteAPI(entryName)
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
const createAfterRenderContext = (context, content) => {
|
|
112
|
+
const baseContext = base(context);
|
|
113
|
+
return __spreadProps(__spreadValues({}, baseContext), {
|
|
114
|
+
template: new TemplateAPI(content)
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
const createMiddlewareContext = (context) => {
|
|
118
|
+
const baseContext = base(context);
|
|
119
|
+
return __spreadProps(__spreadValues({}, baseContext), {
|
|
120
|
+
response: __spreadProps(__spreadValues({}, baseContext.response), {
|
|
121
|
+
locals: context.res.locals || {}
|
|
122
|
+
}),
|
|
123
|
+
source: {
|
|
124
|
+
req: context.req,
|
|
125
|
+
res: context.res
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
export {
|
|
130
|
+
base,
|
|
131
|
+
createAfterMatchContext,
|
|
132
|
+
createAfterRenderContext,
|
|
133
|
+
createMiddlewareContext
|
|
134
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
class RouteAPI {
|
|
2
|
+
constructor(entryName) {
|
|
3
|
+
this.current = entryName;
|
|
4
|
+
this.status = 200;
|
|
5
|
+
this.url = "";
|
|
6
|
+
}
|
|
7
|
+
redirect(url, status = 302) {
|
|
8
|
+
this.url = url;
|
|
9
|
+
this.status = status;
|
|
10
|
+
}
|
|
11
|
+
rewrite(entryName) {
|
|
12
|
+
this.current = entryName;
|
|
13
|
+
}
|
|
14
|
+
use(entryName) {
|
|
15
|
+
this.rewrite(entryName);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export {
|
|
19
|
+
RouteAPI
|
|
20
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Transform } from "stream";
|
|
2
|
+
const RegList = {
|
|
3
|
+
before: {
|
|
4
|
+
head: "<head>",
|
|
5
|
+
body: "<body>"
|
|
6
|
+
},
|
|
7
|
+
after: {
|
|
8
|
+
head: "</head>",
|
|
9
|
+
body: "</body>"
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
class TemplateAPI {
|
|
13
|
+
constructor(content) {
|
|
14
|
+
this.content = content;
|
|
15
|
+
}
|
|
16
|
+
get() {
|
|
17
|
+
return this.content;
|
|
18
|
+
}
|
|
19
|
+
set(content) {
|
|
20
|
+
this.content = content;
|
|
21
|
+
}
|
|
22
|
+
prependHead(fragment) {
|
|
23
|
+
const { head } = RegList.before;
|
|
24
|
+
return this.replace(head, `${head}${fragment}`);
|
|
25
|
+
}
|
|
26
|
+
appendHead(fragment) {
|
|
27
|
+
const { head } = RegList.after;
|
|
28
|
+
return this.replace(head, `${fragment}${head}`);
|
|
29
|
+
}
|
|
30
|
+
prependBody(fragment) {
|
|
31
|
+
const { body } = RegList.before;
|
|
32
|
+
return this.replace(body, `${body}${fragment}`);
|
|
33
|
+
}
|
|
34
|
+
appendBody(fragment) {
|
|
35
|
+
const { body } = RegList.after;
|
|
36
|
+
return this.replace(body, `${fragment}${body}`);
|
|
37
|
+
}
|
|
38
|
+
replace(reg, text) {
|
|
39
|
+
this.content = this.content.replace(reg, text);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const templateInjectableStream = ({
|
|
43
|
+
prependHead,
|
|
44
|
+
appendHead,
|
|
45
|
+
prependBody,
|
|
46
|
+
appendBody
|
|
47
|
+
}) => new Transform({
|
|
48
|
+
write(chunk, _, callback) {
|
|
49
|
+
let chunk_str = chunk.toString();
|
|
50
|
+
if (prependHead) {
|
|
51
|
+
const { head } = RegList.before;
|
|
52
|
+
chunk_str = chunk_str.replace(head, `${head}${prependHead}`);
|
|
53
|
+
}
|
|
54
|
+
if (appendHead) {
|
|
55
|
+
const { head } = RegList.after;
|
|
56
|
+
chunk_str = chunk_str.replace(head, `${appendHead}${head}`);
|
|
57
|
+
}
|
|
58
|
+
if (prependBody) {
|
|
59
|
+
const { body } = RegList.before;
|
|
60
|
+
chunk_str = chunk_str.replace(body, `${body}${prependBody}`);
|
|
61
|
+
}
|
|
62
|
+
if (appendBody) {
|
|
63
|
+
const { body } = RegList.after;
|
|
64
|
+
chunk_str = chunk_str.replace(body, `${appendBody}${body}`);
|
|
65
|
+
}
|
|
66
|
+
this.push(chunk_str);
|
|
67
|
+
callback();
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
export {
|
|
71
|
+
TemplateAPI,
|
|
72
|
+
templateInjectableStream
|
|
73
|
+
};
|