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

Commit 248cd13

Browse files
committed
fix(files.get):
- Move path.join call out of readStream to not mess with the AST parser - Fix big file test
1 parent 833f249 commit 248cd13

File tree

5 files changed

+64
-36
lines changed

5 files changed

+64
-36
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
"aegir": "^6.0.0",
3434
"chai": "^3.5.0",
3535
"gulp": "^3.9.1",
36-
"interface-ipfs-core": "^0.5.0",
36+
"interface-ipfs-core": "^0.6.0",
3737
"ipfsd-ctl": "^0.14.0",
38+
"passthrough-counter": "^1.0.0",
3839
"pre-commit": "^1.1.3",
3940
"stream-equal": "^0.1.8",
4041
"stream-http": "^2.3.1",

src/add-to-dagnode-transform.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = function (err, res, send, done) {
88
if (err) {
99
return done(err)
1010
}
11+
1112
async.map(res, function map (entry, next) {
1213
getDagNode(send, entry.Hash, function (err, node) {
1314
if (err) {

src/api/add-files.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const isNode = require('detect-node')
34
const addToDagNodesTransform = require('../add-to-dagnode-transform')
45

56
module.exports = (send) => {
@@ -9,6 +10,10 @@ module.exports = (send) => {
910
opts = {}
1011
}
1112

13+
if (!isNode) {
14+
return cb(new Error('Recursive uploads are not supported in the browser'))
15+
}
16+
1217
if (typeof (path) !== 'string') {
1318
return cb(new Error('"path" must be a string'))
1419
}

src/request-api.js

+25-13
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,27 @@ const Wreck = require('wreck')
44
const Qs = require('qs')
55
const ndjson = require('ndjson')
66
const getFilesStream = require('./get-files-stream')
7+
const Counter = require('passthrough-counter')
78

89
const isNode = require('detect-node')
910

1011
// -- Internal
1112

1213
function parseChunkedJson (res, cb) {
1314
const parsed = []
15+
const c = new Counter()
1416
res
17+
.pipe(c)
1518
.pipe(ndjson.parse())
16-
.on('data', parsed.push.bind(parsed))
17-
.on('end', () => cb(null, parsed))
19+
.on('data', (obj) => {
20+
parsed.push(obj)
21+
})
22+
.on('end', () => {
23+
cb(null, parsed)
24+
})
1825
}
1926

20-
function onRes (buffer, cb) {
27+
function onRes (buffer, cb, uri) {
2128
return (err, res) => {
2229
if (err) {
2330
return cb(err)
@@ -42,10 +49,14 @@ function onRes (buffer, cb) {
4249
})
4350
}
4451

45-
if (stream && !buffer) return cb(null, res)
52+
if (stream && !buffer) {
53+
return cb(null, res)
54+
}
4655

4756
if (chunkedObjects) {
48-
if (isJson) return parseChunkedJson(res, cb)
57+
if (isJson) {
58+
return parseChunkedJson(res, cb)
59+
}
4960

5061
return Wreck.read(res, null, cb)
5162
}
@@ -56,6 +67,11 @@ function onRes (buffer, cb) {
5667

5768
function requestAPI (config, path, args, qs, files, buffer, cb) {
5869
qs = qs || {}
70+
71+
if (Array.isArray(files)) {
72+
qs.recursive = true
73+
}
74+
5975
if (Array.isArray(path)) path = path.join('/')
6076
if (args && !Array.isArray(args)) args = [args]
6177
if (args) qs.arg = args
@@ -67,10 +83,6 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
6783
delete qs.r
6884
}
6985

70-
if (!isNode && qs.recursive && path === 'add') {
71-
return cb(new Error('Recursive uploads are not supported in the browser'))
72-
}
73-
7486
qs['stream-channels'] = true
7587

7688
let stream
@@ -104,7 +116,7 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
104116
opts.payload = stream
105117
}
106118

107-
return Wreck.request(opts.method, opts.uri, opts, onRes(buffer, cb))
119+
return Wreck.request(opts.method, opts.uri, opts, onRes(buffer, cb, opts.uri))
108120
}
109121

110122
// -- Interface
@@ -128,9 +140,9 @@ exports = module.exports = function getRequestAPI (config) {
128140
return requestAPI(config, path, args, qs, files, buffer, cb)
129141
}
130142

131-
// Wraps the 'send' function such that an asynchronous transform may be
132-
// applied to its result before passing it on to either its callback or
133-
// promise.
143+
// Wraps the 'send' function such that an asynchronous
144+
// transform may be applied to its result before
145+
// passing it on to either its callback or promise.
134146
send.withTransform = function (transform) {
135147
return function (path, args, qs, files, buffer, cb) {
136148
if (typeof buffer === 'function') {

test/api/get.spec.js

+31-22
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ const path = require('path')
1717
// const extract = require('tar-stream').extract
1818

1919
const testfile = fs.readFileSync(path.join(__dirname, '/../testfile.txt'))
20+
2021
let testfileBig
2122

2223
if (isNode) {
23-
testfileBig = fs.createReadStream(path.join(__dirname, '/../15mb.random'), { bufferSize: 128 })
24+
const tfbPath = path.join(__dirname, '/../15mb.random')
25+
testfileBig = fs.createReadStream(tfbPath, { bufferSize: 128 })
2426
}
2527

26-
describe.skip('.get', () => {
28+
describe('.get', () => {
2729
it('get with no compression args', (done) => {
2830
apiClients.a
2931
.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => {
@@ -92,35 +94,42 @@ describe.skip('.get', () => {
9294
return done()
9395
}
9496

95-
apiClients.a.get('Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq', (err, res) => {
97+
apiClients.a.get('Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq', (err, files) => {
9698
expect(err).to.not.exist
9799

98-
// Do not blow out the memory of nodejs :)
99-
streamEqual(res, testfileBig, (err, equal) => {
100-
expect(err).to.not.exist
101-
expect(equal).to.be.true
102-
done()
100+
files.on('data', (file) => {
101+
// Do not blow out the memory of nodejs :)
102+
streamEqual(file.content, testfileBig, (err, equal) => {
103+
expect(err).to.not.exist
104+
expect(equal).to.be.true
105+
done()
106+
})
103107
})
104108
})
105109
})
106110

107-
describe.skip('promise', () => {
111+
describe('promise', () => {
108112
it('get', (done) => {
109113
apiClients.a.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
110-
.then((res) => {
111-
let buf = ''
112-
res
113-
.on('error', (err) => {
114-
throw err
115-
})
116-
.on('data', (data) => {
117-
buf += data
118-
})
119-
.on('end', () => {
120-
expect(buf).to.contain(testfile.toString())
121-
done()
122-
})
114+
.then((files) => {
115+
files.on('data', (file) => {
116+
let buf = ''
117+
file.content
118+
.on('error', (err) => {
119+
throw err
120+
})
121+
.on('data', (data) => {
122+
buf += data.toString()
123+
})
124+
.on('end', () => {
125+
expect(buf).to.contain(testfile.toString())
126+
done()
127+
})
128+
})
123129
})
130+
.catch((err) => {
131+
expect(err).to.not.exist
132+
})
124133
})
125134
})
126135
})

0 commit comments

Comments
 (0)