@eggjs/koa 2.14.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.
@@ -0,0 +1,588 @@
1
+
2
+ 'use strict';
3
+
4
+ /**
5
+ * Module dependencies.
6
+ */
7
+
8
+ const contentDisposition = require('content-disposition');
9
+ const getType = require('cache-content-type');
10
+ const onFinish = require('on-finished');
11
+ const escape = require('escape-html');
12
+ const typeis = require('type-is').is;
13
+ const statuses = require('statuses');
14
+ const destroy = require('destroy');
15
+ const assert = require('assert');
16
+ const extname = require('path').extname;
17
+ const vary = require('vary');
18
+ const only = require('only');
19
+ const util = require('util');
20
+ const encodeUrl = require('encodeurl');
21
+ const Stream = require('stream');
22
+
23
+ /**
24
+ * Prototype.
25
+ */
26
+
27
+ module.exports = {
28
+
29
+ /**
30
+ * Return the request socket.
31
+ *
32
+ * @return {Connection}
33
+ * @api public
34
+ */
35
+
36
+ get socket() {
37
+ return this.res.socket;
38
+ },
39
+
40
+ /**
41
+ * Return response header.
42
+ *
43
+ * @return {Object}
44
+ * @api public
45
+ */
46
+
47
+ get header() {
48
+ const { res } = this;
49
+ return typeof res.getHeaders === 'function'
50
+ ? res.getHeaders()
51
+ : res._headers || {}; // Node < 7.7
52
+ },
53
+
54
+ /**
55
+ * Return response header, alias as response.header
56
+ *
57
+ * @return {Object}
58
+ * @api public
59
+ */
60
+
61
+ get headers() {
62
+ return this.header;
63
+ },
64
+
65
+ /**
66
+ * Get response status code.
67
+ *
68
+ * @return {Number}
69
+ * @api public
70
+ */
71
+
72
+ get status() {
73
+ return this.res.statusCode;
74
+ },
75
+
76
+ /**
77
+ * Set response status code.
78
+ *
79
+ * @param {Number} code
80
+ * @api public
81
+ */
82
+
83
+ set status(code) {
84
+ if (this.headerSent) return;
85
+
86
+ assert(Number.isInteger(code), 'status code must be a number');
87
+ assert(code >= 100 && code <= 999, `invalid status code: ${code}`);
88
+ this._explicitStatus = true;
89
+ this.res.statusCode = code;
90
+ if (this.req.httpVersionMajor < 2) this.res.statusMessage = statuses[code];
91
+ if (this.body && statuses.empty[code]) this.body = null;
92
+ },
93
+
94
+ /**
95
+ * Get response status message
96
+ *
97
+ * @return {String}
98
+ * @api public
99
+ */
100
+
101
+ get message() {
102
+ return this.res.statusMessage || statuses[this.status];
103
+ },
104
+
105
+ /**
106
+ * Set response status message
107
+ *
108
+ * @param {String} msg
109
+ * @api public
110
+ */
111
+
112
+ set message(msg) {
113
+ this.res.statusMessage = msg;
114
+ },
115
+
116
+ /**
117
+ * Get response body.
118
+ *
119
+ * @return {Mixed}
120
+ * @api public
121
+ */
122
+
123
+ get body() {
124
+ return this._body;
125
+ },
126
+
127
+ /**
128
+ * Set response body.
129
+ *
130
+ * @param {String|Buffer|Object|Stream} val
131
+ * @api public
132
+ */
133
+
134
+ set body(val) {
135
+ const original = this._body;
136
+ this._body = val;
137
+
138
+ // no content
139
+ if (null == val) {
140
+ if (!statuses.empty[this.status]) this.status = 204;
141
+ if (val === null) this._explicitNullBody = true;
142
+ this.remove('Content-Type');
143
+ this.remove('Content-Length');
144
+ this.remove('Transfer-Encoding');
145
+ return;
146
+ }
147
+
148
+ // set the status
149
+ if (!this._explicitStatus) this.status = 200;
150
+
151
+ // set the content-type only if not yet set
152
+ const setType = !this.has('Content-Type');
153
+
154
+ // string
155
+ if ('string' === typeof val) {
156
+ if (setType) this.type = /^\s*</.test(val) ? 'html' : 'text';
157
+ this.length = Buffer.byteLength(val);
158
+ return;
159
+ }
160
+
161
+ // buffer
162
+ if (Buffer.isBuffer(val)) {
163
+ if (setType) this.type = 'bin';
164
+ this.length = val.length;
165
+ return;
166
+ }
167
+
168
+ // stream
169
+ if (val instanceof Stream) {
170
+ onFinish(this.res, destroy.bind(null, val));
171
+ if (original != val) {
172
+ val.once('error', err => this.ctx.onerror(err));
173
+ // overwriting
174
+ if (null != original) this.remove('Content-Length');
175
+ }
176
+
177
+ if (setType) this.type = 'bin';
178
+ return;
179
+ }
180
+
181
+ // json
182
+ this.remove('Content-Length');
183
+ this.type = 'json';
184
+ },
185
+
186
+ /**
187
+ * Set Content-Length field to `n`.
188
+ *
189
+ * @param {Number} n
190
+ * @api public
191
+ */
192
+
193
+ set length(n) {
194
+ if (!this.has('Transfer-Encoding')) {
195
+ this.set('Content-Length', n);
196
+ }
197
+ },
198
+
199
+ /**
200
+ * Return parsed response Content-Length when present.
201
+ *
202
+ * @return {Number}
203
+ * @api public
204
+ */
205
+
206
+ get length() {
207
+ if (this.has('Content-Length')) {
208
+ return parseInt(this.get('Content-Length'), 10) || 0;
209
+ }
210
+
211
+ const { body } = this;
212
+ if (!body || body instanceof Stream) return undefined;
213
+ if ('string' === typeof body) return Buffer.byteLength(body);
214
+ if (Buffer.isBuffer(body)) return body.length;
215
+ return Buffer.byteLength(JSON.stringify(body));
216
+ },
217
+
218
+ /**
219
+ * Check if a header has been written to the socket.
220
+ *
221
+ * @return {Boolean}
222
+ * @api public
223
+ */
224
+
225
+ get headerSent() {
226
+ return this.res.headersSent;
227
+ },
228
+
229
+ /**
230
+ * Vary on `field`.
231
+ *
232
+ * @param {String} field
233
+ * @api public
234
+ */
235
+
236
+ vary(field) {
237
+ if (this.headerSent) return;
238
+
239
+ vary(this.res, field);
240
+ },
241
+
242
+ /**
243
+ * Perform a 302 redirect to `url`.
244
+ *
245
+ * The string "back" is special-cased
246
+ * to provide Referrer support, when Referrer
247
+ * is not present `alt` or "/" is used.
248
+ *
249
+ * Examples:
250
+ *
251
+ * this.redirect('back');
252
+ * this.redirect('back', '/index.html');
253
+ * this.redirect('/login');
254
+ * this.redirect('http://google.com');
255
+ *
256
+ * @param {String} url
257
+ * @param {String} [alt]
258
+ * @api public
259
+ */
260
+
261
+ redirect(url, alt) {
262
+ // location
263
+ if ('back' === url) url = this.ctx.get('Referrer') || alt || '/';
264
+ this.set('Location', encodeUrl(url));
265
+
266
+ // status
267
+ if (!statuses.redirect[this.status]) this.status = 302;
268
+
269
+ // html
270
+ if (this.ctx.accepts('html')) {
271
+ url = escape(url);
272
+ this.type = 'text/html; charset=utf-8';
273
+ this.body = `Redirecting to <a href="${url}">${url}</a>.`;
274
+ return;
275
+ }
276
+
277
+ // text
278
+ this.type = 'text/plain; charset=utf-8';
279
+ this.body = `Redirecting to ${url}.`;
280
+ },
281
+
282
+ /**
283
+ * Set Content-Disposition header to "attachment" with optional `filename`.
284
+ *
285
+ * @param {String} filename
286
+ * @api public
287
+ */
288
+
289
+ attachment(filename, options) {
290
+ if (filename) this.type = extname(filename);
291
+ this.set('Content-Disposition', contentDisposition(filename, options));
292
+ },
293
+
294
+ /**
295
+ * Set Content-Type response header with `type` through `mime.lookup()`
296
+ * when it does not contain a charset.
297
+ *
298
+ * Examples:
299
+ *
300
+ * this.type = '.html';
301
+ * this.type = 'html';
302
+ * this.type = 'json';
303
+ * this.type = 'application/json';
304
+ * this.type = 'png';
305
+ *
306
+ * @param {String} type
307
+ * @api public
308
+ */
309
+
310
+ set type(type) {
311
+ type = getType(type);
312
+ if (type) {
313
+ this.set('Content-Type', type);
314
+ } else {
315
+ this.remove('Content-Type');
316
+ }
317
+ },
318
+
319
+ /**
320
+ * Set the Last-Modified date using a string or a Date.
321
+ *
322
+ * this.response.lastModified = new Date();
323
+ * this.response.lastModified = '2013-09-13';
324
+ *
325
+ * @param {String|Date} type
326
+ * @api public
327
+ */
328
+
329
+ set lastModified(val) {
330
+ if ('string' === typeof val) val = new Date(val);
331
+ this.set('Last-Modified', val.toUTCString());
332
+ },
333
+
334
+ /**
335
+ * Get the Last-Modified date in Date form, if it exists.
336
+ *
337
+ * @return {Date}
338
+ * @api public
339
+ */
340
+
341
+ get lastModified() {
342
+ const date = this.get('last-modified');
343
+ if (date) return new Date(date);
344
+ },
345
+
346
+ /**
347
+ * Set the ETag of a response.
348
+ * This will normalize the quotes if necessary.
349
+ *
350
+ * this.response.etag = 'md5hashsum';
351
+ * this.response.etag = '"md5hashsum"';
352
+ * this.response.etag = 'W/"123456789"';
353
+ *
354
+ * @param {String} etag
355
+ * @api public
356
+ */
357
+
358
+ set etag(val) {
359
+ if (!/^(W\/)?"/.test(val)) val = `"${val}"`;
360
+ this.set('ETag', val);
361
+ },
362
+
363
+ /**
364
+ * Get the ETag of a response.
365
+ *
366
+ * @return {String}
367
+ * @api public
368
+ */
369
+
370
+ get etag() {
371
+ return this.get('ETag');
372
+ },
373
+
374
+ /**
375
+ * Return the response mime type void of
376
+ * parameters such as "charset".
377
+ *
378
+ * @return {String}
379
+ * @api public
380
+ */
381
+
382
+ get type() {
383
+ const type = this.get('Content-Type');
384
+ if (!type) return '';
385
+ return type.split(';', 1)[0];
386
+ },
387
+
388
+ /**
389
+ * Check whether the response is one of the listed types.
390
+ * Pretty much the same as `this.request.is()`.
391
+ *
392
+ * @param {String|String[]} [type]
393
+ * @param {String[]} [types]
394
+ * @return {String|false}
395
+ * @api public
396
+ */
397
+
398
+ is(type, ...types) {
399
+ return typeis(this.type, type, ...types);
400
+ },
401
+
402
+ /**
403
+ * Return response header.
404
+ *
405
+ * Examples:
406
+ *
407
+ * this.get('Content-Type');
408
+ * // => "text/plain"
409
+ *
410
+ * this.get('content-type');
411
+ * // => "text/plain"
412
+ *
413
+ * @param {String} field
414
+ * @return {String}
415
+ * @api public
416
+ */
417
+
418
+ get(field) {
419
+ return this.header[field.toLowerCase()] || '';
420
+ },
421
+
422
+ /**
423
+ * Returns true if the header identified by name is currently set in the outgoing headers.
424
+ * The header name matching is case-insensitive.
425
+ *
426
+ * Examples:
427
+ *
428
+ * this.has('Content-Type');
429
+ * // => true
430
+ *
431
+ * this.get('content-type');
432
+ * // => true
433
+ *
434
+ * @param {String} field
435
+ * @return {boolean}
436
+ * @api public
437
+ */
438
+
439
+ has(field) {
440
+ return typeof this.res.hasHeader === 'function'
441
+ ? this.res.hasHeader(field)
442
+ // Node < 7.7
443
+ : field.toLowerCase() in this.headers;
444
+ },
445
+
446
+ /**
447
+ * Set header `field` to `val` or pass
448
+ * an object of header fields.
449
+ *
450
+ * Examples:
451
+ *
452
+ * this.set('Foo', ['bar', 'baz']);
453
+ * this.set('Accept', 'application/json');
454
+ * this.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
455
+ *
456
+ * @param {String|Object|Array} field
457
+ * @param {String} val
458
+ * @api public
459
+ */
460
+
461
+ set(field, val) {
462
+ if (this.headerSent) return;
463
+
464
+ if (2 === arguments.length) {
465
+ if (Array.isArray(val)) val = val.map(v => typeof v === 'string' ? v : String(v));
466
+ else if (typeof val !== 'string') val = String(val);
467
+ this.res.setHeader(field, val);
468
+ } else {
469
+ for (const key in field) {
470
+ this.set(key, field[key]);
471
+ }
472
+ }
473
+ },
474
+
475
+ /**
476
+ * Append additional header `field` with value `val`.
477
+ *
478
+ * Examples:
479
+ *
480
+ * ```
481
+ * this.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);
482
+ * this.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');
483
+ * this.append('Warning', '199 Miscellaneous warning');
484
+ * ```
485
+ *
486
+ * @param {String} field
487
+ * @param {String|Array} val
488
+ * @api public
489
+ */
490
+
491
+ append(field, val) {
492
+ const prev = this.get(field);
493
+
494
+ if (prev) {
495
+ val = Array.isArray(prev)
496
+ ? prev.concat(val)
497
+ : [prev].concat(val);
498
+ }
499
+
500
+ return this.set(field, val);
501
+ },
502
+
503
+ /**
504
+ * Remove header `field`.
505
+ *
506
+ * @param {String} name
507
+ * @api public
508
+ */
509
+
510
+ remove(field) {
511
+ if (this.headerSent) return;
512
+
513
+ this.res.removeHeader(field);
514
+ },
515
+
516
+ /**
517
+ * Checks if the request is writable.
518
+ * Tests for the existence of the socket
519
+ * as node sometimes does not set it.
520
+ *
521
+ * @return {Boolean}
522
+ * @api private
523
+ */
524
+
525
+ get writable() {
526
+ // can't write any more after response finished
527
+ // response.writableEnded is available since Node > 12.9
528
+ // https://nodejs.org/api/http.html#http_response_writableended
529
+ // response.finished is undocumented feature of previous Node versions
530
+ // https://stackoverflow.com/questions/16254385/undocumented-response-finished-in-node-js
531
+ if (this.res.writableEnded || this.res.finished) return false;
532
+
533
+ const socket = this.res.socket;
534
+ // There are already pending outgoing res, but still writable
535
+ // https://github.com/nodejs/node/blob/v4.4.7/lib/_http_server.js#L486
536
+ if (!socket) return true;
537
+ return socket.writable;
538
+ },
539
+
540
+ /**
541
+ * Inspect implementation.
542
+ *
543
+ * @return {Object}
544
+ * @api public
545
+ */
546
+
547
+ inspect() {
548
+ if (!this.res) return;
549
+ const o = this.toJSON();
550
+ o.body = this.body;
551
+ return o;
552
+ },
553
+
554
+ /**
555
+ * Return JSON representation.
556
+ *
557
+ * @return {Object}
558
+ * @api public
559
+ */
560
+
561
+ toJSON() {
562
+ return only(this, [
563
+ 'status',
564
+ 'message',
565
+ 'header'
566
+ ]);
567
+ },
568
+
569
+ /**
570
+ * Flush any set headers and begin the body
571
+ */
572
+
573
+ flushHeaders() {
574
+ this.res.flushHeaders();
575
+ }
576
+ };
577
+
578
+ /**
579
+ * Custom inspection implementation for node 6+.
580
+ *
581
+ * @return {Object}
582
+ * @api public
583
+ */
584
+
585
+ /* istanbul ignore else */
586
+ if (util.inspect.custom) {
587
+ module.exports[util.inspect.custom] = module.exports.inspect;
588
+ }
package/package.json ADDED
@@ -0,0 +1,93 @@
1
+ {
2
+ "name": "@eggjs/koa",
3
+ "version": "2.14.2",
4
+ "description": "Koa web app framework",
5
+ "main": "lib/application.js",
6
+ "files": [
7
+ "dist",
8
+ "lib"
9
+ ],
10
+ "exports": {
11
+ ".": {
12
+ "require": "./lib/application.js",
13
+ "import": "./dist/koa.mjs"
14
+ },
15
+ "./lib/request": "./lib/request.js",
16
+ "./lib/request.js": "./lib/request.js",
17
+ "./lib/response": "./lib/response.js",
18
+ "./lib/response.js": "./lib/response.js",
19
+ "./lib/application": "./lib/application.js",
20
+ "./lib/application.js": "./lib/application.js",
21
+ "./lib/context": "./lib/context.js",
22
+ "./lib/context.js": "./lib/context.js",
23
+ "./*": "./*.js",
24
+ "./*.js": "./*.js",
25
+ "./package": "./package.json",
26
+ "./package.json": "./package.json"
27
+ },
28
+ "scripts": {
29
+ "test": "jest --forceExit",
30
+ "ci": "npm test -- --coverage --maxWorkers 2",
31
+ "lint": "eslint --ignore-path .gitignore .",
32
+ "authors": "git log --format='%aN <%aE>' | sort -u > AUTHORS",
33
+ "build": "gen-esm-wrapper . ./dist/koa.mjs",
34
+ "prepare": "npm run build"
35
+ },
36
+ "repository": "eggjs/koa",
37
+ "keywords": [
38
+ "web",
39
+ "app",
40
+ "http",
41
+ "application",
42
+ "framework",
43
+ "middleware",
44
+ "rack"
45
+ ],
46
+ "license": "MIT",
47
+ "dependencies": {
48
+ "accepts": "^1.3.5",
49
+ "cache-content-type": "^1.0.0",
50
+ "content-disposition": "~0.5.2",
51
+ "content-type": "^1.0.4",
52
+ "cookies": "~0.8.0",
53
+ "debug": "^4.3.2",
54
+ "delegates": "^1.0.0",
55
+ "depd": "^2.0.0",
56
+ "destroy": "^1.0.4",
57
+ "encodeurl": "^1.0.2",
58
+ "escape-html": "^1.0.3",
59
+ "fresh": "~0.5.2",
60
+ "http-assert": "^1.3.0",
61
+ "http-errors": "^1.6.3",
62
+ "is-generator-function": "^1.0.7",
63
+ "koa-compose": "^4.1.0",
64
+ "koa-convert": "^2.0.0",
65
+ "on-finished": "^2.3.0",
66
+ "only": "~0.0.2",
67
+ "parseurl": "^1.3.2",
68
+ "statuses": "^1.5.0",
69
+ "type-is": "^1.6.16",
70
+ "vary": "^1.1.2"
71
+ },
72
+ "devDependencies": {
73
+ "eslint": "^7.32.0",
74
+ "eslint-config-koa": "^2.0.0",
75
+ "eslint-config-standard": "^16.0.3",
76
+ "eslint-plugin-import": "^2.18.2",
77
+ "eslint-plugin-node": "^11.1.0",
78
+ "eslint-plugin-promise": "^5.1.0",
79
+ "eslint-plugin-standard": "^5.0.0",
80
+ "gen-esm-wrapper": "^1.0.6",
81
+ "jest": "^27.0.6",
82
+ "supertest": "^3.1.0"
83
+ },
84
+ "engines": {
85
+ "node": ">= 16.13.0"
86
+ },
87
+ "jest": {
88
+ "testEnvironment": "node"
89
+ },
90
+ "publishConfig": {
91
+ "access": "public"
92
+ }
93
+ }