Skip to content

FileHandle.stream #38350

Closed
Closed
@ronag

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.

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.fsIssues and PRs related to the fs subsystem / file system.good first issueIssues that are suitable for first-time contributors.stale

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions