@@ -14,7 +14,7 @@ const waterfall = require('async/waterfall')
14
14
const isStream = require ( 'is-stream' )
15
15
const isSource = require ( 'is-pull-stream' ) . isSource
16
16
const Duplex = require ( 'readable-stream' ) . Duplex
17
- const OtherBuffer = require ( 'buffer' ) . Buffer
17
+ const isString = require ( 'lodash/isString' )
18
18
const CID = require ( 'cids' )
19
19
const toB58String = require ( 'multihashes' ) . toB58String
20
20
const errCode = require ( 'err-code' )
@@ -292,15 +292,23 @@ module.exports = function (self) {
292
292
293
293
options = options || { }
294
294
295
- const ok = Buffer . isBuffer ( data ) ||
296
- isStream . readable ( data ) ||
297
- Array . isArray ( data ) ||
298
- OtherBuffer . isBuffer ( data ) ||
299
- typeof data === 'object' ||
300
- isSource ( data )
295
+ // Buffer, pull stream or Node.js stream
296
+ const isBufferOrStream = obj => Buffer . isBuffer ( obj ) || isStream . readable ( obj ) || isSource ( obj )
297
+ // An object like { content?, path? }, where content isBufferOrStream and path isString
298
+ const isContentObject = obj => {
299
+ if ( typeof obj !== 'object' ) return false
300
+ // path is optional if content is present
301
+ if ( obj . content ) return isBufferOrStream ( obj . content )
302
+ // path must be a non-empty string if no content
303
+ return Boolean ( obj . path ) && isString ( obj . path )
304
+ }
305
+ // An input atom: a buffer, stream or content object
306
+ const isInput = obj => isBufferOrStream ( obj ) || isContentObject ( obj )
307
+ // All is ok if data isInput or data is an array of isInput
308
+ const ok = isInput ( data ) || ( Array . isArray ( data ) && data . every ( isInput ) )
301
309
302
310
if ( ! ok ) {
303
- return callback ( new Error ( 'first arg must be a buffer, readable stream, pull stream, an object or array of objects' ) )
311
+ return callback ( new Error ( 'invalid input: expected buffer, readable stream, pull stream, object or array of objects' ) )
304
312
}
305
313
306
314
// CID v0 is for multihashes encoded with sha2-256
0 commit comments