Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
trying
Browse files Browse the repository at this point in the history
  • Loading branch information
Weedshaker committed May 3, 2024
1 parent e52b6f8 commit 7af267d
Show file tree
Hide file tree
Showing 11 changed files with 508 additions and 11 deletions.
157 changes: 147 additions & 10 deletions src/controllers/Ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,156 @@
* @return {CustomElementConstructor | *}
*/
export default class Ipfs extends HTMLElement {
constructor() {
super()
this.loadDependency('Helia', 'helia.min.js').then(helia => console.log('controller hooked', {heliaClient: helia}))
this.loadDependency('HeliaUnixfs', 'unixfs.min.js').then(HeliaUnixfs => console.log('controller hooked', {unixfsClient: HeliaUnixfs}))
this.loadDependency('HeliaIpns', 'ipns.min.js').then(HeliaIpns => console.log('controller hooked', {ipnsClient: HeliaIpns}))
this.loadDependency('HeliaJson', 'json.min.js').then(HeliaJson => console.log('controller hooked', {jsonClient: HeliaJson}))
async connectedCallback () {
// https://github.com/ipfs-examples/helia-101/blob/main/301-networking.js

// try next: https://sgtpooki-helia-playground.on.fleek.co/


// create two helia nodes
const node1 = await this.createNode()
//const node2 = await this.createNode()

// connect them together
/*
const multiaddrs = node2.libp2p.getMultiaddrs()
if (multiaddrs[0]) await node1.libp2p.dial(multiaddrs[0])
*/

// create a filesystem on top of Helia, in this case it's UnixFS
const fs = await this.getUnixfs(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 Dude'))

console.log('Added file:', cid.toString())

// create a filesystem on top of the second Helia node
//const fs2 = await this.getUnixfs(node2)
const fs2 = await this.getUnixfs(node1)

// 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)
}

/**
* unixfs
*
* @returns {Promise<any>}
*/
getUnixfs (node = this.createNode()) {
return Promise.all([
this.loadDependency('HeliaUnixfs', 'unixfs.min.js'),
node
]).then(([unixfs, node]) => unixfs.unixfs(node))
}

/**
* ipns
*
* @returns {Promise<any>}
*/
getIpns (node = this.createNode()) {
return Promise.all([
this.loadDependency('HeliaIpns', 'ipns.min.js'),
node
]).then(([ipns, node]) => ipns.ipns(node))
}

/**
* json
*
* @returns {Promise<any>}
*/
getJson (node = this.createNode()) {
return Promise.all([
this.loadDependency('HeliaJson', 'json.min.js'),
node
]).then(([json, node]) => json.json(node))
}

/**
* create helia node
*
* @returns {Promise<any>}
*/
createNode () {
// code adjusted from example https://github.com/ipfs-examples/helia-examples/blob/main/examples/helia-101/README.md
// libp2p
const storesAndLibp2p = Promise.all([
this.loadDependency('ChainsafeLibp2PNoise', 'libp2p-noise.min.js'),
this.loadDependency('ChainsafeLibp2PYamux', 'libp2p-yamux.min.js'),
this.loadDependency('Libp2PBootstrap', 'bootstrap.min.js'),
this.loadDependency('Libp2PIdentify', 'identify.min.js'),
this.loadDependency('Libp2PWebsockets', 'websockets.min.js'),
this.loadDependency('BlockstoreCore', 'blockstore-core.min.js'),
this.loadDependency('DatastoreCore', 'datastore-core.min.js'),
this.loadDependency('Libp2P', 'libp2p.min.js')
]).then(([noise, yamux, bootstrap, identify, websockets, BlockstoreCore, DatastoreCore, libp2p]) => {
const datastore = new DatastoreCore.MemoryDatastore()
const blockstore = new BlockstoreCore.MemoryBlockstore()
return {
datastore,
blockstore,
libp2p: libp2p.createLibp2p({
datastore,
transports: [
websockets.webSockets()
],
connectionEncryption: [
noise.noise()
],
streamMuxers: [
yamux.yamux()
],
peerDiscovery: [
bootstrap.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'
]
})
],
services: {
identify: identify.identify()
}
})
}
})
// helia
return Promise.all([
this.loadDependency('Helia', 'helia.min.js'),
storesAndLibp2p
]).then(([helia, storesAndLibp2p]) => {
const {datastore, blockstore, libp2p} = storesAndLibp2p
return helia.createHelia({
datastore,
blockstore,
libp2p
})
})
}

/**
* fetch dependency
*
* @returns {Promise<any>}
*/
* fetch dependency
*
* @returns {Promise<any>}
*/
loadDependency (globalNamespace, fileName) {
// make it global to self so that other components can know when it has been loaded
return this[`_loadDependency${globalNamespace}`] || (this[`_loadDependency${globalNamespace}`] = new Promise((resolve, reject) => {
Expand Down
1 change: 0 additions & 1 deletion src/helia
Submodule helia deleted from 2d070b
3 changes: 3 additions & 0 deletions src/helia_dist/blockstore-core.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/helia_dist/bootstrap.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/helia_dist/datastore-core.min.js

Large diffs are not rendered by default.

103 changes: 103 additions & 0 deletions src/helia_dist/identify.min.js

Large diffs are not rendered by default.

109 changes: 109 additions & 0 deletions src/helia_dist/libp2p-noise.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/helia_dist/libp2p-yamux.min.js

Large diffs are not rendered by default.

116 changes: 116 additions & 0 deletions src/helia_dist/libp2p.min.js

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions src/helia_dist/readme.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
# Helia

> The Helia Repo (submodule) does not properly bundle into a es6 digestible. So, we have to get/update it manually from jsdelivr.
>> all used modules: https://github.com/ipfs-examples/helia-101/blob/main/301-networking.js use websocket instead of tcp: https://github.com/ipfs-examples/helia-examples/blob/main/examples/helia-script-tag/index.html
- [libp2p-noise](https://www.jsdelivr.com/package/npm/@chainsafe/libp2p-noise)
- [libp2p-yamux](https://www.jsdelivr.com/package/npm/@chainsafe/libp2p-yamux)
- [bootstrap](https://www.jsdelivr.com/package/npm/@libp2p/bootstrap)
- [identify](https://www.jsdelivr.com/package/npm/@libp2p/identify)
- [websockets](https://www.jsdelivr.com/package/npm/@libp2p/websockets)
- [blockstore-core](https://www.jsdelivr.com/package/npm/blockstore-core)
- [datastore-core](https://www.jsdelivr.com/package/npm/datastore-core)
- [libp2p](https://www.jsdelivr.com/package/npm/libp2p)
- [helia](https://www.jsdelivr.com/package/npm/helia)
- [unixfs](https://www.jsdelivr.com/package/npm/@helia/unixfs)
- [ipns](https://www.jsdelivr.com/package/npm/@helia/ipns)
- [json](https://www.jsdelivr.com/package/npm/@helia/json)

## Version

- Libp2p-noise 15.0.0
- Libp2p-yamux 6.0.2
- Bootstrap 10.0.22
- Identify 2.0.0
- Websockets 8.0.22
- Blockstore-core 4.4.1
- Datastore-core 9.2.9
- Libp2p 1.5.1
- Helia 4.1.1
- Unixfs 3.0.5
- Ipns 7.2.1
Expand Down
3 changes: 3 additions & 0 deletions src/helia_dist/websockets.min.js

Large diffs are not rendered by default.

0 comments on commit 7af267d

Please sign in to comment.