@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
package/dist/cjs/workerServer.js
CHANGED
|
@@ -10,10 +10,16 @@ function _export(target, all) {
|
|
|
10
10
|
});
|
|
11
11
|
}
|
|
12
12
|
_export(exports, {
|
|
13
|
-
ReturnResponse: ()
|
|
14
|
-
|
|
13
|
+
ReturnResponse: function() {
|
|
14
|
+
return ReturnResponse;
|
|
15
|
+
},
|
|
16
|
+
createHandler: function() {
|
|
17
|
+
return createHandler;
|
|
18
|
+
}
|
|
15
19
|
});
|
|
16
20
|
const _define_property = require("@swc/helpers/_/_define_property");
|
|
21
|
+
const _plugin = require("@modern-js/plugin");
|
|
22
|
+
const _indexworker = require("./libs/hook-api/index.worker");
|
|
17
23
|
const _logger = require("./libs/logger");
|
|
18
24
|
const _route = require("./libs/route");
|
|
19
25
|
const _metrics = require("./libs/metrics");
|
|
@@ -59,20 +65,56 @@ class ReturnResponse {
|
|
|
59
65
|
}
|
|
60
66
|
}
|
|
61
67
|
const RESPONSE_NOTFOUND = new ReturnResponse("404: Page not found", 404);
|
|
68
|
+
const isRedirect = (code) => {
|
|
69
|
+
return [
|
|
70
|
+
301,
|
|
71
|
+
302,
|
|
72
|
+
307,
|
|
73
|
+
308
|
|
74
|
+
].includes(code);
|
|
75
|
+
};
|
|
76
|
+
const checkIsSent = (context) => {
|
|
77
|
+
if (context.res.isSent) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
if (context.res.headers.get("Location") && isRedirect(context.res.status)) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
};
|
|
85
|
+
const middlewarePipeline = (0, _plugin.createAsyncPipeline)();
|
|
62
86
|
const createHandler = (manifest) => {
|
|
63
87
|
const routeMgr = new _route.RouteMatchManager();
|
|
64
88
|
const { pages, routes } = manifest;
|
|
65
89
|
routeMgr.reset(routes);
|
|
66
90
|
return async (options) => {
|
|
91
|
+
var _page_serverHooks, _page_serverHooks_afterMatch;
|
|
67
92
|
const { request, loadableStats, routeManifest } = options;
|
|
68
93
|
const url = new URL(request.url);
|
|
69
94
|
const pageMatch = routeMgr.match(url.pathname);
|
|
70
95
|
if (!pageMatch) {
|
|
71
96
|
return RESPONSE_NOTFOUND;
|
|
72
97
|
}
|
|
73
|
-
const
|
|
98
|
+
const entryName = pageMatch.spec.urlPath;
|
|
99
|
+
const page = pages[entryName];
|
|
100
|
+
const logger = new _logger.Logger({
|
|
101
|
+
level: "warn"
|
|
102
|
+
});
|
|
103
|
+
const metrics = _metrics.metrics;
|
|
104
|
+
const hookContext = createWorkerHookContext(request.url, logger, metrics);
|
|
105
|
+
const afterMatchHookContext = (0, _indexworker.createAfterMatchContext)(hookContext, entryName);
|
|
106
|
+
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, () => void 0);
|
|
107
|
+
if (checkIsSent(hookContext)) {
|
|
108
|
+
return new ReturnResponse(hookContext.res.body || "Unkown body", hookContext.res.status, hookContext.res.headers);
|
|
109
|
+
}
|
|
74
110
|
if (page.serverRender) {
|
|
75
111
|
try {
|
|
112
|
+
var _page_serverHooks1, _page_serverHooks2, _page_serverHooks_afterRender;
|
|
113
|
+
const middlewarsHookContext = (0, _indexworker.createMiddlewareContext)(hookContext);
|
|
114
|
+
applyMiddlewares(middlewarsHookContext, (_page_serverHooks1 = page.serverHooks) === null || _page_serverHooks1 === void 0 ? void 0 : _page_serverHooks1.middleware);
|
|
115
|
+
if (checkIsSent(hookContext)) {
|
|
116
|
+
return new ReturnResponse(hookContext.res.body || "Unkown body", hookContext.res.status, hookContext.res.headers);
|
|
117
|
+
}
|
|
76
118
|
const responseLike = {
|
|
77
119
|
headers: {},
|
|
78
120
|
statusCode: 200,
|
|
@@ -94,23 +136,26 @@ const createHandler = (manifest) => {
|
|
|
94
136
|
redirection: {},
|
|
95
137
|
template: page.template,
|
|
96
138
|
entryName: page.entryName,
|
|
97
|
-
logger
|
|
98
|
-
|
|
99
|
-
}),
|
|
100
|
-
metrics: _metrics.metrics,
|
|
139
|
+
logger,
|
|
140
|
+
metrics,
|
|
101
141
|
// FIXME: pass correctly req & res
|
|
102
142
|
req: request,
|
|
103
143
|
res: responseLike
|
|
104
144
|
};
|
|
105
145
|
const body = await page.serverRender(serverRenderContext);
|
|
106
|
-
|
|
146
|
+
const afterRenderHookContext = (0, _indexworker.createAfterRenderContext)(hookContext, body);
|
|
147
|
+
(_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, () => void 0);
|
|
148
|
+
if (checkIsSent(hookContext)) {
|
|
149
|
+
return new ReturnResponse(hookContext.res.body || "Unkown body", hookContext.res.status, hookContext.res.headers);
|
|
150
|
+
}
|
|
151
|
+
return new ReturnResponse(afterRenderHookContext.template.get(), responseLike.statusCode, responseLike.headers);
|
|
107
152
|
} catch (e) {
|
|
108
153
|
console.warn(`page(${pageMatch.spec.urlPath}) serverRender occur error: `);
|
|
109
154
|
console.warn(e);
|
|
110
155
|
return createResponse(page.template);
|
|
111
156
|
}
|
|
112
157
|
}
|
|
113
|
-
console.warn(`Can't not page(${
|
|
158
|
+
console.warn(`Can't not page(${entryName}) serverRender`);
|
|
114
159
|
return createResponse(page.template);
|
|
115
160
|
function createServerRequest(url2, baseUrl, request2, params) {
|
|
116
161
|
const { pathname, host, searchParams } = url2;
|
|
@@ -139,3 +184,39 @@ function createResponse(template) {
|
|
|
139
184
|
return RESPONSE_NOTFOUND;
|
|
140
185
|
}
|
|
141
186
|
}
|
|
187
|
+
function createWorkerHookContext(url, logger, metrics) {
|
|
188
|
+
const [res, req] = [
|
|
189
|
+
{
|
|
190
|
+
headers: new Headers(),
|
|
191
|
+
body: "",
|
|
192
|
+
status: 200,
|
|
193
|
+
isSent: false
|
|
194
|
+
},
|
|
195
|
+
new Request(url)
|
|
196
|
+
];
|
|
197
|
+
return {
|
|
198
|
+
res,
|
|
199
|
+
req,
|
|
200
|
+
logger,
|
|
201
|
+
metrics
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
function applyMiddlewares(ctx, middleware) {
|
|
205
|
+
if (middleware) {
|
|
206
|
+
const middlewares = (() => {
|
|
207
|
+
if (Array.isArray(middleware)) {
|
|
208
|
+
return middleware;
|
|
209
|
+
} else {
|
|
210
|
+
return [
|
|
211
|
+
middleware
|
|
212
|
+
];
|
|
213
|
+
}
|
|
214
|
+
})();
|
|
215
|
+
middlewares.forEach((middleware2) => {
|
|
216
|
+
middlewarePipeline.use(middleware2);
|
|
217
|
+
});
|
|
218
|
+
middlewarePipeline.run(ctx, {
|
|
219
|
+
onLast: () => void 0
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
2
|
+
import { _ as _create_class } from "@swc/helpers/_/_create_class";
|
|
3
|
+
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
4
|
+
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
5
|
+
import cookie from "cookie";
|
|
6
|
+
export var BaseResponse = /* @__PURE__ */ function() {
|
|
7
|
+
"use strict";
|
|
8
|
+
function BaseResponse2(res) {
|
|
9
|
+
_class_call_check(this, BaseResponse2);
|
|
10
|
+
_define_property(this, "cookies", void 0);
|
|
11
|
+
_define_property(this, "res", void 0);
|
|
12
|
+
this.res = res;
|
|
13
|
+
this.cookies = {
|
|
14
|
+
set: this.setCookie.bind(this),
|
|
15
|
+
clear: this.clearCookie.bind(this)
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
_create_class(BaseResponse2, [
|
|
19
|
+
{
|
|
20
|
+
key: "get",
|
|
21
|
+
value: function get(key) {
|
|
22
|
+
return this.res.getHeader(key);
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
key: "set",
|
|
27
|
+
value: function set(key, value) {
|
|
28
|
+
return this.res.setHeader(key, value);
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
key: "status",
|
|
33
|
+
value: function status(code) {
|
|
34
|
+
this.res.statusCode = code;
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
key: "setCookie",
|
|
39
|
+
value: function setCookie(key, value, options) {
|
|
40
|
+
var cookieValue = String(this.res.getHeader("set-cookie") || "");
|
|
41
|
+
var fmt = Array.isArray(cookieValue) ? cookieValue : [
|
|
42
|
+
cookieValue
|
|
43
|
+
].filter(Boolean);
|
|
44
|
+
fmt.push(cookie.serialize(key, value, options));
|
|
45
|
+
this.res.setHeader("set-cookie", fmt.length === 1 ? fmt[0] : fmt);
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
key: "clearCookie",
|
|
50
|
+
value: function clearCookie() {
|
|
51
|
+
this.res.removeHeader("set-cookie");
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
key: "raw",
|
|
56
|
+
value: function raw(body, options) {
|
|
57
|
+
var _this = this;
|
|
58
|
+
var _ref = options || {}, status = _ref.status, _ref_headers = _ref.headers, headers = _ref_headers === void 0 ? {} : _ref_headers;
|
|
59
|
+
Object.entries(headers).forEach(function(param) {
|
|
60
|
+
var _param = _sliced_to_array(param, 2), key = _param[0], value = _param[1];
|
|
61
|
+
_this.res.setHeader(key, value);
|
|
62
|
+
});
|
|
63
|
+
if (status) {
|
|
64
|
+
this.res.statusCode = status;
|
|
65
|
+
}
|
|
66
|
+
this.res.end(body);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
]);
|
|
70
|
+
return BaseResponse2;
|
|
71
|
+
}();
|
|
72
|
+
export var BaseRequest = /* @__PURE__ */ function() {
|
|
73
|
+
"use strict";
|
|
74
|
+
function BaseRequest2(request) {
|
|
75
|
+
_class_call_check(this, BaseRequest2);
|
|
76
|
+
_define_property(this, "url", void 0);
|
|
77
|
+
_define_property(this, "host", void 0);
|
|
78
|
+
_define_property(this, "pathname", void 0);
|
|
79
|
+
_define_property(this, "query", void 0);
|
|
80
|
+
_define_property(this, "headers", void 0);
|
|
81
|
+
_define_property(this, "cookie", void 0);
|
|
82
|
+
_define_property(this, "cookies", void 0);
|
|
83
|
+
_define_property(this, "_cookie", void 0);
|
|
84
|
+
this.url = request.url;
|
|
85
|
+
this.host = request.host;
|
|
86
|
+
this.pathname = request.path;
|
|
87
|
+
this.query = request.query;
|
|
88
|
+
this.headers = request.headers;
|
|
89
|
+
this.cookie = request.headers.cookie || "";
|
|
90
|
+
this._cookie = cookie.parse(this.cookie);
|
|
91
|
+
this.cookies = {
|
|
92
|
+
get: this.getCookie.bind(this)
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
_create_class(BaseRequest2, [
|
|
96
|
+
{
|
|
97
|
+
key: "getCookie",
|
|
98
|
+
value: function getCookie(key) {
|
|
99
|
+
return this._cookie[key];
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
]);
|
|
103
|
+
return BaseRequest2;
|
|
104
|
+
}();
|
|
@@ -1,116 +1,13 @@
|
|
|
1
|
-
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
2
|
-
import { _ as _create_class } from "@swc/helpers/_/_create_class";
|
|
3
|
-
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
4
1
|
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
5
2
|
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
6
|
-
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
7
|
-
import cookie from "cookie";
|
|
8
3
|
import { RouteAPI } from "./route";
|
|
9
4
|
import { TemplateAPI } from "./template";
|
|
10
|
-
|
|
11
|
-
"use strict";
|
|
12
|
-
function Response2(res) {
|
|
13
|
-
_class_call_check(this, Response2);
|
|
14
|
-
_define_property(this, "cookies", void 0);
|
|
15
|
-
_define_property(this, "res", void 0);
|
|
16
|
-
this.res = res;
|
|
17
|
-
this.cookies = {
|
|
18
|
-
set: this.setCookie.bind(this),
|
|
19
|
-
clear: this.clearCookie.bind(this)
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
_create_class(Response2, [
|
|
23
|
-
{
|
|
24
|
-
key: "get",
|
|
25
|
-
value: function get(key) {
|
|
26
|
-
return this.res.getHeader(key);
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
key: "set",
|
|
31
|
-
value: function set(key, value) {
|
|
32
|
-
return this.res.setHeader(key, value);
|
|
33
|
-
}
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
key: "status",
|
|
37
|
-
value: function status(code) {
|
|
38
|
-
this.res.statusCode = code;
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
key: "setCookie",
|
|
43
|
-
value: function setCookie(key, value, options) {
|
|
44
|
-
var cookieValue = this.res.getHeader("set-cookie");
|
|
45
|
-
var fmt = Array.isArray(cookieValue) ? cookieValue : [
|
|
46
|
-
cookieValue
|
|
47
|
-
].filter(Boolean);
|
|
48
|
-
fmt.push(cookie.serialize(key, value, options));
|
|
49
|
-
this.res.setHeader("set-cookie", fmt.length === 1 ? fmt[0] : fmt);
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
|
-
{
|
|
53
|
-
key: "clearCookie",
|
|
54
|
-
value: function clearCookie() {
|
|
55
|
-
this.res.removeHeader("set-cookie");
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
{
|
|
59
|
-
key: "raw",
|
|
60
|
-
value: function raw(body, options) {
|
|
61
|
-
var _this = this;
|
|
62
|
-
var _ref = options || {}, status = _ref.status, _ref_headers = _ref.headers, headers = _ref_headers === void 0 ? {} : _ref_headers;
|
|
63
|
-
Object.entries(headers).forEach(function(param) {
|
|
64
|
-
var _param = _sliced_to_array(param, 2), key = _param[0], value = _param[1];
|
|
65
|
-
_this.res.setHeader(key, value);
|
|
66
|
-
});
|
|
67
|
-
if (status) {
|
|
68
|
-
this.res.statusCode = status;
|
|
69
|
-
}
|
|
70
|
-
this.res.end(body);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
]);
|
|
74
|
-
return Response2;
|
|
75
|
-
}();
|
|
76
|
-
var Request = /* @__PURE__ */ function() {
|
|
77
|
-
"use strict";
|
|
78
|
-
function Request2(ctx) {
|
|
79
|
-
_class_call_check(this, Request2);
|
|
80
|
-
_define_property(this, "url", void 0);
|
|
81
|
-
_define_property(this, "host", void 0);
|
|
82
|
-
_define_property(this, "pathname", void 0);
|
|
83
|
-
_define_property(this, "query", void 0);
|
|
84
|
-
_define_property(this, "headers", void 0);
|
|
85
|
-
_define_property(this, "cookie", void 0);
|
|
86
|
-
_define_property(this, "cookies", void 0);
|
|
87
|
-
_define_property(this, "_cookie", void 0);
|
|
88
|
-
this.url = ctx.url;
|
|
89
|
-
this.host = ctx.host;
|
|
90
|
-
this.pathname = ctx.path;
|
|
91
|
-
this.query = ctx.query;
|
|
92
|
-
this.headers = ctx.headers;
|
|
93
|
-
this.cookie = ctx.headers.cookie || "";
|
|
94
|
-
this._cookie = cookie.parse(this.cookie);
|
|
95
|
-
this.cookies = {
|
|
96
|
-
get: this.getCookie.bind(this)
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
_create_class(Request2, [
|
|
100
|
-
{
|
|
101
|
-
key: "getCookie",
|
|
102
|
-
value: function getCookie(key) {
|
|
103
|
-
return this._cookie[key];
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
]);
|
|
107
|
-
return Request2;
|
|
108
|
-
}();
|
|
5
|
+
import { BaseRequest, BaseResponse } from "./base";
|
|
109
6
|
export var base = function(context) {
|
|
110
7
|
var res = context.res;
|
|
111
8
|
return {
|
|
112
|
-
response: new
|
|
113
|
-
request: new
|
|
9
|
+
response: new BaseResponse(res),
|
|
10
|
+
request: new BaseRequest(context),
|
|
114
11
|
logger: context.logger,
|
|
115
12
|
metrics: context.metrics
|
|
116
13
|
};
|
|
@@ -129,10 +26,9 @@ export var createAfterRenderContext = function(context, content) {
|
|
|
129
26
|
};
|
|
130
27
|
export var createMiddlewareContext = function(context) {
|
|
131
28
|
var baseContext = base(context);
|
|
29
|
+
baseContext.response.locals = context.res.locals || {};
|
|
132
30
|
return _object_spread_props(_object_spread({}, baseContext), {
|
|
133
|
-
response:
|
|
134
|
-
locals: context.res.locals || {}
|
|
135
|
-
}),
|
|
31
|
+
response: baseContext.response,
|
|
136
32
|
source: {
|
|
137
33
|
req: context.req,
|
|
138
34
|
res: context.res
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
2
|
+
import { _ as _create_class } from "@swc/helpers/_/_create_class";
|
|
3
|
+
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
4
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
5
|
+
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
6
|
+
import { BaseRequest, BaseResponse } from "./base";
|
|
7
|
+
import { RouteAPI } from "./route";
|
|
8
|
+
import { TemplateAPI } from "./template";
|
|
9
|
+
var ServerResponse = /* @__PURE__ */ function() {
|
|
10
|
+
"use strict";
|
|
11
|
+
function ServerResponse2(res) {
|
|
12
|
+
_class_call_check(this, ServerResponse2);
|
|
13
|
+
_define_property(this, "locals", void 0);
|
|
14
|
+
_define_property(this, "res", void 0);
|
|
15
|
+
this.res = res;
|
|
16
|
+
this.locals = res.locals;
|
|
17
|
+
}
|
|
18
|
+
_create_class(ServerResponse2, [
|
|
19
|
+
{
|
|
20
|
+
key: "statusCode",
|
|
21
|
+
get: function get() {
|
|
22
|
+
return this.res.status;
|
|
23
|
+
},
|
|
24
|
+
set: function set(code) {
|
|
25
|
+
this.res.status = code;
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
key: "getHeader",
|
|
30
|
+
value: function getHeader(key) {
|
|
31
|
+
var _this_res_headers_get;
|
|
32
|
+
return (_this_res_headers_get = this.res.headers.get(key)) !== null && _this_res_headers_get !== void 0 ? _this_res_headers_get : void 0;
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
key: "setHeader",
|
|
37
|
+
value: function setHeader(key, value) {
|
|
38
|
+
this.res.headers.set(key, value);
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
key: "removeHeader",
|
|
43
|
+
value: function removeHeader(key) {
|
|
44
|
+
this.res.headers.delete(key);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
key: "end",
|
|
49
|
+
value: function end(body) {
|
|
50
|
+
this.res.body = body;
|
|
51
|
+
this.res.isSent = true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
]);
|
|
55
|
+
return ServerResponse2;
|
|
56
|
+
}();
|
|
57
|
+
export var base = function(context) {
|
|
58
|
+
var req = context.req, res = context.res, logger = context.logger, metrics = context.metrics;
|
|
59
|
+
var serverResponse = new ServerResponse(res);
|
|
60
|
+
var _ref = new URL(req.url), host = _ref.host, pathname = _ref.pathname, searchParams = _ref.searchParams;
|
|
61
|
+
var headers = {};
|
|
62
|
+
req.headers.forEach(function(value, key) {
|
|
63
|
+
headers[key] = value;
|
|
64
|
+
});
|
|
65
|
+
return {
|
|
66
|
+
response: new BaseResponse(serverResponse),
|
|
67
|
+
request: new BaseRequest({
|
|
68
|
+
url: req.url,
|
|
69
|
+
host: host,
|
|
70
|
+
path: pathname,
|
|
71
|
+
headers: headers,
|
|
72
|
+
// eslint-disable-next-line node/no-unsupported-features/es-builtins
|
|
73
|
+
query: Object.fromEntries(searchParams)
|
|
74
|
+
}),
|
|
75
|
+
logger: logger,
|
|
76
|
+
metrics: metrics
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
export var createAfterMatchContext = function(context, entryName) {
|
|
80
|
+
var baseContext = base(context);
|
|
81
|
+
return _object_spread_props(_object_spread({}, baseContext), {
|
|
82
|
+
router: new RouteAPI(entryName)
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
export var createAfterRenderContext = function(context, content) {
|
|
86
|
+
var baseContext = base(context);
|
|
87
|
+
return _object_spread_props(_object_spread({}, baseContext), {
|
|
88
|
+
template: new TemplateAPI(content)
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
export var createMiddlewareContext = function(context) {
|
|
92
|
+
var baseContext = base(context);
|
|
93
|
+
baseContext.response.locals = context.res.locals;
|
|
94
|
+
var _context_req = context.req, url = _context_req.url, headers = _context_req.headers;
|
|
95
|
+
var rawRequest = new Request(url, {
|
|
96
|
+
headers: headers
|
|
97
|
+
});
|
|
98
|
+
return _object_spread_props(_object_spread({}, baseContext), {
|
|
99
|
+
response: baseContext.response,
|
|
100
|
+
source: {
|
|
101
|
+
req: rawRequest,
|
|
102
|
+
res: baseContext.response
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
};
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
2
2
|
import { _ as _create_class } from "@swc/helpers/_/_create_class";
|
|
3
3
|
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
4
|
-
|
|
5
|
-
var RegList = {
|
|
4
|
+
export var RegList = {
|
|
6
5
|
before: {
|
|
7
6
|
head: "<head>",
|
|
8
7
|
body: "<body>"
|
|
@@ -69,29 +68,3 @@ export var TemplateAPI = /* @__PURE__ */ function() {
|
|
|
69
68
|
]);
|
|
70
69
|
return TemplateAPI2;
|
|
71
70
|
}();
|
|
72
|
-
export var templateInjectableStream = function(param) {
|
|
73
|
-
var prependHead = param.prependHead, appendHead = param.appendHead, prependBody = param.prependBody, appendBody = param.appendBody;
|
|
74
|
-
return new Transform({
|
|
75
|
-
write: function write(chunk, _, callback) {
|
|
76
|
-
var chunk_str = chunk.toString();
|
|
77
|
-
if (prependHead) {
|
|
78
|
-
var head = RegList.before.head;
|
|
79
|
-
chunk_str = chunk_str.replace(head, "".concat(head).concat(prependHead));
|
|
80
|
-
}
|
|
81
|
-
if (appendHead) {
|
|
82
|
-
var head1 = RegList.after.head;
|
|
83
|
-
chunk_str = chunk_str.replace(head1, "".concat(appendHead).concat(head1));
|
|
84
|
-
}
|
|
85
|
-
if (prependBody) {
|
|
86
|
-
var body = RegList.before.body;
|
|
87
|
-
chunk_str = chunk_str.replace(body, "".concat(body).concat(prependBody));
|
|
88
|
-
}
|
|
89
|
-
if (appendBody) {
|
|
90
|
-
var body1 = RegList.after.body;
|
|
91
|
-
chunk_str = chunk_str.replace(body1, "".concat(appendBody).concat(body1));
|
|
92
|
-
}
|
|
93
|
-
this.push(chunk_str);
|
|
94
|
-
callback();
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Transform } from "stream";
|
|
2
|
+
import { RegList } from "./template";
|
|
3
|
+
export var templateInjectableStream = function(param) {
|
|
4
|
+
var prependHead = param.prependHead, appendHead = param.appendHead, prependBody = param.prependBody, appendBody = param.appendBody;
|
|
5
|
+
return new Transform({
|
|
6
|
+
write: function write(chunk, _, callback) {
|
|
7
|
+
var chunk_str = chunk.toString();
|
|
8
|
+
if (prependHead) {
|
|
9
|
+
var head = RegList.before.head;
|
|
10
|
+
chunk_str = chunk_str.replace(head, "".concat(head).concat(prependHead));
|
|
11
|
+
}
|
|
12
|
+
if (appendHead) {
|
|
13
|
+
var head1 = RegList.after.head;
|
|
14
|
+
chunk_str = chunk_str.replace(head1, "".concat(appendHead).concat(head1));
|
|
15
|
+
}
|
|
16
|
+
if (prependBody) {
|
|
17
|
+
var body = RegList.before.body;
|
|
18
|
+
chunk_str = chunk_str.replace(body, "".concat(body).concat(prependBody));
|
|
19
|
+
}
|
|
20
|
+
if (appendBody) {
|
|
21
|
+
var body1 = RegList.after.body;
|
|
22
|
+
chunk_str = chunk_str.replace(body1, "".concat(appendBody).concat(body1));
|
|
23
|
+
}
|
|
24
|
+
this.push(chunk_str);
|
|
25
|
+
callback();
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
2
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
3
3
|
import path from "path";
|
|
4
|
-
import {
|
|
4
|
+
import { mime } from "@modern-js/utils";
|
|
5
5
|
import { ERROR_DIGEST } from "../../constants";
|
|
6
6
|
import { handleDirectory } from "./static";
|
|
7
7
|
import { readFile } from "./reader";
|
|
@@ -41,9 +41,6 @@ export var createRenderHandler = function(param) {
|
|
|
41
41
|
];
|
|
42
42
|
case 2:
|
|
43
43
|
templatePath = entry;
|
|
44
|
-
if (!fs.existsSync(templatePath)) {
|
|
45
|
-
throw new Error("Could not find template file: ".concat(templatePath));
|
|
46
|
-
}
|
|
47
44
|
return [
|
|
48
45
|
4,
|
|
49
46
|
readFile(templatePath)
|
|
@@ -3,7 +3,7 @@ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
|
3
3
|
import { _ as _create_class } from "@swc/helpers/_/_create_class";
|
|
4
4
|
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
5
5
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
6
|
-
import { fs } from "@modern-js/utils";
|
|
6
|
+
import { fs as extraFS } from "@modern-js/utils";
|
|
7
7
|
import LRU from "lru-cache";
|
|
8
8
|
var Byte = 1;
|
|
9
9
|
var KB = 1024 * Byte;
|
|
@@ -12,28 +12,18 @@ var getContentLength = function(cache) {
|
|
|
12
12
|
return cache.content.length;
|
|
13
13
|
};
|
|
14
14
|
var createCacheItem = function() {
|
|
15
|
-
var _ref = _async_to_generator(function(
|
|
16
|
-
var content;
|
|
15
|
+
var _ref = _async_to_generator(function(content, mtime) {
|
|
17
16
|
return _ts_generator(this, function(_state) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
content = _state.sent();
|
|
26
|
-
return [
|
|
27
|
-
2,
|
|
28
|
-
{
|
|
29
|
-
content: content,
|
|
30
|
-
mtime: mtime
|
|
31
|
-
}
|
|
32
|
-
];
|
|
33
|
-
}
|
|
17
|
+
return [
|
|
18
|
+
2,
|
|
19
|
+
{
|
|
20
|
+
content: content,
|
|
21
|
+
mtime: mtime
|
|
22
|
+
}
|
|
23
|
+
];
|
|
34
24
|
});
|
|
35
25
|
});
|
|
36
|
-
return function createCacheItem2(
|
|
26
|
+
return function createCacheItem2(content, mtime) {
|
|
37
27
|
return _ref.apply(this, arguments);
|
|
38
28
|
};
|
|
39
29
|
}();
|
|
@@ -42,16 +32,19 @@ export var LruReader = /* @__PURE__ */ function() {
|
|
|
42
32
|
function LruReader2() {
|
|
43
33
|
_class_call_check(this, LruReader2);
|
|
44
34
|
_define_property(this, "cache", void 0);
|
|
35
|
+
_define_property(this, "fs", void 0);
|
|
45
36
|
this.cache = new LRU({
|
|
46
37
|
max: 256 * MB,
|
|
47
38
|
length: getContentLength,
|
|
48
39
|
maxAge: 5 * 60 * 5e3
|
|
49
40
|
});
|
|
41
|
+
this.fs = extraFS;
|
|
50
42
|
}
|
|
51
43
|
_create_class(LruReader2, [
|
|
52
44
|
{
|
|
53
45
|
key: "init",
|
|
54
|
-
value: function init2() {
|
|
46
|
+
value: function init2(fs) {
|
|
47
|
+
this.fs = fs || extraFS;
|
|
55
48
|
}
|
|
56
49
|
},
|
|
57
50
|
{
|
|
@@ -64,10 +57,11 @@ export var LruReader = /* @__PURE__ */ function() {
|
|
|
64
57
|
value: function read(filepath) {
|
|
65
58
|
var _this = this;
|
|
66
59
|
return _async_to_generator(function() {
|
|
67
|
-
var content, stat, item;
|
|
60
|
+
var fs, content, stat, content1, item;
|
|
68
61
|
return _ts_generator(this, function(_state) {
|
|
69
62
|
switch (_state.label) {
|
|
70
63
|
case 0:
|
|
64
|
+
fs = _this.fs;
|
|
71
65
|
if (_this.cache.has(filepath)) {
|
|
72
66
|
content = _this.cache.get(filepath).content;
|
|
73
67
|
return [
|
|
@@ -98,9 +92,15 @@ export var LruReader = /* @__PURE__ */ function() {
|
|
|
98
92
|
}
|
|
99
93
|
return [
|
|
100
94
|
4,
|
|
101
|
-
|
|
95
|
+
fs.promises.readFile(filepath)
|
|
102
96
|
];
|
|
103
97
|
case 1:
|
|
98
|
+
content1 = _state.sent();
|
|
99
|
+
return [
|
|
100
|
+
4,
|
|
101
|
+
createCacheItem(content1, stat.mtime)
|
|
102
|
+
];
|
|
103
|
+
case 2:
|
|
104
104
|
item = _state.sent();
|
|
105
105
|
_this.cache.set(filepath, item);
|
|
106
106
|
return [
|
|
@@ -115,7 +115,7 @@ export var LruReader = /* @__PURE__ */ function() {
|
|
|
115
115
|
{
|
|
116
116
|
key: "update",
|
|
117
117
|
value: function update() {
|
|
118
|
-
var cache =
|
|
118
|
+
var _this = this, cache = _this.cache, fs = _this.fs;
|
|
119
119
|
var files = cache.keys();
|
|
120
120
|
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
121
121
|
try {
|
|
@@ -181,8 +181,8 @@ export var readFile = function() {
|
|
|
181
181
|
export var updateFile = function() {
|
|
182
182
|
reader.update();
|
|
183
183
|
};
|
|
184
|
-
export var init = function() {
|
|
185
|
-
reader.init();
|
|
184
|
+
export var init = function(fs) {
|
|
185
|
+
reader.init(fs);
|
|
186
186
|
};
|
|
187
187
|
export var close = function() {
|
|
188
188
|
reader.close();
|