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

Commit 88aeef4

Browse files
authored
docs/improve newcomers experience (#723)
* docs: update basics example * docs: improve documentation on the readme * docs: update webpack example to have a ref to browserify-zlib-next * docs(examples): update browserify example and add more information about zlib shim * docs(examples): revamp basics example, make it init the repo on tmp * docs(example): fix webpack example
1 parent 55b8171 commit 88aeef4

File tree

20 files changed

+229
-172
lines changed

20 files changed

+229
-172
lines changed

README.md

Lines changed: 53 additions & 48 deletions
Large diffs are not rendered by default.

examples/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# `js-ipfs` Examples and Tutorials
2+
3+
In this folder, you can find a variety of examples to help you get started in using js-ipfs, in Node.js and in the Browser. Every example as a specific purpose and some of each incorporate a full tutorial that you can follow through, helping you expand your knowledge about IPFS and the Distributed Web in General.
4+
5+
Let us know if you find any issue or if you want to contribute and add a new tutorial, feel welcome to submit a PR, thank you!
6+
7+
## Examples
8+
9+
- [js-ipfs basic, how to spawn a node and add a file to IPFS](/examples)
10+
- [How to bundle js-ipfs with Browserify](/bundle-browserify)
11+
- [How to bundle js-ipfs with WebPack](/bundle-webpack)
12+
13+
## Tutorials

examples/basics/hello.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, how are you today? Welcome to the Distributed Web!

examples/basics/index.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'use strict'
2+
3+
const fs = require('fs')
4+
const os = require('os')
5+
const series = require('async/series')
6+
const IPFS = require('../../src/core') // replace this by line below
7+
// const IPFS = require('ipfs')
8+
9+
/*
10+
* Create a new IPFS instance, using default repo (fs) on default path (~/.ipfs)
11+
*/
12+
const node = new IPFS(os.tmpDir() + '/' + new Date().toString())
13+
14+
const fileToAdd = {
15+
path: 'hello.txt',
16+
content: fs.createReadStream('./hello.txt')
17+
}
18+
19+
let fileMultihash
20+
21+
series([
22+
/*
23+
* Display version of js-ipfs
24+
*/
25+
(cb) => {
26+
node.version((err, version) => {
27+
if (err) { return cb(err) }
28+
29+
console.log('IPFS Version:', version.version)
30+
cb()
31+
})
32+
},
33+
/*
34+
* Initialize the repo for this node
35+
*/
36+
(cb) => node.init({ emptyRepo: true, bits: 2048 }, cb),
37+
/*
38+
* Load the repo config into the IPFS node
39+
*/
40+
(cb) => node.load(cb),
41+
/*
42+
* Take the node online (bitswap, network and so on)
43+
*/
44+
(cb) => node.goOnline(cb),
45+
/*
46+
* Add a file to IPFS - Complete Files API on:
47+
* https://github.com/ipfs/interface-ipfs-core/tree/master/API/files
48+
*/
49+
(cb) => {
50+
if (node.isOnline()) {
51+
console.log('\nNode is now ready and online')
52+
}
53+
54+
node.files.add(fileToAdd, (err, result) => {
55+
if (err) { return cb(err) }
56+
57+
console.log('\nAdded file:')
58+
console.log(result[0])
59+
fileMultihash = result[0].hash
60+
cb()
61+
})
62+
},
63+
/*
64+
* Awesome we've added a file so let's retrieve and
65+
* display its contents from IPFS
66+
*/
67+
(cb) => {
68+
node.files.cat(fileMultihash, (err, stream) => {
69+
if (err) { return cb(err) }
70+
71+
console.log('\nFile content:')
72+
stream.pipe(process.stdout)
73+
stream.on('end', process.exit)
74+
})
75+
}
76+
], (err) => {
77+
if (err) {
78+
return console.log(err)
79+
}
80+
console.log('Success!')
81+
})

examples/bundle-browserify/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ You should see the following:
1515

1616
![](https://ipfs.io/ipfs/QmNtpcWCEd6LjdPNfBFDaVZdD4jpgT8ZTAwoFJXKhYMJdo/1.png)
1717
![](https://ipfs.io/ipfs/QmNtpcWCEd6LjdPNfBFDaVZdD4jpgT8ZTAwoFJXKhYMJdo/2.png)
18+
19+
## Special note
20+
21+
In order to use js-ipfs in the browser, you need to replace the default `zlib` library by `browserify-zlib-next`, a full implementation of the native `zlib` package, full in Node.js.
22+
See the package.json to learn how to do this and avoid this pitfall. More context on: https://github.com/ipfs/js-ipfs#use-in-the-browser-with-browserify-webpack-or-any-bundler

examples/bundle-browserify/package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
"description": "Bundle js-ipfs with Browserify",
55
"main": "index.js",
66
"scripts": {
7-
"start": "browserify index.js > bundle.js && http-server -a 127.0.0.1 -p 8888"
7+
"bundle": "browserify src/index.js > public/bundle.js",
8+
"serve": "http-server public -a 127.0.0.1 -p 8888",
9+
"start": "npm run bundle && npm run serve"
10+
},
11+
"browser": {
12+
"zlib": "browserify-zlib-next"
813
},
914
"keywords": [],
1015
"license": "MIT",
1116
"devDependencies": {
1217
"browserify": "^13.1.1",
1318
"concat-stream": "^1.5.2",
1419
"http-server": "^0.9.0",
15-
"ipfs": "^0.18.0"
20+
"browserify-zlib-next": "^1.0.1"
1621
},
1722
"dependencies": {}
1823
}

examples/bundle-browserify/index.js renamed to examples/bundle-browserify/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

3-
var IPFS = require('ipfs')
3+
var IPFS = require('../../../src/core') // replace this by line below
4+
// var IPFS = require('ipfs')
45

56
// Create the IPFS node instance
67
// for simplicity, we create a new repo everytime the node

examples/bundle-webpack/.babelrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"stage": 0
2+
"presets": [
3+
"stage-0",
4+
"react"
5+
]
36
}

examples/bundle-webpack/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ You should see the following:
1717

1818
![](https://ipfs.io/ipfs/QmZndNLRct3co7h1yVB72S4qfwAwbq7DQghCpWpVQ45jSi/1.png)
1919

20+
## Special note
21+
22+
In order to use js-ipfs in the browser, you need to replace the default `zlib` library by `browserify-zlib-next`, a full implementation of the native `zlib` package, full in Node.js. See the WebPack config to learn how to do this and avoid this pitfall. More context on: https://github.com/ipfs/js-ipfs#use-in-the-browser-with-browserify-webpack-or-any-bundler
23+
24+
## Special note 2
25+
26+
You need to use WebPack@2 or above, version 1 won't work
File renamed without changes.

examples/bundle-webpack/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
<title>Sample App</title>
44
</head>
55
<body>
6-
<div id='root'>
7-
</div>
6+
<div id='root'></div>
87
<script src="/static/bundle.js"></script>
98
</body>
109
</html>

examples/bundle-webpack/package.json

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
"license": "MIT",
99
"keywords": [],
1010
"devDependencies": {
11-
"babel-core": "^5.4.7",
12-
"babel-loader": "^5.1.2",
13-
"ipfs": "^0.18.0",
14-
"json-loader": "^0.5.3",
15-
"react": "^0.13.0",
16-
"react-hot-loader": "^1.3.0",
17-
"webpack": "^1.9.6",
18-
"webpack-dev-server": "^1.8.2"
11+
"babel-core": "^6.22.1",
12+
"babel-loader": "^6.2.10",
13+
"babel-preset-react": "^6.22.0",
14+
"json-loader": "^0.5.4",
15+
"react": "^15.4.2",
16+
"react-hot-loader": "^1.3.1",
17+
"webpack": "^2.2.0",
18+
"webpack-dev-server": "^1.16.2"
19+
},
20+
"dependencies": {
21+
"browserify-zlib-next": "^1.0.1"
1922
}
2023
}

examples/bundle-webpack/server.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
'use strict'
2-
var webpack = require('webpack')
3-
var WebpackDevServer = require('webpack-dev-server')
4-
var config = require('./webpack.config')
52

6-
new WebpackDevServer(webpack(config), {
3+
const webpack = require('webpack')
4+
const WebpackDevServer = require('webpack-dev-server')
5+
const config = require('./webpack.config')
6+
7+
const wds = new WebpackDevServer(webpack(config), {
78
publicPath: config.output.publicPath,
89
hot: true,
910
historyApiFallback: true
10-
}).listen(3000, 'localhost', function (err, result) {
11+
})
12+
13+
wds.listen(3000, 'localhost', (err, result) => {
1114
if (err) {
12-
console.log(err)
15+
throw err
1316
}
1417

1518
console.log('Listening at localhost:3000')

examples/bundle-webpack/src/App.js renamed to examples/bundle-webpack/src/components/app.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict'
2+
23
const React = require('react')
3-
const IPFS = require('ipfs')
4+
const IPFS = require('../../../../src/core') // replace this by line below
5+
// const IPFS = require('ipfs')
46

57
const stringToUse = 'hello world from webpacked IPFS'
68

@@ -83,22 +85,25 @@ class App extends React.Component {
8385
}
8486
}
8587
render () {
86-
return <div style={{textAlign: 'center'}}>
87-
<h1>Everything is working!</h1>
88-
<p>Your ID is <strong>{this.state.id}</strong></p>
89-
<p>Your IPFS version is <strong>{this.state.version}</strong></p>
90-
<p>Your IPFS protocol version is <strong>{this.state.protocol_version}</strong></p>
91-
<div>
88+
return (
89+
<div style={{textAlign: 'center'}}>
90+
<h1>Everything is working!</h1>
91+
<p>Your ID is <strong>{this.state.id}</strong></p>
92+
<p>Your IPFS version is <strong>{this.state.version}</strong></p>
93+
<p>Your IPFS protocol version is <strong>{this.state.protocol_version}</strong></p>
94+
<hr />
9295
<div>
9396
Added a file! <br />
9497
{this.state.added_file_hash}
9598
</div>
96-
<div>
99+
<br />
100+
<br />
101+
<p>
97102
Contents of this file: <br />
98103
{this.state.added_file_contents}
99-
</div>
104+
</p>
100105
</div>
101-
</div>
106+
)
102107
}
103108
}
104109
module.exports = App
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
const React = require('react')
4+
const ReactDOM = require('react-dom')
5+
const App = require('./app')
6+
7+
ReactDOM.render(<App />, document.getElementById('root'))

examples/bundle-webpack/src/index.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

examples/bundle-webpack/webpack.config.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
entry: [
99
'webpack-dev-server/client?http://localhost:3000',
1010
'webpack/hot/only-dev-server',
11-
'./src/index'
11+
'./src/components/index'
1212
],
1313
output: {
1414
path: path.join(__dirname, 'dist'),
@@ -21,13 +21,24 @@ module.exports = {
2121
module: {
2222
loaders: [{
2323
test: /\.js$/,
24-
loaders: ['react-hot', 'babel'],
24+
loaders: ['react-hot-loader', 'babel-loader'],
2525
include: path.join(__dirname, 'src')
2626
}, { test: /\.json$/, loader: 'json-loader' }]
2727
},
2828
node: {
2929
fs: 'empty',
3030
net: 'empty',
3131
tls: 'empty'
32+
},
33+
/*
34+
* In order to transfer files, this is a very important step in your Webpack
35+
* configuration, see more at:
36+
* https://github.com/ipfs/js-ipfs#use-in-the-browser-with-browserify-webpack-or-any-bundler
37+
*/
38+
resolve: {
39+
alias: {
40+
zlib: 'browserify-zlib-next'
41+
}
3242
}
43+
3344
}

examples/getting-started/hello.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)