From 3f1dc9ee894f90e606f076243a2fcbaa89445b5e Mon Sep 17 00:00:00 2001 From: nginnever Date: Thu, 12 May 2016 18:21:19 -0700 Subject: [PATCH 1/2] get command --- src/api/get.js | 24 +++++++++ src/load-commands.js | 1 + test/api/get.spec.js | 119 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 src/api/get.js create mode 100644 test/api/get.spec.js diff --git a/src/api/get.js b/src/api/get.js new file mode 100644 index 000000000..f2d122652 --- /dev/null +++ b/src/api/get.js @@ -0,0 +1,24 @@ +'use strict' + +module.exports = (send) => { + return function get (path, archive, compress, compressionLevel, cb) { + if (archive === true && typeof compress === 'function') { + cb = compress + compressionLevel = null + compress = null + } + if (archive === true && typeof compress === 'number') { + archive = null + cb = compressionLevel + compressionLevel = compress + compress = true + } + if (typeof archive === 'function') { + cb = archive + archive = null + compressionLevel = null + compress = null + } + return send('get', path, [archive, compress, compressionLevel], null, cb) + } +} diff --git a/src/load-commands.js b/src/load-commands.js index fc5ee614b..7c9e67935 100644 --- a/src/load-commands.js +++ b/src/load-commands.js @@ -12,6 +12,7 @@ function requireCommands () { diag: require('./api/diag'), id: require('./api/id'), files: require('./api/files'), + get: require('./api/get'), log: require('./api/log'), ls: require('./api/ls'), mount: require('./api/mount'), diff --git a/test/api/get.spec.js b/test/api/get.spec.js new file mode 100644 index 000000000..9674f6476 --- /dev/null +++ b/test/api/get.spec.js @@ -0,0 +1,119 @@ +/* eslint-env mocha */ +/* globals apiClients */ +'use strict' + +const expect = require('chai').expect +const isNode = require('detect-node') +const fs = require('fs') + +const path = require('path') +const streamEqual = require('stream-equal') + +let testfile +let testfileBig + +if (isNode) { + testfile = fs.readFileSync(path.join(__dirname, '/../testfile.txt')) + testfileBig = fs.createReadStream(path.join(__dirname, '/../15mb.random'), { bufferSize: 128 }) +} else { + testfile = require('raw!../testfile.txt') +} + +describe('.get', () => { + it('get with no compression args', (done) => { + apiClients.a + .get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => { + expect(err).to.not.exist + + let buf = '' + res + .on('error', (err) => { + expect(err).to.not.exist + }) + .on('data', (data) => { + buf += data + }) + .on('end', () => { + expect(buf).to.contain(testfile.toString()) + done() + }) + }) + }) + + it('get with archive true', (done) => { + apiClients.a + .get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', true, (err, res) => { + expect(err).to.not.exist + + let buf = '' + res + .on('error', (err) => { + expect(err).to.not.exist + }) + .on('data', (data) => { + buf += data + }) + .on('end', () => { + expect(buf).to.contain(testfile.toString()) + done() + }) + }) + }) + + it('get with compression level', (done) => { + apiClients.a + .get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', true, 1, (err, res) => { + expect(err).to.not.exist + + let buf = '' + res + .on('error', (err) => { + expect(err).to.not.exist + }) + .on('data', (data) => { + buf += data + }) + .on('end', () => { + expect(buf).to.contain(testfile.toString()) + done() + }) + }) + }) + + it.skip('get BIG file', (done) => { + if (!isNode) { + return done() + } + + apiClients.a.get('Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq', (err, res) => { + expect(err).to.not.exist + + // Do not blow out the memory of nodejs :) + streamEqual(res, testfileBig, (err, equal) => { + expect(err).to.not.exist + expect(equal).to.be.true + done() + }) + }) + }) + + describe('promise', () => { + it.skip('get', (done) => { + return apiClients.a.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') + .then((res) => { + let buf = '' + res + .on('error', (err) => { + throw err + }) + .on('data', (data) => { + buf += data + }) + .on('end', () => { + expect(buf).to.contain(testfile.toString()) + done() + }) + }) + }) + }) +}) From 95daa372dfe1b8f2e076c02471c3e3a7922509cd Mon Sep 17 00:00:00 2001 From: nginnever Date: Fri, 13 May 2016 09:23:01 -0700 Subject: [PATCH 2/2] cr --- src/api/get.js | 23 ++++------------ test/api/get.spec.js | 62 ++++++++++++++++---------------------------- 2 files changed, 27 insertions(+), 58 deletions(-) diff --git a/src/api/get.js b/src/api/get.js index f2d122652..aabaabba0 100644 --- a/src/api/get.js +++ b/src/api/get.js @@ -1,24 +1,11 @@ 'use strict' module.exports = (send) => { - return function get (path, archive, compress, compressionLevel, cb) { - if (archive === true && typeof compress === 'function') { - cb = compress - compressionLevel = null - compress = null + return function get (path, opts, cb) { + if (typeof opts === 'function' && !cb) { + cb = opts + opts = {} } - if (archive === true && typeof compress === 'number') { - archive = null - cb = compressionLevel - compressionLevel = compress - compress = true - } - if (typeof archive === 'function') { - cb = archive - archive = null - compressionLevel = null - compress = null - } - return send('get', path, [archive, compress, compressionLevel], null, cb) + return send('get', path, opts, null, cb) } } diff --git a/test/api/get.spec.js b/test/api/get.spec.js index 9674f6476..82278616a 100644 --- a/test/api/get.spec.js +++ b/test/api/get.spec.js @@ -5,6 +5,7 @@ const expect = require('chai').expect const isNode = require('detect-node') const fs = require('fs') +const bl = require('bl') const path = require('path') const streamEqual = require('stream-equal') @@ -24,59 +25,40 @@ describe('.get', () => { apiClients.a .get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => { expect(err).to.not.exist - - let buf = '' - res - .on('error', (err) => { - expect(err).to.not.exist - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(buf).to.contain(testfile.toString()) - done() - }) + res.pipe(bl((err, bldata) => { + expect(err).to.not.exist + expect(bldata.toString()).to.contain(testfile.toString()) + done() + })) }) }) it('get with archive true', (done) => { apiClients.a - .get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', true, (err, res) => { + .get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {archive: true}, (err, res) => { expect(err).to.not.exist + res.pipe(bl((err, bldata) => { + expect(err).to.not.exist + expect(bldata.toString()).to.contain(testfile.toString()) + done() + })) + }) + }) - let buf = '' - res - .on('error', (err) => { - expect(err).to.not.exist - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(buf).to.contain(testfile.toString()) - done() - }) + it('get err with out of range compression level', (done) => { + apiClients.a + .get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {compress: true, 'compression-level': 10}, (err, res) => { + expect(err).to.exist + expect(err.toString()).to.equal('Error: Compression level must be between 1 and 9') + done() }) }) it('get with compression level', (done) => { apiClients.a - .get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', true, 1, (err, res) => { + .get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {compress: true, 'compression-level': 1}, (err, res) => { expect(err).to.not.exist - - let buf = '' - res - .on('error', (err) => { - expect(err).to.not.exist - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(buf).to.contain(testfile.toString()) - done() - }) + done() }) })