Closed
Description
It might be possible to have a more efficient way to pipe a file to a stream by adding a .stream
method to FileHandle
.
e.g.
FileHandle.prototype.stream = async function (dst, { start = 0, end, signal } = {}) {
// TODO: this[kRef](), this[kUnref](), if (signal.aborted) throw AbortError()
let pos = start
while (true) {
if (end && pos >= end) {
break
}
const { buffer, bytesRead } = await this.read({
buffer: Buffer.allocUnsafe(Math.min(end - pos, 16384)),
position: pos,
})
if (bytesRead === 0) {
break
}
if (dst.writableNeedDrain) {
await EE.once(res, 'drain', { signal })
}
if (dst.destroyed) {
break // TODO: break or throw?
}
dst.write(buffer)
pos += bytesRead
}
}
const fileHandle = fsp.open(filePath)
try {
await fileHandle.stream(dst)
} finally {
await fileHandle.close()
}
vs
await pipeline(fs.createReadStream(filePath), dst)
Less ergonomic but potentially better performance. This idea needs benchmarking.