UNPKG

multiformats

Version:

Interface for multihash, multicodec, multibase and CID

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