@substrate-system/bencode
Version:
Bencode de/encoder
12 lines (10 loc) • 6.07 kB
JavaScript
var I=Object.defineProperty;var s=(e,n)=>I(e,"name",{value:n,configurable:!0});var N=Object.defineProperty,U=s((e,n)=>N(e,"name",{value:n,configurable:!0}),"__name");var p="0123456789abcdef",_=[],A=[];for(let e=0;e<256;e++)_[e]=p[e>>4&15]+p[e&15],e<16&&(e<10?A[48+e]=e:A[87+e]=e);var b=U(e=>{let n=e.length,t="",o=0;for(;o<n;)t+=_[e[o++]];return t},"arr2hex");var k=U((e,n=0)=>{let t=e.length||0;if(!n){let f=t;for(;f--;)n+=e[f].length}let o=new Uint8Array(n),c=n,u=t;for(;u--;)c-=e[u].length,o.set(e[u],c);return o},"concat");var L="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",j=typeof Uint8Array>"u"?[]:new Uint8Array(256);for(g=0;g<L.length;g++)j[L.charCodeAt(g)]=g;var g;var O=Object.defineProperty,D=s((e,n)=>O(e,"name",{value:n,configurable:!0}),"__name"),v=new TextDecoder,m=D((e,n)=>n?new TextDecoder(n).decode(e):v.decode(e),"arr2text"),P=new TextEncoder,a=D(e=>P.encode(e),"text2arr");var C=typeof window<"u"?window:globalThis,T=C.crypto||C.msCrypto||{},ce=T.subtle||T.webkitSubtle;function l(e){let n=e<0?1:0;return e=Math.abs(Number(e||1)),Math.floor(Math.log10(e))+1+n}s(l,"digitCount");function d(e){return ArrayBuffer.isView(e)?"arraybufferview":Array.isArray(e)?"array":e instanceof Number?"number":e instanceof Boolean?"boolean":e instanceof Set?"set":e instanceof Map?"map":e instanceof String?"string":e instanceof ArrayBuffer?"arraybuffer":typeof e}s(d,"getType");function i(e,n,t){let o=[];i._encode(o,e);let c=k(o);return i.bytes=c.length,ArrayBuffer.isView(n)?(n.set(c,t),n):c}s(i,"encode");i.bytes=-1;i._floatConversionDetected=!1;i._encode=function(e,n){if(n!=null)switch(d(n)){case"object":i.dict(e,n);break;case"map":i.dictMap(e,n);break;case"array":i.list(e,n);break;case"set":i.listSet(e,n);break;case"string":i.string(e,n);break;case"number":i.number(e,n);break;case"boolean":i.number(e,n);break;case"arraybufferview":i.buffer(e,new Uint8Array(n.buffer,n.byteOffset,n.byteLength));break;case"arraybuffer":i.buffer(e,new Uint8Array(n));break}};var y=new Uint8Array([101]),S=new Uint8Array([100]),E=new Uint8Array([108]);i.buffer=function(e,n){e.push(a(n.length+":"),n)};i.string=function(e,n){e.push(a(a(n).byteLength+":"+n))};i.number=function(e,n){if(Number.isInteger(n))return e.push(a("i"+BigInt(n)+"e"));let t=2147483648,o=n/t<<0,c=n%t<<0,u=o*t+c;e.push(a("i"+u+"e")),u!==n&&!i._floatConversionDetected&&(i._floatConversionDetected=!0,console.warn('WARNING: Possible data corruption detected with value "'+n+'":','Bencoding only defines support for integers, value was converted to "'+u+'"'),console.trace())};i.dict=function(e,n){e.push(S);let t=0,o,c=Object.keys(n).sort(),u=c.length;for(;t<u;t++)o=c[t],n[o]!=null&&(i.string(e,o),i._encode(e,n[o]));e.push(y)};i.dictMap=function(e,n){e.push(S);let t=Array.from(n.keys()).sort();for(let o of t)n.get(o)!=null&&(ArrayBuffer.isView(o)?i._encode(e,o):i.string(e,String(o)),i._encode(e,n.get(o)));e.push(y)};i.list=function(e,n){let t=0,o=n.length;for(e.push(E);t<o;t++)n[t]!=null&&i._encode(e,n[t]);e.push(y)};i.listSet=function(e,n){e.push(E);for(let t of n)t!=null&&i._encode(e,t);e.push(y)};var R=i;function F(e){let n=2;for(let t of e)n+=h(t);return n}s(F,"listLength");function G(e){let n=2;for(let[t,o]of e){let c=a(t).byteLength;n+=l(c)+1+c,n+=h(o)}return n}s(G,"mapLength");function V(e){let n=2,t=Object.keys(e);for(let o=0;o<t.length;o++){let c=a(t[o]).byteLength;n+=l(c)+1+c,n+=h(e[t[o]])}return n}s(V,"objectLength");function q(e){let n=a(e).byteLength;return l(n)+1+n}s(q,"stringLength");function Y(e){let n=e.byteLength-e.byteOffset;return l(n)+1+n}s(Y,"arrayBufferLength");function h(e){if(e==null)return 0;let t=d(e);switch(t){case"arraybufferview":return Y(e);case"string":return q(e);case"array":case"set":return F(e);case"number":return 1+l(Math.floor(e))+1;case"bigint":return 1+e.toString().length+1;case"object":return V(e);case"map":return G(e);default:throw new TypeError(`Unsupported value of type "${t}"`)}}s(h,"encodingLength");var x=h;var H=105,W=58,X=100,J=108,w=101,r=s(function e(n,t,o,c){if(!n||n.length===0)throw new Error("Missing data to decode.");return typeof t!="number"&&c==null&&(c=t,t=void 0),typeof o!="number"&&c==null&&(c=o,o=void 0),e.position=0,e.encoding=c||null,e.data=ArrayBuffer.isView(n)?new Uint8Array(n.slice(t,o)):a(n),e.bytes=e.data.length,e.next()},"decode");r.bytes=0;r.position=0;r.data=null;r.encoding=null;r.next=function(){switch(r.data[r.position]){case X:return r.dictionary();case J:return r.list();case H:return r.integer();default:return r.buffer()}};r.find=function(e){if(!r.data?.length)return null;let n=r.position,t=r.data.length,o=r.data;for(;n<t;){if(o[n]===e)return n;n++}throw new Error('Invalid data: Missing delimiter "'+String.fromCharCode(e)+'" [0x'+e.toString(16)+"]")};r.dictionary=function(){if(!r.data)return null;r.position++;let e={};for(;r.data[r.position]!==w;){let n=r.buffer();if(typeof n=="string"){e[n]=r.next();continue}let t=m(n);t.includes("\uFFFD")&&(t=b(n)),e[t]=r.next()}return r.position++,e};r.list=function(){r.position++;let e=[];for(;r.data[r.position]!==w;)e.push(r.next());return r.position++,e};r.integer=function(){let e=r.find(w),n=M(r.data,r.position+1,e);if(!e)throw new Error("not end");return r.position+=e+1-r.position,n};r.buffer=function(){let e=r.find(W),n=(e||0)+1,t=M(r.data,r.position,e),o=n+t;return r.position=o,r.encoding?m(r.data.slice(n,o)):r.data.slice(n,o)};var B=r;function M(e,n,t){let o=0,c=1;for(let u=n;u<t;u++){let f=e[u];if(f<58&&f>=48){o=o*10+(f-48);continue}if(!(u===n&&f===43)){if(u===n&&f===45){c=-1;continue}if(f===46)break;throw new Error("not a number: buffer["+u+"] = "+f)}}return o*c}s(M,"getIntFromBuffer");var K=x,Te={encode:R,decode:B,byteLength:x,encodingLength:K};export{Te as default,i as encode};
/*! Bundled license information:
@substrate-system/uint8-util/dist/util.js:
(* Common package for dealing with hex/string/uint8 conversions (and sha1 hashing)
*
* @author Jimmy Wärting <jimmy@warting.se> (https://jimmy.warting.se/opensource)
* @license MIT
*)
*/
//# sourceMappingURL=index.min.js.map