From 7d9bff2fd34fa09986da868fe09104a60b538150 Mon Sep 17 00:00:00 2001 From: Enrico Marino Date: Wed, 24 May 2017 22:56:20 +0200 Subject: [PATCH] Add basic example using ES7 async/await --- examples/basics/index-es7.js | 73 ++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 examples/basics/index-es7.js diff --git a/examples/basics/index-es7.js b/examples/basics/index-es7.js new file mode 100644 index 0000000000..db431acae7 --- /dev/null +++ b/examples/basics/index-es7.js @@ -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://github.com/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()