From 8ec577871de8a0d361f5dd109d94901108edc0b5 Mon Sep 17 00:00:00 2001 From: nginnever Date: Sat, 21 May 2016 16:12:56 -0700 Subject: [PATCH 1/4] updated cat core to return just file stream --- src/cli/commands/files/cat.js | 6 ++---- src/core/ipfs/files.js | 4 +++- src/http-api/resources/files.js | 4 +--- test/core-tests/test-files.js | 12 +++++------- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/cli/commands/files/cat.js b/src/cli/commands/files/cat.js index c517b8ad3e..ff87a82a69 100644 --- a/src/cli/commands/files/cat.js +++ b/src/cli/commands/files/cat.js @@ -31,13 +31,11 @@ module.exports = Command.extend({ }) return } - ipfs.files.cat(path, (err, res) => { + ipfs.files.cat(path, (err, file) => { if (err) { throw (err) } - res.on('data', (data) => { - data.stream.pipe(process.stdout) - }) + file.pipe(process.stdout) }) }) } diff --git a/src/core/ipfs/files.js b/src/core/ipfs/files.js index 4b4dcf8c34..aa09337154 100644 --- a/src/core/ipfs/files.js +++ b/src/core/ipfs/files.js @@ -45,7 +45,9 @@ module.exports = function files (self) { callback('This dag node is a directory', null) } else { const exportStream = Exporter(hash, self._dagS) - callback(null, exportStream) + exportStream.on('data', (object) => { + callback(null, object.stream) + }) } }) }, diff --git a/src/http-api/resources/files.js b/src/http-api/resources/files.js index 8647a8e314..23d477e7a4 100644 --- a/src/http-api/resources/files.js +++ b/src/http-api/resources/files.js @@ -42,9 +42,7 @@ exports.cat = { Code: 0 }).code(500) } - stream.on('data', (data) => { - return reply(data.stream) - }) + return reply(stream) }) } } diff --git a/test/core-tests/test-files.js b/test/core-tests/test-files.js index 3be2be1530..1c16a3ab7e 100644 --- a/test/core-tests/test-files.js +++ b/test/core-tests/test-files.js @@ -37,13 +37,11 @@ describe('files', () => { const hash = 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o' ipfs.files.cat(hash, (err, res) => { expect(err).to.not.exist - res.on('data', (data) => { - data.stream.pipe(bl((err, bldata) => { - expect(err).to.not.exist - expect(bldata.toString()).to.equal('hello world\n') - done() - })) - }) + res.pipe(bl((err, bldata) => { + expect(err).to.not.exist + expect(bldata.toString()).to.equal('hello world\n') + done() + })) }) }) From ef6fcaa41a9a0b710aee4686795f77dd538881f5 Mon Sep 17 00:00:00 2001 From: nginnever Date: Sat, 21 May 2016 16:34:20 -0700 Subject: [PATCH 2/4] s/on/once on stream --- src/core/ipfs/files.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/ipfs/files.js b/src/core/ipfs/files.js index aa09337154..2d5a151661 100644 --- a/src/core/ipfs/files.js +++ b/src/core/ipfs/files.js @@ -45,7 +45,7 @@ module.exports = function files (self) { callback('This dag node is a directory', null) } else { const exportStream = Exporter(hash, self._dagS) - exportStream.on('data', (object) => { + exportStream.once('data', (object) => { callback(null, object.stream) }) } From 9bebc1a9ad88f1421449c9ddaab39eb7cfdd3be7 Mon Sep 17 00:00:00 2001 From: nginnever Date: Sat, 21 May 2016 20:42:08 -0700 Subject: [PATCH 3/4] promisify files core, begin convert core tests to interface core --- src/core/ipfs/files.js | 31 ++++++++-------- test/core-tests/test-files.js | 66 +++++++---------------------------- 2 files changed, 29 insertions(+), 68 deletions(-) diff --git a/src/core/ipfs/files.js b/src/core/ipfs/files.js index 2d5a151661..6864d41cfd 100644 --- a/src/core/ipfs/files.js +++ b/src/core/ipfs/files.js @@ -3,16 +3,17 @@ const Importer = require('ipfs-unixfs-engine').importer const Exporter = require('ipfs-unixfs-engine').exporter const UnixFS = require('ipfs-unixfs') +const promisify = require('promisify-es6') module.exports = function files (self) { return { - add: (arr, callback) => { + add: promisify((arr, cb) => { if (typeof arr === 'function') { - callback = arr + cb = arr arr = undefined } - if (callback === undefined) { - callback = function noop () {} + if (cb === undefined) { + cb = function noop () {} } if (arr === undefined) { return new Importer(self._dagS) @@ -26,7 +27,7 @@ module.exports = function files (self) { }) i.once('end', () => { - callback(null, res) + cb(null, res) }) arr.forEach((tuple) => { @@ -34,26 +35,28 @@ module.exports = function files (self) { }) i.end() - }, - cat: (hash, callback) => { + }), + + cat: promisify((hash, cb) => { self._dagS.get(hash, (err, fetchedNode) => { if (err) { - return callback(err, null) + return cb(err, null) } const data = UnixFS.unmarshal(fetchedNode.data) if (data.type === 'directory') { - callback('This dag node is a directory', null) + cb('This dag node is a directory', null) } else { const exportStream = Exporter(hash, self._dagS) exportStream.once('data', (object) => { - callback(null, object.stream) + cb(null, object.stream) }) } }) - }, - get: (hash, callback) => { + }), + + get: promisify((hash, cb) => { var exportFile = Exporter(hash, self._dagS) - callback(null, exportFile) - } + cb(null, exportFile) + }) } } diff --git a/test/core-tests/test-files.js b/test/core-tests/test-files.js index 1c16a3ab7e..02e84b888e 100644 --- a/test/core-tests/test-files.js +++ b/test/core-tests/test-files.js @@ -1,62 +1,20 @@ /* eslint-env mocha */ 'use strict' -const bl = require('bl') -const expect = require('chai').expect -const Readable = require('stream').Readable -const bs58 = require('bs58') +const test = require('interface-ipfs-core') const IPFS = require('../../src/core') -describe('files', () => { - let ipfs - - before((done) => { - ipfs = new IPFS(require('./repo-path')) - ipfs.load(done) - }) - - it('add', (done) => { - const buffered = new Buffer('some data') - const rs = new Readable() - rs.push(buffered) - rs.push(null) - const arr = [] - const filePair = {path: 'data.txt', stream: rs} - arr.push(filePair) - ipfs.files.add(arr, (err, res) => { - expect(err).to.not.exist - expect(res[0].path).to.equal('data.txt') - expect(res[0].size).to.equal(17) - expect(bs58.encode(res[0].multihash).toString()).to.equal('QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS') - done() +const common = { + setup: function (cb) { + const ipfs = new IPFS(require('./repo-path')) + ipfs.load(() => { + cb(null, ipfs) }) - }) + }, + teardown: function (cb) { + cb() + } +} - it('cat', (done) => { - const hash = 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o' - ipfs.files.cat(hash, (err, res) => { - expect(err).to.not.exist - res.pipe(bl((err, bldata) => { - expect(err).to.not.exist - expect(bldata.toString()).to.equal('hello world\n') - done() - })) - }) - }) - - it('get', (done) => { - // TODO create non-trival get test - const hash = 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o' - ipfs.files.get(hash, (err, res) => { - expect(err).to.not.exist - res.on('data', (data) => { - data.stream.pipe(bl((err, bldata) => { - expect(err).to.not.exist - expect(bldata.toString()).to.equal('hello world\n') - done() - })) - }) - }) - }) -}) +test.files(common) From a3546104a958dd4d2a80cb486f470d3d03243907 Mon Sep 17 00:00:00 2001 From: nginnever Date: Sat, 21 May 2016 21:22:48 -0700 Subject: [PATCH 4/4] update add cli for promise --- src/cli/commands/files/add.js | 64 +++++++++++++++++++---------------- src/cli/commands/files/cat.js | 1 + src/core/ipfs/files.js | 2 +- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/cli/commands/files/add.js b/src/cli/commands/files/add.js index f69a8b700f..22824d7cbc 100644 --- a/src/cli/commands/files/add.js +++ b/src/cli/commands/files/add.js @@ -59,37 +59,41 @@ module.exports = Command.extend({ if (err) { throw err } - const i = ipfs.files.add() - var filePair - i.on('data', (file) => { - console.log('added', bs58.encode(file.multihash).toString(), file.path) - }) - i.once('end', () => { - return - }) - if (res.length !== 0) { - const index = inPath.lastIndexOf('/') - parallelLimit(res.map((element) => (callback) => { - if (!fs.statSync(element).isDirectory()) { - i.write({ - path: element.substring(index + 1, element.length), - stream: fs.createReadStream(element) - }) - } - callback() - }), 10, (err) => { - if (err) { - throw err - } - i.end() + ipfs.files.add((err, i) => { + if (err) { + throw err + } + var filePair + i.on('data', (file) => { + console.log('added', bs58.encode(file.multihash).toString(), file.path) }) - } else { - rs = fs.createReadStream(inPath) - inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length) - filePair = {path: inPath, stream: rs} - i.write(filePair) - i.end() - } + i.once('end', () => { + return + }) + if (res.length !== 0) { + const index = inPath.lastIndexOf('/') + parallelLimit(res.map((element) => (callback) => { + if (!fs.statSync(element).isDirectory()) { + i.write({ + path: element.substring(index + 1, element.length), + stream: fs.createReadStream(element) + }) + } + callback() + }), 10, (err) => { + if (err) { + throw err + } + i.end() + }) + } else { + rs = fs.createReadStream(inPath) + inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length) + filePair = {path: inPath, stream: rs} + i.write(filePair) + i.end() + } + }) }) }) } diff --git a/src/cli/commands/files/cat.js b/src/cli/commands/files/cat.js index ff87a82a69..5aea902820 100644 --- a/src/cli/commands/files/cat.js +++ b/src/cli/commands/files/cat.js @@ -35,6 +35,7 @@ module.exports = Command.extend({ if (err) { throw (err) } + console.log(file) file.pipe(process.stdout) }) }) diff --git a/src/core/ipfs/files.js b/src/core/ipfs/files.js index 6864d41cfd..d92499b8e7 100644 --- a/src/core/ipfs/files.js +++ b/src/core/ipfs/files.js @@ -16,7 +16,7 @@ module.exports = function files (self) { cb = function noop () {} } if (arr === undefined) { - return new Importer(self._dagS) + cb(null, new Importer(self._dagS)) } const i = new Importer(self._dagS)