Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 74a9147

Browse files
hugomrdiasAlan Shaw
and
Alan Shaw
authored
docs: port examples to new api (#1222)
* fix: change name-api examples to new api * fix: change files-api example to new api * fix: change upload example to new api * fix: change bundle-webpack to support new api * fix: remove browserify example doesnt support imports * fix: change pubsub example to the new api * Update examples/bundle-webpack/src/App.js Co-Authored-By: Alan Shaw <[email protected]> * Update examples/bundle-webpack/src/App.js Co-Authored-By: Alan Shaw <[email protected]> * Update examples/files-api/files-api.js Co-Authored-By: Alan Shaw <[email protected]> * Update examples/upload-file-via-browser/src/App.js Co-Authored-By: Alan Shaw <[email protected]> * Update examples/upload-file-via-browser/src/App.js Co-Authored-By: Alan Shaw <[email protected]> Co-authored-by: Alan Shaw <[email protected]>
1 parent d3eee0d commit 74a9147

File tree

22 files changed

+24529
-1200
lines changed

22 files changed

+24529
-1200
lines changed

examples/browser-pubsub/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ <h1 class="aqua fw2 montserrat dib ma0 pv2 ph1 v-mid fr f3 lh-copy">Pubsub</h1>
3737
<div id="console" class="f7 db w-100 ph1 pv2 monospace input-reset ba b--black-20 border-box overflow-scroll" style="height: 300px">
3838
</div>
3939
</div>
40-
<script src="bundle.js"></script>
40+
<script src="index.js"></script>
4141
</body>
4242
</html>

examples/browser-pubsub/package.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
"private": true,
66
"main": "index.js",
77
"scripts": {
8-
"start": "npm run build && npm run serve",
9-
"build": "browserify index.js > bundle.js",
10-
"serve": "http-server -a 127.0.0.1 -p 8888",
11-
"test": "echo \"Error: no test specified\" && exit 1"
8+
"start": "parcel index.html"
129
},
1310
"author": "Alan Shaw",
1411
"license": "MIT",
1512
"dependencies": {
16-
"browserify": "^16.5.0",
17-
"http-server": "^0.11.1",
1813
"ipfs-http-client": "../../"
14+
},
15+
"browserslist": [
16+
"last 2 versions and not dead and > 2%"
17+
],
18+
"devDependencies": {
19+
"parcel-bundler": "^1.12.4"
1920
}
2021
}

examples/bundle-browserify/.gitignore

-1
This file was deleted.

examples/bundle-browserify/README.md

-35
This file was deleted.

examples/bundle-browserify/img/1.png

-98.2 KB
Binary file not shown.

examples/bundle-browserify/img/2.png

-143 KB
Binary file not shown.

examples/bundle-browserify/index.html

-26
This file was deleted.

examples/bundle-browserify/index.js

-36
This file was deleted.

examples/bundle-browserify/package.json

-18
This file was deleted.

examples/bundle-webpack/package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
"webpack-dev-server": "~3.3.1"
2222
},
2323
"browserslist": [
24-
">1%",
25-
"not dead",
26-
"not ie <= 11",
27-
"not op_mini all"
24+
"last 2 versions and not dead and > 2%"
2825
]
2926
}

examples/bundle-webpack/src/App.js

+20-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const React = require('react')
33
const ipfsClient = require('ipfs-http-client')
44

5-
const ipfs = ipfsClient('localhost', '5001')
5+
const ipfs = ipfsClient('/ip4/127.0.0.1/tcp/5001')
66
const stringToUse = 'hello world from webpacked IPFS'
77

