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

Add basic example using ES7 async/await #865

Closed
wants to merge 1 commit into from
Closed
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
73 changes: 73 additions & 0 deletions examples/basics/index-es7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'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')
}

async function main () {
try {
/*
* Display version of js-ipfs
*/
let version = await node.version()
console.log('IPFS Version:', version)

/*
* Initialize the repo for this node
*/
await promisify(node.init)({ emptyRepo: true, bits: 2048 })

/*
* Take the node online (bitswap, network and so on)
*/
await promisify(node.start)()
console.log('IPFS node is online')

/*
* Add a file to IPFS - Complete Files API on:
* https:/ipfs/interface-ipfs-core/tree/master/API/files
*/
let addedFiles = await node.files.add(fileToAdd)
let addedFile = addedFiles[0]
console.log('\nAdded file:')
console.log(addedFile)

/*
* Awesome we've added a file so let's retrieve and
* display its contents from IPFS
*/
let stream = await node.files.cat(addedFile.hash)
console.log('\nFile content:\n')
stream.pipe(process.stdout)
stream.on('end', process.exit)

console.log('Success!')

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

main()