@helia/dag-cbor 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +4 -0
- package/README.md +59 -0
- package/dist/index.min.js +3 -0
- package/dist/src/index.d.ts +88 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +51 -0
- package/dist/src/index.js.map +1 -0
- package/package.json +156 -0
- package/src/index.ts +120 -0
package/LICENSE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://github.com/ipfs/helia" title="Helia">
|
|
3
|
+
<img src="https://raw.githubusercontent.com/ipfs/helia/main/assets/helia.png" alt="Helia logo" width="300" />
|
|
4
|
+
</a>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
# @helia/dag-cbor <!-- omit in toc -->
|
|
8
|
+
|
|
9
|
+
[](https://ipfs.tech)
|
|
10
|
+
[](https://discuss.ipfs.tech)
|
|
11
|
+
[](https://codecov.io/gh/ipfs/helia-dag-cbor)
|
|
12
|
+
[](https://github.com/ipfs/helia-dag-cbor/actions/workflows/js-test-and-release.yml?query=branch%3Amain)
|
|
13
|
+
|
|
14
|
+
> Add/get IPLD blocks containing dag-cbor with your Helia node
|
|
15
|
+
|
|
16
|
+
## Table of contents <!-- omit in toc -->
|
|
17
|
+
|
|
18
|
+
- [Install](#install)
|
|
19
|
+
- [Browser `<script>` tag](#browser-script-tag)
|
|
20
|
+
- [API Docs](#api-docs)
|
|
21
|
+
- [License](#license)
|
|
22
|
+
- [Contribute](#contribute)
|
|
23
|
+
|
|
24
|
+
## Install
|
|
25
|
+
|
|
26
|
+
```console
|
|
27
|
+
$ npm i @helia/dag-cbor
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Browser `<script>` tag
|
|
31
|
+
|
|
32
|
+
Loading this module through a script tag will make it's exports available as `HeliaDagCbor` in the global namespace.
|
|
33
|
+
|
|
34
|
+
```html
|
|
35
|
+
<script src="https://unpkg.com/@helia/dag-cbor/dist/index.min.js"></script>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## API Docs
|
|
39
|
+
|
|
40
|
+
- <https://ipfs.github.io/helia-dag-cbor/modules/_helia_dag_cbor.html>
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
Licensed under either of
|
|
45
|
+
|
|
46
|
+
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
|
|
47
|
+
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
|
|
48
|
+
|
|
49
|
+
## Contribute
|
|
50
|
+
|
|
51
|
+
Contributions welcome! Please check out [the issues](https://github.com/ipfs/helia-dag-cbor/issues).
|
|
52
|
+
|
|
53
|
+
Also see our [contributing document](https://github.com/ipfs/community/blob/master/CONTRIBUTING_JS.md) for more information on how we work, and about contributing in general.
|
|
54
|
+
|
|
55
|
+
Please be aware that all interactions related to this repo are subject to the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).
|
|
56
|
+
|
|
57
|
+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
|
|
58
|
+
|
|
59
|
+
[](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md)
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.HeliaDagCbor = factory()}(typeof self !== 'undefined' ? self : this, function () {
|
|
2
|
+
"use strict";var HeliaDagCbor=(()=>{var ke=Object.defineProperty;var wr=Object.getOwnPropertyDescriptor;var br=Object.getOwnPropertyNames;var xr=Object.prototype.hasOwnProperty;var Er=(e,t)=>{for(var r in t)ke(e,r,{get:t[r],enumerable:!0})},Ur=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of br(t))!xr.call(e,o)&&o!==r&&ke(e,o,{get:()=>t[o],enumerable:!(n=wr(t,o))||n.enumerable});return e};var vr=e=>Ur(ke({},"__esModule",{value:!0}),e);var On={};Er(On,{dagCbor:()=>zn});var Ar=He,Je=128,kr=127,Tr=~kr,Sr=Math.pow(2,31);function He(e,t,r){t=t||[],r=r||0;for(var n=r;e>=Sr;)t[r++]=e&255|Je,e/=128;for(;e&Tr;)t[r++]=e&255|Je,e>>>=7;return t[r]=e|0,He.bytes=r-n+1,t}var Br=Te,Nr=128,Ke=127;function Te(e,n){var r=0,n=n||0,o=0,i=n,u,d=e.length;do{if(i>=d)throw Te.bytes=0,new RangeError("Could not decode varint");u=e[i++],r+=o<28?(u&Ke)<<o:(u&Ke)*Math.pow(2,o),o+=7}while(u>=Nr);return Te.bytes=i-n,r}var _r=Math.pow(2,7),Ir=Math.pow(2,14),$r=Math.pow(2,21),Cr=Math.pow(2,28),Mr=Math.pow(2,35),zr=Math.pow(2,42),Or=Math.pow(2,49),Dr=Math.pow(2,56),Fr=Math.pow(2,63),Vr=function(e){return e<_r?1:e<Ir?2:e<$r?3:e<Cr?4:e<Mr?5:e<zr?6:e<Or?7:e<Dr?8:e<Fr?9:10},Lr={encode:Ar,decode:Br,encodingLength:Vr},Rr=Lr,re=Rr;var ne=(e,t=0)=>[re.decode(e,t),re.decode.bytes],J=(e,t,r=0)=>(re.encode(e,t,r),t),K=e=>re.encodingLength(e);var Ln=new Uint8Array(0);var Qe=(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},H=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")};var oe=(e,t)=>{let r=t.byteLength,n=K(e),o=n+K(r),i=new Uint8Array(o+r);return J(e,i,0),J(r,i,n),i.set(t,o),new X(e,r,t,i)},We=e=>{let t=H(e),[r,n]=ne(t),[o,i]=ne(t.subarray(n)),u=t.subarray(n+i);if(u.byteLength!==o)throw new Error("Incorrect length");return new X(r,o,u,t)},Ze=(e,t)=>{if(e===t)return!0;{let r=t;return e.code===r.code&&e.size===r.size&&r.bytes instanceof Uint8Array&&Qe(e.bytes,r.bytes)}},X=class{constructor(t,r,n,o){this.code=t,this.size=r,this.digest=n,this.bytes=o}};function Pr(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),u=i.charCodeAt(0);if(r[u]!==255)throw new TypeError(i+" is ambiguous");r[u]=o}var d=e.length,p=e.charAt(0),b=Math.log(d)/Math.log(256),h=Math.log(256)/Math.log(d);function G(m){if(m instanceof Uint8Array||(ArrayBuffer.isView(m)?m=new Uint8Array(m.buffer,m.byteOffset,m.byteLength):Array.isArray(m)&&(m=Uint8Array.from(m))),!(m instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(m.length===0)return"";for(var x=0,j=0,B=0,$=m.length;B!==$&&m[B]===0;)B++,x++;for(var C=($-B)*h+1>>>0,k=new Uint8Array(C);B!==$;){for(var M=m[B],L=0,N=C-1;(M!==0||L<j)&&N!==-1;N--,L++)M+=256*k[N]>>>0,k[N]=M%d>>>0,M=M/d>>>0;if(M!==0)throw new Error("Non-zero carry");j=L,B++}for(var D=C-j;D!==C&&k[D]===0;)D++;for(var he=p.repeat(x);D<C;++D)he+=e.charAt(k[D]);return he}function pe(m){if(typeof m!="string")throw new TypeError("Expected String");if(m.length===0)return new Uint8Array;var x=0;if(m[x]!==" "){for(var j=0,B=0;m[x]===p;)j++,x++;for(var $=(m.length-x)*b+1>>>0,C=new Uint8Array($);m[x];){var k=r[m.charCodeAt(x)];if(k===255)return;for(var M=0,L=$-1;(k!==0||M<B)&&L!==-1;L--,M++)k+=d*C[L]>>>0,C[L]=k%256>>>0,k=k/256>>>0;if(k!==0)throw new Error("Non-zero carry");B=M,x++}if(m[x]!==" "){for(var N=$-B;N!==$&&C[N]===0;)N++;for(var D=new Uint8Array(j+($-N)),he=j;N!==$;)D[he++]=C[N++];return D}}}function gr(m){var x=pe(m);if(x)return x;throw new Error(`Non-${t} character`)}return{encode:G,decodeUnsafe:pe,decode:gr}}var jr=Pr,qr=jr,et=qr;var Se=class{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")}},Be=class{constructor(t,r,n){if(this.name=t,this.prefix=r,r.codePointAt(0)===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=r.codePointAt(0),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 tt(this,t)}},Ne=class{constructor(t){this.decoders=t}or(t){return tt(this,t)}decode(t){let r=t[0],n=this.decoders[r];if(n)return n.decode(t);throw RangeError(`Unable to decode multibase string ${JSON.stringify(t)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}},tt=(e,t)=>new Ne({...e.decoders||{[e.prefix]:e},...t.decoders||{[t.prefix]:t}}),_e=class{constructor(t,r,n,o){this.name=t,this.prefix=r,this.baseEncode=n,this.baseDecode=o,this.encoder=new Se(t,r,n),this.decoder=new Be(t,r,o)}encode(t){return this.encoder.encode(t)}decode(t){return this.decoder.decode(t)}},rt=({name:e,prefix:t,encode:r,decode:n})=>new _e(e,t,r,n),Ie=({prefix:e,name:t,alphabet:r})=>{let{encode:n,decode:o}=et(r,t);return rt({prefix:e,name:t,encode:n,decode:i=>H(o(i))})},Gr=(e,t,r,n)=>{let o={};for(let h=0;h<t.length;++h)o[t[h]]=h;let i=e.length;for(;e[i-1]==="=";)--i;let u=new Uint8Array(i*r/8|0),d=0,p=0,b=0;for(let h=0;h<i;++h){let G=o[e[h]];if(G===void 0)throw new SyntaxError(`Non-${n} character`);p=p<<r|G,d+=r,d>=8&&(d-=8,u[b++]=255&p>>d)}if(d>=r||255&p<<8-d)throw new SyntaxError("Unexpected end of data");return u},Jr=(e,t,r)=>{let n=t[t.length-1]==="=",o=(1<<r)-1,i="",u=0,d=0;for(let p=0;p<e.length;++p)for(d=d<<8|e[p],u+=8;u>r;)u-=r,i+=t[o&d>>u];if(u&&(i+=t[o&d<<r-u]),n)for(;i.length*r&7;)i+="=";return i},z=({name:e,prefix:t,bitsPerChar:r,alphabet:n})=>rt({prefix:t,name:e,encode(o){return Jr(o,n,r)},decode(o){return Gr(o,n,r,e)}});var F=Ie({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),Hn=Ie({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var ie=z({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),Wn=z({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Zn=z({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Yn=z({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),eo=z({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),to=z({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),ro=z({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),no=z({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),oo=z({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var nt=(e,t)=>{let{bytes:r,version:n}=e;switch(n){case 0:return Hr(r,$e(e),t||F.encoder);default:return Xr(r,$e(e),t||ie.encoder)}};var ot=new WeakMap,$e=e=>{let t=ot.get(e);if(t==null){let r=new Map;return ot.set(e,r),r}return t},y=class{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!==se)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(r.code!==Qr)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return y.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=oe(t,r);return y.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 y.equals(this,t)}static equals(t,r){let n=r;return n&&t.code===n.code&&t.version===n.version&&Ze(t.multihash,n.multihash)}toString(t){return nt(this,t)}toJSON(){return{"/":nt(this)}}link(){return this}get[Symbol.toStringTag](){return"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 y)return r;if(r["/"]!=null&&r["/"]===r.bytes||r.asCID===r){let{version:n,code:o,multihash:i,bytes:u}=r;return new y(n,o,i,u||it(n,o,i.bytes))}else if(r[Wr]===!0){let{version:n,multihash:o,code:i}=r,u=We(o);return y.create(n,i,u)}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!==se)throw new Error(`Version 0 CID must use dag-pb (code: ${se}) block encoding`);return new y(t,r,n,n.bytes)}case 1:{let o=it(t,r,n.bytes);return new y(t,r,n,o)}default:throw new Error("Invalid version")}}static createV0(t){return y.create(0,se,t)}static createV1(t,r){return y.create(1,t,r)}static decode(t){let[r,n]=y.decodeFirst(t);if(n.length)throw new Error("Incorrect length");return r}static decodeFirst(t){let r=y.inspectBytes(t),n=r.size-r.multihashSize,o=H(t.subarray(n,n+r.multihashSize));if(o.byteLength!==r.multihashSize)throw new Error("Incorrect length");let i=o.subarray(r.multihashSize-r.digestSize),u=new X(r.multihashCode,r.digestSize,i,o);return[r.version===0?y.createV0(u):y.createV1(r.codec,u),t.subarray(r.size)]}static inspectBytes(t){let r=0,n=()=>{let[G,pe]=ne(t.subarray(r));return r+=pe,G},o=n(),i=se;if(o===18?(o=0,r=0):i=n(),o!==0&&o!==1)throw new RangeError(`Invalid CID version ${o}`);let u=r,d=n(),p=n(),b=r+p,h=b-u;return{version:o,codec:i,multihashCode:d,digestSize:p,multihashSize:h,size:b}}static parse(t,r){let[n,o]=Kr(t,r),i=y.decode(o);if(i.version===0&&t[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return $e(i).set(n,t),i}},Kr=(e,t)=>{switch(e[0]){case"Q":{let r=t||F;return[F.prefix,r.decode(`${F.prefix}${e}`)]}case F.prefix:{let r=t||F;return[F.prefix,r.decode(e)]}case ie.prefix:{let r=t||ie;return[ie.prefix,r.decode(e)]}default:{if(t==null)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}},Hr=(e,t,r)=>{let{prefix:n}=r;if(n!==F.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},Xr=(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},se=112,Qr=18,it=(e,t,r)=>{let n=K(e),o=n+K(t),i=new Uint8Array(o+r.byteLength);return J(e,i,0),J(t,i,n),i.set(r,o),i},Wr=Symbol.for("@ipld/js-cid/CID");var Zr=["string","number","bigint","symbol"],Yr=["Function","Generator","AsyncGenerator","GeneratorFunction","AsyncGeneratorFunction","AsyncFunction","Observable","Array","Buffer","Object","RegExp","Date","Error","Map","Set","WeakMap","WeakSet","ArrayBuffer","SharedArrayBuffer","DataView","Promise","URL","HTMLElement","Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array","BigInt64Array","BigUint64Array"];function st(e){if(e===null)return"null";if(e===void 0)return"undefined";if(e===!0||e===!1)return"boolean";let t=typeof e;if(Zr.includes(t))return t;if(t==="function")return"Function";if(Array.isArray(e))return"Array";if(en(e))return"Buffer";let r=tn(e);return r||"Object"}function en(e){return e&&e.constructor&&e.constructor.isBuffer&&e.constructor.isBuffer.call(null,e)}function tn(e){let t=Object.prototype.toString.call(e).slice(8,-1);if(Yr.includes(t))return t}var s=class{constructor(t,r,n){this.major=t,this.majorEncoded=t<<5,this.name=r,this.terminal=n}toString(){return`Type[${this.major}].${this.name}`}compare(t){return this.major<t.major?-1:this.major>t.major?1:0}};s.uint=new s(0,"uint",!0);s.negint=new s(1,"negint",!0);s.bytes=new s(2,"bytes",!0);s.string=new s(3,"string",!0);s.array=new s(4,"array",!1);s.map=new s(5,"map",!1);s.tag=new s(6,"tag",!1);s.float=new s(7,"float",!0);s.false=new s(7,"false",!0);s.true=new s(7,"true",!0);s.null=new s(7,"null",!0);s.undefined=new s(7,"undefined",!0);s.break=new s(7,"break",!0);var a=class{constructor(t,r,n){this.type=t,this.value=r,this.encodedLength=n,this.encodedBytes=void 0,this.byteValue=void 0}toString(){return`Token[${this.type}].${this.value}`}};var Q=globalThis.process&&!globalThis.process.browser&&globalThis.Buffer&&typeof globalThis.Buffer.isBuffer=="function",rn=new TextDecoder,nn=new TextEncoder;function me(e){return Q&&globalThis.Buffer.isBuffer(e)}function Ce(e){return e instanceof Uint8Array?me(e)?new Uint8Array(e.buffer,e.byteOffset,e.byteLength):e:Uint8Array.from(e)}var dt=Q?(e,t,r)=>r-t>64?globalThis.Buffer.from(e.subarray(t,r)).toString("utf8"):at(e,t,r):(e,t,r)=>r-t>64?rn.decode(e.subarray(t,r)):at(e,t,r),ft=Q?e=>e.length>64?globalThis.Buffer.from(e):ct(e):e=>e.length>64?nn.encode(e):ct(e),O=e=>Uint8Array.from(e),W=Q?(e,t,r)=>me(e)?new Uint8Array(e.subarray(t,r)):e.slice(t,r):(e,t,r)=>e.slice(t,r),lt=Q?(e,t)=>(e=e.map(r=>r instanceof Uint8Array?r:globalThis.Buffer.from(r)),Ce(globalThis.Buffer.concat(e,t))):(e,t)=>{let r=new Uint8Array(t),n=0;for(let o of e)n+o.length>r.length&&(o=o.subarray(0,r.length-n)),r.set(o,n),n+=o.length;return r},pt=Q?e=>globalThis.Buffer.allocUnsafe(e):e=>new Uint8Array(e);function ht(e,t){if(me(e)&&me(t))return e.compare(t);for(let r=0;r<e.length;r++)if(e[r]!==t[r])return e[r]<t[r]?-1:1;return 0}function ct(e,t=1/0){let r,n=e.length,o=null,i=[];for(let u=0;u<n;++u){if(r=e.charCodeAt(u),r>55295&&r<57344){if(!o){if(r>56319){(t-=3)>-1&&i.push(239,191,189);continue}else if(u+1===n){(t-=3)>-1&&i.push(239,191,189);continue}o=r;continue}if(r<56320){(t-=3)>-1&&i.push(239,191,189),o=r;continue}r=(o-55296<<10|r-56320)+65536}else o&&(t-=3)>-1&&i.push(239,191,189);if(o=null,r<128){if((t-=1)<0)break;i.push(r)}else if(r<2048){if((t-=2)<0)break;i.push(r>>6|192,r&63|128)}else if(r<65536){if((t-=3)<0)break;i.push(r>>12|224,r>>6&63|128,r&63|128)}else if(r<1114112){if((t-=4)<0)break;i.push(r>>18|240,r>>12&63|128,r>>6&63|128,r&63|128)}else throw new Error("Invalid code point")}return i}function at(e,t,r){let n=[];for(;t<r;){let o=e[t],i=null,u=o>239?4:o>223?3:o>191?2:1;if(t+u<=r){let d,p,b,h;switch(u){case 1:o<128&&(i=o);break;case 2:d=e[t+1],(d&192)===128&&(h=(o&31)<<6|d&63,h>127&&(i=h));break;case 3:d=e[t+1],p=e[t+2],(d&192)===128&&(p&192)===128&&(h=(o&15)<<12|(d&63)<<6|p&63,h>2047&&(h<55296||h>57343)&&(i=h));break;case 4:d=e[t+1],p=e[t+2],b=e[t+3],(d&192)===128&&(p&192)===128&&(b&192)===128&&(h=(o&15)<<18|(d&63)<<12|(p&63)<<6|b&63,h>65535&&h<1114112&&(i=h))}}i===null?(i=65533,u=1):i>65535&&(i-=65536,n.push(i>>>10&1023|55296),i=56320|i&1023),n.push(i),t+=u}return on(n)}var ut=4096;function on(e){let t=e.length;if(t<=ut)return String.fromCharCode.apply(String,e);let r="",n=0;for(;n<t;)r+=String.fromCharCode.apply(String,e.slice(n,n+=ut));return r}var sn=256,ce=class{constructor(t=sn){this.chunkSize=t,this.cursor=0,this.maxCursor=-1,this.chunks=[],this._initReuseChunk=null}reset(){this.cursor=0,this.maxCursor=-1,this.chunks.length&&(this.chunks=[]),this._initReuseChunk!==null&&(this.chunks.push(this._initReuseChunk),this.maxCursor=this._initReuseChunk.length-1)}push(t){let r=this.chunks[this.chunks.length-1];if(this.cursor+t.length<=this.maxCursor+1){let o=r.length-(this.maxCursor-this.cursor)-1;r.set(t,o)}else{if(r){let o=r.length-(this.maxCursor-this.cursor)-1;o<r.length&&(this.chunks[this.chunks.length-1]=r.subarray(0,o),this.maxCursor=this.cursor-1)}t.length<64&&t.length<this.chunkSize?(r=pt(this.chunkSize),this.chunks.push(r),this.maxCursor+=r.length,this._initReuseChunk===null&&(this._initReuseChunk=r),r.set(t,0)):(this.chunks.push(t),this.maxCursor+=t.length)}this.cursor+=t.length}toBytes(t=!1){let r;if(this.chunks.length===1){let n=this.chunks[0];t&&this.cursor>n.length/2?(r=this.cursor===n.length?n:n.subarray(0,this.cursor),this._initReuseChunk=null,this.chunks=[]):r=W(n,0,this.cursor)}else r=lt(this.chunks,this.cursor);return t&&this.reset(),r}};var f="CBOR decode error:",Me="CBOR encode error:",ae=[];ae[23]=1;ae[24]=2;ae[25]=3;ae[26]=5;ae[27]=9;function V(e,t,r){if(e.length-t<r)throw new Error(`${f} not enough data for type`)}var g=[24,256,65536,4294967296,BigInt("18446744073709551616")];function E(e,t,r){V(e,t,1);let n=e[t];if(r.strict===!0&&n<g[0])throw new Error(`${f} integer encoded in more bytes than necessary (strict decode)`);return n}function U(e,t,r){V(e,t,2);let n=e[t]<<8|e[t+1];if(r.strict===!0&&n<g[1])throw new Error(`${f} integer encoded in more bytes than necessary (strict decode)`);return n}function v(e,t,r){V(e,t,4);let n=e[t]*16777216+(e[t+1]<<16)+(e[t+2]<<8)+e[t+3];if(r.strict===!0&&n<g[2])throw new Error(`${f} integer encoded in more bytes than necessary (strict decode)`);return n}function A(e,t,r){V(e,t,8);let n=e[t]*16777216+(e[t+1]<<16)+(e[t+2]<<8)+e[t+3],o=e[t+4]*16777216+(e[t+5]<<16)+(e[t+6]<<8)+e[t+7],i=(BigInt(n)<<BigInt(32))+BigInt(o);if(r.strict===!0&&i<g[3])throw new Error(`${f} integer encoded in more bytes than necessary (strict decode)`);if(i<=Number.MAX_SAFE_INTEGER)return Number(i);if(r.allowBigInt===!0)return i;throw new Error(`${f} integers outside of the safe integer range are not supported`)}function mt(e,t,r,n){return new a(s.uint,E(e,t+1,n),2)}function yt(e,t,r,n){return new a(s.uint,U(e,t+1,n),3)}function gt(e,t,r,n){return new a(s.uint,v(e,t+1,n),5)}function wt(e,t,r,n){return new a(s.uint,A(e,t+1,n),9)}function T(e,t){return w(e,0,t.value)}function w(e,t,r){if(r<g[0]){let n=Number(r);e.push([t|n])}else if(r<g[1]){let n=Number(r);e.push([t|24,n])}else if(r<g[2]){let n=Number(r);e.push([t|25,n>>>8,n&255])}else if(r<g[3]){let n=Number(r);e.push([t|26,n>>>24&255,n>>>16&255,n>>>8&255,n&255])}else{let n=BigInt(r);if(n<g[4]){let o=[t|27,0,0,0,0,0,0,0],i=Number(n&BigInt(4294967295)),u=Number(n>>BigInt(32)&BigInt(4294967295));o[8]=i&255,i=i>>8,o[7]=i&255,i=i>>8,o[6]=i&255,i=i>>8,o[5]=i&255,o[4]=u&255,u=u>>8,o[3]=u&255,u=u>>8,o[2]=u&255,u=u>>8,o[1]=u&255,e.push(o)}else throw new Error(`${f} encountered BigInt larger than allowable range`)}}T.encodedSize=function(t){return w.encodedSize(t.value)};w.encodedSize=function(t){return t<g[0]?1:t<g[1]?2:t<g[2]?3:t<g[3]?5:9};T.compareTokens=function(t,r){return t.value<r.value?-1:t.value>r.value?1:0};function bt(e,t,r,n){return new a(s.negint,-1-E(e,t+1,n),2)}function xt(e,t,r,n){return new a(s.negint,-1-U(e,t+1,n),3)}function Et(e,t,r,n){return new a(s.negint,-1-v(e,t+1,n),5)}var ze=BigInt(-1),Ut=BigInt(1);function vt(e,t,r,n){let o=A(e,t+1,n);if(typeof o!="bigint"){let i=-1-o;if(i>=Number.MIN_SAFE_INTEGER)return new a(s.negint,i,9)}if(n.allowBigInt!==!0)throw new Error(`${f} integers outside of the safe integer range are not supported`);return new a(s.negint,ze-BigInt(o),9)}function ye(e,t){let r=t.value,n=typeof r=="bigint"?r*ze-Ut:r*-1-1;w(e,t.type.majorEncoded,n)}ye.encodedSize=function(t){let r=t.value,n=typeof r=="bigint"?r*ze-Ut:r*-1-1;return n<g[0]?1:n<g[1]?2:n<g[2]?3:n<g[3]?5:9};ye.compareTokens=function(t,r){return t.value<r.value?1:t.value>r.value?-1:0};function ue(e,t,r,n){V(e,t,r+n);let o=W(e,t+r,t+r+n);return new a(s.bytes,o,r+n)}function At(e,t,r,n){return ue(e,t,1,r)}function kt(e,t,r,n){return ue(e,t,2,E(e,t+1,n))}function Tt(e,t,r,n){return ue(e,t,3,U(e,t+1,n))}function St(e,t,r,n){return ue(e,t,5,v(e,t+1,n))}function Bt(e,t,r,n){let o=A(e,t+1,n);if(typeof o=="bigint")throw new Error(`${f} 64-bit integer bytes lengths not supported`);return ue(e,t,9,o)}function ge(e){return e.encodedBytes===void 0&&(e.encodedBytes=e.type===s.string?ft(e.value):e.value),e.encodedBytes}function Z(e,t){let r=ge(t);w(e,t.type.majorEncoded,r.length),e.push(r)}Z.encodedSize=function(t){let r=ge(t);return w.encodedSize(r.length)+r.length};Z.compareTokens=function(t,r){return an(ge(t),ge(r))};function an(e,t){return e.length<t.length?-1:e.length>t.length?1:ht(e,t)}function de(e,t,r,n,o){let i=r+n;V(e,t,i);let u=new a(s.string,dt(e,t+r,t+i),i);return o.retainStringBytes===!0&&(u.byteValue=W(e,t+r,t+i)),u}function Nt(e,t,r,n){return de(e,t,1,r,n)}function _t(e,t,r,n){return de(e,t,2,E(e,t+1,n),n)}function It(e,t,r,n){return de(e,t,3,U(e,t+1,n),n)}function $t(e,t,r,n){return de(e,t,5,v(e,t+1,n),n)}function Ct(e,t,r,n){let o=A(e,t+1,n);if(typeof o=="bigint")throw new Error(`${f} 64-bit integer string lengths not supported`);return de(e,t,9,o,n)}var Mt=Z;function Y(e,t,r,n){return new a(s.array,n,r)}function zt(e,t,r,n){return Y(e,t,1,r)}function Ot(e,t,r,n){return Y(e,t,2,E(e,t+1,n))}function Dt(e,t,r,n){return Y(e,t,3,U(e,t+1,n))}function Ft(e,t,r,n){return Y(e,t,5,v(e,t+1,n))}function Vt(e,t,r,n){let o=A(e,t+1,n);if(typeof o=="bigint")throw new Error(`${f} 64-bit integer array lengths not supported`);return Y(e,t,9,o)}function Lt(e,t,r,n){if(n.allowIndefinite===!1)throw new Error(`${f} indefinite length items not allowed`);return Y(e,t,1,1/0)}function we(e,t){w(e,s.array.majorEncoded,t.value)}we.compareTokens=T.compareTokens;we.encodedSize=function(t){return w.encodedSize(t.value)};function ee(e,t,r,n){return new a(s.map,n,r)}function Rt(e,t,r,n){return ee(e,t,1,r)}function Pt(e,t,r,n){return ee(e,t,2,E(e,t+1,n))}function jt(e,t,r,n){return ee(e,t,3,U(e,t+1,n))}function qt(e,t,r,n){return ee(e,t,5,v(e,t+1,n))}function Gt(e,t,r,n){let o=A(e,t+1,n);if(typeof o=="bigint")throw new Error(`${f} 64-bit integer map lengths not supported`);return ee(e,t,9,o)}function Jt(e,t,r,n){if(n.allowIndefinite===!1)throw new Error(`${f} indefinite length items not allowed`);return ee(e,t,1,1/0)}function be(e,t){w(e,s.map.majorEncoded,t.value)}be.compareTokens=T.compareTokens;be.encodedSize=function(t){return w.encodedSize(t.value)};function Kt(e,t,r,n){return new a(s.tag,r,1)}function Ht(e,t,r,n){return new a(s.tag,E(e,t+1,n),2)}function Xt(e,t,r,n){return new a(s.tag,U(e,t+1,n),3)}function Qt(e,t,r,n){return new a(s.tag,v(e,t+1,n),5)}function Wt(e,t,r,n){return new a(s.tag,A(e,t+1,n),9)}function xe(e,t){w(e,s.tag.majorEncoded,t.value)}xe.compareTokens=T.compareTokens;xe.encodedSize=function(t){return w.encodedSize(t.value)};var hn=20,mn=21,yn=22,gn=23;function Zt(e,t,r,n){if(n.allowUndefined===!1)throw new Error(`${f} undefined values are not supported`);return n.coerceUndefinedToNull===!0?new a(s.null,null,1):new a(s.undefined,void 0,1)}function Yt(e,t,r,n){if(n.allowIndefinite===!1)throw new Error(`${f} indefinite length items not allowed`);return new a(s.break,void 0,1)}function Oe(e,t,r){if(r){if(r.allowNaN===!1&&Number.isNaN(e))throw new Error(`${f} NaN values are not supported`);if(r.allowInfinity===!1&&(e===1/0||e===-1/0))throw new Error(`${f} Infinity values are not supported`)}return new a(s.float,e,t)}function er(e,t,r,n){return Oe(De(e,t+1),3,n)}function tr(e,t,r,n){return Oe(Fe(e,t+1),5,n)}function rr(e,t,r,n){return Oe(sr(e,t+1),9,n)}function Ee(e,t,r){let n=t.value;if(n===!1)e.push([s.float.majorEncoded|hn]);else if(n===!0)e.push([s.float.majorEncoded|mn]);else if(n===null)e.push([s.float.majorEncoded|yn]);else if(n===void 0)e.push([s.float.majorEncoded|gn]);else{let o,i=!1;(!r||r.float64!==!0)&&(or(n),o=De(_,1),n===o||Number.isNaN(n)?(_[0]=249,e.push(_.slice(0,3)),i=!0):(ir(n),o=Fe(_,1),n===o&&(_[0]=250,e.push(_.slice(0,5)),i=!0))),i||(wn(n),o=sr(_,1),_[0]=251,e.push(_.slice(0,9)))}}Ee.encodedSize=function(t,r){let n=t.value;if(n===!1||n===!0||n===null||n===void 0)return 1;if(!r||r.float64!==!0){or(n);let o=De(_,1);if(n===o||Number.isNaN(n))return 3;if(ir(n),o=Fe(_,1),n===o)return 5}return 9};var nr=new ArrayBuffer(9),S=new DataView(nr,1),_=new Uint8Array(nr,0);function or(e){if(e===1/0)S.setUint16(0,31744,!1);else if(e===-1/0)S.setUint16(0,64512,!1);else if(Number.isNaN(e))S.setUint16(0,32256,!1);else{S.setFloat32(0,e);let t=S.getUint32(0),r=(t&2139095040)>>23,n=t&8388607;if(r===255)S.setUint16(0,31744,!1);else if(r===0)S.setUint16(0,(e&2147483648)>>16|n>>13,!1);else{let o=r-127;o<-24?S.setUint16(0,0):o<-14?S.setUint16(0,(t&2147483648)>>16|1<<24+o,!1):S.setUint16(0,(t&2147483648)>>16|o+15<<10|n>>13,!1)}}}function De(e,t){if(e.length-t<2)throw new Error(`${f} not enough data for float16`);let r=(e[t]<<8)+e[t+1];if(r===31744)return 1/0;if(r===64512)return-1/0;if(r===32256)return NaN;let n=r>>10&31,o=r&1023,i;return n===0?i=o*2**-24:n!==31?i=(o+1024)*2**(n-25):i=o===0?1/0:NaN,r&32768?-i:i}function ir(e){S.setFloat32(0,e,!1)}function Fe(e,t){if(e.length-t<4)throw new Error(`${f} not enough data for float32`);let r=(e.byteOffset||0)+t;return new DataView(e.buffer,r,4).getFloat32(0,!1)}function wn(e){S.setFloat64(0,e,!1)}function sr(e,t){if(e.length-t<8)throw new Error(`${f} not enough data for float64`);let r=(e.byteOffset||0)+t;return new DataView(e.buffer,r,8).getFloat64(0,!1)}Ee.compareTokens=T.compareTokens;function l(e,t,r){throw new Error(`${f} encountered invalid minor (${r}) for major ${e[t]>>>5}`)}function Ue(e){return()=>{throw new Error(`${f} ${e}`)}}var c=[];for(let e=0;e<=23;e++)c[e]=l;c[24]=mt;c[25]=yt;c[26]=gt;c[27]=wt;c[28]=l;c[29]=l;c[30]=l;c[31]=l;for(let e=32;e<=55;e++)c[e]=l;c[56]=bt;c[57]=xt;c[58]=Et;c[59]=vt;c[60]=l;c[61]=l;c[62]=l;c[63]=l;for(let e=64;e<=87;e++)c[e]=At;c[88]=kt;c[89]=Tt;c[90]=St;c[91]=Bt;c[92]=l;c[93]=l;c[94]=l;c[95]=Ue("indefinite length bytes/strings are not supported");for(let e=96;e<=119;e++)c[e]=Nt;c[120]=_t;c[121]=It;c[122]=$t;c[123]=Ct;c[124]=l;c[125]=l;c[126]=l;c[127]=Ue("indefinite length bytes/strings are not supported");for(let e=128;e<=151;e++)c[e]=zt;c[152]=Ot;c[153]=Dt;c[154]=Ft;c[155]=Vt;c[156]=l;c[157]=l;c[158]=l;c[159]=Lt;for(let e=160;e<=183;e++)c[e]=Rt;c[184]=Pt;c[185]=jt;c[186]=qt;c[187]=Gt;c[188]=l;c[189]=l;c[190]=l;c[191]=Jt;for(let e=192;e<=215;e++)c[e]=Kt;c[216]=Ht;c[217]=Xt;c[218]=Qt;c[219]=Wt;c[220]=l;c[221]=l;c[222]=l;c[223]=l;for(let e=224;e<=243;e++)c[e]=Ue("simple values are not supported");c[244]=l;c[245]=l;c[246]=l;c[247]=Zt;c[248]=Ue("simple values are not supported");c[249]=er;c[250]=tr;c[251]=rr;c[252]=l;c[253]=l;c[254]=l;c[255]=Yt;var I=[];for(let e=0;e<24;e++)I[e]=new a(s.uint,e,1);for(let e=-1;e>=-24;e--)I[31-e]=new a(s.negint,e,1);I[64]=new a(s.bytes,new Uint8Array(0),1);I[96]=new a(s.string,"",1);I[128]=new a(s.array,0,1);I[160]=new a(s.map,0,1);I[244]=new a(s.false,!1,1);I[245]=new a(s.true,!0,1);I[246]=new a(s.null,null,1);function cr(e){switch(e.type){case s.false:return O([244]);case s.true:return O([245]);case s.null:return O([246]);case s.bytes:return e.value.length?void 0:O([64]);case s.string:return e.value===""?O([96]):void 0;case s.array:return e.value===0?O([128]):void 0;case s.map:return e.value===0?O([160]):void 0;case s.uint:return e.value<24?O([Number(e.value)]):void 0;case s.negint:if(e.value>=-24)return O([31-Number(e.value)])}}var xn={float64:!1,mapSorter:vn,quickEncodeToken:cr};function En(){let e=[];return e[s.uint.major]=T,e[s.negint.major]=ye,e[s.bytes.major]=Z,e[s.string.major]=Mt,e[s.array.major]=we,e[s.map.major]=be,e[s.tag.major]=xe,e[s.float.major]=Ee,e}var ar=En(),Ve=new ce,te=class{constructor(t,r){this.obj=t,this.parent=r}includes(t){let r=this;do if(r.obj===t)return!0;while(r=r.parent);return!1}static createCheck(t,r){if(t&&t.includes(r))throw new Error(`${Me} object contains circular references`);return new te(r,t)}},R={null:new a(s.null,null),undefined:new a(s.undefined,void 0),true:new a(s.true,!0),false:new a(s.false,!1),emptyArray:new a(s.array,0),emptyMap:new a(s.map,0)},P={number(e,t,r,n){return!Number.isInteger(e)||!Number.isSafeInteger(e)?new a(s.float,e):e>=0?new a(s.uint,e):new a(s.negint,e)},bigint(e,t,r,n){return e>=BigInt(0)?new a(s.uint,e):new a(s.negint,e)},Uint8Array(e,t,r,n){return new a(s.bytes,e)},string(e,t,r,n){return new a(s.string,e)},boolean(e,t,r,n){return e?R.true:R.false},null(e,t,r,n){return R.null},undefined(e,t,r,n){return R.undefined},ArrayBuffer(e,t,r,n){return new a(s.bytes,new Uint8Array(e))},DataView(e,t,r,n){return new a(s.bytes,new Uint8Array(e.buffer,e.byteOffset,e.byteLength))},Array(e,t,r,n){if(!e.length)return r.addBreakTokens===!0?[R.emptyArray,new a(s.break)]:R.emptyArray;n=te.createCheck(n,e);let o=[],i=0;for(let u of e)o[i++]=ve(u,r,n);return r.addBreakTokens?[new a(s.array,e.length),o,new a(s.break)]:[new a(s.array,e.length),o]},Object(e,t,r,n){let o=t!=="Object",i=o?e.keys():Object.keys(e),u=o?e.size:i.length;if(!u)return r.addBreakTokens===!0?[R.emptyMap,new a(s.break)]:R.emptyMap;n=te.createCheck(n,e);let d=[],p=0;for(let b of i)d[p++]=[ve(b,r,n),ve(o?e.get(b):e[b],r,n)];return Un(d,r),r.addBreakTokens?[new a(s.map,u),d,new a(s.break)]:[new a(s.map,u),d]}};P.Map=P.Object;P.Buffer=P.Uint8Array;for(let e of"Uint8Clamped Uint16 Uint32 Int8 Int16 Int32 BigUint64 BigInt64 Float32 Float64".split(" "))P[`${e}Array`]=P.DataView;function ve(e,t={},r){let n=st(e),o=t&&t.typeEncoders&&t.typeEncoders[n]||P[n];if(typeof o=="function"){let u=o(e,n,t,r);if(u!=null)return u}let i=P[n];if(!i)throw new Error(`${Me} unsupported type: ${n}`);return i(e,n,t,r)}function Un(e,t){t.mapSorter&&e.sort(t.mapSorter)}function vn(e,t){let r=Array.isArray(e[0])?e[0][0]:e[0],n=Array.isArray(t[0])?t[0][0]:t[0];if(r.type!==n.type)return r.type.compare(n.type);let o=r.type.major,i=ar[o].compareTokens(r,n);return i===0&&console.warn("WARNING: complex key types used, CBOR key sorting guarantees are gone"),i}function ur(e,t,r,n){if(Array.isArray(t))for(let o of t)ur(e,o,r,n);else r[t.type.major](e,t,n)}function An(e,t,r){let n=ve(e,r);if(!Array.isArray(n)&&r.quickEncodeToken){let o=r.quickEncodeToken(n);if(o)return o;let i=t[n.type.major];if(i.encodedSize){let u=i.encodedSize(n,r),d=new ce(u);if(i(d,n,r),d.chunks.length!==1)throw new Error(`Unexpected error: pre-calculated length for ${n} was wrong`);return Ce(d.chunks[0])}}return Ve.reset(),ur(Ve,n,t,r),Ve.toBytes(!0)}function Le(e,t){return t=Object.assign({},xn,t),An(e,ar,t)}var kn={strict:!1,allowIndefinite:!0,allowUndefined:!0,allowBigInt:!0},Re=class{constructor(t,r={}){this.pos=0,this.data=t,this.options=r}done(){return this.pos>=this.data.length}next(){let t=this.data[this.pos],r=I[t];if(r===void 0){let n=c[t];if(!n)throw new Error(`${f} no decoder for major type ${t>>>5} (byte 0x${t.toString(16).padStart(2,"0")})`);let o=t&31;r=n(this.data,this.pos,o,this.options)}return this.pos+=r.encodedLength,r}},fe=Symbol.for("DONE"),Ae=Symbol.for("BREAK");function Tn(e,t,r){let n=[];for(let o=0;o<e.value;o++){let i=le(t,r);if(i===Ae){if(e.value===1/0)break;throw new Error(`${f} got unexpected break to lengthed array`)}if(i===fe)throw new Error(`${f} found array but not enough entries (got ${o}, expected ${e.value})`);n[o]=i}return n}function Sn(e,t,r){let n=r.useMaps===!0,o=n?void 0:{},i=n?new Map:void 0;for(let u=0;u<e.value;u++){let d=le(t,r);if(d===Ae){if(e.value===1/0)break;throw new Error(`${f} got unexpected break to lengthed map`)}if(d===fe)throw new Error(`${f} found map but not enough entries (got ${u} [no key], expected ${e.value})`);if(n!==!0&&typeof d!="string")throw new Error(`${f} non-string keys not supported (got ${typeof d})`);if(r.rejectDuplicateMapKeys===!0&&(n&&i.has(d)||!n&&d in o))throw new Error(`${f} found repeat map key "${d}"`);let p=le(t,r);if(p===fe)throw new Error(`${f} found map but not enough entries (got ${u} [no value], expected ${e.value})`);n?i.set(d,p):o[d]=p}return n?i:o}function le(e,t){if(e.done())return fe;let r=e.next();if(r.type===s.break)return Ae;if(r.type.terminal)return r.value;if(r.type===s.array)return Tn(r,e,t);if(r.type===s.map)return Sn(r,e,t);if(r.type===s.tag){if(t.tags&&typeof t.tags[r.value]=="function"){let n=le(e,t);return t.tags[r.value](n)}throw new Error(`${f} tag not supported (${r.value})`)}throw new Error("unsupported")}function Pe(e,t){if(!(e instanceof Uint8Array))throw new Error(`${f} data to decode must be a Uint8Array`);t=Object.assign({},kn,t);let r=t.tokenizer||new Re(e,t),n=le(r,t);if(n===fe)throw new Error(`${f} did not find any content to decode`);if(n===Ae)throw new Error(`${f} got unexpected break`);if(!r.done())throw new Error(`${f} too many terminals, data makes no sense`);return n}var dr=42;function Nn(e){if(e.asCID!==e&&e["/"]!==e.bytes)return null;let t=y.asCID(e);if(!t)return null;let r=new Uint8Array(t.bytes.byteLength+1);return r.set(t.bytes,1),[new a(s.tag,dr),new a(s.bytes,r)]}function _n(){throw new Error("`undefined` is not supported by the IPLD Data Model and cannot be encoded")}function In(e){if(Number.isNaN(e))throw new Error("`NaN` is not supported by the IPLD Data Model and cannot be encoded");if(e===1/0||e===-1/0)throw new Error("`Infinity` and `-Infinity` is not supported by the IPLD Data Model and cannot be encoded");return null}var $n={float64:!0,typeEncoders:{Object:Nn,undefined:_n,number:In}};function Cn(e){if(e[0]!==0)throw new Error("Invalid CID for CBOR tag 42; expected leading 0x00");return y.decode(e.subarray(1))}var fr={allowIndefinite:!1,coerceUndefinedToNull:!0,allowNaN:!1,allowInfinity:!1,allowBigInt:!0,strict:!0,useMaps:!1,rejectDuplicateMapKeys:!0,tags:[]};fr.tags[dr]=Cn;var lr=113,pr=e=>Le(e,$n),hr=e=>Pe(e,fr);var qe=({name:e,code:t,encode:r})=>new je(e,t,r),je=class{constructor(t,r,n){this.name=t,this.code=r,this.encode=n}digest(t){if(t instanceof Uint8Array){let r=this.encode(t);return r instanceof Uint8Array?oe(this.code,r):r.then(n=>oe(this.code,n))}else throw Error("Unknown type, must be binary type")}};var mr=e=>async t=>new Uint8Array(await crypto.subtle.digest(e,t)),yr=qe({name:"sha2-256",code:18,encode:mr("SHA-256")}),li=qe({name:"sha2-512",code:19,encode:mr("SHA-512")});var Ge=class{components;constructor(t){this.components=t}async add(t,r={}){let n=pr(t),o=await(r.hasher??yr).digest(n),i=y.createV1(lr,o);return await this.components.blockstore.put(i,n,r),i}async get(t,r={}){let n=await this.components.blockstore.get(t,r);return hr(n)}};function zn(e){return new Ge(e)}return vr(On);})();
|
|
3
|
+
return HeliaDagCbor}));
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* `@helia/dag-cbor` makes working with DAG-JSON {@link https://github.com/ipfs/helia Helia} simple & straightforward.
|
|
5
|
+
*
|
|
6
|
+
* See the {@link Strings Strings interface} for all available operations.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { createHelia } from 'helia'
|
|
12
|
+
* import { dagCbor } from '@helia/dag-cbor'
|
|
13
|
+
* import { CID } from 'multiformats/cid'
|
|
14
|
+
*
|
|
15
|
+
* const d = dagCbor(helia)
|
|
16
|
+
* const cid = await d.put({
|
|
17
|
+
* hello: 'world'
|
|
18
|
+
* })
|
|
19
|
+
* const obj = await d.get(cid)
|
|
20
|
+
*
|
|
21
|
+
* console.info(obj)
|
|
22
|
+
* // { hello: 'world' }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
import { CID } from 'multiformats/cid';
|
|
26
|
+
import type { Blocks, GetBlockProgressEvents, PutBlockProgressEvents } from '@helia/interface/blocks';
|
|
27
|
+
import type { AbortOptions } from '@libp2p/interfaces';
|
|
28
|
+
import type { BlockCodec } from 'multiformats/codecs/interface';
|
|
29
|
+
import type { MultihashHasher } from 'multiformats/hashes/interface';
|
|
30
|
+
import type { ProgressOptions } from 'progress-events';
|
|
31
|
+
export interface DAGCBORComponents {
|
|
32
|
+
blockstore: Blocks;
|
|
33
|
+
}
|
|
34
|
+
export interface AddOptions extends AbortOptions, ProgressOptions<PutBlockProgressEvents> {
|
|
35
|
+
hasher: MultihashHasher;
|
|
36
|
+
}
|
|
37
|
+
export interface GetOptions extends AbortOptions, ProgressOptions<GetBlockProgressEvents> {
|
|
38
|
+
codec: BlockCodec<any, unknown>;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The JSON interface provides a simple and intuitive way to add/get objects
|
|
42
|
+
* with your Helia node and is a great place to start learning about IPFS.
|
|
43
|
+
*/
|
|
44
|
+
export interface DAGCBOR {
|
|
45
|
+
/**
|
|
46
|
+
* Add an object to your Helia node and get a CID that refers to the block the
|
|
47
|
+
* object has been stored as.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
*
|
|
51
|
+
* ```typescript
|
|
52
|
+
* import { json } from '@helia/dag-cbor'
|
|
53
|
+
*
|
|
54
|
+
* const j = json(helia)
|
|
55
|
+
* const cid = await str.add({ hello: 'world' })
|
|
56
|
+
*
|
|
57
|
+
* console.info(cid)
|
|
58
|
+
* // CID(bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae)
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
add: (str: unknown, options?: Partial<AddOptions>) => Promise<CID>;
|
|
62
|
+
/**
|
|
63
|
+
* Get an object from your Helia node, either previously added to it or to
|
|
64
|
+
* another node on the network.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
*
|
|
68
|
+
* ```typescript
|
|
69
|
+
* import { json } from '@helia/dag-cbor'
|
|
70
|
+
* import { CID } from 'multiformats/cid'
|
|
71
|
+
*
|
|
72
|
+
* const j = json(helia)
|
|
73
|
+
* const cid = CID.parse('bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae')
|
|
74
|
+
* const obj = await j.get(cid)
|
|
75
|
+
*
|
|
76
|
+
* console.info(obj)
|
|
77
|
+
* // { hello: 'world' }
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
get: <T>(cid: CID, options?: Partial<GetOptions>) => Promise<T>;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Create a {@link DAGCBOR} instance for use with {@link https://github.com/ipfs/helia Helia}
|
|
84
|
+
*/
|
|
85
|
+
export declare function dagCbor(helia: {
|
|
86
|
+
blockstore: Blocks;
|
|
87
|
+
}): DAGCBOR;
|
|
88
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAGtC,OAAO,KAAK,EAAE,MAAM,EAAE,sBAAsB,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAA;AACrG,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACtD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAA;AAC/D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AACpE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEtD,MAAM,WAAW,iBAAiB;IAChC,UAAU,EAAE,MAAM,CAAA;CACnB;AAED,MAAM,WAAW,UAAW,SAAQ,YAAY,EAAE,eAAe,CAAC,sBAAsB,CAAC;IACvF,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,MAAM,WAAW,UAAW,SAAQ,YAAY,EAAE,eAAe,CAAC,sBAAsB,CAAC;IACvF,KAAK,EAAE,UAAU,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;CAChC;AAED;;;GAGG;AACH,MAAM,WAAW,OAAO;IACtB;;;;;;;;;;;;;;;OAeG;IACH,GAAG,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,KAAK,OAAO,CAAC,GAAG,CAAC,CAAA;IAElE;;;;;;;;;;;;;;;;;OAiBG;IACH,GAAG,EAAE,CAAC,CAAC,EAAG,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;CACjE;AA0BD;;GAEG;AACH,wBAAgB,OAAO,CAAE,KAAK,EAAE;IAAE,UAAU,EAAE,MAAM,CAAA;CAAE,GAAG,OAAO,CAE/D"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* `@helia/dag-cbor` makes working with DAG-JSON {@link https://github.com/ipfs/helia Helia} simple & straightforward.
|
|
5
|
+
*
|
|
6
|
+
* See the {@link Strings Strings interface} for all available operations.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { createHelia } from 'helia'
|
|
12
|
+
* import { dagCbor } from '@helia/dag-cbor'
|
|
13
|
+
* import { CID } from 'multiformats/cid'
|
|
14
|
+
*
|
|
15
|
+
* const d = dagCbor(helia)
|
|
16
|
+
* const cid = await d.put({
|
|
17
|
+
* hello: 'world'
|
|
18
|
+
* })
|
|
19
|
+
* const obj = await d.get(cid)
|
|
20
|
+
*
|
|
21
|
+
* console.info(obj)
|
|
22
|
+
* // { hello: 'world' }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
import { CID } from 'multiformats/cid';
|
|
26
|
+
import * as codec from '@ipld/dag-cbor';
|
|
27
|
+
import { sha256 } from 'multiformats/hashes/sha2';
|
|
28
|
+
class DefaultDAGCBOR {
|
|
29
|
+
components;
|
|
30
|
+
constructor(components) {
|
|
31
|
+
this.components = components;
|
|
32
|
+
}
|
|
33
|
+
async add(obj, options = {}) {
|
|
34
|
+
const buf = codec.encode(obj);
|
|
35
|
+
const hash = await (options.hasher ?? sha256).digest(buf);
|
|
36
|
+
const cid = CID.createV1(codec.code, hash);
|
|
37
|
+
await this.components.blockstore.put(cid, buf, options);
|
|
38
|
+
return cid;
|
|
39
|
+
}
|
|
40
|
+
async get(cid, options = {}) {
|
|
41
|
+
const buf = await this.components.blockstore.get(cid, options);
|
|
42
|
+
return codec.decode(buf);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Create a {@link DAGCBOR} instance for use with {@link https://github.com/ipfs/helia Helia}
|
|
47
|
+
*/
|
|
48
|
+
export function dagCbor(helia) {
|
|
49
|
+
return new DefaultDAGCBOR(helia);
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,KAAK,KAAK,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AA+DjD,MAAM,cAAc;IACD,UAAU,CAAmB;IAE9C,YAAa,UAA6B;QACxC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;IAC9B,CAAC;IAED,KAAK,CAAC,GAAG,CAAE,GAAQ,EAAE,UAA+B,EAAE;QACpD,MAAM,GAAG,GAAG,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QAC7B,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,IAAI,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QACzD,MAAM,GAAG,GAAG,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;QAE1C,MAAM,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,CAAA;QAEvD,OAAO,GAAG,CAAA;IACZ,CAAC;IAED,KAAK,CAAC,GAAG,CAAM,GAAQ,EAAE,UAA+B,EAAE;QACxD,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;QAE9D,OAAO,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IAC1B,CAAC;CACF;AAED;;GAEG;AACH,MAAM,UAAU,OAAO,CAAE,KAA6B;IACpD,OAAO,IAAI,cAAc,CAAC,KAAK,CAAC,CAAA;AAClC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@helia/dag-cbor",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Add/get IPLD blocks containing dag-cbor with your Helia node",
|
|
5
|
+
"license": "Apache-2.0 OR MIT",
|
|
6
|
+
"homepage": "https://github.com/ipfs/helia-dag-cbor/tree/master/packages/dag-cbor#readme",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/ipfs/helia-dag-cbor.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/ipfs/helia-dag-cbor/issues"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"IPFS"
|
|
16
|
+
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=16.0.0",
|
|
19
|
+
"npm": ">=7.0.0"
|
|
20
|
+
},
|
|
21
|
+
"type": "module",
|
|
22
|
+
"types": "./dist/src/index.d.ts",
|
|
23
|
+
"files": [
|
|
24
|
+
"src",
|
|
25
|
+
"dist",
|
|
26
|
+
"!dist/test",
|
|
27
|
+
"!**/*.tsbuildinfo"
|
|
28
|
+
],
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/src/index.d.ts",
|
|
32
|
+
"import": "./dist/src/index.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"eslintConfig": {
|
|
36
|
+
"extends": "ipfs",
|
|
37
|
+
"parserOptions": {
|
|
38
|
+
"sourceType": "module"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"release": {
|
|
42
|
+
"branches": [
|
|
43
|
+
"main"
|
|
44
|
+
],
|
|
45
|
+
"plugins": [
|
|
46
|
+
[
|
|
47
|
+
"@semantic-release/commit-analyzer",
|
|
48
|
+
{
|
|
49
|
+
"preset": "conventionalcommits",
|
|
50
|
+
"releaseRules": [
|
|
51
|
+
{
|
|
52
|
+
"breaking": true,
|
|
53
|
+
"release": "major"
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
"revert": true,
|
|
57
|
+
"release": "patch"
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"type": "feat",
|
|
61
|
+
"release": "minor"
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
"type": "fix",
|
|
65
|
+
"release": "patch"
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
"type": "docs",
|
|
69
|
+
"release": "patch"
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
"type": "test",
|
|
73
|
+
"release": "patch"
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
"type": "deps",
|
|
77
|
+
"release": "patch"
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
"scope": "no-release",
|
|
81
|
+
"release": false
|
|
82
|
+
}
|
|
83
|
+
]
|
|
84
|
+
}
|
|
85
|
+
],
|
|
86
|
+
[
|
|
87
|
+
"@semantic-release/release-notes-generator",
|
|
88
|
+
{
|
|
89
|
+
"preset": "conventionalcommits",
|
|
90
|
+
"presetConfig": {
|
|
91
|
+
"types": [
|
|
92
|
+
{
|
|
93
|
+
"type": "feat",
|
|
94
|
+
"section": "Features"
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
"type": "fix",
|
|
98
|
+
"section": "Bug Fixes"
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
"type": "chore",
|
|
102
|
+
"section": "Trivial Changes"
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
"type": "docs",
|
|
106
|
+
"section": "Documentation"
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
"type": "deps",
|
|
110
|
+
"section": "Dependencies"
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
"type": "test",
|
|
114
|
+
"section": "Tests"
|
|
115
|
+
}
|
|
116
|
+
]
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
],
|
|
120
|
+
"@semantic-release/changelog",
|
|
121
|
+
"@semantic-release/npm",
|
|
122
|
+
"@semantic-release/github",
|
|
123
|
+
"@semantic-release/git"
|
|
124
|
+
]
|
|
125
|
+
},
|
|
126
|
+
"scripts": {
|
|
127
|
+
"clean": "aegir clean",
|
|
128
|
+
"lint": "aegir lint",
|
|
129
|
+
"dep-check": "aegir dep-check",
|
|
130
|
+
"build": "aegir build",
|
|
131
|
+
"docs": "aegir docs",
|
|
132
|
+
"test": "aegir test",
|
|
133
|
+
"test:chrome": "aegir test -t browser --cov",
|
|
134
|
+
"test:chrome-webworker": "aegir test -t webworker",
|
|
135
|
+
"test:firefox": "aegir test -t browser -- --browser firefox",
|
|
136
|
+
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
|
|
137
|
+
"test:node": "aegir test -t node --cov",
|
|
138
|
+
"test:electron-main": "aegir test -t electron-main",
|
|
139
|
+
"release": "aegir release"
|
|
140
|
+
},
|
|
141
|
+
"dependencies": {
|
|
142
|
+
"@helia/interface": "^1.0.0",
|
|
143
|
+
"@ipld/dag-cbor": "^9.0.0",
|
|
144
|
+
"@libp2p/interfaces": "^3.3.1",
|
|
145
|
+
"interface-blockstore": "^5.0.0",
|
|
146
|
+
"multiformats": "^11.0.1",
|
|
147
|
+
"progress-events": "^1.0.0"
|
|
148
|
+
},
|
|
149
|
+
"devDependencies": {
|
|
150
|
+
"aegir": "^39.0.3",
|
|
151
|
+
"blockstore-core": "^4.0.1"
|
|
152
|
+
},
|
|
153
|
+
"typedoc": {
|
|
154
|
+
"entryPoint": "./src/index.ts"
|
|
155
|
+
}
|
|
156
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* `@helia/dag-cbor` makes working with DAG-JSON {@link https://github.com/ipfs/helia Helia} simple & straightforward.
|
|
5
|
+
*
|
|
6
|
+
* See the {@link Strings Strings interface} for all available operations.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { createHelia } from 'helia'
|
|
12
|
+
* import { dagCbor } from '@helia/dag-cbor'
|
|
13
|
+
* import { CID } from 'multiformats/cid'
|
|
14
|
+
*
|
|
15
|
+
* const d = dagCbor(helia)
|
|
16
|
+
* const cid = await d.put({
|
|
17
|
+
* hello: 'world'
|
|
18
|
+
* })
|
|
19
|
+
* const obj = await d.get(cid)
|
|
20
|
+
*
|
|
21
|
+
* console.info(obj)
|
|
22
|
+
* // { hello: 'world' }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import * as codec from '@ipld/dag-cbor'
|
|
27
|
+
import { CID } from 'multiformats/cid'
|
|
28
|
+
import { sha256 } from 'multiformats/hashes/sha2'
|
|
29
|
+
import type { Blocks, GetBlockProgressEvents, PutBlockProgressEvents } from '@helia/interface/blocks'
|
|
30
|
+
import type { AbortOptions } from '@libp2p/interfaces'
|
|
31
|
+
import type { BlockCodec } from 'multiformats/codecs/interface'
|
|
32
|
+
import type { MultihashHasher } from 'multiformats/hashes/interface'
|
|
33
|
+
import type { ProgressOptions } from 'progress-events'
|
|
34
|
+
|
|
35
|
+
export interface DAGCBORComponents {
|
|
36
|
+
blockstore: Blocks
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface AddOptions extends AbortOptions, ProgressOptions<PutBlockProgressEvents> {
|
|
40
|
+
hasher: MultihashHasher
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface GetOptions extends AbortOptions, ProgressOptions<GetBlockProgressEvents> {
|
|
44
|
+
codec: BlockCodec<any, unknown>
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The JSON interface provides a simple and intuitive way to add/get objects
|
|
49
|
+
* with your Helia node and is a great place to start learning about IPFS.
|
|
50
|
+
*/
|
|
51
|
+
export interface DAGCBOR {
|
|
52
|
+
/**
|
|
53
|
+
* Add an object to your Helia node and get a CID that refers to the block the
|
|
54
|
+
* object has been stored as.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
*
|
|
58
|
+
* ```typescript
|
|
59
|
+
* import { json } from '@helia/dag-cbor'
|
|
60
|
+
*
|
|
61
|
+
* const j = json(helia)
|
|
62
|
+
* const cid = await str.add({ hello: 'world' })
|
|
63
|
+
*
|
|
64
|
+
* console.info(cid)
|
|
65
|
+
* // CID(bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae)
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
add: (str: unknown, options?: Partial<AddOptions>) => Promise<CID>
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Get an object from your Helia node, either previously added to it or to
|
|
72
|
+
* another node on the network.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
*
|
|
76
|
+
* ```typescript
|
|
77
|
+
* import { json } from '@helia/dag-cbor'
|
|
78
|
+
* import { CID } from 'multiformats/cid'
|
|
79
|
+
*
|
|
80
|
+
* const j = json(helia)
|
|
81
|
+
* const cid = CID.parse('bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae')
|
|
82
|
+
* const obj = await j.get(cid)
|
|
83
|
+
*
|
|
84
|
+
* console.info(obj)
|
|
85
|
+
* // { hello: 'world' }
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
get: <T> (cid: CID, options?: Partial<GetOptions>) => Promise<T>
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
class DefaultDAGCBOR implements DAGCBOR {
|
|
92
|
+
private readonly components: DAGCBORComponents
|
|
93
|
+
|
|
94
|
+
constructor (components: DAGCBORComponents) {
|
|
95
|
+
this.components = components
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async add (obj: any, options: Partial<AddOptions> = {}): Promise<CID> {
|
|
99
|
+
const buf = codec.encode(obj)
|
|
100
|
+
const hash = await (options.hasher ?? sha256).digest(buf)
|
|
101
|
+
const cid = CID.createV1(codec.code, hash)
|
|
102
|
+
|
|
103
|
+
await this.components.blockstore.put(cid, buf, options)
|
|
104
|
+
|
|
105
|
+
return cid
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async get <T> (cid: CID, options: Partial<GetOptions> = {}): Promise<T> {
|
|
109
|
+
const buf = await this.components.blockstore.get(cid, options)
|
|
110
|
+
|
|
111
|
+
return codec.decode(buf)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Create a {@link DAGCBOR} instance for use with {@link https://github.com/ipfs/helia Helia}
|
|
117
|
+
*/
|
|
118
|
+
export function dagCbor (helia: { blockstore: Blocks }): DAGCBOR {
|
|
119
|
+
return new DefaultDAGCBOR(helia)
|
|
120
|
+
}
|