koa 2.11.0 → 2.12.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/History.md CHANGED
@@ -1,4 +1,21 @@
1
1
 
2
+ 2.12.0 / 2020-05-18
3
+ ==================
4
+
5
+ **features**
6
+ * [[`0d2f421`](http://github.com/koajs/koa/commit/0d2f421c265350d3d84e1bc261572954479f27d3)] - feat: error handler treat err.statusCode as the same as err.status (#1460) (Vijay Krishnavanshi <<vijaykrishnavanshi@gmail.com>>)
7
+ * [[`8d52105`](http://github.com/koajs/koa/commit/8d52105a34234be9e771ff3b76b43e4e30328943)] - feat: allow bodyless responses for non empty status codes (#1447) (ejose19 <<8742215+ejose19@users.noreply.github.com>>)
8
+
9
+ **others**
10
+ * [[`faeaff5`](http://github.com/koajs/koa/commit/faeaff5c149a81a188ab8e5af0b994029e45acbb)] - fox: remove `error-inject` and fix error handling (#1409) (Konstantin Vyatkin <<tino@vtkn.io>>)
11
+ * [[`f7c732f`](http://github.com/koajs/koa/commit/f7c732fd06f724505e9090add4d977e667da55a8)] - docs: fixed incorrect onerror example (#1459) (Paul Annekov <<paul.annekov@gmail.com>>)
12
+ * [[`143d8f7`](http://github.com/koajs/koa/commit/143d8f72f2a232b4c97eac00e7811015911e4f7c)] - Always use strict equality. (#1225) (Yazan Medanat <<medanat@gmail.com>>)
13
+ * [[`6b6b0dd`](http://github.com/koajs/koa/commit/6b6b0ddf7aff073e65493c6efaffab8331c0331c)] - docs(api): add app.use chainability note (#1449) (Zac Anger <<zac@zacanger.com>>)
14
+ * [[`8ddab48`](http://github.com/koajs/koa/commit/8ddab48cbdbca1e6d1cc8c3ddae45491db524d51)] - docs: Document response status with empty body (#1445) (Marc-Aurèle DARCHE <<152407+madarche@users.noreply.github.com>>)
15
+ * [[`7deedb2`](http://github.com/koajs/koa/commit/7deedb235274223f1b9da46dee296545b23598de)] - docs: Updating context.md with the latest cookies opts (#1433) (Brad Ito <<phlogisticfugu@users.noreply.github.com>>)
16
+ * [[`3e97a10`](http://github.com/koajs/koa/commit/3e97a106bb846d9337737011bb85149ddd797229)] - docs(links): remove Google+ link (#1439) (laffachan <<45162759+laffachan@users.noreply.github.com>>)
17
+ * [[`eda2760`](http://github.com/koajs/koa/commit/eda27608f7d39ede86d7b402aae64b1867ce31c6)] - build: Drop unused Travis sudo: false directive (#1416) (Olle Jonsson <<olle.jonsson@gmail.com>>)
18
+
2
19
  2.11.0 / 2019-10-28
3
20
  ==================
4
21
 
package/Readme.md CHANGED
@@ -221,7 +221,6 @@ See [AUTHORS](AUTHORS).
221
221
  - [Examples](https://github.com/koajs/examples)
222
222
  - [Middleware](https://github.com/koajs/koa/wiki) list
223
223
  - [Wiki](https://github.com/koajs/koa/wiki)
224
- - [G+ Community](https://plus.google.com/communities/101845768320796750641)
225
224
  - [Reddit Community](https://www.reddit.com/r/koajs)
226
225
  - [Mailing list](https://groups.google.com/forum/#!forum/koajs)
227
226
  - [中文文档 v1.x](https://github.com/guo-yu/koa-guide)
@@ -197,7 +197,7 @@ module.exports = class Application extends Emitter {
197
197
  onerror(err) {
198
198
  if (!(err instanceof Error)) throw new TypeError(util.format('non-error thrown: %j', err));
199
199
 
200
- if (404 == err.status || err.expose) return;
200
+ if (404 === err.status || err.expose) return;
201
201
  if (this.silent) return;
202
202
 
203
203
  const msg = err.stack || err.toString();
@@ -238,6 +238,11 @@ function respond(ctx) {
238
238
 
239
239
  // status body
240
240
  if (null == body) {
241
+ if (ctx.response._explicitNullBody) {
242
+ ctx.response.remove('Content-Type');
243
+ ctx.response.remove('Transfer-Encoding');
244
+ return res.end();
245
+ }
241
246
  if (ctx.req.httpVersionMajor >= 2) {
242
247
  body = String(code);
243
248
  } else {
@@ -252,7 +257,7 @@ function respond(ctx) {
252
257
 
253
258
  // responses
254
259
  if (Buffer.isBuffer(body)) return res.end(body);
255
- if ('string' == typeof body) return res.end(body);
260
+ if ('string' === typeof body) return res.end(body);
256
261
  if (body instanceof Stream) return body.pipe(res);
257
262
 
258
263
  // body: json
package/lib/context.js CHANGED
@@ -143,16 +143,18 @@ const proto = module.exports = {
143
143
  // force text/plain
144
144
  this.type = 'text';
145
145
 
146
+ let statusCode = err.status || err.statusCode;
147
+
146
148
  // ENOENT support
147
- if ('ENOENT' == err.code) err.status = 404;
149
+ if ('ENOENT' === err.code) statusCode = 404;
148
150
 
149
151
  // default to 500
150
- if ('number' != typeof err.status || !statuses[err.status]) err.status = 500;
152
+ if ('number' !== typeof statusCode || !statuses[statusCode]) statusCode = 500;
151
153
 
152
154
  // respond
153
- const code = statuses[err.status];
155
+ const code = statuses[statusCode];
154
156
  const msg = err.expose ? err.message : code;
155
- this.status = err.status;
157
+ this.status = err.status = statusCode;
156
158
  this.length = Buffer.byteLength(msg);
157
159
  res.end(msg);
158
160
  },
package/lib/request.js CHANGED
@@ -272,7 +272,7 @@ module.exports = {
272
272
  get hostname() {
273
273
  const host = this.host;
274
274
  if (!host) return '';
275
- if ('[' == host[0]) return this.URL.hostname || ''; // IPv6
275
+ if ('[' === host[0]) return this.URL.hostname || ''; // IPv6
276
276
  return host.split(':', 1)[0];
277
277
  },
278
278
 
@@ -311,10 +311,10 @@ module.exports = {
311
311
  const s = this.ctx.status;
312
312
 
313
313
  // GET or HEAD for weak freshness validation only
314
- if ('GET' != method && 'HEAD' != method) return false;
314
+ if ('GET' !== method && 'HEAD' !== method) return false;
315
315
 
316
316
  // 2xx or 304 as per rfc2616 14.26
317
- if ((s >= 200 && s < 300) || 304 == s) {
317
+ if ((s >= 200 && s < 300) || 304 === s) {
318
318
  return fresh(this.header, this.response.header);
319
319
  }
320
320
 
@@ -382,7 +382,7 @@ module.exports = {
382
382
 
383
383
  get length() {
384
384
  const len = this.get('Content-Length');
385
- if (len == '') return;
385
+ if (len === '') return;
386
386
  return ~~len;
387
387
  },
388
388
 
@@ -415,7 +415,7 @@ module.exports = {
415
415
  */
416
416
 
417
417
  get secure() {
418
- return 'https' == this.protocol;
418
+ return 'https' === this.protocol;
419
419
  },
420
420
 
421
421
  /**
package/lib/response.js CHANGED
@@ -6,7 +6,6 @@
6
6
  */
7
7
 
8
8
  const contentDisposition = require('content-disposition');
9
- const ensureErrorHandler = require('error-inject');
10
9
  const getType = require('cache-content-type');
11
10
  const onFinish = require('on-finished');
12
11
  const escape = require('escape-html');
@@ -139,6 +138,7 @@ module.exports = {
139
138
  // no content
140
139
  if (null == val) {
141
140
  if (!statuses.empty[this.status]) this.status = 204;
141
+ if (val === null) this._explicitNullBody = true;
142
142
  this.remove('Content-Type');
143
143
  this.remove('Content-Length');
144
144
  this.remove('Transfer-Encoding');
@@ -152,7 +152,7 @@ module.exports = {
152
152
  const setType = !this.has('Content-Type');
153
153
 
154
154
  // string
155
- if ('string' == typeof val) {
155
+ if ('string' === typeof val) {
156
156
  if (setType) this.type = /^\s*</.test(val) ? 'html' : 'text';
157
157
  this.length = Buffer.byteLength(val);
158
158
  return;
@@ -166,12 +166,13 @@ module.exports = {
166
166
  }
167
167
 
168
168
  // stream
169
- if ('function' == typeof val.pipe) {
169
+ if (val instanceof Stream) {
170
170
  onFinish(this.res, destroy.bind(null, val));
171
- ensureErrorHandler(val, err => this.ctx.onerror(err));
172
-
173
- // overwriting
174
- if (null != original && original != val) this.remove('Content-Length');
171
+ if (original != val) {
172
+ val.once('error', err => this.ctx.onerror(err));
173
+ // overwriting
174
+ if (null != original) this.remove('Content-Length');
175
+ }
175
176
 
176
177
  if (setType) this.type = 'bin';
177
178
  return;
@@ -257,7 +258,7 @@ module.exports = {
257
258
 
258
259
  redirect(url, alt) {
259
260
  // location
260
- if ('back' == url) url = this.ctx.get('Referrer') || alt || '/';
261
+ if ('back' === url) url = this.ctx.get('Referrer') || alt || '/';
261
262
  this.set('Location', encodeUrl(url));
262
263
 
263
264
  // status
@@ -324,7 +325,7 @@ module.exports = {
324
325
  */
325
326
 
326
327
  set lastModified(val) {
327
- if ('string' == typeof val) val = new Date(val);
328
+ if ('string' === typeof val) val = new Date(val);
328
329
  this.set('Last-Modified', val.toUTCString());
329
330
  },
330
331
 
@@ -457,7 +458,7 @@ module.exports = {
457
458
  set(field, val) {
458
459
  if (this.headerSent) return;
459
460
 
460
- if (2 == arguments.length) {
461
+ if (2 === arguments.length) {
461
462
  if (Array.isArray(val)) val = val.map(v => typeof v === 'string' ? v : String(v));
462
463
  else if (typeof val !== 'string') val = String(val);
463
464
  this.res.setHeader(field, val);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koa",
3
- "version": "2.11.0",
3
+ "version": "2.12.0",
4
4
  "description": "Koa web app framework",
5
5
  "main": "lib/application.js",
6
6
  "scripts": {
@@ -32,7 +32,6 @@
32
32
  "depd": "^1.1.2",
33
33
  "destroy": "^1.0.4",
34
34
  "encodeurl": "^1.0.2",
35
- "error-inject": "^1.0.0",
36
35
  "escape-html": "^1.0.3",
37
36
  "fresh": "~0.5.2",
38
37
  "http-assert": "^1.3.0",