"use strict";const fs=require("fs"),path=require("path"),stream=require("stream"),tar=require("tar-stream"),utils=require("../utils"),ready=require("get-ready");class TarFileStream extends stream.Transform{constructor(e){super(e);const r=tar.pack();r.on("data",e=>this.push(e)),r.on("end",()=>this.ready(!0));const t=utils.sourceType(e.source);if("file"===t)fs.stat(e.source,(t,s)=>{if(t)return this.emit("error",t);this.entry=r.entry({name:e.relativePath||path.basename(e.source),size:s.size,mode:511&s.mode},e=>{if(e)return this.emit("error",e);r.finalize()});const i=fs.createReadStream(e.source,e.fs);i.on("error",e=>this.emit("error",e)),i.pipe(this)});else if("buffer"===t){if(!e.relativePath)return this.emit("error","opts.relativePath is required if opts.source is a buffer");r.entry({name:e.relativePath},e.source),r.finalize(),this.end()}else{if(!e.relativePath)return process.nextTick(()=>this.emit("error","opts.relativePath is required"));if(e.size)this.entry=r.entry({name:e.relativePath,size:e.size},e=>{if(e)return this.emit("error",e);r.finalize()});else{e.suppressSizeWarning||console.warn("You should specify the size of streamming data by opts.size to prevent all streaming data from loading into memory. If you are sure about memory cost, pass opts.supressSizeWarning: true to suppress this warning");const t=[];this.entry=new stream.Writable({write(e,r,s){t.push(e),s()}}),this.entry.on("finish",()=>{r.entry({name:e.relativePath},Buffer.concat(t)),r.finalize()})}"stream"===t&&(e.source.on("error",e=>this.emit("error",e)),e.source.pipe(this))}}_transform(e,r,t){this.entry&&this.entry.write(e,r,t)}_flush(e){this.entry&&this.entry.end(),this.ready(e)}}ready.mixin(TarFileStream.prototype),module.exports=TarFileStream;