Skip to content

Commit 4e55acd

Browse files
committed
fix(send): nodejs/node#26311
1 parent 354c505 commit 4e55acd

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

‎packages/send/src/sendFile.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createReadStream, statSync } from 'node:fs'
22
import type { IncomingMessage as I, ServerResponse as S } from 'node:http'
33
import { extname, isAbsolute } from 'node:path'
44
import { join } from 'node:path'
5+
import { Writable } from 'node:stream'
56
import { pipeline } from 'node:stream/promises'
67
import mime from 'mime'
78
import { createETag } from './utils.js'
@@ -45,6 +46,10 @@ export const enableCaching = (res: Res, caching: Caching): void => {
4546
if (cc) res.setHeader('Cache-Control', cc)
4647
}
4748

49+
const makeIndestructible = (stream: NodeJS.WritableStream) => {
50+
return new Writable({ write: stream.write.bind(stream) })
51+
}
52+
4853
/**
4954
* Sends a file by piping a stream to response.
5055
*
@@ -103,7 +108,7 @@ export const sendFile = <Request extends Req = Req, Response extends Res = Res>(
103108
res.writeHead(status, headers)
104109

105110
const stream = createReadStream(filePath, options)
106-
await pipeline(stream, res)
111+
await pipeline(stream, makeIndestructible(res))
107112

108113
return res
109114
}

0 commit comments

Comments
 (0)