Skip to content

Commit

Permalink
remove statuses package, precompute errors (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Jan 14, 2023
1 parent 75e0ff4 commit ae3d3d9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
26 changes: 18 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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`.
*
Expand Down Expand Up @@ -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)
Expand All @@ -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])
}

/**
Expand Down Expand Up @@ -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])
}

/**
Expand Down Expand Up @@ -875,7 +883,7 @@ function contentRange (type, size, range) {
*/

function createHtmlDocument (title, body) {
return '<!DOCTYPE html>\n' +
const html = '<!DOCTYPE html>\n' +
'<html lang="en">\n' +
'<head>\n' +
'<meta charset="utf-8">\n' +
Expand All @@ -885,6 +893,8 @@ function createHtmlDocument (title, body) {
'<pre>' + body + '</pre>\n' +
'</body>\n' +
'</html>\n'

return [html, Buffer.byteLength(html)]
}

/**
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 0 additions & 18 deletions test/statuses.test.js

This file was deleted.

0 comments on commit ae3d3d9

Please sign in to comment.