Skip to content

Commit 90d18b4

Browse files
authored
fix: switch delegated routing example to vite (#181)
React-scripts have a dep conflict with react-native that's unlikely to be resolved so switch to vite.
1 parent 564ef51 commit 90d18b4

File tree

8 files changed

+122
-34
lines changed

8 files changed

+122
-34
lines changed

examples/js-libp2p-example-delegated-routing/package.json

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
},
1414
"type": "module",
1515
"scripts": {
16-
"build": "BUILD_PATH=dist PUBLIC_URL=/ react-scripts build",
17-
"start": "PUBLIC_URL=/ react-scripts start",
16+
"start": "vite",
17+
"build": "vite build",
18+
"server": "node ./server.js",
1819
"test:firefox": "npm run build && playwright test --browser=firefox test",
1920
"test:chrome": "npm run build && playwright test test",
2021
"test": "npm run build && test-browser-example test"
@@ -23,20 +24,14 @@
2324
"@helia/delegated-routing-v1-http-api-client": "^4.0.0",
2425
"@helia/delegated-routing-v1-http-api-server": "^4.0.0",
2526
"libp2p": "^2.0.0",
26-
"react": "^18.2.0",
27-
"react-dom": "^18.2.0",
28-
"react-scripts": "^5.0.1"
27+
"react": "^18.3.1",
28+
"react-dom": "^18.3.1",
29+
"vite": "^5.3.1"
2930
},
3031
"devDependencies": {
31-
"test-ipfs-example": "^1.0.0",
3232
"eslint-config-ipfs": "^7.0.2",
33-
"helia": "next"
33+
"helia": "^5.0.0",
34+
"test-ipfs-example": "^1.0.0"
3435
},
35-
"browserslist": [
36-
">0.2%",
37-
"not dead",
38-
"not ie <= 11",
39-
"not op_mini all"
40-
],
4136
"private": true
4237
}
Binary file not shown.

examples/js-libp2p-example-delegated-routing/public/index.html

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
6+
<meta name="theme-color" content="#000000">
7+
<title>Delegated Routing</title>
8+
<link rel="stylesheet" type="text/css" href="main.css">
9+
</head>
10+
<body>
11+
<div>
12+
<header class="center">
13+
<h1>Delegated Routing</h1>
14+
</header>
15+
<section class="center">
16+
<form id="findProvidersForm">
17+
<label>
18+
Find providers of CID:
19+
<input type="text" value="" id="find-providers-input" />
20+
<input type="submit" value="Find Providers" id="find-providers-button" />
21+
</label>
22+
</form>
23+
<form id="findPeerForm">
24+
<label>
25+
Find peer:
26+
<input type="text" value="" id="find-peer-input" />
27+
<input type="submit" value="Find PeerInfo" id="find-peer-button" />
28+
</label>
29+
</form>
30+
</section>
31+
<section id="loading-notification">
32+
<div class="lds-ripple"><div></div><div></div></div>
33+
</section>
34+
<section>
35+
<pre id="output">
36+
</pre>
37+
</section>
38+
</div>
39+
<script src="index.js" type="module"></script>
40+
</body>
41+
</html>
Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,60 @@
11
/* eslint-disable no-console */
2-
import 'react'
3-
import ReactDOM from 'react-dom'
4-
import App from './App.js' // eslint-disable-line no-unused-vars
52

6-
ReactDOM.render(<App />, document.getElementById('root'))
3+
import { createDelegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client'
4+
import { peerIdFromString } from '@libp2p/peer-id'
5+
import { createLibp2p } from 'libp2p'
6+
import { CID } from 'multiformats/cid'
7+
8+
const DOM = {
9+
findProvidersInput: () => document.getElementById('find-providers-input'),
10+
findProvidersButton: () => document.getElementById('find-providers-button'),
11+
12+
findPeerInput: () => document.getElementById('find-peer-input'),
13+
findPeerButton: () => document.getElementById('find-peer-button'),
14+
15+
loadingNotificationElement: () => document.getElementById('loading-notification'),
16+
17+
output: () => document.getElementById('output')
18+
}
19+
20+
const libp2p = await createLibp2p({
21+
services: {
22+
delegatedRouting: () => createDelegatedRoutingV1HttpApiClient('http://127.0.0.1:9832')
23+
}
24+
})
25+
26+
// find providers
27+
DOM.findProvidersButton().onclick = async (event) => {
28+
event.preventDefault()
29+
DOM.loadingNotificationElement().className = 'loading'
30+
31+
try {
32+
const cid = CID.parse(DOM.findProvidersInput().value)
33+
const providers = []
34+
DOM.output().innerText = ''
35+
36+
for await (const provider of libp2p.contentRouting.findProviders(cid)) {
37+
providers.push(provider)
38+
39+
DOM.output().innerText = JSON.stringify(providers, null, 2)
40+
}
41+
} finally {
42+
DOM.loadingNotificationElement().className = ''
43+
}
44+
}
45+
46+
// find peer
47+
DOM.findPeerButton().onclick = async (event) => {
48+
event.preventDefault()
49+
DOM.loadingNotificationElement().className = 'loading'
50+
51+
try {
52+
const peerId = peerIdFromString(DOM.findPeerInput().value)
53+
DOM.output().innerText = ''
54+
const peerInfo = await libp2p.peerRouting.findPeer(peerId)
55+
56+
DOM.output().innerText = JSON.stringify(peerInfo, null, 2)
57+
} finally {
58+
DOM.loadingNotificationElement().className = ''
59+
}
60+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default {
2+
build: {
3+
target: 'es2022',
4+
outDir: '../dist',
5+
emptyOutDir: true
6+
},
7+
optimizeDeps: {
8+
esbuildOptions: { target: 'es2022', supported: { bigint: true } }
9+
},
10+
server: {
11+
open: true
12+
},
13+
root: './src'
14+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "module",
66
"scripts": {
77
"reset": "aegir run clean && aegir clean **/node_modules **/package-lock.json",
8-
"test": "aegir run test",
8+
"test": "aegir run --concurrency 1 test",
99
"clean": "aegir run clean",
1010
"build": "aegir run build",
1111
"lint": "aegir exec aegir -- lint --files '**/*.{js,ts,jsx}' '!**/node_modules/**' '!**/dist/**'",

0 commit comments

Comments
 (0)