Home > Back-end >  How can i save a file i download using fetch with fs
How can i save a file i download using fetch with fs

Time:08-13

I try downloading files with the fetch() function from github.
Then i try to save the fetched file Stream as a file with the fs-module.
When doing it, i get this error:

TypeError [ERR_INVALID_ARG_TYPE]: The "transform.writable" property must be an instance of WritableStream. Received an instance of WriteStream

My problem is, that i don't know the difference between WriteStream and WritableStream or how to convert them.

This is the code i run:

async function downloadFile(link, filename = "download") {
    var response = await fetch(link);
    var body = await response.body;
    var filepath = "./"   filename;
    var download_write_stream = fs.createWriteStream(filepath);
    console.log(download_write_stream.writable);
    await body.pipeTo(download_write_stream);
}

Node.js: v18.7.0

CodePudding user response:

Good question. Web streams are something new, and they are different way of handling streams. WritableStream tells us that we can create WritableStreams as follows:

import {
  WritableStream
} from 'node:stream/web';

const stream = new WritableStream({
  write(chunk) {
    console.log(chunk);
  }
});

Then, you could create a custom stream that writes each chunk to disk. An easy way could be:

const download_write_stream = fs.createWriteStream('./the_path');


const stream = new WritableStream({
  write(chunk) {
    download_write_stream.write(chunk);
  },
});

async function downloadFile(link, filename = 'download') {
  const response = await fetch(link);
  const body = await response.body;
  await body.pipeTo(stream);
}
  • Related