@modern-js/server 1.2.0 → 1.2.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/js/modern/index.js +1 -7
- package/dist/js/modern/libs/context/context.js +3 -6
- package/dist/js/modern/libs/context/index.js +1 -7
- package/dist/js/modern/libs/proxy.js +1 -1
- package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +1 -2
- package/dist/js/modern/libs/render/index.js +6 -2
- package/dist/js/modern/libs/render/reader.js +8 -11
- package/dist/js/modern/libs/render/ssr.js +2 -1
- package/dist/js/modern/libs/route/index.js +2 -1
- package/dist/js/modern/server/index.js +3 -1
- package/dist/js/modern/server/modern-server.js +19 -10
- package/dist/js/modern/utils.js +1 -9
- package/dist/js/node/index.js +1 -49
- package/dist/js/node/libs/context/context.js +3 -6
- package/dist/js/node/libs/context/index.js +1 -7
- package/dist/js/node/libs/proxy.js +1 -1
- package/dist/js/node/libs/render/cache/__tests__/cache.test.js +1 -2
- package/dist/js/node/libs/render/index.js +6 -2
- package/dist/js/node/libs/render/reader.js +8 -11
- package/dist/js/node/libs/render/ssr.js +2 -1
- package/dist/js/node/libs/route/index.js +6 -0
- package/dist/js/node/server/index.js +3 -1
- package/dist/js/node/server/modern-server.js +19 -10
- package/dist/js/node/utils.js +2 -12
- package/dist/types/index.d.ts +1 -3
- package/dist/types/libs/context/context.d.ts +7 -7
- package/dist/types/libs/context/index.d.ts +1 -8
- package/dist/types/libs/render/index.d.ts +10 -1
- package/dist/types/libs/render/ssr.d.ts +2 -1
- package/dist/types/libs/render/type.d.ts +2 -21
- package/dist/types/libs/route/index.d.ts +2 -1
- package/dist/types/server/modern-server.d.ts +1 -1
- package/dist/types/type.d.ts +6 -0
- package/dist/types/utils.d.ts +1 -2
- package/package.json +7 -5
- package/src/index.ts +2 -8
- package/src/libs/context/context.ts +8 -7
- package/src/libs/context/index.ts +2 -6
- package/src/libs/proxy.ts +1 -1
- package/src/libs/render/cache/__tests__/cache.test.ts +2 -2
- package/src/libs/render/index.ts +21 -11
- package/src/libs/render/reader.ts +8 -8
- package/src/libs/render/ssr.ts +4 -0
- package/src/libs/render/type.ts +2 -17
- package/src/libs/route/index.ts +2 -1
- package/src/server/index.ts +1 -1
- package/src/server/modern-server.ts +18 -11
- package/src/type.ts +7 -0
- package/src/utils.ts +0 -14
- package/tests/.eslintrc.js +6 -0
- package/tests/context.test.ts +41 -0
- package/tests/fixtures/hosting-files/static/index.js +1 -0
- package/tests/fixtures/pure/modern.config.js +5 -0
- package/tests/fixtures/pure/package.json +21 -0
- package/tests/fixtures/pure/src/App.css +119 -0
- package/tests/fixtures/pure/src/App.tsx +43 -0
- package/tests/fixtures/pure/tsconfig.json +13 -0
- package/tests/fixtures/route-spec/index.json +29 -0
- package/tests/helper.ts +8 -0
- package/tests/hook.test.ts +44 -0
- package/tests/middleware.test.ts +178 -0
- package/tests/route.test.ts +54 -0
- package/tests/server.test.ts +89 -0
- package/tests/tsconfig.json +14 -0
- package/tests/utils.test.ts +40 -0
package/dist/js/modern/index.js
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
import { Server } from "./server";
|
|
2
|
-
export * from "./type";
|
|
3
|
-
export * from "./libs/context";
|
|
4
|
-
export * from "./libs/route";
|
|
5
2
|
export { Server };
|
|
6
3
|
export default (options => {
|
|
7
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
8
|
-
const allowEnvs = ['production', 'development', 'test'];
|
|
9
|
-
|
|
10
4
|
if (options == null) {
|
|
11
|
-
throw new Error();
|
|
5
|
+
throw new Error('can not start mserver without options');
|
|
12
6
|
}
|
|
13
7
|
|
|
14
8
|
const server = new Server(options);
|
|
@@ -13,10 +13,7 @@ export class ModernServerContext {
|
|
|
13
13
|
/**
|
|
14
14
|
* url params
|
|
15
15
|
*/
|
|
16
|
-
constructor(req, res
|
|
17
|
-
logger,
|
|
18
|
-
metrics
|
|
19
|
-
}) {
|
|
16
|
+
constructor(req, res) {
|
|
20
17
|
this.req = void 0;
|
|
21
18
|
this.res = void 0;
|
|
22
19
|
this.params = {};
|
|
@@ -24,8 +21,8 @@ export class ModernServerContext {
|
|
|
24
21
|
this.metrics = void 0;
|
|
25
22
|
this.req = req;
|
|
26
23
|
this.res = res;
|
|
27
|
-
this.logger = logger;
|
|
28
|
-
this.metrics = metrics;
|
|
24
|
+
this.logger = req.logger;
|
|
25
|
+
this.metrics = req.metrics;
|
|
29
26
|
this.bind();
|
|
30
27
|
}
|
|
31
28
|
|
|
@@ -1,9 +1,3 @@
|
|
|
1
1
|
import { ModernServerContext } from "./context";
|
|
2
|
-
export const createContext = (req, res,
|
|
3
|
-
logger,
|
|
4
|
-
metrics
|
|
5
|
-
}) => new ModernServerContext(req, res, {
|
|
6
|
-
logger,
|
|
7
|
-
metrics
|
|
8
|
-
});
|
|
2
|
+
export const createContext = (req, res) => new ModernServerContext(req, res);
|
|
9
3
|
export { ModernServerContext };
|
|
@@ -19,6 +19,7 @@ const createCacheConfig = (config = {}) => _objectSpread({
|
|
|
19
19
|
matches: null
|
|
20
20
|
}, config);
|
|
21
21
|
|
|
22
|
+
jest.setTimeout(60000);
|
|
22
23
|
describe('cache', () => {
|
|
23
24
|
it('should cache correctly', async () => {
|
|
24
25
|
destroyCache();
|
|
@@ -178,7 +179,6 @@ describe('cache', () => {
|
|
|
178
179
|
}
|
|
179
180
|
}
|
|
180
181
|
});
|
|
181
|
-
jest.setTimeout(1000 * 10);
|
|
182
182
|
it('should stale cache correctly', async () => {
|
|
183
183
|
destroyCache();
|
|
184
184
|
const cache = createCache();
|
|
@@ -204,7 +204,6 @@ describe('cache', () => {
|
|
|
204
204
|
const staleResult = await cache.get(context);
|
|
205
205
|
expect(staleResult === null || staleResult === void 0 ? void 0 : staleResult.isStale).toBe(true);
|
|
206
206
|
});
|
|
207
|
-
jest.setTimeout(1000 * 15);
|
|
208
207
|
it('should garbage cache correctly', async () => {
|
|
209
208
|
destroyCache();
|
|
210
209
|
const cache = createCache();
|
|
@@ -9,7 +9,11 @@ import { ERROR_DIGEST } from "../../constants";
|
|
|
9
9
|
export const createRenderHandler = ({
|
|
10
10
|
distDir,
|
|
11
11
|
staticGenerate
|
|
12
|
-
}) => async function render(
|
|
12
|
+
}) => async function render({
|
|
13
|
+
ctx,
|
|
14
|
+
route,
|
|
15
|
+
runner
|
|
16
|
+
}) {
|
|
13
17
|
if (ctx.resHasHandled()) {
|
|
14
18
|
return null;
|
|
15
19
|
}
|
|
@@ -39,7 +43,7 @@ export const createRenderHandler = ({
|
|
|
39
43
|
bundle: route.bundle,
|
|
40
44
|
template: templateHTML,
|
|
41
45
|
staticGenerate
|
|
42
|
-
});
|
|
46
|
+
}, runner);
|
|
43
47
|
return result;
|
|
44
48
|
} catch (err) {
|
|
45
49
|
ctx.error(ERROR_DIGEST.ERENDER, err.stack);
|
|
@@ -15,9 +15,9 @@ const createCacheItem = async (filepath, mtime) => {
|
|
|
15
15
|
};
|
|
16
16
|
|
|
17
17
|
class LruReader {
|
|
18
|
+
// private timer?: NodeJS.Timeout;
|
|
18
19
|
constructor() {
|
|
19
20
|
this.cache = void 0;
|
|
20
|
-
this.timer = void 0;
|
|
21
21
|
this.cache = new LRU({
|
|
22
22
|
max: 256 * MB,
|
|
23
23
|
length: getContentLength,
|
|
@@ -26,14 +26,12 @@ class LruReader {
|
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
init() {
|
|
30
|
-
this.timeTask();
|
|
29
|
+
init() {// this.timeTask();
|
|
31
30
|
}
|
|
32
31
|
|
|
33
|
-
close() {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
32
|
+
close() {// if (this.timer) {
|
|
33
|
+
// clearInterval(this.timer);
|
|
34
|
+
// }
|
|
37
35
|
}
|
|
38
36
|
|
|
39
37
|
async read(filepath) {
|
|
@@ -92,11 +90,10 @@ class LruReader {
|
|
|
92
90
|
cache.del(filepath);
|
|
93
91
|
}
|
|
94
92
|
}
|
|
95
|
-
}
|
|
93
|
+
} // private timeTask() {
|
|
94
|
+
// this.timer = setInterval(() => this.update, 5 * 60 * 1000).unref();
|
|
95
|
+
// }
|
|
96
96
|
|
|
97
|
-
timeTask() {
|
|
98
|
-
this.timer = setInterval(() => this.update, 5 * 60 * 1000).unref();
|
|
99
|
-
}
|
|
100
97
|
|
|
101
98
|
}
|
|
102
99
|
|
|
@@ -2,7 +2,7 @@ import path from 'path';
|
|
|
2
2
|
import { SERVER_RENDER_FUNCTION_NAME } from '@modern-js/utils';
|
|
3
3
|
import mime from 'mime-types';
|
|
4
4
|
import cache from "./cache";
|
|
5
|
-
export const render = async (ctx, renderOptions) => {
|
|
5
|
+
export const render = async (ctx, renderOptions, runner) => {
|
|
6
6
|
const {
|
|
7
7
|
bundle,
|
|
8
8
|
distDir,
|
|
@@ -27,6 +27,7 @@ export const render = async (ctx, renderOptions) => {
|
|
|
27
27
|
logger: ctx.logger,
|
|
28
28
|
metrics: ctx.metrics
|
|
29
29
|
};
|
|
30
|
+
runner.extendSSRContext(context);
|
|
30
31
|
|
|
31
32
|
const serverRender = require(bundleJS)[SERVER_RENDER_FUNCTION_NAME];
|
|
32
33
|
|
|
@@ -120,9 +120,11 @@ export class Server {
|
|
|
120
120
|
});
|
|
121
121
|
const appContext = await this.initAppContext();
|
|
122
122
|
serverManager.run(() => {
|
|
123
|
+
var _options$config$outpu;
|
|
124
|
+
|
|
123
125
|
ConfigContext.set(this.options.config);
|
|
124
126
|
AppContext.set(_objectSpread(_objectSpread({}, appContext), {}, {
|
|
125
|
-
distDirectory: path.join(options.pwd, options.config.output.path || 'dist')
|
|
127
|
+
distDirectory: path.join(options.pwd, ((_options$config$outpu = options.config.output) === null || _options$config$outpu === void 0 ? void 0 : _options$config$outpu.path) || 'dist')
|
|
126
128
|
}));
|
|
127
129
|
});
|
|
128
130
|
return serverManager.init({});
|
|
@@ -44,6 +44,8 @@ export class ModernServer {
|
|
|
44
44
|
metrics,
|
|
45
45
|
proxyTarget
|
|
46
46
|
}) {
|
|
47
|
+
var _config$output;
|
|
48
|
+
|
|
47
49
|
this.pwd = void 0;
|
|
48
50
|
this.distDir = void 0;
|
|
49
51
|
this.workDir = void 0;
|
|
@@ -60,15 +62,15 @@ export class ModernServer {
|
|
|
60
62
|
this.routeRenderHandler = void 0;
|
|
61
63
|
this.frameWebHandler = null;
|
|
62
64
|
this.frameAPIHandler = null;
|
|
65
|
+
this.proxyHandler = null;
|
|
63
66
|
this._handler = void 0;
|
|
64
67
|
this.staticGenerate = false;
|
|
65
|
-
this.proxyHandler = null;
|
|
66
68
|
|
|
67
69
|
require('ignore-styles');
|
|
68
70
|
|
|
69
71
|
this.isDev = Boolean(dev);
|
|
70
72
|
this.pwd = pwd;
|
|
71
|
-
this.distDir = path.join(pwd, config.output.path || 'dist');
|
|
73
|
+
this.distDir = path.join(pwd, ((_config$output = config.output) === null || _config$output === void 0 ? void 0 : _config$output.path) || 'dist');
|
|
72
74
|
this.workDir = this.isDev ? pwd : this.distDir;
|
|
73
75
|
this.conf = config;
|
|
74
76
|
this.logger = logger;
|
|
@@ -128,7 +130,7 @@ export class ModernServer {
|
|
|
128
130
|
const {
|
|
129
131
|
favicon,
|
|
130
132
|
faviconByEntries
|
|
131
|
-
} = this.conf.output;
|
|
133
|
+
} = this.conf.output || {};
|
|
132
134
|
const favicons = this.prepareFavicons(favicon, faviconByEntries); // Only work when without setting `assetPrefix`.
|
|
133
135
|
// Setting `assetPrefix` means these resources should be uploaded to CDN.
|
|
134
136
|
|
|
@@ -313,7 +315,11 @@ export class ModernServer {
|
|
|
313
315
|
}
|
|
314
316
|
|
|
315
317
|
async handleWeb(context, route) {
|
|
316
|
-
return this.routeRenderHandler(
|
|
318
|
+
return this.routeRenderHandler({
|
|
319
|
+
ctx: context,
|
|
320
|
+
route,
|
|
321
|
+
runner: this.runner
|
|
322
|
+
});
|
|
317
323
|
}
|
|
318
324
|
|
|
319
325
|
verifyMatch(_c, _m) {// empty
|
|
@@ -332,7 +338,7 @@ export class ModernServer {
|
|
|
332
338
|
context
|
|
333
339
|
}); // match routes in the route spec
|
|
334
340
|
|
|
335
|
-
const matched = this.router.match(context.
|
|
341
|
+
const matched = this.router.match(context.path);
|
|
336
342
|
|
|
337
343
|
if (!matched) {
|
|
338
344
|
this.render404(context);
|
|
@@ -520,10 +526,9 @@ export class ModernServer {
|
|
|
520
526
|
requestHandler(req, res, next = () => {// empty
|
|
521
527
|
}) {
|
|
522
528
|
res.statusCode = 200;
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
});
|
|
529
|
+
req.logger = req.logger || this.logger;
|
|
530
|
+
req.metrics = req.metrics || this.metrics;
|
|
531
|
+
const context = createContext(req, res);
|
|
527
532
|
|
|
528
533
|
try {
|
|
529
534
|
this._handler(context, next);
|
|
@@ -555,7 +560,11 @@ export class ModernServer {
|
|
|
555
560
|
|
|
556
561
|
if (entryName === status.toString() || entryName === '_error') {
|
|
557
562
|
try {
|
|
558
|
-
const file = await this.routeRenderHandler(
|
|
563
|
+
const file = await this.routeRenderHandler({
|
|
564
|
+
route,
|
|
565
|
+
ctx: context,
|
|
566
|
+
runner: this.runner
|
|
567
|
+
});
|
|
559
568
|
|
|
560
569
|
if (file) {
|
|
561
570
|
context.res.end(file.content);
|
package/dist/js/modern/utils.js
CHANGED
|
@@ -47,12 +47,4 @@ export const createErrorDocument = (status, text) => {
|
|
|
47
47
|
</body>
|
|
48
48
|
</html>
|
|
49
49
|
`;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
export function applyMixins(derivedCtor, constructors) {
|
|
53
|
-
constructors.forEach(baseCtor => {
|
|
54
|
-
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
|
|
55
|
-
Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name) || Object.create(null));
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
}
|
|
50
|
+
};
|
package/dist/js/node/index.js
CHANGED
|
@@ -3,9 +3,6 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
var _exportNames = {
|
|
7
|
-
Server: true
|
|
8
|
-
};
|
|
9
6
|
Object.defineProperty(exports, "Server", {
|
|
10
7
|
enumerable: true,
|
|
11
8
|
get: function () {
|
|
@@ -16,54 +13,9 @@ exports.default = void 0;
|
|
|
16
13
|
|
|
17
14
|
var _server = require("./server");
|
|
18
15
|
|
|
19
|
-
var _type = require("./type");
|
|
20
|
-
|
|
21
|
-
Object.keys(_type).forEach(function (key) {
|
|
22
|
-
if (key === "default" || key === "__esModule") return;
|
|
23
|
-
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
24
|
-
if (key in exports && exports[key] === _type[key]) return;
|
|
25
|
-
Object.defineProperty(exports, key, {
|
|
26
|
-
enumerable: true,
|
|
27
|
-
get: function () {
|
|
28
|
-
return _type[key];
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
var _context = require("./libs/context");
|
|
34
|
-
|
|
35
|
-
Object.keys(_context).forEach(function (key) {
|
|
36
|
-
if (key === "default" || key === "__esModule") return;
|
|
37
|
-
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
38
|
-
if (key in exports && exports[key] === _context[key]) return;
|
|
39
|
-
Object.defineProperty(exports, key, {
|
|
40
|
-
enumerable: true,
|
|
41
|
-
get: function () {
|
|
42
|
-
return _context[key];
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
var _route = require("./libs/route");
|
|
48
|
-
|
|
49
|
-
Object.keys(_route).forEach(function (key) {
|
|
50
|
-
if (key === "default" || key === "__esModule") return;
|
|
51
|
-
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
52
|
-
if (key in exports && exports[key] === _route[key]) return;
|
|
53
|
-
Object.defineProperty(exports, key, {
|
|
54
|
-
enumerable: true,
|
|
55
|
-
get: function () {
|
|
56
|
-
return _route[key];
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
|
|
61
16
|
var _default = options => {
|
|
62
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
63
|
-
const allowEnvs = ['production', 'development', 'test'];
|
|
64
|
-
|
|
65
17
|
if (options == null) {
|
|
66
|
-
throw new Error();
|
|
18
|
+
throw new Error('can not start mserver without options');
|
|
67
19
|
}
|
|
68
20
|
|
|
69
21
|
const server = new _server.Server(options);
|
|
@@ -25,10 +25,7 @@ class ModernServerContext {
|
|
|
25
25
|
/**
|
|
26
26
|
* url params
|
|
27
27
|
*/
|
|
28
|
-
constructor(req, res
|
|
29
|
-
logger,
|
|
30
|
-
metrics
|
|
31
|
-
}) {
|
|
28
|
+
constructor(req, res) {
|
|
32
29
|
this.req = void 0;
|
|
33
30
|
this.res = void 0;
|
|
34
31
|
this.params = {};
|
|
@@ -36,8 +33,8 @@ class ModernServerContext {
|
|
|
36
33
|
this.metrics = void 0;
|
|
37
34
|
this.req = req;
|
|
38
35
|
this.res = res;
|
|
39
|
-
this.logger = logger;
|
|
40
|
-
this.metrics = metrics;
|
|
36
|
+
this.logger = req.logger;
|
|
37
|
+
this.metrics = req.metrics;
|
|
41
38
|
this.bind();
|
|
42
39
|
}
|
|
43
40
|
|
|
@@ -13,12 +13,6 @@ exports.createContext = void 0;
|
|
|
13
13
|
|
|
14
14
|
var _context = require("./context");
|
|
15
15
|
|
|
16
|
-
const createContext = (req, res,
|
|
17
|
-
logger,
|
|
18
|
-
metrics
|
|
19
|
-
}) => new _context.ModernServerContext(req, res, {
|
|
20
|
-
logger,
|
|
21
|
-
metrics
|
|
22
|
-
});
|
|
16
|
+
const createContext = (req, res) => new _context.ModernServerContext(req, res);
|
|
23
17
|
|
|
24
18
|
exports.createContext = createContext;
|
|
@@ -24,6 +24,7 @@ const createCacheConfig = (config = {}) => _objectSpread({
|
|
|
24
24
|
matches: null
|
|
25
25
|
}, config);
|
|
26
26
|
|
|
27
|
+
jest.setTimeout(60000);
|
|
27
28
|
describe('cache', () => {
|
|
28
29
|
it('should cache correctly', async () => {
|
|
29
30
|
(0, _spr.destroyCache)();
|
|
@@ -183,7 +184,6 @@ describe('cache', () => {
|
|
|
183
184
|
}
|
|
184
185
|
}
|
|
185
186
|
});
|
|
186
|
-
jest.setTimeout(1000 * 10);
|
|
187
187
|
it('should stale cache correctly', async () => {
|
|
188
188
|
(0, _spr.destroyCache)();
|
|
189
189
|
const cache = (0, _spr.createCache)();
|
|
@@ -209,7 +209,6 @@ describe('cache', () => {
|
|
|
209
209
|
const staleResult = await cache.get(context);
|
|
210
210
|
expect(staleResult === null || staleResult === void 0 ? void 0 : staleResult.isStale).toBe(true);
|
|
211
211
|
});
|
|
212
|
-
jest.setTimeout(1000 * 15);
|
|
213
212
|
it('should garbage cache correctly', async () => {
|
|
214
213
|
(0, _spr.destroyCache)();
|
|
215
214
|
const cache = (0, _spr.createCache)();
|
|
@@ -30,7 +30,11 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
30
30
|
const createRenderHandler = ({
|
|
31
31
|
distDir,
|
|
32
32
|
staticGenerate
|
|
33
|
-
}) => async function render(
|
|
33
|
+
}) => async function render({
|
|
34
|
+
ctx,
|
|
35
|
+
route,
|
|
36
|
+
runner
|
|
37
|
+
}) {
|
|
34
38
|
if (ctx.resHasHandled()) {
|
|
35
39
|
return null;
|
|
36
40
|
}
|
|
@@ -63,7 +67,7 @@ const createRenderHandler = ({
|
|
|
63
67
|
bundle: route.bundle,
|
|
64
68
|
template: templateHTML,
|
|
65
69
|
staticGenerate
|
|
66
|
-
});
|
|
70
|
+
}, runner);
|
|
67
71
|
return result;
|
|
68
72
|
} catch (err) {
|
|
69
73
|
ctx.error(_constants.ERROR_DIGEST.ERENDER, err.stack);
|
|
@@ -26,9 +26,9 @@ const createCacheItem = async (filepath, mtime) => {
|
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
class LruReader {
|
|
29
|
+
// private timer?: NodeJS.Timeout;
|
|
29
30
|
constructor() {
|
|
30
31
|
this.cache = void 0;
|
|
31
|
-
this.timer = void 0;
|
|
32
32
|
this.cache = new _lruCache.default({
|
|
33
33
|
max: 256 * MB,
|
|
34
34
|
length: getContentLength,
|
|
@@ -37,14 +37,12 @@ class LruReader {
|
|
|
37
37
|
});
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
init() {
|
|
41
|
-
this.timeTask();
|
|
40
|
+
init() {// this.timeTask();
|
|
42
41
|
}
|
|
43
42
|
|
|
44
|
-
close() {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
43
|
+
close() {// if (this.timer) {
|
|
44
|
+
// clearInterval(this.timer);
|
|
45
|
+
// }
|
|
48
46
|
}
|
|
49
47
|
|
|
50
48
|
async read(filepath) {
|
|
@@ -105,11 +103,10 @@ class LruReader {
|
|
|
105
103
|
cache.del(filepath);
|
|
106
104
|
}
|
|
107
105
|
}
|
|
108
|
-
}
|
|
106
|
+
} // private timeTask() {
|
|
107
|
+
// this.timer = setInterval(() => this.update, 5 * 60 * 1000).unref();
|
|
108
|
+
// }
|
|
109
109
|
|
|
110
|
-
timeTask() {
|
|
111
|
-
this.timer = setInterval(() => this.update, 5 * 60 * 1000).unref();
|
|
112
|
-
}
|
|
113
110
|
|
|
114
111
|
}
|
|
115
112
|
|
|
@@ -15,7 +15,7 @@ var _cache = _interopRequireDefault(require("./cache"));
|
|
|
15
15
|
|
|
16
16
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
17
|
|
|
18
|
-
const render = async (ctx, renderOptions) => {
|
|
18
|
+
const render = async (ctx, renderOptions, runner) => {
|
|
19
19
|
const {
|
|
20
20
|
bundle,
|
|
21
21
|
distDir,
|
|
@@ -42,6 +42,7 @@ const render = async (ctx, renderOptions) => {
|
|
|
42
42
|
logger: ctx.logger,
|
|
43
43
|
metrics: ctx.metrics
|
|
44
44
|
};
|
|
45
|
+
runner.extendSSRContext(context);
|
|
45
46
|
|
|
46
47
|
const serverRender = require(bundleJS)[_utils.SERVER_RENDER_FUNCTION_NAME];
|
|
47
48
|
|
|
@@ -4,6 +4,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.RouteMatchManager = void 0;
|
|
7
|
+
Object.defineProperty(exports, "RouteMatcher", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () {
|
|
10
|
+
return _matcher.RouteMatcher;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
7
13
|
|
|
8
14
|
var _matcher = require("./matcher");
|
|
9
15
|
|
|
@@ -137,10 +137,12 @@ class Server {
|
|
|
137
137
|
const appContext = await this.initAppContext();
|
|
138
138
|
|
|
139
139
|
_serverPlugin.serverManager.run(() => {
|
|
140
|
+
var _options$config$outpu;
|
|
141
|
+
|
|
140
142
|
_core.ConfigContext.set(this.options.config);
|
|
141
143
|
|
|
142
144
|
_core.AppContext.set(_objectSpread(_objectSpread({}, appContext), {}, {
|
|
143
|
-
distDirectory: _path.default.join(options.pwd, options.config.output.path || 'dist')
|
|
145
|
+
distDirectory: _path.default.join(options.pwd, ((_options$config$outpu = options.config.output) === null || _options$config$outpu === void 0 ? void 0 : _options$config$outpu.path) || 'dist')
|
|
144
146
|
}));
|
|
145
147
|
});
|
|
146
148
|
|
|
@@ -74,6 +74,8 @@ class ModernServer {
|
|
|
74
74
|
metrics,
|
|
75
75
|
proxyTarget
|
|
76
76
|
}) {
|
|
77
|
+
var _config$output;
|
|
78
|
+
|
|
77
79
|
this.pwd = void 0;
|
|
78
80
|
this.distDir = void 0;
|
|
79
81
|
this.workDir = void 0;
|
|
@@ -90,15 +92,15 @@ class ModernServer {
|
|
|
90
92
|
this.routeRenderHandler = void 0;
|
|
91
93
|
this.frameWebHandler = null;
|
|
92
94
|
this.frameAPIHandler = null;
|
|
95
|
+
this.proxyHandler = null;
|
|
93
96
|
this._handler = void 0;
|
|
94
97
|
this.staticGenerate = false;
|
|
95
|
-
this.proxyHandler = null;
|
|
96
98
|
|
|
97
99
|
require('ignore-styles');
|
|
98
100
|
|
|
99
101
|
this.isDev = Boolean(dev);
|
|
100
102
|
this.pwd = pwd;
|
|
101
|
-
this.distDir = _path.default.join(pwd, config.output.path || 'dist');
|
|
103
|
+
this.distDir = _path.default.join(pwd, ((_config$output = config.output) === null || _config$output === void 0 ? void 0 : _config$output.path) || 'dist');
|
|
102
104
|
this.workDir = this.isDev ? pwd : this.distDir;
|
|
103
105
|
this.conf = config;
|
|
104
106
|
this.logger = logger;
|
|
@@ -158,7 +160,7 @@ class ModernServer {
|
|
|
158
160
|
const {
|
|
159
161
|
favicon,
|
|
160
162
|
faviconByEntries
|
|
161
|
-
} = this.conf.output;
|
|
163
|
+
} = this.conf.output || {};
|
|
162
164
|
const favicons = this.prepareFavicons(favicon, faviconByEntries); // Only work when without setting `assetPrefix`.
|
|
163
165
|
// Setting `assetPrefix` means these resources should be uploaded to CDN.
|
|
164
166
|
|
|
@@ -348,7 +350,11 @@ class ModernServer {
|
|
|
348
350
|
}
|
|
349
351
|
|
|
350
352
|
async handleWeb(context, route) {
|
|
351
|
-
return this.routeRenderHandler(
|
|
353
|
+
return this.routeRenderHandler({
|
|
354
|
+
ctx: context,
|
|
355
|
+
route,
|
|
356
|
+
runner: this.runner
|
|
357
|
+
});
|
|
352
358
|
}
|
|
353
359
|
|
|
354
360
|
verifyMatch(_c, _m) {// empty
|
|
@@ -367,7 +373,7 @@ class ModernServer {
|
|
|
367
373
|
context
|
|
368
374
|
}); // match routes in the route spec
|
|
369
375
|
|
|
370
|
-
const matched = this.router.match(context.
|
|
376
|
+
const matched = this.router.match(context.path);
|
|
371
377
|
|
|
372
378
|
if (!matched) {
|
|
373
379
|
this.render404(context);
|
|
@@ -555,10 +561,9 @@ class ModernServer {
|
|
|
555
561
|
requestHandler(req, res, next = () => {// empty
|
|
556
562
|
}) {
|
|
557
563
|
res.statusCode = 200;
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
});
|
|
564
|
+
req.logger = req.logger || this.logger;
|
|
565
|
+
req.metrics = req.metrics || this.metrics;
|
|
566
|
+
const context = (0, _context.createContext)(req, res);
|
|
562
567
|
|
|
563
568
|
try {
|
|
564
569
|
this._handler(context, next);
|
|
@@ -590,7 +595,11 @@ class ModernServer {
|
|
|
590
595
|
|
|
591
596
|
if (entryName === status.toString() || entryName === '_error') {
|
|
592
597
|
try {
|
|
593
|
-
const file = await this.routeRenderHandler(
|
|
598
|
+
const file = await this.routeRenderHandler({
|
|
599
|
+
route,
|
|
600
|
+
ctx: context,
|
|
601
|
+
runner: this.runner
|
|
602
|
+
});
|
|
594
603
|
|
|
595
604
|
if (file) {
|
|
596
605
|
context.res.end(file.content);
|
package/dist/js/node/utils.js
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.applyMixins = applyMixins;
|
|
7
6
|
exports.toMessage = exports.noop = exports.mergeExtension = exports.createErrorDocument = void 0;
|
|
8
7
|
|
|
9
8
|
const mergeExtension = users => {
|
|
@@ -64,15 +63,6 @@ const createErrorDocument = (status, text) => {
|
|
|
64
63
|
</body>
|
|
65
64
|
</html>
|
|
66
65
|
`;
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
exports.createErrorDocument = createErrorDocument;
|
|
66
|
+
};
|
|
71
67
|
|
|
72
|
-
|
|
73
|
-
constructors.forEach(baseCtor => {
|
|
74
|
-
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
|
|
75
|
-
Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name) || Object.create(null));
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
}
|
|
68
|
+
exports.createErrorDocument = createErrorDocument;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { ModernServerOptions } from './type';
|
|
2
2
|
import { Server } from './server';
|
|
3
|
-
export * from './type';
|
|
4
|
-
export * from './libs/context';
|
|
5
|
-
export * from './libs/route';
|
|
6
3
|
export type { SSRServerContext } from './libs/render/type';
|
|
7
4
|
export { Server };
|
|
5
|
+
export type { ModernServerOptions };
|
|
8
6
|
|
|
9
7
|
declare const _default: (options: ModernServerOptions) => Promise<Server>;
|
|
10
8
|
|