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

Commit bf17442

Browse files
hackergrrldaviddias
authored andcommitted
Add files.get command and tests.
1 parent 4c96439 commit bf17442

File tree

4 files changed

+92
-11
lines changed

4 files changed

+92
-11
lines changed

src/api/get.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
'use strict'
22

3+
const tarStreamToObjects = require('../tar-stream-to-objects')
4+
const cleanMultihash = require('../clean-multihash')
5+
const promisify = require('promisify-es6')
6+
37
module.exports = (send) => {
4-
return function get (path, opts, cb) {
8+
return promisify(function get (path, opts, cb) {
59
if (typeof opts === 'function' && !cb) {
610
cb = opts
711
opts = {}
812
}
9-
return send('get', path, opts, null, cb)
10-
}
13+
14+
// opts is the real callback -- 'cb' is being injected by promisify
15+
if (typeof opts === 'function' && typeof cb === 'function') {
16+
cb = opts
17+
opts = {}
18+
}
19+
20+
try {
21+
path = cleanMultihash(path)
22+
} catch (err) {
23+
return cb(err)
24+
}
25+
26+
var sendWithTransform = send.withTransform(tarStreamToObjects)
27+
28+
return sendWithTransform('get', path, opts, null, cb)
29+
})
1130
}

src/load-commands.js

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ function requireCommands () {
3434
const files = require('./api/files')(send)
3535
files.add = require('./api/add')(send)
3636
files.createAddStream = require('./api/add-stream.js')(send)
37+
files.get = require('./api/get')(send)
38+
39+
// aliases
40+
cmds.add = files.add
41+
cmds.createAddStream = files.createAddStream
42+
cmds.get = files.get
3743

3844
return files
3945
}

src/tar-stream-to-objects.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
const tar = require('tar-stream')
4+
const Readable = require('readable-stream')
5+
const through = require('through2')
6+
7+
// transform tar stream into readable stream of
8+
// { path: 'string', content: Readable }
9+
module.exports = function (err, res, send, done) {
10+
11+
if (err) return done(err)
12+
13+
var ex = tar.extract()
14+
res.pipe(ex)
15+
16+
var objStream = new Readable({ objectMode: true })
17+
objStream._read = function noop () {}
18+
19+
ex.on('entry', function (header, stream, next) {
20+
objStream.push({
21+
path: header.name,
22+
content: stream
23+
})
24+
next()
25+
})
26+
ex.on('finish', () => {
27+
objStream.push(null)
28+
})
29+
30+
done(null, objStream)
31+
}
32+

test/api/get.spec.js

+32-8
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ const expect = require('chai').expect
66
const isNode = require('detect-node')
77
const fs = require('fs')
88
const bl = require('bl')
9+
const concat = require('concat-stream')
10+
const through = require('through2')
911

1012
const path = require('path')
1113
const streamEqual = require('stream-equal')
1214

15+
const extract = require('tar-stream').extract
16+
1317
let testfile
1418
let testfileBig
1519

@@ -24,10 +28,20 @@ describe('.get', () => {
2428
it('get with no compression args', (done) => {
2529
apiClients.a
2630
.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => {
27-
expect(err).to.not.exist
28-
res.pipe(bl((err, bldata) => {
29-
expect(err).to.not.exist
30-
expect(bldata.toString()).to.contain(testfile.toString())
31+
32+
// accumulate the files and their content
33+
var files = []
34+
res.pipe(through.obj((file, enc, next) => {
35+
file.content.pipe(concat((content) => {
36+
files.push({
37+
path: file.path,
38+
content: content
39+
})
40+
next()
41+
}))
42+
}, () => {
43+
expect(files).to.be.length(1)
44+
expect(files[0].content.toString()).to.contain(testfile.toString())
3145
done()
3246
}))
3347
})
@@ -36,10 +50,20 @@ describe('.get', () => {
3650
it('get with archive true', (done) => {
3751
apiClients.a
3852
.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {archive: true}, (err, res) => {
39-
expect(err).to.not.exist
40-
res.pipe(bl((err, bldata) => {
41-
expect(err).to.not.exist
42-
expect(bldata.toString()).to.contain(testfile.toString())
53+
54+
// accumulate the files and their content
55+
var files = []
56+
res.pipe(through.obj((file, enc, next) => {
57+
file.content.pipe(concat((content) => {
58+
files.push({
59+
path: file.path,
60+
content: content
61+
})
62+
next()
63+
}))
64+
}, () => {
65+
expect(files).to.be.length(1)
66+
expect(files[0].content.toString()).to.contain(testfile.toString())
4367
done()
4468
}))
4569
})

0 commit comments

Comments
 (0)