UNPKG

@substrate-system/bencode

Version:
12 lines (10 loc) 3.21 kB
var m=Object.defineProperty;var s=(e,r)=>m(e,"name",{value:r,configurable:!0});var _=Object.defineProperty,C=s((e,r)=>_(e,"name",{value:r,configurable:!0}),"__name");var u="0123456789abcdef",U=[],y=[];for(let e=0;e<256;e++)U[e]=u[e>>4&15]+u[e&15],e<16&&(e<10?y[48+e]=e:y[87+e]=e);var p=C((e,r=0)=>{let t=e.length||0;if(!r){let h=t;for(;h--;)r+=e[h].length}let o=new Uint8Array(r),i=r,c=t;for(;c--;)i-=e[c].length,o.set(e[c],i);return o},"concat");var g="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",k=typeof Uint8Array>"u"?[]:new Uint8Array(256);for(f=0;f<g.length;f++)k[g.charCodeAt(f)]=f;var f;var v=Object.defineProperty,B=s((e,r)=>v(e,"name",{value:r,configurable:!0}),"__name"),I=new TextDecoder;var T=new TextEncoder,l=B(e=>T.encode(e),"text2arr");var d=typeof window<"u"?window:globalThis,b=d.crypto||d.msCrypto||{},R=b.subtle||b.webkitSubtle;function w(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(w,"getType");function n(e,r,t){let o=[];n._encode(o,e);let i=p(o);return n.bytes=i.length,ArrayBuffer.isView(r)?(r.set(i,t),r):i}s(n,"encode");n.bytes=-1;n._floatConversionDetected=!1;n._encode=function(e,r){if(r!=null)switch(w(r)){case"object":n.dict(e,r);break;case"map":n.dictMap(e,r);break;case"array":n.list(e,r);break;case"set":n.listSet(e,r);break;case"string":n.string(e,r);break;case"number":n.number(e,r);break;case"boolean":n.number(e,r);break;case"arraybufferview":n.buffer(e,new Uint8Array(r.buffer,r.byteOffset,r.byteLength));break;case"arraybuffer":n.buffer(e,new Uint8Array(r));break}};var a=new Uint8Array([101]),A=new Uint8Array([100]),x=new Uint8Array([108]);n.buffer=function(e,r){e.push(l(r.length+":"),r)};n.string=function(e,r){e.push(l(l(r).byteLength+":"+r))};n.number=function(e,r){if(Number.isInteger(r))return e.push(l("i"+BigInt(r)+"e"));let t=2147483648,o=r/t<<0,i=r%t<<0,c=o*t+i;e.push(l("i"+c+"e")),c!==r&&!n._floatConversionDetected&&(n._floatConversionDetected=!0,console.warn('WARNING: Possible data corruption detected with value "'+r+'":','Bencoding only defines support for integers, value was converted to "'+c+'"'),console.trace())};n.dict=function(e,r){e.push(A);let t=0,o,i=Object.keys(r).sort(),c=i.length;for(;t<c;t++)o=i[t],r[o]!=null&&(n.string(e,o),n._encode(e,r[o]));e.push(a)};n.dictMap=function(e,r){e.push(A);let t=Array.from(r.keys()).sort();for(let o of t)r.get(o)!=null&&(ArrayBuffer.isView(o)?n._encode(e,o):n.string(e,String(o)),n._encode(e,r.get(o)));e.push(a)};n.list=function(e,r){let t=0,o=r.length;for(e.push(x);t<o;t++)r[t]!=null&&n._encode(e,r[t]);e.push(a)};n.listSet=function(e,r){e.push(x);for(let t of r)t!=null&&n._encode(e,t);e.push(a)};var Y=n;export{Y as default,n 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=encode.min.js.map