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
}
File renamed without changes.

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

0 commit comments

Comments
 (0)