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

Commit ada8206

Browse files
nginneverhackergrrl
authored andcommitted
get command
1 parent 59e6656 commit ada8206

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

src/api/get.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict'
2+
3+
module.exports = (send) => {
4+
return function get (path, archive, compress, compressionLevel, cb) {
5+
if (archive === true && typeof compress === 'function') {
6+
cb = compress
7+
compressionLevel = null
8+
compress = null
9+
}
10+
if (archive === true && typeof compress === 'number') {
11+
archive = null
12+
cb = compressionLevel
13+
compressionLevel = compress
14+
compress = true
15+
}
16+
if (typeof archive === 'function') {
17+
cb = archive
18+
archive = null
19+
compressionLevel = null
20+
compress = null
21+
}
22+
return send('get', path, [archive, compress, compressionLevel], null, cb)
23+
}
24+
}

src/load-commands.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function requireCommands () {
1111
dht: require('./api/dht'),
1212
diag: require('./api/diag'),
1313
id: require('./api/id'),
14+
get: require('./api/get'),
1415
log: require('./api/log'),
1516
ls: require('./api/ls'),
1617
mount: require('./api/mount'),

test/api/get.spec.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/* eslint-env mocha */
2+
/* globals apiClients */
3+
'use strict'
4+
5+
const expect = require('chai').expect
6+
const isNode = require('detect-node')
7+
const fs = require('fs')
8+
9+
const path = require('path')
10+
const streamEqual = require('stream-equal')
11+
12+
let testfile
13+
let testfileBig
14+
15+
if (isNode) {
16+
testfile = fs.readFileSync(path.join(__dirname, '/../testfile.txt'))
17+
testfileBig = fs.createReadStream(path.join(__dirname, '/../15mb.random'), { bufferSize: 128 })
18+
} else {
19+
testfile = require('raw!../testfile.txt')
20+
}
21+
22+
describe('.get', () => {
23+
it('get with no compression args', (done) => {
24+
apiClients.a
25+
.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => {
26+
expect(err).to.not.exist
27+
28+
let buf = ''
29+
res
30+
.on('error', (err) => {
31+
expect(err).to.not.exist
32+
})
33+
.on('data', (data) => {
34+
buf += data
35+
})
36+
.on('end', () => {
37+
expect(buf).to.contain(testfile.toString())
38+
done()
39+
})
40+
})
41+
})
42+
43+
it('get with archive true', (done) => {
44+
apiClients.a
45+
.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', true, (err, res) => {
46+
expect(err).to.not.exist
47+
48+
let buf = ''
49+
res
50+
.on('error', (err) => {
51+
expect(err).to.not.exist
52+
})
53+
.on('data', (data) => {
54+
buf += data
55+
})
56+
.on('end', () => {
57+
expect(buf).to.contain(testfile.toString())
58+
done()
59+
})
60+
})
61+
})
62+
63+
it('get with compression level', (done) => {
64+
apiClients.a
65+
.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', true, 1, (err, res) => {
66+
expect(err).to.not.exist
67+
68+
let buf = ''
69+
res
70+
.on('error', (err) => {
71+
expect(err).to.not.exist
72+
})
73+
.on('data', (data) => {
74+
buf += data
75+
})
76+
.on('end', () => {
77+
expect(buf).to.contain(testfile.toString())
78+
done()
79+
})
80+
})
81+
})
82+
83+
it.skip('get BIG file', (done) => {
84+
if (!isNode) {
85+
return done()
86+
}
87+
88+
apiClients.a.get('Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq', (err, res) => {
89+
expect(err).to.not.exist
90+
91+
// Do not blow out the memory of nodejs :)
92+
streamEqual(res, testfileBig, (err, equal) => {
93+
expect(err).to.not.exist
94+
expect(equal).to.be.true
95+
done()
96+
})
97+
})
98+
})
99+
100+
describe('promise', () => {
101+
it.skip('get', (done) => {
102+
return apiClients.a.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
103+
.then((res) => {
104+
let buf = ''
105+
res
106+
.on('error', (err) => {
107+
throw err
108+
})
109+
.on('data', (data) => {
110+
buf += data
111+
})
112+
.on('end', () => {
113+
expect(buf).to.contain(testfile.toString())
114+
done()
115+
})
116+
})
117+
})
118+
})
119+
})

0 commit comments

Comments
 (0)