multiformats
Advanced tools
| (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 pe=Object.getOwnPropertyDescriptor;var ue=Object.getOwnPropertyNames;var le=Object.prototype.hasOwnProperty;var N=(r,e)=>{for(var t in e)P(r,t,{get:e[t],enumerable:!0})},be=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of ue(e))!le.call(r,o)&&o!==t&&P(r,o,{get:()=>e[o],enumerable:!(n=pe(e,o))||n.enumerable});return r};var xe=r=>be(P({},"__esModule",{value:!0}),r);var _e={};N(_e,{CID:()=>q,bytes:()=>Q,digest:()=>T,hasher:()=>ee,varint:()=>L});var Q={};N(Q,{coerce:()=>E,empty:()=>te,equals:()=>j,fromHex:()=>me,fromString:()=>ye,isBinary:()=>ge,toHex:()=>we,toString:()=>ve});var te=new Uint8Array(0);function we(r){return r.reduce((e,t)=>e+t.toString(16).padStart(2,"0"),"")}function me(r){let e=r.match(/../g);return e!=null?new Uint8Array(e.map(t=>parseInt(t,16))):te}function j(r,e){if(r===e)return!0;if(r.byteLength!==e.byteLength)return!1;for(let t=0;t<r.byteLength;t++)if(r[t]!==e[t])return!1;return!0}function E(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 r instanceof ArrayBuffer||ArrayBuffer.isView(r)}function ye(r){return new TextEncoder().encode(r)}function ve(r){return new TextDecoder().decode(r)}function Ce(r,e){if(r.length>=255)throw new TypeError("Alphabet too long");for(var t=new Uint8Array(256),n=0;n<t.length;n++)t[n]=255;for(var o=0;o<r.length;o++){var i=r.charAt(o),s=i.charCodeAt(0);if(t[s]!==255)throw new TypeError(i+" is ambiguous");t[s]=o}var c=r.length,h=r.charAt(0),y=Math.log(c)/Math.log(256),U=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 d=0,C=0,p=0,l=a.length;p!==l&&a[p]===0;)p++,d++;for(var b=(l-p)*U+1>>>0,f=new Uint8Array(b);p!==l;){for(var x=a[p],v=0,u=b-1;(x!==0||v<C)&&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");C=v,p++}for(var m=b-C;m!==b&&f[m]===0;)m++;for(var k=h.repeat(d);m<b;++m)k+=r.charAt(f[m]);return k}function $(a){if(typeof a!="string")throw new TypeError("Expected String");if(a.length===0)return new Uint8Array;var d=0;if(a[d]!==" "){for(var C=0,p=0;a[d]===h;)C++,d++;for(var l=(a.length-d)*y+1>>>0,b=new Uint8Array(l);a[d];){var f=t[a.charCodeAt(d)];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,d++}if(a[d]!==" "){for(var u=l-p;u!==l&&b[u]===0;)u++;for(var m=new Uint8Array(C+(l-u)),k=C;u!==l;)m[k++]=b[u++];return m}}}function he(a){var d=$(a);if(d)return d;throw new Error(`Non-${e} character`)}return{encode:J,decodeUnsafe:$,decode:he}}var Ee=Ce,Se=Ee,re=Se;var X=class{name;prefix;baseEncode;constructor(e,t,n){this.name=e,this.prefix=t,this.baseEncode=n}encode(e){if(e instanceof Uint8Array)return`${this.prefix}${this.baseEncode(e)}`;throw Error("Unknown type, must be binary type")}},G=class{name;prefix;baseDecode;prefixCodePoint;constructor(e,t,n){this.name=e,this.prefix=t;let o=t.codePointAt(0);if(o===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=o,this.baseDecode=n}decode(e){if(typeof e=="string"){if(e.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(e)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(e.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(e){return ne(this,e)}},K=class{decoders;constructor(e){this.decoders=e}or(e){return ne(this,e)}decode(e){let t=e[0],n=this.decoders[t];if(n!=null)return n.decode(e);throw RangeError(`Unable to decode multibase string ${JSON.stringify(e)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}};function ne(r,e){return new K({...r.decoders??{[r.prefix]:r},...e.decoders??{[e.prefix]:e}})}var W=class{name;prefix;baseEncode;baseDecode;encoder;decoder;constructor(e,t,n,o){this.name=e,this.prefix=t,this.baseEncode=n,this.baseDecode=o,this.encoder=new X(e,t,n),this.decoder=new G(e,t,o)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}};function oe({name:r,prefix:e,encode:t,decode:n}){return new W(r,e,t,n)}function z({name:r,prefix:e,alphabet:t}){let{encode:n,decode:o}=re(t,r);return oe({prefix:e,name:r,encode:n,decode:i=>E(o(i))})}function Ae(r,e,t,n){let o=r.length;for(;r[o-1]==="=";)--o;let i=new Uint8Array(o*t/8|0),s=0,c=0,h=0;for(let y=0;y<o;++y){let U=e[r[y]];if(U===void 0)throw new SyntaxError(`Non-${n} character`);c=c<<t|U,s+=t,s>=8&&(s-=8,i[h++]=255&c>>s)}if(s>=t||(255&c<<8-s)!==0)throw new SyntaxError("Unexpected end of data");return i}function De(r,e,t){let n=e[e.length-1]==="=",o=(1<<t)-1,i="",s=0,c=0;for(let h=0;h<r.length;++h)for(c=c<<8|r[h],s+=8;s>t;)s-=t,i+=e[o&c>>s];if(s!==0&&(i+=e[o&c<<t-s]),n)for(;(i.length*t&7)!==0;)i+="=";return i}function Ue(r){let e={};for(let t=0;t<r.length;++t)e[r[t]]=t;return e}function w({name:r,prefix:e,bitsPerChar:t,alphabet:n}){let o=Ue(n);return oe({prefix:e,name:r,encode(i){return De(i,n,t)},decode(i){return Ae(i,o,t,r)}})}var O=w({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),st=w({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),at=w({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),ct=w({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),dt=w({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),ft=w({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),ht=w({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),pt=w({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),ut=w({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var F=z({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),xt=z({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var g=z({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),gt=z({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var T={};N(T,{Digest:()=>D,create:()=>M,decode:()=>Y,equals:()=>H,hasCode:()=>Xe});var L={};N(L,{decode:()=>I,encodeTo:()=>S,encodingLength:()=>A});var ze=ae,ie=128,Ie=127,Me=~Ie,Ne=Math.pow(2,31);function ae(r,e,t){e=e||[],t=t||0;for(var n=t;r>=Ne;)e[t++]=r&255|ie,r/=128;for(;r&Me;)e[t++]=r&255|ie,r>>>=7;return e[t]=r|0,ae.bytes=t-n+1,e}var Oe=Z,Ve=128,se=127;function Z(r,n){var t=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++],t+=o<28?(s&se)<<o:(s&se)*Math.pow(2,o),o+=7}while(s>=Ve);return Z.bytes=i-n,t}var Le=Math.pow(2,7),Te=Math.pow(2,14),Be=Math.pow(2,21),$e=Math.pow(2,28),ke=Math.pow(2,35),Fe=Math.pow(2,42),qe=Math.pow(2,49),Re=Math.pow(2,56),Je=Math.pow(2,63),Pe=function(r){return r<Le?1:r<Te?2:r<Be?3:r<$e?4:r<ke?5:r<Fe?6:r<qe?7:r<Re?8:r<Je?9:10},je={encode:ze,decode:Oe,encodingLength:Pe},Qe=je,V=Qe;function I(r,e=0){return[V.decode(r,e),V.decode.bytes]}function S(r,e,t=0){return V.encode(r,e,t),e}function A(r){return V.encodingLength(r)}function M(r,e){let t=e.byteLength,n=A(r),o=n+A(t),i=new Uint8Array(o+t);return S(r,i,0),S(t,i,n),i.set(e,o),new D(r,t,e,i)}function Y(r){let e=E(r),[t,n]=I(e),[o,i]=I(e.subarray(n)),s=e.subarray(n+i);if(s.byteLength!==o)throw new Error("Incorrect length");return new D(t,o,s,e)}function H(r,e){if(r===e)return!0;{let t=e;return r.code===t.code&&r.size===t.size&&t.bytes instanceof Uint8Array&&j(r.bytes,t.bytes)}}var D=class{code;size;digest;bytes;constructor(e,t,n,o){this.code=e,this.size=t,this.digest=n,this.bytes=o}};function Xe(r,e){return r.code===e}function ce(r,e){let{bytes:t,version:n}=r;switch(n){case 0:return Ke(t,_(r),e??g.encoder);default:return We(t,_(r),e??O.encoder)}}var de=new WeakMap;function _(r){let e=de.get(r);if(e==null){let t=new Map;return de.set(r,t),t}return e}var q=class r{code;version;multihash;bytes;"/";constructor(e,t,n,o){this.code=t,this.version=e,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:e,multihash:t}=this;if(e!==B)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(t.code!==Ze)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return r.createV0(t)}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:e,digest:t}=this.multihash,n=M(e,t);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(e){return r.equals(this,e)}static equals(e,t){let n=t;return n!=null&&e.code===n.code&&e.version===n.version&&H(e.multihash,n.multihash)}toString(e){return ce(this,e)}toJSON(){return{"/":ce(this)}}link(){return this}[Symbol.toStringTag]="CID";[Symbol.for("nodejs.util.inspect.custom")](){return`CID(${this.toString()})`}static asCID(e){if(e==null)return null;let t=e;if(t instanceof r)return t;if(t["/"]!=null&&t["/"]===t.bytes||t.asCID===t){let{version:n,code:o,multihash:i,bytes:s}=t;return new r(n,o,i,s??fe(n,o,i.bytes))}else if(t[Ye]===!0){let{version:n,multihash:o,code:i}=t,s=Y(o);return r.create(n,i,s)}else return null}static create(e,t,n){if(typeof t!="number")throw new Error("String codecs are no longer supported");if(!(n.bytes instanceof Uint8Array))throw new Error("Invalid digest");switch(e){case 0:{if(t!==B)throw new Error(`Version 0 CID must use dag-pb (code: ${B}) block encoding`);return new r(e,t,n,n.bytes)}case 1:{let o=fe(e,t,n.bytes);return new r(e,t,n,o)}default:throw new Error("Invalid version")}}static createV0(e){return r.create(0,B,e)}static createV1(e,t){return r.create(1,e,t)}static decode(e){let[t,n]=r.decodeFirst(e);if(n.length!==0)throw new Error("Incorrect length");return t}static decodeFirst(e){let t=r.inspectBytes(e),n=t.size-t.multihashSize,o=E(e.subarray(n,n+t.multihashSize));if(o.byteLength!==t.multihashSize)throw new Error("Incorrect length");let i=o.subarray(t.multihashSize-t.digestSize),s=new D(t.multihashCode,t.digestSize,i,o);return[t.version===0?r.createV0(s):r.createV1(t.codec,s),e.subarray(t.size)]}static inspectBytes(e){let t=0,n=()=>{let[J,$]=I(e.subarray(t));return t+=$,J},o=n(),i=B;if(o===18?(o=0,t=0):i=n(),o!==0&&o!==1)throw new RangeError(`Invalid CID version ${o}`);let s=t,c=n(),h=n(),y=t+h,U=y-s;return{version:o,codec:i,multihashCode:c,digestSize:h,multihashSize:U,size:y}}static parse(e,t){let[n,o]=Ge(e,t),i=r.decode(o);if(i.version===0&&e[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return _(i).set(n,e),i}};function Ge(r,e){switch(r[0]){case"Q":{let t=e??g;return[g.prefix,t.decode(`${g.prefix}${r}`)]}case g.prefix:{let t=e??g;return[g.prefix,t.decode(r)]}case O.prefix:{let t=e??O;return[O.prefix,t.decode(r)]}case F.prefix:{let t=e??F;return[F.prefix,t.decode(r)]}default:{if(e==null)throw Error("To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided");return[r[0],e.decode(r)]}}}function Ke(r,e,t){let{prefix:n}=t;if(n!==g.prefix)throw Error(`Cannot string encode V0 in ${t.name} encoding`);let o=e.get(n);if(o==null){let i=t.encode(r).slice(1);return e.set(n,i),i}else return o}function We(r,e,t){let{prefix:n}=t,o=e.get(n);if(o==null){let i=t.encode(r);return e.set(n,i),i}else return o}var B=112,Ze=18;function fe(r,e,t){let n=A(r),o=n+A(e),i=new Uint8Array(o+t.byteLength);return S(r,i,0),S(e,i,n),i.set(t,o),i}var Ye=Symbol.for("@ipld/js-cid/CID");var ee={};N(ee,{Hasher:()=>R,from:()=>He});function He({name:r,code:e,encode:t}){return new R(r,e,t)}var R=class{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){let t=this.encode(e);return t instanceof Uint8Array?M(this.code,t):t.then(n=>M(this.code,n))}else throw Error("Unknown type, must be binary type")}};return xe(_e);})(); | ||
| "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 |
@@ -35,3 +35,3 @@ import * as json from './codecs/json.js'; | ||
| encode: (input: Uint8Array) => Uint8Array; | ||
| digest: (input: Uint8Array) => digest.Digest<0, number>; | ||
| digest: (input: Uint8Array, options?: hasher.DigestOptions) => digest.Digest<0, number>; | ||
| }; | ||
@@ -38,0 +38,0 @@ sha256: hasher.Hasher<"sha2-256", 18>; |
| import * as Digest from './digest.js'; | ||
| import type { MultihashHasher } from './interface.js'; | ||
| type Await<T> = Promise<T> | T; | ||
| export declare function from<Name extends string, Code extends number>({ name, code, encode }: { | ||
| export interface HasherInit<Name extends string, Code extends number> { | ||
| name: Name; | ||
| code: Code; | ||
| encode(input: Uint8Array): Await<Uint8Array>; | ||
| }): Hasher<Name, Code>; | ||
| /** | ||
| * The minimum length a hash is allowed to be truncated to in bytes | ||
| * | ||
| * @default 20 | ||
| */ | ||
| minDigestLength?: number; | ||
| /** | ||
| * The maximum length a hash is allowed to be truncated to in bytes. If not | ||
| * specified it will be inferred from the length of the digest. | ||
| */ | ||
| maxDigestLength?: number; | ||
| } | ||
| export declare function from<Name extends string, Code extends number>({ name, code, encode, minDigestLength, maxDigestLength }: HasherInit<Name, Code>): Hasher<Name, Code>; | ||
| export interface DigestOptions { | ||
| /** | ||
| * Truncate the returned digest to this number of bytes. | ||
| * | ||
| * This may cause the digest method to throw/reject if the passed value is | ||
| * greater than the digest length or below a threshold under which the risk of | ||
| * hash collisions is significant. | ||
| * | ||
| * The actual value of this threshold can depend on the hashing algorithm in | ||
| * use. | ||
| */ | ||
| truncate?: number; | ||
| } | ||
| /** | ||
@@ -17,6 +42,8 @@ * Hasher represents a hashing algorithm implementation that produces as | ||
| readonly encode: (input: Uint8Array) => Await<Uint8Array>; | ||
| constructor(name: Name, code: Code, encode: (input: Uint8Array) => Await<Uint8Array>); | ||
| digest(input: Uint8Array): Await<Digest.Digest<Code, number>>; | ||
| readonly minDigestLength: number; | ||
| readonly maxDigestLength?: number; | ||
| constructor(name: Name, code: Code, encode: (input: Uint8Array) => Await<Uint8Array>, minDigestLength?: number, maxDigestLength?: number); | ||
| digest(input: Uint8Array, options?: DigestOptions): Await<Digest.Digest<Code, number>>; | ||
| } | ||
| export {}; | ||
| //# sourceMappingURL=hasher.d.ts.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"hasher.d.ts","sourceRoot":"","sources":["../../../src/hashes/hasher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAErD,KAAK,KAAK,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AAE9B,wBAAgB,IAAI,CAAE,IAAI,SAAS,MAAM,EAAE,IAAI,SAAS,MAAM,EAAG,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,IAAI,CAAC;IAAC,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,KAAK,CAAC,UAAU,CAAC,CAAA;CAAE,GAAG,MAA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AAI,EAAE,MAAM,EAAE,eAAe,EAAE,eAAe,EAAE,EAAE,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAErK;AAED,MAAM,WAAW,aAAa;IAC5B;;;;;;;;;OASG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED;;;GAGG;AACH,qBAAa,MAAM,CAAC,IAAI,SAAS,MAAM,EAAE,IAAI,SAAS,MAAM,CAAE,YAAW,eAAe,CAAC,IAAI,CAAC;IAC5F,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAA;IACnB,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAA;IACnB,QAAQ,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,KAAK,CAAC,UAAU,CAAC,CAAA;IACzD,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAA;IAChC,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAA;gBAEpB,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,KAAK,CAAC,UAAU,CAAC,EAAE,eAAe,CAAC,EAAE,MAAM,EAAE,eAAe,CAAC,EAAE,MAAM;IAQzI,MAAM,CAAE,KAAK,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;CAwBxF"} |
| import * as Digest from './digest.js'; | ||
| export function from({ name, code, encode }) { | ||
| return new Hasher(name, code, encode); | ||
| const DEFAULT_MIN_DIGEST_LENGTH = 20; | ||
| export function from({ name, code, encode, minDigestLength, maxDigestLength }) { | ||
| return new Hasher(name, code, encode, minDigestLength, maxDigestLength); | ||
| } | ||
@@ -13,14 +14,26 @@ /** | ||
| encode; | ||
| constructor(name, code, encode) { | ||
| minDigestLength; | ||
| maxDigestLength; | ||
| constructor(name, code, encode, minDigestLength, maxDigestLength) { | ||
| this.name = name; | ||
| this.code = code; | ||
| this.encode = encode; | ||
| this.minDigestLength = minDigestLength ?? DEFAULT_MIN_DIGEST_LENGTH; | ||
| this.maxDigestLength = maxDigestLength; | ||
| } | ||
| digest(input) { | ||
| digest(input, options) { | ||
| if (options?.truncate != null) { | ||
| if (options.truncate < this.minDigestLength) { | ||
| throw new Error(`Invalid truncate option, must be greater than or equal to ${this.minDigestLength}`); | ||
| } | ||
| if (this.maxDigestLength != null && options.truncate > this.maxDigestLength) { | ||
| throw new Error(`Invalid truncate option, must be less than or equal to ${this.maxDigestLength}`); | ||
| } | ||
| } | ||
| if (input instanceof Uint8Array) { | ||
| const result = this.encode(input); | ||
| return result instanceof Uint8Array | ||
| ? Digest.create(this.code, result) | ||
| /* c8 ignore next 1 */ | ||
| : result.then(digest => Digest.create(this.code, digest)); | ||
| if (result instanceof Uint8Array) { | ||
| return createDigest(result, this.code, options?.truncate); | ||
| } | ||
| return result.then(digest => createDigest(digest, this.code, options?.truncate)); | ||
| } | ||
@@ -33,2 +46,15 @@ else { | ||
| } | ||
| /** | ||
| * Create a Digest from the passed uint8array and code, optionally truncating it | ||
| * first. | ||
| */ | ||
| function createDigest(digest, code, truncate) { | ||
| if (truncate != null && truncate !== digest.byteLength) { | ||
| if (truncate > digest.byteLength) { | ||
| throw new Error(`Invalid truncate option, must be less than or equal to ${digest.byteLength}`); | ||
| } | ||
| digest = digest.subarray(0, truncate); | ||
| } | ||
| return Digest.create(code, digest); | ||
| } | ||
| //# sourceMappingURL=hasher.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"hasher.js","sourceRoot":"","sources":["../../../src/hashes/hasher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AAKrC,MAAM,UAAU,IAAI,CAA6C,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAA4E;IAC/J,OAAO,IAAI,MAAM,CAAC,IAAI,EA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AAC,CAAC,CAAA;QAClF,CAAC;aAAM,CAAC;YACN,MAAM,KAAK,CAAC,mCAAmC,CAAC,CAAA;YAChD,sBAAsB;QACxB,CAAC;IACH,CAAC;CACF;AAED;;;GAGG;AACH,SAAS,YAAY,CAAwB,MAAkB,EAAE,IAAU,EAAE,QAAiB;IAC5F,IAAI,QAAQ,IAAI,IAAI,IAAI,QAAQ,KAAK,MAAM,CAAC,UAAU,EAAE,CAAC;QACvD,IAAI,QAAQ,GAAG,MAAM,CAAC,UAAU,EAAE,CAAC;YACjC,MAAM,IAAI,KAAK,CAAC,0DAA0D,MAAM,CAAC,UAAU,EAAE,CAAC,CAAA;QAChG,CAAC;QAED,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAA;IACvC,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;AACpC,CAAC"} |
| import * as Digest from './digest.js'; | ||
| import type { DigestOptions } from './hasher.js'; | ||
| declare const code: 0x0; | ||
| declare function digest(input: Uint8Array): Digest.Digest<typeof code, number>; | ||
| declare function digest(input: Uint8Array, options?: DigestOptions): Digest.Digest<typeof code, number>; | ||
| export declare const identity: { | ||
@@ -5,0 +6,0 @@ code: 0; |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../../../src/hashes/identity.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AAErC,QAAA,MAAM,IAAI,EAAE,GAAS,CAAA;AAKrB,iBAAS,MAAM,CAAE,KAAK,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,IAAI,EAAE,MAAM,CAAC,CAEtE;AAED,eAAO,MAAM,QAAQ;;;oBANC,UAAU,KAAK,UAAU;;CAMO,CAAA"} | ||
| {"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../../../src/hashes/identity.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAEhD,QAAA,MAAM,IAAI,EAAE,GAAS,CAAA;AAKrB,iBAAS,MAAM,CAAE,KAAK,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,IAAI,EAAE,MAAM,CAAC,CAU/F;AAED,eAAO,MAAM,QAAQ;;;oBAdC,UAAU,KAAK,UAAU;;CAcO,CAAA"} |
@@ -6,3 +6,9 @@ import { coerce } from '../bytes.js'; | ||
| const encode = coerce; | ||
| function digest(input) { | ||
| function digest(input, options) { | ||
| if (options?.truncate != null && options.truncate !== input.byteLength) { | ||
| if (options.truncate < 0 || options.truncate > input.byteLength) { | ||
| throw new Error(`Invalid truncate option, must be less than or equal to ${input.byteLength}`); | ||
| } | ||
| input = input.subarray(0, options.truncate); | ||
| } | ||
| return Digest.create(code, encode(input)); | ||
@@ -9,0 +15,0 @@ } |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"identity.js","sourceRoot":"","sources":["../../../src/hashes/identity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AAErC,MAAM,IAAI,GAAQ,GAAG,CAAA;AACrB,MAAM,IAAI,GAAG,UAAU,CAAA;AAEvB,MAAM,MAAM,GAAsC,MAAM,CAAA;AAExD,SAAS,MAAM,CAAE,KAAiB;IAChC,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAA;AAC3C,CAAC;AAED,MAAM,CAAC,MAAM,QAAQ,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAA"} | ||
| {"version":3,"file":"identity.js","sourceRoot":"","sources":["../../../src/hashes/identity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AAGrC,MAAM,IAAI,GAAQ,GAAG,CAAA;AACrB,MAAM,IAAI,GAAG,UAAU,CAAA;AAEvB,MAAM,MAAM,GAAsC,MAAM,CAAA;AAExD,SAAS,MAAM,CAAE,KAAiB,EAAE,OAAuB;IACzD,IAAI,OAAO,EAAE,QAAQ,IAAI,IAAI,IAAI,OAAO,CAAC,QAAQ,KAAK,KAAK,CAAC,UAAU,EAAE,CAAC;QACvE,IAAI,OAAO,CAAC,QAAQ,GAAG,CAAC,IAAI,OAAO,CAAC,QAAQ,GAAG,KAAK,CAAC,UAAU,EAAE,CAAC;YAChE,MAAM,IAAI,KAAK,CAAC,0DAA0D,KAAK,CAAC,UAAU,EAAE,CAAC,CAAA;QAC/F,CAAC;QAED,KAAK,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAA;IAC7C,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAA;AAC3C,CAAC;AAED,MAAM,CAAC,MAAM,QAAQ,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAA"} |
@@ -0,1 +1,2 @@ | ||
| import type { DigestOptions } from './hasher.js'; | ||
| /** | ||
@@ -34,3 +35,3 @@ * Represents a multihash digest which carries information about the | ||
| */ | ||
| digest(input: Uint8Array): Promise<MultihashDigest<Code>> | MultihashDigest<Code>; | ||
| digest(input: Uint8Array, options?: DigestOptions): Promise<MultihashDigest<Code>> | MultihashDigest<Code>; | ||
| /** | ||
@@ -56,4 +57,4 @@ * Name of the multihash | ||
| export interface SyncMultihashHasher<Code extends number = number> extends MultihashHasher<Code> { | ||
| digest(input: Uint8Array): MultihashDigest<Code>; | ||
| digest(input: Uint8Array, options?: DigestOptions): MultihashDigest<Code>; | ||
| } | ||
| //# sourceMappingURL=interface.d.ts.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../../../src/hashes/interface.ts"],"names":[],"mappings":"AAEA;;;GAGG;AAMH,MAAM,WAAW,eAAe,CAAC,IAAI,SAAS,MAAM,GAAG,MAAM;IAC3D;;OAEG;IACH,IAAI,EAAE,IAAI,CAAA;IAEV;;OAEG;IACH,MAAM,EAAE,UAAU,CAAA;IAElB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IAEZ;;OAEG;IACH,KAAK,EAAE,UAAU,CAAA;CAClB;AAED;;;GAGG;AACH,MAAM,WAAW,eAAe,CAAC,IAAI,SAAS,MAAM,GAAG,MAAM;IAC3D;;;;;OAKG;IACH,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,GAAG,eAAe,CAAC,IAAI,CAAC,CAAA;IAEjF;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IAEZ;;OAEG;IACH,IAAI,EAAE,IAAI,CAAA;CACX;AAED;;;;;;;;;GASG;AACH,MAAM,WAAW,mBAAmB,CAAC,IAAI,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,eAAe,CAAC,IAAI,CAAC;IAC9F,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,eAAe,CAAC,IAAI,CAAC,CAAA;CACjD"} | ||
| {"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../../../src/hashes/interface.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAEhD;;;GAGG;AAMH,MAAM,WAAW,eAAe,CAAC,IAAI,SAAS,MAAM,GAAG,MAAM;IAC3D;;OAEG;IACH,IAAI,EAAE,IAAI,CAAA;IAEV;;OAEG;IACH,MAAM,EAAE,UAAU,CAAA;IAElB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IAEZ;;OAEG;IACH,KAAK,EAAE,UAAU,CAAA;CAClB;AAED;;;GAGG;AACH,MAAM,WAAW,eAAe,CAAC,IAAI,SAAS,MAAM,GAAG,MAAM;IAC3D;;;;;OAKG;IACH,MAAM,CAAC,KAAK,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,GAAG,eAAe,CAAC,IAAI,CAAC,CAAA;IAE1G;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IAEZ;;OAEG;IACH,IAAI,EAAE,IAAI,CAAA;CACX;AAED;;;;;;;;;GASG;AACH,MAAM,WAAW,mBAAmB,CAAC,IAAI,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,eAAe,CAAC,IAAI,CAAC;IAC9F,MAAM,CAAC,KAAK,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,eAAe,CAAC,IAAI,CAAC,CAAA;CAC1E"} |
@@ -143,2 +143,6 @@ { | ||
| "./hashes/hasher:Hasher": "https://multiformats.github.io/js-multiformats/classes/hashes_hasher.Hasher.html", | ||
| "DigestOptions": "https://multiformats.github.io/js-multiformats/interfaces/hashes_hasher.DigestOptions.html", | ||
| "./hashes/hasher:DigestOptions": "https://multiformats.github.io/js-multiformats/interfaces/hashes_hasher.DigestOptions.html", | ||
| "HasherInit": "https://multiformats.github.io/js-multiformats/interfaces/hashes_hasher.HasherInit.html", | ||
| "./hashes/hasher:HasherInit": "https://multiformats.github.io/js-multiformats/interfaces/hashes_hasher.HasherInit.html", | ||
| "from": "https://multiformats.github.io/js-multiformats/functions/hashes_hasher.from.html", | ||
@@ -145,0 +149,0 @@ "./hashes/hasher:from": "https://multiformats.github.io/js-multiformats/functions/hashes_hasher.from.html", |
+1
-1
| { | ||
| "name": "multiformats", | ||
| "version": "13.3.7", | ||
| "version": "13.4.0", | ||
| "description": "Interface for multihash, multicodec, multibase and CID", | ||
@@ -5,0 +5,0 @@ "author": "Mikeal Rogers <mikeal.rogers@gmail.com> (https://www.mikealrogers.com/)", |
+75
-8
@@ -6,6 +6,41 @@ import * as Digest from './digest.js' | ||
| export function from <Name extends string, Code extends number> ({ name, code, encode }: { name: Name, code: Code, encode(input: Uint8Array): Await<Uint8Array> }): Hasher<Name, Code> { | ||
| return new Hasher(name, code, encode) | ||
| const DEFAULT_MIN_DIGEST_LENGTH = 20 | ||
| export interface HasherInit <Name extends string, Code extends number> { | ||
| name: Name | ||
| code: Code | ||
| encode(input: Uint8Array): Await<Uint8Array> | ||
| /** | ||
| * The minimum length a hash is allowed to be truncated to in bytes | ||
| * | ||
| * @default 20 | ||
| */ | ||
| minDigestLength?: number | ||
| /** | ||
| * The maximum length a hash is allowed to be truncated to in bytes. If not | ||
| * specified it will be inferred from the length of the digest. | ||
| */ | ||
| maxDigestLength?: number | ||
| } | ||
| export function from <Name extends string, Code extends number> ({ name, code, encode, minDigestLength, maxDigestLength }: HasherInit<Name, Code>): Hasher<Name, Code> { | ||
| return new Hasher(name, code, encode, minDigestLength, maxDigestLength) | ||
| } | ||
| export interface DigestOptions { | ||
| /** | ||
| * Truncate the returned digest to this number of bytes. | ||
| * | ||
| * This may cause the digest method to throw/reject if the passed value is | ||
| * greater than the digest length or below a threshold under which the risk of | ||
| * hash collisions is significant. | ||
| * | ||
| * The actual value of this threshold can depend on the hashing algorithm in | ||
| * use. | ||
| */ | ||
| truncate?: number | ||
| } | ||
| /** | ||
@@ -19,16 +54,32 @@ * Hasher represents a hashing algorithm implementation that produces as | ||
| readonly encode: (input: Uint8Array) => Await<Uint8Array> | ||
| readonly minDigestLength: number | ||
| readonly maxDigestLength?: number | ||
| constructor (name: Name, code: Code, encode: (input: Uint8Array) => Await<Uint8Array>) { | ||
| constructor (name: Name, code: Code, encode: (input: Uint8Array) => Await<Uint8Array>, minDigestLength?: number, maxDigestLength?: number) { | ||
| this.name = name | ||
| this.code = code | ||
| this.encode = encode | ||
| this.minDigestLength = minDigestLength ?? DEFAULT_MIN_DIGEST_LENGTH | ||
| this.maxDigestLength = maxDigestLength | ||
| } | ||
| digest (input: Uint8Array): Await<Digest.Digest<Code, number>> { | ||
| digest (input: Uint8Array, options?: DigestOptions): Await<Digest.Digest<Code, number>> { | ||
| if (options?.truncate != null) { | ||
| if (options.truncate < this.minDigestLength) { | ||
| throw new Error(`Invalid truncate option, must be greater than or equal to ${this.minDigestLength}`) | ||
| } | ||
| if (this.maxDigestLength != null && options.truncate > this.maxDigestLength) { | ||
| throw new Error(`Invalid truncate option, must be less than or equal to ${this.maxDigestLength}`) | ||
| } | ||
| } | ||
| if (input instanceof Uint8Array) { | ||
| const result = this.encode(input) | ||
| return result instanceof Uint8Array | ||
| ? Digest.create(this.code, result) | ||
| /* c8 ignore next 1 */ | ||
| : result.then(digest => Digest.create(this.code, digest)) | ||
| if (result instanceof Uint8Array) { | ||
| return createDigest(result, this.code, options?.truncate) | ||
| } | ||
| return result.then(digest => createDigest(digest, this.code, options?.truncate)) | ||
| } else { | ||
@@ -40,1 +91,17 @@ throw Error('Unknown type, must be binary type') | ||
| } | ||
| /** | ||
| * Create a Digest from the passed uint8array and code, optionally truncating it | ||
| * first. | ||
| */ | ||
| function createDigest <Code extends number> (digest: Uint8Array, code: Code, truncate?: number): Digest.Digest<Code, number> { | ||
| if (truncate != null && truncate !== digest.byteLength) { | ||
| if (truncate > digest.byteLength) { | ||
| throw new Error(`Invalid truncate option, must be less than or equal to ${digest.byteLength}`) | ||
| } | ||
| digest = digest.subarray(0, truncate) | ||
| } | ||
| return Digest.create(code, digest) | ||
| } |
| import { coerce } from '../bytes.js' | ||
| import * as Digest from './digest.js' | ||
| import type { DigestOptions } from './hasher.js' | ||
@@ -9,3 +10,11 @@ const code: 0x0 = 0x0 | ||
| function digest (input: Uint8Array): Digest.Digest<typeof code, number> { | ||
| function digest (input: Uint8Array, options?: DigestOptions): Digest.Digest<typeof code, number> { | ||
| if (options?.truncate != null && options.truncate !== input.byteLength) { | ||
| if (options.truncate < 0 || options.truncate > input.byteLength) { | ||
| throw new Error(`Invalid truncate option, must be less than or equal to ${input.byteLength}`) | ||
| } | ||
| input = input.subarray(0, options.truncate) | ||
| } | ||
| return Digest.create(code, encode(input)) | ||
@@ -12,0 +21,0 @@ } |
| // # Multihash | ||
| import type { DigestOptions } from './hasher.js' | ||
| /** | ||
@@ -45,3 +47,3 @@ * Represents a multihash digest which carries information about the | ||
| */ | ||
| digest(input: Uint8Array): Promise<MultihashDigest<Code>> | MultihashDigest<Code> | ||
| digest(input: Uint8Array, options?: DigestOptions): Promise<MultihashDigest<Code>> | MultihashDigest<Code> | ||
@@ -70,3 +72,3 @@ /** | ||
| export interface SyncMultihashHasher<Code extends number = number> extends MultihashHasher<Code> { | ||
| digest(input: Uint8Array): MultihashDigest<Code> | ||
| digest(input: Uint8Array, options?: DigestOptions): MultihashDigest<Code> | ||
| } |
Sorry, the diff of this file is too big to display
407176
2.9%5181
2.59%