1
1
'use strict'
2
- /* eslint-env browser */
3
2
4
- const { Buffer } = require ( 'buffer' )
5
3
const errCode = require ( 'err-code' )
6
4
const toAsyncIterable = require ( '../lib/file-data-to-async-iterable' )
5
+ const isBytes = require ( '../lib/is-bytes' )
6
+ const isBloby = require ( '../lib/is-bloby' )
7
7
8
8
/*
9
9
Transform one of:
@@ -16,6 +16,8 @@ Blob|File
16
16
{ path, content: AsyncIterable<Buffer> }
17
17
{ path, content: PullStream<Buffer> }
18
18
Iterable<Number>
19
+ Iterable<Buffer>
20
+ Iterable<Blob>
19
21
Iterable<{ path, content: Buffer }>
20
22
Iterable<{ path, content: Blob }>
21
23
Iterable<{ path, content: Iterable<Number> }>
@@ -36,20 +38,16 @@ AsyncIterable<{ path, content: AsyncIterable<Buffer> }>
36
38
37
39
module . exports = function normalizeInput ( input ) {
38
40
// Buffer|ArrayBuffer|TypedArray
39
- if ( Buffer . isBuffer ( input ) || ArrayBuffer . isView ( input ) || input instanceof ArrayBuffer ) {
40
- return ( async function * ( ) { // eslint-disable-line require-await
41
- yield normalizeTuple ( { path : '' , content : input } )
42
- } ) ( )
43
- }
44
-
45
41
// Blob|File
46
- if ( typeof Blob !== 'undefined' && input instanceof Blob ) {
42
+ if ( isBytes ( input ) || isBloby ( input ) ) {
47
43
return ( async function * ( ) { // eslint-disable-line require-await
48
44
yield normalizeTuple ( { path : '' , content : input } )
49
45
} ) ( )
50
46
}
51
47
52
48
// Iterable<Number>
49
+ // Iterable<Buffer>
50
+ // Iterable<Blob>
53
51
// Iterable<{ path, content: Buffer }>
54
52
// Iterable<{ path, content: Blob }>
55
53
// Iterable<{ path, content: Iterable<Number> }>
@@ -58,7 +56,9 @@ module.exports = function normalizeInput (input) {
58
56
if ( input [ Symbol . iterator ] ) {
59
57
return ( async function * ( ) { // eslint-disable-line require-await
60
58
for ( const chunk of input ) {
61
- if ( typeof chunk === 'object' && ( chunk . path || chunk . content ) ) {
59
+ if ( isBytes ( chunk ) || isBloby ( chunk ) ) {
60
+ yield normalizeTuple ( { path : '' , content : chunk } )
61
+ } else if ( isFileObject ( chunk ) ) {
62
62
yield normalizeTuple ( chunk )
63
63
} else if ( Number . isInteger ( chunk ) ) { // Must be an Iterable<Number> i.e. Buffer/ArrayBuffer/Array of bytes
64
64
yield normalizeTuple ( { path : '' , content : input } )
@@ -79,7 +79,7 @@ module.exports = function normalizeInput (input) {
79
79
if ( input [ Symbol . asyncIterator ] ) {
80
80
return ( async function * ( ) {
81
81
for await ( const chunk of input ) {
82
- if ( typeof chunk === 'object' && ( chunk . path || chunk . content ) ) {
82
+ if ( isFileObject ( chunk ) ) {
83
83
yield normalizeTuple ( chunk )
84
84
} else { // Must be an AsyncIterable<Buffer> i.e. a Stream
85
85
let path = ''
@@ -110,7 +110,7 @@ module.exports = function normalizeInput (input) {
110
110
// { path, content: Iterable<Buffer> }
111
111
// { path, content: AsyncIterable<Buffer> }
112
112
// { path, content: PullStream<Buffer> }
113
- if ( typeof input === 'object' && ( input . path || input . content ) ) {
113
+ if ( isFileObject ( input ) ) {
114
114
// eslint-disable-next-line require-await
115
115
return ( async function * ( ) { yield normalizeTuple ( input ) } ) ( )
116
116
}
@@ -128,3 +128,8 @@ module.exports = function normalizeInput (input) {
128
128
function normalizeTuple ( { path, content } ) {
129
129
return { path : path || '' , content : content ? toAsyncIterable ( content ) : null }
130
130
}
131
+
132
+ // An object with a path or content property
133
+ function isFileObject ( obj ) {
134
+ return typeof obj === 'object' && ( obj . path || obj . content )
135
+ }
0 commit comments