4 lines
12 KiB
JavaScript
4 lines
12 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 R=Object.defineProperty;var he=Object.getOwnPropertyDescriptor;var pe=Object.getOwnPropertyNames;var ue=Object.prototype.hasOwnProperty;var N=(r,e)=>{for(var t in e)R(r,t,{get:e[t],enumerable:!0})},le=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of pe(e))!ue.call(r,o)&&o!==t&&R(r,o,{get:()=>e[o],enumerable:!(n=he(e,o))||n.enumerable});return r};var be=r=>le(R({},"__esModule",{value:!0}),r);var Ze={};N(Ze,{CID:()=>F,bytes:()=>P,digest:()=>T,hasher:()=>_,varint:()=>L});var P={};N(P,{coerce:()=>S,empty:()=>ee,equals:()=>J,fromHex:()=>xe,fromString:()=>me,isBinary:()=>ge,toHex:()=>we,toString:()=>ye});var ee=new Uint8Array(0);function we(r){return r.reduce((e,t)=>e+t.toString(16).padStart(2,"0"),"")}function xe(r){let e=r.match(/../g);return e!=null?new Uint8Array(e.map(t=>parseInt(t,16))):ee}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 S(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 me(r){return new TextEncoder().encode(r)}function ye(r){return new TextDecoder().decode(r)}function ve(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,d=r.charAt(0),U=Math.log(c)/Math.log(256),h=Math.log(256)/Math.log(c);function z(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 w=(b-u)*h+1>>>0,p=new Uint8Array(w);u!==b;){for(var x=a[u],v=0,l=w-1;(x!==0||v<E)&&l!==-1;l--,v++)x+=256*p[l]>>>0,p[l]=x%c>>>0,x=x/c>>>0;if(x!==0)throw new Error("Non-zero carry");E=v,u++}for(var m=w-E;m!==w&&p[m]===0;)m++;for(var k=d.repeat(f);m<w;++m)k+=r.charAt(p[m]);return k}function $(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 b=(a.length-f)*U+1>>>0,w=new Uint8Array(b);a[f];){var p=t[a.charCodeAt(f)];if(p===255)return;for(var x=0,v=b-1;(p!==0||x<u)&&v!==-1;v--,x++)p+=c*w[v]>>>0,w[v]=p%256>>>0,p=p/256>>>0;if(p!==0)throw new Error("Non-zero carry");u=x,f++}if(a[f]!==" "){for(var l=b-u;l!==b&&w[l]===0;)l++;for(var m=new Uint8Array(E+(b-l)),k=E;l!==b;)m[k++]=w[l++];return m}}}function fe(a){var f=$(a);if(f)return f;throw new Error(`Non-${e} character`)}return{encode:z,decodeUnsafe:$,decode:fe}}var Ee=ve,Se=Ee,te=Se;var j=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")}},Q=class{name;prefix;baseDecode;prefixCodePoint;constructor(e,t,n){if(this.name=e,this.prefix=t,t.codePointAt(0)===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=t.codePointAt(0),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 re(this,e)}},G=class{decoders;constructor(e){this.decoders=e}or(e){return re(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 re(r,e){return new G({...r.decoders??{[r.prefix]:r},...e.decoders??{[e.prefix]:e}})}var X=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 j(e,t,n),this.decoder=new Q(e,t,o)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}};function ne({name:r,prefix:e,encode:t,decode:n}){return new X(r,e,t,n)}function K({name:r,prefix:e,alphabet:t}){let{encode:n,decode:o}=te(t,r);return ne({prefix:e,name:r,encode:n,decode:i=>S(o(i))})}function Ae(r,e,t,n){let o={};for(let h=0;h<e.length;++h)o[e[h]]=h;let i=r.length;for(;r[i-1]==="=";)--i;let s=new Uint8Array(i*t/8|0),c=0,d=0,U=0;for(let h=0;h<i;++h){let z=o[r[h]];if(z===void 0)throw new SyntaxError(`Non-${n} character`);d=d<<t|z,c+=t,c>=8&&(c-=8,s[U++]=255&d>>c)}if(c>=t||255&d<<8-c)throw new SyntaxError("Unexpected end of data");return s}function Ce(r,e,t){let n=e[e.length-1]==="=",o=(1<<t)-1,i="",s=0,c=0;for(let d=0;d<r.length;++d)for(c=c<<8|r[d],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;)i+="=";return i}function g({name:r,prefix:e,bitsPerChar:t,alphabet:n}){return ne({prefix:e,name:r,encode(o){return Ce(o,n,t)},decode(o){return Ae(o,n,t,r)}})}var O=g({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),nt=g({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),ot=g({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),it=g({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),st=g({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),at=g({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),ct=g({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),dt=g({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),ft=g({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var y=K({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),ut=K({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var T={};N(T,{Digest:()=>D,create:()=>M,decode:()=>Z,equals:()=>Y});var L={};N(L,{decode:()=>I,encodeTo:()=>A,encodingLength:()=>C});var De=se,oe=128,Ue=127,ze=~Ue,Ie=Math.pow(2,31);function se(r,e,t){e=e||[],t=t||0;for(var n=t;r>=Ie;)e[t++]=r&255|oe,r/=128;for(;r&ze;)e[t++]=r&255|oe,r>>>=7;return e[t]=r|0,se.bytes=t-n+1,e}var Me=W,Ne=128,ie=127;function W(r,n){var t=0,n=n||0,o=0,i=n,s,c=r.length;do{if(i>=c)throw W.bytes=0,new RangeError("Could not decode varint");s=r[i++],t+=o<28?(s&ie)<<o:(s&ie)*Math.pow(2,o),o+=7}while(s>=Ne);return W.bytes=i-n,t}var Oe=Math.pow(2,7),Ve=Math.pow(2,14),Le=Math.pow(2,21),Te=Math.pow(2,28),Be=Math.pow(2,35),$e=Math.pow(2,42),ke=Math.pow(2,49),Fe=Math.pow(2,56),qe=Math.pow(2,63),Re=function(r){return r<Oe?1:r<Ve?2:r<Le?3:r<Te?4:r<Be?5:r<$e?6:r<ke?7:r<Fe?8:r<qe?9:10},Je={encode:De,decode:Me,encodingLength:Re},Pe=Je,V=Pe;function I(r,e=0){return[V.decode(r,e),V.decode.bytes]}function A(r,e,t=0){return V.encode(r,e,t),e}function C(r){return V.encodingLength(r)}function M(r,e){let t=e.byteLength,n=C(r),o=n+C(t),i=new Uint8Array(o+t);return A(r,i,0),A(t,i,n),i.set(e,o),new D(r,t,e,i)}function Z(r){let e=S(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 Y(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 ae(r,e){let{bytes:t,version:n}=r;switch(n){case 0:return Qe(t,H(r),e??y.encoder);default:return Ge(t,H(r),e??O.encoder)}}var ce=new WeakMap;function H(r){let e=ce.get(r);if(e==null){let t=new Map;return ce.set(r,t),t}return e}var F=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!==Xe)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&&Y(e.multihash,n.multihash)}toString(e){return ae(this,e)}toJSON(){return{"/":ae(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??de(n,o,i.bytes))}else if(t[Ke]===!0){let{version:n,multihash:o,code:i}=t,s=Z(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=de(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=S(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[z,$]=I(e.subarray(t));return t+=$,z},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(),d=n(),U=t+d,h=U-s;return{version:o,codec:i,multihashCode:c,digestSize:d,multihashSize:h,size:U}}static parse(e,t){let[n,o]=je(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 H(i).set(n,e),i}};function je(r,e){switch(r[0]){case"Q":{let t=e??y;return[y.prefix,t.decode(`${y.prefix}${r}`)]}case y.prefix:{let t=e??y;return[y.prefix,t.decode(r)]}case O.prefix:{let t=e??O;return[O.prefix,t.decode(r)]}default:{if(e==null)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[r[0],e.decode(r)]}}}function Qe(r,e,t){let{prefix:n}=t;if(n!==y.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 Ge(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,Xe=18;function de(r,e,t){let n=C(r),o=n+C(e),i=new Uint8Array(o+t.byteLength);return A(r,i,0),A(e,i,n),i.set(t,o),i}var Ke=Symbol.for("@ipld/js-cid/CID");var _={};N(_,{Hasher:()=>q,from:()=>We});function We({name:r,code:e,encode:t}){return new q(r,e,t)}var q=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 be(Ze);})();
|
|
return Multiformats}));
|