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

Commit e96c859

Browse files
committed
fix(files.get): add dirs on the browser
1 parent fdf6e01 commit e96c859

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"gulp": "^3.9.1",
3636
"interface-ipfs-core": "^0.5.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

+2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ module.exports = function (err, res, send, done) {
88
if (err) {
99
return done(err)
1010
}
11+
1112
async.map(res, function map (entry, next) {
13+
console.log('RECEIVED', entry.Name)
1214
getDagNode(send, entry.Hash, function (err, node) {
1315
if (err) {
1416
return next(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/get-files-stream.js

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ function getFilesStream (files, opts) {
122122
content: file
123123
}
124124
}).forEach((file) => {
125+
console.log('SENDING (MULTIPART)', file.path, file.dir)
125126
mp.addPart({
126127
headers: headers(file),
127128
body: file.content

src/request-api.js

+31-13
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,29 @@ 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+
console.log('Parsed total of:', parsed.length)
24+
console.log('Total byte size was:', c.length)
25+
cb(null, parsed)
26+
})
1827
}
1928

20-
function onRes (buffer, cb) {
29+
function onRes (buffer, cb, uri) {
2130
return (err, res) => {
2231
if (err) {
2332
return cb(err)
@@ -27,6 +36,10 @@ function onRes (buffer, cb) {
2736
const chunkedObjects = Boolean(res.headers['x-chunked-output'])
2837
const isJson = res.headers['content-type'] && res.headers['content-type'].indexOf('application/json') === 0
2938

39+
if (uri.indexOf('add') !== -1) {
40+
console.log('HEADERS (stream, chunked, json)', stream, chunkedObjects, isJson)
41+
}
42+
3043
if (res.statusCode >= 400 || !res.statusCode) {
3144
const error = new Error(`Server responded with ${res.statusCode}`)
3245

@@ -42,10 +55,14 @@ function onRes (buffer, cb) {
4255
})
4356
}
4457

45-
if (stream && !buffer) return cb(null, res)
58+
if (stream && !buffer) {
59+
return cb(null, res)
60+
}
4661

4762
if (chunkedObjects) {
48-
if (isJson) return parseChunkedJson(res, cb)
63+
if (isJson) {
64+
return parseChunkedJson(res, cb)
65+
}
4966

5067
return Wreck.read(res, null, cb)
5168
}
@@ -56,6 +73,11 @@ function onRes (buffer, cb) {
5673

5774
function requestAPI (config, path, args, qs, files, buffer, cb) {
5875
qs = qs || {}
76+
77+
if (Array.isArray(files)) {
78+
qs.recursive = true
79+
}
80+
5981
if (Array.isArray(path)) path = path.join('/')
6082
if (args && !Array.isArray(args)) args = [args]
6183
if (args) qs.arg = args
@@ -67,10 +89,6 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
6789
delete qs.r
6890
}
6991

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

7694
let stream
@@ -104,7 +122,7 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
104122
opts.payload = stream
105123
}
106124

107-
return Wreck.request(opts.method, opts.uri, opts, onRes(buffer, cb))
125+
return Wreck.request(opts.method, opts.uri, opts, onRes(buffer, cb, opts.uri))
108126
}
109127

110128
// -- Interface
@@ -128,9 +146,9 @@ exports = module.exports = function getRequestAPI (config) {
128146
return requestAPI(config, path, args, qs, files, buffer, cb)
129147
}
130148

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.
149+
// Wraps the 'send' function such that an asynchronous
150+
// transform may be applied to its result before
151+
// passing it on to either its callback or promise.
134152
send.withTransform = function (transform) {
135153
return function (path, args, qs, files, buffer, cb) {
136154
if (typeof buffer === 'function') {

0 commit comments

Comments
 (0)