@modern-js/prod-server 2.21.1 → 2.22.1
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 +33 -0
- package/dist/cjs/constants.js +15 -5
- package/dist/cjs/index.js +12 -4
- package/dist/cjs/libs/context/context.js +3 -1
- package/dist/cjs/libs/context/index.js +6 -2
- package/dist/cjs/libs/hook-api/base.js +88 -0
- package/dist/cjs/libs/hook-api/index.js +17 -79
- package/dist/cjs/libs/hook-api/index.worker.js +109 -0
- package/dist/cjs/libs/hook-api/route.js +3 -1
- package/dist/cjs/libs/hook-api/template.js +6 -26
- package/dist/cjs/libs/hook-api/templateForStream.js +35 -0
- package/dist/cjs/libs/loadConfig.js +9 -3
- package/dist/cjs/libs/logger.js +6 -2
- package/dist/cjs/libs/metrics.js +3 -1
- package/dist/cjs/libs/proxy.js +6 -2
- package/dist/cjs/libs/render/cache/__tests__/cacheable.js +3 -1
- package/dist/cjs/libs/render/cache/__tests__/error-configuration.js +3 -1
- package/dist/cjs/libs/render/cache/__tests__/matched-cache.js +3 -1
- package/dist/cjs/libs/render/cache/index.js +3 -1
- package/dist/cjs/libs/render/cache/page-caches/index.js +3 -1
- package/dist/cjs/libs/render/cache/page-caches/lru.js +3 -1
- package/dist/cjs/libs/render/cache/spr.js +6 -2
- package/dist/cjs/libs/render/cache/util.js +24 -8
- package/dist/cjs/libs/render/index.js +3 -4
- package/dist/cjs/libs/render/measure.js +6 -2
- package/dist/cjs/libs/render/reader.js +32 -19
- package/dist/cjs/libs/render/ssr.js +3 -1
- package/dist/cjs/libs/render/static.js +3 -1
- package/dist/cjs/libs/render/type.js +3 -1
- package/dist/cjs/libs/render/utils.js +8 -3
- package/dist/cjs/libs/route/index.js +6 -2
- package/dist/cjs/libs/route/matcher.js +3 -1
- package/dist/cjs/libs/route/route.js +3 -1
- package/dist/cjs/libs/serveFile.js +6 -2
- package/dist/cjs/renderHtml.js +75 -0
- package/dist/cjs/server/index.js +9 -3
- package/dist/cjs/server/modernServer.js +21 -16
- package/dist/cjs/server/modernServerSplit.js +3 -1
- package/dist/cjs/utils.js +27 -9
- package/dist/cjs/workerServer.js +90 -9
- package/dist/esm/libs/hook-api/base.js +104 -0
- package/dist/esm/libs/hook-api/index.js +5 -109
- package/dist/esm/libs/hook-api/index.worker.js +105 -0
- package/dist/esm/libs/hook-api/template.js +1 -28
- package/dist/esm/libs/hook-api/templateForStream.js +28 -0
- package/dist/esm/libs/render/index.js +1 -4
- package/dist/esm/libs/render/reader.js +26 -26
- package/dist/esm/libs/render/utils.js +2 -1
- package/dist/esm/renderHtml.js +141 -0
- package/dist/esm/server/index.js +11 -1
- package/dist/esm/server/modernServer.js +45 -37
- package/dist/esm/workerServer.js +98 -8
- package/dist/esm-node/libs/hook-api/base.js +68 -0
- package/dist/esm-node/libs/hook-api/index.js +5 -74
- package/dist/esm-node/libs/hook-api/index.worker.js +84 -0
- package/dist/esm-node/libs/hook-api/template.js +1 -25
- package/dist/esm-node/libs/hook-api/templateForStream.js +25 -0
- package/dist/esm-node/libs/render/index.js +1 -4
- package/dist/esm-node/libs/render/reader.js +14 -11
- package/dist/esm-node/libs/render/utils.js +2 -1
- package/dist/esm-node/renderHtml.js +71 -0
- package/dist/esm-node/server/index.js +6 -2
- package/dist/esm-node/server/modernServer.js +18 -15
- package/dist/esm-node/workerServer.js +84 -7
- package/dist/types/libs/context/context.d.ts +1 -1
- package/dist/types/libs/hook-api/base.d.ts +53 -0
- package/dist/types/libs/hook-api/index.worker.d.ts +18 -0
- package/dist/types/libs/hook-api/template.d.ts +11 -14
- package/dist/types/libs/hook-api/templateForStream.d.ts +13 -0
- package/dist/types/libs/render/reader.d.ts +4 -2
- package/dist/types/renderHtml.d.ts +21 -0
- package/dist/types/server/index.d.ts +5 -1
- package/dist/types/server/modernServer.d.ts +4 -3
- package/dist/types/utils.d.ts +1 -1
- package/dist/types/workerServer.d.ts +19 -8
- package/package.json +21 -7
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { TemplateAPI
|
|
1
|
+
import { TemplateAPI } from "../hook-api/template";
|
|
2
|
+
import { templateInjectableStream } from "../hook-api/templateForStream";
|
|
2
3
|
export var injectServerData = function(content, context) {
|
|
3
4
|
var template = new TemplateAPI(content);
|
|
4
5
|
template.prependHead('<script type="application/json" id="__MODERN_SERVER_DATA__">'.concat(JSON.stringify(context.serverData), "</script>"));
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { _ as _assert_this_initialized } from "@swc/helpers/_/_assert_this_initialized";
|
|
2
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
3
|
+
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
4
|
+
import { _ as _create_class } from "@swc/helpers/_/_create_class";
|
|
5
|
+
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
6
|
+
import { _ as _inherits } from "@swc/helpers/_/_inherits";
|
|
7
|
+
import { _ as _create_super } from "@swc/helpers/_/_create_super";
|
|
8
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
9
|
+
import { OutgoingMessage } from "http";
|
|
10
|
+
import { Readable } from "stream";
|
|
11
|
+
import { Socket } from "net";
|
|
12
|
+
import { Server } from "./server";
|
|
13
|
+
import { isRedirect } from "./utils";
|
|
14
|
+
var IncomingMessageLike = /* @__PURE__ */ function(Readable2) {
|
|
15
|
+
"use strict";
|
|
16
|
+
_inherits(IncomingMessageLike2, Readable2);
|
|
17
|
+
var _super = _create_super(IncomingMessageLike2);
|
|
18
|
+
function IncomingMessageLike2(param) {
|
|
19
|
+
var method = param.method, url = param.url, headers = param.headers;
|
|
20
|
+
_class_call_check(this, IncomingMessageLike2);
|
|
21
|
+
var _this;
|
|
22
|
+
_this = _super.call(this);
|
|
23
|
+
_define_property(_assert_this_initialized(_this), "headers", void 0);
|
|
24
|
+
_define_property(_assert_this_initialized(_this), "method", void 0);
|
|
25
|
+
_define_property(_assert_this_initialized(_this), "url", void 0);
|
|
26
|
+
_define_property(_assert_this_initialized(_this), "socket", void 0);
|
|
27
|
+
_this.socket = new Socket();
|
|
28
|
+
_this.headers = headers || {};
|
|
29
|
+
_this.headers.host = "localhost:8080";
|
|
30
|
+
_this.method = method || "get";
|
|
31
|
+
_this.url = url;
|
|
32
|
+
return _this;
|
|
33
|
+
}
|
|
34
|
+
return IncomingMessageLike2;
|
|
35
|
+
}(Readable);
|
|
36
|
+
var ServerResponseLike = /* @__PURE__ */ function(OutgoingMessage2) {
|
|
37
|
+
"use strict";
|
|
38
|
+
_inherits(ServerResponseLike2, OutgoingMessage2);
|
|
39
|
+
var _super = _create_super(ServerResponseLike2);
|
|
40
|
+
function ServerResponseLike2() {
|
|
41
|
+
_class_call_check(this, ServerResponseLike2);
|
|
42
|
+
var _this;
|
|
43
|
+
_this = _super.call(this);
|
|
44
|
+
_define_property(_assert_this_initialized(_this), "statusCode", void 0);
|
|
45
|
+
_define_property(_assert_this_initialized(_this), "data", void 0);
|
|
46
|
+
_this.statusCode = 200;
|
|
47
|
+
_this.data = [];
|
|
48
|
+
return _this;
|
|
49
|
+
}
|
|
50
|
+
_create_class(ServerResponseLike2, [
|
|
51
|
+
{
|
|
52
|
+
key: "end",
|
|
53
|
+
value: function end(chunk, _encoding, cb) {
|
|
54
|
+
this.data.push(chunk.toString());
|
|
55
|
+
cb && cb();
|
|
56
|
+
this.emit("finish");
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
]);
|
|
61
|
+
return ServerResponseLike2;
|
|
62
|
+
}(OutgoingMessage);
|
|
63
|
+
var CustomServer = /* @__PURE__ */ function(Server2) {
|
|
64
|
+
"use strict";
|
|
65
|
+
_inherits(CustomServer2, Server2);
|
|
66
|
+
var _super = _create_super(CustomServer2);
|
|
67
|
+
function CustomServer2() {
|
|
68
|
+
_class_call_check(this, CustomServer2);
|
|
69
|
+
return _super.apply(this, arguments);
|
|
70
|
+
}
|
|
71
|
+
_create_class(CustomServer2, [
|
|
72
|
+
{
|
|
73
|
+
key: "render",
|
|
74
|
+
value: function render(req, res, _url) {
|
|
75
|
+
var _this = this;
|
|
76
|
+
return _async_to_generator(function() {
|
|
77
|
+
var handler;
|
|
78
|
+
return _ts_generator(this, function(_state) {
|
|
79
|
+
handler = _this.getRequestHandler();
|
|
80
|
+
handler(req, res);
|
|
81
|
+
return [
|
|
82
|
+
2,
|
|
83
|
+
null
|
|
84
|
+
];
|
|
85
|
+
});
|
|
86
|
+
})();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
]);
|
|
90
|
+
return CustomServer2;
|
|
91
|
+
}(Server);
|
|
92
|
+
function renderHtml(_) {
|
|
93
|
+
return _renderHtml.apply(this, arguments);
|
|
94
|
+
}
|
|
95
|
+
function _renderHtml() {
|
|
96
|
+
_renderHtml = _async_to_generator(function(param) {
|
|
97
|
+
var url, method, headers, body, serverOptions, req, res, customServer;
|
|
98
|
+
return _ts_generator(this, function(_state) {
|
|
99
|
+
switch (_state.label) {
|
|
100
|
+
case 0:
|
|
101
|
+
url = param.url, method = param.method, headers = param.headers, body = param.body, serverOptions = param.serverOptions;
|
|
102
|
+
req = new IncomingMessageLike({
|
|
103
|
+
method: method,
|
|
104
|
+
url: url,
|
|
105
|
+
headers: headers
|
|
106
|
+
});
|
|
107
|
+
if (body) {
|
|
108
|
+
req.push(body);
|
|
109
|
+
req.push(null);
|
|
110
|
+
}
|
|
111
|
+
res = new ServerResponseLike();
|
|
112
|
+
customServer = new CustomServer(serverOptions);
|
|
113
|
+
return [
|
|
114
|
+
4,
|
|
115
|
+
customServer.init({
|
|
116
|
+
disableHttpServer: true
|
|
117
|
+
})
|
|
118
|
+
];
|
|
119
|
+
case 1:
|
|
120
|
+
_state.sent();
|
|
121
|
+
customServer.render(req, res);
|
|
122
|
+
return [
|
|
123
|
+
2,
|
|
124
|
+
new Promise(function(resolve) {
|
|
125
|
+
res.addListener("finish", function() {
|
|
126
|
+
resolve({
|
|
127
|
+
headers: res.getHeaders(),
|
|
128
|
+
redirected: isRedirect(res.statusCode),
|
|
129
|
+
status: res.statusCode,
|
|
130
|
+
url: url,
|
|
131
|
+
body: res.data.join()
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
})
|
|
135
|
+
];
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
return _renderHtml.apply(this, arguments);
|
|
140
|
+
}
|
|
141
|
+
module.exports = renderHtml;
|
package/dist/esm/server/index.js
CHANGED
|
@@ -49,6 +49,9 @@ export var Server = /* @__PURE__ */ function() {
|
|
|
49
49
|
* - 执行 server init
|
|
50
50
|
*/
|
|
51
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;
|
|
52
55
|
var _this = this;
|
|
53
56
|
return _async_to_generator(function() {
|
|
54
57
|
var options;
|
|
@@ -94,17 +97,24 @@ export var Server = /* @__PURE__ */ function() {
|
|
|
94
97
|
];
|
|
95
98
|
case 6:
|
|
96
99
|
_state.sent();
|
|
100
|
+
if (!!disableHttpServer)
|
|
101
|
+
return [
|
|
102
|
+
3,
|
|
103
|
+
8
|
|
104
|
+
];
|
|
97
105
|
return [
|
|
98
106
|
4,
|
|
99
107
|
_this.server.createHTTPServer(_this.getRequestHandler())
|
|
100
108
|
];
|
|
101
109
|
case 7:
|
|
102
110
|
_this.app = _state.sent();
|
|
111
|
+
_state.label = 8;
|
|
112
|
+
case 8:
|
|
103
113
|
return [
|
|
104
114
|
4,
|
|
105
115
|
_this.server.onInit(_this.runner, _this.app)
|
|
106
116
|
];
|
|
107
|
-
case
|
|
117
|
+
case 9:
|
|
108
118
|
_state.sent();
|
|
109
119
|
return [
|
|
110
120
|
2,
|
|
@@ -45,7 +45,7 @@ export var ModernServer = /* @__PURE__ */ function() {
|
|
|
45
45
|
_define_property(this, "staticGenerate", void 0);
|
|
46
46
|
require("ignore-styles");
|
|
47
47
|
this.pwd = pwd;
|
|
48
|
-
this.distDir = path.
|
|
48
|
+
this.distDir = path.resolve(pwd, config.output.path || "dist");
|
|
49
49
|
this.workDir = this.distDir;
|
|
50
50
|
this.conf = config;
|
|
51
51
|
debug("server conf", this.conf);
|
|
@@ -71,6 +71,7 @@ export var ModernServer = /* @__PURE__ */ function() {
|
|
|
71
71
|
case 0:
|
|
72
72
|
_this.runner = runner;
|
|
73
73
|
distDir = _this.distDir, staticGenerate = _this.staticGenerate, conf = _this.conf;
|
|
74
|
+
_this.initReader();
|
|
74
75
|
debug("final server conf", _this.conf);
|
|
75
76
|
_this.proxyHandler = createProxyHandler((_conf_bff = conf.bff) === null || _conf_bff === void 0 ? void 0 : _conf_bff.proxy);
|
|
76
77
|
if (_this.proxyHandler) {
|
|
@@ -78,8 +79,7 @@ export var ModernServer = /* @__PURE__ */ function() {
|
|
|
78
79
|
_this.addHandler(handler);
|
|
79
80
|
});
|
|
80
81
|
}
|
|
81
|
-
|
|
82
|
-
app.on("close", function() {
|
|
82
|
+
app === null || app === void 0 ? void 0 : app.on("close", function() {
|
|
83
83
|
_this.reader.close();
|
|
84
84
|
});
|
|
85
85
|
usageRoutes = _this.filterRoutes(_this.getRoutes());
|
|
@@ -132,39 +132,6 @@ export var ModernServer = /* @__PURE__ */ function() {
|
|
|
132
132
|
}
|
|
133
133
|
)
|
|
134
134
|
},
|
|
135
|
-
{
|
|
136
|
-
key: "onServerChange",
|
|
137
|
-
value: function onServerChange(param) {
|
|
138
|
-
var filepath = param.filepath;
|
|
139
|
-
var _this = this;
|
|
140
|
-
return _async_to_generator(function() {
|
|
141
|
-
var pwd, api, server, apiPath, serverPath, onlyApi, onlyWeb;
|
|
142
|
-
return _ts_generator(this, function(_state) {
|
|
143
|
-
switch (_state.label) {
|
|
144
|
-
case 0:
|
|
145
|
-
pwd = _this.pwd;
|
|
146
|
-
api = AGGRED_DIR.api, server = AGGRED_DIR.server;
|
|
147
|
-
apiPath = path.normalize(path.join(pwd, api));
|
|
148
|
-
serverPath = path.normalize(path.join(pwd, server));
|
|
149
|
-
onlyApi = filepath.startsWith(apiPath);
|
|
150
|
-
onlyWeb = filepath.startsWith(serverPath);
|
|
151
|
-
return [
|
|
152
|
-
4,
|
|
153
|
-
_this.prepareFrameHandler({
|
|
154
|
-
onlyWeb: onlyWeb,
|
|
155
|
-
onlyApi: onlyApi
|
|
156
|
-
})
|
|
157
|
-
];
|
|
158
|
-
case 1:
|
|
159
|
-
_state.sent();
|
|
160
|
-
return [
|
|
161
|
-
2
|
|
162
|
-
];
|
|
163
|
-
}
|
|
164
|
-
});
|
|
165
|
-
})();
|
|
166
|
-
}
|
|
167
|
-
},
|
|
168
135
|
{
|
|
169
136
|
key: "getRequestHandler",
|
|
170
137
|
value: (
|
|
@@ -235,9 +202,50 @@ export var ModernServer = /* @__PURE__ */ function() {
|
|
|
235
202
|
}
|
|
236
203
|
},
|
|
237
204
|
{
|
|
238
|
-
key: "
|
|
205
|
+
key: "initReader",
|
|
239
206
|
value: (
|
|
240
207
|
/* —————————————————————— function will be overwrite —————————————————————— */
|
|
208
|
+
function initReader() {
|
|
209
|
+
this.reader.init();
|
|
210
|
+
}
|
|
211
|
+
)
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
key: "onServerChange",
|
|
215
|
+
value: function onServerChange(param) {
|
|
216
|
+
var filepath = param.filepath;
|
|
217
|
+
var _this = this;
|
|
218
|
+
return _async_to_generator(function() {
|
|
219
|
+
var pwd, api, server, apiPath, serverPath, onlyApi, onlyWeb;
|
|
220
|
+
return _ts_generator(this, function(_state) {
|
|
221
|
+
switch (_state.label) {
|
|
222
|
+
case 0:
|
|
223
|
+
pwd = _this.pwd;
|
|
224
|
+
api = AGGRED_DIR.api, server = AGGRED_DIR.server;
|
|
225
|
+
apiPath = path.normalize(path.join(pwd, api));
|
|
226
|
+
serverPath = path.normalize(path.join(pwd, server));
|
|
227
|
+
onlyApi = filepath.startsWith(apiPath);
|
|
228
|
+
onlyWeb = filepath.startsWith(serverPath);
|
|
229
|
+
return [
|
|
230
|
+
4,
|
|
231
|
+
_this.prepareFrameHandler({
|
|
232
|
+
onlyWeb: onlyWeb,
|
|
233
|
+
onlyApi: onlyApi
|
|
234
|
+
})
|
|
235
|
+
];
|
|
236
|
+
case 1:
|
|
237
|
+
_state.sent();
|
|
238
|
+
return [
|
|
239
|
+
2
|
|
240
|
+
];
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
})();
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
key: "getRoutes",
|
|
248
|
+
value: (
|
|
241
249
|
// get routes info
|
|
242
250
|
function getRoutes() {
|
|
243
251
|
if (this.presetRoutes) {
|
package/dist/esm/workerServer.js
CHANGED
|
@@ -4,6 +4,8 @@ import { _ as _create_class } from "@swc/helpers/_/_create_class";
|
|
|
4
4
|
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
5
5
|
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
6
6
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
7
|
+
import { createAsyncPipeline } from "@modern-js/plugin";
|
|
8
|
+
import { createAfterMatchContext, createAfterRenderContext, createMiddlewareContext } from "./libs/hook-api/index.worker";
|
|
7
9
|
import { Logger } from "./libs/logger";
|
|
8
10
|
import { RouteMatchManager } from "./libs/route";
|
|
9
11
|
import { metrics as defaultMetrics } from "./libs/metrics";
|
|
@@ -65,13 +67,31 @@ export var ReturnResponse = /* @__PURE__ */ function() {
|
|
|
65
67
|
return ReturnResponse2;
|
|
66
68
|
}();
|
|
67
69
|
var RESPONSE_NOTFOUND = new ReturnResponse("404: Page not found", 404);
|
|
70
|
+
var isRedirect = function(code) {
|
|
71
|
+
return [
|
|
72
|
+
301,
|
|
73
|
+
302,
|
|
74
|
+
307,
|
|
75
|
+
308
|
|
76
|
+
].includes(code);
|
|
77
|
+
};
|
|
78
|
+
var checkIsSent = function(context) {
|
|
79
|
+
if (context.res.isSent) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
if (context.res.headers.get("Location") && isRedirect(context.res.status)) {
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
};
|
|
87
|
+
var middlewarePipeline = createAsyncPipeline();
|
|
68
88
|
export var createHandler = function(manifest) {
|
|
69
89
|
var routeMgr = new RouteMatchManager();
|
|
70
90
|
var pages = manifest.pages, routes = manifest.routes;
|
|
71
91
|
routeMgr.reset(routes);
|
|
72
92
|
return function() {
|
|
73
93
|
var _ref = _async_to_generator(function(options) {
|
|
74
|
-
var request, loadableStats, routeManifest, url, pageMatch, page, responseLike, params, baseUrl, serverRenderContext, body, e;
|
|
94
|
+
var _page_serverHooks, _page_serverHooks_afterMatch, request, loadableStats, routeManifest, url, pageMatch, entryName, page, logger, metrics, hookContext, afterMatchHookContext, _page_serverHooks1, _page_serverHooks2, _page_serverHooks_afterRender, middlewarsHookContext, responseLike, params, baseUrl, serverRenderContext, body, afterRenderHookContext, e;
|
|
75
95
|
function createServerRequest(url2, baseUrl2, request2, params2) {
|
|
76
96
|
var pathname = url2.pathname, host = url2.host, searchParams = url2.searchParams;
|
|
77
97
|
var rawHeaders = request2.headers;
|
|
@@ -102,7 +122,23 @@ export var createHandler = function(manifest) {
|
|
|
102
122
|
RESPONSE_NOTFOUND
|
|
103
123
|
];
|
|
104
124
|
}
|
|
105
|
-
|
|
125
|
+
entryName = pageMatch.spec.urlPath;
|
|
126
|
+
page = pages[entryName];
|
|
127
|
+
logger = new Logger({
|
|
128
|
+
level: "warn"
|
|
129
|
+
});
|
|
130
|
+
metrics = defaultMetrics;
|
|
131
|
+
hookContext = createWorkerHookContext(request.url, logger, metrics);
|
|
132
|
+
afterMatchHookContext = createAfterMatchContext(hookContext, entryName);
|
|
133
|
+
page === null || page === void 0 ? void 0 : (_page_serverHooks = page.serverHooks) === null || _page_serverHooks === void 0 ? void 0 : (_page_serverHooks_afterMatch = _page_serverHooks.afterMatch) === null || _page_serverHooks_afterMatch === void 0 ? void 0 : _page_serverHooks_afterMatch.call(_page_serverHooks, afterMatchHookContext, function() {
|
|
134
|
+
return void 0;
|
|
135
|
+
});
|
|
136
|
+
if (checkIsSent(hookContext)) {
|
|
137
|
+
return [
|
|
138
|
+
2,
|
|
139
|
+
new ReturnResponse(hookContext.res.body || "Unkown body", hookContext.res.status, hookContext.res.headers)
|
|
140
|
+
];
|
|
141
|
+
}
|
|
106
142
|
if (!page.serverRender)
|
|
107
143
|
return [
|
|
108
144
|
3,
|
|
@@ -116,6 +152,14 @@ export var createHandler = function(manifest) {
|
|
|
116
152
|
,
|
|
117
153
|
4
|
|
118
154
|
]);
|
|
155
|
+
middlewarsHookContext = createMiddlewareContext(hookContext);
|
|
156
|
+
applyMiddlewares(middlewarsHookContext, (_page_serverHooks1 = page.serverHooks) === null || _page_serverHooks1 === void 0 ? void 0 : _page_serverHooks1.middleware);
|
|
157
|
+
if (checkIsSent(hookContext)) {
|
|
158
|
+
return [
|
|
159
|
+
2,
|
|
160
|
+
new ReturnResponse(hookContext.res.body || "Unkown body", hookContext.res.status, hookContext.res.headers)
|
|
161
|
+
];
|
|
162
|
+
}
|
|
119
163
|
responseLike = {
|
|
120
164
|
headers: {},
|
|
121
165
|
statusCode: 200,
|
|
@@ -137,10 +181,8 @@ export var createHandler = function(manifest) {
|
|
|
137
181
|
redirection: {},
|
|
138
182
|
template: page.template,
|
|
139
183
|
entryName: page.entryName,
|
|
140
|
-
logger:
|
|
141
|
-
|
|
142
|
-
}),
|
|
143
|
-
metrics: defaultMetrics,
|
|
184
|
+
logger: logger,
|
|
185
|
+
metrics: metrics,
|
|
144
186
|
// FIXME: pass correctly req & res
|
|
145
187
|
req: request,
|
|
146
188
|
res: responseLike
|
|
@@ -151,9 +193,19 @@ export var createHandler = function(manifest) {
|
|
|
151
193
|
];
|
|
152
194
|
case 2:
|
|
153
195
|
body = _state.sent();
|
|
196
|
+
afterRenderHookContext = createAfterRenderContext(hookContext, body);
|
|
197
|
+
(_page_serverHooks2 = page.serverHooks) === null || _page_serverHooks2 === void 0 ? void 0 : (_page_serverHooks_afterRender = _page_serverHooks2.afterRender) === null || _page_serverHooks_afterRender === void 0 ? void 0 : _page_serverHooks_afterRender.call(_page_serverHooks2, afterRenderHookContext, function() {
|
|
198
|
+
return void 0;
|
|
199
|
+
});
|
|
200
|
+
if (checkIsSent(hookContext)) {
|
|
201
|
+
return [
|
|
202
|
+
2,
|
|
203
|
+
new ReturnResponse(hookContext.res.body || "Unkown body", hookContext.res.status, hookContext.res.headers)
|
|
204
|
+
];
|
|
205
|
+
}
|
|
154
206
|
return [
|
|
155
207
|
2,
|
|
156
|
-
new ReturnResponse(
|
|
208
|
+
new ReturnResponse(afterRenderHookContext.template.get(), responseLike.statusCode, responseLike.headers)
|
|
157
209
|
];
|
|
158
210
|
case 3:
|
|
159
211
|
e = _state.sent();
|
|
@@ -164,7 +216,7 @@ export var createHandler = function(manifest) {
|
|
|
164
216
|
createResponse(page.template)
|
|
165
217
|
];
|
|
166
218
|
case 4:
|
|
167
|
-
console.warn("Can't not page(".concat(
|
|
219
|
+
console.warn("Can't not page(".concat(entryName, ") serverRender"));
|
|
168
220
|
return [
|
|
169
221
|
2,
|
|
170
222
|
createResponse(page.template)
|
|
@@ -184,3 +236,41 @@ function createResponse(template) {
|
|
|
184
236
|
return RESPONSE_NOTFOUND;
|
|
185
237
|
}
|
|
186
238
|
}
|
|
239
|
+
function createWorkerHookContext(url, logger, metrics) {
|
|
240
|
+
var _ref = [
|
|
241
|
+
{
|
|
242
|
+
headers: new Headers(),
|
|
243
|
+
body: "",
|
|
244
|
+
status: 200,
|
|
245
|
+
isSent: false
|
|
246
|
+
},
|
|
247
|
+
new Request(url)
|
|
248
|
+
], res = _ref[0], req = _ref[1];
|
|
249
|
+
return {
|
|
250
|
+
res: res,
|
|
251
|
+
req: req,
|
|
252
|
+
logger: logger,
|
|
253
|
+
metrics: metrics
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
function applyMiddlewares(ctx, middleware) {
|
|
257
|
+
if (middleware) {
|
|
258
|
+
var middlewares = function() {
|
|
259
|
+
if (Array.isArray(middleware)) {
|
|
260
|
+
return middleware;
|
|
261
|
+
} else {
|
|
262
|
+
return [
|
|
263
|
+
middleware
|
|
264
|
+
];
|
|
265
|
+
}
|
|
266
|
+
}();
|
|
267
|
+
middlewares.forEach(function(middleware2) {
|
|
268
|
+
middlewarePipeline.use(middleware2);
|
|
269
|
+
});
|
|
270
|
+
middlewarePipeline.run(ctx, {
|
|
271
|
+
onLast: function() {
|
|
272
|
+
return void 0;
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
2
|
+
import cookie from "cookie";
|
|
3
|
+
export class BaseResponse {
|
|
4
|
+
get(key) {
|
|
5
|
+
return this.res.getHeader(key);
|
|
6
|
+
}
|
|
7
|
+
set(key, value) {
|
|
8
|
+
return this.res.setHeader(key, value);
|
|
9
|
+
}
|
|
10
|
+
status(code) {
|
|
11
|
+
this.res.statusCode = code;
|
|
12
|
+
}
|
|
13
|
+
setCookie(key, value, options) {
|
|
14
|
+
const cookieValue = String(this.res.getHeader("set-cookie") || "");
|
|
15
|
+
const fmt = Array.isArray(cookieValue) ? cookieValue : [
|
|
16
|
+
cookieValue
|
|
17
|
+
].filter(Boolean);
|
|
18
|
+
fmt.push(cookie.serialize(key, value, options));
|
|
19
|
+
this.res.setHeader("set-cookie", fmt.length === 1 ? fmt[0] : fmt);
|
|
20
|
+
}
|
|
21
|
+
clearCookie() {
|
|
22
|
+
this.res.removeHeader("set-cookie");
|
|
23
|
+
}
|
|
24
|
+
raw(body, options) {
|
|
25
|
+
const { status, headers = {} } = options || {};
|
|
26
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
27
|
+
this.res.setHeader(key, value);
|
|
28
|
+
});
|
|
29
|
+
if (status) {
|
|
30
|
+
this.res.statusCode = status;
|
|
31
|
+
}
|
|
32
|
+
this.res.end(body);
|
|
33
|
+
}
|
|
34
|
+
constructor(res) {
|
|
35
|
+
_define_property(this, "cookies", void 0);
|
|
36
|
+
_define_property(this, "res", void 0);
|
|
37
|
+
this.res = res;
|
|
38
|
+
this.cookies = {
|
|
39
|
+
set: this.setCookie.bind(this),
|
|
40
|
+
clear: this.clearCookie.bind(this)
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export class BaseRequest {
|
|
45
|
+
getCookie(key) {
|
|
46
|
+
return this._cookie[key];
|
|
47
|
+
}
|
|
48
|
+
constructor(request) {
|
|
49
|
+
_define_property(this, "url", void 0);
|
|
50
|
+
_define_property(this, "host", void 0);
|
|
51
|
+
_define_property(this, "pathname", void 0);
|
|
52
|
+
_define_property(this, "query", void 0);
|
|
53
|
+
_define_property(this, "headers", void 0);
|
|
54
|
+
_define_property(this, "cookie", void 0);
|
|
55
|
+
_define_property(this, "cookies", void 0);
|
|
56
|
+
_define_property(this, "_cookie", void 0);
|
|
57
|
+
this.url = request.url;
|
|
58
|
+
this.host = request.host;
|
|
59
|
+
this.pathname = request.path;
|
|
60
|
+
this.query = request.query;
|
|
61
|
+
this.headers = request.headers;
|
|
62
|
+
this.cookie = request.headers.cookie || "";
|
|
63
|
+
this._cookie = cookie.parse(this.cookie);
|
|
64
|
+
this.cookies = {
|
|
65
|
+
get: this.getCookie.bind(this)
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -1,78 +1,11 @@
|
|
|
1
|
-
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
2
|
-
import cookie from "cookie";
|
|
3
1
|
import { RouteAPI } from "./route";
|
|
4
2
|
import { TemplateAPI } from "./template";
|
|
5
|
-
|
|
6
|
-
get(key) {
|
|
7
|
-
return this.res.getHeader(key);
|
|
8
|
-
}
|
|
9
|
-
set(key, value) {
|
|
10
|
-
return this.res.setHeader(key, value);
|
|
11
|
-
}
|
|
12
|
-
status(code) {
|
|
13
|
-
this.res.statusCode = code;
|
|
14
|
-
}
|
|
15
|
-
setCookie(key, value, options) {
|
|
16
|
-
const cookieValue = this.res.getHeader("set-cookie");
|
|
17
|
-
const fmt = Array.isArray(cookieValue) ? cookieValue : [
|
|
18
|
-
cookieValue
|
|
19
|
-
].filter(Boolean);
|
|
20
|
-
fmt.push(cookie.serialize(key, value, options));
|
|
21
|
-
this.res.setHeader("set-cookie", fmt.length === 1 ? fmt[0] : fmt);
|
|
22
|
-
}
|
|
23
|
-
clearCookie() {
|
|
24
|
-
this.res.removeHeader("set-cookie");
|
|
25
|
-
}
|
|
26
|
-
raw(body, options) {
|
|
27
|
-
const { status, headers = {} } = options || {};
|
|
28
|
-
Object.entries(headers).forEach(([key, value]) => {
|
|
29
|
-
this.res.setHeader(key, value);
|
|
30
|
-
});
|
|
31
|
-
if (status) {
|
|
32
|
-
this.res.statusCode = status;
|
|
33
|
-
}
|
|
34
|
-
this.res.end(body);
|
|
35
|
-
}
|
|
36
|
-
constructor(res) {
|
|
37
|
-
_define_property(this, "cookies", void 0);
|
|
38
|
-
_define_property(this, "res", void 0);
|
|
39
|
-
this.res = res;
|
|
40
|
-
this.cookies = {
|
|
41
|
-
set: this.setCookie.bind(this),
|
|
42
|
-
clear: this.clearCookie.bind(this)
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
class Request {
|
|
47
|
-
getCookie(key) {
|
|
48
|
-
return this._cookie[key];
|
|
49
|
-
}
|
|
50
|
-
constructor(ctx) {
|
|
51
|
-
_define_property(this, "url", void 0);
|
|
52
|
-
_define_property(this, "host", void 0);
|
|
53
|
-
_define_property(this, "pathname", void 0);
|
|
54
|
-
_define_property(this, "query", void 0);
|
|
55
|
-
_define_property(this, "headers", void 0);
|
|
56
|
-
_define_property(this, "cookie", void 0);
|
|
57
|
-
_define_property(this, "cookies", void 0);
|
|
58
|
-
_define_property(this, "_cookie", void 0);
|
|
59
|
-
this.url = ctx.url;
|
|
60
|
-
this.host = ctx.host;
|
|
61
|
-
this.pathname = ctx.path;
|
|
62
|
-
this.query = ctx.query;
|
|
63
|
-
this.headers = ctx.headers;
|
|
64
|
-
this.cookie = ctx.headers.cookie || "";
|
|
65
|
-
this._cookie = cookie.parse(this.cookie);
|
|
66
|
-
this.cookies = {
|
|
67
|
-
get: this.getCookie.bind(this)
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
}
|
|
3
|
+
import { BaseRequest, BaseResponse } from "./base";
|
|
71
4
|
export const base = (context) => {
|
|
72
5
|
const { res } = context;
|
|
73
6
|
return {
|
|
74
|
-
response: new
|
|
75
|
-
request: new
|
|
7
|
+
response: new BaseResponse(res),
|
|
8
|
+
request: new BaseRequest(context),
|
|
76
9
|
logger: context.logger,
|
|
77
10
|
metrics: context.metrics
|
|
78
11
|
};
|
|
@@ -93,12 +26,10 @@ export const createAfterRenderContext = (context, content) => {
|
|
|
93
26
|
};
|
|
94
27
|
export const createMiddlewareContext = (context) => {
|
|
95
28
|
const baseContext = base(context);
|
|
29
|
+
baseContext.response.locals = context.res.locals || {};
|
|
96
30
|
return {
|
|
97
31
|
...baseContext,
|
|
98
|
-
response:
|
|
99
|
-
...baseContext.response,
|
|
100
|
-
locals: context.res.locals || {}
|
|
101
|
-
},
|
|
32
|
+
response: baseContext.response,
|
|
102
33
|
source: {
|
|
103
34
|
req: context.req,
|
|
104
35
|
res: context.res
|