Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove statuses package, precompute html errors #17

Merged
merged 2 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')

Expand Down Expand Up @@ -61,6 +60,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 @@ -184,8 +192,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 @@ -195,13 +201,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 @@ -403,11 +411,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 @@ -874,7 +882,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 @@ -884,6 +892,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 @@ -28,8 +28,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.