@helia/strings 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 +67 -0
- package/dist/index.min.js +3 -0
- package/dist/src/index.d.ts +87 -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 +154 -0
- package/src/index.ts +122 -0
package/LICENSE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# @helia/strings <!-- omit in toc -->
|
|
2
|
+
|
|
3
|
+
[](https://ipfs.tech)
|
|
4
|
+
[](https://discuss.ipfs.tech)
|
|
5
|
+
[](https://codecov.io/gh/ipfs/helia-strings)
|
|
6
|
+
[](https://github.com/ipfs/helia-strings/actions/workflows/js-test-and-release.yml?query=branch%3Amain)
|
|
7
|
+
|
|
8
|
+
> Add/get IPLD blocks containing strings with your Helia node
|
|
9
|
+
|
|
10
|
+
## Table of contents <!-- omit in toc -->
|
|
11
|
+
|
|
12
|
+
- - [Install](#install)
|
|
13
|
+
- [Browser `<script>` tag](#browser-script-tag)
|
|
14
|
+
- [@helia/unixfs <!-- omit in toc -->](#heliaunixfs----omit-in-toc---)
|
|
15
|
+
- [API Docs](#api-docs)
|
|
16
|
+
- [License](#license)
|
|
17
|
+
- [Contribute](#contribute)
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```console
|
|
22
|
+
$ npm i @helia/strings
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Browser `<script>` tag
|
|
26
|
+
|
|
27
|
+
Loading this module through a script tag will make it's exports available as `HeliaStrings` in the global namespace.
|
|
28
|
+
|
|
29
|
+
```html
|
|
30
|
+
<script src="https://unpkg.com/@helia/strings/dist/index.min.js"></script>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
<p align="center">
|
|
34
|
+
<a href="https://github.com/ipfs/helia" title="Helia">
|
|
35
|
+
<img src="https://raw.githubusercontent.com/ipfs/helia/main/assets/helia.png" alt="Helia logo" width="300" />
|
|
36
|
+
</a>
|
|
37
|
+
</p>
|
|
38
|
+
|
|
39
|
+
# @helia/unixfs <!-- omit in toc -->
|
|
40
|
+
|
|
41
|
+
[](https://ipfs.tech)
|
|
42
|
+
[](https://discuss.ipfs.tech)
|
|
43
|
+
[](https://codecov.io/gh/ipfs/helia-unixfs)
|
|
44
|
+
[](https://github.com/ipfs/helia-unixfs/actions/workflows/js-test-and-release.yml?query=branch%3Amain)
|
|
45
|
+
|
|
46
|
+
## API Docs
|
|
47
|
+
|
|
48
|
+
- <https://ipfs.github.io/helia-strings/modules/_helia_strings.html>
|
|
49
|
+
|
|
50
|
+
## License
|
|
51
|
+
|
|
52
|
+
Licensed under either of
|
|
53
|
+
|
|
54
|
+
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
|
|
55
|
+
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
|
|
56
|
+
|
|
57
|
+
## Contribute
|
|
58
|
+
|
|
59
|
+
Contributions welcome! Please check out [the issues](https://github.com/ipfs/helia-strings/issues).
|
|
60
|
+
|
|
61
|
+
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.
|
|
62
|
+
|
|
63
|
+
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).
|
|
64
|
+
|
|
65
|
+
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.
|
|
66
|
+
|
|
67
|
+
[](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.HeliaStrings = factory()}(typeof self !== 'undefined' ? self : this, function () {
|
|
2
|
+
"use strict";var HeliaStrings=(()=>{var X=Object.defineProperty;var je=Object.getOwnPropertyDescriptor;var qe=Object.getOwnPropertyNames;var Re=Object.prototype.hasOwnProperty;var u=(r,e)=>{for(var t in e)X(r,t,{get:e[t],enumerable:!0})},Je=(r,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of qe(e))!Re.call(r,n)&&n!==t&&X(r,n,{get:()=>e[n],enumerable:!(o=je(e,n))||o.enumerable});return r};var Xe=r=>Je(X({},"__esModule",{value:!0}),r);var or={};u(or,{strings:()=>rr});var Qe=we,be=128,Ge=127,Ke=~Ge,He=Math.pow(2,31);function we(r,e,t){e=e||[],t=t||0;for(var o=t;r>=He;)e[t++]=r&255|be,r/=128;for(;r&Ke;)e[t++]=r&255|be,r>>>=7;return e[t]=r|0,we.bytes=t-o+1,e}var We=Q,Ze=128,xe=127;function Q(r,o){var t=0,o=o||0,n=0,s=o,i,c=r.length;do{if(s>=c)throw Q.bytes=0,new RangeError("Could not decode varint");i=r[s++],t+=n<28?(i&xe)<<n:(i&xe)*Math.pow(2,n),n+=7}while(i>=Ze);return Q.bytes=s-o,t}var Ye=Math.pow(2,7),_e=Math.pow(2,14),et=Math.pow(2,21),tt=Math.pow(2,28),rt=Math.pow(2,35),ot=Math.pow(2,42),nt=Math.pow(2,49),st=Math.pow(2,56),it=Math.pow(2,63),at=function(r){return r<Ye?1:r<_e?2:r<et?3:r<tt?4:r<rt?5:r<ot?6:r<nt?7:r<st?8:r<it?9:10},ct={encode:Qe,decode:We,encodingLength:at},ft=ct,V=ft;var P=(r,e=0)=>[V.decode(r,e),V.decode.bytes],D=(r,e,t=0)=>(V.encode(r,e,t),e),O=r=>V.encodingLength(r);var ar=new Uint8Array(0);var ge=(r,e)=>{if(r===e)return!0;if(r.byteLength!==e.byteLength)return!1;for(let t=0;t<r.byteLength;t++)if(r[t]!==e[t])return!1;return!0},w=r=>{if(r instanceof Uint8Array&&r.constructor.name==="Uint8Array")return r;if(r instanceof ArrayBuffer)return new Uint8Array(r);if(ArrayBuffer.isView(r))return new Uint8Array(r.buffer,r.byteOffset,r.byteLength);throw new Error("Unknown type, must be binary type")};var ye=r=>new TextEncoder().encode(r),ve=r=>new TextDecoder().decode(r);var T=(r,e)=>{let t=e.byteLength,o=O(r),n=o+O(t),s=new Uint8Array(n+t);return D(r,s,0),D(t,s,o),s.set(e,n),new M(r,t,e,s)},Se=r=>{let e=w(r),[t,o]=P(e),[n,s]=P(e.subarray(o)),i=e.subarray(o+s);if(i.byteLength!==n)throw new Error("Incorrect length");return new M(t,n,i,e)},Ee=(r,e)=>{if(r===e)return!0;{let t=e;return r.code===t.code&&r.size===t.size&&t.bytes instanceof Uint8Array&&ge(r.bytes,t.bytes)}},M=class{constructor(e,t,o,n){this.code=e,this.size=t,this.digest=o,this.bytes=n}};var Z={};u(Z,{base58btc:()=>A,base58flickr:()=>mt});function dt(r,e){if(r.length>=255)throw new TypeError("Alphabet too long");for(var t=new Uint8Array(256),o=0;o<t.length;o++)t[o]=255;for(var n=0;n<r.length;n++){var s=r.charAt(n),i=s.charCodeAt(0);if(t[i]!==255)throw new TypeError(s+" is ambiguous");t[i]=n}var c=r.length,h=r.charAt(0),B=Math.log(c)/Math.log(256),l=Math.log(256)/Math.log(c);function z(a){if(a instanceof Uint8Array||(ArrayBuffer.isView(a)?a=new Uint8Array(a.buffer,a.byteOffset,a.byteLength):Array.isArray(a)&&(a=Uint8Array.from(a))),!(a instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(a.length===0)return"";for(var p=0,C=0,b=0,g=a.length;b!==g&&a[b]===0;)b++,p++;for(var y=(g-b)*l+1>>>0,m=new Uint8Array(y);b!==g;){for(var v=a[b],E=0,x=y-1;(v!==0||E<C)&&x!==-1;x--,E++)v+=256*m[x]>>>0,m[x]=v%c>>>0,v=v/c>>>0;if(v!==0)throw new Error("Non-zero carry");C=E,b++}for(var S=y-C;S!==y&&m[S]===0;)S++;for(var $=h.repeat(p);S<y;++S)$+=r.charAt(m[S]);return $}function F(a){if(typeof a!="string")throw new TypeError("Expected String");if(a.length===0)return new Uint8Array;var p=0;if(a[p]!==" "){for(var C=0,b=0;a[p]===h;)C++,p++;for(var g=(a.length-p)*B+1>>>0,y=new Uint8Array(g);a[p];){var m=t[a.charCodeAt(p)];if(m===255)return;for(var v=0,E=g-1;(m!==0||v<b)&&E!==-1;E--,v++)m+=c*y[E]>>>0,y[E]=m%256>>>0,m=m/256>>>0;if(m!==0)throw new Error("Non-zero carry");b=v,p++}if(a[p]!==" "){for(var x=g-b;x!==g&&y[x]===0;)x++;for(var S=new Uint8Array(C+(g-x)),$=C;x!==g;)S[$++]=y[x++];return S}}}function $e(a){var p=F(a);if(p)return p;throw new Error(`Non-${e} character`)}return{encode:z,decodeUnsafe:F,decode:$e}}var ht=dt,pt=ht,Ue=pt;var G=class{constructor(e,t,o){this.name=e,this.prefix=t,this.baseEncode=o}encode(e){if(e instanceof Uint8Array)return`${this.prefix}${this.baseEncode(e)}`;throw Error("Unknown type, must be binary type")}},K=class{constructor(e,t,o){if(this.name=e,this.prefix=t,t.codePointAt(0)===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=t.codePointAt(0),this.baseDecode=o}decode(e){if(typeof e=="string"){if(e.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(e)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(e.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(e){return Ce(this,e)}},H=class{constructor(e){this.decoders=e}or(e){return Ce(this,e)}decode(e){let t=e[0],o=this.decoders[t];if(o)return o.decode(e);throw RangeError(`Unable to decode multibase string ${JSON.stringify(e)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}},Ce=(r,e)=>new H({...r.decoders||{[r.prefix]:r},...e.decoders||{[e.prefix]:e}}),W=class{constructor(e,t,o,n){this.name=e,this.prefix=t,this.baseEncode=o,this.baseDecode=n,this.encoder=new G(e,t,o),this.decoder=new K(e,t,n)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}},N=({name:r,prefix:e,encode:t,decode:o})=>new W(r,e,t,o),U=({prefix:r,name:e,alphabet:t})=>{let{encode:o,decode:n}=Ue(t,e);return N({prefix:r,name:e,encode:o,decode:s=>w(n(s))})},ut=(r,e,t,o)=>{let n={};for(let l=0;l<e.length;++l)n[e[l]]=l;let s=r.length;for(;r[s-1]==="=";)--s;let i=new Uint8Array(s*t/8|0),c=0,h=0,B=0;for(let l=0;l<s;++l){let z=n[r[l]];if(z===void 0)throw new SyntaxError(`Non-${o} character`);h=h<<t|z,c+=t,c>=8&&(c-=8,i[B++]=255&h>>c)}if(c>=t||255&h<<8-c)throw new SyntaxError("Unexpected end of data");return i},lt=(r,e,t)=>{let o=e[e.length-1]==="=",n=(1<<t)-1,s="",i=0,c=0;for(let h=0;h<r.length;++h)for(c=c<<8|r[h],i+=8;i>t;)i-=t,s+=e[n&c>>i];if(i&&(s+=e[n&c<<t-i]),o)for(;s.length*t&7;)s+="=";return s},f=({name:r,prefix:e,bitsPerChar:t,alphabet:o})=>N({prefix:e,name:r,encode(n){return lt(n,o,t)},decode(n){return ut(n,o,t,r)}});var A=U({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),mt=U({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Y={};u(Y,{base32:()=>L,base32hex:()=>gt,base32hexpad:()=>vt,base32hexpadupper:()=>At,base32hexupper:()=>yt,base32pad:()=>xt,base32padupper:()=>wt,base32upper:()=>bt,base32z:()=>St});var L=f({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),bt=f({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),xt=f({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),wt=f({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),gt=f({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),yt=f({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),vt=f({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),At=f({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),St=f({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var Te=(r,e)=>{let{bytes:t,version:o}=r;switch(o){case 0:return Ut(t,_(r),e||A.encoder);default:return Ct(t,_(r),e||L.encoder)}};var Be=new WeakMap,_=r=>{let e=Be.get(r);if(e==null){let t=new Map;return Be.set(r,t),t}return e},d=class{constructor(e,t,o,n){this.code=t,this.version=e,this.multihash=o,this.bytes=n,this["/"]=n}get asCID(){return this}get byteOffset(){return this.bytes.byteOffset}get byteLength(){return this.bytes.byteLength}toV0(){switch(this.version){case 0:return this;case 1:{let{code:e,multihash:t}=this;if(e!==k)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(t.code!==Tt)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return d.createV0(t)}default:throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`)}}toV1(){switch(this.version){case 0:{let{code:e,digest:t}=this.multihash,o=T(e,t);return d.createV1(this.code,o)}case 1:return this;default:throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`)}}equals(e){return d.equals(this,e)}static equals(e,t){let o=t;return o&&e.code===o.code&&e.version===o.version&&Ee(e.multihash,o.multihash)}toString(e){return Te(this,e)}toJSON(){return{"/":Te(this)}}link(){return this}get[Symbol.toStringTag](){return"CID"}[Symbol.for("nodejs.util.inspect.custom")](){return`CID(${this.toString()})`}static asCID(e){if(e==null)return null;let t=e;if(t instanceof d)return t;if(t["/"]!=null&&t["/"]===t.bytes||t.asCID===t){let{version:o,code:n,multihash:s,bytes:i}=t;return new d(o,n,s,i||ze(o,n,s.bytes))}else if(t[Bt]===!0){let{version:o,multihash:n,code:s}=t,i=Se(n);return d.create(o,s,i)}else return null}static create(e,t,o){if(typeof t!="number")throw new Error("String codecs are no longer supported");if(!(o.bytes instanceof Uint8Array))throw new Error("Invalid digest");switch(e){case 0:{if(t!==k)throw new Error(`Version 0 CID must use dag-pb (code: ${k}) block encoding`);return new d(e,t,o,o.bytes)}case 1:{let n=ze(e,t,o.bytes);return new d(e,t,o,n)}default:throw new Error("Invalid version")}}static createV0(e){return d.create(0,k,e)}static createV1(e,t){return d.create(1,e,t)}static decode(e){let[t,o]=d.decodeFirst(e);if(o.length)throw new Error("Incorrect length");return t}static decodeFirst(e){let t=d.inspectBytes(e),o=t.size-t.multihashSize,n=w(e.subarray(o,o+t.multihashSize));if(n.byteLength!==t.multihashSize)throw new Error("Incorrect length");let s=n.subarray(t.multihashSize-t.digestSize),i=new M(t.multihashCode,t.digestSize,s,n);return[t.version===0?d.createV0(i):d.createV1(t.codec,i),e.subarray(t.size)]}static inspectBytes(e){let t=0,o=()=>{let[z,F]=P(e.subarray(t));return t+=F,z},n=o(),s=k;if(n===18?(n=0,t=0):s=o(),n!==0&&n!==1)throw new RangeError(`Invalid CID version ${n}`);let i=t,c=o(),h=o(),B=t+h,l=B-i;return{version:n,codec:s,multihashCode:c,digestSize:h,multihashSize:l,size:B}}static parse(e,t){let[o,n]=Et(e,t),s=d.decode(n);if(s.version===0&&e[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return _(s).set(o,e),s}},Et=(r,e)=>{switch(r[0]){case"Q":{let t=e||A;return[A.prefix,t.decode(`${A.prefix}${r}`)]}case A.prefix:{let t=e||A;return[A.prefix,t.decode(r)]}case L.prefix:{let t=e||L;return[L.prefix,t.decode(r)]}default:{if(e==null)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[r[0],e.decode(r)]}}},Ut=(r,e,t)=>{let{prefix:o}=t;if(o!==A.prefix)throw Error(`Cannot string encode V0 in ${t.name} encoding`);let n=e.get(o);if(n==null){let s=t.encode(r).slice(1);return e.set(o,s),s}else return n},Ct=(r,e,t)=>{let{prefix:o}=t,n=e.get(o);if(n==null){let s=t.encode(r);return e.set(o,s),s}else return n},k=112,Tt=18,ze=(r,e,t)=>{let o=O(r),n=o+O(e),s=new Uint8Array(n+t.byteLength);return D(r,s,0),D(e,s,o),s.set(t,n),s},Bt=Symbol.for("@ipld/js-cid/CID");var oe={};u(oe,{sha256:()=>re,sha512:()=>zt});var te=({name:r,code:e,encode:t})=>new ee(r,e,t),ee=class{constructor(e,t,o){this.name=e,this.code=t,this.encode=o}digest(e){if(e instanceof Uint8Array){let t=this.encode(e);return t instanceof Uint8Array?T(this.code,t):t.then(o=>T(this.code,o))}else throw Error("Unknown type, must be binary type")}};var Oe=r=>async e=>new Uint8Array(await crypto.subtle.digest(r,e)),re=te({name:"sha2-256",code:18,encode:Oe("SHA-256")}),zt=te({name:"sha2-512",code:19,encode:Oe("SHA-512")});var q={};u(q,{code:()=>Ot,decode:()=>Nt,encode:()=>Mt,name:()=>Dt});var Dt="raw",Ot=85,Mt=r=>w(r),Nt=r=>w(r);var ne={};u(ne,{identity:()=>Lt});var Lt=N({prefix:"\0",name:"identity",encode:r=>ve(r),decode:r=>ye(r)});var se={};u(se,{base2:()=>Vt});var Vt=f({prefix:"0",name:"base2",alphabet:"01",bitsPerChar:1});var ie={};u(ie,{base8:()=>Pt});var Pt=f({prefix:"7",name:"base8",alphabet:"01234567",bitsPerChar:3});var ae={};u(ae,{base10:()=>It});var It=U({prefix:"9",name:"base10",alphabet:"0123456789"});var ce={};u(ce,{base16:()=>kt,base16upper:()=>Ft});var kt=f({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4}),Ft=f({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4});var fe={};u(fe,{base36:()=>$t,base36upper:()=>jt});var $t=U({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),jt=U({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var de={};u(de,{base64:()=>qt,base64pad:()=>Rt,base64url:()=>Jt,base64urlpad:()=>Xt});var qt=f({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),Rt=f({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),Jt=f({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),Xt=f({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var he={};u(he,{base256emoji:()=>Wt});var Me=Array.from("\u{1F680}\u{1FA90}\u2604\u{1F6F0}\u{1F30C}\u{1F311}\u{1F312}\u{1F313}\u{1F314}\u{1F315}\u{1F316}\u{1F317}\u{1F318}\u{1F30D}\u{1F30F}\u{1F30E}\u{1F409}\u2600\u{1F4BB}\u{1F5A5}\u{1F4BE}\u{1F4BF}\u{1F602}\u2764\u{1F60D}\u{1F923}\u{1F60A}\u{1F64F}\u{1F495}\u{1F62D}\u{1F618}\u{1F44D}\u{1F605}\u{1F44F}\u{1F601}\u{1F525}\u{1F970}\u{1F494}\u{1F496}\u{1F499}\u{1F622}\u{1F914}\u{1F606}\u{1F644}\u{1F4AA}\u{1F609}\u263A\u{1F44C}\u{1F917}\u{1F49C}\u{1F614}\u{1F60E}\u{1F607}\u{1F339}\u{1F926}\u{1F389}\u{1F49E}\u270C\u2728\u{1F937}\u{1F631}\u{1F60C}\u{1F338}\u{1F64C}\u{1F60B}\u{1F497}\u{1F49A}\u{1F60F}\u{1F49B}\u{1F642}\u{1F493}\u{1F929}\u{1F604}\u{1F600}\u{1F5A4}\u{1F603}\u{1F4AF}\u{1F648}\u{1F447}\u{1F3B6}\u{1F612}\u{1F92D}\u2763\u{1F61C}\u{1F48B}\u{1F440}\u{1F62A}\u{1F611}\u{1F4A5}\u{1F64B}\u{1F61E}\u{1F629}\u{1F621}\u{1F92A}\u{1F44A}\u{1F973}\u{1F625}\u{1F924}\u{1F449}\u{1F483}\u{1F633}\u270B\u{1F61A}\u{1F61D}\u{1F634}\u{1F31F}\u{1F62C}\u{1F643}\u{1F340}\u{1F337}\u{1F63B}\u{1F613}\u2B50\u2705\u{1F97A}\u{1F308}\u{1F608}\u{1F918}\u{1F4A6}\u2714\u{1F623}\u{1F3C3}\u{1F490}\u2639\u{1F38A}\u{1F498}\u{1F620}\u261D\u{1F615}\u{1F33A}\u{1F382}\u{1F33B}\u{1F610}\u{1F595}\u{1F49D}\u{1F64A}\u{1F639}\u{1F5E3}\u{1F4AB}\u{1F480}\u{1F451}\u{1F3B5}\u{1F91E}\u{1F61B}\u{1F534}\u{1F624}\u{1F33C}\u{1F62B}\u26BD\u{1F919}\u2615\u{1F3C6}\u{1F92B}\u{1F448}\u{1F62E}\u{1F646}\u{1F37B}\u{1F343}\u{1F436}\u{1F481}\u{1F632}\u{1F33F}\u{1F9E1}\u{1F381}\u26A1\u{1F31E}\u{1F388}\u274C\u270A\u{1F44B}\u{1F630}\u{1F928}\u{1F636}\u{1F91D}\u{1F6B6}\u{1F4B0}\u{1F353}\u{1F4A2}\u{1F91F}\u{1F641}\u{1F6A8}\u{1F4A8}\u{1F92C}\u2708\u{1F380}\u{1F37A}\u{1F913}\u{1F619}\u{1F49F}\u{1F331}\u{1F616}\u{1F476}\u{1F974}\u25B6\u27A1\u2753\u{1F48E}\u{1F4B8}\u2B07\u{1F628}\u{1F31A}\u{1F98B}\u{1F637}\u{1F57A}\u26A0\u{1F645}\u{1F61F}\u{1F635}\u{1F44E}\u{1F932}\u{1F920}\u{1F927}\u{1F4CC}\u{1F535}\u{1F485}\u{1F9D0}\u{1F43E}\u{1F352}\u{1F617}\u{1F911}\u{1F30A}\u{1F92F}\u{1F437}\u260E\u{1F4A7}\u{1F62F}\u{1F486}\u{1F446}\u{1F3A4}\u{1F647}\u{1F351}\u2744\u{1F334}\u{1F4A3}\u{1F438}\u{1F48C}\u{1F4CD}\u{1F940}\u{1F922}\u{1F445}\u{1F4A1}\u{1F4A9}\u{1F450}\u{1F4F8}\u{1F47B}\u{1F910}\u{1F92E}\u{1F3BC}\u{1F975}\u{1F6A9}\u{1F34E}\u{1F34A}\u{1F47C}\u{1F48D}\u{1F4E3}\u{1F942}"),Qt=Me.reduce((r,e,t)=>(r[t]=e,r),[]),Gt=Me.reduce((r,e,t)=>(r[e.codePointAt(0)]=t,r),[]);function Kt(r){return r.reduce((e,t)=>(e+=Qt[t],e),"")}function Ht(r){let e=[];for(let t of r){let o=Gt[t.codePointAt(0)];if(o===void 0)throw new Error(`Non-base256emoji character: ${t}`);e.push(o)}return new Uint8Array(e)}var Wt=N({prefix:"\u{1F680}",name:"base256emoji",encode:Kt,decode:Ht});var pe={};u(pe,{identity:()=>_t});var Ne=0,Zt="identity",Le=w,Yt=r=>T(Ne,Le(r)),_t={code:Ne,name:Zt,encode:Le,digest:Yt};var Mr=new TextEncoder,Nr=new TextDecoder;var ue={...ne,...se,...ie,...ae,...ce,...Y,...fe,...Z,...de,...he},kr={...oe,...pe};function R(r){return globalThis.Buffer!=null?new Uint8Array(r.buffer,r.byteOffset,r.byteLength):r}function Ve(r=0){return globalThis.Buffer?.allocUnsafe!=null?R(globalThis.Buffer.allocUnsafe(r)):new Uint8Array(r)}function Ie(r,e,t,o){return{name:r,prefix:e,encoder:{name:r,prefix:e,encode:t},decoder:{decode:o}}}var Pe=Ie("utf8","u",r=>"u"+new TextDecoder("utf8").decode(r),r=>new TextEncoder().encode(r.substring(1))),le=Ie("ascii","a",r=>{let e="a";for(let t=0;t<r.length;t++)e+=String.fromCharCode(r[t]);return e},r=>{r=r.substring(1);let e=Ve(r.length);for(let t=0;t<r.length;t++)e[t]=r.charCodeAt(t);return e}),tr={utf8:Pe,"utf-8":Pe,hex:ue.base16,latin1:le,ascii:le,binary:le,...ue},J=tr;function ke(r,e="utf8"){let t=J[e];if(t==null)throw new Error(`Unsupported encoding "${e}"`);return(e==="utf8"||e==="utf-8")&&globalThis.Buffer!=null&&globalThis.Buffer.from!=null?R(globalThis.Buffer.from(r,"utf-8")):t.decoder.decode(`${t.prefix}${r}`)}function Fe(r,e="utf8"){let t=J[e];if(t==null)throw new Error(`Unsupported encoding "${e}"`);return(e==="utf8"||e==="utf-8")&&globalThis.Buffer!=null&&globalThis.Buffer.from!=null?globalThis.Buffer.from(r.buffer,r.byteOffset,r.byteLength).toString("utf8"):t.encoder.encode(r).substring(1)}var me=class{constructor(e){this.components=e}async add(e,t={}){let o=ke(e),n=await(t.hasher??re).digest(o),s=t.codec??q,i=d.createV1(s.code,n);return await this.components.blockstore.put(i,o,t),i}async get(e,t={}){let o=await this.components.blockstore.get(e,t);return Fe(o)}};function rr(r){return new me(r)}return Xe(or);})();
|
|
3
|
+
return HeliaStrings}));
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* `@helia/strings` makes working with strings {@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 { strings } from '@helia/strings'
|
|
13
|
+
* import { CID } from 'multiformats/cid'
|
|
14
|
+
*
|
|
15
|
+
* const str = strings(helia)
|
|
16
|
+
* const cid = await str.put('hello world')
|
|
17
|
+
* const string = await str.get(cid)
|
|
18
|
+
*
|
|
19
|
+
* console.info(string)
|
|
20
|
+
* // hello world
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
import { CID } from 'multiformats/cid';
|
|
24
|
+
import type { Blocks, GetBlockProgressEvents, PutBlockProgressEvents } from '@helia/interface/blocks';
|
|
25
|
+
import type { AbortOptions } from '@libp2p/interfaces';
|
|
26
|
+
import type { ProgressOptions } from 'progress-events';
|
|
27
|
+
import type { BlockCodec } from 'multiformats/codecs/interface';
|
|
28
|
+
import type { MultihashHasher } from 'multiformats/hashes/interface';
|
|
29
|
+
export interface StringsComponents {
|
|
30
|
+
blockstore: Blocks;
|
|
31
|
+
}
|
|
32
|
+
export interface AddOptions extends AbortOptions, ProgressOptions<PutBlockProgressEvents> {
|
|
33
|
+
hasher: MultihashHasher;
|
|
34
|
+
codec: BlockCodec<any, unknown>;
|
|
35
|
+
}
|
|
36
|
+
export interface GetOptions extends AbortOptions, ProgressOptions<GetBlockProgressEvents> {
|
|
37
|
+
codec: BlockCodec<any, unknown>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The UnixFS interface provides familiar filesystem operations to make working with
|
|
41
|
+
* UnixFS DAGs simple and intuitive.
|
|
42
|
+
*/
|
|
43
|
+
export interface Strings {
|
|
44
|
+
/**
|
|
45
|
+
* Add a string to your Helia node and get a CID that refers to the block the
|
|
46
|
+
* string has been stored as.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
*
|
|
50
|
+
* ```typescript
|
|
51
|
+
* import { strings } from '@helia/strings'
|
|
52
|
+
*
|
|
53
|
+
* const str = strings(helia)
|
|
54
|
+
* const cid = await str.add('hello world')
|
|
55
|
+
*
|
|
56
|
+
* console.info(cid)
|
|
57
|
+
* // CID(QmFoo)
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
add: (str: string, options?: Partial<AddOptions>) => Promise<CID>;
|
|
61
|
+
/**
|
|
62
|
+
* Get a string from your Helia node, either previously added to it or to
|
|
63
|
+
* another node on the network.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
*
|
|
67
|
+
* ```typescript
|
|
68
|
+
* import { strings } from '@helia/strings'
|
|
69
|
+
* import { CID } from 'multiformats/cid'
|
|
70
|
+
*
|
|
71
|
+
* const str = strings(helia)
|
|
72
|
+
* const cid = CID.parse('')
|
|
73
|
+
* const string = await str.get(cid)
|
|
74
|
+
*
|
|
75
|
+
* console.info(string)
|
|
76
|
+
* // hello world
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
get: (cid: CID, options?: Partial<GetOptions>) => Promise<string>;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Create a {@link Strings} instance for use with {@link https://github.com/ipfs/helia Helia}
|
|
83
|
+
*/
|
|
84
|
+
export declare function strings(helia: {
|
|
85
|
+
blockstore: Blocks;
|
|
86
|
+
}): Strings;
|
|
87
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,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,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAKtD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAA;AAC/D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAEpE,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;IACvB,KAAK,EAAE,UAAU,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;CAChC;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,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,KAAK,OAAO,CAAC,GAAG,CAAC,CAAA;IAEjE;;;;;;;;;;;;;;;;;OAiBG;IACH,GAAG,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;CAClE;AA2BD;;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/strings` makes working with strings {@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 { strings } from '@helia/strings'
|
|
13
|
+
* import { CID } from 'multiformats/cid'
|
|
14
|
+
*
|
|
15
|
+
* const str = strings(helia)
|
|
16
|
+
* const cid = await str.put('hello world')
|
|
17
|
+
* const string = await str.get(cid)
|
|
18
|
+
*
|
|
19
|
+
* console.info(string)
|
|
20
|
+
* // hello world
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
import { CID } from 'multiformats/cid';
|
|
24
|
+
import { sha256 } from 'multiformats/hashes/sha2';
|
|
25
|
+
import * as raw from 'multiformats/codecs/raw';
|
|
26
|
+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
27
|
+
import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
|
|
28
|
+
class DefaultStrings {
|
|
29
|
+
constructor(components) {
|
|
30
|
+
this.components = components;
|
|
31
|
+
}
|
|
32
|
+
async add(string, options = {}) {
|
|
33
|
+
const buf = uint8ArrayFromString(string);
|
|
34
|
+
const hash = await (options.hasher ?? sha256).digest(buf);
|
|
35
|
+
const codec = options.codec ?? raw;
|
|
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 uint8ArrayToString(buf);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Create a {@link Strings} instance for use with {@link https://github.com/ipfs/helia Helia}
|
|
47
|
+
*/
|
|
48
|
+
export function strings(helia) {
|
|
49
|
+
return new DefaultStrings(helia);
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAItC,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AACjD,OAAO,KAAK,GAAG,MAAM,yBAAyB,CAAA;AAC9C,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AA6DtE,MAAM,cAAc;IAGlB,YAAa,UAA6B;QACxC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;IAC9B,CAAC;IAED,KAAK,CAAC,GAAG,CAAE,MAAc,EAAE,UAA+B,EAAE;QAC1D,MAAM,GAAG,GAAG,oBAAoB,CAAC,MAAM,CAAC,CAAA;QACxC,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,IAAI,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QACzD,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,GAAG,CAAA;QAClC,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,CAAE,GAAQ,EAAE,UAA+B,EAAE;QACpD,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;QAE9D,OAAO,kBAAkB,CAAC,GAAG,CAAC,CAAA;IAChC,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,154 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@helia/strings",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Add/get IPLD blocks containing strings with your Helia node",
|
|
5
|
+
"license": "Apache-2.0 OR MIT",
|
|
6
|
+
"homepage": "https://github.com/ipfs/helia-strings/tree/master/packages/strings#readme",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/ipfs/helia-strings.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/ipfs/helia-strings/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
|
+
"interface-blockstore": "^5.0.0",
|
|
143
|
+
"multiformats": "^11.0.1",
|
|
144
|
+
"progress-events": "^1.0.0",
|
|
145
|
+
"uint8arrays": "^4.0.3"
|
|
146
|
+
},
|
|
147
|
+
"devDependencies": {
|
|
148
|
+
"aegir": "^38.1.0",
|
|
149
|
+
"blockstore-core": "^4.0.1"
|
|
150
|
+
},
|
|
151
|
+
"typedoc": {
|
|
152
|
+
"entryPoint": "./src/index.ts"
|
|
153
|
+
}
|
|
154
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* `@helia/strings` makes working with strings {@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 { strings } from '@helia/strings'
|
|
13
|
+
* import { CID } from 'multiformats/cid'
|
|
14
|
+
*
|
|
15
|
+
* const str = strings(helia)
|
|
16
|
+
* const cid = await str.put('hello world')
|
|
17
|
+
* const string = await str.get(cid)
|
|
18
|
+
*
|
|
19
|
+
* console.info(string)
|
|
20
|
+
* // hello world
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { CID } from 'multiformats/cid'
|
|
25
|
+
import type { Blocks, GetBlockProgressEvents, PutBlockProgressEvents } from '@helia/interface/blocks'
|
|
26
|
+
import type { AbortOptions } from '@libp2p/interfaces'
|
|
27
|
+
import type { ProgressOptions } from 'progress-events'
|
|
28
|
+
import { sha256 } from 'multiformats/hashes/sha2'
|
|
29
|
+
import * as raw from 'multiformats/codecs/raw'
|
|
30
|
+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
31
|
+
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
32
|
+
import type { BlockCodec } from 'multiformats/codecs/interface'
|
|
33
|
+
import type { MultihashHasher } from 'multiformats/hashes/interface'
|
|
34
|
+
|
|
35
|
+
export interface StringsComponents {
|
|
36
|
+
blockstore: Blocks
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface AddOptions extends AbortOptions, ProgressOptions<PutBlockProgressEvents> {
|
|
40
|
+
hasher: MultihashHasher
|
|
41
|
+
codec: BlockCodec<any, unknown>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface GetOptions extends AbortOptions, ProgressOptions<GetBlockProgressEvents> {
|
|
45
|
+
codec: BlockCodec<any, unknown>
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The UnixFS interface provides familiar filesystem operations to make working with
|
|
50
|
+
* UnixFS DAGs simple and intuitive.
|
|
51
|
+
*/
|
|
52
|
+
export interface Strings {
|
|
53
|
+
/**
|
|
54
|
+
* Add a string to your Helia node and get a CID that refers to the block the
|
|
55
|
+
* string has been stored as.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
*
|
|
59
|
+
* ```typescript
|
|
60
|
+
* import { strings } from '@helia/strings'
|
|
61
|
+
*
|
|
62
|
+
* const str = strings(helia)
|
|
63
|
+
* const cid = await str.add('hello world')
|
|
64
|
+
*
|
|
65
|
+
* console.info(cid)
|
|
66
|
+
* // CID(QmFoo)
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
add: (str: string, options?: Partial<AddOptions>) => Promise<CID>
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Get a string from your Helia node, either previously added to it or to
|
|
73
|
+
* another node on the network.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
*
|
|
77
|
+
* ```typescript
|
|
78
|
+
* import { strings } from '@helia/strings'
|
|
79
|
+
* import { CID } from 'multiformats/cid'
|
|
80
|
+
*
|
|
81
|
+
* const str = strings(helia)
|
|
82
|
+
* const cid = CID.parse('')
|
|
83
|
+
* const string = await str.get(cid)
|
|
84
|
+
*
|
|
85
|
+
* console.info(string)
|
|
86
|
+
* // hello world
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
get: (cid: CID, options?: Partial<GetOptions>) => Promise<string>
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
class DefaultStrings implements Strings {
|
|
93
|
+
private readonly components: StringsComponents
|
|
94
|
+
|
|
95
|
+
constructor (components: StringsComponents) {
|
|
96
|
+
this.components = components
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async add (string: string, options: Partial<AddOptions> = {}): Promise<CID> {
|
|
100
|
+
const buf = uint8ArrayFromString(string)
|
|
101
|
+
const hash = await (options.hasher ?? sha256).digest(buf)
|
|
102
|
+
const codec = options.codec ?? raw
|
|
103
|
+
const cid = CID.createV1(codec.code, hash)
|
|
104
|
+
|
|
105
|
+
await this.components.blockstore.put(cid, buf, options)
|
|
106
|
+
|
|
107
|
+
return cid
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async get (cid: CID, options: Partial<GetOptions> = {}): Promise<string> {
|
|
111
|
+
const buf = await this.components.blockstore.get(cid, options)
|
|
112
|
+
|
|
113
|
+
return uint8ArrayToString(buf)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Create a {@link Strings} instance for use with {@link https://github.com/ipfs/helia Helia}
|
|
119
|
+
*/
|
|
120
|
+
export function strings (helia: { blockstore: Blocks }): Strings {
|
|
121
|
+
return new DefaultStrings(helia)
|
|
122
|
+
}
|