-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: update helia 101, split into separate parts (#28)
Also update all deps
- Loading branch information
1 parent
cf3e81c
commit 7018f2e
Showing
13 changed files
with
293 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { createHelia } from 'helia' | ||
import { unixfs } from '@helia/unixfs' | ||
|
||
// create a Helia node | ||
const helia = await createHelia() | ||
|
||
// create a filesystem on top of Helia, in this case it's UnixFS | ||
const fs = unixfs(helia) | ||
|
||
// we will use this TextEncoder to turn strings into Uint8Arrays | ||
const encoder = new TextEncoder() | ||
|
||
// add the bytes to your node and receive a unique content identifier | ||
const cid = await fs.addBytes(encoder.encode('Hello World 101'), { | ||
onProgress: (evt) => { | ||
console.info('add event', evt.type, evt.detail) | ||
} | ||
}) | ||
|
||
console.log('Added file:', cid.toString()) | ||
|
||
// this decoder will turn Uint8Arrays into strings | ||
const decoder = new TextDecoder() | ||
let text = '' | ||
|
||
for await (const chunk of fs.cat(cid, { | ||
onProgress: (evt) => { | ||
console.info('cat event', evt.type, evt.detail) | ||
} | ||
})) { | ||
text += decoder.decode(chunk, { | ||
stream: true | ||
}) | ||
} | ||
|
||
console.log('Added file contents:', text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { createHelia } from 'helia' | ||
import { unixfs } from '@helia/unixfs' | ||
import { MemoryBlockstore } from 'blockstore-core' | ||
|
||
// the blockstore is where we store the blocks that make up files. this blockstore | ||
// stores everything in-memory - other blockstores are available: | ||
// - https://www.npmjs.com/package/blockstore-fs - a filesystem blockstore (for use in node) | ||
// - https://www.npmjs.com/package/blockstore-idb - an IndexDB blockstore (for use in browsers) | ||
// - https://www.npmjs.com/package/blockstore-level - a LevelDB blockstore (for node or browsers, | ||
// though storing files in a database is rarely a good idea) | ||
const blockstore = new MemoryBlockstore() | ||
|
||
// create a Helia node | ||
const helia = await createHelia({ | ||
blockstore | ||
}) | ||
|
||
// create a filesystem on top of Helia, in this case it's UnixFS | ||
const fs = unixfs(helia) | ||
|
||
// we will use this TextEncoder to turn strings into Uint8Arrays | ||
const encoder = new TextEncoder() | ||
|
||
// add the bytes to your node and receive a unique content identifier | ||
const cid = await fs.addBytes(encoder.encode('Hello World 201')) | ||
|
||
console.log('Added file:', cid.toString()) | ||
|
||
// create a second Helia node using the same blockstore | ||
const helia2 = await createHelia({ | ||
blockstore | ||
}) | ||
|
||
// create a second filesystem | ||
const fs2 = unixfs(helia2) | ||
|
||
// this decoder will turn Uint8Arrays into strings | ||
const decoder = new TextDecoder() | ||
let text = '' | ||
|
||
// read the file from the blockstore using the second Helia node | ||
for await (const chunk of fs2.cat(cid)) { | ||
text += decoder.decode(chunk, { | ||
stream: true | ||
}) | ||
} | ||
|
||
console.log('Added file contents:', text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { createHelia } from 'helia' | ||
import { createLibp2p } from 'libp2p' | ||
import { noise } from '@chainsafe/libp2p-noise' | ||
import { yamux } from '@chainsafe/libp2p-yamux' | ||
import { tcp } from '@libp2p/tcp' | ||
import { bootstrap } from '@libp2p/bootstrap' | ||
import { unixfs } from '@helia/unixfs' | ||
import { MemoryBlockstore } from 'blockstore-core' | ||
import { MemoryDatastore } from 'datastore-core' | ||
|
||
async function createNode () { | ||
// the blockstore is where we store the blocks that make up files | ||
const blockstore = new MemoryBlockstore() | ||
|
||
// application-specific data lives in the datastore | ||
const datastore = new MemoryDatastore() | ||
|
||
// libp2p is the networking layer that underpins Helia | ||
const libp2p = await createLibp2p({ | ||
datastore, | ||
addresses: { | ||
listen: [ | ||
'/ip4/127.0.0.1/tcp/0' | ||
] | ||
}, | ||
transports: [ | ||
tcp() | ||
], | ||
connectionEncryption: [ | ||
noise() | ||
], | ||
streamMuxers: [ | ||
yamux() | ||
], | ||
peerDiscovery: [ | ||
bootstrap({ | ||
list: [ | ||
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", | ||
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa", | ||
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb", | ||
"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt" | ||
] | ||
}) | ||
] | ||
}) | ||
|
||
return await createHelia({ | ||
datastore, | ||
blockstore, | ||
libp2p | ||
}) | ||
} | ||
|
||
// create two helia nodes | ||
const node1 = await createNode() | ||
const node2 = await createNode() | ||
|
||
// connect them together | ||
const multiaddrs = node2.libp2p.getMultiaddrs() | ||
await node1.libp2p.dial(multiaddrs[0]) | ||
|
||
// create a filesystem on top of Helia, in this case it's UnixFS | ||
const fs = unixfs(node1) | ||
|
||
// we will use this TextEncoder to turn strings into Uint8Arrays | ||
const encoder = new TextEncoder() | ||
|
||
// add the bytes to your node and receive a unique content identifier | ||
const cid = await fs.addBytes(encoder.encode('Hello World 301')) | ||
|
||
console.log('Added file:', cid.toString()) | ||
|
||
// create a filesystem on top of the second Helia node | ||
const fs2 = unixfs(node2) | ||
|
||
// this decoder will turn Uint8Arrays into strings | ||
const decoder = new TextDecoder() | ||
let text = '' | ||
|
||
// use the second Helia node to fetch the file from the first Helia node | ||
for await (const chunk of fs2.cat(cid)) { | ||
text += decoder.decode(chunk, { | ||
stream: true | ||
}) | ||
} | ||
|
||
console.log('Fetched file contents:', text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.