@midwayjs/koa 4.0.0-alpha.1 → 4.0.0-beta.2
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/README.md +1 -1
- package/dist/config/config.default.d.ts +13 -1
- package/dist/config/config.default.js +34 -20
- package/dist/framework.js +26 -13
- package/dist/interface.d.ts +13 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +18 -1
- package/package.json +13 -12
package/README.md
CHANGED
|
@@ -2,9 +2,15 @@
|
|
|
2
2
|
* koa server options
|
|
3
3
|
*/
|
|
4
4
|
export declare const koa: {
|
|
5
|
-
contextLoggerFormat: (info: any) => string;
|
|
6
5
|
serverTimeout: number;
|
|
7
6
|
};
|
|
7
|
+
export declare const midwayLogger: {
|
|
8
|
+
clients: {
|
|
9
|
+
appLogger: {
|
|
10
|
+
contextLoggerFormat: (info: any) => string;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
};
|
|
8
14
|
/**
|
|
9
15
|
* The key that signing cookies. It can contain multiple keys seperated by `,`.
|
|
10
16
|
* @member {String} Config#keys
|
|
@@ -18,6 +24,12 @@ export declare const keys = "";
|
|
|
18
24
|
* @property {Boolean} httpOnly - httpOnly property, defaults is true
|
|
19
25
|
*/
|
|
20
26
|
export declare const cookies: {};
|
|
27
|
+
/**
|
|
28
|
+
* default cookie get options
|
|
29
|
+
*/
|
|
30
|
+
export declare const cookiesExtra: {
|
|
31
|
+
defaultGetOptions: {};
|
|
32
|
+
};
|
|
21
33
|
export declare const onerror: {};
|
|
22
34
|
/**
|
|
23
35
|
* @member Config#bodyParser
|
|
@@ -1,31 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.siteFile = exports.bodyParser = exports.onerror = exports.cookies = exports.keys = exports.koa = void 0;
|
|
3
|
+
exports.siteFile = exports.bodyParser = exports.onerror = exports.cookiesExtra = exports.cookies = exports.keys = exports.midwayLogger = exports.koa = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* koa server options
|
|
6
6
|
*/
|
|
7
7
|
exports.koa = {
|
|
8
|
-
contextLoggerFormat: info => {
|
|
9
|
-
const ctx = info.ctx;
|
|
10
|
-
// format: '[$userId/$ip/$traceId/$use_ms $method $url]'
|
|
11
|
-
const userId = ctx.userId || '-';
|
|
12
|
-
const traceId = ctx.traceId ?? ctx.tracer?.traceId ?? '-';
|
|
13
|
-
const use = Date.now() - ctx.startTime;
|
|
14
|
-
const label = userId +
|
|
15
|
-
'/' +
|
|
16
|
-
ctx.ip +
|
|
17
|
-
'/' +
|
|
18
|
-
traceId +
|
|
19
|
-
'/' +
|
|
20
|
-
use +
|
|
21
|
-
'ms ' +
|
|
22
|
-
ctx.method +
|
|
23
|
-
' ' +
|
|
24
|
-
ctx.url;
|
|
25
|
-
return `${info.timestamp} ${info.LEVEL} ${info.pid} [${label}] ${info.message}`;
|
|
26
|
-
},
|
|
27
8
|
serverTimeout: 2 * 60 * 1000,
|
|
28
9
|
};
|
|
10
|
+
exports.midwayLogger = {
|
|
11
|
+
clients: {
|
|
12
|
+
appLogger: {
|
|
13
|
+
contextLoggerFormat: info => {
|
|
14
|
+
const ctx = info.ctx;
|
|
15
|
+
// format: '[$userId/$ip/$traceId/$use_ms $method $url]'
|
|
16
|
+
const userId = ctx.userId || '-';
|
|
17
|
+
const traceId = ctx.traceId ?? ctx.tracer?.traceId ?? '-';
|
|
18
|
+
const use = Date.now() - ctx.startTime;
|
|
19
|
+
const label = userId +
|
|
20
|
+
'/' +
|
|
21
|
+
ctx.ip +
|
|
22
|
+
'/' +
|
|
23
|
+
traceId +
|
|
24
|
+
'/' +
|
|
25
|
+
use +
|
|
26
|
+
'ms ' +
|
|
27
|
+
ctx.method +
|
|
28
|
+
' ' +
|
|
29
|
+
ctx.url;
|
|
30
|
+
return `${info.timestamp} ${info.LEVEL} ${info.pid} [${label}] ${info.message}`;
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
};
|
|
29
35
|
/**
|
|
30
36
|
* The key that signing cookies. It can contain multiple keys seperated by `,`.
|
|
31
37
|
* @member {String} Config#keys
|
|
@@ -42,6 +48,14 @@ exports.cookies = {
|
|
|
42
48
|
// httpOnly: true | false,
|
|
43
49
|
// sameSite: 'none|lax|strict',
|
|
44
50
|
};
|
|
51
|
+
/**
|
|
52
|
+
* default cookie get options
|
|
53
|
+
*/
|
|
54
|
+
exports.cookiesExtra = {
|
|
55
|
+
defaultGetOptions: {
|
|
56
|
+
// sign: false,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
45
59
|
exports.onerror = {};
|
|
46
60
|
/**
|
|
47
61
|
* @member Config#bodyParser
|
package/dist/framework.js
CHANGED
|
@@ -14,6 +14,7 @@ const koa = require("koa");
|
|
|
14
14
|
const onerror_1 = require("./onerror");
|
|
15
15
|
const qs = require("qs");
|
|
16
16
|
const querystring = require("querystring");
|
|
17
|
+
const utils_1 = require("./utils");
|
|
17
18
|
const COOKIES = Symbol('context#cookies');
|
|
18
19
|
class KoaControllerGenerator extends core_1.WebControllerGenerator {
|
|
19
20
|
constructor(app, webRouterService) {
|
|
@@ -41,6 +42,7 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
|
|
|
41
42
|
throw new core_1.MidwayConfigMissingError('config.keys');
|
|
42
43
|
}
|
|
43
44
|
const cookieOptions = this.configService.getConfiguration('cookies');
|
|
45
|
+
const cookieGetOptions = this.configService.getConfiguration('cookiesExtra.defaultGetOptions');
|
|
44
46
|
this.app = new koa({
|
|
45
47
|
keys: [].concat(appKeys),
|
|
46
48
|
proxy: this.configurationOptions.proxy,
|
|
@@ -51,7 +53,7 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
|
|
|
51
53
|
Object.defineProperty(this.app.context, 'cookies', {
|
|
52
54
|
get() {
|
|
53
55
|
if (!this[COOKIES]) {
|
|
54
|
-
this[COOKIES] = new cookies_1.Cookies(this, this.app.keys, cookieOptions);
|
|
56
|
+
this[COOKIES] = new cookies_1.Cookies(this, this.app.keys, cookieOptions, cookieGetOptions);
|
|
55
57
|
}
|
|
56
58
|
return this[COOKIES];
|
|
57
59
|
},
|
|
@@ -190,9 +192,9 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
|
|
|
190
192
|
};
|
|
191
193
|
// https config
|
|
192
194
|
if (serverOptions.key && serverOptions.cert) {
|
|
193
|
-
serverOptions.key = core_1.
|
|
194
|
-
serverOptions.cert = core_1.
|
|
195
|
-
serverOptions.ca = core_1.
|
|
195
|
+
serverOptions.key = core_1.PathFileUtils.getFileContentSync(serverOptions.key);
|
|
196
|
+
serverOptions.cert = core_1.PathFileUtils.getFileContentSync(serverOptions.cert);
|
|
197
|
+
serverOptions.ca = core_1.PathFileUtils.getFileContentSync(serverOptions.ca);
|
|
196
198
|
process.env.MIDWAY_HTTP_SSL = 'true';
|
|
197
199
|
if (serverOptions.http2) {
|
|
198
200
|
this.server = require('http2').createSecureServer(serverOptions, this.app.callback());
|
|
@@ -215,27 +217,38 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
|
|
|
215
217
|
if (core_1.Types.isNumber(this.configurationOptions.serverTimeout)) {
|
|
216
218
|
this.server.setTimeout(this.configurationOptions.serverTimeout);
|
|
217
219
|
}
|
|
220
|
+
this.configurationOptions.listenOptions = {
|
|
221
|
+
port: this.configurationOptions.port,
|
|
222
|
+
host: this.configurationOptions.hostname,
|
|
223
|
+
...this.configurationOptions.listenOptions,
|
|
224
|
+
};
|
|
218
225
|
// set port and listen server
|
|
219
|
-
|
|
220
|
-
|
|
226
|
+
let customPort = process.env.MIDWAY_HTTP_PORT ||
|
|
227
|
+
this.configurationOptions.listenOptions.port;
|
|
228
|
+
if (customPort === 0 || customPort === '0') {
|
|
229
|
+
customPort = await (0, utils_1.getFreePort)();
|
|
230
|
+
this.logger.info(`[midway:koa] server has auto-assigned port ${customPort}`);
|
|
231
|
+
}
|
|
232
|
+
this.configurationOptions.listenOptions.port = Number(customPort);
|
|
233
|
+
if (this.configurationOptions.listenOptions.port) {
|
|
221
234
|
new Promise(resolve => {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
args.push(this.configurationOptions.hostname);
|
|
225
|
-
}
|
|
226
|
-
args.push(() => {
|
|
235
|
+
// 使用 ListenOptions 对象启动服务器
|
|
236
|
+
this.server.listen(this.configurationOptions.listenOptions, () => {
|
|
227
237
|
resolve();
|
|
228
238
|
});
|
|
229
|
-
|
|
230
|
-
process.env.MIDWAY_HTTP_PORT = String(
|
|
239
|
+
// 设置环境变量
|
|
240
|
+
process.env.MIDWAY_HTTP_PORT = String(this.configurationOptions.listenOptions.port);
|
|
231
241
|
});
|
|
242
|
+
this.logger.debug(`[midway:koa] server is listening on port ${customPort}`);
|
|
232
243
|
}
|
|
233
244
|
}
|
|
234
245
|
async beforeStop() {
|
|
235
246
|
if (this.server) {
|
|
236
247
|
new Promise(resolve => {
|
|
237
248
|
this.server.close(resolve);
|
|
249
|
+
process.env.MIDWAY_HTTP_PORT = '';
|
|
238
250
|
});
|
|
251
|
+
this.logger.debug('[midway:koa] server is stopped!');
|
|
239
252
|
}
|
|
240
253
|
}
|
|
241
254
|
getFrameworkName() {
|
package/dist/interface.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
|
+
/// <reference types="node" />
|
|
3
4
|
import { IConfigurationOptions, IMidwayApplication, IMidwayContext } from '@midwayjs/core';
|
|
4
5
|
import * as koa from 'koa';
|
|
5
6
|
import { Context as KoaContext, DefaultState, Middleware, Next } from 'koa';
|
|
6
7
|
import { RouterParamValue } from '@midwayjs/core';
|
|
7
8
|
import * as qs from 'qs';
|
|
9
|
+
import { ListenOptions } from 'net';
|
|
8
10
|
export interface State extends DefaultState {
|
|
9
11
|
}
|
|
10
12
|
export type IMidwayKoaContext = IMidwayContext<KoaContext>;
|
|
@@ -15,6 +17,10 @@ export type IMidwayKoaApplication = IMidwayApplication<IMidwayKoaContext, koa<St
|
|
|
15
17
|
* @param middlewareId
|
|
16
18
|
*/
|
|
17
19
|
generateMiddleware(middlewareId: any): Promise<Middleware<State, IMidwayKoaContext>>;
|
|
20
|
+
/**
|
|
21
|
+
* Get the port that the application is listening on
|
|
22
|
+
*/
|
|
23
|
+
getPort(): string;
|
|
18
24
|
}>;
|
|
19
25
|
/**
|
|
20
26
|
* @deprecated use NextFunction definition
|
|
@@ -86,7 +92,14 @@ export interface IMidwayKoaConfigurationOptions extends IConfigurationOptions {
|
|
|
86
92
|
* qs options
|
|
87
93
|
*/
|
|
88
94
|
queryParseOptions?: qs.IParseOptions;
|
|
95
|
+
/**
|
|
96
|
+
* https/https/http2 server options
|
|
97
|
+
*/
|
|
89
98
|
serverOptions?: Record<string, any>;
|
|
99
|
+
/**
|
|
100
|
+
* listen options
|
|
101
|
+
*/
|
|
102
|
+
listenOptions?: ListenOptions;
|
|
90
103
|
}
|
|
91
104
|
export type MiddlewareParamArray = Array<Middleware<DefaultState, IMidwayKoaContext>>;
|
|
92
105
|
export interface IWebMiddleware {
|
package/dist/utils.d.ts
CHANGED
package/dist/utils.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.escapeHtml = exports.tpl = exports.isProduction = exports.sendToWormhole = exports.accepts = exports.detectStatus = void 0;
|
|
3
|
+
exports.getFreePort = exports.escapeHtml = exports.tpl = exports.isProduction = exports.sendToWormhole = exports.accepts = exports.detectStatus = void 0;
|
|
4
|
+
const net_1 = require("net");
|
|
4
5
|
function detectStatus(err) {
|
|
5
6
|
// detect status
|
|
6
7
|
let status = err.status || 500;
|
|
@@ -153,4 +154,20 @@ function escapeHtml(string) {
|
|
|
153
154
|
return lastIndex !== index ? html + str.substring(lastIndex, index) : html;
|
|
154
155
|
}
|
|
155
156
|
exports.escapeHtml = escapeHtml;
|
|
157
|
+
async function getFreePort() {
|
|
158
|
+
return new Promise((resolve, reject) => {
|
|
159
|
+
const server = (0, net_1.createServer)();
|
|
160
|
+
server.listen(0, () => {
|
|
161
|
+
try {
|
|
162
|
+
const port = server.address().port;
|
|
163
|
+
server.close();
|
|
164
|
+
resolve(port);
|
|
165
|
+
}
|
|
166
|
+
catch (err) {
|
|
167
|
+
reject(err);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
exports.getFreePort = getFreePort;
|
|
156
173
|
//# sourceMappingURL=utils.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/koa",
|
|
3
|
-
"version": "4.0.0-
|
|
3
|
+
"version": "4.0.0-beta.2",
|
|
4
4
|
"description": "Midway Web Framework for KOA",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -24,20 +24,21 @@
|
|
|
24
24
|
],
|
|
25
25
|
"license": "MIT",
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@midwayjs/core": "^4.0.0-
|
|
28
|
-
"@midwayjs/mock": "^4.0.0-
|
|
27
|
+
"@midwayjs/core": "^4.0.0-beta.2",
|
|
28
|
+
"@midwayjs/mock": "^4.0.0-beta.2",
|
|
29
29
|
"@types/koa-router": "7.4.8",
|
|
30
|
-
"
|
|
30
|
+
"axios": "1.12.0",
|
|
31
|
+
"fs-extra": "11.3.0"
|
|
31
32
|
},
|
|
32
33
|
"dependencies": {
|
|
33
34
|
"@koa/router": "^12.0.0",
|
|
34
|
-
"@midwayjs/cookies": "^1.0
|
|
35
|
-
"@midwayjs/session": "^4.0.0-
|
|
36
|
-
"@types/koa": "
|
|
37
|
-
"@types/qs": "6.9.
|
|
38
|
-
"koa": "
|
|
35
|
+
"@midwayjs/cookies": "^1.3.0",
|
|
36
|
+
"@midwayjs/session": "^4.0.0-beta.2",
|
|
37
|
+
"@types/koa": "3.0.0",
|
|
38
|
+
"@types/qs": "6.9.18",
|
|
39
|
+
"koa": "3.0.1",
|
|
39
40
|
"koa-bodyparser": "4.4.1",
|
|
40
|
-
"qs": "6.
|
|
41
|
+
"qs": "6.14.0"
|
|
41
42
|
},
|
|
42
43
|
"author": "Harry Chen <czy88840616@gmail.com>",
|
|
43
44
|
"repository": {
|
|
@@ -45,7 +46,7 @@
|
|
|
45
46
|
"url": "https://github.com/midwayjs/midway.git"
|
|
46
47
|
},
|
|
47
48
|
"engines": {
|
|
48
|
-
"node": ">=
|
|
49
|
+
"node": ">=20"
|
|
49
50
|
},
|
|
50
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "53bfef4c5279da5f09025e4610bdbf64f94f60bd"
|
|
51
52
|
}
|