How to send multiple large files as streams to server from Node.js?

20 Views Asked by At

Is there a way to use something like this in Node.js with basic APIs?

const fileStream1 = fs.readFileSync('tmp/large.2gb.file.gz')
const fileStream2 = fs.readFileSync("tmp/large.10gb.file.gz");

const formData = new FormData()
formData.append('file1', fileStream1)
formData.append("file2", fileStream2);

fetch('/upload', {
  method: 'POST',
  body: formData
})

And have it stream the files? All of the examples I've seen, like https://stackoverflow.com/a/76957447/169992, read the file into memory and set it in append('file', blobOrFile), and the MDN docs say it's a File or Blob only. How else can you accomplish multiple streaming file uploads in Node.js? If you get lower level to the https module, can you do it there somehow?

That last link seems like a hack, not sure that is the way to go:

export async function createStreamableFile(
  path: string,
): Promise<File> {
  const name = basename(path)
  const handle = await open(path)
  const { size } = await handle.stat()

  const file = new File([], name)
  file.stream = () => handle.readableWebStream() as ReadableStream

  // Set correct size otherwise, fetch will encounter UND_ERR_REQ_CONTENT_LENGTH_MISMATCH
  Object.defineProperty(file, 'size', { get: () => size })

  return file
}
0

There are 0 best solutions below