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

Commit

Permalink
docs: update http client examples (#3172)
Browse files Browse the repository at this point in the history
Moves http client examples to the examples folder and adds tests to prevent regressions.
  • Loading branch information
achingbrain authored Jul 15, 2020
1 parent f3f8d2a commit 5b5eabc
Show file tree
Hide file tree
Showing 77 changed files with 753 additions and 435 deletions.
2 changes: 1 addition & 1 deletion examples/browser-mfs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"http-server": "^0.11.1",
"terser-webpack-plugin": "^1.2.1",
"test-ipfs-example": "^2.0.3",
"webpack": "^4.28.4",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/browser-readablestream/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"http-server": "^0.11.1",
"terser-webpack-plugin": "^1.2.1",
"test-ipfs-example": "^2.0.3",
"webpack": "^4.28.4"
"webpack": "^4.43.0"
},
"dependencies": {
"ipfs": "^0.47.0",
Expand Down
6 changes: 3 additions & 3 deletions examples/browser-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
"copy-webpack-plugin": "^5.0.4",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-hot-loader": "^4.8.8",
"react-hot-loader": "^4.12.21",
"test-ipfs-example": "^2.0.3",
"webpack": "^4.28.4",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.1.14"
"webpack-dev-server": "^3.11.0"
},
"dependencies": {
"ipfs": "^0.47.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"libp2p-kad-dht": "^0.19.9",
"libp2p-mdns": "^0.14.1",
"libp2p-mplex": "^0.9.5",
"libp2p-secio": "^0.12.4",
"libp2p-secio": "^0.12.6",
"libp2p-tcp": "^0.14.5"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ This example is a demo web application that allows you to connect to an IPFS nod

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

```sh
git clone https://github.com/ipfs/js-ipfs-http-client.git
cd js-ipfs-http-client
npm install # Installs ipfs-http-client dependencies
cd examples/browser-pubsub
npm install # Installs browser-pubsub app dependencies
```console
$ git clone https://github.com/ipfs/js-ipfs.git
$ cd js-ipfs/examples/http-client-browser-pubsub
$ npm install
```

Start the example application:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ async function main () {
await sleep()
const peers = await ipfs.swarm.peers()
peers.forEach(peer => {
const fullAddr = `${peer.addr}/ipfs/${peer.peer.toB58String()}`
log(`<span class="${addr.endsWith(peer.peer.toB58String()) ? 'teal' : ''}">${fullAddr}</span>`)
const fullAddr = `${peer.addr}/ipfs/${peer.peer.toString()}`
log(`<span class="${addr.endsWith(peer.peer.toString()) ? 'teal' : ''}">${fullAddr}</span>`)
})
log(`(${peers.length} peers total)`)
}
Expand Down Expand Up @@ -84,18 +84,6 @@ async function main () {
} catch (_) {
log(msg.data.toString('hex'))
}
}, {
onError: (err, fatal) => {
if (fatal) {
console.error(err)
log(`<span class="red">${err.message}</span>`)
topic = null
log('Resubscribing in 5s...')
setTimeout(catchAndLog(() => subscribe(nextTopic), log), 5000)
} else {
console.warn(err)
}
}
})

topic = nextTopic
Expand Down
28 changes: 28 additions & 0 deletions examples/http-client-browser-pubsub/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "example-http-client-browser-pubsub-example",
"version": "0.0.0",
"description": "An example demonstrating pubsub in the browser",
"private": true,
"main": "index.js",
"scripts": {
"build": "parcel build index.html --public-url ./",
"start": "parcel index.html -p 8888",
"test": "test-ipfs-example"
},
"author": "Alan Shaw",
"license": "MIT",
"dependencies": {
"ipfs-http-client": "^44.3.0"
},
"browserslist": [
"last 2 versions and not dead and > 2%"
],
"devDependencies": {
"execa": "^4.0.0",
"go-ipfs": "^0.6.0",
"ipfs": "^0.47.0",
"ipfsd-ctl": "^4.1.1",
"parcel-bundler": "^1.12.4",
"test-ipfs-example": "^2.0.3"
}
}
144 changes: 144 additions & 0 deletions examples/http-client-browser-pubsub/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
'use strict'

const path = require('path')
const execa = require('execa')
const { createFactory } = require('ipfsd-ctl')
const df = createFactory({
ipfsHttpModule: require('ipfs-http-client'),
}, {
js: {
ipfsBin: require.resolve('ipfs/src/cli/bin.js')
},
go: {
ipfsBin: require('go-ipfs').path(),
args: ['--enable-pubsub-experiment']
}
})
const {
startServer
} = require('test-ipfs-example/utils')
const pkg = require('./package.json')

async function testUI (url, id, apiAddr, peerAddr, topic) {
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')], {
cwd: path.resolve(__dirname, '../'),
env: {
...process.env,
CI: true,
IPFS_EXAMPLE_TEST_URL: url,
IPFS_API_ADDRESS: apiAddr,
IPFS_ID: id,
IPFS_PEER_ADDRESS: peerAddr,
IPFS_TOPIC: topic
},
all: true
})
proc.all.on('data', (data) => {
process.stdout.write(data)
})

await proc
}

async function runTest () {
const app1 = await startServer(__dirname)
const app2 = await startServer(__dirname)
const js = await df.spawn({
type: 'js',
test: true,
ipfsOptions: {
config: {
Addresses: {
API: '/ip4/127.0.0.1/tcp/0'
},
API: {
HTTPHeaders: {
'Access-Control-Allow-Origin': [
app1.url
]
}
}
}
}
})
const go = await df.spawn({
type: 'go',
test: true,
ipfsOptions: {
config: {
Addresses: {
API: '/ip4/127.0.0.1/tcp/0'
},
API: {
HTTPHeaders: {
'Access-Control-Allow-Origin': [
app2.url
]
}
}
}
}
})

const topic = `topic-${Math.random()}`

try {
await Promise.all([
testUI(app1.url, js.api.peerId.id, js.apiAddr, go.api.peerId.addresses[0].toString(), topic),
testUI(app2.url, go.api.peerId.id, go.apiAddr, js.api.peerId.addresses[0].toString(), topic)
])
} finally {
await js.stop()
await go.stop()
await app1.stop()
await app2.stop()
}
}

module.exports = runTest

module.exports[pkg.name] = function (browser) {
browser
.url(process.env.IPFS_EXAMPLE_TEST_URL)
.waitForElementVisible('#api-url')
.clearValue('#api-url')
.setValue('#api-url', process.env.IPFS_API_ADDRESS)
.pause(1000)
.click('#node-connect')

browser.expect.element('#console').text.to.contain(`Connecting to ${process.env.IPFS_API_ADDRESS}\nSuccess!`)

// connect to peer
browser
.waitForElementVisible('#peer-addr')
.clearValue('#peer-addr')
.setValue('#peer-addr', process.env.IPFS_PEER_ADDRESS)
.pause(1000)
.click('#peer-connect')

browser.expect.element('#console').text.to.contain(`Connecting to peer ${process.env.IPFS_PEER_ADDRESS}\nSuccess!`)

// subscribe to topic
browser
.waitForElementVisible('#topic')
.clearValue('#topic')
.setValue('#topic', process.env.IPFS_TOPIC)
.pause(1000)
.click('#subscribe')

browser.expect.element('#console').text.to.contain(`Subscribing to ${process.env.IPFS_TOPIC}...\nSuccess!`)

// send a message
browser
.waitForElementVisible('#message')
.clearValue('#message')
.setValue('#message', 'hello')
.pause(1000)
.click('#send')

const remotePeerId = process.env.IPFS_PEER_ADDRESS.split('/').pop()

browser.expect.element('#console').text.to.contain(`from ${remotePeerId}:\n"hello"`)

browser.end()
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Once the daemon is on, run the following commands within this folder:
> npm start
```

Now open your browser at `http://localhost:3000`
Now open your browser at `http://localhost:8888`

You should see the following:

Expand Down
36 changes: 36 additions & 0 deletions examples/http-client-bundle-webpack/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "example-http-client-bundle-webpack",
"version": "1.0.0",
"description": "Bundle js-ipfs-http-client with Webpack",
"scripts": {
"clean": "rm -rf ./dist",
"build": "webpack",
"start": "node server.js",
"test": "test-ipfs-example"
},
"author": "Victor Bjelkholm <victor@ipfs.io>",
"license": "MIT",
"keywords": [],
"dependencies": {
"ipfs-http-client": "^44.3.0",
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.3.1",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.5",
"copy-webpack-plugin": "^5.0.4",
"execa": "^4.0.0",
"ipfs": "^0.47.0",
"ipfsd-ctl": "^4.1.1",
"react-hot-loader": "^4.12.21",
"test-ipfs-example": "^2.0.3",
"webpack": "^4.43.0",
"webpack-dev-server": "^3.11.0"
},
"browserslist": [
"last 2 versions and not dead and > 2%"
]
}
18 changes: 18 additions & 0 deletions examples/http-client-bundle-webpack/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

const webpack = require('webpack')
const WebpackDevServer = require('webpack-dev-server')
const config = require('./webpack.config')

const wds = new WebpackDevServer(webpack(config), {
hot: true,
historyApiFallback: true
})

wds.listen(8888, 'localhost', (err) => {
if (err) {
throw err
}

console.log('Listening at localhost:8888')
})
Loading

0 comments on commit 5b5eabc

Please sign in to comment.