This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
index.js
71 lines (58 loc) · 1.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict'
const concat = require('concat-stream')
const IPFS = require('../../../src/core') // replace this by line below
// var IPFS = require('ipfs')
// Create the IPFS node instance
// for simplicity, we create a new repo everytime the node
// is created, because you can't init already existing repos
const repoPath = String(Math.random())
const node = new IPFS({
repo: repoPath,
init: false,
start: false,
EXPERIMENTAL: {
pubsub: false
}
})
// expose the node to the window, for the fun!
window.ipfs = node
node.init({ emptyRepo: true, bits: 2048 }, function (err) {
if (err) {
throw err
}
node.start(function (err) {
if (err) {
throw err
}
console.log('IPFS node is ready')
})
})
function store () {
var toStore = document.getElementById('source').value
node.files.add(new Buffer(toStore), function (err, res) {
if (err || !res) {
return console.error('ipfs add error', err, res)
}
res.forEach(function (file) {
if (file && file.hash) {
console.log('successfully stored', file.hash)
display(file.hash)
}
})
})
}
function display (hash) {
// buffer: true results in the returned result being a buffer rather than a stream
node.files.cat(hash, function (err, res) {
if (err || !res) {
return console.error('ipfs cat error', err, res)
}
document.getElementById('hash').innerText = hash
res.pipe(concat(function (data) {
document.getElementById('content').innerText = data
}))
})
}
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('store').onclick = store
})