4 lines
13 KiB
JavaScript
4 lines
13 KiB
JavaScript
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.Multiformats = factory()}(typeof self !== 'undefined' ? self : this, function () {
|
|
"use strict";var Multiformats=(()=>{var P=Object.defineProperty;var ue=Object.getOwnPropertyDescriptor;var le=Object.getOwnPropertyNames;var be=Object.prototype.hasOwnProperty;var L=(r,t)=>{for(var e in t)P(r,e,{get:t[e],enumerable:!0})},xe=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of le(t))!be.call(r,o)&&o!==e&&P(r,o,{get:()=>t[o],enumerable:!(n=ue(t,o))||n.enumerable});return r};var we=r=>xe(P({},"__esModule",{value:!0}),r);var tt={};L(tt,{CID:()=>F,bytes:()=>G,digest:()=>V,hasher:()=>ee,varint:()=>O});var G={};L(G,{coerce:()=>D,empty:()=>te,equals:()=>j,fromHex:()=>me,fromString:()=>ve,isBinary:()=>ye,toHex:()=>ge,toString:()=>Ee});var te=new Uint8Array(0);function ge(r){return r.reduce((t,e)=>t+e.toString(16).padStart(2,"0"),"")}function me(r){let t=r.match(/../g);return t!=null?new Uint8Array(t.map(e=>parseInt(e,16))):te}function j(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 D(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 ye(r){return r instanceof ArrayBuffer||ArrayBuffer.isView(r)}function ve(r){return new TextEncoder().encode(r)}function Ee(r){return new TextDecoder().decode(r)}function De(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 i=r.charAt(o),s=i.charCodeAt(0);if(e[s]!==255)throw new TypeError(i+" is ambiguous");e[s]=o}var c=r.length,d=r.charAt(0),y=Math.log(c)/Math.log(256),I=Math.log(256)/Math.log(c);function J(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 h=0,E=0,p=0,l=a.length;p!==l&&a[p]===0;)p++,h++;for(var b=(l-p)*I+1>>>0,f=new Uint8Array(b);p!==l;){for(var x=a[p],v=0,u=b-1;(x!==0||v<E)&&u!==-1;u--,v++)x+=256*f[u]>>>0,f[u]=x%c>>>0,x=x/c>>>0;if(x!==0)throw new Error("Non-zero carry");E=v,p++}for(var g=b-E;g!==b&&f[g]===0;)g++;for(var q=d.repeat(h);g<b;++g)q+=r.charAt(f[g]);return q}function B(a){if(typeof a!="string")throw new TypeError("Expected String");if(a.length===0)return new Uint8Array;var h=0;if(a[h]!==" "){for(var E=0,p=0;a[h]===d;)E++,h++;for(var l=(a.length-h)*y+1>>>0,b=new Uint8Array(l);a[h];){var f=e[a.charCodeAt(h)];if(f===255)return;for(var x=0,v=l-1;(f!==0||x<p)&&v!==-1;v--,x++)f+=c*b[v]>>>0,b[v]=f%256>>>0,f=f/256>>>0;if(f!==0)throw new Error("Non-zero carry");p=x,h++}if(a[h]!==" "){for(var u=l-p;u!==l&&b[u]===0;)u++;for(var g=new Uint8Array(E+(l-u)),q=E;u!==l;)g[q++]=b[u++];return g}}}function pe(a){var h=B(a);if(h)return h;throw new Error(`Non-${t} character`)}return{encode:J,decodeUnsafe:B,decode:pe}}var Ce=De,Se=Ce,re=Se;var Q=class{name;prefix;baseEncode;constructor(t,e,n){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")}},X=class{name;prefix;baseDecode;prefixCodePoint;constructor(t,e,n){this.name=t,this.prefix=e;let 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 ne(this,t)}},K=class{decoders;constructor(t){this.decoders=t}or(t){return ne(this,t)}decode(t){let 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 ne(r,t){return new K({...r.decoders??{[r.prefix]:r},...t.decoders??{[t.prefix]:t}})}var W=class{name;prefix;baseEncode;baseDecode;encoder;decoder;constructor(t,e,n,o){this.name=t,this.prefix=e,this.baseEncode=n,this.baseDecode=o,this.encoder=new Q(t,e,n),this.decoder=new X(t,e,o)}encode(t){return this.encoder.encode(t)}decode(t){return this.decoder.decode(t)}};function oe({name:r,prefix:t,encode:e,decode:n}){return new W(r,t,e,n)}function U({name:r,prefix:t,alphabet:e}){let{encode:n,decode:o}=re(e,r);return oe({prefix:t,name:r,encode:n,decode:i=>D(o(i))})}function Ae(r,t,e,n){let o=r.length;for(;r[o-1]==="=";)--o;let i=new Uint8Array(o*e/8|0),s=0,c=0,d=0;for(let y=0;y<o;++y){let I=t[r[y]];if(I===void 0)throw new SyntaxError(`Non-${n} character`);c=c<<e|I,s+=e,s>=8&&(s-=8,i[d++]=255&c>>s)}if(s>=e||(255&c<<8-s)!==0)throw new SyntaxError("Unexpected end of data");return i}function Ie(r,t,e){let n=t[t.length-1]==="=",o=(1<<e)-1,i="",s=0,c=0;for(let d=0;d<r.length;++d)for(c=c<<8|r[d],s+=8;s>e;)s-=e,i+=t[o&c>>s];if(s!==0&&(i+=t[o&c<<e-s]),n)for(;(i.length*e&7)!==0;)i+="=";return i}function Ue(r){let t={};for(let e=0;e<r.length;++e)t[r[e]]=e;return t}function w({name:r,prefix:t,bitsPerChar:e,alphabet:n}){let o=Ue(n);return oe({prefix:t,name:r,encode(i){return Ie(i,n,e)},decode(i){return Ae(i,o,e,r)}})}var M=w({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),ct=w({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),ht=w({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),ft=w({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),dt=w({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),pt=w({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),ut=w({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),lt=w({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),bt=w({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var k=U({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),gt=U({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var m=U({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),vt=U({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var V={};L(V,{Digest:()=>A,create:()=>T,decode:()=>H,equals:()=>Y,hasCode:()=>Xe});var O={};L(O,{decode:()=>z,encodeTo:()=>C,encodingLength:()=>S});var ze=ae,ie=128,Le=127,Me=~Le,Ne=Math.pow(2,31);function ae(r,t,e){t=t||[],e=e||0;for(var n=e;r>=Ne;)t[e++]=r&255|ie,r/=128;for(;r&Me;)t[e++]=r&255|ie,r>>>=7;return t[e]=r|0,ae.bytes=e-n+1,t}var Oe=Z,Te=128,se=127;function Z(r,n){var e=0,n=n||0,o=0,i=n,s,c=r.length;do{if(i>=c)throw Z.bytes=0,new RangeError("Could not decode varint");s=r[i++],e+=o<28?(s&se)<<o:(s&se)*Math.pow(2,o),o+=7}while(s>=Te);return Z.bytes=i-n,e}var Ve=Math.pow(2,7),$e=Math.pow(2,14),Be=Math.pow(2,21),qe=Math.pow(2,28),ke=Math.pow(2,35),Fe=Math.pow(2,42),Re=Math.pow(2,49),Je=Math.pow(2,56),Pe=Math.pow(2,63),je=function(r){return r<Ve?1:r<$e?2:r<Be?3:r<qe?4:r<ke?5:r<Fe?6:r<Re?7:r<Je?8:r<Pe?9:10},Ge={encode:ze,decode:Oe,encodingLength:je},Qe=Ge,N=Qe;function z(r,t=0){return[N.decode(r,t),N.decode.bytes]}function C(r,t,e=0){return N.encode(r,t,e),t}function S(r){return N.encodingLength(r)}function T(r,t){let e=t.byteLength,n=S(r),o=n+S(e),i=new Uint8Array(o+e);return C(r,i,0),C(e,i,n),i.set(t,o),new A(r,e,t,i)}function H(r){let t=D(r),[e,n]=z(t),[o,i]=z(t.subarray(n)),s=t.subarray(n+i);if(s.byteLength!==o)throw new Error("Incorrect length");return new A(e,o,s,t)}function Y(r,t){if(r===t)return!0;{let e=t;return r.code===e.code&&r.size===e.size&&e.bytes instanceof Uint8Array&&j(r.bytes,e.bytes)}}var A=class{code;size;digest;bytes;constructor(t,e,n,o){this.code=t,this.size=e,this.digest=n,this.bytes=o}};function Xe(r,t){return r.code===t}function ce(r,t){let{bytes:e,version:n}=r;switch(n){case 0:return We(e,_(r),t??m.encoder);default:return Ze(e,_(r),t??M.encoder)}}var he=new WeakMap;function _(r){let t=he.get(r);if(t==null){let e=new Map;return he.set(r,e),e}return t}var F=class r{code;version;multihash;bytes;"/";constructor(t,e,n,o){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:{let{code:t,multihash:e}=this;if(t!==$)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(e.code!==He)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return r.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:{let{code:t,digest:e}=this.multihash,n=T(t,e);return r.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 r.equals(this,t)}static equals(t,e){let n=e;return n!=null&&t.code===n.code&&t.version===n.version&&Y(t.multihash,n.multihash)}toString(t){return ce(this,t)}toJSON(){return{"/":ce(this)}}link(){return this}[Symbol.toStringTag]="CID";[Symbol.for("nodejs.util.inspect.custom")](){return`CID(${this.toString()})`}static asCID(t){if(t==null)return null;let e=t;if(e instanceof r)return e;if(e["/"]!=null&&e["/"]===e.bytes||e.asCID===e){let{version:n,code:o,multihash:i,bytes:s}=e;return new r(n,o,i,s??fe(n,o,i.bytes))}else if(e[Ye]===!0){let{version:n,multihash:o,code:i}=e,s=H(o);return r.create(n,i,s)}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!==$)throw new Error(`Version 0 CID must use dag-pb (code: ${$}) block encoding`);return new r(t,e,n,n.bytes)}case 1:{let o=fe(t,e,n.bytes);return new r(t,e,n,o)}default:throw new Error("Invalid version")}}static createV0(t){return r.create(0,$,t)}static createV1(t,e){return r.create(1,t,e)}static decode(t){let[e,n]=r.decodeFirst(t);if(n.length!==0)throw new Error("Incorrect length");return e}static decodeFirst(t){let e=r.inspectBytes(t),n=e.size-e.multihashSize,o=D(t.subarray(n,n+e.multihashSize));if(o.byteLength!==e.multihashSize)throw new Error("Incorrect length");let i=o.subarray(e.multihashSize-e.digestSize),s=new A(e.multihashCode,e.digestSize,i,o);return[e.version===0?r.createV0(s):r.createV1(e.codec,s),t.subarray(e.size)]}static inspectBytes(t){let e=0,n=()=>{let[J,B]=z(t.subarray(e));return e+=B,J},o=n(),i=$;if(o===18?(o=0,e=0):i=n(),o!==0&&o!==1)throw new RangeError(`Invalid CID version ${o}`);let s=e,c=n(),d=n(),y=e+d,I=y-s;return{version:o,codec:i,multihashCode:c,digestSize:d,multihashSize:I,size:y}}static parse(t,e){let[n,o]=Ke(t,e),i=r.decode(o);if(i.version===0&&t[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return _(i).set(n,t),i}};function Ke(r,t){switch(r[0]){case"Q":{let e=t??m;return[m.prefix,e.decode(`${m.prefix}${r}`)]}case m.prefix:{let e=t??m;return[m.prefix,e.decode(r)]}case M.prefix:{let e=t??M;return[M.prefix,e.decode(r)]}case k.prefix:{let e=t??k;return[k.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 We(r,t,e){let{prefix:n}=e;if(n!==m.prefix)throw Error(`Cannot string encode V0 in ${e.name} encoding`);let o=t.get(n);if(o==null){let i=e.encode(r).slice(1);return t.set(n,i),i}else return o}function Ze(r,t,e){let{prefix:n}=e,o=t.get(n);if(o==null){let i=e.encode(r);return t.set(n,i),i}else return o}var $=112,He=18;function fe(r,t,e){let n=S(r),o=n+S(t),i=new Uint8Array(o+e.byteLength);return C(r,i,0),C(t,i,n),i.set(e,o),i}var Ye=Symbol.for("@ipld/js-cid/CID");var ee={};L(ee,{Hasher:()=>R,from:()=>et});var _e=20;function et({name:r,code:t,encode:e,minDigestLength:n,maxDigestLength:o}){return new R(r,t,e,n,o)}var R=class{name;code;encode;minDigestLength;maxDigestLength;constructor(t,e,n,o,i){this.name=t,this.code=e,this.encode=n,this.minDigestLength=o??_e,this.maxDigestLength=i}digest(t,e){if(e?.truncate!=null){if(e.truncate<this.minDigestLength)throw new Error(`Invalid truncate option, must be greater than or equal to ${this.minDigestLength}`);if(this.maxDigestLength!=null&&e.truncate>this.maxDigestLength)throw new Error(`Invalid truncate option, must be less than or equal to ${this.maxDigestLength}`)}if(t instanceof Uint8Array){let n=this.encode(t);return n instanceof Uint8Array?de(n,this.code,e?.truncate):n.then(o=>de(o,this.code,e?.truncate))}else throw Error("Unknown type, must be binary type")}};function de(r,t,e){if(e!=null&&e!==r.byteLength){if(e>r.byteLength)throw new Error(`Invalid truncate option, must be less than or equal to ${r.byteLength}`);r=r.subarray(0,e)}return T(t,r)}return we(tt);})();
|
|
return Multiformats}));
|
|
//# sourceMappingURL=index.min.js.map
|