@@ -4,20 +4,29 @@ 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
+ console . log ( 'Parsed total of:' , parsed . length )
24
+ console . log ( 'Total byte size was:' , c . length )
25
+ cb ( null , parsed )
26
+ } )
18
27
}
19
28
20
- function onRes ( buffer , cb ) {
29
+ function onRes ( buffer , cb , uri ) {
21
30
return ( err , res ) => {
22
31
if ( err ) {
23
32
return cb ( err )
@@ -27,6 +36,10 @@ function onRes (buffer, cb) {
27
36
const chunkedObjects = Boolean ( res . headers [ 'x-chunked-output' ] )
28
37
const isJson = res . headers [ 'content-type' ] && res . headers [ 'content-type' ] . indexOf ( 'application/json' ) === 0
29
38
39
+ if ( uri . indexOf ( 'add' ) !== - 1 ) {
40
+ console . log ( 'HEADERS (stream, chunked, json)' , stream , chunkedObjects , isJson )
41
+ }
42
+
30
43
if ( res . statusCode >= 400 || ! res . statusCode ) {
31
44
const error = new Error ( `Server responded with ${ res . statusCode } ` )
32
45
@@ -42,10 +55,14 @@ function onRes (buffer, cb) {
42
55
} )
43
56
}
44
57
45
- if ( stream && ! buffer ) return cb ( null , res )
58
+ if ( stream && ! buffer ) {
59
+ return cb ( null , res )
60
+ }
46
61
47
62
if ( chunkedObjects ) {
48
- if ( isJson ) return parseChunkedJson ( res , cb )
63
+ if ( isJson ) {
64
+ return parseChunkedJson ( res , cb )
65
+ }
49
66
50
67
return Wreck . read ( res , null , cb )
51
68
}
@@ -56,6 +73,11 @@ function onRes (buffer, cb) {
56
73
57
74
function requestAPI ( config , path , args , qs , files , buffer , cb ) {
58
75
qs = qs || { }
76
+
77
+ if ( Array . isArray ( files ) ) {
78
+ qs . recursive = true
79
+ }
80
+
59
81
if ( Array . isArray ( path ) ) path = path . join ( '/' )
60
82
if ( args && ! Array . isArray ( args ) ) args = [ args ]
61
83
if ( args ) qs . arg = args
@@ -67,10 +89,6 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
67
89
delete qs . r
68
90
}
69
91
70
- if ( ! isNode && qs . recursive && path === 'add' ) {
71
- return cb ( new Error ( 'Recursive uploads are not supported in the browser' ) )
72
- }
73
-
74
92
qs [ 'stream-channels' ] = true
75
93
76
94
let stream
@@ -104,7 +122,7 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
104
122
opts . payload = stream
105
123
}
106
124
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 ) )
108
126
}
109
127
110
128
// -- Interface
@@ -128,9 +146,9 @@ exports = module.exports = function getRequestAPI (config) {
128
146
return requestAPI ( config , path , args , qs , files , buffer , cb )
129
147
}
130
148
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.
134
152
send . withTransform = function ( transform ) {
135
153
return function ( path , args , qs , files , buffer , cb ) {
136
154
if ( typeof buffer === 'function' ) {
0 commit comments