diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5d1ee7a1..b64e1ee6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -69,6 +69,7 @@ jobs: - helia-101 - helia-cjs - helia-webpack + - helia-vite-react steps: - uses: convictional/trigger-workflow-and-wait@f69fa9eedd3c62a599220f4d5745230e237904be with: diff --git a/examples/helia-vite-react/.gitignore b/examples/helia-vite-react/.gitignore new file mode 100644 index 00000000..96dd8d17 --- /dev/null +++ b/examples/helia-vite-react/.gitignore @@ -0,0 +1,25 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +node_modules +dist +dist-ssr +*.local + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +.DS_Store +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? +/test-results/ diff --git a/examples/helia-vite-react/README.md b/examples/helia-vite-react/README.md new file mode 100644 index 00000000..c0f4a5b7 --- /dev/null +++ b/examples/helia-vite-react/README.md @@ -0,0 +1,116 @@ +

+ + Helia logo + +

+ +

Helia with React+Vite

+ +

+ +
+ Explore the docs + · + View Demo + · + Report Bug + · + Request Feature/Example +

+ +## Table of Contents + +- [Table of Contents](#table-of-contents) +- [About The Project](#about-the-project) +- [Getting Started](#getting-started) + - [Prerequisites](#prerequisites) + - [Installation and Running example](#installation-and-running-example) +- [Usage](#usage) +- [Documentation](#documentation) +- [Contributing](#contributing) +- [Want to hack on IPFS?](#want-to-hack-on-ipfs) + +## About The Project + +- Read the [docs](https://ipfs.github.io/helia/modules/helia.html) +- Look into other [examples](https://github.com/ipfs-examples/helia-examples) to learn how to spawn a Helia node in Node.js and in the Browser +- Visit https://dweb-primer.ipfs.io to learn about IPFS and the concepts that underpin it +- Head over to https://proto.school to take interactive tutorials that cover core IPFS APIs +- Check out https://docs.ipfs.io for tips, how-tos and more +- See https://blog.ipfs.io for news and more +- Need help? Please ask 'How do I?' questions on https://discuss.ipfs.io + +## Getting Started + +### Prerequisites + +Make sure you have installed all of the following prerequisites on your development machine: + +- Git - [Download & Install Git](https://git-scm.com/downloads). OSX and Linux machines typically have this already installed. +- Node.js - [Download & Install Node.js](https://nodejs.org/en/download/) and the npm package manager. + +### Installation and Running example + +```console +> npm install +> npm start +or +> npm run dev +``` +To run the test + +```console +npx playwright install +npm run test +``` + +Now open your browser at `http://localhost:3000` + +## Usage + +In this example, you will find a boilerplate you can use to guide yourself into creating a react+vite app with helia, this provides a pattern to reuse the same client across components with the context API and suggests how to integrate it with custom hooks + +You should see the following: + +![](./public/hello-helia.gif) + +_For more examples, please refer to the [Documentation](#documentation)_ + +## Documentation + +- [IPFS Primer](https://dweb-primer.ipfs.io/) +- [IPFS Docs](https://docs.ipfs.io/) +- [Tutorials](https://proto.school) +- [More examples](https://github.com/ipfs-examples/helia-examples) +- [API - Helia](https://ipfs.github.io/helia/modules/helia.html) +- [API - @helia/unixfs](https://ipfs.github.io/helia-unixfs/modules/helia.html) + +## Contributing + +Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**. + +1. Fork the IPFS Project +2. Create your Feature Branch (`git checkout -b feature/amazing-feature`) +3. Commit your Changes (`git commit -a -m 'feat: add some amazing feature'`) +4. Push to the Branch (`git push origin feature/amazing-feature`) +5. Open a Pull Request + +## Want to hack on IPFS? + +[![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md) + +The IPFS implementation in JavaScript needs your help! There are a few things you can do right now to help out: + +Read the [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md) and [JavaScript Contributing Guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING_JS.md). + +- **Check out existing issues** The [issue list](https://github.com/ipfs/helia/issues) has many that are marked as ['help wanted'](https://github.com/ipfs/helia/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3A%22help+wanted%22) or ['difficulty:easy'](https://github.com/ipfs/helia/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3Adifficulty%3Aeasy) which make great starting points for development, many of which can be tackled with no prior IPFS knowledge +- **Look at the [Helia Roadmap](https://github.com/ipfs/helia/blob/main/ROADMAP.md)** This are the high priority items being worked on right now +- **Perform code reviews** More eyes will help + a. speed the project along + b. ensure quality, and + c. reduce possible future bugs +- **Add tests**. There can never be enough tests + +[cid]: https://docs.ipfs.tech/concepts/content-addressing "Content Identifier" +[Uint8Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array +[libp2p]: https://libp2p.io diff --git a/examples/helia-vite-react/index.html b/examples/helia-vite-react/index.html new file mode 100644 index 00000000..21dfebaa --- /dev/null +++ b/examples/helia-vite-react/index.html @@ -0,0 +1,13 @@ + + + + + + + Helia + Vite + React + + +
+ + + diff --git a/examples/helia-vite-react/package.json b/examples/helia-vite-react/package.json new file mode 100644 index 00000000..b27ee521 --- /dev/null +++ b/examples/helia-vite-react/package.json @@ -0,0 +1,36 @@ +{ + "name": "helia-vite", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "clean": "rimraf ./dist", + "dev": "vite", + "start": "vite", + "build": "vite build", + "preview": "vite preview", + "test": "npm run build && playwright test tests" + }, + "dependencies": { + "@chainsafe/libp2p-noise": "^11.0.1", + "@chainsafe/libp2p-yamux": "^3.0.7", + "@helia/unixfs": "^1.2.0", + "@libp2p/bootstrap": "^6.0.2", + "@libp2p/websockets": "^5.0.6", + "blockstore-core": "^4.0.1", + "datastore-core": "^9.0.3", + "helia": "next", + "libp2p": "^0.42.2", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "rimraf": "^4.4.0" + }, + "devDependencies": { + "@playwright/test": "^1.31.2", + "@types/react": "^18.0.28", + "@types/react-dom": "^18.0.11", + "@vitejs/plugin-react": "^3.1.0", + "test-util-ipfs-example": "^1.0.2", + "vite": "^4.2.0" + } +} diff --git a/examples/helia-vite-react/public/hello-helia.gif b/examples/helia-vite-react/public/hello-helia.gif new file mode 100644 index 00000000..68e63af1 Binary files /dev/null and b/examples/helia-vite-react/public/hello-helia.gif differ diff --git a/examples/helia-vite-react/public/vite.svg b/examples/helia-vite-react/public/vite.svg new file mode 100644 index 00000000..e7b8dfb1 --- /dev/null +++ b/examples/helia-vite-react/public/vite.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/examples/helia-vite-react/src/App.css b/examples/helia-vite-react/src/App.css new file mode 100644 index 00000000..b9d355df --- /dev/null +++ b/examples/helia-vite-react/src/App.css @@ -0,0 +1,42 @@ +#root { + max-width: 1280px; + margin: 0 auto; + padding: 2rem; + text-align: center; +} + +.logo { + height: 6em; + padding: 1.5em; + will-change: filter; + transition: filter 300ms; +} +.logo:hover { + filter: drop-shadow(0 0 2em #646cffaa); +} +.logo.react:hover { + filter: drop-shadow(0 0 2em #61dafbaa); +} + +@keyframes logo-spin { + from { + transform: rotate(0deg); + } + to { + transform: rotate(360deg); + } +} + +@media (prefers-reduced-motion: no-preference) { + a:nth-of-type(2) .logo { + animation: logo-spin infinite 20s linear; + } +} + +.card { + padding: 2em; +} + +.read-the-docs { + color: #888; +} diff --git a/examples/helia-vite-react/src/App.jsx b/examples/helia-vite-react/src/App.jsx new file mode 100644 index 00000000..82df53bd --- /dev/null +++ b/examples/helia-vite-react/src/App.jsx @@ -0,0 +1,55 @@ +import { useState, useContext } from 'react' +import './App.css' +import { useHelia } from '@/hooks/useHelia' +import { useCommitText } from '@/hooks/useCommitText' + +function App() { + const [text, setText] = useState("") + const { error, starting } = useHelia() + const { + cidString, + commitText, + fetchCommitedText, + commitedText, + } = useCommitText() + + return ( +
+
Helia Status
+ setText(event.target.value)} + type="text" /> + +
textCid: {cidString}
+ { cidString && (<> + +
Commited Text: {commitedText}
+ ) + } + +
+ ) +} + +export default App diff --git a/examples/helia-vite-react/src/hooks/useCommitText.jsx b/examples/helia-vite-react/src/hooks/useCommitText.jsx new file mode 100644 index 00000000..2325a0af --- /dev/null +++ b/examples/helia-vite-react/src/hooks/useCommitText.jsx @@ -0,0 +1,51 @@ +import { useState, useEffect, useCallback, useContext } from 'react' +import { useHelia } from '@/hooks/useHelia' + +const encoder = new TextEncoder() +const decoder = new TextDecoder() + +export const useCommitText = () => { + const {helia, fs, error, starting } = useHelia() + const [cid, setCid] = useState(null) + const [cidString, setCidString] = useState("") + const [commitedText, setCommitedText] = useState("") + + const commitText = useCallback(async (text) => { + if (!error && !starting) { + try { + const cid = await fs.addBytes( + encoder.encode(text), + helia.blockstore + ) + setCid(cid) + setCidString(cid.toString()) + console.log('Added file:', cid.toString()) + } catch (e) { + console.error(e) + } + } else { + console.log('please wait for helia to start') + } + }, [error, starting, helia, fs]) + + const fetchCommitedText = useCallback(async () => { + let text = '' + if (!error && !starting) { + try { + for await (const chunk of fs.cat(cid)) { + text += decoder.decode(chunk, { + stream: true + }) + } + setCommitedText(text) + } catch (e) { + console.error(e) + } + } else { + console.log('please wait for helia to start') + } + }, [error, starting, cid, helia, fs]) + // If one forgets to add helia in the dependency array in commitText, additions to the blockstore will not be picked up by react, leading to operations on fs to hang indefinitely in the generator state. As such it would be good practice to ensure to include helia inside the dependency array of all hooks to tell react that the useCallback needs the most up to date helia state + + return { cidString, commitedText, commitText, fetchCommitedText } +} diff --git a/examples/helia-vite-react/src/hooks/useHelia.jsx b/examples/helia-vite-react/src/hooks/useHelia.jsx new file mode 100644 index 00000000..2eef1b45 --- /dev/null +++ b/examples/helia-vite-react/src/hooks/useHelia.jsx @@ -0,0 +1,9 @@ +import { useContext } from 'react' +import { HeliaContext } from '@/provider/HeliaProvider' + +export const useHelia = () => { + const { helia, fs, error, starting } = useContext(HeliaContext) + return {helia, fs, error, starting} +} + + diff --git a/examples/helia-vite-react/src/index.css b/examples/helia-vite-react/src/index.css new file mode 100644 index 00000000..de8a4359 --- /dev/null +++ b/examples/helia-vite-react/src/index.css @@ -0,0 +1,68 @@ +:root { + font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; + line-height: 1.5; + font-weight: 400; + + color-scheme: light dark; + color: rgba(255, 255, 255, 0.87); + background-color: #242424; + + font-synthesis: none; + text-rendering: optimizeLegibility; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + -webkit-text-size-adjust: 100%; +} + +a { + font-weight: 500; + color: #646cff; + text-decoration: inherit; +} +a:hover { + color: #535bf2; +} + +body { + margin: 0; + display: flex; + min-width: 320px; + min-height: 100vh; +} + +h1 { + font-size: 3.2em; + line-height: 1.1; +} + +button { + border-radius: 8px; + border: 1px solid transparent; + padding: 0.6em 1.2em; + font-size: 1em; + font-weight: 500; + font-family: inherit; + background-color: #1a1a1a; + cursor: pointer; + transition: border-color 0.25s; +} +button:hover { + border-color: #646cff; +} +button:focus, +button:focus-visible { + outline: 4px auto -webkit-focus-ring-color; +} + +@media (prefers-color-scheme: light) { + :root { + color: #213547; + background-color: #ffffff; + } + a:hover { + color: #747bff; + } + button { + background-color: #f9f9f9; + } +} diff --git a/examples/helia-vite-react/src/main.jsx b/examples/helia-vite-react/src/main.jsx new file mode 100644 index 00000000..15a45c96 --- /dev/null +++ b/examples/helia-vite-react/src/main.jsx @@ -0,0 +1,13 @@ +import React from 'react' +import ReactDOM from 'react-dom/client' +import App from './App' +import './index.css' +import { HeliaProvider } from '@/provider/HeliaProvider' + +ReactDOM.createRoot(document.getElementById('root')).render( + + + + + , +) diff --git a/examples/helia-vite-react/src/provider/HeliaProvider.jsx b/examples/helia-vite-react/src/provider/HeliaProvider.jsx new file mode 100644 index 00000000..6b951fa2 --- /dev/null +++ b/examples/helia-vite-react/src/provider/HeliaProvider.jsx @@ -0,0 +1,101 @@ +import { createHelia } from 'helia' +import { createLibp2p } from 'libp2p' +import { noise } from '@chainsafe/libp2p-noise' +import { yamux } from '@chainsafe/libp2p-yamux' +import { webSockets } from '@libp2p/websockets' +import { bootstrap } from '@libp2p/bootstrap' +import { unixfs } from '@helia/unixfs' +import { MemoryBlockstore } from 'blockstore-core' +import { MemoryDatastore } from 'datastore-core' +import { + useEffect, + useState, + useCallback, + createContext, + useContext +} from 'react' + +export const HeliaContext = createContext({ + helia: null, + fs: null, + error: false, + starting: true +}) + +export const HeliaProvider = ({children}) => { + const [helia, setHelia] = useState(null) + const [fs, setFs] = useState(null) + const [starting, setStarting] = useState(true) + const [error, setError] = useState(null) + + const startHelia = useCallback(async () => { + if (helia) { + console.info("helia already started") + } else if (window.helia) { + console.info("found a windowed instance of helia, populating ...") + setHelia(window.helia) + setFs(unixfs(helia)) + setStarting(false) + } else { + try { + // 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 + // Make sure to stick libp2p here when running react in strict mode + 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" + ] + }) + ] + }) + console.info('Starting Helia') + const helia = await createHelia({ + datastore, + blockstore, + libp2p + }) + setHelia(helia) + setFs(unixfs(helia)) + setStarting(false) + } catch (e) { + console.error(e) + setError(true) + } + } + }, []) + + useEffect(() => { + startHelia() + }, []) + + return ( + {children} + ) +} diff --git a/examples/helia-vite-react/tests/test.js b/examples/helia-vite-react/tests/test.js new file mode 100644 index 00000000..95c75b52 --- /dev/null +++ b/examples/helia-vite-react/tests/test.js @@ -0,0 +1,45 @@ +import { test, expect } from '@playwright/test'; +import { playwright } from 'test-util-ipfs-example'; + +const play = test.extend({ + ...playwright.servers(), +}); + +play.describe('Use Helia With react and vite', () => { + // DOM + const heliaStatus= "#heliaStatus" + const textInput = "#textInput" + const commitTextButton = "#commitTextButton" + const cidOutput = "#cidOutput" + const fetchCommitedTextButton = "#fetchCommitedTextButton" + const commitedTextOutput = "#commitedTextOutput" + + play.beforeEach(async ({servers, page}) => { + await page.goto(`http://localhost:${servers[0].port}/`); + }) + + play('should properly initialized a Helia node and add/get a file', async ({ page }) => { + // wait for helia node to be online + const text = 'Hello Helia' + console.log('text', text) + const status = await page.locator(heliaStatus) + await expect(status).toHaveCSS( + 'border-color', + 'rgb(0, 128, 0)', // green + {timeout: 7000} + ) + // commit text to the blockstore + await page.fill(textInput, text) + await page.click(commitTextButton) + + await page.waitForSelector(`${cidOutput}:has-text("bafkreig7i5kbqdnoooievvfextf27eoherluxe2pi3j26hu6zpiauydydy")`) + const cidOutputContent = await page.textContent(cidOutput) + + expect(cidOutputContent).toContain("bafkreig7i5kbqdnoooievvfextf27eoherluxe2pi3j26hu6zpiauydydy"); + // retreive text from blockstore + await page.click(fetchCommitedTextButton) + await page.waitForSelector(`${commitedTextOutput}:has-text("${text}")`) + const commitedTextOutputLocator = await page.locator(commitedTextOutput) + await expect(commitedTextOutputLocator).toHaveText(`Commited Text: ${text}`, {timeout: 2000}) + }); +}); diff --git a/examples/helia-vite-react/vite.config.js b/examples/helia-vite-react/vite.config.js new file mode 100644 index 00000000..ffab3bc8 --- /dev/null +++ b/examples/helia-vite-react/vite.config.js @@ -0,0 +1,10 @@ +import { defineConfig } from 'vite' +import react from '@vitejs/plugin-react' +import path from 'path' +// https://vitejs.dev/config/ +export default defineConfig({ + plugins: [react()], + resolve: { + alias: [{ find: '@', replacement: path.resolve(__dirname, '/src') }], + }, +})