@modern-js/prod-server 2.4.0 → 2.4.1-beta.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 +12 -0
- package/dist/js/modern/libs/logger.js +111 -0
- package/dist/js/modern/worker-server.js +47 -12
- package/dist/js/node/libs/logger.js +133 -0
- package/dist/js/node/worker-server.js +49 -13
- package/dist/js/treeshaking/libs/context/context.js +2 -2
- package/dist/js/treeshaking/libs/hook-api/index.js +1 -1
- package/dist/js/treeshaking/libs/logger.js +205 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +3 -3
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +2 -2
- package/dist/js/treeshaking/libs/render/cache/spr.js +8 -8
- package/dist/js/treeshaking/libs/render/measure.js +2 -2
- package/dist/js/treeshaking/libs/render/ssr.js +3 -3
- package/dist/js/treeshaking/libs/route/matcher.js +2 -2
- package/dist/js/treeshaking/libs/serve-file.js +2 -2
- package/dist/js/treeshaking/server/index.js +3 -3
- package/dist/js/treeshaking/server/modern-server.js +8 -8
- package/dist/js/treeshaking/utils.js +1 -1
- package/dist/js/treeshaking/worker-server.js +64 -8
- package/dist/types/libs/context/context.d.ts +1 -1
- package/dist/types/libs/logger.d.ts +63 -0
- package/dist/types/utils.d.ts +1 -1
- package/dist/types/worker-server.d.ts +2 -1
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @modern-js/prod-server
|
|
2
2
|
|
|
3
|
+
## 2.4.1-beta.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 11c053b: feat: ssr support deploy worker
|
|
8
|
+
|
|
9
|
+
feat: ssr 支持边缘部署
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [11c053b]
|
|
12
|
+
- @modern-js/server-core@2.4.1-beta.0
|
|
13
|
+
- @modern-js/utils@2.4.1-beta.0
|
|
14
|
+
|
|
3
15
|
## 2.4.0
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
const LOG_LEVEL = {
|
|
18
|
+
error: 0,
|
|
19
|
+
warn: 1,
|
|
20
|
+
info: 2,
|
|
21
|
+
debug: 3,
|
|
22
|
+
log: 4
|
|
23
|
+
};
|
|
24
|
+
const LOG_TYPES = {
|
|
25
|
+
error: {
|
|
26
|
+
color: "red",
|
|
27
|
+
label: "error",
|
|
28
|
+
level: "error"
|
|
29
|
+
},
|
|
30
|
+
info: {
|
|
31
|
+
color: "cyan",
|
|
32
|
+
label: "info",
|
|
33
|
+
level: "info"
|
|
34
|
+
},
|
|
35
|
+
success: {
|
|
36
|
+
color: "green",
|
|
37
|
+
label: "Success",
|
|
38
|
+
level: "info"
|
|
39
|
+
},
|
|
40
|
+
warn: {
|
|
41
|
+
color: "yellow",
|
|
42
|
+
label: "warn",
|
|
43
|
+
level: "warn"
|
|
44
|
+
},
|
|
45
|
+
debug: {
|
|
46
|
+
color: "red",
|
|
47
|
+
label: "debug",
|
|
48
|
+
level: "debug"
|
|
49
|
+
},
|
|
50
|
+
log: { level: "log" }
|
|
51
|
+
};
|
|
52
|
+
const DEFAULT_CONFIG = {
|
|
53
|
+
displayLabel: true,
|
|
54
|
+
uppercaseLabel: false
|
|
55
|
+
};
|
|
56
|
+
class Logger {
|
|
57
|
+
constructor(options = {}) {
|
|
58
|
+
this.level = options.level || LOG_TYPES.log.level;
|
|
59
|
+
this.config = __spreadValues(__spreadValues({}, DEFAULT_CONFIG), options.config || {});
|
|
60
|
+
this.types = __spreadValues(__spreadValues({}, LOG_TYPES), options.types || {});
|
|
61
|
+
this.longestLabel = this.getLongestLabel();
|
|
62
|
+
Object.keys(this.types).forEach((type) => {
|
|
63
|
+
this[type] = this._log.bind(this, type);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
_log(type, message, ...args) {
|
|
67
|
+
if (message === void 0 || message === null) {
|
|
68
|
+
console.log();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (LOG_LEVEL[type] > LOG_LEVEL[this.level]) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
let label = "";
|
|
75
|
+
let text = "";
|
|
76
|
+
const logType = this.types[type];
|
|
77
|
+
if (this.config.displayLabel && logType.label) {
|
|
78
|
+
label = this.config.uppercaseLabel ? logType.label.toUpperCase() : logType.label;
|
|
79
|
+
label = label.padEnd(this.longestLabel.length);
|
|
80
|
+
}
|
|
81
|
+
if (message instanceof Error) {
|
|
82
|
+
if (message.stack) {
|
|
83
|
+
const [name, ...rest] = message.stack.split("\n");
|
|
84
|
+
text = `${name}
|
|
85
|
+
${rest.join("\n")}`;
|
|
86
|
+
} else {
|
|
87
|
+
text = message.message;
|
|
88
|
+
}
|
|
89
|
+
} else {
|
|
90
|
+
text = `${message}`;
|
|
91
|
+
}
|
|
92
|
+
const log = label.length > 0 ? `${label} ${text}` : text;
|
|
93
|
+
console.log(log, ...args);
|
|
94
|
+
}
|
|
95
|
+
getLongestLabel() {
|
|
96
|
+
let longestLabel = "";
|
|
97
|
+
Object.keys(this.types).forEach((type) => {
|
|
98
|
+
const { label = "" } = this.types[type];
|
|
99
|
+
if (label.length > longestLabel.length) {
|
|
100
|
+
longestLabel = label;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
return longestLabel;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const logger = new Logger();
|
|
107
|
+
logger.Logger = Logger;
|
|
108
|
+
export {
|
|
109
|
+
Logger,
|
|
110
|
+
logger
|
|
111
|
+
};
|
|
@@ -18,7 +18,12 @@ var __async = (__this, __arguments, generator) => {
|
|
|
18
18
|
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
19
|
});
|
|
20
20
|
};
|
|
21
|
+
import { Logger } from "./libs/logger";
|
|
21
22
|
import { RouteMatchManager } from "./libs/route";
|
|
23
|
+
import { metrics as defaultMetrics } from "./libs/metrics";
|
|
24
|
+
const handleUrl = (url) => {
|
|
25
|
+
return url.replace(/^https?:\/\/.*?\//gi, "/");
|
|
26
|
+
};
|
|
22
27
|
const createHandler = (manifest) => {
|
|
23
28
|
const routeMgr = new RouteMatchManager();
|
|
24
29
|
const { pages, routes } = manifest;
|
|
@@ -36,19 +41,49 @@ const createHandler = (manifest) => {
|
|
|
36
41
|
(_d = (_c = ctx.request).pathname) != null ? _d : _c.pathname = ctx.pathname;
|
|
37
42
|
(_f = (_e = ctx.request).params) != null ? _f : _e.params = ctx.params;
|
|
38
43
|
const params = pageMatch.parseURLParams(ctx.url);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
44
|
+
if (page.serverRender) {
|
|
45
|
+
try {
|
|
46
|
+
ctx.body = yield page.serverRender({
|
|
47
|
+
entryName: page.entryName,
|
|
48
|
+
template: page.template,
|
|
49
|
+
query: ctx.query,
|
|
50
|
+
request: ctx.request,
|
|
51
|
+
response: ctx.response,
|
|
52
|
+
pathname: ctx.pathname,
|
|
53
|
+
req: ctx.request,
|
|
54
|
+
res: ctx.response,
|
|
55
|
+
params: ctx.params || params || {},
|
|
56
|
+
logger: ctx.logger || new Logger({
|
|
57
|
+
level: "warn"
|
|
58
|
+
}),
|
|
59
|
+
metrics: ctx.metrics || defaultMetrics,
|
|
60
|
+
loadableStats: ctx.loadableStats,
|
|
61
|
+
routeManifest: ctx.routeManifest
|
|
62
|
+
});
|
|
63
|
+
ctx.status = 200;
|
|
64
|
+
return;
|
|
65
|
+
} catch (e) {
|
|
66
|
+
if (page.template) {
|
|
67
|
+
ctx.body = page.template;
|
|
68
|
+
ctx.status = 200;
|
|
69
|
+
return;
|
|
70
|
+
} else {
|
|
71
|
+
ctx.body = "404: not found";
|
|
72
|
+
ctx.status = 404;
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (page.template) {
|
|
78
|
+
ctx.body = page.template;
|
|
79
|
+
ctx.status = 200;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
ctx.body = "404: not found";
|
|
83
|
+
ctx.status = 404;
|
|
50
84
|
});
|
|
51
85
|
};
|
|
52
86
|
export {
|
|
53
|
-
createHandler
|
|
87
|
+
createHandler,
|
|
88
|
+
handleUrl
|
|
54
89
|
};
|
|
@@ -0,0 +1,133 @@
|
|
|
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 logger_exports = {};
|
|
33
|
+
__export(logger_exports, {
|
|
34
|
+
Logger: () => Logger,
|
|
35
|
+
logger: () => logger
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(logger_exports);
|
|
38
|
+
const LOG_LEVEL = {
|
|
39
|
+
error: 0,
|
|
40
|
+
warn: 1,
|
|
41
|
+
info: 2,
|
|
42
|
+
debug: 3,
|
|
43
|
+
log: 4
|
|
44
|
+
};
|
|
45
|
+
const LOG_TYPES = {
|
|
46
|
+
error: {
|
|
47
|
+
color: "red",
|
|
48
|
+
label: "error",
|
|
49
|
+
level: "error"
|
|
50
|
+
},
|
|
51
|
+
info: {
|
|
52
|
+
color: "cyan",
|
|
53
|
+
label: "info",
|
|
54
|
+
level: "info"
|
|
55
|
+
},
|
|
56
|
+
success: {
|
|
57
|
+
color: "green",
|
|
58
|
+
label: "Success",
|
|
59
|
+
level: "info"
|
|
60
|
+
},
|
|
61
|
+
warn: {
|
|
62
|
+
color: "yellow",
|
|
63
|
+
label: "warn",
|
|
64
|
+
level: "warn"
|
|
65
|
+
},
|
|
66
|
+
debug: {
|
|
67
|
+
color: "red",
|
|
68
|
+
label: "debug",
|
|
69
|
+
level: "debug"
|
|
70
|
+
},
|
|
71
|
+
log: { level: "log" }
|
|
72
|
+
};
|
|
73
|
+
const DEFAULT_CONFIG = {
|
|
74
|
+
displayLabel: true,
|
|
75
|
+
uppercaseLabel: false
|
|
76
|
+
};
|
|
77
|
+
class Logger {
|
|
78
|
+
constructor(options = {}) {
|
|
79
|
+
this.level = options.level || LOG_TYPES.log.level;
|
|
80
|
+
this.config = __spreadValues(__spreadValues({}, DEFAULT_CONFIG), options.config || {});
|
|
81
|
+
this.types = __spreadValues(__spreadValues({}, LOG_TYPES), options.types || {});
|
|
82
|
+
this.longestLabel = this.getLongestLabel();
|
|
83
|
+
Object.keys(this.types).forEach((type) => {
|
|
84
|
+
this[type] = this._log.bind(this, type);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
_log(type, message, ...args) {
|
|
88
|
+
if (message === void 0 || message === null) {
|
|
89
|
+
console.log();
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (LOG_LEVEL[type] > LOG_LEVEL[this.level]) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
let label = "";
|
|
96
|
+
let text = "";
|
|
97
|
+
const logType = this.types[type];
|
|
98
|
+
if (this.config.displayLabel && logType.label) {
|
|
99
|
+
label = this.config.uppercaseLabel ? logType.label.toUpperCase() : logType.label;
|
|
100
|
+
label = label.padEnd(this.longestLabel.length);
|
|
101
|
+
}
|
|
102
|
+
if (message instanceof Error) {
|
|
103
|
+
if (message.stack) {
|
|
104
|
+
const [name, ...rest] = message.stack.split("\n");
|
|
105
|
+
text = `${name}
|
|
106
|
+
${rest.join("\n")}`;
|
|
107
|
+
} else {
|
|
108
|
+
text = message.message;
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
text = `${message}`;
|
|
112
|
+
}
|
|
113
|
+
const log = label.length > 0 ? `${label} ${text}` : text;
|
|
114
|
+
console.log(log, ...args);
|
|
115
|
+
}
|
|
116
|
+
getLongestLabel() {
|
|
117
|
+
let longestLabel = "";
|
|
118
|
+
Object.keys(this.types).forEach((type) => {
|
|
119
|
+
const { label = "" } = this.types[type];
|
|
120
|
+
if (label.length > longestLabel.length) {
|
|
121
|
+
longestLabel = label;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
return longestLabel;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const logger = new Logger();
|
|
128
|
+
logger.Logger = Logger;
|
|
129
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
130
|
+
0 && (module.exports = {
|
|
131
|
+
Logger,
|
|
132
|
+
logger
|
|
133
|
+
});
|
|
@@ -37,10 +37,16 @@ var __async = (__this, __arguments, generator) => {
|
|
|
37
37
|
};
|
|
38
38
|
var worker_server_exports = {};
|
|
39
39
|
__export(worker_server_exports, {
|
|
40
|
-
createHandler: () => createHandler
|
|
40
|
+
createHandler: () => createHandler,
|
|
41
|
+
handleUrl: () => handleUrl
|
|
41
42
|
});
|
|
42
43
|
module.exports = __toCommonJS(worker_server_exports);
|
|
44
|
+
var import_logger = require("./libs/logger");
|
|
43
45
|
var import_route = require("./libs/route");
|
|
46
|
+
var import_metrics = require("./libs/metrics");
|
|
47
|
+
const handleUrl = (url) => {
|
|
48
|
+
return url.replace(/^https?:\/\/.*?\//gi, "/");
|
|
49
|
+
};
|
|
44
50
|
const createHandler = (manifest) => {
|
|
45
51
|
const routeMgr = new import_route.RouteMatchManager();
|
|
46
52
|
const { pages, routes } = manifest;
|
|
@@ -58,20 +64,50 @@ const createHandler = (manifest) => {
|
|
|
58
64
|
(_d = (_c = ctx.request).pathname) != null ? _d : _c.pathname = ctx.pathname;
|
|
59
65
|
(_f = (_e = ctx.request).params) != null ? _f : _e.params = ctx.params;
|
|
60
66
|
const params = pageMatch.parseURLParams(ctx.url);
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
67
|
+
if (page.serverRender) {
|
|
68
|
+
try {
|
|
69
|
+
ctx.body = yield page.serverRender({
|
|
70
|
+
entryName: page.entryName,
|
|
71
|
+
template: page.template,
|
|
72
|
+
query: ctx.query,
|
|
73
|
+
request: ctx.request,
|
|
74
|
+
response: ctx.response,
|
|
75
|
+
pathname: ctx.pathname,
|
|
76
|
+
req: ctx.request,
|
|
77
|
+
res: ctx.response,
|
|
78
|
+
params: ctx.params || params || {},
|
|
79
|
+
logger: ctx.logger || new import_logger.Logger({
|
|
80
|
+
level: "warn"
|
|
81
|
+
}),
|
|
82
|
+
metrics: ctx.metrics || import_metrics.metrics,
|
|
83
|
+
loadableStats: ctx.loadableStats,
|
|
84
|
+
routeManifest: ctx.routeManifest
|
|
85
|
+
});
|
|
86
|
+
ctx.status = 200;
|
|
87
|
+
return;
|
|
88
|
+
} catch (e) {
|
|
89
|
+
if (page.template) {
|
|
90
|
+
ctx.body = page.template;
|
|
91
|
+
ctx.status = 200;
|
|
92
|
+
return;
|
|
93
|
+
} else {
|
|
94
|
+
ctx.body = "404: not found";
|
|
95
|
+
ctx.status = 404;
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (page.template) {
|
|
101
|
+
ctx.body = page.template;
|
|
102
|
+
ctx.status = 200;
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
ctx.body = "404: not found";
|
|
106
|
+
ctx.status = 404;
|
|
72
107
|
});
|
|
73
108
|
};
|
|
74
109
|
// Annotate the CommonJS export names for ESM import in node:
|
|
75
110
|
0 && (module.exports = {
|
|
76
|
-
createHandler
|
|
111
|
+
createHandler,
|
|
112
|
+
handleUrl
|
|
77
113
|
});
|
|
@@ -75,7 +75,7 @@ var ModernServerContext = /*#__PURE__*/ function() {
|
|
|
75
75
|
key: "bind",
|
|
76
76
|
value: function bind() {
|
|
77
77
|
var _this = this;
|
|
78
|
-
var
|
|
78
|
+
var _this1 = this, req = _this1.req, res = _this1.res;
|
|
79
79
|
req.get = function(key) {
|
|
80
80
|
return _this.getReqHeader(key);
|
|
81
81
|
};
|
|
@@ -140,7 +140,7 @@ var ModernServerContext = /*#__PURE__*/ function() {
|
|
|
140
140
|
{
|
|
141
141
|
key: "fresh",
|
|
142
142
|
get: function get() {
|
|
143
|
-
var
|
|
143
|
+
var _this = this, status = _this.status, res = _this.res, method = _this.method;
|
|
144
144
|
if ("GET" !== method && "HEAD" !== method) {
|
|
145
145
|
return false;
|
|
146
146
|
}
|
|
@@ -198,7 +198,7 @@ var Response = /*#__PURE__*/ function() {
|
|
|
198
198
|
key: "raw",
|
|
199
199
|
value: function raw(body, options) {
|
|
200
200
|
var _this = this;
|
|
201
|
-
var
|
|
201
|
+
var _ref = options || {}, status = _ref.status, _ref_headers = _ref.headers, headers = _ref_headers === void 0 ? {} : _ref_headers;
|
|
202
202
|
Object.entries(headers).forEach(function(param) {
|
|
203
203
|
var _param = _slicedToArray(param, 2), key = _param[0], value = _param[1];
|
|
204
204
|
_this.res.setHeader(key, value);
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
function _arrayLikeToArray(arr, len) {
|
|
2
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
3
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
4
|
+
return arr2;
|
|
5
|
+
}
|
|
6
|
+
function _arrayWithHoles(arr) {
|
|
7
|
+
if (Array.isArray(arr)) return arr;
|
|
8
|
+
}
|
|
9
|
+
function _arrayWithoutHoles(arr) {
|
|
10
|
+
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
|
|
11
|
+
}
|
|
12
|
+
function _classCallCheck(instance, Constructor) {
|
|
13
|
+
if (!(instance instanceof Constructor)) {
|
|
14
|
+
throw new TypeError("Cannot call a class as a function");
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function _defineProperties(target, props) {
|
|
18
|
+
for(var i = 0; i < props.length; i++){
|
|
19
|
+
var descriptor = props[i];
|
|
20
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
21
|
+
descriptor.configurable = true;
|
|
22
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
23
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function _createClass(Constructor, protoProps, staticProps) {
|
|
27
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
28
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
29
|
+
return Constructor;
|
|
30
|
+
}
|
|
31
|
+
function _defineProperty(obj, key, value) {
|
|
32
|
+
if (key in obj) {
|
|
33
|
+
Object.defineProperty(obj, key, {
|
|
34
|
+
value: value,
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
writable: true
|
|
38
|
+
});
|
|
39
|
+
} else {
|
|
40
|
+
obj[key] = value;
|
|
41
|
+
}
|
|
42
|
+
return obj;
|
|
43
|
+
}
|
|
44
|
+
function _instanceof(left, right) {
|
|
45
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
46
|
+
return !!right[Symbol.hasInstance](left);
|
|
47
|
+
} else {
|
|
48
|
+
return left instanceof right;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function _iterableToArray(iter) {
|
|
52
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
53
|
+
}
|
|
54
|
+
function _nonIterableRest() {
|
|
55
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
56
|
+
}
|
|
57
|
+
function _nonIterableSpread() {
|
|
58
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
59
|
+
}
|
|
60
|
+
function _objectSpread(target) {
|
|
61
|
+
for(var i = 1; i < arguments.length; i++){
|
|
62
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
63
|
+
var ownKeys = Object.keys(source);
|
|
64
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
65
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
66
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
67
|
+
}));
|
|
68
|
+
}
|
|
69
|
+
ownKeys.forEach(function(key) {
|
|
70
|
+
_defineProperty(target, key, source[key]);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return target;
|
|
74
|
+
}
|
|
75
|
+
function _toArray(arr) {
|
|
76
|
+
return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest();
|
|
77
|
+
}
|
|
78
|
+
function _toConsumableArray(arr) {
|
|
79
|
+
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
|
|
80
|
+
}
|
|
81
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
82
|
+
if (!o) return;
|
|
83
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
84
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
85
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
86
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
87
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
88
|
+
}
|
|
89
|
+
var LOG_LEVEL = {
|
|
90
|
+
error: 0,
|
|
91
|
+
warn: 1,
|
|
92
|
+
info: 2,
|
|
93
|
+
debug: 3,
|
|
94
|
+
log: 4
|
|
95
|
+
};
|
|
96
|
+
var LOG_TYPES = {
|
|
97
|
+
error: {
|
|
98
|
+
color: "red",
|
|
99
|
+
label: "error",
|
|
100
|
+
level: "error"
|
|
101
|
+
},
|
|
102
|
+
info: {
|
|
103
|
+
color: "cyan",
|
|
104
|
+
label: "info",
|
|
105
|
+
level: "info"
|
|
106
|
+
},
|
|
107
|
+
success: {
|
|
108
|
+
color: "green",
|
|
109
|
+
label: "Success",
|
|
110
|
+
level: "info"
|
|
111
|
+
},
|
|
112
|
+
warn: {
|
|
113
|
+
color: "yellow",
|
|
114
|
+
label: "warn",
|
|
115
|
+
level: "warn"
|
|
116
|
+
},
|
|
117
|
+
debug: {
|
|
118
|
+
color: "red",
|
|
119
|
+
label: "debug",
|
|
120
|
+
level: "debug"
|
|
121
|
+
},
|
|
122
|
+
log: {
|
|
123
|
+
level: "log"
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
var DEFAULT_CONFIG = {
|
|
127
|
+
displayLabel: true,
|
|
128
|
+
uppercaseLabel: false
|
|
129
|
+
};
|
|
130
|
+
var Logger = /*#__PURE__*/ function() {
|
|
131
|
+
"use strict";
|
|
132
|
+
function Logger() {
|
|
133
|
+
var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
134
|
+
var _this = this;
|
|
135
|
+
_classCallCheck(this, Logger);
|
|
136
|
+
_defineProperty(this, "level", void 0);
|
|
137
|
+
_defineProperty(this, "config", void 0);
|
|
138
|
+
_defineProperty(this, "types", void 0);
|
|
139
|
+
_defineProperty(this, "longestLabel", void 0);
|
|
140
|
+
this.level = options.level || LOG_TYPES.log.level;
|
|
141
|
+
this.config = _objectSpread({}, DEFAULT_CONFIG, options.config || {});
|
|
142
|
+
this.types = _objectSpread({}, LOG_TYPES, options.types || {});
|
|
143
|
+
this.longestLabel = this.getLongestLabel();
|
|
144
|
+
Object.keys(this.types).forEach(function(type) {
|
|
145
|
+
_this[type] = _this._log.bind(_this, type);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
_createClass(Logger, [
|
|
149
|
+
{
|
|
150
|
+
key: "_log",
|
|
151
|
+
value: function _log(type, message) {
|
|
152
|
+
for(var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++){
|
|
153
|
+
args[_key - 2] = arguments[_key];
|
|
154
|
+
}
|
|
155
|
+
var _console;
|
|
156
|
+
if (message === void 0 || message === null) {
|
|
157
|
+
console.log();
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (LOG_LEVEL[type] > LOG_LEVEL[this.level]) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
var label = "";
|
|
164
|
+
var text = "";
|
|
165
|
+
var logType = this.types[type];
|
|
166
|
+
if (this.config.displayLabel && logType.label) {
|
|
167
|
+
label = this.config.uppercaseLabel ? logType.label.toUpperCase() : logType.label;
|
|
168
|
+
label = label.padEnd(this.longestLabel.length);
|
|
169
|
+
}
|
|
170
|
+
if (_instanceof(message, Error)) {
|
|
171
|
+
if (message.stack) {
|
|
172
|
+
var _message_stack_split = _toArray(message.stack.split("\n")), name = _message_stack_split[0], rest = _message_stack_split.slice(1);
|
|
173
|
+
text = "".concat(name, "\n").concat(rest.join("\n"));
|
|
174
|
+
} else {
|
|
175
|
+
text = message.message;
|
|
176
|
+
}
|
|
177
|
+
} else {
|
|
178
|
+
text = "".concat(message);
|
|
179
|
+
}
|
|
180
|
+
var log = label.length > 0 ? "".concat(label, " ").concat(text) : text;
|
|
181
|
+
(_console = console).log.apply(_console, [
|
|
182
|
+
log
|
|
183
|
+
].concat(_toConsumableArray(args)));
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
key: "getLongestLabel",
|
|
188
|
+
value: function getLongestLabel() {
|
|
189
|
+
var _this = this;
|
|
190
|
+
var longestLabel = "";
|
|
191
|
+
Object.keys(this.types).forEach(function(type) {
|
|
192
|
+
var _this_types_type = _this.types[type], _this_types_type_label = _this_types_type.label, label = _this_types_type_label === void 0 ? "" : _this_types_type_label;
|
|
193
|
+
if (label.length > longestLabel.length) {
|
|
194
|
+
longestLabel = label;
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
return longestLabel;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
]);
|
|
201
|
+
return Logger;
|
|
202
|
+
}();
|
|
203
|
+
var logger = new Logger();
|
|
204
|
+
logger.Logger = Logger;
|
|
205
|
+
export { Logger, logger };
|
|
@@ -209,7 +209,7 @@ describe("test spr util functions", function() {
|
|
|
209
209
|
expect(htmlWithHead).toBe('<head><meta name="x-moden-spr" content="'.concat(hash, '"></head><div>123</div>'));
|
|
210
210
|
});
|
|
211
211
|
it("should only invoke func one time", /*#__PURE__*/ _asyncToGenerator(function() {
|
|
212
|
-
var index, fn, key,
|
|
212
|
+
var index, fn, key, _ref, res1, res2;
|
|
213
213
|
return __generator(this, function(_state) {
|
|
214
214
|
switch(_state.label){
|
|
215
215
|
case 0:
|
|
@@ -236,10 +236,10 @@ describe("test spr util functions", function() {
|
|
|
236
236
|
])
|
|
237
237
|
];
|
|
238
238
|
case 1:
|
|
239
|
-
|
|
239
|
+
_ref = _slicedToArray.apply(void 0, [
|
|
240
240
|
_state.sent(),
|
|
241
241
|
2
|
|
242
|
-
]), res1 =
|
|
242
|
+
]), res1 = _ref[0], res2 = _ref[1];
|
|
243
243
|
expect(res1.isOrigin && res2.isOrigin).toBe(false);
|
|
244
244
|
expect(res1.isOrigin || res2.isOrigin).toBe(true);
|
|
245
245
|
expect(res1.value).toBe(1);
|
|
@@ -517,7 +517,7 @@ describe("cache", function() {
|
|
|
517
517
|
});
|
|
518
518
|
}));
|
|
519
519
|
it("should match cache correctly", /*#__PURE__*/ _asyncToGenerator(function() {
|
|
520
|
-
var cache, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, cacheable, _cacheable, baseCacheable, matchOne, other,
|
|
520
|
+
var cache, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, cacheable, _cacheable, baseCacheable, matchOne, other, _baseCacheable_requestOpt, requestOpt, cacheConfig, content, context, matchContext, cacheResult, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, notMatch, notMatchContext, nothing, err, err;
|
|
521
521
|
return __generator(this, function(_state) {
|
|
522
522
|
switch(_state.label){
|
|
523
523
|
case 0:
|
|
@@ -541,7 +541,7 @@ describe("cache", function() {
|
|
|
541
541
|
];
|
|
542
542
|
cacheable = _step.value;
|
|
543
543
|
_cacheable = _toArray(cacheable), baseCacheable = _cacheable[0], matchOne = _cacheable[1], other = _cacheable.slice(2);
|
|
544
|
-
|
|
544
|
+
_baseCacheable_requestOpt = baseCacheable.requestOpt, requestOpt = _baseCacheable_requestOpt === void 0 ? {} : _baseCacheable_requestOpt, cacheConfig = baseCacheable.cacheConfig, content = baseCacheable.content;
|
|
545
545
|
context = {
|
|
546
546
|
entry: "",
|
|
547
547
|
pathname: requestOpt.url,
|
|
@@ -205,8 +205,8 @@ var CacheManager = /*#__PURE__*/ function() {
|
|
|
205
205
|
max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
|
|
206
206
|
length: function length(n) {
|
|
207
207
|
var len = n.caches.keys().reduce(function(total, cur) {
|
|
208
|
-
var
|
|
209
|
-
return total + (((
|
|
208
|
+
var _n_caches_peek;
|
|
209
|
+
return total + (((_n_caches_peek = n.caches.peek(cur)) === null || _n_caches_peek === void 0 ? void 0 : _n_caches_peek.size) || 0);
|
|
210
210
|
}, 1);
|
|
211
211
|
return len;
|
|
212
212
|
}
|
|
@@ -266,9 +266,9 @@ var CacheManager = /*#__PURE__*/ function() {
|
|
|
266
266
|
{
|
|
267
267
|
key: "queryFactor",
|
|
268
268
|
value: function queryFactor(context, data) {
|
|
269
|
-
var
|
|
270
|
-
var queryKeys = (
|
|
271
|
-
var queryMatches = (
|
|
269
|
+
var _data_includes, _data_matches;
|
|
270
|
+
var queryKeys = (_data_includes = data.includes) === null || _data_includes === void 0 ? void 0 : _data_includes.query;
|
|
271
|
+
var queryMatches = (_data_matches = data.matches) === null || _data_matches === void 0 ? void 0 : _data_matches.query;
|
|
272
272
|
if (!queryKeys || queryKeys.length === 0) {
|
|
273
273
|
return null;
|
|
274
274
|
}
|
|
@@ -280,9 +280,9 @@ var CacheManager = /*#__PURE__*/ function() {
|
|
|
280
280
|
{
|
|
281
281
|
key: "headerFactor",
|
|
282
282
|
value: function headerFactor(context, data) {
|
|
283
|
-
var
|
|
284
|
-
var headerKeys = (
|
|
285
|
-
var headerMatches = (
|
|
283
|
+
var _data_includes, _data_matches;
|
|
284
|
+
var headerKeys = (_data_includes = data.includes) === null || _data_includes === void 0 ? void 0 : _data_includes.header;
|
|
285
|
+
var headerMatches = (_data_matches = data.matches) === null || _data_matches === void 0 ? void 0 : _data_matches.header;
|
|
286
286
|
if (!headerKeys || headerKeys.length === 0) {
|
|
287
287
|
return null;
|
|
288
288
|
}
|
|
@@ -85,7 +85,7 @@ function _unsupportedIterableToArray(o, minLen) {
|
|
|
85
85
|
import { headersWithoutCookie } from "../../utils";
|
|
86
86
|
var createMetrics = function(context, metrics) {
|
|
87
87
|
var entry = context.entryName, request = context.request;
|
|
88
|
-
var
|
|
88
|
+
var _ref = request || {}, _ref_pathname = _ref.pathname, pathname = _ref_pathname === void 0 ? "" : _ref_pathname;
|
|
89
89
|
var emitTimer = function(name, cost) {
|
|
90
90
|
var tags = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
|
|
91
91
|
metrics.emitTimer(name, cost, _objectSpreadProps(_objectSpread({}, tags), {
|
|
@@ -107,7 +107,7 @@ var createMetrics = function(context, metrics) {
|
|
|
107
107
|
};
|
|
108
108
|
var createLogger = function(serverContext, logger) {
|
|
109
109
|
var request = serverContext.request || {};
|
|
110
|
-
var
|
|
110
|
+
var _request_headers = request.headers, headers = _request_headers === void 0 ? {} : _request_headers, _request_pathname = request.pathname, pathname = _request_pathname === void 0 ? "" : _request_pathname;
|
|
111
111
|
var debug = function(message) {
|
|
112
112
|
for(var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++){
|
|
113
113
|
args[_key - 1] = arguments[_key];
|
|
@@ -129,7 +129,7 @@ import cache from "./cache";
|
|
|
129
129
|
import { createLogger, createMetrics } from "./measure";
|
|
130
130
|
var render = function() {
|
|
131
131
|
var _ref = _asyncToGenerator(function(ctx, renderOptions, runner) {
|
|
132
|
-
var
|
|
132
|
+
var _ctx_res, urlPath, bundle, distDir, template, entryName, staticGenerate, bundleJS, loadableUri, loadableStats, routesManifestUri, routeManifest, context, serverRender, content, _context_redirection, url, _context_redirection_status, status;
|
|
133
133
|
return __generator(this, function(_state) {
|
|
134
134
|
switch(_state.label){
|
|
135
135
|
case 0:
|
|
@@ -157,7 +157,7 @@ var render = function() {
|
|
|
157
157
|
status: function(code) {
|
|
158
158
|
ctx.res.statusCode = code;
|
|
159
159
|
},
|
|
160
|
-
locals: ((
|
|
160
|
+
locals: ((_ctx_res = ctx.res) === null || _ctx_res === void 0 ? void 0 : _ctx_res.locals) || {}
|
|
161
161
|
},
|
|
162
162
|
redirection: {},
|
|
163
163
|
template: template,
|
|
@@ -180,7 +180,7 @@ var render = function() {
|
|
|
180
180
|
];
|
|
181
181
|
case 1:
|
|
182
182
|
content = _state.sent();
|
|
183
|
-
|
|
183
|
+
_context_redirection = context.redirection, url = _context_redirection.url, _context_redirection_status = _context_redirection.status, status = _context_redirection_status === void 0 ? 302 : _context_redirection_status;
|
|
184
184
|
if (url) {
|
|
185
185
|
return [
|
|
186
186
|
2,
|
|
@@ -89,9 +89,9 @@ var RouteMatcher = /*#__PURE__*/ function() {
|
|
|
89
89
|
if (!this.urlReg) {
|
|
90
90
|
return this.urlPath.length;
|
|
91
91
|
} else {
|
|
92
|
-
var
|
|
92
|
+
var _result_;
|
|
93
93
|
var result = this.urlReg.exec(pathname);
|
|
94
|
-
return (result === null || result === void 0 ? void 0 : (
|
|
94
|
+
return (result === null || result === void 0 ? void 0 : (_result_ = result[0]) === null || _result_ === void 0 ? void 0 : _result_.length) || null;
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
},
|
|
@@ -147,10 +147,10 @@ var createStaticFileHandler = function(rules) {
|
|
|
147
147
|
var output = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
148
148
|
return function() {
|
|
149
149
|
var _ref = _asyncToGenerator(function(context, next) {
|
|
150
|
-
var requestUrl, req, res,
|
|
150
|
+
var requestUrl, req, res, _output_assetPrefix, assetPrefix, hitRule, resume;
|
|
151
151
|
return __generator(this, function(_state) {
|
|
152
152
|
requestUrl = context.url, req = context.req, res = context.res;
|
|
153
|
-
|
|
153
|
+
_output_assetPrefix = output.assetPrefix, assetPrefix = _output_assetPrefix === void 0 ? "/" : _output_assetPrefix;
|
|
154
154
|
hitRule = rules.find(function(item) {
|
|
155
155
|
if (isString(item.path) && requestUrl.startsWith(item.path)) {
|
|
156
156
|
return true;
|
|
@@ -417,13 +417,13 @@ var Server = /*#__PURE__*/ function() {
|
|
|
417
417
|
value: function createHookRunner() {
|
|
418
418
|
var _this = this;
|
|
419
419
|
return _asyncToGenerator(function() {
|
|
420
|
-
var options,
|
|
420
|
+
var options, _options_internalPlugins, internalPlugins, pwd, _options_plugins, plugins, serverPlugins, loadedPlugins, hooksRunner;
|
|
421
421
|
return __generator(this, function(_state) {
|
|
422
422
|
switch(_state.label){
|
|
423
423
|
case 0:
|
|
424
424
|
serverManager.clear();
|
|
425
425
|
options = _this.options;
|
|
426
|
-
|
|
426
|
+
_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;
|
|
427
427
|
serverPlugins = _this.serverConfig.plugins || [];
|
|
428
428
|
loadedPlugins = loadPlugins(pwd, _toConsumableArray(serverPlugins).concat(_toConsumableArray(plugins)), {
|
|
429
429
|
internalPlugins: internalPlugins
|
|
@@ -471,7 +471,7 @@ var Server = /*#__PURE__*/ function() {
|
|
|
471
471
|
key: "initAppContext",
|
|
472
472
|
value: function initAppContext() {
|
|
473
473
|
var options = this.options;
|
|
474
|
-
var appDirectory = options.pwd,
|
|
474
|
+
var appDirectory = options.pwd, _options_plugins = options.plugins, plugins = _options_plugins === void 0 ? [] : _options_plugins, config = options.config;
|
|
475
475
|
var serverPlugins = plugins.map(function(p) {
|
|
476
476
|
return {
|
|
477
477
|
server: p
|
|
@@ -257,14 +257,14 @@ var ModernServer = /*#__PURE__*/ function() {
|
|
|
257
257
|
value: function onInit(runner, app) {
|
|
258
258
|
var _this = this;
|
|
259
259
|
return _asyncToGenerator(function() {
|
|
260
|
-
var
|
|
260
|
+
var _conf_bff, distDir, staticGenerate, conf, usageRoutes, staticPathRegExp;
|
|
261
261
|
return __generator(this, function(_state) {
|
|
262
262
|
switch(_state.label){
|
|
263
263
|
case 0:
|
|
264
264
|
_this.runner = runner;
|
|
265
265
|
distDir = _this.distDir, staticGenerate = _this.staticGenerate, conf = _this.conf;
|
|
266
266
|
debug("final server conf", _this.conf);
|
|
267
|
-
_this.proxyHandler = createProxyHandler((
|
|
267
|
+
_this.proxyHandler = createProxyHandler((_conf_bff = conf.bff) === null || _conf_bff === void 0 ? void 0 : _conf_bff.proxy);
|
|
268
268
|
if (_this.proxyHandler) {
|
|
269
269
|
_this.proxyHandler.forEach(function(handler) {
|
|
270
270
|
_this.addHandler(handler);
|
|
@@ -502,13 +502,13 @@ var ModernServer = /*#__PURE__*/ function() {
|
|
|
502
502
|
value: function prepareFrameHandler(options) {
|
|
503
503
|
var _this = this;
|
|
504
504
|
return _asyncToGenerator(function() {
|
|
505
|
-
var workDir, runner,
|
|
505
|
+
var workDir, runner, _ref, onlyApi, onlyWeb, _createMiddlewareCollecter, getMiddlewares, collector, _getMiddlewares, pluginAPIExt, pluginWebExt, apiDir, serverDir, webExtension, apiExtension;
|
|
506
506
|
return __generator(this, function(_state) {
|
|
507
507
|
switch(_state.label){
|
|
508
508
|
case 0:
|
|
509
509
|
workDir = _this.workDir, runner = _this.runner;
|
|
510
|
-
|
|
511
|
-
|
|
510
|
+
_ref = options || {}, onlyApi = _ref.onlyApi, onlyWeb = _ref.onlyWeb;
|
|
511
|
+
_createMiddlewareCollecter = createMiddlewareCollecter(), getMiddlewares = _createMiddlewareCollecter.getMiddlewares, collector = _objectWithoutProperties(_createMiddlewareCollecter, [
|
|
512
512
|
"getMiddlewares"
|
|
513
513
|
]);
|
|
514
514
|
return [
|
|
@@ -517,7 +517,7 @@ var ModernServer = /*#__PURE__*/ function() {
|
|
|
517
517
|
];
|
|
518
518
|
case 1:
|
|
519
519
|
_state.sent();
|
|
520
|
-
|
|
520
|
+
_getMiddlewares = getMiddlewares(), pluginAPIExt = _getMiddlewares.api, pluginWebExt = _getMiddlewares.web;
|
|
521
521
|
apiDir = path.join(workDir, API_DIR);
|
|
522
522
|
serverDir = path.join(workDir, SERVER_DIR);
|
|
523
523
|
return [
|
|
@@ -731,7 +731,7 @@ var ModernServer = /*#__PURE__*/ function() {
|
|
|
731
731
|
value: function routeHandler(context) {
|
|
732
732
|
var _this = this;
|
|
733
733
|
return _asyncToGenerator(function() {
|
|
734
|
-
var res, matched, route, afterMatchContext,
|
|
734
|
+
var res, matched, route, afterMatchContext, _afterMatchContext_router, current, url, status, matched2, middlewareContext, renderResult, contentStream, response, afterRenderContext;
|
|
735
735
|
return __generator(this, function(_state) {
|
|
736
736
|
switch(_state.label){
|
|
737
737
|
case 0:
|
|
@@ -778,7 +778,7 @@ var ModernServer = /*#__PURE__*/ function() {
|
|
|
778
778
|
2
|
|
779
779
|
];
|
|
780
780
|
}
|
|
781
|
-
|
|
781
|
+
_afterMatchContext_router = afterMatchContext.router, current = _afterMatchContext_router.current, url = _afterMatchContext_router.url, status = _afterMatchContext_router.status;
|
|
782
782
|
if (url) {
|
|
783
783
|
_this.redirect(res, url, status);
|
|
784
784
|
return [
|
|
@@ -90,7 +90,7 @@ var useLocalPrefix = function(url) {
|
|
|
90
90
|
};
|
|
91
91
|
var getStaticReg = function() {
|
|
92
92
|
var output = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}, html = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
93
|
-
var tmp = output.distPath,
|
|
93
|
+
var tmp = output.distPath, _ref = tmp === void 0 ? {} : tmp, cssPath = _ref.css, jsPath = _ref.js, mediaPath = _ref.media, _output_assetPrefix = output.assetPrefix, assetPrefix = _output_assetPrefix === void 0 ? "/" : _output_assetPrefix;
|
|
94
94
|
var favicon = html.favicon, faviconByEntries = html.faviconByEntries;
|
|
95
95
|
var prefix = useLocalPrefix(assetPrefix) ? assetPrefix : "";
|
|
96
96
|
var favicons = prepareFavicons(favicon, faviconByEntries);
|
|
@@ -122,15 +122,20 @@ var __generator = this && this.__generator || function(thisArg, body) {
|
|
|
122
122
|
};
|
|
123
123
|
}
|
|
124
124
|
};
|
|
125
|
-
var
|
|
125
|
+
var _ctx_request, _ctx_request1, _ctx_request2;
|
|
126
|
+
import { Logger } from "./libs/logger";
|
|
126
127
|
import { RouteMatchManager } from "./libs/route";
|
|
128
|
+
import { metrics as defaultMetrics } from "./libs/metrics";
|
|
129
|
+
var handleUrl = function(url) {
|
|
130
|
+
return url.replace(/^https?:\/\/.*?\//gi, "/");
|
|
131
|
+
};
|
|
127
132
|
var createHandler = function(manifest) {
|
|
128
133
|
var routeMgr = new RouteMatchManager();
|
|
129
134
|
var pages = manifest.pages, routes = manifest.routes;
|
|
130
135
|
routeMgr.reset(routes);
|
|
131
136
|
return function() {
|
|
132
137
|
var _ref = _asyncToGenerator(function(ctx) {
|
|
133
|
-
var pageMatch, page, _query, _pathname, _params, params;
|
|
138
|
+
var pageMatch, page, _query, _pathname, _params, params, e;
|
|
134
139
|
return __generator(this, function(_state) {
|
|
135
140
|
switch(_state.label){
|
|
136
141
|
case 0:
|
|
@@ -143,13 +148,26 @@ var createHandler = function(manifest) {
|
|
|
143
148
|
];
|
|
144
149
|
}
|
|
145
150
|
page = pages[pageMatch.spec.urlPath];
|
|
146
|
-
(_query = (
|
|
147
|
-
(_pathname = (
|
|
148
|
-
(_params = (
|
|
151
|
+
(_query = (_ctx_request = ctx.request).query) !== null && _query !== void 0 ? _query : _ctx_request.query = ctx.query;
|
|
152
|
+
(_pathname = (_ctx_request1 = ctx.request).pathname) !== null && _pathname !== void 0 ? _pathname : _ctx_request1.pathname = ctx.pathname;
|
|
153
|
+
(_params = (_ctx_request2 = ctx.request).params) !== null && _params !== void 0 ? _params : _ctx_request2.params = ctx.params;
|
|
149
154
|
params = pageMatch.parseURLParams(ctx.url);
|
|
155
|
+
if (!page.serverRender) return [
|
|
156
|
+
3,
|
|
157
|
+
4
|
|
158
|
+
];
|
|
159
|
+
_state.label = 1;
|
|
160
|
+
case 1:
|
|
161
|
+
_state.trys.push([
|
|
162
|
+
1,
|
|
163
|
+
3,
|
|
164
|
+
,
|
|
165
|
+
4
|
|
166
|
+
]);
|
|
150
167
|
return [
|
|
151
168
|
4,
|
|
152
169
|
page.serverRender({
|
|
170
|
+
entryName: page.entryName,
|
|
153
171
|
template: page.template,
|
|
154
172
|
query: ctx.query,
|
|
155
173
|
request: ctx.request,
|
|
@@ -157,15 +175,53 @@ var createHandler = function(manifest) {
|
|
|
157
175
|
pathname: ctx.pathname,
|
|
158
176
|
req: ctx.request,
|
|
159
177
|
res: ctx.response,
|
|
160
|
-
params: ctx.params || params || {}
|
|
178
|
+
params: ctx.params || params || {},
|
|
179
|
+
logger: ctx.logger || new Logger({
|
|
180
|
+
level: "warn"
|
|
181
|
+
}),
|
|
182
|
+
metrics: ctx.metrics || defaultMetrics,
|
|
183
|
+
loadableStats: ctx.loadableStats,
|
|
184
|
+
routeManifest: ctx.routeManifest
|
|
161
185
|
})
|
|
162
186
|
];
|
|
163
|
-
case
|
|
187
|
+
case 2:
|
|
164
188
|
ctx.body = _state.sent();
|
|
165
189
|
ctx.status = 200;
|
|
166
190
|
return [
|
|
167
191
|
2
|
|
168
192
|
];
|
|
193
|
+
case 3:
|
|
194
|
+
e = _state.sent();
|
|
195
|
+
if (page.template) {
|
|
196
|
+
ctx.body = page.template;
|
|
197
|
+
ctx.status = 200;
|
|
198
|
+
return [
|
|
199
|
+
2
|
|
200
|
+
];
|
|
201
|
+
} else {
|
|
202
|
+
ctx.body = "404: not found";
|
|
203
|
+
ctx.status = 404;
|
|
204
|
+
return [
|
|
205
|
+
2
|
|
206
|
+
];
|
|
207
|
+
}
|
|
208
|
+
return [
|
|
209
|
+
3,
|
|
210
|
+
4
|
|
211
|
+
];
|
|
212
|
+
case 4:
|
|
213
|
+
if (page.template) {
|
|
214
|
+
ctx.body = page.template;
|
|
215
|
+
ctx.status = 200;
|
|
216
|
+
return [
|
|
217
|
+
2
|
|
218
|
+
];
|
|
219
|
+
}
|
|
220
|
+
ctx.body = "404: not found";
|
|
221
|
+
ctx.status = 404;
|
|
222
|
+
return [
|
|
223
|
+
2
|
|
224
|
+
];
|
|
169
225
|
}
|
|
170
226
|
});
|
|
171
227
|
});
|
|
@@ -174,4 +230,4 @@ var createHandler = function(manifest) {
|
|
|
174
230
|
};
|
|
175
231
|
}();
|
|
176
232
|
};
|
|
177
|
-
export { createHandler };
|
|
233
|
+
export { createHandler, handleUrl };
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
/// <reference types="node" />
|
|
4
4
|
/// <reference types="node" />
|
|
5
5
|
/// <reference types="node/http" />
|
|
6
|
-
/// <reference types=".dts-temp/
|
|
6
|
+
/// <reference types=".dts-temp/lM3iZKWPu2jm0FkDHIE8Y/src/type" />
|
|
7
7
|
import { IncomingMessage, ServerResponse } from 'http';
|
|
8
8
|
import { URL } from 'url';
|
|
9
9
|
import qs from 'querystring';
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
2
|
+
type LogMsg = number | string | Error | null;
|
|
3
|
+
interface LoggerConfiguration {
|
|
4
|
+
label?: string;
|
|
5
|
+
level?: LogLevel;
|
|
6
|
+
}
|
|
7
|
+
interface InstanceConfiguration {
|
|
8
|
+
displayLabel?: boolean;
|
|
9
|
+
uppercaseLabel?: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface ConstructorOptions {
|
|
12
|
+
config?: InstanceConfiguration;
|
|
13
|
+
level?: string;
|
|
14
|
+
types?: Record<string, LoggerConfiguration>;
|
|
15
|
+
}
|
|
16
|
+
type LoggerFunction = (message?: LogMsg, ...args: any[]) => void;
|
|
17
|
+
declare const LOG_TYPES: {
|
|
18
|
+
error: {
|
|
19
|
+
color: string;
|
|
20
|
+
label: string;
|
|
21
|
+
level: string;
|
|
22
|
+
};
|
|
23
|
+
info: {
|
|
24
|
+
color: string;
|
|
25
|
+
label: string;
|
|
26
|
+
level: string;
|
|
27
|
+
};
|
|
28
|
+
success: {
|
|
29
|
+
color: string;
|
|
30
|
+
label: string;
|
|
31
|
+
level: string;
|
|
32
|
+
};
|
|
33
|
+
warn: {
|
|
34
|
+
color: string;
|
|
35
|
+
label: string;
|
|
36
|
+
level: string;
|
|
37
|
+
};
|
|
38
|
+
debug: {
|
|
39
|
+
color: string;
|
|
40
|
+
label: string;
|
|
41
|
+
level: string;
|
|
42
|
+
};
|
|
43
|
+
log: {
|
|
44
|
+
level: string;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
declare class Logger {
|
|
49
|
+
private readonly level;
|
|
50
|
+
private readonly config;
|
|
51
|
+
private readonly types;
|
|
52
|
+
private readonly longestLabel;
|
|
53
|
+
[key: string]: any;
|
|
54
|
+
constructor(options?: ConstructorOptions);
|
|
55
|
+
private _log;
|
|
56
|
+
private getLongestLabel;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
type LoggerInterface = { [key in keyof typeof LOG_TYPES]: LoggerFunction };
|
|
60
|
+
declare const logger: Logger & LoggerInterface;
|
|
61
|
+
export { Logger };
|
|
62
|
+
export { logger };
|
|
63
|
+
export type { LoggerInterface };
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node/http" />
|
|
3
|
-
/// <reference types=".dts-temp/
|
|
3
|
+
/// <reference types=".dts-temp/lM3iZKWPu2jm0FkDHIE8Y/src/type" />
|
|
4
4
|
import { IncomingMessage } from 'http';
|
|
5
5
|
import type { OutputNormalizedConfig, HtmlNormalizedConfig } from '@modern-js/server-core';
|
|
6
6
|
export declare const debug: any;
|
|
@@ -8,8 +8,9 @@ export type Manifest = {
|
|
|
8
8
|
{
|
|
9
9
|
entryName: string;
|
|
10
10
|
template: string;
|
|
11
|
-
serverRender
|
|
11
|
+
serverRender?: (ctx: Record<string, any>) => Promise<string>;
|
|
12
12
|
}>;
|
|
13
13
|
routes: ModernRouteInterface[];
|
|
14
14
|
};
|
|
15
|
+
export declare const handleUrl: (url: string) => string;
|
|
15
16
|
export declare const createHandler: (manifest: Manifest) => (ctx: Context) => Promise<void>;
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "2.4.0",
|
|
14
|
+
"version": "2.4.1-beta.0",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -55,8 +55,8 @@
|
|
|
55
55
|
"merge-deep": "^3.0.3",
|
|
56
56
|
"path-to-regexp": "^6.2.0",
|
|
57
57
|
"serve-static": "^1.14.1",
|
|
58
|
-
"@modern-js/utils": "2.4.0",
|
|
59
|
-
"@modern-js/server-core": "2.4.0"
|
|
58
|
+
"@modern-js/utils": "2.4.1-beta.0",
|
|
59
|
+
"@modern-js/server-core": "2.4.1-beta.0"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
62
|
"@types/cookie": "^0.4.1",
|
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
"portfinder": "^1.0.28",
|
|
74
74
|
"typescript": "^4",
|
|
75
75
|
"@modern-js/types": "2.4.0",
|
|
76
|
-
"@modern-js/server-core": "2.4.0",
|
|
76
|
+
"@modern-js/server-core": "2.4.1-beta.0",
|
|
77
77
|
"@scripts/build": "2.4.0",
|
|
78
78
|
"@scripts/jest-config": "2.4.0"
|
|
79
79
|
},
|