@modern-js/prod-server 2.26.0 → 2.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/dist/cjs/index.js +4 -0
- package/dist/cjs/libs/render/cache/__tests__/cache.test.js +12 -7
- package/dist/cjs/libs/render/cache/index.js +2 -1
- package/dist/cjs/libs/render/cache/spr.js +2 -1
- package/dist/cjs/libs/render/index.js +12 -3
- package/dist/cjs/libs/render/reader.js +2 -1
- package/dist/cjs/libs/render/ssr.js +17 -18
- package/dist/cjs/libs/route/matcher.js +2 -2
- package/dist/cjs/server/index.js +6 -4
- package/dist/cjs/server/modernServer.js +21 -14
- package/dist/cjs/workerServer.js +3 -3
- package/dist/esm/index.js +1 -0
- 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 +12 -3
- package/dist/esm/libs/render/reader.js +2 -2
- package/dist/esm/libs/render/ssr.js +2 -1
- package/dist/esm/libs/route/matcher.js +2 -2
- package/dist/esm/renderHtml.js +6 -6
- package/dist/esm/server/index.js +6 -4
- package/dist/esm/server/modernServer.js +27 -18
- package/dist/esm/server/modernServerSplit.js +6 -6
- package/dist/esm/workerServer.js +2 -2
- package/dist/esm-node/index.js +1 -0
- package/dist/esm-node/libs/render/cache/__tests__/cache.test.js +12 -7
- package/dist/esm-node/libs/render/cache/index.js +2 -1
- package/dist/esm-node/libs/render/cache/spr.js +2 -1
- package/dist/esm-node/libs/render/index.js +12 -3
- package/dist/esm-node/libs/render/reader.js +2 -1
- package/dist/esm-node/libs/render/ssr.js +17 -18
- package/dist/esm-node/libs/route/matcher.js +2 -2
- package/dist/esm-node/server/index.js +6 -4
- package/dist/esm-node/server/modernServer.js +21 -14
- package/dist/esm-node/workerServer.js +3 -3
- package/dist/types/index.d.ts +1 -0
- package/dist/types/libs/context/context.d.ts +1 -1
- package/dist/types/libs/render/index.d.ts +13 -18
- package/dist/types/server/modernServer.d.ts +5 -3
- package/dist/types/utils.d.ts +1 -1
- package/package.json +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# @modern-js/prod-server
|
|
2
2
|
|
|
3
|
+
## 2.28.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 6eae1e7: feat: support worker hmr using dev-server
|
|
8
|
+
feat: 借用 dev-server 支持 wokrer hmr
|
|
9
|
+
|
|
10
|
+
### Patch Changes
|
|
11
|
+
|
|
12
|
+
- Updated dependencies [00b58a7]
|
|
13
|
+
- @modern-js/utils@2.28.0
|
|
14
|
+
- @modern-js/server-core@2.28.0
|
|
15
|
+
- @modern-js/plugin@2.28.0
|
|
16
|
+
|
|
17
|
+
## 2.27.0
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- Updated dependencies [91d14b8]
|
|
22
|
+
- Updated dependencies [6d7104d]
|
|
23
|
+
- @modern-js/utils@2.27.0
|
|
24
|
+
- @modern-js/server-core@2.27.0
|
|
25
|
+
- @modern-js/plugin@2.27.0
|
|
26
|
+
|
|
3
27
|
## 2.26.0
|
|
4
28
|
|
|
5
29
|
### Patch Changes
|
package/dist/cjs/index.js
CHANGED
|
@@ -19,6 +19,9 @@ _export(exports, {
|
|
|
19
19
|
createProxyHandler: function() {
|
|
20
20
|
return _proxy.createProxyHandler;
|
|
21
21
|
},
|
|
22
|
+
createRenderHandler: function() {
|
|
23
|
+
return _render.createRenderHandler;
|
|
24
|
+
},
|
|
22
25
|
default: function() {
|
|
23
26
|
return _default;
|
|
24
27
|
}
|
|
@@ -29,6 +32,7 @@ const _modernServer = require("./server/modernServer");
|
|
|
29
32
|
const _proxy = require("./libs/proxy");
|
|
30
33
|
_export_star._(require("./type"), exports);
|
|
31
34
|
_export_star._(require("./constants"), exports);
|
|
35
|
+
const _render = require("./libs/render");
|
|
32
36
|
const _default = (options) => {
|
|
33
37
|
if (options == null) {
|
|
34
38
|
throw new Error("can not start mserver without options");
|
|
@@ -19,6 +19,7 @@ const createCacheConfig = (config = {}) => ({
|
|
|
19
19
|
jest.setTimeout(6e4);
|
|
20
20
|
describe("cache", () => {
|
|
21
21
|
it("should cache correctly", async () => {
|
|
22
|
+
var _cacheResult;
|
|
22
23
|
(0, _spr.destroyCache)();
|
|
23
24
|
const cache = (0, _spr.createCache)();
|
|
24
25
|
const context = {
|
|
@@ -32,7 +33,7 @@ describe("cache", () => {
|
|
|
32
33
|
await cache.set(context, content, cacheConfig, true);
|
|
33
34
|
const cacheResult = await cache.get(context);
|
|
34
35
|
expect(cacheResult).not.toBe(null);
|
|
35
|
-
expect(cacheResult === null ||
|
|
36
|
+
expect((_cacheResult = cacheResult) === null || _cacheResult === void 0 ? void 0 : _cacheResult.content).toBe("hello");
|
|
36
37
|
});
|
|
37
38
|
it("should ignore cache set when cache config not exist", async () => {
|
|
38
39
|
(0, _spr.destroyCache)();
|
|
@@ -127,6 +128,7 @@ describe("cache", () => {
|
|
|
127
128
|
(0, _spr.destroyCache)();
|
|
128
129
|
const cache = (0, _spr.createCache)();
|
|
129
130
|
for (const cacheable of _cacheable.cacheabelAry) {
|
|
131
|
+
var _cacheResult;
|
|
130
132
|
const context = {
|
|
131
133
|
entry: "",
|
|
132
134
|
pathname: cacheable.requestOpt.url,
|
|
@@ -136,13 +138,14 @@ describe("cache", () => {
|
|
|
136
138
|
const cacheConfig = createCacheConfig(cacheable.cacheConfig || {});
|
|
137
139
|
await cache.set(context, cacheable.content, cacheConfig, true);
|
|
138
140
|
const cacheResult = await cache.get(context);
|
|
139
|
-
expect(cacheResult === null ||
|
|
141
|
+
expect((_cacheResult = cacheResult) === null || _cacheResult === void 0 ? void 0 : _cacheResult.content).toBe(cacheable.content);
|
|
140
142
|
}
|
|
141
143
|
});
|
|
142
144
|
it("should match cache correctly", async () => {
|
|
143
145
|
(0, _spr.destroyCache)();
|
|
144
146
|
const cache = (0, _spr.createCache)();
|
|
145
147
|
for (const cacheable of _matchedcache.matchedCacheableAry) {
|
|
148
|
+
var _cacheResult;
|
|
146
149
|
const [baseCacheable, matchOne, ...other] = cacheable;
|
|
147
150
|
const { requestOpt = {}, cacheConfig, content } = baseCacheable;
|
|
148
151
|
const context = {
|
|
@@ -159,7 +162,7 @@ describe("cache", () => {
|
|
|
159
162
|
headers: matchOne.headers
|
|
160
163
|
};
|
|
161
164
|
const cacheResult = await cache.get(matchContext);
|
|
162
|
-
expect(cacheResult === null ||
|
|
165
|
+
expect((_cacheResult = cacheResult) === null || _cacheResult === void 0 ? void 0 : _cacheResult.content).toBe(content);
|
|
163
166
|
for (const notMatch of other) {
|
|
164
167
|
const notMatchContext = {
|
|
165
168
|
entry: "",
|
|
@@ -173,6 +176,7 @@ describe("cache", () => {
|
|
|
173
176
|
}
|
|
174
177
|
});
|
|
175
178
|
it("should stale cache correctly", async () => {
|
|
179
|
+
var _freshResult, _staleResult;
|
|
176
180
|
(0, _spr.destroyCache)();
|
|
177
181
|
const cache = (0, _spr.createCache)();
|
|
178
182
|
const context = {
|
|
@@ -188,16 +192,17 @@ describe("cache", () => {
|
|
|
188
192
|
const shouldCache = await cache.set(context, content, config, true);
|
|
189
193
|
expect(shouldCache.value).toBe(true);
|
|
190
194
|
const freshResult = await cache.get(context);
|
|
191
|
-
expect(freshResult === null ||
|
|
195
|
+
expect((_freshResult = freshResult) === null || _freshResult === void 0 ? void 0 : _freshResult.isStale).toBe(false);
|
|
192
196
|
await new Promise((resolve) => {
|
|
193
197
|
setTimeout(() => {
|
|
194
198
|
resolve();
|
|
195
199
|
}, 6e3);
|
|
196
200
|
});
|
|
197
201
|
const staleResult = await cache.get(context);
|
|
198
|
-
expect(staleResult === null ||
|
|
202
|
+
expect((_staleResult = staleResult) === null || _staleResult === void 0 ? void 0 : _staleResult.isStale).toBe(true);
|
|
199
203
|
});
|
|
200
204
|
it("should garbage cache correctly", async () => {
|
|
205
|
+
var _freshResult, _staleResult;
|
|
201
206
|
(0, _spr.destroyCache)();
|
|
202
207
|
const cache = (0, _spr.createCache)();
|
|
203
208
|
const context = {
|
|
@@ -214,13 +219,13 @@ describe("cache", () => {
|
|
|
214
219
|
const shouldCache = await cache.set(context, content, config, true);
|
|
215
220
|
expect(shouldCache.value).toBe(true);
|
|
216
221
|
const freshResult = await cache.get(context);
|
|
217
|
-
expect(freshResult === null ||
|
|
222
|
+
expect((_freshResult = freshResult) === null || _freshResult === void 0 ? void 0 : _freshResult.isGarbage).toBe(false);
|
|
218
223
|
await new Promise((resolve) => {
|
|
219
224
|
setTimeout(() => {
|
|
220
225
|
resolve();
|
|
221
226
|
}, 1e4);
|
|
222
227
|
});
|
|
223
228
|
const staleResult = await cache.get(context);
|
|
224
|
-
expect(staleResult === null ||
|
|
229
|
+
expect((_staleResult = staleResult) === null || _staleResult === void 0 ? void 0 : _staleResult.isGarbage).toBe(true);
|
|
225
230
|
});
|
|
226
231
|
});
|
|
@@ -15,6 +15,7 @@ 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;
|
|
18
19
|
const cacheContext = {
|
|
19
20
|
entry: context.entryName,
|
|
20
21
|
...context.request
|
|
@@ -50,7 +51,7 @@ const _default = (renderFn, ctx) => {
|
|
|
50
51
|
const renderResult = await renderFn(context);
|
|
51
52
|
return afterRender(renderResult, saveHtmlIntoCache);
|
|
52
53
|
}
|
|
53
|
-
const cacheHash = cacheFile === null ||
|
|
54
|
+
const cacheHash = (_cacheFile = cacheFile) === null || _cacheFile === void 0 ? void 0 : _cacheFile.hash;
|
|
54
55
|
if (cacheFile.isGarbage) {
|
|
55
56
|
const renderResult = await renderFn(context);
|
|
56
57
|
return afterRender(renderResult, saveHtmlIntoCache);
|
|
@@ -161,9 +161,10 @@ class CacheManager {
|
|
|
161
161
|
return (0, _util.maybeSync)(doCache)(sync);
|
|
162
162
|
}
|
|
163
163
|
async del(context, cacheHash) {
|
|
164
|
+
var _data;
|
|
164
165
|
const cacheKey = this.generateRequestKey(context);
|
|
165
166
|
const data = this.cache.get(cacheKey);
|
|
166
|
-
data === null ||
|
|
167
|
+
(_data = data) === null || _data === void 0 ? void 0 : _data.caches.del(cacheHash);
|
|
167
168
|
}
|
|
168
169
|
constructor(cacheOptions) {
|
|
169
170
|
_define_property._(this, "cache", void 0);
|
|
@@ -17,7 +17,7 @@ const _static = require("./static");
|
|
|
17
17
|
const _reader = require("./reader");
|
|
18
18
|
const _ssr = /* @__PURE__ */ _interop_require_wildcard._(require("./ssr"));
|
|
19
19
|
const _utils1 = require("./utils");
|
|
20
|
-
const createRenderHandler = ({ distDir, staticGenerate, forceCSR, nonce, metaName = "modern-js" }) => async function render({ ctx, route, runner }) {
|
|
20
|
+
const createRenderHandler = ({ distDir, staticGenerate, forceCSR, nonce, ssrRender, metaName = "modern-js" }) => async function render({ ctx, route, runner }) {
|
|
21
21
|
if (ctx.resHasHandled()) {
|
|
22
22
|
return null;
|
|
23
23
|
}
|
|
@@ -35,7 +35,7 @@ const createRenderHandler = ({ distDir, staticGenerate, forceCSR, nonce, metaNam
|
|
|
35
35
|
const useCSR = forceCSR && (ctx.query.csr || ctx.headers[`x-${(0, _utils.cutNameByHyphen)(metaName)}-ssr-fallback`]);
|
|
36
36
|
if (route.isSSR && !useCSR) {
|
|
37
37
|
try {
|
|
38
|
-
const
|
|
38
|
+
const ssrRenderOptions = {
|
|
39
39
|
distDir,
|
|
40
40
|
entryName: route.entryName,
|
|
41
41
|
urlPath: route.urlPath,
|
|
@@ -43,7 +43,16 @@ const createRenderHandler = ({ distDir, staticGenerate, forceCSR, nonce, metaNam
|
|
|
43
43
|
template: content.toString(),
|
|
44
44
|
staticGenerate,
|
|
45
45
|
nonce
|
|
46
|
-
}
|
|
46
|
+
};
|
|
47
|
+
const result = await (ssrRender ? ssrRender(ctx, ssrRenderOptions, runner) : _ssr.render(ctx, {
|
|
48
|
+
distDir,
|
|
49
|
+
entryName: route.entryName,
|
|
50
|
+
urlPath: route.urlPath,
|
|
51
|
+
bundle: route.bundle,
|
|
52
|
+
template: content.toString(),
|
|
53
|
+
staticGenerate,
|
|
54
|
+
nonce
|
|
55
|
+
}, runner));
|
|
47
56
|
return result;
|
|
48
57
|
} catch (err) {
|
|
49
58
|
ctx.error(_constants.ERROR_DIGEST.ERENDER, err.stack || err.message);
|
|
@@ -101,8 +101,9 @@ class LruReader {
|
|
|
101
101
|
}
|
|
102
102
|
const reader = new LruReader();
|
|
103
103
|
const readFile = async (filepath) => {
|
|
104
|
+
var _file;
|
|
104
105
|
const file = await reader.read(filepath);
|
|
105
|
-
return file === null ||
|
|
106
|
+
return (_file = file) === null || _file === void 0 ? void 0 : _file.content;
|
|
106
107
|
};
|
|
107
108
|
const updateFile = () => {
|
|
108
109
|
reader.update();
|
|
@@ -84,21 +84,20 @@ const render = async (ctx, renderOptions, runner) => {
|
|
|
84
84
|
};
|
|
85
85
|
}
|
|
86
86
|
};
|
|
87
|
-
const getRequestBody = (req) => {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
};
|
|
87
|
+
const getRequestBody = (req) => new Promise((resolve, reject) => {
|
|
88
|
+
var _req;
|
|
89
|
+
if (((_req = req) === null || _req === void 0 ? void 0 : _req.method) && req.method.toLowerCase() !== "get") {
|
|
90
|
+
let body = "";
|
|
91
|
+
req.on("data", (chunk) => {
|
|
92
|
+
body += chunk.toString();
|
|
93
|
+
});
|
|
94
|
+
req.on("end", () => {
|
|
95
|
+
resolve(body);
|
|
96
|
+
});
|
|
97
|
+
req.on("error", (err) => {
|
|
98
|
+
reject(err);
|
|
99
|
+
});
|
|
100
|
+
} else {
|
|
101
|
+
resolve(void 0);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
@@ -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_, _result;
|
|
53
53
|
const result = this.urlReg.exec(pathname);
|
|
54
|
-
return (result === null ||
|
|
54
|
+
return ((_result = 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
|
@@ -89,7 +89,8 @@ class Server {
|
|
|
89
89
|
}
|
|
90
90
|
listen(options, listener) {
|
|
91
91
|
const callback = () => {
|
|
92
|
-
|
|
92
|
+
var _listener;
|
|
93
|
+
(_listener = listener) === null || _listener === void 0 ? void 0 : _listener();
|
|
93
94
|
};
|
|
94
95
|
if (typeof options === "object") {
|
|
95
96
|
if (process.env.PORT) {
|
|
@@ -139,6 +140,7 @@ class Server {
|
|
|
139
140
|
});
|
|
140
141
|
}
|
|
141
142
|
initAppContext() {
|
|
143
|
+
var _appContext, _appContext1, _appContext2;
|
|
142
144
|
const { options } = this;
|
|
143
145
|
const { pwd: appDirectory, plugins = [], config, appContext } = options;
|
|
144
146
|
const serverPlugins = plugins.map((p) => ({
|
|
@@ -146,9 +148,9 @@ class Server {
|
|
|
146
148
|
}));
|
|
147
149
|
return {
|
|
148
150
|
appDirectory,
|
|
149
|
-
apiDirectory: appContext === null ||
|
|
150
|
-
lambdaDirectory: appContext === null ||
|
|
151
|
-
sharedDirectory: (appContext === null ||
|
|
151
|
+
apiDirectory: (_appContext = appContext) === null || _appContext === void 0 ? void 0 : _appContext.apiDirectory,
|
|
152
|
+
lambdaDirectory: (_appContext1 = appContext) === null || _appContext1 === void 0 ? void 0 : _appContext1.lambdaDirectory,
|
|
153
|
+
sharedDirectory: ((_appContext2 = appContext) === null || _appContext2 === void 0 ? void 0 : _appContext2.sharedDirectory) || _path.default.resolve(appDirectory, _utils.SHARED_DIR),
|
|
152
154
|
distDirectory: _path.default.join(appDirectory, config.output.path || "dist"),
|
|
153
155
|
plugins: serverPlugins
|
|
154
156
|
};
|
|
@@ -27,9 +27,9 @@ const SERVER_DIR = "./server";
|
|
|
27
27
|
class ModernServer {
|
|
28
28
|
// server prepare
|
|
29
29
|
async onInit(runner, app) {
|
|
30
|
-
var _conf_bff,
|
|
30
|
+
var _conf_bff, _app, _this_conf_output;
|
|
31
31
|
this.runner = runner;
|
|
32
|
-
const { distDir,
|
|
32
|
+
const { distDir, conf } = this;
|
|
33
33
|
this.initReader();
|
|
34
34
|
(0, _utils1.debug)("final server conf", this.conf);
|
|
35
35
|
this.proxyHandler = (0, _proxy.createProxyHandler)((_conf_bff = conf.bff) === null || _conf_bff === void 0 ? void 0 : _conf_bff.proxy);
|
|
@@ -38,7 +38,7 @@ class ModernServer {
|
|
|
38
38
|
this.addHandler(handler);
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
|
-
app === null ||
|
|
41
|
+
(_app = app) === null || _app === void 0 ? void 0 : _app.on("close", () => {
|
|
42
42
|
this.reader.close();
|
|
43
43
|
});
|
|
44
44
|
const usageRoutes = this.filterRoutes(this.getRoutes());
|
|
@@ -46,20 +46,25 @@ class ModernServer {
|
|
|
46
46
|
this.warmupSSRBundle();
|
|
47
47
|
await this.prepareFrameHandler();
|
|
48
48
|
await this.prepareLoaderHandler(usageRoutes, distDir);
|
|
49
|
+
this.routeRenderHandler = this.getRenderHandler();
|
|
50
|
+
await this.setupBeforeProdMiddleware();
|
|
51
|
+
this.addHandler(this.setupStaticMiddleware((_this_conf_output = this.conf.output) === null || _this_conf_output === void 0 ? void 0 : _this_conf_output.assetPrefix));
|
|
52
|
+
this.addHandler(_serveFile.faviconFallbackHandler);
|
|
53
|
+
this.addHandler(this.routeHandler.bind(this));
|
|
54
|
+
this.compose();
|
|
55
|
+
}
|
|
56
|
+
getRenderHandler() {
|
|
57
|
+
var _this_conf_server, _conf_security;
|
|
58
|
+
const { distDir, staticGenerate, conf, metaName } = this;
|
|
49
59
|
const ssrConfig = (_this_conf_server = this.conf.server) === null || _this_conf_server === void 0 ? void 0 : _this_conf_server.ssr;
|
|
50
60
|
const forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
|
|
51
|
-
|
|
61
|
+
return (0, _render.createRenderHandler)({
|
|
52
62
|
distDir,
|
|
53
63
|
staticGenerate,
|
|
54
64
|
forceCSR,
|
|
55
65
|
nonce: (_conf_security = conf.security) === null || _conf_security === void 0 ? void 0 : _conf_security.nonce,
|
|
56
66
|
metaName
|
|
57
67
|
});
|
|
58
|
-
await this.setupBeforeProdMiddleware();
|
|
59
|
-
this.addHandler(this.setupStaticMiddleware((_this_conf_output = this.conf.output) === null || _this_conf_output === void 0 ? void 0 : _this_conf_output.assetPrefix));
|
|
60
|
-
this.addHandler(_serveFile.faviconFallbackHandler);
|
|
61
|
-
this.addHandler(this.routeHandler.bind(this));
|
|
62
|
-
this.compose();
|
|
63
68
|
}
|
|
64
69
|
// server ready
|
|
65
70
|
onRepack(_) {
|
|
@@ -165,9 +170,10 @@ class ModernServer {
|
|
|
165
170
|
return handler;
|
|
166
171
|
}
|
|
167
172
|
async prepareAPIHandler(extension) {
|
|
173
|
+
var _bff, _bff1;
|
|
168
174
|
const { workDir, runner, conf } = this;
|
|
169
175
|
const { bff } = conf;
|
|
170
|
-
const prefix = (bff === null ||
|
|
176
|
+
const prefix = ((_bff = bff) === null || _bff === void 0 ? void 0 : _bff.prefix) || "/api";
|
|
171
177
|
const webOnly = await (0, _utils.isWebOnly)();
|
|
172
178
|
if (webOnly && process.env.NODE_ENV === "development") {
|
|
173
179
|
return (req, res) => {
|
|
@@ -179,7 +185,7 @@ class ModernServer {
|
|
|
179
185
|
pwd: workDir,
|
|
180
186
|
config: extension,
|
|
181
187
|
prefix: Array.isArray(prefix) ? prefix[0] : prefix,
|
|
182
|
-
httpMethodDecider: bff === null ||
|
|
188
|
+
httpMethodDecider: (_bff1 = bff) === null || _bff1 === void 0 ? void 0 : _bff1.httpMethodDecider,
|
|
183
189
|
render: this.render.bind(this)
|
|
184
190
|
}, {
|
|
185
191
|
onLast: () => null
|
|
@@ -446,6 +452,7 @@ class ModernServer {
|
|
|
446
452
|
context.res.end((0, _utils1.createErrorDocument)(status, text));
|
|
447
453
|
}
|
|
448
454
|
constructor({ pwd, config, routes, staticGenerate, logger, metrics, runMode, proxyTarget, appContext }) {
|
|
455
|
+
var _appContext;
|
|
449
456
|
_define_property._(this, "pwd", void 0);
|
|
450
457
|
_define_property._(this, "distDir", void 0);
|
|
451
458
|
_define_property._(this, "workDir", void 0);
|
|
@@ -460,13 +467,13 @@ class ModernServer {
|
|
|
460
467
|
_define_property._(this, "reader", _reader);
|
|
461
468
|
_define_property._(this, "proxyTarget", void 0);
|
|
462
469
|
_define_property._(this, "routeRenderHandler", void 0);
|
|
470
|
+
_define_property._(this, "staticGenerate", void 0);
|
|
471
|
+
_define_property._(this, "metaName", void 0);
|
|
463
472
|
_define_property._(this, "loaderHandler", null);
|
|
464
473
|
_define_property._(this, "frameWebHandler", null);
|
|
465
474
|
_define_property._(this, "frameAPIHandler", null);
|
|
466
475
|
_define_property._(this, "proxyHandler", null);
|
|
467
476
|
_define_property._(this, "_handler", void 0);
|
|
468
|
-
_define_property._(this, "staticGenerate", void 0);
|
|
469
|
-
_define_property._(this, "metaName", void 0);
|
|
470
477
|
require("ignore-styles");
|
|
471
478
|
this.pwd = pwd;
|
|
472
479
|
this.distDir = _path.default.resolve(pwd, config.output.path || "dist");
|
|
@@ -480,6 +487,6 @@ class ModernServer {
|
|
|
480
487
|
this.proxyTarget = proxyTarget;
|
|
481
488
|
this.staticGenerate = staticGenerate || false;
|
|
482
489
|
this.runMode = runMode || _constants.RUN_MODE.FULL;
|
|
483
|
-
this.metaName = appContext === null ||
|
|
490
|
+
this.metaName = (_appContext = appContext) === null || _appContext === void 0 ? void 0 : _appContext.metaName;
|
|
484
491
|
}
|
|
485
492
|
}
|
package/dist/cjs/workerServer.js
CHANGED
|
@@ -88,7 +88,7 @@ const createHandler = (manifest) => {
|
|
|
88
88
|
const { pages, routes } = manifest;
|
|
89
89
|
routeMgr.reset(routes);
|
|
90
90
|
return async (options) => {
|
|
91
|
-
var _page_serverHooks,
|
|
91
|
+
var _page_serverHooks_afterMatch, _page_serverHooks, _page;
|
|
92
92
|
const { request, loadableStats, routeManifest } = options;
|
|
93
93
|
const url = new URL(request.url);
|
|
94
94
|
const pageMatch = routeMgr.match(url.pathname);
|
|
@@ -103,13 +103,13 @@ const createHandler = (manifest) => {
|
|
|
103
103
|
const metrics = _metrics.metrics;
|
|
104
104
|
const hookContext = createWorkerHookContext(request.url, logger, metrics);
|
|
105
105
|
const afterMatchHookContext = (0, _indexworker.createAfterMatchContext)(hookContext, entryName);
|
|
106
|
-
page === null ||
|
|
106
|
+
(_page = 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
107
|
if (checkIsSent(hookContext)) {
|
|
108
108
|
return new ReturnResponse(hookContext.res.body || "Unkown body", hookContext.res.status, hookContext.res.headers);
|
|
109
109
|
}
|
|
110
110
|
if (page.serverRender) {
|
|
111
111
|
try {
|
|
112
|
-
var _page_serverHooks1,
|
|
112
|
+
var _page_serverHooks1, _page_serverHooks_afterRender, _page_serverHooks2;
|
|
113
113
|
const middlewarsHookContext = (0, _indexworker.createMiddlewareContext)(hookContext);
|
|
114
114
|
applyMiddlewares(middlewarsHookContext, (_page_serverHooks1 = page.serverHooks) === null || _page_serverHooks1 === void 0 ? void 0 : _page_serverHooks1.middleware);
|
|
115
115
|
if (checkIsSent(hookContext)) {
|
package/dist/esm/index.js
CHANGED
|
@@ -4,6 +4,7 @@ export { ModernServer } from "./server/modernServer";
|
|
|
4
4
|
export { createProxyHandler } from "./libs/proxy";
|
|
5
5
|
export * from "./type";
|
|
6
6
|
export * from "./constants";
|
|
7
|
+
export { createRenderHandler } from "./libs/render";
|
|
7
8
|
export default function(options) {
|
|
8
9
|
if (options == null) {
|
|
9
10
|
throw new Error("can not start mserver without options");
|
|
@@ -21,7 +21,7 @@ var createCacheConfig = function() {
|
|
|
21
21
|
jest.setTimeout(6e4);
|
|
22
22
|
describe("cache", function() {
|
|
23
23
|
it("should cache correctly", /* @__PURE__ */ _async_to_generator(function() {
|
|
24
|
-
var cache, context, content, cacheConfig, cacheResult;
|
|
24
|
+
var _cacheResult, cache, context, content, cacheConfig, cacheResult;
|
|
25
25
|
return _ts_generator(this, function(_state) {
|
|
26
26
|
switch (_state.label) {
|
|
27
27
|
case 0:
|
|
@@ -48,7 +48,7 @@ describe("cache", function() {
|
|
|
48
48
|
case 2:
|
|
49
49
|
cacheResult = _state.sent();
|
|
50
50
|
expect(cacheResult).not.toBe(null);
|
|
51
|
-
expect(cacheResult === null ||
|
|
51
|
+
expect((_cacheResult = cacheResult) === null || _cacheResult === void 0 ? void 0 : _cacheResult.content).toBe("hello");
|
|
52
52
|
return [
|
|
53
53
|
2
|
|
54
54
|
];
|
|
@@ -261,7 +261,7 @@ describe("cache", function() {
|
|
|
261
261
|
});
|
|
262
262
|
}));
|
|
263
263
|
it("should get cache correctly", /* @__PURE__ */ _async_to_generator(function() {
|
|
264
|
-
var cache, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, cacheable, context, cacheConfig, cacheResult, err;
|
|
264
|
+
var cache, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, cacheable, _cacheResult, context, cacheConfig, cacheResult, err;
|
|
265
265
|
return _ts_generator(this, function(_state) {
|
|
266
266
|
switch (_state.label) {
|
|
267
267
|
case 0:
|
|
@@ -304,7 +304,7 @@ describe("cache", function() {
|
|
|
304
304
|
];
|
|
305
305
|
case 4:
|
|
306
306
|
cacheResult = _state.sent();
|
|
307
|
-
expect(cacheResult === null ||
|
|
307
|
+
expect((_cacheResult = cacheResult) === null || _cacheResult === void 0 ? void 0 : _cacheResult.content).toBe(cacheable.content);
|
|
308
308
|
_state.label = 5;
|
|
309
309
|
case 5:
|
|
310
310
|
_iteratorNormalCompletion = true;
|
|
@@ -346,7 +346,7 @@ describe("cache", function() {
|
|
|
346
346
|
});
|
|
347
347
|
}));
|
|
348
348
|
it("should match cache correctly", /* @__PURE__ */ _async_to_generator(function() {
|
|
349
|
-
var cache, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, cacheable, _cacheable, baseCacheable, matchOne, other, _baseCacheable_requestOpt, requestOpt, cacheConfig, content, context, matchContext, cacheResult, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, notMatch, notMatchContext, nothing, err, err;
|
|
349
|
+
var cache, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, cacheable, _cacheResult, _cacheable, baseCacheable, matchOne, other, _baseCacheable_requestOpt, requestOpt, cacheConfig, content, context, matchContext, cacheResult, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, notMatch, notMatchContext, nothing, err, err;
|
|
350
350
|
return _ts_generator(this, function(_state) {
|
|
351
351
|
switch (_state.label) {
|
|
352
352
|
case 0:
|
|
@@ -396,7 +396,7 @@ describe("cache", function() {
|
|
|
396
396
|
];
|
|
397
397
|
case 4:
|
|
398
398
|
cacheResult = _state.sent();
|
|
399
|
-
expect(cacheResult === null ||
|
|
399
|
+
expect((_cacheResult = cacheResult) === null || _cacheResult === void 0 ? void 0 : _cacheResult.content).toBe(content);
|
|
400
400
|
_iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = void 0;
|
|
401
401
|
_state.label = 5;
|
|
402
402
|
case 5:
|
|
@@ -501,7 +501,7 @@ describe("cache", function() {
|
|
|
501
501
|
});
|
|
502
502
|
}));
|
|
503
503
|
it("should stale cache correctly", /* @__PURE__ */ _async_to_generator(function() {
|
|
504
|
-
var cache, context, config, content, shouldCache, freshResult, staleResult;
|
|
504
|
+
var _freshResult, _staleResult, cache, context, config, content, shouldCache, freshResult, staleResult;
|
|
505
505
|
return _ts_generator(this, function(_state) {
|
|
506
506
|
switch (_state.label) {
|
|
507
507
|
case 0:
|
|
@@ -530,7 +530,7 @@ describe("cache", function() {
|
|
|
530
530
|
];
|
|
531
531
|
case 2:
|
|
532
532
|
freshResult = _state.sent();
|
|
533
|
-
expect(freshResult === null ||
|
|
533
|
+
expect((_freshResult = freshResult) === null || _freshResult === void 0 ? void 0 : _freshResult.isStale).toBe(false);
|
|
534
534
|
return [
|
|
535
535
|
4,
|
|
536
536
|
new Promise(function(resolve) {
|
|
@@ -547,7 +547,7 @@ describe("cache", function() {
|
|
|
547
547
|
];
|
|
548
548
|
case 4:
|
|
549
549
|
staleResult = _state.sent();
|
|
550
|
-
expect(staleResult === null ||
|
|
550
|
+
expect((_staleResult = staleResult) === null || _staleResult === void 0 ? void 0 : _staleResult.isStale).toBe(true);
|
|
551
551
|
return [
|
|
552
552
|
2
|
|
553
553
|
];
|
|
@@ -555,7 +555,7 @@ describe("cache", function() {
|
|
|
555
555
|
});
|
|
556
556
|
}));
|
|
557
557
|
it("should garbage cache correctly", /* @__PURE__ */ _async_to_generator(function() {
|
|
558
|
-
var cache, context, config, content, shouldCache, freshResult, staleResult;
|
|
558
|
+
var _freshResult, _staleResult, cache, context, config, content, shouldCache, freshResult, staleResult;
|
|
559
559
|
return _ts_generator(this, function(_state) {
|
|
560
560
|
switch (_state.label) {
|
|
561
561
|
case 0:
|
|
@@ -585,7 +585,7 @@ describe("cache", function() {
|
|
|
585
585
|
];
|
|
586
586
|
case 2:
|
|
587
587
|
freshResult = _state.sent();
|
|
588
|
-
expect(freshResult === null ||
|
|
588
|
+
expect((_freshResult = freshResult) === null || _freshResult === void 0 ? void 0 : _freshResult.isGarbage).toBe(false);
|
|
589
589
|
return [
|
|
590
590
|
4,
|
|
591
591
|
new Promise(function(resolve) {
|
|
@@ -602,7 +602,7 @@ describe("cache", function() {
|
|
|
602
602
|
];
|
|
603
603
|
case 4:
|
|
604
604
|
staleResult = _state.sent();
|
|
605
|
-
expect(staleResult === null ||
|
|
605
|
+
expect((_staleResult = staleResult) === null || _staleResult === void 0 ? void 0 : _staleResult.isGarbage).toBe(true);
|
|
606
606
|
return [
|
|
607
607
|
2
|
|
608
608
|
];
|
|
@@ -9,7 +9,7 @@ export default function(renderFn, ctx) {
|
|
|
9
9
|
var sprCache = createCache();
|
|
10
10
|
var doRender = function() {
|
|
11
11
|
var _ref = _async_to_generator(function(context) {
|
|
12
|
-
var cacheContext, cacheFile, renderResult, cacheHash, renderResult1, render;
|
|
12
|
+
var _cacheFile, cacheContext, cacheFile, renderResult, cacheHash, renderResult1, render;
|
|
13
13
|
function afterRender(source, onAfterRender) {
|
|
14
14
|
return _afterRender.apply(this, arguments);
|
|
15
15
|
}
|
|
@@ -124,7 +124,7 @@ export default function(renderFn, ctx) {
|
|
|
124
124
|
afterRender(renderResult, saveHtmlIntoCache)
|
|
125
125
|
];
|
|
126
126
|
case 3:
|
|
127
|
-
cacheHash = cacheFile === null ||
|
|
127
|
+
cacheHash = (_cacheFile = cacheFile) === null || _cacheFile === void 0 ? void 0 : _cacheFile.hash;
|
|
128
128
|
if (!cacheFile.isGarbage)
|
|
129
129
|
return [
|
|
130
130
|
3,
|
|
@@ -311,11 +311,11 @@ var CacheManager = /* @__PURE__ */ function() {
|
|
|
311
311
|
value: function del(context, cacheHash) {
|
|
312
312
|
var _this = this;
|
|
313
313
|
return _async_to_generator(function() {
|
|
314
|
-
var cacheKey, data;
|
|
314
|
+
var _data, cacheKey, data;
|
|
315
315
|
return _ts_generator(this, function(_state) {
|
|
316
316
|
cacheKey = _this.generateRequestKey(context);
|
|
317
317
|
data = _this.cache.get(cacheKey);
|
|
318
|
-
data === null ||
|
|
318
|
+
(_data = data) === null || _data === void 0 ? void 0 : _data.caches.del(cacheHash);
|
|
319
319
|
return [
|
|
320
320
|
2
|
|
321
321
|
];
|
|
@@ -8,10 +8,10 @@ import { readFile } from "./reader";
|
|
|
8
8
|
import * as ssr from "./ssr";
|
|
9
9
|
import { injectServerData } from "./utils";
|
|
10
10
|
export var createRenderHandler = function(param) {
|
|
11
|
-
var distDir = param.distDir, staticGenerate = param.staticGenerate, forceCSR = param.forceCSR, nonce = param.nonce, _param_metaName = param.metaName, metaName = _param_metaName === void 0 ? "modern-js" : _param_metaName;
|
|
11
|
+
var distDir = param.distDir, staticGenerate = param.staticGenerate, forceCSR = param.forceCSR, nonce = param.nonce, ssrRender = param.ssrRender, _param_metaName = param.metaName, metaName = _param_metaName === void 0 ? "modern-js" : _param_metaName;
|
|
12
12
|
return function() {
|
|
13
13
|
var _render = _async_to_generator(function(param2) {
|
|
14
|
-
var ctx, route, runner, entryPath, urlPath, entry, result, templatePath, content, useCSR, result1, err;
|
|
14
|
+
var ctx, route, runner, entryPath, urlPath, entry, result, templatePath, content, useCSR, ssrRenderOptions, result1, err;
|
|
15
15
|
return _ts_generator(this, function(_state) {
|
|
16
16
|
switch (_state.label) {
|
|
17
17
|
case 0:
|
|
@@ -67,9 +67,18 @@ export var createRenderHandler = function(param) {
|
|
|
67
67
|
,
|
|
68
68
|
7
|
|
69
69
|
]);
|
|
70
|
+
ssrRenderOptions = {
|
|
71
|
+
distDir: distDir,
|
|
72
|
+
entryName: route.entryName,
|
|
73
|
+
urlPath: route.urlPath,
|
|
74
|
+
bundle: route.bundle,
|
|
75
|
+
template: content.toString(),
|
|
76
|
+
staticGenerate: staticGenerate,
|
|
77
|
+
nonce: nonce
|
|
78
|
+
};
|
|
70
79
|
return [
|
|
71
80
|
4,
|
|
72
|
-
ssr.render(ctx, {
|
|
81
|
+
ssrRender ? ssrRender(ctx, ssrRenderOptions, runner) : ssr.render(ctx, {
|
|
73
82
|
distDir: distDir,
|
|
74
83
|
entryName: route.entryName,
|
|
75
84
|
urlPath: route.urlPath,
|
|
@@ -157,7 +157,7 @@ export var LruReader = /* @__PURE__ */ function() {
|
|
|
157
157
|
var reader = new LruReader();
|
|
158
158
|
export var readFile = function() {
|
|
159
159
|
var _ref = _async_to_generator(function(filepath) {
|
|
160
|
-
var file;
|
|
160
|
+
var _file, file;
|
|
161
161
|
return _ts_generator(this, function(_state) {
|
|
162
162
|
switch (_state.label) {
|
|
163
163
|
case 0:
|
|
@@ -169,7 +169,7 @@ export var readFile = function() {
|
|
|
169
169
|
file = _state.sent();
|
|
170
170
|
return [
|
|
171
171
|
2,
|
|
172
|
-
file === null ||
|
|
172
|
+
(_file = file) === null || _file === void 0 ? void 0 : _file.content
|
|
173
173
|
];
|
|
174
174
|
}
|
|
175
175
|
});
|
|
@@ -114,7 +114,8 @@ export var render = function() {
|
|
|
114
114
|
}();
|
|
115
115
|
var getRequestBody = function(req) {
|
|
116
116
|
return new Promise(function(resolve, reject) {
|
|
117
|
-
|
|
117
|
+
var _req;
|
|
118
|
+
if (((_req = req) === null || _req === void 0 ? void 0 : _req.method) && req.method.toLowerCase() !== "get") {
|
|
118
119
|
var body = "";
|
|
119
120
|
req.on("data", function(chunk) {
|
|
120
121
|
body += chunk.toString();
|