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 fe=Object.getOwnPropertyDescriptor;var pe=Object.getOwnPropertyNames;var le=Object.prototype.hasOwnProperty;var D=(r,e)=>{for(var t in e)R(r,t,{get:e[t],enumerable:!0})},ue=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of pe(e))!le.call(r,o)&&o!==t&&R(r,o,{get:()=>e[o],enumerable:!(n=fe(e,o))||n.enumerable});return r};var we=r=>ue(R({},"__esModule",{value:!0}),r);var Ze={};D(Ze,{CID:()=>h,bytes:()=>j,digest:()=>B,hasher:()=>_,varint:()=>T});var T={};D(T,{decode:()=>O,encodeTo:()=>E,encodingLength:()=>U});var be=re,ee=128,ge=127,me=~ge,ye=Math.pow(2,31);function re(r,e,t){e=e||[],t=t||0;for(var n=t;r>=ye;)e[t++]=r&255|ee,r/=128;for(;r&me;)e[t++]=r&255|ee,r>>>=7;return e[t]=r|0,re.bytes=t-n+1,e}var xe=J,ve=128,te=127;function J(r,n){var t=0,n=n||0,o=0,s=n,i,c=r.length;do{if(s>=c)throw J.bytes=0,new RangeError("Could not decode varint");i=r[s++],t+=o<28?(i&te)<<o:(i&te)*Math.pow(2,o),o+=7}while(i>=ve);return J.bytes=s-n,t}var Ae=Math.pow(2,7),Se=Math.pow(2,14),Ee=Math.pow(2,21),Ue=Math.pow(2,28),ze=Math.pow(2,35),Ce=Math.pow(2,42),Me=Math.pow(2,49),Ne=Math.pow(2,56),Oe=Math.pow(2,63),Ve=function(r){return r<Ae?1:r<Se?2:r<Ee?3:r<Ue?4:r<ze?5:r<Ce?6:r<Me?7:r<Ne?8:r<Oe?9:10},De={encode:be,decode:xe,encodingLength:Ve},Le=De,L=Le;var O=(r,e=0)=>[L.decode(r,e),L.decode.bytes],E=(r,e,t=0)=>(L.encode(r,e,t),e),U=r=>L.encodingLength(r);var B={};D(B,{Digest:()=>C,create:()=>V,decode:()=>Q,equals:()=>G});var j={};D(j,{coerce:()=>z,empty:()=>ne,equals:()=>P,fromHex:()=>Be,fromString:()=>Ie,isBinary:()=>$e,toHex:()=>Te,toString:()=>ke});var ne=new Uint8Array(0),Te=r=>r.reduce((e,t)=>e+t.toString(16).padStart(2,"0"),""),Be=r=>{let e=r.match(/../g);return e?new Uint8Array(e.map(t=>parseInt(t,16))):ne},P=(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},z=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")},$e=r=>r instanceof ArrayBuffer||ArrayBuffer.isView(r),Ie=r=>new TextEncoder().encode(r),ke=r=>new TextDecoder().decode(r);var V=(r,e)=>{let t=e.byteLength,n=U(r),o=n+U(t),s=new Uint8Array(o+t);return E(r,s,0),E(t,s,n),s.set(e,o),new C(r,t,e,s)},Q=r=>{let e=z(r),[t,n]=O(e),[o,s]=O(e.subarray(n)),i=e.subarray(n+s);if(i.byteLength!==o)throw new Error("Incorrect length");return new C(t,o,i,e)},G=(r,e)=>{if(r===e)return!0;{let t=e;return r.code===t.code&&r.size===t.size&&t.bytes instanceof Uint8Array&&P(r.bytes,t.bytes)}},C=class{constructor(e,t,n,o){this.code=e,this.size=t,this.digest=n,this.bytes=o}};function Fe(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 s=r.charAt(o),i=s.charCodeAt(0);if(t[i]!==255)throw new TypeError(s+" is ambiguous");t[i]=o}var c=r.length,d=r.charAt(0),M=Math.log(c)/Math.log(256),p=Math.log(256)/Math.log(c);function N(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,S=0,u=0,b=a.length;u!==b&&a[u]===0;)u++,f++;for(var g=(b-u)*p+1>>>0,l=new Uint8Array(g);u!==b;){for(var m=a[u],A=0,w=g-1;(m!==0||A<S)&&w!==-1;w--,A++)m+=256*l[w]>>>0,l[w]=m%c>>>0,m=m/c>>>0;if(m!==0)throw new Error("Non-zero carry");S=A,u++}for(var x=g-S;x!==g&&l[x]===0;)x++;for(var F=d.repeat(f);x<g;++x)F+=r.charAt(l[x]);return F}function k(a){if(typeof a!="string")throw new TypeError("Expected String");if(a.length===0)return new Uint8Array;var f=0;if(a[f]!==" "){for(var S=0,u=0;a[f]===d;)S++,f++;for(var b=(a.length-f)*M+1>>>0,g=new Uint8Array(b);a[f];){var l=t[a.charCodeAt(f)];if(l===255)return;for(var m=0,A=b-1;(l!==0||m<u)&&A!==-1;A--,m++)l+=c*g[A]>>>0,g[A]=l%256>>>0
|
||
|
return Multiformats}));
|