UNPKG

rafale

Version:

Deploy every services, everywhere, in just a few seconds

1 lines 3.9 kB
var Utils=require("./util"),Headers=require("./headers"),Constants=Utils.Constants,Methods=require("./methods");module.exports=function(t){function e(){return t&&Buffer.isBuffer(t)?(f.loadDataHeaderFromBinary(t),t.slice(f.realDataOffset,f.realDataOffset+f.compressedSize)):new Buffer(0)}function n(t){return f.flags&!1&&Utils.crc32(t)!=f.crc?!1:!0}function r(t,r,s){if("undefined"==typeof r&&"string"==typeof t&&(s=t,t=void 0),l)return t&&r&&r(new Buffer(0),Utils.Errors.DIRECTORY_CONTENT_ERROR),new Buffer(0);var o=e();if(0==o.length)return t&&r&&r(o,Utils.Errors.NO_DATA),o;var i=new Buffer(f.size);switch(i.fill(0),f.method){case Utils.Constants.STORED:return o.copy(i),n(i)?(t&&r&&r(i),i):(t&&r&&r(i,Utils.Errors.BAD_CRC),Utils.Errors.BAD_CRC);case Utils.Constants.DEFLATED:var a=new Methods.Inflater(o);if(!t)return a.inflate(i),n(i)||console.warn(Utils.Errors.BAD_CRC+" "+c.toString()),i;a.inflateAsync(function(t){t.copy(i,0),n(i)?r&&r(i):r&&r(i,Utils.Errors.BAD_CRC)});break;default:return t&&r&&r(new Buffer(0),Utils.Errors.UNKNOWN_METHOD),Utils.Errors.UNKNOWN_METHOD}}function s(n,r){if((!g||!g.length)&&Buffer.isBuffer(t))return n&&r&&r(e()),e();if(g.length&&!l){var s;switch(f.method){case Utils.Constants.STORED:return f.compressedSize=f.size,s=new Buffer(g.length),g.copy(s),n&&r&&r(s),s;default:case Utils.Constants.DEFLATED:var o=new Methods.Deflater(g);if(!n){var i=o.deflate();return f.compressedSize=i.length,i}o.deflateAsync(function(t){s=new Buffer(t.length),f.compressedSize=t.length,t.copy(s),r&&r(s)}),o=null}}else{if(!n||!r)return new Buffer(0);r(new Buffer(0))}}function o(t,e){return(t.readUInt32LE(e+4)<<4)+t.readUInt32LE(e)}function i(t){for(var e,n,r,s=0;s<t.length;)e=t.readUInt16LE(s),s+=2,n=t.readUInt16LE(s),s+=2,r=t.slice(s,s+n),s+=n,Constants.ID_ZIP64===e&&a(r)}function a(t){var e,n,r,s;t.length>=Constants.EF_ZIP64_SCOMP&&(e=o(t,Constants.EF_ZIP64_SUNCOMP),f.size===Constants.EF_ZIP64_OR_32&&(f.size=e)),t.length>=Constants.EF_ZIP64_RHO&&(n=o(t,Constants.EF_ZIP64_SCOMP),f.compressedSize===Constants.EF_ZIP64_OR_32&&(f.compressedSize=n)),t.length>=Constants.EF_ZIP64_DSN&&(r=o(t,Constants.EF_ZIP64_RHO),f.offset===Constants.EF_ZIP64_OR_32&&(f.offset=r)),t.length>=Constants.EF_ZIP64_DSN+4&&(s=t.readUInt32LE(Constants.EF_ZIP64_DSN),f.diskNumStart===Constants.EF_ZIP64_OR_16&&(f.diskNumStart=s))}var f=new Headers.EntryHeader,c=new Buffer(0),u=new Buffer(0),l=!1,g=null,d=new Buffer(0);return{get entryName(){return c.toString()},get rawEntryName(){return c},set entryName(t){c=Utils.toBuffer(t);var e=c[c.length-1];l=47==e||92==e,f.fileNameLength=c.length},get extra(){return d},set extra(t){d=t,f.extraLength=t.length,i(t)},get comment(){return u.toString()},set comment(t){u=Utils.toBuffer(t),f.commentLength=u.length},get name(){var t=c.toString();return l?t.substr(t.length-1).split("/").pop():t.split("/").pop()},get isDirectory(){return l},getCompressedData:function(){return s(!1,null)},getCompressedDataAsync:function(t){s(!0,t)},setData:function(t){g=Utils.toBuffer(t),!l&&g.length?(f.size=g.length,f.method=Utils.Constants.DEFLATED,f.crc=Utils.crc32(t)):f.method=Utils.Constants.STORED},getData:function(t){return r(!1,null,t)},getDataAsync:function(t,e){r(!0,t,e)},set attr(t){f.attr=t},get attr(){return f.attr},set header(t){f.loadFromBinary(t)},get header(){return f},packHeader:function(){var t=f.entryHeaderToBinary();return c.copy(t,Utils.Constants.CENHDR),f.extraLength&&d.copy(t,Utils.Constants.CENHDR+c.length),f.commentLength&&u.copy(t,Utils.Constants.CENHDR+c.length+f.extraLength,u.length),t},toString:function(){return'{\n "entryName" : "'+c.toString()+'",\n "name" : "'+c.toString().split("/").pop()+'",\n "comment" : "'+u.toString()+'",\n "isDirectory" : '+l+',\n "header" : '+f.toString().replace(/\t/gm," ")+',\n "compressedData" : <'+(t&&t.length+" bytes buffer"||"null")+'>\n "data" : <'+(g&&g.length+" bytes buffer"||"null")+">\n}"}}};