-
Notifications
You must be signed in to change notification settings - Fork 72
feat: improve helia 101 example #456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ff9816e
feat: more usage examples for helia unixfs
2color 2561867
deps: update to fix port binding errors
2color 4da90f0
refine eamples
2color f9e4b4f
feat: add an additional example
2color aa5b910
feat: 401 providing, gc and pinning
2color dccedc2
chore: remove unused deps
2color 8b9870a
chore: lint
2color b4c4b59
test: add simple test for 401 example
2color cf020dc
Merge branch 'main' into improve-101-example
achingbrain ef1b14d
deps: upgrade
2color f6cd44a
feat: add extended dag stats
2color 1af65ca
Update examples/helia-101/101-basics.js
2color fbb5678
Update examples/helia-101/101-basics.js
2color 516741e
chore: stop helia so the process exits
2color File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| node_modules | ||
| build | ||
| dist | ||
| blockstore | ||
| datastore | ||
| .docs | ||
| .coverage | ||
| node_modules | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,50 +1,50 @@ | ||
| /* eslint-disable no-console */ | ||
|
|
||
| // @ts-check | ||
| import { createHeliaHTTP } from '@helia/http' | ||
| import { unixfs } from '@helia/unixfs' | ||
| import { MemoryBlockstore } from 'blockstore-core' | ||
| import { createHelia } from 'helia' | ||
| import { FsBlockstore } from 'blockstore-fs' | ||
|
|
||
| // 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 new Helia node with an in-memory blockstore | ||
| const helia1 = await createHeliaHTTP({ | ||
| blockstore: new MemoryBlockstore() | ||
| }) | ||
|
|
||
| // create a filesystem on top of Helia, in this case it's UnixFS | ||
| const fs = unixfs(helia) | ||
| // create a UnixFS filesystem on top of Helia | ||
| const fs1 = unixfs(helia1) | ||
|
|
||
| // we will use this TextEncoder to turn strings into Uint8Arrays | ||
| const encoder = new TextEncoder() | ||
|
|
||
| const message = 'Hello World 201' | ||
|
|
||
| // add the bytes to your node and receive a unique content identifier | ||
| const cid = await fs.addBytes(encoder.encode('Hello World 201')) | ||
| const cid1 = await fs1.addBytes(encoder.encode(message)) | ||
|
|
||
| console.log('Added file:', cid.toString()) | ||
| console.log('Added file contents:', message) | ||
|
|
||
| // create a second Helia node using the same blockstore | ||
| const helia2 = await createHelia({ | ||
| blockstore | ||
| // Create a new Helia node with a filesystem blockstore | ||
| const helia2 = await createHeliaHTTP({ | ||
| blockstore: new FsBlockstore('./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 | ||
| }) | ||
| try { | ||
| // Check if the CID is in the blockstore, which will be true if we ran this script before | ||
| const stats = await fs2.stat(cid1, { offline: true }) // `offline: true` will prevent the node from trying to fetch the block from the network | ||
| console.log(`Found ${cid1.toString()} in blockstore:`, stats) | ||
| } catch (error) { | ||
| console.log("CID can't be found in the blockstore. We will add it now.") | ||
| // If the CID is not in the blockstore, we will add it now | ||
| const cid2 = await fs2.addBytes(encoder.encode(message)) | ||
| console.log('Added file:', cid2.toString()) | ||
| } | ||
|
|
||
| console.log('Added file contents:', text) | ||
| process.exit(0) | ||
2color marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or 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,75 @@ | ||
| /* eslint-disable no-console */ | ||
2color marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // @ts-check | ||
| import { unixfs } from '@helia/unixfs' | ||
| import { createHelia } from 'helia' | ||
|
|
||
| const helia = await createHelia() | ||
|
|
||
| // log when our addresses changes | ||
| helia.libp2p.addEventListener('self:peer:update', (evt) => { | ||
| console.log( | ||
| 'self:peer:update', | ||
| evt.detail.peer.addresses.map((a) => a.multiaddr.toString()) | ||
| ) | ||
| }) | ||
|
|
||
| console.log('Created Helia node with PeerID:', helia.libp2p.peerId.toString()) | ||
|
|
||
| // 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() | ||
|
|
||
| const text = 'Hello World 🗺️🌎🌍🌏 401!' | ||
|
|
||
| // add the bytes to your node and receive a unique content identifier | ||
| let cid = await fs.addFile({ | ||
| content: encoder.encode(text), | ||
| path: './hello-world.txt' | ||
| }) | ||
| console.log('Added file:', cid.toString()) | ||
|
|
||
| // Run garbage collection to remove unpinned blocks | ||
| await helia.gc({ | ||
| onProgress: (evt) => { | ||
| console.info('gc event', evt.type, evt.detail) | ||
| } | ||
| }) | ||
|
|
||
| // This will fail because the block is not pinned | ||
| try { | ||
| const stats = await fs.stat(cid, { offline: true }) // offline to avoid fetching the block from the network | ||
| console.log('Stats:', stats) | ||
| } catch (err) { | ||
| if (err?.name === 'NotFoundError') { | ||
| console.log('Block not found, as expected') | ||
| } else { | ||
| throw err | ||
| } | ||
| } | ||
|
|
||
| // Add the same bytes again, this time we will pin them | ||
| cid = await fs.addFile({ | ||
| content: encoder.encode(text), | ||
| path: './hello-world.txt' | ||
| }) | ||
| console.log('Added file again:', cid.toString()) | ||
|
|
||
| // Pin the block and add some metadata | ||
| for await (const pinnedCid of helia.pins.add(cid, { | ||
| metadata: { | ||
| added: new Date().toISOString(), | ||
| addedBy: '401-providing example' | ||
| } | ||
| })) { | ||
| console.log('Pinned CID to prevent garbage collection:', pinnedCid.toString()) | ||
| } | ||
|
|
||
| const pin = await helia.pins.get(cid) | ||
| console.log('Pin:', pin) | ||
|
|
||
| // Provide the block to the DHT so that other nodes can find and retrieve it | ||
| await helia.routing.provide(cid) | ||
|
|
||
| console.log('CID provided to the DHT:', cid.toString()) | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.