Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.
Merged
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
94 changes: 94 additions & 0 deletions examples/basics/index-es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
'use strict'

const fs = require('fs')
const os = require('os')
const path = require('path')
const promisify = require('promisify-es6')
// const IPFS = require('../../src/core')
// replace this by line below if you are using ipfs as a dependency of your
// project
const IPFS = require('ipfs')

/*
* Create a new IPFS instance, using default repo (fs) on default path (~/.ipfs)
*/
const node = new IPFS({
repo: path.join(os.tmpdir() + '/' + new Date().toString()),
init: false,
start: false,
EXPERIMENTAL: {
pubsub: false
}
})

const fileToAdd = {
path: 'hello.txt',
content: fs.createReadStream('./hello.txt')
}

/*
* Display version of js-ipfs
*/
node.version()
.then((version) => {
console.log('IPFS Version:', version.version)
return Promise.resolve()
})

/*
* Initialize the repo for this node
*/
.then(() => {
// we need to promisify node.init to return a promise
return promisify(node.init)({ emptyRepo: true, bits: 2048 })
})

/*
* Take the node online (bitswap, network and so on)
*/
.then(() => {
// we need to promisify node.start to return a promise
return promisify(node.start)()
})

/*
* Add a file to IPFS - Complete Files API on:
* https://github.com/ipfs/interface-ipfs-core/tree/master/API/files
*/
.then(() => {
if (node.isOnline()) {
console.log('\nNode is now ready and online')
}
return node.files.add(fileToAdd)
})

.then((result) => {
let file = result[0]
console.log('\nAdded file:')
console.log(file)
let fileMultihash = file.hash
return Promise.resolve(fileMultihash)
})

/*
* Awesome we've added a file so let's retrieve and
* display its contents from IPFS
*/
.then((fileMultihash) => {
return node.files.cat(fileMultihash)
})

.then((stream) => {
console.log('\nFile content:')
stream.pipe(process.stdout)
stream.on('end', process.exit)
return Promise.resolve()
})

.then(() => {
console.log('Success!')
})

.catch((err) => {
console.log(err)
})