3 lines
12 KiB
JavaScript
3 lines
12 KiB
JavaScript
var Z=Object.defineProperty;var Y=(r,t,e)=>t in r?Z(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var c=(r,t,e)=>Y(r,typeof t!="symbol"?t+"":t,e);const H=new Uint8Array(0);function Qe(r){const t=r.match(/../g);return t!=null?new Uint8Array(t.map(e=>parseInt(e,16))):H}function ee(r,t){if(r===t)return!0;if(r.byteLength!==t.byteLength)return!1;for(let e=0;e<r.byteLength;e++)if(r[e]!==t[e])return!1;return!0}function F(r){if(r instanceof Uint8Array&&r.constructor.name==="Uint8Array")return r;if(r instanceof ArrayBuffer)return new Uint8Array(r);if(ArrayBuffer.isView(r))return new Uint8Array(r.buffer,r.byteOffset,r.byteLength);throw new Error("Unknown type, must be binary type")}function Ge(r){return new TextEncoder().encode(r)}function Ke(r){return new TextDecoder().decode(r)}function te(r,t){if(r.length>=255)throw new TypeError("Alphabet too long");for(var e=new Uint8Array(256),n=0;n<e.length;n++)e[n]=255;for(var o=0;o<r.length;o++){var s=r.charAt(o),i=s.charCodeAt(0);if(e[i]!==255)throw new TypeError(s+" is ambiguous");e[i]=o}var d=r.length,l=r.charAt(0),S=Math.log(d)/Math.log(256),z=Math.log(256)/Math.log(d);function I(a){if(a instanceof Uint8Array||(ArrayBuffer.isView(a)?a=new Uint8Array(a.buffer,a.byteOffset,a.byteLength):Array.isArray(a)&&(a=Uint8Array.from(a))),!(a instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(a.length===0)return"";for(var f=0,A=0,p=0,w=a.length;p!==w&&a[p]===0;)p++,f++;for(var y=(w-p)*z+1>>>0,u=new Uint8Array(y);p!==w;){for(var g=a[p],E=0,b=y-1;(g!==0||E<A)&&b!==-1;b--,E++)g+=256*u[b]>>>0,u[b]=g%d>>>0,g=g/d>>>0;if(g!==0)throw new Error("Non-zero carry");A=E,p++}for(var v=y-A;v!==y&&u[v]===0;)v++;for(var O=l.repeat(f);v<y;++v)O+=r.charAt(u[v]);return O}function M(a){if(typeof a!="string")throw new TypeError("Expected String");if(a.length===0)return new Uint8Array;var f=0;if(a[f]!==" "){for(var A=0,p=0;a[f]===l;)A++,f++;for(var w=(a.length-f)*S+1>>>0,y=new Uint8Array(w);a[f];){var u=e[a.charCodeAt(f)];if(u===255)return;for(var g=0,E=w-1;(u!==0||g<p)&&E!==-1;E--,g++)u+=d*y[E]>>>0,y[E]=u%256>>>0,u=u/256>>>0;if(u!==0)throw new Error("Non-zero carry");p=g,f++}if(a[f]!==" "){for(var b=w-p;b!==w&&y[b]===0;)b++;for(var v=new Uint8Array(A+(w-b)),O=A;b!==w;)v[O++]=y[b++];return v}}}function W(a){var f=M(a);if(f)return f;throw new Error(`Non-${t} character`)}return{encode:I,decodeUnsafe:M,decode:W}}var re=te,ne=re;class oe{constructor(t,e,n){c(this,"name");c(this,"prefix");c(this,"baseEncode");this.name=t,this.prefix=e,this.baseEncode=n}encode(t){if(t instanceof Uint8Array)return`${this.prefix}${this.baseEncode(t)}`;throw Error("Unknown type, must be binary type")}}class se{constructor(t,e,n){c(this,"name");c(this,"prefix");c(this,"baseDecode");c(this,"prefixCodePoint");this.name=t,this.prefix=e;const o=e.codePointAt(0);if(o===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=o,this.baseDecode=n}decode(t){if(typeof t=="string"){if(t.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(t)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(t.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(t){return G(this,t)}}class ie{constructor(t){c(this,"decoders");this.decoders=t}or(t){return G(this,t)}decode(t){const e=t[0],n=this.decoders[e];if(n!=null)return n.decode(t);throw RangeError(`Unable to decode multibase string ${JSON.stringify(t)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}}function G(r,t){return new ie({...r.decoders??{[r.prefix]:r},...t.decoders??{[t.prefix]:t}})}class ae{constructor(t,e,n,o){c(this,"name");c(this,"prefix");c(this,"baseEncode");c(this,"baseDecode");c(this,"encoder");c(this,"decoder");this.name=t,this.prefix=e,this.baseEncode=n,this.baseDecode=o,this.encoder=new oe(t,e,n),this.decoder=new se(t,e,o)}encode(t){return this.encoder.encode(t)}decode(t){return this.decoder.decode(t)}}function K({name:r,prefix:t,encode:e,decode:n}){return new ae(r,t,e,n)}function T({name:r,prefix:t,alphabet:e}){const{encode:n,decode:o}=ne(e,r);return K({prefix:t,name:r,encode:n,decode:s=>F(o(s))})}function ce(r,t,e,n){let o=r.length;for(;r[o-1]==="=";)--o;const s=new Uint8Array(o*e/8|0);let i=0,d=0,l=0;for(let S=0;S<o;++S){const z=t[r[S]];if(z===void 0)throw new SyntaxError(`Non-${n} character`);d=d<<e|z,i+=e,i>=8&&(i-=8,s[l++]=255&d>>i)}if(i>=e||255&d<<8-i)throw new SyntaxError("Unexpected end of data");return s}function de(r,t,e){const n=t[t.length-1]==="=",o=(1<<e)-1;let s="",i=0,d=0;for(let l=0;l<r.length;++l)for(d=d<<8|r[l],i+=8;i>e;)i-=e,s+=t[o&d>>i];if(i!==0&&(s+=t[o&d<<e-i]),n)for(;s.length*e&7;)s+="=";return s}function he(r){const t={};for(let e=0;e<r.length;++e)t[r[e]]=e;return t}function m({name:r,prefix:t,bitsPerChar:e,alphabet:n}){const o=he(n);return K({prefix:t,name:r,encode(s){return de(s,n,e)},decode(s){return ce(s,o,e,r)}})}const U=m({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),fe=m({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),ue=m({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),le=m({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),pe=m({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),be=m({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),we=m({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),ye=m({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),ge=m({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5}),Xe=Object.freeze(Object.defineProperty({__proto__:null,base32:U,base32hex:pe,base32hexpad:we,base32hexpadupper:ye,base32hexupper:be,base32pad:ue,base32padupper:le,base32upper:fe,base32z:ge},Symbol.toStringTag,{value:"Module"})),$=T({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),ve=T({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"}),We=Object.freeze(Object.defineProperty({__proto__:null,base36:$,base36upper:ve},Symbol.toStringTag,{value:"Module"})),x=T({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),xe=T({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"}),Ze=Object.freeze(Object.defineProperty({__proto__:null,base58btc:x,base58flickr:xe},Symbol.toStringTag,{value:"Module"}));var me=X,R=128,Se=-128,Ee=Math.pow(2,31);function X(r,t,e){t=t||[],e=e||0;for(var n=e;r>=Ee;)t[e++]=r&255|R,r/=128;for(;r&Se;)t[e++]=r&255|R,r>>>=7;return t[e]=r|0,X.bytes=e-n+1,t}var Ae=j,ze=128,_=127;function j(r,n){var e=0,n=n||0,o=0,s=n,i,d=r.length;do{if(s>=d)throw j.bytes=0,new RangeError("Could not decode varint");i=r[s++],e+=o<28?(i&_)<<o:(i&_)*Math.pow(2,o),o+=7}while(i>=ze);return j.bytes=s-n,e}var Ce=Math.pow(2,7),Ue=Math.pow(2,14),Me=Math.pow(2,21),Oe=Math.pow(2,28),$e=Math.pow(2,35),Ne=Math.pow(2,42),De=Math.pow(2,49),Ve=Math.pow(2,56),Te=Math.pow(2,63),Ie=function(r){return r<Ce?1:r<Ue?2:r<Me?3:r<Oe?4:r<$e?5:r<Ne?6:r<De?7:r<Ve?8:r<Te?9:10},je={encode:me,decode:Ae,encodingLength:Ie},N=je;function L(r,t=0){return[N.decode(r,t),N.decode.bytes]}function D(r,t,e=0){return N.encode(r,t,e),t}function V(r){return N.encodingLength(r)}function Le(r,t){const e=t.byteLength,n=V(r),o=n+V(e),s=new Uint8Array(o+e);return D(r,s,0),D(e,s,n),s.set(t,o),new P(r,e,t,s)}function ke(r){const t=F(r),[e,n]=L(t),[o,s]=L(t.subarray(n)),i=t.subarray(n+s);if(i.byteLength!==o)throw new Error("Incorrect length");return new P(e,o,i,t)}function Be(r,t){if(r===t)return!0;{const e=t;return r.code===e.code&&r.size===e.size&&e.bytes instanceof Uint8Array&&ee(r.bytes,e.bytes)}}class P{constructor(t,e,n,o){c(this,"code");c(this,"size");c(this,"digest");c(this,"bytes");this.code=t,this.size=e,this.digest=n,this.bytes=o}}function k(r,t){const{bytes:e,version:n}=r;switch(n){case 0:return Pe(e,B(r),t??x.encoder);default:return Re(e,B(r),t??U.encoder)}}const q=new WeakMap;function B(r){const t=q.get(r);if(t==null){const e=new Map;return q.set(r,e),e}return t}var Q;class h{constructor(t,e,n,o){c(this,"code");c(this,"version");c(this,"multihash");c(this,"bytes");c(this,"/");c(this,Q,"CID");this.code=e,this.version=t,this.multihash=n,this.bytes=o,this["/"]=o}get asCID(){return this}get byteOffset(){return this.bytes.byteOffset}get byteLength(){return this.bytes.byteLength}toV0(){switch(this.version){case 0:return this;case 1:{const{code:t,multihash:e}=this;if(t!==C)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(e.code!==_e)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return h.createV0(e)}default:throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`)}}toV1(){switch(this.version){case 0:{const{code:t,digest:e}=this.multihash,n=Le(t,e);return h.createV1(this.code,n)}case 1:return this;default:throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`)}}equals(t){return h.equals(this,t)}static equals(t,e){const n=e;return n!=null&&t.code===n.code&&t.version===n.version&&Be(t.multihash,n.multihash)}toString(t){return k(this,t)}toJSON(){return{"/":k(this)}}link(){return this}[(Q=Symbol.toStringTag,Symbol.for("nodejs.util.inspect.custom"))](){return`CID(${this.toString()})`}static asCID(t){if(t==null)return null;const e=t;if(e instanceof h)return e;if(e["/"]!=null&&e["/"]===e.bytes||e.asCID===e){const{version:n,code:o,multihash:s,bytes:i}=e;return new h(n,o,s,i??J(n,o,s.bytes))}else if(e[qe]===!0){const{version:n,multihash:o,code:s}=e,i=ke(o);return h.create(n,s,i)}else return null}static create(t,e,n){if(typeof e!="number")throw new Error("String codecs are no longer supported");if(!(n.bytes instanceof Uint8Array))throw new Error("Invalid digest");switch(t){case 0:{if(e!==C)throw new Error(`Version 0 CID must use dag-pb (code: ${C}) block encoding`);return new h(t,e,n,n.bytes)}case 1:{const o=J(t,e,n.bytes);return new h(t,e,n,o)}default:throw new Error("Invalid version")}}static createV0(t){return h.create(0,C,t)}static createV1(t,e){return h.create(1,t,e)}static decode(t){const[e,n]=h.decodeFirst(t);if(n.length!==0)throw new Error("Incorrect length");return e}static decodeFirst(t){const e=h.inspectBytes(t),n=e.size-e.multihashSize,o=F(t.subarray(n,n+e.multihashSize));if(o.byteLength!==e.multihashSize)throw new Error("Incorrect length");const s=o.subarray(e.multihashSize-e.digestSize),i=new P(e.multihashCode,e.digestSize,s,o);return[e.version===0?h.createV0(i):h.createV1(e.codec,i),t.subarray(e.size)]}static inspectBytes(t){let e=0;const n=()=>{const[I,M]=L(t.subarray(e));return e+=M,I};let o=n(),s=C;if(o===18?(o=0,e=0):s=n(),o!==0&&o!==1)throw new RangeError(`Invalid CID version ${o}`);const i=e,d=n(),l=n(),S=e+l,z=S-i;return{version:o,codec:s,multihashCode:d,digestSize:l,multihashSize:z,size:S}}static parse(t,e){const[n,o]=Fe(t,e),s=h.decode(o);if(s.version===0&&t[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return B(s).set(n,t),s}}function Fe(r,t){switch(r[0]){case"Q":{const e=t??x;return[x.prefix,e.decode(`${x.prefix}${r}`)]}case x.prefix:{const e=t??x;return[x.prefix,e.decode(r)]}case U.prefix:{const e=t??U;return[U.prefix,e.decode(r)]}case $.prefix:{const e=t??$;return[$.prefix,e.decode(r)]}default:{if(t==null)throw Error("To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided");return[r[0],t.decode(r)]}}}function Pe(r,t,e){const{prefix:n}=e;if(n!==x.prefix)throw Error(`Cannot string encode V0 in ${e.name} encoding`);const o=t.get(n);if(o==null){const s=e.encode(r).slice(1);return t.set(n,s),s}else return o}function Re(r,t,e){const{prefix:n}=e,o=t.get(n);if(o==null){const s=e.encode(r);return t.set(n,s),s}else return o}const C=112,_e=18;function J(r,t,e){const n=V(r),o=n+V(t),s=new Uint8Array(o+e.byteLength);return D(r,s,0),D(t,s,n),s.set(e,o),s}const qe=Symbol.for("@ipld/js-cid/CID"),Ye=Object.freeze(Object.defineProperty({__proto__:null,CID:h,format:k},Symbol.toStringTag,{value:"Module"}));export{h as C,$ as a,x as b,Le as c,ke as d,U as e,Qe as f,T as g,K as h,Ge as i,F as j,Ze as k,We as l,Xe as m,Ye as n,m as r,Ke as t};
|
|
//# sourceMappingURL=cid-DnxG1pfC.js.map
|