From ae3d3d9b52c2050cc0cd7b8ca5286b3f9ea42722 Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Sat, 14 Jan 2023 09:32:50 +0100 Subject: [PATCH] remove statuses package, precompute errors (#17) --- index.js | 26 ++++++++++++++++++-------- package.json | 3 +-- test/statuses.test.js | 18 ------------------ 3 files changed, 19 insertions(+), 28 deletions(-) delete mode 100644 test/statuses.test.js diff --git a/index.js b/index.js index 3998686..aa23256 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,6 @@ const ms = require('ms') const onFinished = require('on-finished') const parseRange = require('range-parser') const path = require('path') -const statuses = require('statuses') const Stream = require('stream') const util = require('util') const decode = require('fast-decode-uri-component') @@ -62,6 +61,15 @@ const MAX_MAXAGE = 60 * 60 * 24 * 365 * 1000 // 1 year const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/ +const ERROR_RESPONSES = { + 400: createHtmlDocument('Error', 'Bad Request'), + 403: createHtmlDocument('Error', 'Forbidden'), + 404: createHtmlDocument('Error', 'Not Found'), + 412: createHtmlDocument('Error', 'Precondition Failed'), + 416: createHtmlDocument('Error', 'Range Not Satisfiable'), + 500: createHtmlDocument('Error', 'Internal Server Error') +} + /** * Return a `SendStream` for `req` and `path`. * @@ -185,8 +193,6 @@ SendStream.prototype.error = function error (status, err) { } const res = this.res - const msg = statuses.message[status] - const doc = createHtmlDocument('Error', escapeHtml(msg)) // clear existing headers clearHeaders(res) @@ -196,13 +202,15 @@ SendStream.prototype.error = function error (status, err) { setHeaders(res, err.headers) } + const doc = ERROR_RESPONSES[status] + // send basic response res.statusCode = status res.setHeader('Content-Type', 'text/html; charset=UTF-8') - res.setHeader('Content-Length', Buffer.byteLength(doc)) + res.setHeader('Content-Length', doc[1]) res.setHeader('Content-Security-Policy', "default-src 'none'") res.setHeader('X-Content-Type-Options', 'nosniff') - res.end(doc) + res.end(doc[0]) } /** @@ -404,11 +412,11 @@ SendStream.prototype.redirect = function redirect (path) { // redirect res.statusCode = 301 res.setHeader('Content-Type', 'text/html; charset=UTF-8') - res.setHeader('Content-Length', Buffer.byteLength(doc)) + res.setHeader('Content-Length', doc[1]) res.setHeader('Content-Security-Policy', "default-src 'none'") res.setHeader('X-Content-Type-Options', 'nosniff') res.setHeader('Location', loc) - res.end(doc) + res.end(doc[0]) } /** @@ -875,7 +883,7 @@ function contentRange (type, size, range) { */ function createHtmlDocument (title, body) { - return '\n' + + const html = '\n' + '\n' + '\n' + '\n' + @@ -885,6 +893,8 @@ function createHtmlDocument (title, body) { '
' + body + '
\n' + '\n' + '\n' + + return [html, Buffer.byteLength(html)] } /** diff --git a/package.json b/package.json index 7cf1841..535fd57 100644 --- a/package.json +++ b/package.json @@ -29,8 +29,7 @@ "mime": "^3.0.0", "ms": "2.1.3", "on-finished": "2.4.1", - "range-parser": "~1.2.1", - "statuses": "2.0.1" + "range-parser": "~1.2.1" }, "devDependencies": { "@fastify/pre-commit": "^2.0.2", diff --git a/test/statuses.test.js b/test/statuses.test.js deleted file mode 100644 index 40cf8d9..0000000 --- a/test/statuses.test.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict' - -const { test } = require('tap') -const statuses = require('statuses') - -test('statuses', function (t) { - t.plan(1) - - t.test('should have uses statusCodes', function (t) { - t.plan(6) - t.ok(statuses(400)) - t.ok(statuses(403)) - t.ok(statuses(404)) - t.ok(statuses(412)) - t.ok(statuses(416)) - t.ok(statuses(500)) - }) -})