@@ -4,7 +4,7 @@ const promisify = require('promisify-es6')
44const ConcatStream = require ( 'concat-stream' )
55const once = require ( 'once' )
66const isStream = require ( 'is-stream' )
7- const OtherBuffer = require ( 'buffer' ) . Buffer
7+ const isString = require ( 'lodash/isString' )
88const isSource = require ( 'is-pull-stream' ) . isSource
99const FileResultStreamConverter = require ( '../utils/file-result-stream-converter' )
1010const SendFilesStream = require ( '../utils/send-files-stream' )
@@ -25,15 +25,23 @@ module.exports = (send) => {
2525 }
2626 options . converter = FileResultStreamConverter
2727
28- const ok = Buffer . isBuffer ( _files ) ||
29- isStream . readable ( _files ) ||
30- Array . isArray ( _files ) ||
31- OtherBuffer . isBuffer ( _files ) ||
32- typeof _files === 'object' ||
33- isSource ( _files )
28+ // Buffer, pull stream or Node.js stream
29+ const isBufferOrStream = obj => Buffer . isBuffer ( obj ) || isStream . readable ( obj ) || isSource ( obj )
30+ // An object like { content?, path? }, where content isBufferOrStream and path isString
31+ const isContentObject = obj => {
32+ if ( typeof obj !== 'object' ) return false
33+ // path is optional if content is present
34+ if ( obj . content ) return isBufferOrStream ( obj . content )
35+ // path must be a non-empty string if no content
36+ return Boolean ( obj . path ) && isString ( obj . path )
37+ }
38+ // An input atom: a buffer, stream or content object
39+ const isInput = obj => isBufferOrStream ( obj ) || isContentObject ( obj )
40+ // All is ok if data isInput or data is an array of isInput
41+ const ok = isInput ( _files ) || ( Array . isArray ( _files ) && _files . every ( isInput ) )
3442
3543 if ( ! ok ) {
36- return callback ( new Error ( 'first arg must be a buffer, readable stream, pull stream, an object or array of objects' ) )
44+ return callback ( new Error ( 'invalid input: expected buffer, readable stream, pull stream, object or array of objects' ) )
3745 }
3846
3947 const files = [ ] . concat ( _files )
0 commit comments