@ipld/dag-pb
Version:
JS implementation of DAG-PB
5 lines (4 loc) • 19.6 kB
JavaScript
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.IpldDagPb = factory()}(typeof self !== 'undefined' ? self : this, function () {
;var IpldDagPb=(()=>{var $=Object.defineProperty;var Ae=Object.getOwnPropertyDescriptor;var Te=Object.getOwnPropertyNames;var ze=Object.prototype.hasOwnProperty;var Ne=(e,t)=>{for(var r in t)$(e,r,{get:t[r],enumerable:!0})},Le=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Te(t))!ze.call(e,o)&&o!==r&&$(e,o,{get:()=>t[o],enumerable:!(n=Ae(t,o))||n.enumerable});return e};var Ie=e=>Le($({},"__esModule",{value:!0}),e);var gr={};Ne(gr,{code:()=>mr,createLink:()=>ve,createNode:()=>ge,decode:()=>xr,encode:()=>yr,name:()=>br,prepare:()=>W,validate:()=>Z});var Er=new Uint8Array(0);function Y(e,t){if(e===t)return!0;if(e.byteLength!==t.byteLength)return!1;for(let r=0;r<e.byteLength;r++)if(e[r]!==t[r])return!1;return!0}function T(e){if(e instanceof Uint8Array&&e.constructor.name==="Uint8Array")return e;if(e instanceof ArrayBuffer)return new Uint8Array(e);if(ArrayBuffer.isView(e))return new Uint8Array(e.buffer,e.byteOffset,e.byteLength);throw new Error("Unknown type, must be binary type")}function Be(e,t){if(e.length>=255)throw new TypeError("Alphabet too long");for(var r=new Uint8Array(256),n=0;n<r.length;n++)r[n]=255;for(var o=0;o<e.length;o++){var i=e.charAt(o),s=i.charCodeAt(0);if(r[s]!==255)throw new TypeError(i+" is ambiguous");r[s]=o}var a=e.length,f=e.charAt(0),p=Math.log(a)/Math.log(256),A=Math.log(256)/Math.log(a);function G(c){if(c instanceof Uint8Array||(ArrayBuffer.isView(c)?c=new Uint8Array(c.buffer,c.byteOffset,c.byteLength):Array.isArray(c)&&(c=Uint8Array.from(c))),!(c instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(c.length===0)return"";for(var d=0,D=0,u=0,w=c.length;u!==w&&c[u]===0;)u++,d++;for(var b=(w-u)*A+1>>>0,h=new Uint8Array(b);u!==w;){for(var m=c[u],E=0,l=b-1;(m!==0||E<D)&&l!==-1;l--,E++)m+=256*h[l]>>>0,h[l]=m%a>>>0,m=m/a>>>0;if(m!==0)throw new Error("Non-zero carry");D=E,u++}for(var x=b-D;x!==b&&h[x]===0;)x++;for(var M=f.repeat(d);x<b;++x)M+=e.charAt(h[x]);return M}function H(c){if(typeof c!="string")throw new TypeError("Expected String");if(c.length===0)return new Uint8Array;var d=0;if(c[d]!==" "){for(var D=0,u=0;c[d]===f;)D++,d++;for(var w=(c.length-d)*p+1>>>0,b=new Uint8Array(w);c[d];){var h=r[c.charCodeAt(d)];if(h===255)return;for(var m=0,E=w-1;(h!==0||m<u)&&E!==-1;E--,m++)h+=a*b[E]>>>0,b[E]=h%256>>>0,h=h/256>>>0;if(h!==0)throw new Error("Non-zero carry");u=m,d++}if(c[d]!==" "){for(var l=w-u;l!==w&&b[l]===0;)l++;for(var x=new Uint8Array(D+(w-l)),M=D;l!==w;)x[M++]=b[l++];return x}}}function De(c){var d=H(c);if(d)return d;throw new Error(`Non-${t} character`)}return{encode:G,decodeUnsafe:H,decode:De}}var Ce=Be,ke=Ce,_=ke;var F=class{name;prefix;baseEncode;constructor(t,r,n){this.name=t,this.prefix=r,this.baseEncode=n}encode(t){if(t instanceof Uint8Array)return`${this.prefix}${this.baseEncode(t)}`;throw Error("Unknown type, must be binary type")}},j=class{name;prefix;baseDecode;prefixCodePoint;constructor(t,r,n){this.name=t,this.prefix=r;let o=r.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 ee(this,t)}},q=class{decoders;constructor(t){this.decoders=t}or(t){return ee(this,t)}decode(t){let r=t[0],n=this.decoders[r];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 ee(e,t){return new q({...e.decoders??{[e.prefix]:e},...t.decoders??{[t.prefix]:t}})}var R=class{name;prefix;baseEncode;baseDecode;encoder;decoder;constructor(t,r,n,o){this.name=t,this.prefix=r,this.baseEncode=n,this.baseDecode=o,this.encoder=new F(t,r,n),this.decoder=new j(t,r,o)}encode(t){return this.encoder.encode(t)}decode(t){return this.decoder.decode(t)}};function re({name:e,prefix:t,encode:r,decode:n}){return new R(e,t,r,n)}function z({name:e,prefix:t,alphabet:r}){let{encode:n,decode:o}=_(r,e);return re({prefix:t,name:e,encode:n,decode:i=>T(o(i))})}function Pe(e,t,r,n){let o=e.length;for(;e[o-1]==="=";)--o;let i=new Uint8Array(o*r/8|0),s=0,a=0,f=0;for(let p=0;p<o;++p){let A=t[e[p]];if(A===void 0)throw new SyntaxError(`Non-${n} character`);a=a<<r|A,s+=r,s>=8&&(s-=8,i[f++]=255&a>>s)}if(s>=r||(255&a<<8-s)!==0)throw new SyntaxError("Unexpected end of data");return i}function Se(e,t,r){let n=t[t.length-1]==="=",o=(1<<r)-1,i="",s=0,a=0;for(let f=0;f<e.length;++f)for(a=a<<8|e[f],s+=8;s>r;)s-=r,i+=t[o&a>>s];if(s!==0&&(i+=t[o&a<<r-s]),n)for(;(i.length*r&7)!==0;)i+="=";return i}function Ue(e){let t={};for(let r=0;r<e.length;++r)t[e[r]]=r;return t}function y({name:e,prefix:t,bitsPerChar:r,alphabet:n}){let o=Ue(n);return re({prefix:t,name:e,encode(i){return Se(i,n,r)},decode(i){return Pe(i,o,r,e)}})}var C=y({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),Ir=y({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Br=y({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Cr=y({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),kr=y({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Pr=y({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Sr=y({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Ur=y({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Hr=y({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var O=z({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),Vr=z({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var g=z({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),Fr=z({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var He=oe,te=128,Me=127,Oe=~Me,Ve=Math.pow(2,31);function oe(e,t,r){t=t||[],r=r||0;for(var n=r;e>=Ve;)t[r++]=e&255|te,e/=128;for(;e&Oe;)t[r++]=e&255|te,e>>>=7;return t[r]=e|0,oe.bytes=r-n+1,t}var Ge=J,$e=128,ne=127;function J(e,n){var r=0,n=n||0,o=0,i=n,s,a=e.length;do{if(i>=a)throw J.bytes=0,new RangeError("Could not decode varint");s=e[i++],r+=o<28?(s&ne)<<o:(s&ne)*Math.pow(2,o),o+=7}while(s>=$e);return J.bytes=i-n,r}var Fe=Math.pow(2,7),je=Math.pow(2,14),qe=Math.pow(2,21),Re=Math.pow(2,28),Je=Math.pow(2,35),Ke=Math.pow(2,42),Qe=Math.pow(2,49),Xe=Math.pow(2,56),We=Math.pow(2,63),Ze=function(e){return e<Fe?1:e<je?2:e<qe?3:e<Re?4:e<Je?5:e<Ke?6:e<Qe?7:e<Xe?8:e<We?9:10},Ye={encode:He,decode:Ge,encodingLength:Ze},_e=Ye,k=_e;function P(e,t=0){return[k.decode(e,t),k.decode.bytes]}function N(e,t,r=0){return k.encode(e,t,r),t}function L(e){return k.encodingLength(e)}function se(e,t){let r=t.byteLength,n=L(e),o=n+L(r),i=new Uint8Array(o+r);return N(e,i,0),N(r,i,n),i.set(t,o),new I(e,r,t,i)}function ae(e){let t=T(e),[r,n]=P(t),[o,i]=P(t.subarray(n)),s=t.subarray(n+i);if(s.byteLength!==o)throw new Error("Incorrect length");return new I(r,o,s,t)}function ce(e,t){if(e===t)return!0;{let r=t;return e.code===r.code&&e.size===r.size&&r.bytes instanceof Uint8Array&&Y(e.bytes,r.bytes)}}var I=class{code;size;digest;bytes;constructor(t,r,n,o){this.code=t,this.size=r,this.digest=n,this.bytes=o}};function fe(e,t){let{bytes:r,version:n}=e;switch(n){case 0:return tr(r,K(e),t??g.encoder);default:return nr(r,K(e),t??C.encoder)}}var de=new WeakMap;function K(e){let t=de.get(e);if(t==null){let r=new Map;return de.set(e,r),r}return t}var v=class e{code;version;multihash;bytes;"/";constructor(t,r,n,o){this.code=r,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:r}=this;if(t!==S)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(r.code!==or)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return e.createV0(r)}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:r}=this.multihash,n=se(t,r);return e.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 e.equals(this,t)}static equals(t,r){let n=r;return n!=null&&t.code===n.code&&t.version===n.version&&ce(t.multihash,n.multihash)}toString(t){return fe(this,t)}toJSON(){return{"/":fe(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 r=t;if(r instanceof e)return r;if(r["/"]!=null&&r["/"]===r.bytes||r.asCID===r){let{version:n,code:o,multihash:i,bytes:s}=r;return new e(n,o,i,s??he(n,o,i.bytes))}else if(r[ir]===!0){let{version:n,multihash:o,code:i}=r,s=ae(o);return e.create(n,i,s)}else return null}static create(t,r,n){if(typeof r!="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(r!==S)throw new Error(`Version 0 CID must use dag-pb (code: ${S}) block encoding`);return new e(t,r,n,n.bytes)}case 1:{let o=he(t,r,n.bytes);return new e(t,r,n,o)}default:throw new Error("Invalid version")}}static createV0(t){return e.create(0,S,t)}static createV1(t,r){return e.create(1,t,r)}static decode(t){let[r,n]=e.decodeFirst(t);if(n.length!==0)throw new Error("Incorrect length");return r}static decodeFirst(t){let r=e.inspectBytes(t),n=r.size-r.multihashSize,o=T(t.subarray(n,n+r.multihashSize));if(o.byteLength!==r.multihashSize)throw new Error("Incorrect length");let i=o.subarray(r.multihashSize-r.digestSize),s=new I(r.multihashCode,r.digestSize,i,o);return[r.version===0?e.createV0(s):e.createV1(r.codec,s),t.subarray(r.size)]}static inspectBytes(t){let r=0,n=()=>{let[G,H]=P(t.subarray(r));return r+=H,G},o=n(),i=S;if(o===18?(o=0,r=0):i=n(),o!==0&&o!==1)throw new RangeError(`Invalid CID version ${o}`);let s=r,a=n(),f=n(),p=r+f,A=p-s;return{version:o,codec:i,multihashCode:a,digestSize:f,multihashSize:A,size:p}}static parse(t,r){let[n,o]=rr(t,r),i=e.decode(o);if(i.version===0&&t[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return K(i).set(n,t),i}};function rr(e,t){switch(e[0]){case"Q":{let r=t??g;return[g.prefix,r.decode(`${g.prefix}${e}`)]}case g.prefix:{let r=t??g;return[g.prefix,r.decode(e)]}case C.prefix:{let r=t??C;return[C.prefix,r.decode(e)]}case O.prefix:{let r=t??O;return[O.prefix,r.decode(e)]}default:{if(t==null)throw Error("To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}}function tr(e,t,r){let{prefix:n}=r;if(n!==g.prefix)throw Error(`Cannot string encode V0 in ${r.name} encoding`);let o=t.get(n);if(o==null){let i=r.encode(e).slice(1);return t.set(n,i),i}else return o}function nr(e,t,r){let{prefix:n}=r,o=t.get(n);if(o==null){let i=r.encode(e);return t.set(n,i),i}else return o}var S=112,or=18;function he(e,t,r){let n=L(e),o=n+L(t),i=new Uint8Array(o+r.byteLength);return N(e,i,0),N(t,i,n),i.set(r,o),i}var ir=Symbol.for("@ipld/js-cid/CID");var sr=new TextDecoder;function Q(e,t){let r=0;for(let n=0;;n+=7){if(n>=64)throw new Error("protobuf: varint overflow");if(t>=e.length)throw new Error("protobuf: unexpected end of data");let o=e[t++];if(r+=n<28?(o&127)<<n:(o&127)*2**n,o<128)break}return[r,t]}function V(e,t){let r;[r,t]=Q(e,t);let n=t+r;if(r<0||n<0)throw new Error("protobuf: invalid length");if(n>e.length)throw new Error("protobuf: unexpected end of data");return[e.subarray(t,n),n]}function pe(e,t){let r;return[r,t]=Q(e,t),[r&7,r>>3,t]}function ar(e){let t={},r=e.length,n=0;for(;n<r;){let o,i;if([o,i,n]=pe(e,n),i===1){if(t.Hash)throw new Error("protobuf: (PBLink) duplicate Hash section");if(o!==2)throw new Error(`protobuf: (PBLink) wrong wireType (${o}) for Hash`);if(t.Name!==void 0)throw new Error("protobuf: (PBLink) invalid order, found Name before Hash");if(t.Tsize!==void 0)throw new Error("protobuf: (PBLink) invalid order, found Tsize before Hash");[t.Hash,n]=V(e,n)}else if(i===2){if(t.Name!==void 0)throw new Error("protobuf: (PBLink) duplicate Name section");if(o!==2)throw new Error(`protobuf: (PBLink) wrong wireType (${o}) for Name`);if(t.Tsize!==void 0)throw new Error("protobuf: (PBLink) invalid order, found Tsize before Name");let s;[s,n]=V(e,n),t.Name=sr.decode(s)}else if(i===3){if(t.Tsize!==void 0)throw new Error("protobuf: (PBLink) duplicate Tsize section");if(o!==0)throw new Error(`protobuf: (PBLink) wrong wireType (${o}) for Tsize`);[t.Tsize,n]=Q(e,n)}else throw new Error(`protobuf: (PBLink) invalid fieldNumber, expected 1, 2 or 3, got ${i}`)}if(n>r)throw new Error("protobuf: (PBLink) unexpected end of data");return t}function ue(e){let t=e.length,r=0,n,o=!1,i;for(;r<t;){let a,f;if([a,f,r]=pe(e,r),a!==2)throw new Error(`protobuf: (PBNode) invalid wireType, expected 2, got ${a}`);if(f===1){if(i)throw new Error("protobuf: (PBNode) duplicate Data section");[i,r]=V(e,r),n&&(o=!0)}else if(f===2){if(o)throw new Error("protobuf: (PBNode) duplicate Links section");n||(n=[]);let p;[p,r]=V(e,r),n.push(ar(p))}else throw new Error(`protobuf: (PBNode) invalid fieldNumber, expected 1 or 2, got ${f}`)}if(r>t)throw new Error("protobuf: (PBNode) unexpected end of data");let s={};return i&&(s.Data=i),s.Links=n||[],s}var we=new TextEncoder,le=2**32,cr=2**31;function fr(e,t){let r=t.length;if(typeof e.Tsize=="number"){if(e.Tsize<0)throw new Error("Tsize cannot be negative");if(!Number.isSafeInteger(e.Tsize))throw new Error("Tsize too large for encoding");r=U(t,r,e.Tsize)-1,t[r]=24}if(typeof e.Name=="string"){let n=we.encode(e.Name);r-=n.length,t.set(n,r),r=U(t,r,n.length)-1,t[r]=18}return e.Hash&&(r-=e.Hash.length,t.set(e.Hash,r),r=U(t,r,e.Hash.length)-1,t[r]=10),t.length-r}function be(e){let t=hr(e),r=new Uint8Array(t),n=t;if(e.Data&&(n-=e.Data.length,r.set(e.Data,n),n=U(r,n,e.Data.length)-1,r[n]=10),e.Links)for(let o=e.Links.length-1;o>=0;o--){let i=fr(e.Links[o],r.subarray(0,n));n-=i,n=U(r,n,i)-1,r[n]=18}return r}function dr(e){let t=0;if(e.Hash){let r=e.Hash.length;t+=1+r+B(r)}if(typeof e.Name=="string"){let r=we.encode(e.Name).length;t+=1+r+B(r)}return typeof e.Tsize=="number"&&(t+=1+B(e.Tsize)),t}function hr(e){let t=0;if(e.Data){let r=e.Data.length;t+=1+r+B(r)}if(e.Links)for(let r of e.Links){let n=dr(r);t+=1+n+B(n)}return t}function U(e,t,r){t-=B(r);let n=t;for(;r>=cr;)e[t++]=r&127|128,r/=128;for(;r>=128;)e[t++]=r&127|128,r>>>=7;return e[t]=r,n}function B(e){return e%2===0&&e++,Math.floor((pr(e)+6)/7)}function pr(e){let t=0;return e>=le&&(e=Math.floor(e/le),t=32),e>=65536&&(e>>>=16,t+=16),e>=256&&(e>>>=8,t+=8),t+ur[e]}var ur=[0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8];var lr=["Data","Links"],wr=["Hash","Name","Tsize"],X=new TextEncoder;function ye(e,t){if(e===t)return 0;let r=e.Name?X.encode(e.Name):[],n=t.Name?X.encode(t.Name):[],o=r.length,i=n.length;for(let s=0,a=Math.min(o,i);s<a;++s)if(r[s]!==n[s]){o=r[s],i=n[s];break}return o<i?-1:i<o?1:0}function me(e,t){return!Object.keys(e).some(r=>!t.includes(r))}function xe(e){if(typeof e.asCID=="object"){let r=v.asCID(e);if(!r)throw new TypeError("Invalid DAG-PB form");return{Hash:r}}if(typeof e!="object"||Array.isArray(e))throw new TypeError("Invalid DAG-PB form");let t={};if(e.Hash){let r=v.asCID(e.Hash);try{r||(typeof e.Hash=="string"?r=v.parse(e.Hash):e.Hash instanceof Uint8Array&&(r=v.decode(e.Hash)))}catch(n){throw new TypeError(`Invalid DAG-PB form: ${n.message}`)}r&&(t.Hash=r)}if(!t.Hash)throw new TypeError("Invalid DAG-PB form");return typeof e.Name=="string"&&(t.Name=e.Name),typeof e.Tsize=="number"&&(t.Tsize=e.Tsize),t}function W(e){if((e instanceof Uint8Array||typeof e=="string")&&(e={Data:e}),typeof e!="object"||Array.isArray(e))throw new TypeError("Invalid DAG-PB form");let t={};if(e.Data!==void 0)if(typeof e.Data=="string")t.Data=X.encode(e.Data);else if(e.Data instanceof Uint8Array)t.Data=e.Data;else throw new TypeError("Invalid DAG-PB form");if(e.Links!==void 0)if(Array.isArray(e.Links))t.Links=e.Links.map(xe),t.Links.sort(ye);else throw new TypeError("Invalid DAG-PB form");else t.Links=[];return t}function Z(e){if(!e||typeof e!="object"||Array.isArray(e)||e instanceof Uint8Array||e["/"]&&e["/"]===e.bytes)throw new TypeError("Invalid DAG-PB form");if(!me(e,lr))throw new TypeError("Invalid DAG-PB form (extraneous properties)");if(e.Data!==void 0&&!(e.Data instanceof Uint8Array))throw new TypeError("Invalid DAG-PB form (Data must be bytes)");if(!Array.isArray(e.Links))throw new TypeError("Invalid DAG-PB form (Links must be a list)");for(let t=0;t<e.Links.length;t++){let r=e.Links[t];if(!r||typeof r!="object"||Array.isArray(r)||r instanceof Uint8Array||r["/"]&&r["/"]===r.bytes)throw new TypeError("Invalid DAG-PB form (bad link)");if(!me(r,wr))throw new TypeError("Invalid DAG-PB form (extraneous properties on link)");if(r.Hash===void 0)throw new TypeError("Invalid DAG-PB form (link must have a Hash)");if(r.Hash==null||!r.Hash["/"]||r.Hash["/"]!==r.Hash.bytes)throw new TypeError("Invalid DAG-PB form (link Hash must be a CID)");if(r.Name!==void 0&&typeof r.Name!="string")throw new TypeError("Invalid DAG-PB form (link Name must be a string)");if(r.Tsize!==void 0){if(typeof r.Tsize!="number"||r.Tsize%1!==0)throw new TypeError("Invalid DAG-PB form (link Tsize must be an integer)");if(r.Tsize<0)throw new TypeError("Invalid DAG-PB form (link Tsize cannot be negative)")}if(t>0&&ye(r,e.Links[t-1])===-1)throw new TypeError("Invalid DAG-PB form (links must be sorted by Name bytes)")}}function ge(e,t=[]){return W({Data:e,Links:t})}function ve(e,t,r){return xe({Hash:r,Name:e,Tsize:t})}function Ee(e){return e instanceof ArrayBuffer?new Uint8Array(e,0,e.byteLength):e}var br="dag-pb",mr=112;function yr(e){Z(e);let t={};return e.Links&&(t.Links=e.Links.map(r=>{let n={};return r.Hash&&(n.Hash=r.Hash.bytes),r.Name!==void 0&&(n.Name=r.Name),r.Tsize!==void 0&&(n.Tsize=r.Tsize),n})),e.Data&&(t.Data=e.Data),be(t)}function xr(e){let t=Ee(e),r=ue(t),n={};return r.Data&&(n.Data=r.Data),r.Links&&(n.Links=r.Links.map(o=>{let i={};try{i.Hash=v.decode(o.Hash)}catch{}if(!i.Hash)throw new Error("Invalid Hash field found in link, expected CID");return o.Name!==void 0&&(i.Name=o.Name),o.Tsize!==void 0&&(i.Tsize=o.Tsize),i})),n}return Ie(gr);})();
return IpldDagPb}));
//# sourceMappingURL=index.min.js.map