How to write a function that uses asynchronous functions and streams?

139 views Asked by At

My initial attempt using streams inside an asynchronous function was this:

const fs = require('fs');
const openpgp = require('openpgp');
const util = require('util');


const readFileAsync = util.promisify(require('fs').readFile);

const encryptFile = async (inputFile, outputFile) => {
  const readableStream = fs.createReadStream(inputFile);
  const writeStream = fs.createWriteStream(outputFile);

  const pubKey = await readFileAsync('./public.asc');
  const options = {
    message: openpgp.message.fromBinary(readableStream),
    publicKeys: (await openpgp.key.readArmored(pubKey)).keys,
    armor: false,
  };

  const ciphertext = await openpgp.encrypt(options);
  const encrypted  = ciphertext.message.packets.write();

  const stream = await openpgp.stream.webToNode(encrypted);

  return new Promise((resolve, reject) => {
    readableStream.once('error', reject) // does not call reject if inputFile doesn't exist
    stream.once('error', reject);
    writeStream.once('finish', resolve);
    writeStream.once('error', reject);
    stream.pipe(writeStream);
  });

};

However, I found that if inputFile was a path to a file that doesn't exist, readableStream would emit an error that wasn't handled by readableStream.once('error', reject). Why is this the case?

So I tried to wrap the that in a Promise too and found that I had to remove my async/await syntax completely so I ended up with this.

This seems to work but since using streams is unfamiliar territory for me, can anyone point out anything I missed and if there's simply a better way to write this type of code?

const encryptFile = (inputFile, outputFile) => new Promise((resolve, reject) => {
  const readableStream = fs.createReadStream(inputFile);
  const writeStream = fs.createWriteStream(outputFile);
  readableStream.once('error', reject);
  writeStream.once('error', reject);
  writeStream.once('finish', resolve);

  readFileAsync('./public.asc')
    .then(openpgp.key.readArmored)
    .then((pubKey) => openpgp.encrypt({
        message: openpgp.message.fromBinary(readableStream),
        publicKeys: pubKey.keys,
        armor: false,
      })
    )
    .then((encrypted) => encrypted.message.packets.write())
    .then(openpgp.stream.webToNode)
    .then((cipherNodeStream) => {
      cipherNodeStream.once('error', reject);
      cipherNodeStream.pipe(writeStream)
    })
    .catch(reject);
});
0

There are 0 answers