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

Commit 8b13127

Browse files
authored
Merge pull request #607 from ipfs/feat/examples
Feat/examples
2 parents 5baa85f + 0102548 commit 8b13127

File tree

18 files changed

+365
-0
lines changed

18 files changed

+365
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bundle.js
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Bundle js-ipfs with Browserify!
2+
3+
> In this example, you will find a boilerplate you can use to guide yourself into bundling js-ipfs with browserify, so that you can use it in your own web app!
4+
5+
## Run this example
6+
7+
```bash
8+
> npm install
9+
> npm start
10+
```
11+
12+
Now open your browser at `http://localhost:8888`
13+
14+
You should see the following:
15+
16+
![](https://ipfs.io/ipfs/QmNtpcWCEd6LjdPNfBFDaVZdD4jpgT8ZTAwoFJXKhYMJdo/1.png)
17+
![](https://ipfs.io/ipfs/QmNtpcWCEd6LjdPNfBFDaVZdD4jpgT8ZTAwoFJXKhYMJdo/2.png)
98.2 KB
Loading
143 KB
Loading
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8"/>
5+
<title>JS IPFS API - Example - Browser - Add</title>
6+
<script src="bundle.js"></script>
7+
<style>
8+
.content {
9+
border: 1px solid black;
10+
padding: 10px;
11+
margin: 5px 0;
12+
}
13+
</style>
14+
</head>
15+
<body>
16+
<h1>JS IPFS API - Add data to IPFS from the browser</h1>
17+
<textarea id="source">
18+
</textarea>
19+
<button id="store">add to ipfs</button>
20+
<div>
21+
<div>found in ipfs:</div>
22+
<div class="content" id="hash">[ipfs hash]</div>
23+
<div class="content" id="content">[ipfs content]</div>
24+
</div>
25+
</body>
26+
</html>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict'
2+
3+
var IPFS = require('ipfs')
4+
5+
// Create the IPFS node instance
6+
// for simplicity, we create a new repo everytime the node
7+
// is created, because you can't init already existing repos
8+
const repoPath = '' + Math.random()
9+
const node = new IPFS(repoPath)
10+
const concat = require('concat-stream')
11+
12+
node.init({ emptyRepo: true, bits: 2048 }, function (err) {
13+
if (err) {
14+
throw err
15+
}
16+
node.load(function (err) {
17+
if (err) {
18+
throw err
19+
}
20+
21+
node.goOnline(function (err) {
22+
if (err) {
23+
throw err
24+
}
25+
console.log('IPFS node is ready')
26+
})
27+
})
28+
})
29+
30+
function store () {
31+
var toStore = document.getElementById('source').value
32+
33+
node.files.add(new Buffer(toStore), function (err, res) {
34+
if (err || !res) {
35+
return console.error('ipfs add error', err, res)
36+
}
37+
38+
res.forEach(function (file) {
39+
if (file && file.hash) {
40+
console.log('successfully stored', file.hash)
41+
display(file.hash)
42+
}
43+
})
44+
})
45+
}
46+
47+
function display (hash) {
48+
// buffer: true results in the returned result being a buffer rather than a stream
49+
node.files.cat(hash, function (err, res) {
50+
if (err || !res) {
51+
return console.error('ipfs cat error', err, res)
52+
}
53+
54+
document.getElementById('hash').innerText = hash
55+
56+
res.pipe(concat(function (data) {
57+
document.getElementById('content').innerText = data
58+
}))
59+
})
60+
}
61+
62+
document.addEventListener('DOMContentLoaded', function () {
63+
document.getElementById('store').onclick = store
64+
})
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "bundle-browserify",
3+
"version": "1.0.0",
4+
"description": "Bundle js-ipfs with Browserify",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "browserify index.js > bundle.js && http-server -a 127.0.0.1 -p 8888"
8+
},
9+
"keywords": [],
10+
"license": "MIT",
11+
"devDependencies": {
12+
"browserify": "^13.1.1",
13+
"concat-stream": "^1.5.2",
14+
"http-server": "^0.9.0",
15+
"ipfs": "^0.18.0"
16+
},
17+
"dependencies": {}
18+
}

examples/bundle-webpack/.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"stage": 0
3+
}

examples/bundle-webpack/.eslintrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "standard",
3+
"rules": {
4+
"react/jsx-uses-react": 2,
5+
"react/jsx-uses-vars": 2,
6+
"react/react-in-jsx-scope": 2
7+
},
8+
"plugins": [
9+
"react"
10+
]
11+
}

examples/bundle-webpack/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
npm-debug.log
3+
.DS_Store
4+
dist

0 commit comments

Comments
 (0)