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

docs(examples): move to webpack #335

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions examples/browser-add-file/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# JS IPFS API - Example Browser - Add

## Setup

Install [go-ipfs](https://ipfs.io/docs/install/) and run it

```bash
$ ipfs daemon
```

then in this folder run

```bash
$ npm install
$ npm start
```

and open your browser at `http://localhost:8888`
28 changes: 28 additions & 0 deletions examples/browser-add-file/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>JS IPFS API - Example - Browser - Add</title>
<script src="https://wzrd.in/standalone/buffer"></script> <!-- feross buffer -->
<script src="https://npmcdn.com/ipfs-api/dist/index.js"></script>
<script src="index.js"></script>
</head>
<body>
<div>
<h1>Add text - <i>works</i></h1>
<textarea id="source">
</textarea>
<button id="storeText">create in ipfs</button>
</div>
<div>
<h1>Add buffer - <i>does not work</i></h1>
<input type="file" id="filePicker">
<button id="storeBuffer">create in ipfs</button>
</div>
<div>
<h1>Response from ipfs - <i>stream res unhandled</i></h1>
<div id="hash">[ipfs hash]</div>
<div id="content">[ipfs content]</div>
</div>
</body>
</html>
50 changes: 50 additions & 0 deletions examples/browser-add-file/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* globals FileReader */
'use strict'

var ipfs = window.IpfsApi()

function storeText () {
store(document.getElementById('source').value)
}

function storeFile () {
const file = document.getElementById('filePicker').files[0]
const reader = new FileReader()
reader.onload = function () {
store(reader.result)
}
reader.readAsArrayBuffer(file)
}

// from examples/browser-add
function store (toStore) {
ipfs.add(new window.buffer.Buffer(toStore), function (err, res) {
if (err || !res) {
return console.error('ipfs add error', err, res)
}

res.forEach(function (file) {
console.log('successfully stored', file)
display(file.path)
})
})
}

function display (hash) {
ipfs.cat(hash, function (err, res) {
if (err || !res) {
return console.error('ipfs cat error', err, res)
}
if (res.readable) {
console.error('unhandled: cat result is a pipe', res)
} else {
document.getElementById('hash').innerText = hash
document.getElementById('content').innerText = res
}
})
}

document.addEventListener('DOMContentLoaded', function () {
document.getElementById('storeText').onclick = storeText
document.getElementById('storeBuffer').onclick = storeFile
})
16 changes: 16 additions & 0 deletions examples/browser-add-file/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "ipfs-api-example-browser-add-file",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "http-server -a 127.0.0.1 -p 8888"
},
"keywords": [],
"author": "nycoliver",
"license": "MIT",
"devDependencies": {
"http-server": "^0.9.0",
"ipfs-api": "^6.0.3"
}
}
8 changes: 5 additions & 3 deletions examples/browser-add/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ function store () {
}

res.forEach(function (file) {
console.log('successfully stored', file.Hash)
display(file.Hash)
console.log('successfully stored', file)
display(file.path)
})
})
}
Expand All @@ -31,4 +31,6 @@ function display (hash) {
})
}

document.getElementById('store').onclick = store
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('store').onclick = store
})
8 changes: 5 additions & 3 deletions examples/browser-add/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "browserify -t brfs index.js > bundle.js && http-server -a 127.0.0.1 -p 8888"
"start": "webpack index.js bundle.js && http-server -a 127.0.0.1 -p 8888"
},
"keywords": [],
"author": "Friedel Ziegelmayer",
"license": "MIT",
"devDependencies": {
"brfs": "^1.4.3",
"browserify": "^13.0.1",
"http-server": "^0.9.0",
"ipfs-api": "^6.0.3"
"ipfs-api": "^6.0.3",
"json-loader": "^0.5.4",
"transform-loader": "^0.2.3",
"webpack": "^2.1.0-beta.20"
}
}
14 changes: 14 additions & 0 deletions examples/browser-add/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

module.exports = {
module: {
loaders: [{
test: /\.json/,
loader: 'json'
}],
preLoaders: [{
test: /\.js$/,
loader: 'transform?brfs'
}]
}
}