@eggjs/koa 2.17.0 → 2.18.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/Readme.md +9 -8
- package/{lib → dist/commonjs}/application.d.ts +25 -13
- package/dist/commonjs/application.js +255 -0
- package/{lib → dist/commonjs}/context.d.ts +72 -22
- package/dist/commonjs/context.js +209 -0
- package/dist/commonjs/index.d.ts +7 -0
- package/dist/commonjs/index.js +27 -0
- package/dist/commonjs/package.json +3 -0
- package/{lib → dist/commonjs}/request.d.ts +22 -10
- package/{lib → dist/commonjs}/request.js +9 -8
- package/{lib → dist/commonjs}/response.d.ts +24 -9
- package/dist/commonjs/response.js +453 -0
- package/{lib → dist/commonjs}/types.d.ts +1 -2
- package/{lib → dist/commonjs}/types.js +1 -1
- package/dist/esm/application.d.ts +122 -0
- package/dist/esm/application.js +249 -0
- package/dist/esm/context.d.ts +154 -0
- package/dist/esm/context.js +203 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/request.d.ts +329 -0
- package/dist/esm/request.js +522 -0
- package/dist/esm/response.d.ts +225 -0
- package/dist/esm/response.js +447 -0
- package/dist/esm/types.d.ts +10 -0
- package/dist/esm/types.js +2 -0
- package/package.json +65 -31
- package/src/application.ts +276 -0
- package/src/context.ts +284 -0
- package/src/index.ts +9 -0
- package/src/request.ts +580 -0
- package/src/response.ts +473 -0
- package/src/types.ts +11 -0
- package/lib/application.js +0 -264
- package/lib/context.js +0 -221
- package/lib/response.js +0 -447
package/package.json
CHANGED
|
@@ -1,22 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eggjs/koa",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
"version": "2.18.0",
|
|
4
|
+
"engines": {
|
|
5
|
+
"node": ">= 18.7.0"
|
|
6
|
+
},
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"description": "Koa web app framework for https://eggjs.org",
|
|
11
11
|
"scripts": {
|
|
12
|
-
"test": "egg-bin test",
|
|
13
|
-
"ci": "egg-bin cov && npm run
|
|
12
|
+
"test": "npm run lint -- --fix && egg-bin test",
|
|
13
|
+
"ci": "npm run lint && egg-bin cov && npm run prepublishOnly",
|
|
14
14
|
"lint": "eslint src test",
|
|
15
|
-
"tsd": "npm run tsc && tsd",
|
|
16
15
|
"authors": "git log --format='%aN <%aE>' | sort -u > AUTHORS",
|
|
17
|
-
"
|
|
18
|
-
"tsc": "tsc",
|
|
19
|
-
"prepublishOnly": "npm run tsc"
|
|
16
|
+
"prepublishOnly": "tshy && tshy-after"
|
|
20
17
|
},
|
|
21
18
|
"repository": {
|
|
22
19
|
"type": "git",
|
|
@@ -34,9 +31,9 @@
|
|
|
34
31
|
"license": "MIT",
|
|
35
32
|
"dependencies": {
|
|
36
33
|
"accepts": "^1.3.5",
|
|
37
|
-
"cache-content-type": "^
|
|
38
|
-
"content-disposition": "~0.5.
|
|
39
|
-
"content-type": "^1.0.
|
|
34
|
+
"cache-content-type": "^2.0.0",
|
|
35
|
+
"content-disposition": "~0.5.4",
|
|
36
|
+
"content-type": "^1.0.5",
|
|
40
37
|
"cookies": "~0.8.0",
|
|
41
38
|
"delegates": "^1.0.0",
|
|
42
39
|
"destroy": "^1.0.4",
|
|
@@ -45,32 +42,69 @@
|
|
|
45
42
|
"fresh": "~0.5.2",
|
|
46
43
|
"gals": "^1.0.1",
|
|
47
44
|
"http-assert": "^1.3.0",
|
|
48
|
-
"http-errors": "^
|
|
49
|
-
"is-
|
|
45
|
+
"http-errors": "^2.0.0",
|
|
46
|
+
"is-type-of": "^2.1.0",
|
|
50
47
|
"koa-compose": "^4.1.0",
|
|
51
|
-
"on-finished": "^2.
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"type-is": "^1.6.16",
|
|
48
|
+
"on-finished": "^2.4.1",
|
|
49
|
+
"parseurl": "^1.3.3",
|
|
50
|
+
"statuses": "^2.0.1",
|
|
51
|
+
"type-is": "^1.6.18",
|
|
56
52
|
"vary": "^1.1.2"
|
|
57
53
|
},
|
|
58
54
|
"devDependencies": {
|
|
59
55
|
"@eggjs/tsconfig": "^1.3.3",
|
|
56
|
+
"@types/content-type": "^1.1.8",
|
|
57
|
+
"@types/delegates": "^1.0.3",
|
|
58
|
+
"@types/destroy": "^1.0.3",
|
|
59
|
+
"@types/encodeurl": "^1.0.2",
|
|
60
|
+
"@types/escape-html": "^1.0.4",
|
|
61
|
+
"@types/fresh": "^0.5.2",
|
|
62
|
+
"@types/http-errors": "^2.0.4",
|
|
63
|
+
"@types/koa-compose": "^3.2.8",
|
|
60
64
|
"@types/mocha": "^10.0.1",
|
|
61
65
|
"@types/node": "^20.2.5",
|
|
66
|
+
"@types/on-finished": "^2.3.4",
|
|
67
|
+
"@types/parseurl": "^1.3.3",
|
|
68
|
+
"@types/statuses": "^2.0.5",
|
|
69
|
+
"@types/supertest": "^6.0.2",
|
|
70
|
+
"@types/type-is": "^1.6.6",
|
|
71
|
+
"@types/vary": "^1.1.3",
|
|
62
72
|
"egg-bin": "^6.4.0",
|
|
63
73
|
"eslint": "^8.41.0",
|
|
64
74
|
"eslint-config-egg": "^13.1.0",
|
|
65
75
|
"mm": "^3.3.0",
|
|
66
76
|
"supertest": "^3.1.0",
|
|
67
|
-
"tsd": "^0.
|
|
68
|
-
"
|
|
77
|
+
"tsd": "^0.31.0",
|
|
78
|
+
"tshy": "^1.15.1",
|
|
79
|
+
"tshy-after": "^1.0.0",
|
|
80
|
+
"typescript": "^5.4.5"
|
|
69
81
|
},
|
|
70
|
-
"
|
|
71
|
-
|
|
82
|
+
"type": "module",
|
|
83
|
+
"tshy": {
|
|
84
|
+
"exports": {
|
|
85
|
+
"./package.json": "./package.json",
|
|
86
|
+
".": "./src/index.ts"
|
|
87
|
+
}
|
|
72
88
|
},
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
|
|
89
|
+
"exports": {
|
|
90
|
+
"./package.json": "./package.json",
|
|
91
|
+
".": {
|
|
92
|
+
"import": {
|
|
93
|
+
"source": "./src/index.ts",
|
|
94
|
+
"types": "./dist/esm/index.d.ts",
|
|
95
|
+
"default": "./dist/esm/index.js"
|
|
96
|
+
},
|
|
97
|
+
"require": {
|
|
98
|
+
"source": "./src/index.ts",
|
|
99
|
+
"types": "./dist/commonjs/index.d.ts",
|
|
100
|
+
"default": "./dist/commonjs/index.js"
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
"files": [
|
|
105
|
+
"dist",
|
|
106
|
+
"src"
|
|
107
|
+
],
|
|
108
|
+
"main": "./dist/commonjs/index.js",
|
|
109
|
+
"types": "./dist/commonjs/index.d.ts"
|
|
76
110
|
}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { debuglog } from 'node:util';
|
|
2
|
+
import Emitter from 'node:events';
|
|
3
|
+
import util from 'node:util';
|
|
4
|
+
import Stream from 'node:stream';
|
|
5
|
+
import http from 'node:http';
|
|
6
|
+
import type { AsyncLocalStorage } from 'node:async_hooks';
|
|
7
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
8
|
+
import { getAsyncLocalStorage } from 'gals';
|
|
9
|
+
import { isGeneratorFunction } from 'is-type-of';
|
|
10
|
+
import onFinished from 'on-finished';
|
|
11
|
+
import statuses from 'statuses';
|
|
12
|
+
import compose from 'koa-compose';
|
|
13
|
+
import { HttpError } from 'http-errors';
|
|
14
|
+
import Context from './context.js';
|
|
15
|
+
import Request from './request.js';
|
|
16
|
+
import Response from './response.js';
|
|
17
|
+
import type { ContextDelegation } from './context.js';
|
|
18
|
+
import type { CustomError, AnyProto } from './types.js';
|
|
19
|
+
|
|
20
|
+
const debug = debuglog('koa:application');
|
|
21
|
+
|
|
22
|
+
export type ProtoImplClass<T = object> = new(...args: any[]) => T;
|
|
23
|
+
export type Next = () => Promise<void>;
|
|
24
|
+
export type MiddlewareFunc = (ctx: ContextDelegation, next: Next) => Promise<void> | void;
|
|
25
|
+
export type { ContextDelegation as Context } from './context.js';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Expose `Application` class.
|
|
29
|
+
* Inherits from `Emitter.prototype`.
|
|
30
|
+
*/
|
|
31
|
+
export default class Application extends Emitter {
|
|
32
|
+
/**
|
|
33
|
+
* Make HttpError available to consumers of the library so that consumers don't
|
|
34
|
+
* have a direct dependency upon `http-errors`
|
|
35
|
+
*/
|
|
36
|
+
static HttpError = HttpError;
|
|
37
|
+
|
|
38
|
+
proxy: boolean;
|
|
39
|
+
subdomainOffset: number;
|
|
40
|
+
proxyIpHeader: string;
|
|
41
|
+
maxIpsCount: number;
|
|
42
|
+
env: string;
|
|
43
|
+
keys?: string[];
|
|
44
|
+
middleware: MiddlewareFunc[];
|
|
45
|
+
ctxStorage: AsyncLocalStorage<ContextDelegation>;
|
|
46
|
+
silent: boolean;
|
|
47
|
+
ContextClass: ProtoImplClass<Context>;
|
|
48
|
+
context: AnyProto;
|
|
49
|
+
RequestClass: ProtoImplClass<Request>;
|
|
50
|
+
request: AnyProto;
|
|
51
|
+
ResponseClass: ProtoImplClass<Response>;
|
|
52
|
+
response: AnyProto;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Initialize a new `Application`.
|
|
56
|
+
*
|
|
57
|
+
* @param {object} [options] Application options
|
|
58
|
+
* @param {string} [options.env='development'] Environment
|
|
59
|
+
* @param {string[]} [options.keys] Signed cookie keys
|
|
60
|
+
* @param {boolean} [options.proxy] Trust proxy headers
|
|
61
|
+
* @param {number} [options.subdomainOffset] Subdomain offset
|
|
62
|
+
* @param {string} [options.proxyIpHeader] Proxy IP header, defaults to X-Forwarded-For
|
|
63
|
+
* @param {number} [options.maxIpsCount] Max IPs read from proxy IP header, default to 0 (means infinity)
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
constructor(options?: {
|
|
67
|
+
proxy?: boolean;
|
|
68
|
+
subdomainOffset?: number;
|
|
69
|
+
proxyIpHeader?: string;
|
|
70
|
+
maxIpsCount?: number;
|
|
71
|
+
env?: string;
|
|
72
|
+
keys?: string[];
|
|
73
|
+
}) {
|
|
74
|
+
super();
|
|
75
|
+
options = options || {};
|
|
76
|
+
this.proxy = options.proxy || false;
|
|
77
|
+
this.subdomainOffset = options.subdomainOffset || 2;
|
|
78
|
+
this.proxyIpHeader = options.proxyIpHeader || 'X-Forwarded-For';
|
|
79
|
+
this.maxIpsCount = options.maxIpsCount || 0;
|
|
80
|
+
this.env = options.env || process.env.NODE_ENV || 'development';
|
|
81
|
+
if (options.keys) this.keys = options.keys;
|
|
82
|
+
this.middleware = [];
|
|
83
|
+
this.ctxStorage = getAsyncLocalStorage();
|
|
84
|
+
this.silent = false;
|
|
85
|
+
this.ContextClass = class ApplicationContext extends Context {};
|
|
86
|
+
this.context = this.ContextClass.prototype;
|
|
87
|
+
this.RequestClass = class ApplicationRequest extends Request {};
|
|
88
|
+
this.request = this.RequestClass.prototype;
|
|
89
|
+
this.ResponseClass = class ApplicationResponse extends Response {};
|
|
90
|
+
this.response = this.ResponseClass.prototype;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Shorthand for:
|
|
95
|
+
*
|
|
96
|
+
* http.createServer(app.callback()).listen(...)
|
|
97
|
+
*/
|
|
98
|
+
listen(...args: any[]) {
|
|
99
|
+
debug('listen');
|
|
100
|
+
const server = http.createServer(this.callback());
|
|
101
|
+
return server.listen(...args);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Return JSON representation.
|
|
106
|
+
* We only bother showing settings.
|
|
107
|
+
*/
|
|
108
|
+
toJSON() {
|
|
109
|
+
return {
|
|
110
|
+
subdomainOffset: this.subdomainOffset,
|
|
111
|
+
proxy: this.proxy,
|
|
112
|
+
env: this.env,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Inspect implementation.
|
|
118
|
+
*/
|
|
119
|
+
inspect() {
|
|
120
|
+
return this.toJSON();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
[util.inspect.custom]() {
|
|
124
|
+
return this.inspect();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Use the given middleware `fn`.
|
|
129
|
+
*
|
|
130
|
+
* Old-style middleware will be converted.
|
|
131
|
+
*/
|
|
132
|
+
use(fn: MiddlewareFunc) {
|
|
133
|
+
if (typeof fn !== 'function') throw new TypeError('middleware must be a function!');
|
|
134
|
+
if (isGeneratorFunction(fn)) {
|
|
135
|
+
throw new TypeError('Support for generators was removed. ' +
|
|
136
|
+
'See the documentation for examples of how to convert old middleware ' +
|
|
137
|
+
'https://github.com/koajs/koa/blob/master/docs/migration.md');
|
|
138
|
+
}
|
|
139
|
+
debug('use %s #%d', (fn as any)._name || fn.name || '-', this.middleware.length);
|
|
140
|
+
this.middleware.push(fn);
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Return a request handler callback
|
|
146
|
+
* for node's native http server.
|
|
147
|
+
*/
|
|
148
|
+
callback() {
|
|
149
|
+
const fn = compose(this.middleware);
|
|
150
|
+
|
|
151
|
+
if (!this.listenerCount('error')) {
|
|
152
|
+
this.on('error', this.onerror.bind(this));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const handleRequest = (req: IncomingMessage, res: ServerResponse) => {
|
|
156
|
+
const ctx = this.createContext(req, res);
|
|
157
|
+
return this.ctxStorage.run(ctx, async () => {
|
|
158
|
+
return await this.#handleRequest(ctx, fn);
|
|
159
|
+
});
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
return handleRequest;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* return current context from async local storage
|
|
167
|
+
*/
|
|
168
|
+
get currentContext() {
|
|
169
|
+
return this.ctxStorage.getStore();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Handle request in callback.
|
|
174
|
+
* @private
|
|
175
|
+
*/
|
|
176
|
+
async #handleRequest(ctx: ContextDelegation, fnMiddleware: (ctx: ContextDelegation) => Promise<void>) {
|
|
177
|
+
const res = ctx.res;
|
|
178
|
+
res.statusCode = 404;
|
|
179
|
+
const onerror = (err: any) => ctx.onerror(err);
|
|
180
|
+
onFinished(res, onerror);
|
|
181
|
+
try {
|
|
182
|
+
await fnMiddleware(ctx);
|
|
183
|
+
return this._respond(ctx);
|
|
184
|
+
} catch (err) {
|
|
185
|
+
return onerror(err);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Initialize a new context.
|
|
191
|
+
* @private
|
|
192
|
+
*/
|
|
193
|
+
protected createContext(req: IncomingMessage, res: ServerResponse) {
|
|
194
|
+
const context = new this.ContextClass(this, req, res);
|
|
195
|
+
return context as ContextDelegation;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Default error handler.
|
|
200
|
+
* @private
|
|
201
|
+
*/
|
|
202
|
+
protected onerror(err: CustomError) {
|
|
203
|
+
// When dealing with cross-globals a normal `instanceof` check doesn't work properly.
|
|
204
|
+
// See https://github.com/koajs/koa/issues/1466
|
|
205
|
+
// We can probably remove it once jest fixes https://github.com/facebook/jest/issues/2549.
|
|
206
|
+
const isNativeError = err instanceof Error ||
|
|
207
|
+
Object.prototype.toString.call(err) === '[object Error]';
|
|
208
|
+
if (!isNativeError) throw new TypeError(util.format('non-error thrown: %j', err));
|
|
209
|
+
|
|
210
|
+
if (err.status === 404 || err.expose) return;
|
|
211
|
+
if (this.silent) return;
|
|
212
|
+
|
|
213
|
+
const msg = err.stack || err.toString();
|
|
214
|
+
console.error(`\n${msg.replace(/^/gm, ' ')}\n`);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Response helper.
|
|
219
|
+
*/
|
|
220
|
+
protected _respond(ctx: ContextDelegation) {
|
|
221
|
+
// allow bypassing koa
|
|
222
|
+
if (ctx.respond === false) return;
|
|
223
|
+
|
|
224
|
+
if (!ctx.writable) return;
|
|
225
|
+
|
|
226
|
+
const res = ctx.res;
|
|
227
|
+
let body = ctx.body;
|
|
228
|
+
const code = ctx.status;
|
|
229
|
+
|
|
230
|
+
// ignore body
|
|
231
|
+
if (statuses.empty[code]) {
|
|
232
|
+
// strip headers
|
|
233
|
+
ctx.body = null;
|
|
234
|
+
return res.end();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (ctx.method === 'HEAD') {
|
|
238
|
+
if (!res.headersSent && !ctx.response.has('Content-Length')) {
|
|
239
|
+
const { length } = ctx.response;
|
|
240
|
+
if (Number.isInteger(length)) ctx.length = length;
|
|
241
|
+
}
|
|
242
|
+
return res.end();
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// status body
|
|
246
|
+
if (body == null) {
|
|
247
|
+
if (ctx.response._explicitNullBody) {
|
|
248
|
+
ctx.response.remove('Content-Type');
|
|
249
|
+
ctx.response.remove('Transfer-Encoding');
|
|
250
|
+
return res.end();
|
|
251
|
+
}
|
|
252
|
+
if (ctx.req.httpVersionMajor >= 2) {
|
|
253
|
+
body = String(code);
|
|
254
|
+
} else {
|
|
255
|
+
body = ctx.message || String(code);
|
|
256
|
+
}
|
|
257
|
+
if (!res.headersSent) {
|
|
258
|
+
ctx.type = 'text';
|
|
259
|
+
ctx.length = Buffer.byteLength(body);
|
|
260
|
+
}
|
|
261
|
+
return res.end(body);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// responses
|
|
265
|
+
if (Buffer.isBuffer(body)) return res.end(body);
|
|
266
|
+
if (typeof body === 'string') return res.end(body);
|
|
267
|
+
if (body instanceof Stream) return body.pipe(res);
|
|
268
|
+
|
|
269
|
+
// body: json
|
|
270
|
+
body = JSON.stringify(body);
|
|
271
|
+
if (!res.headersSent) {
|
|
272
|
+
ctx.length = Buffer.byteLength(body);
|
|
273
|
+
}
|
|
274
|
+
res.end(body);
|
|
275
|
+
}
|
|
276
|
+
}
|
package/src/context.ts
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import util from 'node:util';
|
|
2
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
3
|
+
import createError from 'http-errors';
|
|
4
|
+
import httpAssert from 'http-assert';
|
|
5
|
+
import delegate from 'delegates';
|
|
6
|
+
import statuses from 'statuses';
|
|
7
|
+
import Cookies from 'cookies';
|
|
8
|
+
import type Application from './application.js';
|
|
9
|
+
import type Request from './request.js';
|
|
10
|
+
import type Response from './response.js';
|
|
11
|
+
import type { CustomError, AnyProto } from './types.js';
|
|
12
|
+
|
|
13
|
+
export default class Context {
|
|
14
|
+
app: Application;
|
|
15
|
+
req: IncomingMessage;
|
|
16
|
+
res: ServerResponse;
|
|
17
|
+
request: Request & AnyProto;
|
|
18
|
+
response: Response & AnyProto;
|
|
19
|
+
state: Record<string, any>;
|
|
20
|
+
originalUrl: string;
|
|
21
|
+
respond?: boolean;
|
|
22
|
+
|
|
23
|
+
constructor(app: Application, req: IncomingMessage, res: ServerResponse) {
|
|
24
|
+
this.app = app;
|
|
25
|
+
this.req = req;
|
|
26
|
+
this.res = res;
|
|
27
|
+
this.state = {};
|
|
28
|
+
this.request = new app.RequestClass(app, this, req, res);
|
|
29
|
+
this.response = new app.ResponseClass(app, this as any, req, res);
|
|
30
|
+
this.request.response = this.response;
|
|
31
|
+
this.response.request = this.request;
|
|
32
|
+
this.originalUrl = req.url!;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* util.inspect() implementation, which
|
|
37
|
+
* just returns the JSON output.
|
|
38
|
+
*/
|
|
39
|
+
inspect() {
|
|
40
|
+
return this.toJSON();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Custom inspection implementation for newer Node.js versions.
|
|
45
|
+
*/
|
|
46
|
+
[util.inspect.custom]() {
|
|
47
|
+
return this.inspect();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Return JSON representation.
|
|
52
|
+
*
|
|
53
|
+
* Here we explicitly invoke .toJSON() on each
|
|
54
|
+
* object, as iteration will otherwise fail due
|
|
55
|
+
* to the getters and cause utilities such as
|
|
56
|
+
* clone() to fail.
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
toJSON() {
|
|
60
|
+
return {
|
|
61
|
+
request: this.request.toJSON(),
|
|
62
|
+
response: this.response.toJSON(),
|
|
63
|
+
app: this.app.toJSON(),
|
|
64
|
+
originalUrl: this.originalUrl,
|
|
65
|
+
req: '<original node req>',
|
|
66
|
+
res: '<original node res>',
|
|
67
|
+
socket: '<original node socket>',
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Similar to .throw(), adds assertion.
|
|
73
|
+
*
|
|
74
|
+
* this.assert(this.user, 401, 'Please login!');
|
|
75
|
+
*
|
|
76
|
+
* See: https://github.com/jshttp/http-assert
|
|
77
|
+
* @param {Mixed} value
|
|
78
|
+
* @param {Number} status
|
|
79
|
+
* @param {String} opts
|
|
80
|
+
*/
|
|
81
|
+
assert(value: any, status?: number, opts?: Record<string, any>): void;
|
|
82
|
+
assert(value: any, status?: number, msg?: string, opts?: Record<string, any>): void;
|
|
83
|
+
assert(value: any, status?: number, msgOrOptions?: string | Record<string, any>, opts?: Record<string, any>) {
|
|
84
|
+
if (typeof msgOrOptions === 'string') {
|
|
85
|
+
return httpAssert(value, status, msgOrOptions, opts);
|
|
86
|
+
}
|
|
87
|
+
return httpAssert(value, status, msgOrOptions);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Throw an error with `status` (default 500) and
|
|
92
|
+
* `msg`. Note that these are user-level
|
|
93
|
+
* errors, and the message may be exposed to the client.
|
|
94
|
+
*
|
|
95
|
+
* this.throw(403)
|
|
96
|
+
* this.throw(400, 'name required')
|
|
97
|
+
* this.throw('something exploded')
|
|
98
|
+
* this.throw(new Error('invalid'))
|
|
99
|
+
* this.throw(400, new Error('invalid'))
|
|
100
|
+
* this.throw(400, new Error('invalid'), { foo: 'bar' })
|
|
101
|
+
* this.throw(new Error('invalid'), { foo: 'bar' })
|
|
102
|
+
*
|
|
103
|
+
* See: https://github.com/jshttp/http-errors
|
|
104
|
+
*
|
|
105
|
+
* Note: `status` should only be passed as the first parameter.
|
|
106
|
+
*
|
|
107
|
+
* @param {String|Number|Error} status error, msg or status
|
|
108
|
+
* @param {String|Number|Error|Object} [error] error, msg, status or errorProps
|
|
109
|
+
* @param {Object} [errorProps] error object properties
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
throw(status: number): void;
|
|
113
|
+
throw(status: number, errorProps: object): void;
|
|
114
|
+
throw(status: number, errorMessage: string): void;
|
|
115
|
+
throw(status: number, errorMessage: string, errorProps: object): void;
|
|
116
|
+
throw(status: number, error: Error): void;
|
|
117
|
+
throw(status: number, error: Error, errorProps: object): void;
|
|
118
|
+
throw(errorMessage: string): void;
|
|
119
|
+
throw(errorMessage: string, errorProps: object): void;
|
|
120
|
+
throw(errorMessage: string, status: number): void;
|
|
121
|
+
throw(errorMessage: string, status: number, errorProps: object): void;
|
|
122
|
+
throw(error: Error): void;
|
|
123
|
+
throw(error: Error, errorProps: object): void;
|
|
124
|
+
throw(error: Error, status: number): void;
|
|
125
|
+
throw(error: Error, status: number, errorProps: object): void;
|
|
126
|
+
throw(arg1: number | string | Error, arg2?: number | string | Error | object, errorProps?: object) {
|
|
127
|
+
const args: any[] = [];
|
|
128
|
+
if (typeof arg2 === 'number') {
|
|
129
|
+
// throw(error, status)
|
|
130
|
+
args.push(arg2);
|
|
131
|
+
args.push(arg1);
|
|
132
|
+
} else {
|
|
133
|
+
// throw(status, error?)
|
|
134
|
+
args.push(arg1);
|
|
135
|
+
if (arg2) {
|
|
136
|
+
args.push(arg2);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (errorProps) {
|
|
140
|
+
args.push(errorProps);
|
|
141
|
+
}
|
|
142
|
+
throw createError(...args);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Default error handling.
|
|
147
|
+
* @private
|
|
148
|
+
*/
|
|
149
|
+
onerror(err: CustomError) {
|
|
150
|
+
// don't do anything if there is no error.
|
|
151
|
+
// this allows you to pass `this.onerror`
|
|
152
|
+
// to node-style callbacks.
|
|
153
|
+
if (err === null || err === undefined) return;
|
|
154
|
+
|
|
155
|
+
// When dealing with cross-globals a normal `instanceof` check doesn't work properly.
|
|
156
|
+
// See https://github.com/koajs/koa/issues/1466
|
|
157
|
+
// We can probably remove it once jest fixes https://github.com/facebook/jest/issues/2549.
|
|
158
|
+
const isNativeError = err instanceof Error ||
|
|
159
|
+
Object.prototype.toString.call(err) === '[object Error]';
|
|
160
|
+
if (!isNativeError) err = new Error(util.format('non-error thrown: %j', err));
|
|
161
|
+
|
|
162
|
+
let headerSent = false;
|
|
163
|
+
if (this.response.headerSent || !this.response.writable) {
|
|
164
|
+
headerSent = (err as any).headerSent = true;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// delegate
|
|
168
|
+
this.app.emit('error', err, this);
|
|
169
|
+
|
|
170
|
+
// nothing we can do here other
|
|
171
|
+
// than delegate to the app-level
|
|
172
|
+
// handler and log.
|
|
173
|
+
if (headerSent) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const { res } = this;
|
|
178
|
+
|
|
179
|
+
// first unset all headers
|
|
180
|
+
res.getHeaderNames().forEach(name => res.removeHeader(name));
|
|
181
|
+
|
|
182
|
+
// then set those specified
|
|
183
|
+
if (err.headers) this.response.set(err.headers);
|
|
184
|
+
|
|
185
|
+
// force text/plain
|
|
186
|
+
this.response.type = 'text';
|
|
187
|
+
|
|
188
|
+
let statusCode = err.status || err.statusCode;
|
|
189
|
+
|
|
190
|
+
// ENOENT support
|
|
191
|
+
if (err.code === 'ENOENT') statusCode = 404;
|
|
192
|
+
|
|
193
|
+
// default to 500
|
|
194
|
+
if (typeof statusCode !== 'number' || !statuses.message[statusCode]) statusCode = 500;
|
|
195
|
+
|
|
196
|
+
// respond
|
|
197
|
+
const statusMessage = statuses.message[statusCode] as string;
|
|
198
|
+
const msg = err.expose ? err.message : statusMessage;
|
|
199
|
+
this.response.status = err.status = statusCode;
|
|
200
|
+
this.response.length = Buffer.byteLength(msg);
|
|
201
|
+
res.end(msg);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
#cookies: Cookies;
|
|
205
|
+
get cookies() {
|
|
206
|
+
if (!this.#cookies) {
|
|
207
|
+
this.#cookies = new Cookies(this.req, this.res, {
|
|
208
|
+
keys: this.app.keys,
|
|
209
|
+
secure: this.request.secure,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
return this.#cookies;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
set cookies(cookies: Cookies) {
|
|
216
|
+
this.#cookies = cookies;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Request delegation.
|
|
222
|
+
*/
|
|
223
|
+
|
|
224
|
+
delegate(Context.prototype, 'request')
|
|
225
|
+
.method('acceptsLanguages')
|
|
226
|
+
.method('acceptsEncodings')
|
|
227
|
+
.method('acceptsCharsets')
|
|
228
|
+
.method('accepts')
|
|
229
|
+
.method('get')
|
|
230
|
+
.method('is')
|
|
231
|
+
.access('querystring')
|
|
232
|
+
.access('idempotent')
|
|
233
|
+
.access('socket')
|
|
234
|
+
.access('search')
|
|
235
|
+
.access('method')
|
|
236
|
+
.access('query')
|
|
237
|
+
.access('path')
|
|
238
|
+
.access('url')
|
|
239
|
+
.access('accept')
|
|
240
|
+
.getter('origin')
|
|
241
|
+
.getter('href')
|
|
242
|
+
.getter('subdomains')
|
|
243
|
+
.getter('protocol')
|
|
244
|
+
.getter('host')
|
|
245
|
+
.getter('hostname')
|
|
246
|
+
.getter('URL')
|
|
247
|
+
.getter('header')
|
|
248
|
+
.getter('headers')
|
|
249
|
+
.getter('secure')
|
|
250
|
+
.getter('stale')
|
|
251
|
+
.getter('fresh')
|
|
252
|
+
.getter('ips')
|
|
253
|
+
.getter('ip');
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Response delegation.
|
|
257
|
+
*/
|
|
258
|
+
|
|
259
|
+
delegate(Context.prototype, 'response')
|
|
260
|
+
.method('attachment')
|
|
261
|
+
.method('redirect')
|
|
262
|
+
.method('remove')
|
|
263
|
+
.method('vary')
|
|
264
|
+
.method('has')
|
|
265
|
+
.method('set')
|
|
266
|
+
.method('append')
|
|
267
|
+
.method('flushHeaders')
|
|
268
|
+
.access('status')
|
|
269
|
+
.access('message')
|
|
270
|
+
.access('body')
|
|
271
|
+
.access('length')
|
|
272
|
+
.access('type')
|
|
273
|
+
.access('lastModified')
|
|
274
|
+
.access('etag')
|
|
275
|
+
.getter('headerSent')
|
|
276
|
+
.getter('writable');
|
|
277
|
+
|
|
278
|
+
export type ContextDelegation = Context & Pick<Request, 'acceptsLanguages' | 'acceptsEncodings' | 'acceptsCharsets'
|
|
279
|
+
| 'accepts' | 'get' | 'is' | 'querystring' | 'idempotent' | 'socket' | 'search' | 'method' | 'query'
|
|
280
|
+
| 'path' | 'url' | 'accept' | 'origin' | 'href' | 'subdomains' | 'protocol' | 'host' | 'hostname'
|
|
281
|
+
| 'URL' | 'header' | 'headers' | 'secure' | 'stale' | 'fresh' | 'ips' | 'ip'>
|
|
282
|
+
& Pick<Response, 'attachment' | 'redirect' | 'remove' | 'vary' | 'has' | 'set' | 'append' | 'flushHeaders'
|
|
283
|
+
| 'status' | 'message' | 'body' | 'length' | 'type' | 'lastModified' | 'etag' | 'headerSent' | 'writable'>
|
|
284
|
+
& AnyProto;
|