UNPKG

fast-extract

Version:

Extract contents from various archive types (tar, tar.bz2, tar.gz, tar.xz, tgz, zip)

58 lines (57 loc) 2.01 kB
import writer from './compat/flush-write-stream.js'; import pumpify from './compat/pumpify.js'; import rimrafAll from './compat/rimrafAll.js'; import createPipeline from './createPipeline.js'; import exitCleanup from './exitCleanup.js'; export default function createWriteStream(dest, options_) { if (typeof options_ === 'string') options_ = { type: options_ }; const options = { _tempPaths: [], ...options_ }; const streams = createPipeline(dest, options); const generatedFiles = [ dest ].concat(options._tempPaths); generatedFiles.forEach(exitCleanup.add); let error = null; let ended = false; function onError(err, callback) { if (error || ended) return callback(err); error = err; res.destroy(err); return rimrafAll(generatedFiles, (err2)=>{ generatedFiles.forEach(exitCleanup.remove); callback(err || err2); }); } function onEnd(callback) { if (error || ended) return callback(); ended = true; return rimrafAll(options._tempPaths, (err)=>{ generatedFiles.forEach(exitCleanup.remove); callback(err); }); } const res = streams.length < 2 ? streams[0] : pumpify(streams); const write = writer(function write(chunk, encoding, callback) { res.write(chunk, encoding, (err)=>{ if (error) return; // skip if errored so will not emit errors multiple times err ? onError(err, callback) : callback(); }); }, function flush(callback) { if (error) return; // skip if errored so will not emit errors multiple times res.end((err)=>{ if (error) return; // skip if errored so will not emit errors multiple times err ? onError(err || error, callback) : onEnd(callback); }); }); res.on('error', (err)=>{ onError(err, ()=>{ write.destroy(err); }); }); return write; }