"use strict";const fs=require("fs"),path=require("path"),mkdirp=require("mkdirp"),pump=require("pump");function destType(e){if("function"==typeof e._write||"function"==typeof e._transform)return"stream";if("string"!=typeof e){const e=new Error("Type is not supported, must be a file path, or a writable stream");throw e.name="IlligalDestinationError",e}return"path"}exports.sourceType=(e=>{if(e){if(e instanceof Buffer)return"buffer";if("function"==typeof e._read||"function"==typeof e._transform)return"stream";if("string"!=typeof e){const e=new Error("Type is not supported, must be a file path, file buffer, or a readable stream");throw e.name="IlligalSourceError",e}return"file"}}),exports.destType=destType;const illigalEntryError=new Error("Type is not supported, must be a file path, directory path, file buffer, or a readable stream");function safePipe(e){return new Promise((r,t)=>{pump(e[0],e[1],e=>{if(e)return t(e);r()})})}illigalEntryError.name="IlligalEntryError",exports.entryType=(e=>{if(e){if(e instanceof Buffer)return"buffer";if("function"==typeof e._read||"function"==typeof e._transform)return"stream";if("string"!=typeof e)throw illigalEntryError;return"fileOrDir"}}),exports.clone=(e=>{const r={};for(const t in e)r[t]=e[t];return r}),exports.makeFileProcessFn=(e=>(r,t,n)=>{(n=n||{}).source=r;const o="path"===destType(t)?fs.createWriteStream(t):t;return safePipe([new e(n),o])}),exports.makeCompressDirFn=(e=>(r,t,n)=>{const o="path"===destType(t)?fs.createWriteStream(t):t,i=new e;return i.addEntry(r,n),safePipe([i,o])}),exports.makeUncompressFn=(e=>(r,t,n)=>{if((n=n||{}).source=r,"path"!==destType(t)){const e=new Error("uncompress destination must be a directory");throw e.name="IlligalDestError",e}return new Promise((r,o)=>{mkdirp(t,i=>{if(i)return o(i);let s=0,f=0,a=!1;function p(){a&&s===f&&r()}new e(n).on("finish",()=>{a=!0,p()}).on("error",o).on("entry",(e,r,n)=>{if(r.on("end",n),"file"===e.type){const n=path.join(t,e.name);mkdirp(path.dirname(n),t=>{if(t)return o(t);s++,pump(r,fs.createWriteStream(n,{mode:e.mode}),e=>{if(e)return o(e);f++,p()})})}else mkdirp(path.join(t,e.name),e=>{if(e)return o(e);r.resume()})})})})}),exports.streamToBuffer=(e=>new Promise((r,t)=>{const n=[];e.on("readable",()=>{let r;for(;r=e.read();)n.push(r)}).on("end",()=>r(Buffer.concat(n))).on("error",e=>t(e))})),exports.safePipe=safePipe,exports.stripFileName=((e,r,t)=>{Buffer.isBuffer(r)&&(r=r.toString()),-1!==r.indexOf("\\")&&(r=r.replace(/\\+/g,"/")),"/"===r[0]&&(r=r.replace(/^\/+/,""));let n=r.split("/");return-1!==n.indexOf("..")&&(r=path.normalize(r),"win32"===process.platform&&(r=r.replace(/\\+/g,"/")),r=r.replace(/(\.\.\/)+/,""),"directory"===t&&r&&"/"!==r[r.length-1]&&(r+="/"),n=r.split("/")),e=Math.min(e,n.length-1),n.slice(e).join("/")||"/"});