From fddf4ed14ceea98163230f2f3267c0d1ecb60635 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 17 Jan 2023 21:26:38 +0100 Subject: [PATCH] remove on-finished --- lib/SendStream.js | 16 ++++++++++------ package.json | 3 +-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/SendStream.js b/lib/SendStream.js index 7c55ad9..eebb2ee 100644 --- a/lib/SendStream.js +++ b/lib/SendStream.js @@ -18,7 +18,6 @@ const escapeHtml = require('escape-html') const fresh = require('fresh') const mime = require('mime') const ms = require('ms') -const onFinished = require('on-finished') const { clearHeaders } = require('./clearHeaders') const { collapseLeadingSlashes } = require('./collapseLeadingSlashes') @@ -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) diff --git a/package.json b/package.json index 8471aaa..cf8a9ff 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,7 @@ "fresh": "0.5.2", "http-errors": "2.0.0", "mime": "^3.0.0", - "ms": "2.1.3", - "on-finished": "2.4.1" + "ms": "2.1.3" }, "devDependencies": { "@fastify/pre-commit": "^2.0.2",