Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 5b5eabc

Browse files
authored
docs: update http client examples (#3172)
Moves http client examples to the examples folder and adds tests to prevent regressions.
1 parent f3f8d2a commit 5b5eabc

File tree

77 files changed

+753
-435
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+753
-435
lines changed

examples/browser-mfs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"http-server": "^0.11.1",
1818
"terser-webpack-plugin": "^1.2.1",
1919
"test-ipfs-example": "^2.0.3",
20-
"webpack": "^4.28.4",
20+
"webpack": "^4.43.0",
2121
"webpack-cli": "^3.3.11"
2222
},
2323
"dependencies": {

examples/browser-readablestream/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"http-server": "^0.11.1",
1818
"terser-webpack-plugin": "^1.2.1",
1919
"test-ipfs-example": "^2.0.3",
20-
"webpack": "^4.28.4"
20+
"webpack": "^4.43.0"
2121
},
2222
"dependencies": {
2323
"ipfs": "^0.47.0",

examples/browser-webpack/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
"copy-webpack-plugin": "^5.0.4",
2020
"react": "^16.8.6",
2121
"react-dom": "^16.8.6",
22-
"react-hot-loader": "^4.8.8",
22+
"react-hot-loader": "^4.12.21",
2323
"test-ipfs-example": "^2.0.3",
24-
"webpack": "^4.28.4",
24+
"webpack": "^4.43.0",
2525
"webpack-cli": "^3.3.11",
26-
"webpack-dev-server": "^3.1.14"
26+
"webpack-dev-server": "^3.11.0"
2727
},
2828
"dependencies": {
2929
"ipfs": "^0.47.0"

examples/custom-libp2p/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"libp2p-kad-dht": "^0.19.9",
1717
"libp2p-mdns": "^0.14.1",
1818
"libp2p-mplex": "^0.9.5",
19-
"libp2p-secio": "^0.12.4",
19+
"libp2p-secio": "^0.12.6",
2020
"libp2p-tcp": "^0.14.5"
2121
},
2222
"devDependencies": {

packages/ipfs-http-client/examples/browser-pubsub/README.md renamed to examples/http-client-browser-pubsub/README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@ This example is a demo web application that allows you to connect to an IPFS nod
2828

2929
With Node.js and git installed, clone the repo and install the project dependencies:
3030

31-
```sh
32-
git clone https://github.com/ipfs/js-ipfs-http-client.git
33-
cd js-ipfs-http-client
34-
npm install # Installs ipfs-http-client dependencies
35-
cd examples/browser-pubsub
36-
npm install # Installs browser-pubsub app dependencies
31+
```console
32+
$ git clone https://github.com/ipfs/js-ipfs.git
33+
$ cd js-ipfs/examples/http-client-browser-pubsub
34+
$ npm install
3735
```
3836

3937
Start the example application:
File renamed without changes.

packages/ipfs-http-client/examples/browser-pubsub/index.js renamed to examples/http-client-browser-pubsub/index.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ async function main () {
5454
await sleep()
5555
const peers = await ipfs.swarm.peers()
5656
peers.forEach(peer => {
57-
const fullAddr = `${peer.addr}/ipfs/${peer.peer.toB58String()}`
58-
log(`<span class="${addr.endsWith(peer.peer.toB58String()) ? 'teal' : ''}">${fullAddr}</span>`)
57+
const fullAddr = `${peer.addr}/ipfs/${peer.peer.toString()}`
58+
log(`<span class="${addr.endsWith(peer.peer.toString()) ? 'teal' : ''}">${fullAddr}</span>`)
5959
})
6060
log(`(${peers.length} peers total)`)
6161
}
@@ -84,18 +84,6 @@ async function main () {
8484
} catch (_) {
8585
log(msg.data.toString('hex'))
8686
}
87-
}, {
88-
onError: (err, fatal) => {
89-
if (fatal) {
90-
console.error(err)
91-
log(`<span class="red">${err.message}</span>`)
92-
topic = null
93-
log('Resubscribing in 5s...')
94-
setTimeout(catchAndLog(() => subscribe(nextTopic), log), 5000)
95-
} else {
96-
console.warn(err)
97-
}
98-
}
9987
})
10088

10189
topic = nextTopic
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "example-http-client-browser-pubsub-example",
3+
"version": "0.0.0",
4+
"description": "An example demonstrating pubsub in the browser",
5+
"private": true,
6+
"main": "index.js",
7+
"scripts": {
8+
"build": "parcel build index.html --public-url ./",
9+
"start": "parcel index.html -p 8888",
10+
"test": "test-ipfs-example"
11+
},
12+
"author": "Alan Shaw",
13+
"license": "MIT",
14+
"dependencies": {
15+
"ipfs-http-client": "^44.3.0"
16+
},
17+
"browserslist": [
18+
"last 2 versions and not dead and > 2%"
19+
],
20+
"devDependencies": {
21+
"execa": "^4.0.0",
22+
"go-ipfs": "^0.6.0",
23+
"ipfs": "^0.47.0",
24+
"ipfsd-ctl": "^4.1.1",
25+
"parcel-bundler": "^1.12.4",
26+
"test-ipfs-example": "^2.0.3"
27+
}
28+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
const execa = require('execa')
5+
const { createFactory } = require('ipfsd-ctl')
6+
const df = createFactory({
7+
ipfsHttpModule: require('ipfs-http-client'),
8+
}, {
9+
js: {
10+
ipfsBin: require.resolve('ipfs/src/cli/bin.js')
11+
},
12+
go: {
13+
ipfsBin: require('go-ipfs').path(),
14+
args: ['--enable-pubsub-experiment']
15+
}
16+
})
17+
const {
18+
startServer
19+
} = require('test-ipfs-example/utils')
20+
const pkg = require('./package.json')
21+
22+
async function testUI (url, id, apiAddr, peerAddr, topic) {
23+
const proc = execa(require.resolve('test-ipfs-example/node_modules/.bin/nightwatch'), ['--config', require.resolve('test-ipfs-example/nightwatch.conf.js'), path.join(__dirname, 'test.js')], {
24+
cwd: path.resolve(__dirname, '../'),
25+
env: {
26+
...process.env,
27+
CI: true,
28+
IPFS_EXAMPLE_TEST_URL: url,
29+
IPFS_API_ADDRESS: apiAddr,
30+
IPFS_ID: id,
31+
IPFS_PEER_ADDRESS: peerAddr,
32+
IPFS_TOPIC: topic
33+
},
34+
all: true
35+
})
36+
proc.all.on('data', (data) => {
37+
process.stdout.write(data)
38+
})
39+
40+
await proc
41+
}
42+
43+
async function runTest () {
44+
const app1 = await startServer(__dirname)
45+
const app2 = await startServer(__dirname)
46+
const js = await df.spawn({
47+
type: 'js',
48+
test: true,
49+
ipfsOptions: {
50+
config: {
51+
Addresses: {
52+
API: '/ip4/127.0.0.1/tcp/0'
53+
},
54+
API: {
55+
HTTPHeaders: {
56+
'Access-Control-Allow-Origin': [
57+
app1.url
58+
]
59+
}
60+
}
61+
}
62+
}
63+
})
64+
const go = await df.spawn({
65+
type: 'go',
66+
test: true,
67+
ipfsOptions: {
68+
config: {
69+
Addresses: {
70+
API: '/ip4/127.0.0.1/tcp/0'
71+
},
72+
API: {
73+
HTTPHeaders: {
74+
'Access-Control-Allow-Origin': [
75+
app2.url
76+
]
77+
}
78+
}
79+
}
80+
}
81+
})
82+
83+
const topic = `topic-${Math.random()}`
84+
85+
try {
86+
await Promise.all([
87+
testUI(app1.url, js.api.peerId.id, js.apiAddr, go.api.peerId.addresses[0].toString(), topic),
88+
testUI(app2.url, go.api.peerId.id, go.apiAddr, js.api.peerId.addresses[0].toString(), topic)
89+
])
90+
} finally {
91+
await js.stop()
92+
await go.stop()
93+
await app1.stop()
94+
await app2.stop()
95+
}
96+
}
97+
98+
module.exports = runTest
99+
100+
module.exports[pkg.name] = function (browser) {
101+
browser
102+
.url(process.env.IPFS_EXAMPLE_TEST_URL)
103+
.waitForElementVisible('#api-url')
104+
.clearValue('#api-url')
105+
.setValue('#api-url', process.env.IPFS_API_ADDRESS)
106+
.pause(1000)
107+
.click('#node-connect')
108+
109+
browser.expect.element('#console').text.to.contain(`Connecting to ${process.env.IPFS_API_ADDRESS}\nSuccess!`)
110+
111+
// connect to peer
112+
browser
113+
.waitForElementVisible('#peer-addr')
114+
.clearValue('#peer-addr')
115+
.setValue('#peer-addr', process.env.IPFS_PEER_ADDRESS)
116+
.pause(1000)
117+
.click('#peer-connect')
118+
119+
browser.expect.element('#console').text.to.contain(`Connecting to peer ${process.env.IPFS_PEER_ADDRESS}\nSuccess!`)
120+
121+
// subscribe to topic
122+
browser
123+
.waitForElementVisible('#topic')
124+
.clearValue('#topic')
125+
.setValue('#topic', process.env.IPFS_TOPIC)
126+
.pause(1000)
127+
.click('#subscribe')
128+
129+
browser.expect.element('#console').text.to.contain(`Subscribing to ${process.env.IPFS_TOPIC}...\nSuccess!`)
130+
131+
// send a message
132+
browser
133+
.waitForElementVisible('#message')
134+
.clearValue('#message')
135+
.setValue('#message', 'hello')
136+
.pause(1000)
137+
.click('#send')
138+
139+
const remotePeerId = process.env.IPFS_PEER_ADDRESS.split('/').pop()
140+
141+
browser.expect.element('#console').text.to.contain(`from ${remotePeerId}:\n"hello"`)
142+
143+
browser.end()
144+
}
File renamed without changes.

0 commit comments

Comments
 (0)