Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

files.get #271

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions src/api/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

module.exports = (send) => {
return function get (path, archive, compress, compressionLevel, cb) {
if (archive === true && typeof compress === 'function') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use a switch statement instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or since archive, compress, compressionLevel are all optional, why not pass them on the opts object?

I think that you can just return the argCommand and it will take care of that :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, please make archive, compress and compressionLevel into an options object.

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, aren't the opts a object and not an array?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep

}
}
1 change: 1 addition & 0 deletions src/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
119 changes: 119 additions & 0 deletions test/api/get.spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use bl for these tests

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()
})
})
})
})
})