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