From dd35564df4ee56162c2d077edab84e1369558f01 Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Fri, 11 Oct 2019 16:40:06 +0000 Subject: [PATCH] Remove statuses and replace http-errors with http-errors-lite --- lib/application.js | 2 +- lib/context.js | 4 +- lib/response.js | 2 +- lib/statuses.js | 81 +++++++++++++++++++++++++++++++++++++ package.json | 3 +- test/application/respond.js | 21 ---------- test/application/use.js | 2 +- test/context/throw.js | 29 ------------- test/response/status.js | 15 ------- 9 files changed, 87 insertions(+), 72 deletions(-) create mode 100644 lib/statuses.js diff --git a/lib/application.js b/lib/application.js index c0a3e9a..12adc00 100644 --- a/lib/application.js +++ b/lib/application.js @@ -12,7 +12,7 @@ const compose = require('koa-compose'); const isJSON = require('koa-is-json'); const context = require('./context'); const request = require('./request'); -const statuses = require('statuses'); +const statuses = require('./statuses'); const Emitter = require('events'); const util = require('util'); const Stream = require('stream'); diff --git a/lib/context.js b/lib/context.js index 85e55dc..97dac5a 100644 --- a/lib/context.js +++ b/lib/context.js @@ -6,10 +6,10 @@ */ const util = require('util'); -const createError = require('http-errors'); +const createError = require('http-errors-lite'); const httpAssert = require('http-assert'); const delegate = require('./delegates'); -const statuses = require('statuses'); +const statuses = require('./statuses'); /** * Context prototype. diff --git a/lib/response.js b/lib/response.js index d1cee29..bc98a1a 100644 --- a/lib/response.js +++ b/lib/response.js @@ -11,7 +11,7 @@ const onFinish = require('on-finished'); const isJSON = require('koa-is-json'); const escape = require('escape-html'); const typeis = require('type-is').is; -const statuses = require('statuses'); +const statuses = require('./statuses'); const destroy = require('destroy'); const assert = require('assert'); const extname = require('path').extname; diff --git a/lib/statuses.js b/lib/statuses.js new file mode 100644 index 0000000..fb228f6 --- /dev/null +++ b/lib/statuses.js @@ -0,0 +1,81 @@ + +module.exports = { + 100: 'Continue', + 101: 'Switching Protocols', + 102: 'Processing', + 103: 'Early Hints', + 200: 'OK', + 201: 'Created', + 202: 'Accepted', + 203: 'Non-Authoritative Information', + 204: 'No Content', + 205: 'Reset Content', + 206: 'Partial Content', + 207: 'Multi-Status', + 208: 'Already Reported', + 226: 'IM Used', + 300: 'Multiple Choices', + 301: 'Moved Permanently', + 302: 'Found', + 303: 'See Other', + 304: 'Not Modified', + 305: 'Use Proxy', + 306: '(Unused)', + 307: 'Temporary Redirect', + 308: 'Permanent Redirect', + 400: 'Bad Request', + 401: 'Unauthorized', + 402: 'Payment Required', + 403: 'Forbidden', + 404: 'Not Found', + 405: 'Method Not Allowed', + 406: 'Not Acceptable', + 407: 'Proxy Authentication Required', + 408: 'Request Timeout', + 409: 'Conflict', + 410: 'Gone', + 411: 'Length Required', + 412: 'Precondition Failed', + 413: 'Payload Too Large', + 414: 'URI Too Long', + 415: 'Unsupported Media Type', + 416: 'Range Not Satisfiable', + 417: 'Expectation Failed', + 418: 'I\'m a teapot', + 421: 'Misdirected Request', + 422: 'Unprocessable Entity', + 423: 'Locked', + 424: 'Failed Dependency', + 425: 'Too Early', + 426: 'Upgrade Required', + 428: 'Precondition Required', + 429: 'Too Many Requests', + 431: 'Request Header Fields Too Large', + 451: 'Unavailable For Legal Reasons', + 500: 'Internal Server Error', + 501: 'Not Implemented', + 502: 'Bad Gateway', + 503: 'Service Unavailable', + 504: 'Gateway Timeout', + 505: 'HTTP Version Not Supported', + 506: 'Variant Also Negotiates', + 507: 'Insufficient Storage', + 508: 'Loop Detected', + 509: 'Bandwidth Limit Exceeded', + 510: 'Not Extended', + 511: 'Network Authentication Required', + redirect: { + 300: true, + 301: true, + 302: true, + 303: true, + 305: true, + 307: true, + 308: true + }, + empty: { + 204: true, + 205: true, + 304: true + } +}; diff --git a/package.json b/package.json index 741231f..59066bc 100644 --- a/package.json +++ b/package.json @@ -30,12 +30,11 @@ "escape-html": "^1.0.3", "fresh": "~0.5.2", "http-assert": "^1.3.0", - "http-errors": "^1.6.3", + "http-errors-lite": "^2.0.2", "koa-compose": "^4.1.0", "koa-is-json": "^1.0.0", "on-finished": "^2.3.0", "parseurl": "^1.3.2", - "statuses": "^1.5.0", "type-is": "^1.6.16", "vary": "^1.1.2" }, diff --git a/test/application/respond.js b/test/application/respond.js index 14f1321..614b357 100644 --- a/test/application/respond.js +++ b/test/application/respond.js @@ -2,7 +2,6 @@ 'use strict'; const request = require('supertest'); -const statuses = require('statuses'); const assert = require('assert'); const Koa = require('../..'); const fs = require('fs'); @@ -351,26 +350,6 @@ describe('app.respond', () => { }); }); - describe('with custom status=700', () => { - it('should respond with the associated status message', async() => { - const app = new Koa(); - statuses['700'] = 'custom status'; - - app.use(ctx => { - ctx.status = 700; - }); - - const server = app.listen(); - - const res = await request(server) - .get('/') - .expect(700) - .expect('custom status'); - - assert.equal(res.res.statusMessage, 'custom status'); - }); - }); - describe('with custom statusMessage=ok', () => { it('should respond with the custom status message', async() => { const app = new Koa(); diff --git a/test/application/use.js b/test/application/use.js index d91d988..99d9542 100644 --- a/test/application/use.js +++ b/test/application/use.js @@ -44,7 +44,7 @@ describe('app.use(fn)', () => { it('should catch thrown errors in non-async functions', () => { const app = new Koa(); - app.use(ctx => ctx.throw('Not Found', 404)); + app.use(ctx => ctx.throw(404, 'Not Found')); return request(app.callback()) .get('/') diff --git a/test/context/throw.js b/test/context/throw.js index 5c091c8..208c332 100644 --- a/test/context/throw.js +++ b/test/context/throw.js @@ -32,21 +32,6 @@ describe('ctx.throw(err)', () => { }); }); -describe('ctx.throw(err, status)', () => { - it('should throw the error and set .status', () => { - const ctx = context(); - const error = new Error('test'); - - try { - ctx.throw(error, 422); - } catch (err) { - assert.equal(err.status, 422); - assert.equal(err.message, 'test'); - assert.equal(err.expose, true); - } - }); -}); - describe('ctx.throw(status, err)', () => { it('should throw the error and set .status', () => { const ctx = context(); @@ -62,20 +47,6 @@ describe('ctx.throw(status, err)', () => { }); }); -describe('ctx.throw(msg, status)', () => { - it('should throw an error', () => { - const ctx = context(); - - try { - ctx.throw('name required', 400); - } catch (err) { - assert.equal(err.message, 'name required'); - assert.equal(err.status, 400); - assert.equal(err.expose, true); - } - }); -}); - describe('ctx.throw(status, msg)', () => { it('should throw an error', () => { const ctx = context(); diff --git a/test/response/status.js b/test/response/status.js index b7e6b0b..5386d2b 100644 --- a/test/response/status.js +++ b/test/response/status.js @@ -3,7 +3,6 @@ const response = require('../helpers/context').response; const request = require('supertest'); -const statuses = require('statuses'); const assert = require('assert'); const Koa = require('../..'); @@ -29,20 +28,6 @@ describe('res.status=', () => { }); }); - describe('and custom status', () => { - beforeEach(() => statuses['700'] = 'custom status'); - - it('should set the status', () => { - const res = response(); - res.status = 700; - assert.equal(res.status, 700); - }); - - it('should not throw', () => { - response().status = 700; - }); - }); - describe('and HTTP/2', () => { it('should not set the status message', () => { const res = response({