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 on-finished #34

Merged
merged 2 commits into from
Jan 17, 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
16 changes: 10 additions & 6 deletions lib/SendStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const escapeHtml = require('escape-html')
const fresh = require('fresh')
const mime = require('mime')
const ms = require('@lukeed/ms')
const onFinished = require('on-finished')

const { clearHeaders } = require('./clearHeaders')
const { collapseLeadingSlashes } = require('./collapseLeadingSlashes')
Expand Down Expand Up @@ -692,18 +691,23 @@ SendStream.prototype.stream = function stream (path, options) {
this.emit('stream', stream)
stream.pipe(res)

// cleanup
function cleanup () {
let destroyed = false

// destroy piped stream
function destroy () {
if (destroyed) {
return
}
destroyed = true
stream.destroy()
}

// response finished, cleanup
onFinished(res, cleanup)
res.once('finish', destroy)

// error handling
stream.on('error', function onerror (err) {
// clean up stream early
cleanup()
destroy()

// error
self.onStatError(err)
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"fresh": "0.5.2",
"http-errors": "2.0.0",
"mime": "^3.0.0",
"@lukeed/ms": "^2.0.1",
"on-finished": "2.4.1"
"@lukeed/ms": "^2.0.1"
},
"devDependencies": {
"@fastify/pre-commit": "^2.0.2",
Expand Down