@@ -12,6 +12,7 @@ const toPull = require('stream-to-pull-stream')
12
12
const deferred = require ( 'pull-defer' )
13
13
const waterfall = require ( 'async/waterfall' )
14
14
const isStream = require ( 'is-stream' )
15
+ const isSource = require ( 'is-pull-stream' ) . isSource
15
16
const Duplex = require ( 'readable-stream' ) . Duplex
16
17
const OtherBuffer = require ( 'buffer' ) . Buffer
17
18
const CID = require ( 'cids' )
@@ -60,6 +61,10 @@ function normalizeContent (opts, content) {
60
61
data = { path : '' , content : toPull . source ( data ) }
61
62
}
62
63
64
+ if ( isSource ( data ) ) {
65
+ data = { path : '' , content : data }
66
+ }
67
+
63
68
if ( data && data . content && typeof data . content !== 'function' ) {
64
69
if ( Buffer . isBuffer ( data . content ) ) {
65
70
data . content = pull . values ( [ data . content ] )
@@ -196,40 +201,56 @@ module.exports = function files (self) {
196
201
}
197
202
198
203
return {
199
- add : promisify ( ( data , options = { } , callback ) => {
200
- if ( typeof options === 'function' ) {
201
- callback = options
202
- options = { }
203
- } else if ( ! callback || typeof callback !== 'function' ) {
204
- callback = noop
205
- }
204
+ add : ( ( ) => {
205
+ const add = promisify ( ( data , options = { } , callback ) => {
206
+ if ( typeof options === 'function' ) {
207
+ callback = options
208
+ options = { }
209
+ } else if ( ! callback || typeof callback !== 'function' ) {
210
+ callback = noop
211
+ }
206
212
207
- const ok = Buffer . isBuffer ( data ) ||
208
- isStream . readable ( data ) ||
209
- Array . isArray ( data ) ||
210
- OtherBuffer . isBuffer ( data ) ||
211
- typeof data === 'object'
213
+ const ok = Buffer . isBuffer ( data ) ||
214
+ isStream . readable ( data ) ||
215
+ Array . isArray ( data ) ||
216
+ OtherBuffer . isBuffer ( data ) ||
217
+ typeof data === 'object' ||
218
+ isSource ( data )
212
219
213
- if ( ! ok ) {
214
- return callback ( new Error ( 'first arg must be a buffer, readable stream, an object or array of objects' ) )
215
- }
220
+ if ( ! ok ) {
221
+ return callback ( new Error ( 'first arg must be a buffer, readable stream, pull stream, an object or array of objects' ) )
222
+ }
216
223
217
- // CID v0 is for multihashes encoded with sha2-256
218
- if ( options . hashAlg && options . cidVersion !== 1 ) {
219
- options . cidVersion = 1
220
- }
224
+ // CID v0 is for multihashes encoded with sha2-256
225
+ if ( options . hashAlg && options . cidVersion !== 1 ) {
226
+ options . cidVersion = 1
227
+ }
221
228
222
- pull (
223
- pull . values ( [ data ] ) ,
224
- _addPullStream ( options ) ,
225
- sort ( ( a , b ) => {
226
- if ( a . path < b . path ) return 1
227
- if ( a . path > b . path ) return - 1
228
- return 0
229
- } ) ,
230
- pull . collect ( callback )
231
- )
232
- } ) ,
229
+ pull (
230
+ pull . values ( [ data ] ) ,
231
+ _addPullStream ( options ) ,
232
+ sort ( ( a , b ) => {
233
+ if ( a . path < b . path ) return 1
234
+ if ( a . path > b . path ) return - 1
235
+ return 0
236
+ } ) ,
237
+ pull . collect ( callback )
238
+ )
239
+ } )
240
+
241
+ return function ( ) {
242
+ const args = Array . from ( arguments )
243
+
244
+ // If we files.add(<pull stream>), then promisify thinks the pull stream
245
+ // is a callback! Add an empty options object in this case so that a
246
+ // promise is returned.
247
+ if ( args . length === 1 && isSource ( args [ 0 ] ) ) {
248
+ args . push ( { } )
249
+ }
250
+
251
+ return add . apply ( null , args )
252
+ }
253
+ } ) ( ) ,
233
254
234
255
addReadableStream : ( options ) => {
235
256
options = options || { }
0 commit comments