@@ -4,20 +4,27 @@ const Wreck = require('wreck')
4
4
const Qs = require ( 'qs' )
5
5
const ndjson = require ( 'ndjson' )
6
6
const getFilesStream = require ( './get-files-stream' )
7
+ const Counter = require ( 'passthrough-counter' )
7
8
8
9
const isNode = require ( 'detect-node' )
9
10
10
11
// -- Internal
11
12
12
13
function parseChunkedJson ( res , cb ) {
13
14
const parsed = [ ]
15
+ const c = new Counter ( )
14
16
res
17
+ . pipe ( c )
15
18
. 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
+ } )
18
25
}
19
26
20
- function onRes ( buffer , cb ) {
27
+ function onRes ( buffer , cb , uri ) {
21
28
return ( err , res ) => {
22
29
if ( err ) {
23
30
return cb ( err )
@@ -42,10 +49,14 @@ function onRes (buffer, cb) {
42
49
} )
43
50
}
44
51
45
- if ( stream && ! buffer ) return cb ( null , res )
52
+ if ( stream && ! buffer ) {
53
+ return cb ( null , res )
54
+ }
46
55
47
56
if ( chunkedObjects ) {
48
- if ( isJson ) return parseChunkedJson ( res , cb )
57
+ if ( isJson ) {
58
+ return parseChunkedJson ( res , cb )
59
+ }
49
60
50
61
return Wreck . read ( res , null , cb )
51
62
}
@@ -56,6 +67,11 @@ function onRes (buffer, cb) {
56
67
57
68
function requestAPI ( config , path , args , qs , files , buffer , cb ) {
58
69
qs = qs || { }
70
+
71
+ if ( Array . isArray ( files ) ) {
72
+ qs . recursive = true
73
+ }
74
+
59
75
if ( Array . isArray ( path ) ) path = path . join ( '/' )
60
76
if ( args && ! Array . isArray ( args ) ) args = [ args ]
61
77
if ( args ) qs . arg = args
@@ -67,10 +83,6 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
67
83
delete qs . r
68
84
}
69
85
70
- if ( ! isNode && qs . recursive && path === 'add' ) {
71
- return cb ( new Error ( 'Recursive uploads are not supported in the browser' ) )
72
- }
73
-
74
86
qs [ 'stream-channels' ] = true
75
87
76
88
let stream
@@ -104,7 +116,7 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
104
116
opts . payload = stream
105
117
}
106
118
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 ) )
108
120
}
109
121
110
122
// -- Interface
@@ -128,9 +140,9 @@ exports = module.exports = function getRequestAPI (config) {
128
140
return requestAPI ( config , path , args , qs , files , buffer , cb )
129
141
}
130
142
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.
134
146
send . withTransform = function ( transform ) {
135
147
return function ( path , args , qs , files , buffer , cb ) {
136
148
if ( typeof buffer === 'function' ) {
0 commit comments