88
class App extends React.Component {
@@ -16,24 +16,27 @@ class App extends React.Component {
1616
added_file_contents: null
1717
}
1818
}
19-
componentDidMount () {
20-
ipfs.id((err, res) => {
21-
if (err) throw err
22-
this.setState({
23-
id: res.id,
24-
version: res.agentVersion,
25-
protocol_version: res.protocolVersion
26-
})
19+
async componentDidMount () {
20+
const id = await ipfs.id()
21+
this.setState({
22+
id: id.id,
23+
version: id.agentVersion,
24+
protocol_version: id.protocolVersion
2725
})
28-
ipfs.add([Buffer.from(stringToUse)], (err, res) => {
29-
if (err) throw err
30-
const hash = res[0].hash
26+
27+
const source = ipfs.add(stringToUse)
28+
for await (const file of source) {
29+
console.log("TCL: App -> forawait -> file", file)
30+
const hash = file.path
3131
this.setState({ added_file_hash: hash })
32-
ipfs.cat(hash, (err, data) => {
33-
if (err) throw err
34-
this.setState({ added_file_contents: data.toString() })
35-
})
36-
})
32+
33+
const source = ipfs.cat(hash)
34+
const data = []
35+
for await (const chunk of source) {
36+
data.push(chunk)
37+
}
38+
this.setState({ added_file_contents: Buffer.concat(data).toString() })
39+
}
3740
}
3841
render () {
3942
return <div style={{ textAlign: 'center' }}>

examples/files-api/files-api.js

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
/* eslint-disable no-console */
12
'use strict'
23

3-
const ipfs = require('../../src')('localhost', 5001)
4+
// Run `ipfs daemon` in your terminal to start the IPFS daemon
5+
// Look for `API server listening on /ip4/127.0.0.1/tcp/5001`
6+
const ipfs = require('../../src')('/ip4/127.0.0.1/tcp/5001')
47

5-
ipfs.files.ls('/folder1', function (err, res) {
6-
if (err) {
7-
return console.log('got an error', err)
8-
}
9-
if (res.readable) {
10-
res.pipe(process.stdout)
11-
} else {
12-
console.log(res)
8+
const run = async () => {
9+
await ipfs.files.write(
10+
'/temp/hello-world',
11+
Buffer.from('Hello, world!'),
12+
{ create: true, parents: true }
13+
)
14+
const source = ipfs.files.ls('/temp')
15+
16+
for await (const file of source) {
17+
console.log(file)
1318
}
14-
})
19+
}
20+
21+
run()

examples/name-api/index.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ <h3>Resolve an IPNS name</h3>
5858
</p>
5959
</div>
6060

61-
<script src="https://unpkg.com/ipfs-http-client/dist/index.js"></script>
62-
<script src="bundle.js"></script>
61+
<script src="index.js"></script>
6362
</body>
6463
</html>

examples/name-api/index.js

+28-29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
/* eslint-disable no-console */
12
'use strict'
2-
3-
const ipfs = window.IpfsHttpClient('/ip4/127.0.0.1/tcp/5001')
3+
const ipfsHttp = require('ipfs-http-client')
4+
const ipfs = ipfsHttp('/ip4/127.0.0.1/tcp/5001')
45

56
const DOM = {
67
status: document.getElementById('status'),
@@ -30,7 +31,7 @@ const showStatus = (text, bg) => {
3031
}
3132

3233
const enableForms = () => {
33-
for (let btn of DOM.buttons) {
34+
for (const btn of DOM.buttons) {
3435
btn.disabled = false
3536
}
3637
}
@@ -48,31 +49,30 @@ const init = () => {
4849
}
4950

5051
// Adds a new file to IPFS and publish it
51-
const addAndPublish = (e) => {
52+
const addAndPublish = async (e) => {
5253
e.preventDefault()
5354

54-
let input = e.target.elements['text']
55-
let buffer = Buffer.from(input.value)
55+
const input = e.target.elements.text
56+
const buffer = Buffer.from(input.value)
5657

5758
showStatus('adding to IPFS...', COLORS.active)
59+
try {
60+
for await (const file of ipfs.add(buffer)) {
61+
showStatus('success!', COLORS.success)
5862

59-
ipfs.add(buffer)
60-
.then(res => {
61-
showStatus(`success!`, COLORS.success)
62-
63-
publish(res[0].path)
63+
publish(file.path)
6464

6565
input.value = ''
66-
})
67-
.catch(err => {
68-
showStatus('failed to add the data', COLORS.error)
69-
console.error(err)
70-
})
66+
}
67+
} catch (err) {
68+
showStatus('failed to add the data', COLORS.error)
69+
console.error(err)
70+
}
7171
}
7272

7373
// Publishes an IPFS file or directory under your node's identity
7474
const publish = (path) => {
75-
showStatus(`publishing...`, COLORS.active)
75+
showStatus('publishing...', COLORS.active)
7676
DOM.publishResultsDiv.classList.add('hidden')
7777

7878
ipfs.name.publish(path)
@@ -90,36 +90,35 @@ const publish = (path) => {
9090
}
9191

9292
// Resolves an IPNS name
93-
const resolve = (name) => {
94-
showStatus(`resolving...`, COLORS.active)
93+
const resolve = async (name) => {
94+
showStatus('resolving...', COLORS.active)
9595
DOM.resolveResultsDiv.classList.add('hidden')
96-
97-
ipfs.name.resolve(name)
98-
.then(path => {
96+
try {
97+
for await (const path of ipfs.name.resolve(name)) {
9998
showStatus('success!', COLORS.success)
10099
DOM.resolveResultsDiv.classList.remove('hidden')
101100
DOM.resolveResult.innerText = path
102101
DOM.resolveGatewayLink.href = `${IPFS_DOMAIN}${path}`
103-
})
104-
.catch(err => {
105-
showStatus(`error resolving ${name}`, COLORS.error)
106-
console.error(err)
107-
})
102+
}
103+
} catch (err) {
104+
showStatus(`error resolving ${name}`, COLORS.error)
105+
console.error(err)
106+
}
108107
}
109108

110109
// Event listeners
111110
DOM.publishNew.onsubmit = addAndPublish
112111

113112
DOM.publishPath.onsubmit = (e) => {
114113
e.preventDefault()
115-
let input = e.target.elements['path']
114+
const input = e.target.elements.path
116115
publish(input.value)
117116
input.value = ''
118117
}
119118

120119
DOM.resolveName.onsubmit = (e) => {
121120
e.preventDefault()
122-
let input = e.target.elements['name']
121+
const input = e.target.elements.name
123122
resolve(input.value)
124123
input.value = ''
125124
}

0 commit comments

Comments
 (0)