Search code examples
node.jssavestreamwriter

How do i stream a variable to a file Node JS?


I need to save a response to a file. The response is a zip file returned from a server which is recieved as a blob. I need to save to blob as a zip file on my local machine. The application itself is Electron, and the file needs to be stored in the background (without bothering the user). Filetype is zip ( and needs to be unzipped after this.

const writer = fs.createWriteStream(path.join(dir, 'files.zip'));
                writer.on('pipe', (src) => {
                  console.log('Something is piping into the writer.');
                });
                writer.end('This is the end\n');
                writer.on('finish', () => {
                  console.log('All writes are now complete.');
                });

writer.pipe(new Blob([response.data]));

Best i can do gives a 1kb corrupt file. I have read the node docs, but i can't get it to work.

ANY response is greatly appreciated, and please elaborate if you can. I feel like i need to use some type of buffer.


Solution

  • So i figured finally it out.

    Instead of a return type of Blob i had to return an arraybuffer. Next step is using the JSzip library and convert my function to async.

    Final result:

    //Create JSZip object to read incoming blob
    const zip = new JSZip;
    
    try {
      //Await unpacked arrayBuffer
      const zippedFiles = (await zip.loadAsync(response.data, { createFolders: true })).files;
    
      for (const file of Object.values(zippedFiles)) {
        if (file.dir) {
          await mkdirAsync(path.join(dir, file.name), { recursive: true });
        } else {
            file
              .nodeStream()
              .pipe(fs.createWriteStream(path.join(dir, file.name)))
              .on("finish", console.log);
          }
        }
      } catch (e) {
       throw MeaningfulError;
     }
    

    In short: This function takes an arraybuffer (of type .zip) and unpacks it to your system.

    • response.data is the arraybuffer (which needs to be unpacked).
    • dir is the directory in which the content needs to be unpacked.

    And that's all you need!