69 lines
2.0 KiB
JavaScript
69 lines
2.0 KiB
JavaScript
'use strict'
|
|
|
|
const IPFS = require('ipfs')
|
|
const Repo = require('ipfs-repo')
|
|
const LevelStore = require('datastore-level')
|
|
|
|
function MemoryLevelStore() {
|
|
return new LevelStore('/memory', {db: require('memdown')})
|
|
}
|
|
MemoryLevelStore.prototype = new LevelStore('/memory', {db: require('memdown')})
|
|
MemoryLevelStore.prototype.constructor = MemoryLevelStore
|
|
|
|
const stringToUse = 'Hello world from self-contained, self-hosted js-ipfs!!!'
|
|
|
|
function App() {
|
|
let node
|
|
|
|
create()
|
|
|
|
function create () {
|
|
// Create the IPFS node instance
|
|
|
|
var repo_name = String(Math.random() + Date.now())
|
|
var storage_backends = {
|
|
root: MemoryLevelStore,
|
|
blocks: MemoryLevelStore,
|
|
datastore: MemoryLevelStore,
|
|
}
|
|
|
|
node = new IPFS({ repo: new Repo(repo_name, {storageBackends: storage_backends}) })
|
|
|
|
node.once('ready', () => {
|
|
console.log('IPFS node is ready')
|
|
ops()
|
|
})
|
|
}
|
|
|
|
function ops () {
|
|
node.id((err, res) => {
|
|
if (err) {
|
|
throw err
|
|
}
|
|
|
|
document.body.insertAdjacentHTML('beforeend',
|
|
"<h1>Everything is working!</h1>" +
|
|
"<p>Your ID is <strong>" + res.id + "</strong></p>" +
|
|
"<p>Your IPFS version is <strong>" + res.agentVersion + "</strong></p>" +
|
|
"<p>Your IPFS protocol version is <strong>" + res.protocolVersion + "</strong></p>"
|
|
)
|
|
})
|
|
|
|
node.files.add([Buffer.from(stringToUse)], (err, filesAdded) => {
|
|
if (err) { console.log("ERROR"); throw err }
|
|
|
|
const hash = filesAdded[0].hash
|
|
document.body.insertAdjacentHTML('beforeend', "<hr /><div>Added a file!<br /><a href=\"https://gateway.ipfs.io/ipfs/" + hash + "\">" + hash + "</a></div>")
|
|
|
|
node.files.cat(hash, (err, data) => {
|
|
if (err) { console.log("ERROR 2"); throw err }
|
|
document.body.insertAdjacentHTML('beforeend', "<p>Contents of this file: <br />" + data + "</p>")
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
module.exports = App
|
|
|
|
App()
|