Skip to content

Commit

Permalink
Merge branch 'main' into feat/add-cra-example
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Jul 27, 2023
2 parents b363cde + ba9bb4d commit 3c8efb0
Show file tree
Hide file tree
Showing 82 changed files with 993 additions and 1,000 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
- helia-electron
- helia-esbuild
- helia-nextjs
- helia-parcel
- helia-script-tag
- helia-vite
- helia-vue
Expand Down Expand Up @@ -82,6 +83,7 @@ jobs:
- helia-create-react-app
- helia-electron
- helia-esbuild
- helia-parcel
- helia-nextjs
- helia-script-tag
- helia-vite
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ yarn.lock
.DS_Store
.next
.vscode
test-results
playwright-report
.parcel-cache
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
- [Table of Contents](#table-of-contents)
- [About The Project](#about-the-project)
- [Getting Started](#getting-started)
- [Examples](#examples)
- [Basics](#basics)
- [Frameworks](#frameworks)
- [Bundlers](#bundlers)
- [Prerequisites](#prerequisites)
- [IPFS Tutorials at ProtoSchool](#ipfs-tutorials-at-protoschool)
- [Documentation](#documentation)
Expand All @@ -44,6 +48,28 @@

## Getting Started

### Examples

Feel free to jump directly into the examples, however going through the following sections will help build context and background knowledge.

#### Basics

- [Helia-101](/examples/helia-101/): Spawn a Helia node, add a file and cat the file
- [Helia CommonJS](/examples/helia-cjs/): Just like Helia-101, but with [CommonJS](https://en.wikipedia.org/wiki/CommonJS) instead of [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)
- [Helia via CDNs](/examples/helia-script-tag/): A simple proof-of-concept to distributing and using helia using `<script>` tags

#### Frameworks

- [Helia with Electron](/examples/helia-electron/): Create an Electron app with helia
- [Helia with Next.js](/examples/helia-nextjs/): Create a Next.js app with helia
- [Helia with Vue](/examples/helia-vue/): Create a vue app with helia
- [Helia with Vite](/examples/helia-vite/): Create a react+vite app with helia

#### Bundlers

- [Helia with esbuild](/examples/helia-esbuild/): Bundle helia with esbuild
- [Helia with Webpack](/examples/helia-webpack/): Bundle helia with webpack

### Prerequisites

Make sure you have installed all of the following prerequisites on your development machine:
Expand Down
2 changes: 1 addition & 1 deletion examples/helia-101/101-basics.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */

import { createHelia } from 'helia'
import { unixfs } from '@helia/unixfs'
import { createHelia } from 'helia'

// create a Helia node
const helia = await createHelia()
Expand Down
2 changes: 1 addition & 1 deletion examples/helia-101/201-storage.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable no-console */

import { createHelia } from 'helia'
import { unixfs } from '@helia/unixfs'
import { MemoryBlockstore } from 'blockstore-core'
import { createHelia } from 'helia'

// the blockstore is where we store the blocks that make up files. this blockstore
// stores everything in-memory - other blockstores are available:
Expand Down
14 changes: 9 additions & 5 deletions examples/helia-101/301-networking.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint-disable no-console */

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 { bootstrap } from '@libp2p/bootstrap'
import { tcp } from '@libp2p/tcp'
import { MemoryBlockstore } from 'blockstore-core'
import { MemoryDatastore } from 'datastore-core'
import { createHelia } from 'helia'
import { createLibp2p } from 'libp2p'
import { identifyService } from 'libp2p/identify'

async function createNode () {
// the blockstore is where we store the blocks that make up files
Expand Down Expand Up @@ -43,7 +44,10 @@ async function createNode () {
'/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt'
]
})
]
],
services: {
identify: identifyService()
}
})

return await createHelia({
Expand Down
12 changes: 8 additions & 4 deletions examples/helia-101/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Commonly used datastore implementations are:

The final example is [301-networking.js](./301-networking.js).

Adding blocks to your local blockstore is great but when you add a libp2p instance to your Helia config you unlock the full power of the distributed web.
Adding blocks to your local blockstore is great but using your Helia node's libp2p instance allows you to unlock the full power of the distributed web.

With libp2p configured you can retrieve blocks from remote peers, and those peers can retrieve blocks from you.

Expand All @@ -184,6 +184,7 @@ With libp2p configured you can retrieve blocks from remote peers, and those peer

```js
import { createLibp2p } from 'libp2p'
import { identifyService } from 'libp2p/identify'
import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { webSockets } from '@libp2p/websockets'
Expand Down Expand Up @@ -212,19 +213,22 @@ const libp2p = await createLibp2p({
"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt"
]
})
]
],
services: {
identify: identifyService()
}
})
```

### Putting it all together

Once your Helia node is configured with a libp2p node, you can go to an IPFS Gateway and load the printed hash. Go ahead and try it!
Since your Helia node is configured with a libp2p node, you can go to an IPFS Gateway and load the printed hash. Go ahead and try it!

```bash
> node 301-networking.js

Added file: bafkreife2klsil6kaxqhvmhgldpsvk5yutzm4i5bgjoq6fydefwtihnesa
# Copy that hash and load it on the gateway, here is a prefiled url:
# Copy that hash and load it on the gateway, here is a prefilled url:
# https://ipfs.io/ipfs/bafkreife2klsil6kaxqhvmhgldpsvk5yutzm4i5bgjoq6fydefwtihnesa
```

Expand Down
20 changes: 10 additions & 10 deletions examples/helia-101/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
"scripts": {
"start": "node index.js",
"serve": "npm run start",
"test": "node test.js"
"test": "test-node-example test/*"
},
"dependencies": {
"@chainsafe/libp2p-noise": "^11.0.0",
"@chainsafe/libp2p-yamux": "^3.0.5",
"@helia/unixfs": "^1.0.2",
"@libp2p/bootstrap": "^6.0.0",
"@libp2p/tcp": "^6.1.2",
"blockstore-core": "^4.0.1",
"datastore-core": "^9.0.3",
"@chainsafe/libp2p-noise": "^12.0.0",
"@chainsafe/libp2p-yamux": "^4.0.1",
"@helia/unixfs": "^1.4.1",
"@libp2p/bootstrap": "^8.0.0",
"@libp2p/tcp": "^7.0.1",
"blockstore-core": "^4.1.0",
"datastore-core": "^9.1.1",
"helia": "^1.0.0",
"libp2p": "^0.43.3"
"libp2p": "^0.45.0"
},
"devDependencies": {
"test-util-ipfs-example": "^1.0.2"
"test-ipfs-example": "^1.0.0"
}
}
11 changes: 0 additions & 11 deletions examples/helia-101/test.js

This file was deleted.

11 changes: 11 additions & 0 deletions examples/helia-101/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import path from 'path'
import { fileURLToPath } from 'url'
import { waitForOutput } from 'test-ipfs-example/node'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

await waitForOutput('Added file contents: Hello World 101', 'node', [path.resolve(__dirname, '../101-basics.js')])

await waitForOutput('Added file contents: Hello World 201', 'node', [path.resolve(__dirname, '../201-storage.js')])

await waitForOutput('Fetched file contents: Hello World 301', 'node', [path.resolve(__dirname, '../301-networking.js')])
43 changes: 1 addition & 42 deletions examples/helia-cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,10 @@

async function main () {
const { createHelia } = await import('helia')
const { createLibp2p } = await import('libp2p')
const { noise } = await import('@chainsafe/libp2p-noise')
const { yamux } = await import('@chainsafe/libp2p-yamux')
const { webSockets } = await import('@libp2p/websockets')
const { bootstrap } = await import('@libp2p/bootstrap')
const { unixfs } = await import('@helia/unixfs')
const { MemoryBlockstore } = await import('blockstore-core')
const { MemoryDatastore } = await import('datastore-core')

// 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,
transports: [
webSockets()
],
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'
]
})
]
})

// create a Helia node
const helia = await createHelia({
datastore,
blockstore,
libp2p
})
const helia = await createHelia()

// print out our node's PeerId
console.log(helia.libp2p.peerId)
Expand Down
15 changes: 4 additions & 11 deletions examples/helia-cjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@
"scripts": {
"start": "node index.js",
"serve": "npm run start",
"test": "node test.mjs"
"test": "test-node-example test/*"
},
"dependencies": {
"@chainsafe/libp2p-noise": "^11.0.0",
"@chainsafe/libp2p-yamux": "^3.0.5",
"@helia/unixfs": "^1.0.5",
"@libp2p/bootstrap": "^6.0.0",
"@libp2p/websockets": "^5.0.3",
"blockstore-core": "^4.0.1",
"datastore-core": "^9.0.3",
"helia": "^1.0.0",
"libp2p": "^0.43.3"
"@helia/unixfs": "^1.4.1",
"helia": "^1.0.0"
},
"devDependencies": {
"test-util-ipfs-example": "^1.0.2"
"test-ipfs-example": "^1.0.0"
}
}
7 changes: 0 additions & 7 deletions examples/helia-cjs/test.mjs

This file was deleted.

7 changes: 7 additions & 0 deletions examples/helia-cjs/test/index.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { waitForOutput } from 'test-ipfs-example/node'
import path from 'path'
import { fileURLToPath } from 'url'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

await waitForOutput('Added file contents: Hello World 101', 'node', [path.resolve(__dirname, '../index.js')])
48 changes: 1 addition & 47 deletions examples/helia-electron/helia.mjs
Original file line number Diff line number Diff line change
@@ -1,53 +1,7 @@
/* eslint-disable no-console */

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 { MemoryBlockstore } from 'blockstore-core'
import { MemoryDatastore } from 'datastore-core'

export 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
})
return await createHelia()
}
15 changes: 4 additions & 11 deletions examples/helia-electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,15 @@
"clean": "echo 'Nothing to clean...'",
"start": "electron .",
"serve": "npm run start",
"test": "xvfb-maybe node test.mjs"
"test": "xvfb-maybe test-node-example test/*"
},
"dependencies": {
"@chainsafe/libp2p-noise": "^11.0.4",
"@chainsafe/libp2p-yamux": "^3.0.7",
"@libp2p/bootstrap": "^6.0.3",
"@libp2p/tcp": "^6.1.5",
"blockstore-core": "^4.1.0",
"datastore-core": "^9.1.1",
"helia": "^1.0.0",
"libp2p": "^0.43.3"
"helia": "^1.3.7"
},
"devDependencies": {
"electron": "^24.0.0",
"electron": "^25.0.1",
"electron-rebuild": "^3.1.1",
"test-util-ipfs-example": "^1.0.2",
"test-ipfs-example": "^1.0.0",
"xvfb-maybe": "^0.2.1"
},
"greenkeeper": {
Expand Down
Loading

0 comments on commit 3c8efb0

Please sign in to comment.