@modern-js/prod-server 2.35.0 → 2.35.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/dist/cjs/libs/context/context.js +29 -5
- package/dist/cjs/libs/preload/flushServerHeader.js +2 -2
- package/dist/cjs/libs/preload/parseLinks.js +8 -9
- package/dist/cjs/libs/preload/transformLinks2String.js +1 -2
- package/dist/cjs/libs/render/cache/__tests__/cache.test.js +7 -12
- package/dist/cjs/libs/render/cache/index.js +1 -2
- package/dist/cjs/libs/render/cache/spr.js +1 -2
- package/dist/cjs/libs/render/index.js +3 -2
- package/dist/cjs/libs/render/reader.js +1 -2
- package/dist/cjs/libs/route/matcher.js +2 -2
- package/dist/cjs/server/index.js +5 -7
- package/dist/cjs/server/modernServer.js +25 -27
- package/dist/cjs/workerServer.js +5 -6
- package/dist/esm/libs/context/context.js +29 -5
- package/dist/esm/libs/preload/flushServerHeader.js +3 -3
- package/dist/esm/libs/preload/parseLinks.js +7 -8
- package/dist/esm/libs/preload/transformLinks2String.js +1 -2
- package/dist/esm/libs/render/cache/__tests__/cache.test.js +12 -12
- package/dist/esm/libs/render/cache/index.js +2 -2
- package/dist/esm/libs/render/cache/spr.js +2 -2
- package/dist/esm/libs/render/index.js +5 -2
- package/dist/esm/libs/render/reader.js +2 -2
- package/dist/esm/libs/route/matcher.js +2 -2
- package/dist/esm/server/index.js +6 -8
- package/dist/esm/server/modernServer.js +26 -37
- package/dist/esm/workerServer.js +5 -6
- package/dist/esm-node/libs/context/context.js +29 -5
- package/dist/esm-node/libs/preload/flushServerHeader.js +2 -2
- package/dist/esm-node/libs/preload/parseLinks.js +7 -8
- package/dist/esm-node/libs/preload/transformLinks2String.js +1 -2
- package/dist/esm-node/libs/render/cache/__tests__/cache.test.js +7 -12
- package/dist/esm-node/libs/render/cache/index.js +1 -2
- package/dist/esm-node/libs/render/cache/spr.js +1 -2
- package/dist/esm-node/libs/render/index.js +3 -2
- package/dist/esm-node/libs/render/reader.js +1 -2
- package/dist/esm-node/libs/route/matcher.js +2 -2
- package/dist/esm-node/server/index.js +6 -8
- package/dist/esm-node/server/modernServer.js +25 -27
- package/dist/esm-node/workerServer.js +5 -6
- package/dist/types/libs/context/context.d.ts +2 -1
- package/dist/types/type.d.ts +1 -0
- package/dist/types/utils.d.ts +1 -1
- package/package.json +9 -8
- package/dist/cjs/libs/logger.js +0 -123
- package/dist/esm/libs/logger.js +0 -125
- package/dist/esm-node/libs/logger.js +0 -106
- package/dist/types/libs/logger.d.ts +0 -61
|
@@ -8,6 +8,9 @@ Object.defineProperty(exports, "ModernServerContext", {
|
|
|
8
8
|
return ModernServerContext;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
+
const _class_private_field_get = require("@swc/helpers/_/_class_private_field_get");
|
|
12
|
+
const _class_private_field_init = require("@swc/helpers/_/_class_private_field_init");
|
|
13
|
+
const _class_private_field_set = require("@swc/helpers/_/_class_private_field_set");
|
|
11
14
|
const _define_property = require("@swc/helpers/_/_define_property");
|
|
12
15
|
const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
|
|
13
16
|
const _url = require("url");
|
|
@@ -20,6 +23,7 @@ const _serverTiming = require("../serverTiming");
|
|
|
20
23
|
const _reporter = require("../reporter");
|
|
21
24
|
const _utils1 = require("../../utils");
|
|
22
25
|
const MOCK_URL_BASE = "https://modernjs.dev/";
|
|
26
|
+
var _urls = /* @__PURE__ */ new WeakMap(), _queries = /* @__PURE__ */ new WeakMap();
|
|
23
27
|
class ModernServerContext {
|
|
24
28
|
get logger() {
|
|
25
29
|
return this.req.logger;
|
|
@@ -29,7 +33,12 @@ class ModernServerContext {
|
|
|
29
33
|
}
|
|
30
34
|
get parsedURL() {
|
|
31
35
|
try {
|
|
32
|
-
|
|
36
|
+
let url = _class_private_field_get._(this, _urls).get(this.req.url);
|
|
37
|
+
if (!url) {
|
|
38
|
+
url = new _url.URL(this.req.url, MOCK_URL_BASE);
|
|
39
|
+
_class_private_field_get._(this, _urls).set(this.req.url, url);
|
|
40
|
+
}
|
|
41
|
+
return url;
|
|
33
42
|
} catch (e) {
|
|
34
43
|
this.logger.error("Parse URL error", e.stack || e.message);
|
|
35
44
|
return new _url.URL("/_modern_mock_path", MOCK_URL_BASE);
|
|
@@ -118,7 +127,8 @@ class ModernServerContext {
|
|
|
118
127
|
if (!host) {
|
|
119
128
|
host = this.getReqHeader("Host");
|
|
120
129
|
}
|
|
121
|
-
|
|
130
|
+
host = host.split(/\s*,\s*/, 1)[0] || "undefined";
|
|
131
|
+
return host;
|
|
122
132
|
}
|
|
123
133
|
get protocol() {
|
|
124
134
|
if (this.req.socket.encrypted) {
|
|
@@ -153,7 +163,12 @@ class ModernServerContext {
|
|
|
153
163
|
}
|
|
154
164
|
get query() {
|
|
155
165
|
const str = this.querystring;
|
|
156
|
-
|
|
166
|
+
let query = _class_private_field_get._(this, _queries).get(str);
|
|
167
|
+
if (!query) {
|
|
168
|
+
query = _querystring.default.parse(str);
|
|
169
|
+
_class_private_field_get._(this, _queries).set(str, query);
|
|
170
|
+
}
|
|
171
|
+
return query;
|
|
157
172
|
}
|
|
158
173
|
/* response property */
|
|
159
174
|
get status() {
|
|
@@ -172,7 +187,6 @@ class ModernServerContext {
|
|
|
172
187
|
this.logger.error(`Web Server Error - ${dig}, error = %s, req.url = %s, req.headers = %o`, e instanceof Error ? e.stack || e.message : e, this.path, (0, _utils1.headersWithoutCookie)(this.headers));
|
|
173
188
|
}
|
|
174
189
|
constructor(req, res, options) {
|
|
175
|
-
var _options;
|
|
176
190
|
_define_property._(this, "req", void 0);
|
|
177
191
|
_define_property._(this, "res", void 0);
|
|
178
192
|
_define_property._(this, "params", {});
|
|
@@ -180,10 +194,20 @@ class ModernServerContext {
|
|
|
180
194
|
_define_property._(this, "serverTiming", void 0);
|
|
181
195
|
_define_property._(this, "serverData", {});
|
|
182
196
|
_define_property._(this, "options", {});
|
|
197
|
+
_class_private_field_init._(this, _urls, {
|
|
198
|
+
writable: true,
|
|
199
|
+
value: void 0
|
|
200
|
+
});
|
|
201
|
+
_class_private_field_init._(this, _queries, {
|
|
202
|
+
writable: true,
|
|
203
|
+
value: void 0
|
|
204
|
+
});
|
|
183
205
|
this.req = req;
|
|
184
206
|
this.res = res;
|
|
185
207
|
this.options = options || {};
|
|
186
208
|
this.bind();
|
|
187
|
-
|
|
209
|
+
_class_private_field_set._(this, _urls, /* @__PURE__ */ new Map());
|
|
210
|
+
_class_private_field_set._(this, _queries, /* @__PURE__ */ new Map());
|
|
211
|
+
this.serverTiming = new _serverTiming.ServerTiming(this.res, (0, _utils.cutNameByHyphen)((options === null || options === void 0 ? void 0 : options.metaName) || "modern-js"));
|
|
188
212
|
}
|
|
189
213
|
}
|
|
@@ -24,9 +24,9 @@ async function flushServerHeader({ serverConf, ctx, distDir, template, headers }
|
|
|
24
24
|
const link = (0, _transformLinks2String.transformLinks2String)(links, ssrConf.preload);
|
|
25
25
|
res.set("link", link);
|
|
26
26
|
for (const key in headers || {}) {
|
|
27
|
-
|
|
28
|
-
const value = (_headers = headers) === null || _headers === void 0 ? void 0 : _headers[key];
|
|
27
|
+
const value = headers === null || headers === void 0 ? void 0 : headers[key];
|
|
29
28
|
value && res.set(key, value);
|
|
30
29
|
}
|
|
31
30
|
res.flushHeaders();
|
|
31
|
+
res.modernFlushedHeaders = true;
|
|
32
32
|
}
|
|
@@ -13,8 +13,8 @@ const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildc
|
|
|
13
13
|
const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
|
|
14
14
|
const _utils = require("@modern-js/utils");
|
|
15
15
|
const _nodehtmlparser = require("node-html-parser");
|
|
16
|
-
const _remixrouter = require("@modern-js/utils/
|
|
17
|
-
const
|
|
16
|
+
const _remixrouter = require("@modern-js/runtime-utils/remix-router");
|
|
17
|
+
const _node = require("@modern-js/runtime-utils/node");
|
|
18
18
|
async function parseLinks({ pathname, distDir, template }) {
|
|
19
19
|
const links = await parseLinksFromRoutes(pathname, distDir);
|
|
20
20
|
return links.concat(parseLinksFromHtml(template));
|
|
@@ -58,9 +58,9 @@ async function parseLinksFromRoutes(pathname, distDir) {
|
|
|
58
58
|
}
|
|
59
59
|
const routesJson = await Promise.resolve(routesJsonPath).then((p) => /* @__PURE__ */ _interop_require_wildcard._(require(p)));
|
|
60
60
|
const serverRoutes = routesJson.routes;
|
|
61
|
-
const entry = (0,
|
|
61
|
+
const entry = (0, _node.matchEntry)(pathname, serverRoutes);
|
|
62
62
|
if (entry) {
|
|
63
|
-
var _routeAssets_entryName,
|
|
63
|
+
var _routeAssets_entryName, _assets_filter, _assets_filter1;
|
|
64
64
|
const routes = await Promise.resolve(nestedRoutesSpec).then((p) => /* @__PURE__ */ _interop_require_wildcard._(require(p)));
|
|
65
65
|
const { entryName } = entry;
|
|
66
66
|
if (!entryName) {
|
|
@@ -74,23 +74,22 @@ async function parseLinksFromRoutes(pathname, distDir) {
|
|
|
74
74
|
const { routeAssets } = routesManifest;
|
|
75
75
|
const matches = (0, _remixrouter.matchRoutes)(entryRoutes, pathname, entry.urlPath);
|
|
76
76
|
const entryAssets = (_routeAssets_entryName = routeAssets[entryName]) === null || _routeAssets_entryName === void 0 ? void 0 : _routeAssets_entryName.assets;
|
|
77
|
-
const assets =
|
|
77
|
+
const assets = matches === null || matches === void 0 ? void 0 : matches.reduce((acc, match) => {
|
|
78
78
|
const routeId = match.route.id;
|
|
79
79
|
if (routeId) {
|
|
80
|
-
var _matchedManifest;
|
|
81
80
|
const matchedManifest = routeAssets[routeId];
|
|
82
|
-
const assets2 =
|
|
81
|
+
const assets2 = matchedManifest === null || matchedManifest === void 0 ? void 0 : matchedManifest.assets;
|
|
83
82
|
if (Array.isArray(assets2)) {
|
|
84
83
|
acc.push(...assets2);
|
|
85
84
|
}
|
|
86
85
|
}
|
|
87
86
|
return acc;
|
|
88
87
|
}, []).concat(entryAssets || []);
|
|
89
|
-
const cssLinks =
|
|
88
|
+
const cssLinks = assets === null || assets === void 0 ? void 0 : (_assets_filter = assets.filter((asset) => asset.endsWith(".css"))) === null || _assets_filter === void 0 ? void 0 : _assets_filter.map((uri) => ({
|
|
90
89
|
uri,
|
|
91
90
|
as: "style"
|
|
92
91
|
}));
|
|
93
|
-
const scriptLinks =
|
|
92
|
+
const scriptLinks = assets === null || assets === void 0 ? void 0 : (_assets_filter1 = assets.filter((asset) => asset.endsWith(".js"))) === null || _assets_filter1 === void 0 ? void 0 : _assets_filter1.map((uri) => ({
|
|
94
93
|
uri,
|
|
95
94
|
as: "script"
|
|
96
95
|
}));
|
|
@@ -18,7 +18,6 @@ function transformLinks2String(links, preload) {
|
|
|
18
18
|
return resolveLinks;
|
|
19
19
|
}
|
|
20
20
|
function addInclude(links, include) {
|
|
21
|
-
var _include;
|
|
22
21
|
const images = [
|
|
23
22
|
"gif",
|
|
24
23
|
"jpg",
|
|
@@ -44,7 +43,7 @@ function addInclude(links, include) {
|
|
|
44
43
|
"ttf",
|
|
45
44
|
"otf"
|
|
46
45
|
];
|
|
47
|
-
const includes = (
|
|
46
|
+
const includes = (include === null || include === void 0 ? void 0 : include.map((item) => {
|
|
48
47
|
if (typeof item === "string") {
|
|
49
48
|
const type = (() => {
|
|
50
49
|
if (item.endsWith(".js")) {
|
|
@@ -19,7 +19,6 @@ const createCacheConfig = (config = {}) => ({
|
|
|
19
19
|
jest.setTimeout(6e4);
|
|
20
20
|
describe("cache", () => {
|
|
21
21
|
it("should cache correctly", async () => {
|
|
22
|
-
var _cacheResult;
|
|
23
22
|
(0, _spr.destroyCache)();
|
|
24
23
|
const cache = (0, _spr.createCache)();
|
|
25
24
|
const context = {
|
|
@@ -33,7 +32,7 @@ describe("cache", () => {
|
|
|
33
32
|
await cache.set(context, content, cacheConfig, true);
|
|
34
33
|
const cacheResult = await cache.get(context);
|
|
35
34
|
expect(cacheResult).not.toBe(null);
|
|
36
|
-
expect(
|
|
35
|
+
expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe("hello");
|
|
37
36
|
});
|
|
38
37
|
it("should ignore cache set when cache config not exist", async () => {
|
|
39
38
|
(0, _spr.destroyCache)();
|
|
@@ -128,7 +127,6 @@ describe("cache", () => {
|
|
|
128
127
|
(0, _spr.destroyCache)();
|
|
129
128
|
const cache = (0, _spr.createCache)();
|
|
130
129
|
for (const cacheable of _cacheable.cacheabelAry) {
|
|
131
|
-
var _cacheResult;
|
|
132
130
|
const context = {
|
|
133
131
|
entry: "",
|
|
134
132
|
pathname: cacheable.requestOpt.url,
|
|
@@ -138,14 +136,13 @@ describe("cache", () => {
|
|
|
138
136
|
const cacheConfig = createCacheConfig(cacheable.cacheConfig || {});
|
|
139
137
|
await cache.set(context, cacheable.content, cacheConfig, true);
|
|
140
138
|
const cacheResult = await cache.get(context);
|
|
141
|
-
expect(
|
|
139
|
+
expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe(cacheable.content);
|
|
142
140
|
}
|
|
143
141
|
});
|
|
144
142
|
it("should match cache correctly", async () => {
|
|
145
143
|
(0, _spr.destroyCache)();
|
|
146
144
|
const cache = (0, _spr.createCache)();
|
|
147
145
|
for (const cacheable of _matchedcache.matchedCacheableAry) {
|
|
148
|
-
var _cacheResult;
|
|
149
146
|
const [baseCacheable, matchOne, ...other] = cacheable;
|
|
150
147
|
const { requestOpt = {}, cacheConfig, content } = baseCacheable;
|
|
151
148
|
const context = {
|
|
@@ -162,7 +159,7 @@ describe("cache", () => {
|
|
|
162
159
|
headers: matchOne.headers
|
|
163
160
|
};
|
|
164
161
|
const cacheResult = await cache.get(matchContext);
|
|
165
|
-
expect(
|
|
162
|
+
expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe(content);
|
|
166
163
|
for (const notMatch of other) {
|
|
167
164
|
const notMatchContext = {
|
|
168
165
|
entry: "",
|
|
@@ -176,7 +173,6 @@ describe("cache", () => {
|
|
|
176
173
|
}
|
|
177
174
|
});
|
|
178
175
|
it("should stale cache correctly", async () => {
|
|
179
|
-
var _freshResult, _staleResult;
|
|
180
176
|
(0, _spr.destroyCache)();
|
|
181
177
|
const cache = (0, _spr.createCache)();
|
|
182
178
|
const context = {
|
|
@@ -192,17 +188,16 @@ describe("cache", () => {
|
|
|
192
188
|
const shouldCache = await cache.set(context, content, config, true);
|
|
193
189
|
expect(shouldCache.value).toBe(true);
|
|
194
190
|
const freshResult = await cache.get(context);
|
|
195
|
-
expect(
|
|
191
|
+
expect(freshResult === null || freshResult === void 0 ? void 0 : freshResult.isStale).toBe(false);
|
|
196
192
|
await new Promise((resolve) => {
|
|
197
193
|
setTimeout(() => {
|
|
198
194
|
resolve();
|
|
199
195
|
}, 6e3);
|
|
200
196
|
});
|
|
201
197
|
const staleResult = await cache.get(context);
|
|
202
|
-
expect(
|
|
198
|
+
expect(staleResult === null || staleResult === void 0 ? void 0 : staleResult.isStale).toBe(true);
|
|
203
199
|
});
|
|
204
200
|
it("should garbage cache correctly", async () => {
|
|
205
|
-
var _freshResult, _staleResult;
|
|
206
201
|
(0, _spr.destroyCache)();
|
|
207
202
|
const cache = (0, _spr.createCache)();
|
|
208
203
|
const context = {
|
|
@@ -219,13 +214,13 @@ describe("cache", () => {
|
|
|
219
214
|
const shouldCache = await cache.set(context, content, config, true);
|
|
220
215
|
expect(shouldCache.value).toBe(true);
|
|
221
216
|
const freshResult = await cache.get(context);
|
|
222
|
-
expect(
|
|
217
|
+
expect(freshResult === null || freshResult === void 0 ? void 0 : freshResult.isGarbage).toBe(false);
|
|
223
218
|
await new Promise((resolve) => {
|
|
224
219
|
setTimeout(() => {
|
|
225
220
|
resolve();
|
|
226
221
|
}, 1e4);
|
|
227
222
|
});
|
|
228
223
|
const staleResult = await cache.get(context);
|
|
229
|
-
expect(
|
|
224
|
+
expect(staleResult === null || staleResult === void 0 ? void 0 : staleResult.isGarbage).toBe(true);
|
|
230
225
|
});
|
|
231
226
|
});
|
|
@@ -15,7 +15,6 @@ const _util = require("./util");
|
|
|
15
15
|
const _default = (renderFn, ctx) => {
|
|
16
16
|
const sprCache = (0, _spr.createCache)();
|
|
17
17
|
const doRender = async (context) => {
|
|
18
|
-
var _cacheFile;
|
|
19
18
|
const cacheContext = {
|
|
20
19
|
entry: context.entryName,
|
|
21
20
|
...context.request
|
|
@@ -51,7 +50,7 @@ const _default = (renderFn, ctx) => {
|
|
|
51
50
|
const renderResult = await renderFn(context);
|
|
52
51
|
return afterRender(renderResult, saveHtmlIntoCache);
|
|
53
52
|
}
|
|
54
|
-
const cacheHash =
|
|
53
|
+
const cacheHash = cacheFile === null || cacheFile === void 0 ? void 0 : cacheFile.hash;
|
|
55
54
|
if (cacheFile.isGarbage) {
|
|
56
55
|
const renderResult = await renderFn(context);
|
|
57
56
|
return afterRender(renderResult, saveHtmlIntoCache);
|
|
@@ -161,10 +161,9 @@ class CacheManager {
|
|
|
161
161
|
return (0, _util.maybeSync)(doCache)(sync);
|
|
162
162
|
}
|
|
163
163
|
async del(context, cacheHash) {
|
|
164
|
-
var _data;
|
|
165
164
|
const cacheKey = this.generateRequestKey(context);
|
|
166
165
|
const data = this.cache.get(cacheKey);
|
|
167
|
-
|
|
166
|
+
data === null || data === void 0 ? void 0 : data.caches.del(cacheHash);
|
|
168
167
|
}
|
|
169
168
|
constructor(cacheOptions) {
|
|
170
169
|
_define_property._(this, "cache", void 0);
|
|
@@ -18,6 +18,7 @@ const _static = require("./static");
|
|
|
18
18
|
const _reader = require("./reader");
|
|
19
19
|
const _ssr = /* @__PURE__ */ _interop_require_wildcard._(require("./ssr"));
|
|
20
20
|
const _utils1 = require("./utils");
|
|
21
|
+
const calcFallback = (metaName) => `x-${(0, _utils.cutNameByHyphen)(metaName)}-ssr-fallback`;
|
|
21
22
|
const createRenderHandler = ({ distDir, staticGenerate, conf, forceCSR, nonce, ssrRender, metaName = "modern-js" }) => async function render({ ctx, route, runner }) {
|
|
22
23
|
if (ctx.resHasHandled()) {
|
|
23
24
|
return null;
|
|
@@ -33,7 +34,7 @@ const createRenderHandler = ({ distDir, staticGenerate, conf, forceCSR, nonce, s
|
|
|
33
34
|
if (!content) {
|
|
34
35
|
return null;
|
|
35
36
|
}
|
|
36
|
-
const useCSR = forceCSR && (ctx.query.csr || ctx.headers[
|
|
37
|
+
const useCSR = forceCSR && (ctx.query.csr || ctx.headers[calcFallback(metaName)]);
|
|
37
38
|
if (route.isSSR && !useCSR) {
|
|
38
39
|
try {
|
|
39
40
|
const userAgent = ctx.getReqHeader("User-Agent");
|
|
@@ -71,7 +72,7 @@ const createRenderHandler = ({ distDir, staticGenerate, conf, forceCSR, nonce, s
|
|
|
71
72
|
return result;
|
|
72
73
|
} catch (err) {
|
|
73
74
|
ctx.error(_constants.ERROR_DIGEST.ERENDER, err.stack || err.message);
|
|
74
|
-
ctx.res.set(
|
|
75
|
+
ctx.res.set(calcFallback(metaName), "1");
|
|
75
76
|
}
|
|
76
77
|
}
|
|
77
78
|
return {
|
|
@@ -101,9 +101,8 @@ class LruReader {
|
|
|
101
101
|
}
|
|
102
102
|
const reader = new LruReader();
|
|
103
103
|
const readFile = async (filepath) => {
|
|
104
|
-
var _file;
|
|
105
104
|
const file = await reader.read(filepath);
|
|
106
|
-
return
|
|
105
|
+
return file === null || file === void 0 ? void 0 : file.content;
|
|
107
106
|
};
|
|
108
107
|
const updateFile = () => {
|
|
109
108
|
reader.update();
|
|
@@ -49,9 +49,9 @@ class RouteMatcher {
|
|
|
49
49
|
if (!this.urlReg) {
|
|
50
50
|
return this.urlPath.length;
|
|
51
51
|
} else {
|
|
52
|
-
var _result_
|
|
52
|
+
var _result_;
|
|
53
53
|
const result = this.urlReg.exec(pathname);
|
|
54
|
-
return (
|
|
54
|
+
return (result === null || result === void 0 ? void 0 : (_result_ = result[0]) === null || _result_ === void 0 ? void 0 : _result_.length) || null;
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
// if match url path
|
package/dist/cjs/server/index.js
CHANGED
|
@@ -91,7 +91,7 @@ class Server {
|
|
|
91
91
|
const { pwd, config } = options;
|
|
92
92
|
const { serverConfig } = this;
|
|
93
93
|
const finalServerConfig = this.runConfigHook(runner, serverConfig);
|
|
94
|
-
const resolvedConfigPath = _path.default.join(
|
|
94
|
+
const resolvedConfigPath = (0, _utils.ensureAbsolutePath)(pwd, _path.default.join(config.output.path || "dist", _utils.OUTPUT_CONFIG_FILE));
|
|
95
95
|
options.config = (0, _loadConfig.loadConfig)({
|
|
96
96
|
cliConfig: config,
|
|
97
97
|
serverConfig: finalServerConfig,
|
|
@@ -103,8 +103,7 @@ class Server {
|
|
|
103
103
|
}
|
|
104
104
|
listen(options, listener) {
|
|
105
105
|
const callback = () => {
|
|
106
|
-
|
|
107
|
-
(_listener = listener) === null || _listener === void 0 ? void 0 : _listener();
|
|
106
|
+
listener === null || listener === void 0 ? void 0 : listener();
|
|
108
107
|
};
|
|
109
108
|
if (typeof options === "object") {
|
|
110
109
|
if (process.env.PORT) {
|
|
@@ -154,7 +153,6 @@ class Server {
|
|
|
154
153
|
});
|
|
155
154
|
}
|
|
156
155
|
initAppContext() {
|
|
157
|
-
var _appContext, _appContext1, _appContext2;
|
|
158
156
|
const { options } = this;
|
|
159
157
|
const { pwd: appDirectory, plugins = [], config, appContext } = options;
|
|
160
158
|
const serverPlugins = plugins.map((p) => ({
|
|
@@ -162,9 +160,9 @@ class Server {
|
|
|
162
160
|
}));
|
|
163
161
|
return {
|
|
164
162
|
appDirectory,
|
|
165
|
-
apiDirectory:
|
|
166
|
-
lambdaDirectory:
|
|
167
|
-
sharedDirectory: (
|
|
163
|
+
apiDirectory: appContext === null || appContext === void 0 ? void 0 : appContext.apiDirectory,
|
|
164
|
+
lambdaDirectory: appContext === null || appContext === void 0 ? void 0 : appContext.lambdaDirectory,
|
|
165
|
+
sharedDirectory: (appContext === null || appContext === void 0 ? void 0 : appContext.sharedDirectory) || _path.default.resolve(appDirectory, _utils.SHARED_DIR),
|
|
168
166
|
distDirectory: _path.default.join(appDirectory, config.output.path || "dist"),
|
|
169
167
|
plugins: serverPlugins
|
|
170
168
|
};
|
|
@@ -14,7 +14,7 @@ const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildc
|
|
|
14
14
|
const _http = require("http");
|
|
15
15
|
const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
|
|
16
16
|
const _utils = require("@modern-js/utils");
|
|
17
|
-
const _time = require("@modern-js/utils/
|
|
17
|
+
const _time = require("@modern-js/runtime-utils/time");
|
|
18
18
|
const _route = require("../libs/route");
|
|
19
19
|
const _render = require("../libs/render");
|
|
20
20
|
const _serveFile = require("../libs/serveFile");
|
|
@@ -28,7 +28,7 @@ const SERVER_DIR = "./server";
|
|
|
28
28
|
class ModernServer {
|
|
29
29
|
// server prepare
|
|
30
30
|
async onInit(runner, app) {
|
|
31
|
-
var _conf_bff,
|
|
31
|
+
var _conf_bff, _this_conf_output;
|
|
32
32
|
this.runner = runner;
|
|
33
33
|
const { distDir, conf } = this;
|
|
34
34
|
this.initReader();
|
|
@@ -40,7 +40,7 @@ class ModernServer {
|
|
|
40
40
|
this.addHandler(handler);
|
|
41
41
|
});
|
|
42
42
|
}
|
|
43
|
-
|
|
43
|
+
app === null || app === void 0 ? void 0 : app.on("close", () => {
|
|
44
44
|
this.reader.close();
|
|
45
45
|
});
|
|
46
46
|
const usageRoutes = this.filterRoutes(this.getRoutes());
|
|
@@ -176,10 +176,9 @@ class ModernServer {
|
|
|
176
176
|
return handler;
|
|
177
177
|
}
|
|
178
178
|
async prepareAPIHandler(extension) {
|
|
179
|
-
var _bff, _bff1;
|
|
180
179
|
const { workDir, runner, conf } = this;
|
|
181
180
|
const { bff } = conf;
|
|
182
|
-
const prefix = (
|
|
181
|
+
const prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || "/api";
|
|
183
182
|
const webOnly = await (0, _utils.isWebOnly)();
|
|
184
183
|
if (webOnly && process.env.NODE_ENV === "development") {
|
|
185
184
|
return (req, res) => {
|
|
@@ -191,7 +190,7 @@ class ModernServer {
|
|
|
191
190
|
pwd: workDir,
|
|
192
191
|
config: extension,
|
|
193
192
|
prefix: Array.isArray(prefix) ? prefix[0] : prefix,
|
|
194
|
-
httpMethodDecider:
|
|
193
|
+
httpMethodDecider: bff === null || bff === void 0 ? void 0 : bff.httpMethodDecider,
|
|
195
194
|
render: this.render.bind(this)
|
|
196
195
|
}, {
|
|
197
196
|
onLast: () => null
|
|
@@ -302,16 +301,14 @@ class ModernServer {
|
|
|
302
301
|
await this.handleAPI(context);
|
|
303
302
|
return;
|
|
304
303
|
}
|
|
305
|
-
if (route.entryName) {
|
|
304
|
+
if (route.entryName && this.runMode === _constants.RUN_MODE.FULL) {
|
|
306
305
|
const afterMatchContext = (0, _hookapi.createAfterMatchContext)(context, route.entryName);
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
cost && reporter.reportTiming(_constants.ServerReportTimings.SERVER_HOOK_AFTER_MATCH, cost);
|
|
314
|
-
}
|
|
306
|
+
const end2 = (0, _time.time)();
|
|
307
|
+
await this.runner.afterMatch(afterMatchContext, {
|
|
308
|
+
onLast: _utils1.noop
|
|
309
|
+
});
|
|
310
|
+
const cost = end2();
|
|
311
|
+
cost && reporter.reportTiming(_constants.ServerReportTimings.SERVER_HOOK_AFTER_MATCH, cost);
|
|
315
312
|
if (this.isSend(res)) {
|
|
316
313
|
return;
|
|
317
314
|
}
|
|
@@ -354,16 +351,14 @@ class ModernServer {
|
|
|
354
351
|
responseStream.pipe(res);
|
|
355
352
|
return;
|
|
356
353
|
}
|
|
357
|
-
if (route.entryName) {
|
|
354
|
+
if (route.entryName && this.runMode === _constants.RUN_MODE.FULL) {
|
|
358
355
|
const afterRenderContext = (0, _hookapi.createAfterRenderContext)(context, response.toString());
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
cost && reporter.reportTiming(_constants.ServerReportTimings.SERVER_HOOK_AFTER_RENDER, cost);
|
|
366
|
-
}
|
|
356
|
+
const end2 = (0, _time.time)();
|
|
357
|
+
await this.runner.afterRender(afterRenderContext, {
|
|
358
|
+
onLast: _utils1.noop
|
|
359
|
+
});
|
|
360
|
+
const cost = end2();
|
|
361
|
+
cost && reporter.reportTiming(_constants.ServerReportTimings.SERVER_HOOK_AFTER_RENDER, cost);
|
|
367
362
|
if (this.isSend(res)) {
|
|
368
363
|
return;
|
|
369
364
|
}
|
|
@@ -372,7 +367,11 @@ class ModernServer {
|
|
|
372
367
|
res.end(response);
|
|
373
368
|
}
|
|
374
369
|
isSend(res) {
|
|
375
|
-
if (res.
|
|
370
|
+
if (res.modernFlushedHeaders) {
|
|
371
|
+
if (res.writableFinished) {
|
|
372
|
+
return true;
|
|
373
|
+
}
|
|
374
|
+
} else if (res.headersSent) {
|
|
376
375
|
return true;
|
|
377
376
|
}
|
|
378
377
|
if (res.getHeader("Location") && (0, _utils1.isRedirect)(res.statusCode)) {
|
|
@@ -477,7 +476,6 @@ class ModernServer {
|
|
|
477
476
|
context.res.end((0, _utils1.createErrorDocument)(status, text));
|
|
478
477
|
}
|
|
479
478
|
constructor({ pwd, config, routes, staticGenerate, logger, metrics, runMode, proxyTarget, appContext }) {
|
|
480
|
-
var _appContext;
|
|
481
479
|
_define_property._(this, "pwd", void 0);
|
|
482
480
|
_define_property._(this, "distDir", void 0);
|
|
483
481
|
_define_property._(this, "workDir", void 0);
|
|
@@ -511,6 +509,6 @@ class ModernServer {
|
|
|
511
509
|
this.proxyTarget = proxyTarget;
|
|
512
510
|
this.staticGenerate = staticGenerate || false;
|
|
513
511
|
this.runMode = runMode || _constants.RUN_MODE.FULL;
|
|
514
|
-
this.metaName =
|
|
512
|
+
this.metaName = appContext === null || appContext === void 0 ? void 0 : appContext.metaName;
|
|
515
513
|
}
|
|
516
514
|
}
|
package/dist/cjs/workerServer.js
CHANGED
|
@@ -19,8 +19,8 @@ _export(exports, {
|
|
|
19
19
|
});
|
|
20
20
|
const _define_property = require("@swc/helpers/_/_define_property");
|
|
21
21
|
const _plugin = require("@modern-js/plugin");
|
|
22
|
+
const _logger = require("@modern-js/utils/logger");
|
|
22
23
|
const _indexworker = require("./libs/hook-api/index.worker");
|
|
23
|
-
const _logger = require("./libs/logger");
|
|
24
24
|
const _route = require("./libs/route");
|
|
25
25
|
const _metrics = require("./libs/metrics");
|
|
26
26
|
const _reporter = require("./libs/reporter");
|
|
@@ -89,7 +89,7 @@ const createHandler = (manifest) => {
|
|
|
89
89
|
const { pages, routes } = manifest;
|
|
90
90
|
routeMgr.reset(routes);
|
|
91
91
|
return async (options) => {
|
|
92
|
-
var _page_serverHooks_afterMatch, _page_serverHooks
|
|
92
|
+
var _page_serverHooks_afterMatch, _page_serverHooks;
|
|
93
93
|
const { request, loadableStats, routeManifest } = options;
|
|
94
94
|
const url = new URL(request.url);
|
|
95
95
|
const pageMatch = routeMgr.match(url.pathname);
|
|
@@ -105,7 +105,7 @@ const createHandler = (manifest) => {
|
|
|
105
105
|
const reporter = _reporter.defaultReporter;
|
|
106
106
|
const hookContext = createWorkerHookContext(request.url, logger, metrics, reporter);
|
|
107
107
|
const afterMatchHookContext = (0, _indexworker.createAfterMatchContext)(hookContext, entryName);
|
|
108
|
-
|
|
108
|
+
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);
|
|
109
109
|
if (checkIsSent(hookContext)) {
|
|
110
110
|
return new ReturnResponse(hookContext.res.body || "Unkown body", hookContext.res.status, hookContext.res.headers);
|
|
111
111
|
}
|
|
@@ -141,14 +141,13 @@ const createHandler = (manifest) => {
|
|
|
141
141
|
logger,
|
|
142
142
|
reporter: _reporter.defaultReporter,
|
|
143
143
|
metrics,
|
|
144
|
-
// FIXME: pass correctly req & res
|
|
145
144
|
req: request,
|
|
145
|
+
res: responseLike,
|
|
146
146
|
serverTiming: {
|
|
147
147
|
addServeTiming() {
|
|
148
148
|
return this;
|
|
149
149
|
}
|
|
150
|
-
}
|
|
151
|
-
res: responseLike
|
|
150
|
+
}
|
|
152
151
|
};
|
|
153
152
|
const body = await page.serverRender(serverRenderContext);
|
|
154
153
|
const afterRenderHookContext = (0, _indexworker.createAfterRenderContext)(hookContext, body);
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
2
|
+
import { _ as _class_private_field_get } from "@swc/helpers/_/_class_private_field_get";
|
|
3
|
+
import { _ as _class_private_field_init } from "@swc/helpers/_/_class_private_field_init";
|
|
4
|
+
import { _ as _class_private_field_set } from "@swc/helpers/_/_class_private_field_set";
|
|
2
5
|
import { _ as _create_class } from "@swc/helpers/_/_create_class";
|
|
3
6
|
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
4
7
|
import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
|
|
@@ -12,11 +15,11 @@ import { ServerTiming } from "../serverTiming";
|
|
|
12
15
|
import { defaultReporter } from "../reporter";
|
|
13
16
|
import { headersWithoutCookie } from "../../utils";
|
|
14
17
|
var MOCK_URL_BASE = "https://modernjs.dev/";
|
|
18
|
+
var _urls = /* @__PURE__ */ new WeakMap(), _queries = /* @__PURE__ */ new WeakMap();
|
|
15
19
|
export var ModernServerContext = /* @__PURE__ */ function() {
|
|
16
20
|
"use strict";
|
|
17
21
|
function ModernServerContext2(req, res, options) {
|
|
18
22
|
_class_call_check(this, ModernServerContext2);
|
|
19
|
-
var _options;
|
|
20
23
|
_define_property(this, "req", void 0);
|
|
21
24
|
_define_property(this, "res", void 0);
|
|
22
25
|
_define_property(this, "params", {});
|
|
@@ -24,11 +27,21 @@ export var ModernServerContext = /* @__PURE__ */ function() {
|
|
|
24
27
|
_define_property(this, "serverTiming", void 0);
|
|
25
28
|
_define_property(this, "serverData", {});
|
|
26
29
|
_define_property(this, "options", {});
|
|
30
|
+
_class_private_field_init(this, _urls, {
|
|
31
|
+
writable: true,
|
|
32
|
+
value: void 0
|
|
33
|
+
});
|
|
34
|
+
_class_private_field_init(this, _queries, {
|
|
35
|
+
writable: true,
|
|
36
|
+
value: void 0
|
|
37
|
+
});
|
|
27
38
|
this.req = req;
|
|
28
39
|
this.res = res;
|
|
29
40
|
this.options = options || {};
|
|
30
41
|
this.bind();
|
|
31
|
-
|
|
42
|
+
_class_private_field_set(this, _urls, /* @__PURE__ */ new Map());
|
|
43
|
+
_class_private_field_set(this, _queries, /* @__PURE__ */ new Map());
|
|
44
|
+
this.serverTiming = new ServerTiming(this.res, cutNameByHyphen((options === null || options === void 0 ? void 0 : options.metaName) || "modern-js"));
|
|
32
45
|
}
|
|
33
46
|
_create_class(ModernServerContext2, [
|
|
34
47
|
{
|
|
@@ -47,7 +60,12 @@ export var ModernServerContext = /* @__PURE__ */ function() {
|
|
|
47
60
|
key: "parsedURL",
|
|
48
61
|
get: function get() {
|
|
49
62
|
try {
|
|
50
|
-
|
|
63
|
+
var url = _class_private_field_get(this, _urls).get(this.req.url);
|
|
64
|
+
if (!url) {
|
|
65
|
+
url = new URL(this.req.url, MOCK_URL_BASE);
|
|
66
|
+
_class_private_field_get(this, _urls).set(this.req.url, url);
|
|
67
|
+
}
|
|
68
|
+
return url;
|
|
51
69
|
} catch (e) {
|
|
52
70
|
this.logger.error("Parse URL error", e.stack || e.message);
|
|
53
71
|
return new URL("/_modern_mock_path", MOCK_URL_BASE);
|
|
@@ -173,7 +191,8 @@ export var ModernServerContext = /* @__PURE__ */ function() {
|
|
|
173
191
|
if (!host) {
|
|
174
192
|
host = this.getReqHeader("Host");
|
|
175
193
|
}
|
|
176
|
-
|
|
194
|
+
host = host.split(/\s*,\s*/, 1)[0] || "undefined";
|
|
195
|
+
return host;
|
|
177
196
|
}
|
|
178
197
|
},
|
|
179
198
|
{
|
|
@@ -226,7 +245,12 @@ export var ModernServerContext = /* @__PURE__ */ function() {
|
|
|
226
245
|
key: "query",
|
|
227
246
|
get: function get() {
|
|
228
247
|
var str = this.querystring;
|
|
229
|
-
|
|
248
|
+
var query = _class_private_field_get(this, _queries).get(str);
|
|
249
|
+
if (!query) {
|
|
250
|
+
query = qs.parse(str);
|
|
251
|
+
_class_private_field_get(this, _queries).set(str, query);
|
|
252
|
+
}
|
|
253
|
+
return query;
|
|
230
254
|
}
|
|
231
255
|
},
|
|
232
256
|
{
|
|
@@ -7,7 +7,7 @@ export function flushServerHeader(_) {
|
|
|
7
7
|
}
|
|
8
8
|
function _flushServerHeader() {
|
|
9
9
|
_flushServerHeader = _async_to_generator(function(param) {
|
|
10
|
-
var serverConf, ctx, distDir, template, headers, _ref, ssrConf, res, links, link, key,
|
|
10
|
+
var serverConf, ctx, distDir, template, headers, _ref, ssrConf, res, links, link, key, value;
|
|
11
11
|
return _ts_generator(this, function(_state) {
|
|
12
12
|
switch (_state.label) {
|
|
13
13
|
case 0:
|
|
@@ -32,11 +32,11 @@ function _flushServerHeader() {
|
|
|
32
32
|
link = transformLinks2String(links, ssrConf.preload);
|
|
33
33
|
res.set("link", link);
|
|
34
34
|
for (var key2 in headers || {}) {
|
|
35
|
-
;
|
|
36
|
-
value = (_headers = headers) === null || _headers === void 0 ? void 0 : _headers[key2];
|
|
35
|
+
value = headers === null || headers === void 0 ? void 0 : headers[key2];
|
|
37
36
|
value && res.set(key2, value);
|
|
38
37
|
}
|
|
39
38
|
res.flushHeaders();
|
|
39
|
+
res.modernFlushedHeaders = true;
|
|
40
40
|
return [
|
|
41
41
|
2
|
|
42
42
|
];
|