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

Commit

Permalink
Add files.get command and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
hackergrrl committed Jun 23, 2016
1 parent b6b9738 commit a025372
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 11 deletions.
25 changes: 22 additions & 3 deletions src/api/get.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
'use strict'

const tarStreamToObjects = require('../tar-stream-to-objects')
const cleanMultihash = require('../clean-multihash')
const promisify = require('promisify-es6')

module.exports = (send) => {
return function get (path, opts, cb) {
return promisify(function get (path, opts, cb) {
if (typeof opts === 'function' && !cb) {
cb = opts
opts = {}
}
return send('get', path, opts, null, cb)
}

// opts is the real callback -- 'cb' is being injected by promisify
if (typeof opts === 'function' && typeof cb === 'function') {
cb = opts
opts = {}
}

try {
path = cleanMultihash(path)
} catch (err) {
return cb(err)
}

var sendWithTransform = send.withTransform(tarStreamToObjects)

return sendWithTransform('get', path, opts, null, cb)
})
}
2 changes: 2 additions & 0 deletions src/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ function requireCommands () {
const files = require('./api/files')(send)
files.add = require('./api/add')(send)
files.createAddStream = require('./api/add-stream.js')(send)
files.get = require('./api/get')(send)
// aliases
cmds.add = files.add
cmds.createAddStream = files.createAddStream
cmds.get = files.get
return files
}

Expand Down
32 changes: 32 additions & 0 deletions src/tar-stream-to-objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

const tar = require('tar-stream')
const Readable = require('readable-stream')
const through = require('through2')

// transform tar stream into readable stream of
// { path: 'string', content: Readable }
module.exports = function (err, res, send, done) {

if (err) return done(err)

var ex = tar.extract()
res.pipe(ex)

var objStream = new Readable({ objectMode: true })
objStream._read = function noop () {}

ex.on('entry', function (header, stream, next) {
objStream.push({
path: header.name,
content: stream
})
next()
})
ex.on('finish', () => {
objStream.push(null)
})

done(null, objStream)
}

40 changes: 32 additions & 8 deletions test/api/get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ const expect = require('chai').expect
const isNode = require('detect-node')
const fs = require('fs')
const bl = require('bl')
const concat = require('concat-stream')
const through = require('through2')

const path = require('path')
const streamEqual = require('stream-equal')

const extract = require('tar-stream').extract

let testfile
let testfileBig

Expand All @@ -24,10 +28,20 @@ describe('.get', () => {
it('get with no compression args', (done) => {
apiClients.a
.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => {
expect(err).to.not.exist
res.pipe(bl((err, bldata) => {
expect(err).to.not.exist
expect(bldata.toString()).to.contain(testfile.toString())

// accumulate the files and their content
var files = []
res.pipe(through.obj((file, enc, next) => {
file.content.pipe(concat((content) => {
files.push({
path: file.path,
content: content
})
next()
}))
}, () => {
expect(files).to.be.length(1)
expect(files[0].content.toString()).to.contain(testfile.toString())
done()
}))
})
Expand All @@ -36,10 +50,20 @@ describe('.get', () => {
it('get with archive true', (done) => {
apiClients.a
.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())

// accumulate the files and their content
var files = []
res.pipe(through.obj((file, enc, next) => {
file.content.pipe(concat((content) => {
files.push({
path: file.path,
content: content
})
next()
}))
}, () => {
expect(files).to.be.length(1)
expect(files[0].content.toString()).to.contain(testfile.toString())
done()
}))
})
Expand Down

0 comments on commit a025372

Please sign in to comment.