UNPKG

uint8arrays

Version:

Utility functions to make dealing with Uint8Arrays easier

5 lines (4 loc) 19.4 kB
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.Uint8Arrays = factory()}(typeof self !== 'undefined' ? self : this, function () { "use strict";var Uint8Arrays=(()=>{var G=Object.defineProperty;var Re=Object.getOwnPropertyDescriptor;var Je=Object.getOwnPropertyNames;var Ge=Object.prototype.hasOwnProperty;var d=(r,e)=>{for(var t in e)G(r,t,{get:e[t],enumerable:!0})},Xe=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of Je(e))!Ge.call(r,o)&&o!==t&&G(r,o,{get:()=>e[o],enumerable:!(n=Re(e,o))||n.enumerable});return r};var Qe=r=>Xe(G({},"__esModule",{value:!0}),r);var or={};d(or,{compare:()=>be,concat:()=>xe,equals:()=>le,fromString:()=>Fe,toString:()=>ke,xor:()=>me});function le(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 D(r=0){return new Uint8Array(r)}function me(r,e){if(r.length!==e.length)throw new Error("Inputs should have the same length");let t=D(r.length);for(let n=0;n<r.length;n++)t[n]=r[n]^e[n];return t}function be(r,e){for(let t=0;t<r.byteLength;t++){if(r[t]<e[t])return-1;if(r[t]>e[t])return 1}return r.byteLength>e.byteLength?1:r.byteLength<e.byteLength?-1:0}function xe(r,e){e==null&&(e=r.reduce((o,i)=>o+i.length,0));let t=D(e),n=0;for(let o of r)t.set(o,n),n+=o.length;return t}var W={};d(W,{base10:()=>tt});var br=new Uint8Array(0);function ge(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 v(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 new TextEncoder().encode(r)}function we(r){return new TextDecoder().decode(r)}function He(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 f=r.length,u=r.charAt(0),A=Math.log(f)/Math.log(256),U=Math.log(256)/Math.log(f);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 p=0,C=0,l=0,b=a.length;l!==b&&a[l]===0;)l++,p++;for(var x=(b-l)*U+1>>>0,h=new Uint8Array(x);l!==b;){for(var g=a[l],E=0,m=x-1;(g!==0||E<C)&&m!==-1;m--,E++)g+=256*h[m]>>>0,h[m]=g%f>>>0,g=g/f>>>0;if(g!==0)throw new Error("Non-zero carry");C=E,l++}for(var w=x-C;w!==x&&h[w]===0;)w++;for(var F=u.repeat(p);w<x;++w)F+=r.charAt(h[w]);return F}function q(a){if(typeof a!="string")throw new TypeError("Expected String");if(a.length===0)return new Uint8Array;var p=0;if(a[p]!==" "){for(var C=0,l=0;a[p]===u;)C++,p++;for(var b=(a.length-p)*A+1>>>0,x=new Uint8Array(b);a[p];){var h=t[a.charCodeAt(p)];if(h===255)return;for(var g=0,E=b-1;(h!==0||g<l)&&E!==-1;E--,g++)h+=f*x[E]>>>0,x[E]=h%256>>>0,h=h/256>>>0;if(h!==0)throw new Error("Non-zero carry");l=g,p++}if(a[p]!==" "){for(var m=b-l;m!==b&&x[m]===0;)m++;for(var w=new Uint8Array(C+(b-m)),F=C;m!==b;)w[F++]=x[m++];return w}}}function je(a){var p=q(a);if(p)return p;throw new Error(`Non-${e} character`)}return{encode:J,decodeUnsafe:q,decode:je}}var We=He,Ze=We,Ae=Ze;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")}},Q=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 Ee(this,e)}},K=class{decoders;constructor(e){this.decoders=e}or(e){return Ee(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 Ee(r,e){return new K({...r.decoders??{[r.prefix]:r},...e.decoders??{[e.prefix]:e}})}var H=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 Q(e,t,o)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}};function L({name:r,prefix:e,encode:t,decode:n}){return new H(r,e,t,n)}function S({name:r,prefix:e,alphabet:t}){let{encode:n,decode:o}=Ae(t,r);return L({prefix:e,name:r,encode:n,decode:i=>v(o(i))})}function Ye(r,e,t,n){let o=r.length;for(;r[o-1]==="=";)--o;let i=new Uint8Array(o*t/8|0),s=0,f=0,u=0;for(let A=0;A<o;++A){let U=e[r[A]];if(U===void 0)throw new SyntaxError(`Non-${n} character`);f=f<<t|U,s+=t,s>=8&&(s-=8,i[u++]=255&f>>s)}if(s>=t||(255&f<<8-s)!==0)throw new SyntaxError("Unexpected end of data");return i}function _e(r,e,t){let n=e[e.length-1]==="=",o=(1<<t)-1,i="",s=0,f=0;for(let u=0;u<r.length;++u)for(f=f<<8|r[u],s+=8;s>t;)s-=t,i+=e[o&f>>s];if(s!==0&&(i+=e[o&f<<t-s]),n)for(;(i.length*t&7)!==0;)i+="=";return i}function et(r){let e={};for(let t=0;t<r.length;++t)e[r[t]]=t;return e}function c({name:r,prefix:e,bitsPerChar:t,alphabet:n}){let o=et(n);return L({prefix:e,name:r,encode(i){return _e(i,n,t)},decode(i){return Ye(i,o,t,r)}})}var tt=S({prefix:"9",name:"base10",alphabet:"0123456789"});var Z={};d(Z,{base16:()=>rt,base16upper:()=>nt});var rt=c({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4}),nt=c({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4});var Y={};d(Y,{base2:()=>ot});var ot=c({prefix:"0",name:"base2",alphabet:"01",bitsPerChar:1});var _={};d(_,{base256emoji:()=>ft});var Se=Array.from("\u{1F680}\u{1FA90}\u2604\u{1F6F0}\u{1F30C}\u{1F311}\u{1F312}\u{1F313}\u{1F314}\u{1F315}\u{1F316}\u{1F317}\u{1F318}\u{1F30D}\u{1F30F}\u{1F30E}\u{1F409}\u2600\u{1F4BB}\u{1F5A5}\u{1F4BE}\u{1F4BF}\u{1F602}\u2764\u{1F60D}\u{1F923}\u{1F60A}\u{1F64F}\u{1F495}\u{1F62D}\u{1F618}\u{1F44D}\u{1F605}\u{1F44F}\u{1F601}\u{1F525}\u{1F970}\u{1F494}\u{1F496}\u{1F499}\u{1F622}\u{1F914}\u{1F606}\u{1F644}\u{1F4AA}\u{1F609}\u263A\u{1F44C}\u{1F917}\u{1F49C}\u{1F614}\u{1F60E}\u{1F607}\u{1F339}\u{1F926}\u{1F389}\u{1F49E}\u270C\u2728\u{1F937}\u{1F631}\u{1F60C}\u{1F338}\u{1F64C}\u{1F60B}\u{1F497}\u{1F49A}\u{1F60F}\u{1F49B}\u{1F642}\u{1F493}\u{1F929}\u{1F604}\u{1F600}\u{1F5A4}\u{1F603}\u{1F4AF}\u{1F648}\u{1F447}\u{1F3B6}\u{1F612}\u{1F92D}\u2763\u{1F61C}\u{1F48B}\u{1F440}\u{1F62A}\u{1F611}\u{1F4A5}\u{1F64B}\u{1F61E}\u{1F629}\u{1F621}\u{1F92A}\u{1F44A}\u{1F973}\u{1F625}\u{1F924}\u{1F449}\u{1F483}\u{1F633}\u270B\u{1F61A}\u{1F61D}\u{1F634}\u{1F31F}\u{1F62C}\u{1F643}\u{1F340}\u{1F337}\u{1F63B}\u{1F613}\u2B50\u2705\u{1F97A}\u{1F308}\u{1F608}\u{1F918}\u{1F4A6}\u2714\u{1F623}\u{1F3C3}\u{1F490}\u2639\u{1F38A}\u{1F498}\u{1F620}\u261D\u{1F615}\u{1F33A}\u{1F382}\u{1F33B}\u{1F610}\u{1F595}\u{1F49D}\u{1F64A}\u{1F639}\u{1F5E3}\u{1F4AB}\u{1F480}\u{1F451}\u{1F3B5}\u{1F91E}\u{1F61B}\u{1F534}\u{1F624}\u{1F33C}\u{1F62B}\u26BD\u{1F919}\u2615\u{1F3C6}\u{1F92B}\u{1F448}\u{1F62E}\u{1F646}\u{1F37B}\u{1F343}\u{1F436}\u{1F481}\u{1F632}\u{1F33F}\u{1F9E1}\u{1F381}\u26A1\u{1F31E}\u{1F388}\u274C\u270A\u{1F44B}\u{1F630}\u{1F928}\u{1F636}\u{1F91D}\u{1F6B6}\u{1F4B0}\u{1F353}\u{1F4A2}\u{1F91F}\u{1F641}\u{1F6A8}\u{1F4A8}\u{1F92C}\u2708\u{1F380}\u{1F37A}\u{1F913}\u{1F619}\u{1F49F}\u{1F331}\u{1F616}\u{1F476}\u{1F974}\u25B6\u27A1\u2753\u{1F48E}\u{1F4B8}\u2B07\u{1F628}\u{1F31A}\u{1F98B}\u{1F637}\u{1F57A}\u26A0\u{1F645}\u{1F61F}\u{1F635}\u{1F44E}\u{1F932}\u{1F920}\u{1F927}\u{1F4CC}\u{1F535}\u{1F485}\u{1F9D0}\u{1F43E}\u{1F352}\u{1F617}\u{1F911}\u{1F30A}\u{1F92F}\u{1F437}\u260E\u{1F4A7}\u{1F62F}\u{1F486}\u{1F446}\u{1F3A4}\u{1F647}\u{1F351}\u2744\u{1F334}\u{1F4A3}\u{1F438}\u{1F48C}\u{1F4CD}\u{1F940}\u{1F922}\u{1F445}\u{1F4A1}\u{1F4A9}\u{1F450}\u{1F4F8}\u{1F47B}\u{1F910}\u{1F92E}\u{1F3BC}\u{1F975}\u{1F6A9}\u{1F34E}\u{1F34A}\u{1F47C}\u{1F48D}\u{1F4E3}\u{1F942}"),it=Se.reduce((r,e,t)=>(r[t]=e,r),[]),st=Se.reduce((r,e,t)=>{let n=e.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${e}`);return r[n]=t,r},[]);function at(r){return r.reduce((e,t)=>(e+=it[t],e),"")}function ct(r){let e=[];for(let t of r){let n=t.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${t}`);let o=st[n];if(o==null)throw new Error(`Non-base256emoji character: ${t}`);e.push(o)}return new Uint8Array(e)}var ft=L({prefix:"\u{1F680}",name:"base256emoji",encode:at,decode:ct});var ee={};d(ee,{base32:()=>I,base32hex:()=>ut,base32hexpad:()=>mt,base32hexpadupper:()=>bt,base32hexupper:()=>lt,base32pad:()=>dt,base32padupper:()=>ht,base32upper:()=>pt,base32z:()=>xt});var I=c({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),pt=c({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),dt=c({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),ht=c({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),ut=c({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),lt=c({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),mt=c({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),bt=c({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),xt=c({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var te={};d(te,{base36:()=>O,base36upper:()=>gt});var O=S({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),gt=S({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var re={};d(re,{base58btc:()=>y,base58flickr:()=>yt});var y=S({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),yt=S({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var ne={};d(ne,{base64:()=>wt,base64pad:()=>vt,base64url:()=>At,base64urlpad:()=>Et});var wt=c({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),vt=c({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),At=c({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),Et=c({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var oe={};d(oe,{base8:()=>St});var St=c({prefix:"7",name:"base8",alphabet:"01234567",bitsPerChar:3});var ie={};d(ie,{identity:()=>Ct});var Ct=L({prefix:"\0",name:"identity",encode:r=>we(r),decode:r=>ye(r)});var Tr=new TextEncoder,Nr=new TextDecoder;var ae={};d(ae,{identity:()=>Kt});var Lt=De,Ce=128,It=127,zt=~It,Mt=Math.pow(2,31);function De(r,e,t){e=e||[],t=t||0;for(var n=t;r>=Mt;)e[t++]=r&255|Ce,r/=128;for(;r&zt;)e[t++]=r&255|Ce,r>>>=7;return e[t]=r|0,De.bytes=t-n+1,e}var Tt=se,Nt=128,Ue=127;function se(r,n){var t=0,n=n||0,o=0,i=n,s,f=r.length;do{if(i>=f)throw se.bytes=0,new RangeError("Could not decode varint");s=r[i++],t+=o<28?(s&Ue)<<o:(s&Ue)*Math.pow(2,o),o+=7}while(s>=Nt);return se.bytes=i-n,t}var Ot=Math.pow(2,7),Bt=Math.pow(2,14),$t=Math.pow(2,21),Vt=Math.pow(2,28),Pt=Math.pow(2,35),qt=Math.pow(2,42),Ft=Math.pow(2,49),kt=Math.pow(2,56),jt=Math.pow(2,63),Rt=function(r){return r<Ot?1:r<Bt?2:r<$t?3:r<Vt?4:r<Pt?5:r<qt?6:r<Ft?7:r<kt?8:r<jt?9:10},Jt={encode:Lt,decode:Tt,encodingLength:Rt},Gt=Jt,B=Gt;function $(r,e=0){return[B.decode(r,e),B.decode.bytes]}function z(r,e,t=0){return B.encode(r,e,t),e}function M(r){return B.encodingLength(r)}function N(r,e){let t=e.byteLength,n=M(r),o=n+M(t),i=new Uint8Array(o+t);return z(r,i,0),z(t,i,n),i.set(e,o),new T(r,t,e,i)}function Le(r){let e=v(r),[t,n]=$(e),[o,i]=$(e.subarray(n)),s=e.subarray(n+i);if(s.byteLength!==o)throw new Error("Incorrect length");return new T(t,o,s,e)}function Ie(r,e){if(r===e)return!0;{let t=e;return r.code===t.code&&r.size===t.size&&t.bytes instanceof Uint8Array&&ge(r.bytes,t.bytes)}}var T=class{code;size;digest;bytes;constructor(e,t,n,o){this.code=e,this.size=t,this.digest=n,this.bytes=o}};var ze=0,Xt="identity",Me=v;function Qt(r,e){if(e?.truncate!=null&&e.truncate!==r.byteLength){if(e.truncate<0||e.truncate>r.byteLength)throw new Error(`Invalid truncate option, must be less than or equal to ${r.byteLength}`);r=r.subarray(0,e.truncate)}return N(ze,Me(r))}var Kt={code:ze,name:Xt,encode:Me,digest:Qt};var pe={};d(pe,{sha256:()=>Wt,sha512:()=>Zt});var Ht=20;function fe({name:r,code:e,encode:t,minDigestLength:n,maxDigestLength:o}){return new ce(r,e,t,n,o)}var ce=class{name;code;encode;minDigestLength;maxDigestLength;constructor(e,t,n,o,i){this.name=e,this.code=t,this.encode=n,this.minDigestLength=o??Ht,this.maxDigestLength=i}digest(e,t){if(t?.truncate!=null){if(t.truncate<this.minDigestLength)throw new Error(`Invalid truncate option, must be greater than or equal to ${this.minDigestLength}`);if(this.maxDigestLength!=null&&t.truncate>this.maxDigestLength)throw new Error(`Invalid truncate option, must be less than or equal to ${this.maxDigestLength}`)}if(e instanceof Uint8Array){let n=this.encode(e);return n instanceof Uint8Array?Te(n,this.code,t?.truncate):n.then(o=>Te(o,this.code,t?.truncate))}else throw Error("Unknown type, must be binary type")}};function Te(r,e,t){if(t!=null&&t!==r.byteLength){if(t>r.byteLength)throw new Error(`Invalid truncate option, must be less than or equal to ${r.byteLength}`);r=r.subarray(0,t)}return N(e,r)}function Oe(r){return async e=>new Uint8Array(await crypto.subtle.digest(r,e))}var Wt=fe({name:"sha2-256",code:18,encode:Oe("SHA-256")}),Zt=fe({name:"sha2-512",code:19,encode:Oe("SHA-512")});function Be(r,e){let{bytes:t,version:n}=r;return n===0?_t(t,de(r),e??y.encoder):er(t,de(r),e??I.encoder)}var $e=new WeakMap;function de(r){let e=$e.get(r);if(e==null){let t=new Map;return $e.set(r,t),t}return e}var j=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!==P)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(t.code!==tr)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=N(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&&Ie(e.multihash,n.multihash)}toString(e){return Be(this,e)}toJSON(){return{"/":Be(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??Ve(n,o,i.bytes))}else if(t[rr]===!0){let{version:n,multihash:o,code:i}=t,s=Le(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!==P)throw new Error(`Version 0 CID must use dag-pb (code: ${P}) block encoding`);return new r(e,t,n,n.bytes)}case 1:{let o=Ve(e,t,n.bytes);return new r(e,t,n,o)}default:throw new Error("Invalid version")}}static createV0(e){return r.create(0,P,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=v(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 T(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,q]=$(e.subarray(t));return t+=q,J},o=n(),i=P;if(o===18?(o=0,t=0):i=n(),o!==0&&o!==1)throw new RangeError(`Invalid CID version ${o}`);let s=t,f=n(),u=n(),A=t+u,U=A-s;return{version:o,codec:i,multihashCode:f,digestSize:u,multihashSize:U,size:A}}static parse(e,t){let[n,o]=Yt(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 de(i).set(n,e),i}};function Yt(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 I.prefix:{let t=e??I;return[I.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, base36 or base58btc encoded CID multibase decoder must be provided");return[r[0],e.decode(r)]}}}function _t(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 er(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 P=112,tr=18;function Ve(r,e,t){let n=M(r),o=n+M(e),i=new Uint8Array(o+t.byteLength);return z(r,i,0),z(e,i,n),i.set(t,o),i}var rr=Symbol.for("@ipld/js-cid/CID");var he={...ie,...Y,...oe,...W,...Z,...ee,...te,...re,...ne,..._},tn={...pe,...ae};function qe(r,e,t,n){return{name:r,prefix:e,encoder:{name:r,prefix:e,encode:t},decoder:{decode:n}}}var Pe=qe("utf8","u",r=>"u"+new TextDecoder("utf8").decode(r),r=>new TextEncoder().encode(r.substring(1))),ue=qe("ascii","a",r=>{let e="a";for(let t=0;t<r.length;t++)e+=String.fromCharCode(r[t]);return e},r=>{r=r.substring(1);let e=D(r.length);for(let t=0;t<r.length;t++)e[t]=r.charCodeAt(t);return e}),nr={utf8:Pe,"utf-8":Pe,hex:he.base16,latin1:ue,ascii:ue,binary:ue,...he},R=nr;function Fe(r,e="utf8"){let t=R[e];if(t==null)throw new Error(`Unsupported encoding "${e}"`);return t.decoder.decode(`${t.prefix}${r}`)}function ke(r,e="utf8"){let t=R[e];if(t==null)throw new Error(`Unsupported encoding "${e}"`);return t.encoder.encode(r).substring(1)}return Qe(or);})(); return Uint8Arrays})); //# sourceMappingURL=index.min.js.map