From d6ce83d1802349418390487dd45aff649ca00683 Mon Sep 17 00:00:00 2001 From: David Dias Date: Sat, 12 Nov 2016 03:56:58 +0000 Subject: [PATCH] fix(lint and polish): add a little more comments --- .../hello.txt | 0 examples/getting-started/index.js | 84 +++++++++++++++++++ examples/gettingstarted/index.js | 40 --------- 3 files changed, 84 insertions(+), 40 deletions(-) rename examples/{gettingstarted => getting-started}/hello.txt (100%) create mode 100644 examples/getting-started/index.js delete mode 100644 examples/gettingstarted/index.js diff --git a/examples/gettingstarted/hello.txt b/examples/getting-started/hello.txt similarity index 100% rename from examples/gettingstarted/hello.txt rename to examples/getting-started/hello.txt diff --git a/examples/getting-started/index.js b/examples/getting-started/index.js new file mode 100644 index 0000000000..4fb7fdf79a --- /dev/null +++ b/examples/getting-started/index.js @@ -0,0 +1,84 @@ +'use strict' + +const IPFS = require('../../src/core') // replace this by line below +// const IPFS = require('ipfs') + +/* + * Create a new IPFS instance, using default repo (fs) on default path (~/.ipfs) + */ +const node = new IPFS() + +const fs = require('fs') + +/* + * Display version of js-ipfs + */ +node.version(gotVersion) + +function gotVersion (err, version) { + if (err) { + return console.error(err) + } + + console.log(version) + + /* + * Load the config into memory (generate the Public Key from the Private Key) + */ + node.load((err) => { + if (err) { + return console.log(err) + } + console.log('Repo was loaded\n') + + /* + * Our instance is set, now let's goOnline (turn on bitswap) and do cool + * stuff + */ + + node.goOnline((err) => { + if (err) { + return console.log(err) + } + + // We can test to see if we actually are online if we want to + if (node.isOnline()) { + console.log('\nYep, we are online') + } + + /* + * Add a file to IPFS - Complete Files API on: + * https://github.com/ipfs/interface-ipfs-core/tree/master/API/files + */ + + const file = { + path: 'hello.txt', + content: fs.createReadStream('./hello.txt') + } + + node.files.add(file, (err, result) => { + if (err) { + return console.error(err) + } + + /* + * Awesome we've added a file so let's retrieve and + * display its contents from IPFS + */ + + console.log('\n', result, '\n') + + node.files.cat(result[0].hash, (err, stream) => { + if (err) { + return console.error(err) + } + + console.log('file content: \n') + + stream.pipe(process.stdout) + stream.on('end', process.exit) + }) + }) + }) + }) +} diff --git a/examples/gettingstarted/index.js b/examples/gettingstarted/index.js deleted file mode 100644 index e7e196a997..0000000000 --- a/examples/gettingstarted/index.js +++ /dev/null @@ -1,40 +0,0 @@ -var fs = require('fs') -var IPFS = require('ipfs') -var node = new IPFS() - -var fileName = './hello.txt' - -// Display version of js-ipfs -node.version(function (err, versionData) { - if (!err) { - console.log(versionData) - // We can load the repo like this. - node.load(function (err) { - // If there is an erro loading the repo we can find out like this. - if (err) { - console.log(err) - } else { - console.log('The repo is loaded now.') - } - // Ok let's go online and do some cool stuff - node.goOnline(function () { - // We can test to see if we actually are online if we want to - if (node.isOnline()) console.log('Yep, we are online') - // Now that we are online now. Let's add a file. - var readStream = fs.createReadStream(fileName) - node.files.add(readStream, function (err, data) { - if (!err) { - // Awesome we've added a file so let's retrieve and display its contents from IPFS - node.files.cat(data[0].hash, function (err, stream) { - if (!err) { - stream.pipe(process.stdout, { end: false }) - // let's call it a day now and go offline - node.goOffline() - } else { console.log('Oops for some reason there was a problem retrieving your file: ' + err) } - }) - } else { console.log('Oops there was a problem: ' + err) } - }) - }) - }) - } else { console.log(err) } -})