Monday 22 April 2019

Basic question about streams

Let's say I am doing something like:input.pipe(output); input is a readable stream of a file that exists in a temp folder. My question: will there be a problem if that temp folder is deleted before the stream has finished piping?Here is a code example that better illustrates it:/** * I am trying to pipe from a readable stream (which exists in a temp folder), * to a writable stream in my assets folder. Once the stream has finished piping * or if there was an error, I want to delete the temp folder. */ const fs = require('fs'); const path = require('path'); const { deleteFolder } = require('./utils'); try { const tempFolderPath = path.join(__dirname, 'temp'); const inputPath = path.join(tempFolderPath, 'file.txt'); const outputPath = path.join(__dirname, 'assets', 'file.txt'); const input = fs.createReadStream(inputPath); const output = fs.createWriteStream(outputPath); input.pipe(output); } catch (error) { console.error(error); } finally { // I want to delete the temp folder regardless if there was an error or not. // However, I am worried if there could be an issue if this runs before the // `input.pipe(output);` line completes because the `input` stream exists // inside `myFolder`. deleteFolder(tempFolderPath); } This code seems to be working for me, but I wonder if it is just because my file is small and it is able to pipe quickly before the deleteFolder() runs. Any help would be appreciated, thanks!

Submitted April 22, 2019 at 06:58PM by zaynv

No comments:

Post a Comment