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