-
Notifications
You must be signed in to change notification settings - Fork 6
/
jpacks.min.js
1 lines (1 loc) · 9.67 KB
/
jpacks.min.js
1
!function(n){function r(){function n(n){n=n||{};var r=this;Object.keys(n).forEach(function(e){r[e]=n[e]})}function r(n){n=n||{},Object.keys(n).forEach(function(r){f[r]=n[r]})}function e(r,e){if(r instanceof ArrayBuffer)return r;"string"==typeof r&&n.stringBytes&&(r=n.stringBytes(r,e));var t=new ArrayBuffer(r.length);return new Uint8Array(t,0,r.length).set(r),t}function t(r,t,a,i){if(null===r)return null;var u=n.from(r);return a=a||{},i=i||[0],Object.keys(u.defaultOptions||{}).forEach(function(n){void 0===a[n]&&(a[n]=u.defaultOptions[n])}),Object.keys(f).forEach(function(n){void 0===a[n]&&(a[n]=f[n])}),u.unpack(e(t,a),a,i)}function a(r,e,t,a){if(null===r)return null;var i=n.from(r);return a=a||[],t=t||{},Object.keys(i.defaultOptions||{}).forEach(function(n){void 0===t[n]&&(t[n]=i.defaultOptions[n])}),Object.keys(f).forEach(function(n){void 0===t[n]&&(t[n]=f[n])}),i.pack(e,t,a),a}function i(n,r,e){return n.length<=0?n:function(){var t=[];if([].push.apply(t,e),[].push.apply(t,arguments),t.length>=n.length)return n.apply(null,t);var a=i(n,r,t);return"function"==typeof r&&r(a,t),a}}function u(n){function r(n){if(null===n)return"null";if(!n)return n;if(n.namespace)return"number"===n.namespace?"'"+n.name+"'":n.args?n.namespace+"("+u.apply(null,n.args)+")":"'"+n.namespace+"'";if("function"==typeof n){if(!n.name)return n.namespace="$fn",n.namespace}else if("object"==typeof n){var e=new n.constructor;return Object.keys(n).forEach(function(t){e[t]=r(n[t])}),e}return n.name?"'"+n.name+"'":"string"==typeof n?"'"+n+"'":n}if(arguments.length>1){for(var e=[],t=0;t<arguments.length;t++)e.push(u(arguments[t]));return e.join()}return JSON.stringify(r(n)||"").replace(/"/g,"")}var c={};n.register=function(r,e){c[r]=e,n[r]||(n[r]=e)},n.def=n.register;var o=[function(n){for(var r={},e=n;"string"==typeof e;){if(r[e])return;e=c[e],r[e]=!0}return e}];n.pushPattern=function(n){o.push(n)},n.from=function(r){if(r instanceof n)return r;for(var e=-1,t=0;t<o.length;t++)if(e!==t){var a=o[t](r);if(a){if(a instanceof n)return a;r=a,e=t,t=0}}};var f={littleEndian:!0};return n.setDefaultOptions=r,n.arrayBufferFrom=e,n.unpack=t,n.pack=a,n.together=i,n.stringify=u,n.prototype.toString=function(){return u(this)},n}function e(){function n(n,r){var e;return"number"!==r&&null!==r&&(e=A.from(r)),new A({unpack:function(t,a,i){var u;if(0===(u=e?A.unpack(e,t,a,i):r))return[];var c=[],o=A.from(n);if(o.array&&(a.littleEndian||1===o.size)){var f=i[0],s=null===u?t.byteLength-f:o.size*u,p=new ArrayBuffer(s),g=new o.array(p);return new Uint8Array(p).set(new Uint8Array(t,f,Math.min(s,t.byteLength-f))),[].push.apply(c,g),i[0]+=s,c}if(null===u)for(var y;i[0]<t.byteLength&&y!==i[0];)y=i[0],c.push(A.unpack(o,t,a,i));else for(var l=0;l<u;l++)c.push(A.unpack(o,t,a,i));return c},pack:function(t,a,i){var u=A.from(n);if(t||e){var c;if(e?(c=t?t.length:0,A.pack(e,c,a,i)):c=null===r?(t||[]).length:r,t&&c){if(u.array&&(a.littleEndian||1===u.size)){var o=u.size*c,f=new ArrayBuffer(o);new u.array(f).set(t.slice(0,c));var s=new Uint8Array(f);return void[].push.apply(i,s)}for(var p=0;p<c;p++)A.pack(u,(t||[])[p],a,i)}}},namespace:"array",args:arguments,size:"number"===r?itemSchema.size*r:void 0})}function e(n){return j(n,"uint8")}function t(n){return j(n,"uint16")}function a(n){return j(n,"uint32")}function i(n){return A.array("uint8",n)}function u(n){if(n instanceof A)return n;var r=Object.keys(n);return new A({unpack:function(e,t,a){var i=new n.constructor,u=t.$scope;return t.$scope={target:i,offsets:new n.constructor,schema:n},r.forEach(function(r){t.$scope.exit?i[r]=null:(t.$scope.offsets[r]=a[0],i[r]=A.unpack(n[r],e,t,a))}),t.$scope=u,i},pack:function(e,t,a){var i=t.$scope;t.$scope={target:e,offsets:new n.constructor,schema:n},r.every(function(r){return!t.$scope.exit&&(t.$scope.offsets[r]=a.length,A.pack(n[r],e[r],t,a),!0)}),t.$scope=i},args:arguments,namespace:"object"})}function c(n,r){var e=Object.keys(n);return new A({unpack:function(t,a,i){var u=i[0],c={};return e.forEach(function(r){i[0]=u,c[r]=A.unpack(n[r],t,a,i)}),i[0]+=r,c},pack:function(t,a,i){var u=new ArrayBuffer(r),c=new Uint8Array(u);e.forEach(function(r){if(void 0!==t[r]){var e=[];A.pack(n[r],t[r],a,e),c.set(e)}}),[].push.apply(i,c)},size:r,args:arguments,namespace:"union"})}function o(n,r){if(r=A.from(r),n instanceof Array){var e={};n.forEach(function(n,r){e[n]=r}),n=e}var t=Object.keys(n);return new A({unpack:function(e,a,i){var u,c=A.unpack(r,e,a,i);return t.every(function(r){return n[r]!==c||(u=r,!1)}),u||c},pack:function(e,a,i){if("number"==typeof e)return void A.pack(r,e,a,i);if(t.every(function(t){return t!==e||(A.pack(r,n[t],a,i),!1)}))throw new Error('Not find enum "'+e+'".')},namespace:"enums",args:arguments})}function f(n){return/[\u0080-\uffff]/.test(n)?unescape(encodeURIComponent(n)):n}function s(n){return/[\u00c0-\u00df][\u0080-\u00bf]/.test(n)||/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/.test(n)?decodeURIComponent(escape(n)):n}function p(n,r){return r=r||{},r.browser||"undefined"==typeof Buffer?"function"==typeof TextEncoder?Array.from(new TextEncoder(r.encoding).encode(n)):f(n).split("").map(function(n){return n.charCodeAt()}):new Buffer(n,r.encoding||"utf8")}function g(n){var r=A.array("uint8",n);return new A({unpack:function(n,e,t){var a=A.unpack(r,n,e,t);return e.browser||"undefined"==typeof Buffer?"function"==typeof TextDecoder?new TextDecoder(e.encoding).decode(a):s(String.fromCharCode.apply(String,a)):new Buffer(a).toString(e.encoding)},pack:function(n,e,t){A.pack(r,p(n,e),e,t)},namespace:"string",args:arguments})}function y(n){return new A({unpack:function(r,e,t){var a;a=null===n?new Uint8Array(r,t[0]):A.unpack(A.bytes(n),r,e,t);for(var i=0;a[i];)i++;var u=A.unpack(A.string(i),a,e);return null===n&&(t[0]+=i+1),u},pack:function(r,e,t){var a=[0];[].unshift.apply(a,A.stringBytes(r,e)),A.pack(A.bytes(n),a,e,t)},namespace:"cstring",args:arguments})}function l(n,r){return new A({unpack:function(e,t,a){var i=t.$scope.target[n];return A.unpack(r(i),e,t,a)},pack:function(e,t,a){var i=t.$scope.target[n];A.pack(r(i),e,t,a)},namespace:"depend",args:arguments})}function v(n,r){return x(n,A.array(r))}function m(){return new A({unpack:function(n,r,e){return r.$scope.exit=!0,null},pack:function(n,r,e){r.$scope.exit=!0},namespace:"exit",args:arguments})}function h(n,r,e,t){var a=A.bytes(t);return new A({unpack:function(n,t,i){var u=r(A.unpack(a,n,t,i));return A.unpack(e,u,t)},pack:function(r,t,i){var u=n(A.pack(e,r,t));A.pack(a,u,t,i)},namespace:"parse",args:arguments})}function k(n,r){return new A({unpack:function(){return/string|number/.test(typeof n)?n+r:"function"==typeof n?n(r):r},pack:function(){},args:arguments,namespace:"virtual"})}function d(n,r){return new A({unpack:function(e,t,a){var i=t.$scope.target[n],u=A.from(r);return A.unpack(A.array(u,i),e,t,a)},pack:function(e,t,a){for(var i=t.$scope.schema[n],u=A.from(r),c=A.pack(i,e.length,t),o=t.$scope.offsets[n],f=0;f<c.length;f++)a[o+f]=c[f];A.pack(A.array(u,null),e,t,a)},namespace:"link",args:arguments})}function w(n,r){return C(n,A.array(r))}function b(n){var r={};return n.forEach(function(n){var e=A.from(n);if(e){var t=e.args[0];Object.keys(t).forEach(function(n){r[n]=t[n]})}}),A.object(r)}var A=r(),E={int8:{type:"Int8",size:1,alias:["shortint"],array:Int8Array},uint8:{type:"Uint8",size:1,alias:["byte"],array:Uint8Array},int16:{type:"Int16",size:2,alias:["smallint"],array:Int16Array},uint16:{type:"Uint16",size:2,alias:["word"],array:Uint16Array},int32:{type:"Int32",size:4,alias:["longint"],array:Int32Array},uint32:{type:"Uint32",size:4,alias:["longword"],array:Uint32Array},float32:{type:"Float32",size:4,alias:["single"],array:Float32Array},float64:{type:"Float64",size:8,alias:["double"],array:Float64Array}};Object.keys(E).forEach(function(n){var r=E[n],e=new A({unpack:function(n){return function(e,t,a){var i=a[0];a[0]+=r.size;var u;return u=e instanceof DataView?e:new DataView(e),u[n](i,t.littleEndian)}}("get"+r.type),pack:function(n){return function(e,t,a){var i=new ArrayBuffer(r.size),u=new DataView(i),c=new Uint8Array(i);u[n](0,e,t.littleEndian),[].push.apply(a,c)}}("set"+r.type),size:r.size,name:n,namespace:"number",array:r.array});A.register(n,e),(r.alias||[]).forEach(function(n){A.register(n,e)})});var j=A.together(n,function(n,r){n.namespace="array",n.args=r});A.register("array",j),A.register("shortArray",e),A.register("smallArray",t),A.register("longArray",a),A.register("bytes",i);var z=A.together(u,function(n,r){n.namespace="object",n.args=r});A.register("object",z),A.pushPattern(function(n){if("object"==typeof n){if(n instanceof A)return;if(n instanceof Array)return;return z(n)}});var O=A.together(c,function(n,r){n.namespace="union",n.args=r});A.register("union",O);var $=A.together(o,function(n,r){n.namespace="enums",n.args=r});A.register("enums",$),A.stringBytes=p;var B=A.together(g,function(n,r){n.namespace="string",n.args=r});A.register("string",B),A.register("shortString",B("uint8")),A.register("smallString",B("uint16")),A.register("longString",B("uint32"));var U=A.together(y,function(n,r){n.namespace="cstring",n.args=r});A.register("cstring",U),A.register("pchar",U(null));var x=A.together(l,function(n,r){n.namespace="depend",n.args=r});A.register("depend",x),A.register("dependArray",v);var S=A.together(m,function(n,r){n.namespace="exit",n.args=r});A.register("exit",S);var I=A.together(h,function(n,r){n.namespace="parse",n.args=r});A.register("parse",I);var D=A.together(k,function(n,r){n.namespace="virtual",n.args=r});A.register("virtual",D);var C=A.together(d,function(n,r){n.namespace="link",n.args=r});A.register("link",C),A.register("linkArray",w);var F=A.together(b,function(n,r){n.namespace="merge",n.args=r});return A.register("merge",F),A}var t=e();t.create=e;var a=t;"function"==typeof define?(define.amd||define.cmd)&&define(function(){return a}):"undefined"!=typeof module&&module.exports?module.exports=a:window.jpacks=a}();