Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 5ab9c36

Browse files
author
Alan Shaw
committed
fix: normalize input for iterable of buffer or blob
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 7acec6f commit 5ab9c36

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

src/add/normalise-input.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
2-
/* eslint-env browser */
32

4-
const { Buffer } = require('buffer')
53
const errCode = require('err-code')
64
const toAsyncIterable = require('../lib/file-data-to-async-iterable')
5+
const isBytes = require('../lib/is-bytes')
6+
const isBloby = require('../lib/is-bloby')
77

88
/*
99
Transform one of:
@@ -16,6 +16,8 @@ Blob|File
1616
{ path, content: AsyncIterable<Buffer> }
1717
{ path, content: PullStream<Buffer> }
1818
Iterable<Number>
19+
Iterable<Buffer>
20+
Iterable<Blob>
1921
Iterable<{ path, content: Buffer }>
2022
Iterable<{ path, content: Blob }>
2123
Iterable<{ path, content: Iterable<Number> }>
@@ -36,20 +38,16 @@ AsyncIterable<{ path, content: AsyncIterable<Buffer> }>
3638

3739
module.exports = function normalizeInput (input) {
3840
// 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-
4541
// Blob|File
46-
if (typeof Blob !== 'undefined' && input instanceof Blob) {
42+
if (isBytes(input) || isBloby(input)) {
4743
return (async function * () { // eslint-disable-line require-await
4844
yield normalizeTuple({ path: '', content: input })
4945
})()
5046
}
5147

5248
// Iterable<Number>
49+
// Iterable<Buffer>
50+
// Iterable<Blob>
5351
// Iterable<{ path, content: Buffer }>
5452
// Iterable<{ path, content: Blob }>
5553
// Iterable<{ path, content: Iterable<Number> }>
@@ -58,7 +56,9 @@ module.exports = function normalizeInput (input) {
5856
if (input[Symbol.iterator]) {
5957
return (async function * () { // eslint-disable-line require-await
6058
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)) {
6262
yield normalizeTuple(chunk)
6363
} else if (Number.isInteger(chunk)) { // Must be an Iterable<Number> i.e. Buffer/ArrayBuffer/Array of bytes
6464
yield normalizeTuple({ path: '', content: input })
@@ -79,7 +79,7 @@ module.exports = function normalizeInput (input) {
7979
if (input[Symbol.asyncIterator]) {
8080
return (async function * () {
8181
for await (const chunk of input) {
82-
if (typeof chunk === 'object' && (chunk.path || chunk.content)) {
82+
if (isFileObject(chunk)) {
8383
yield normalizeTuple(chunk)
8484
} else { // Must be an AsyncIterable<Buffer> i.e. a Stream
8585
let path = ''
@@ -110,7 +110,7 @@ module.exports = function normalizeInput (input) {
110110
// { path, content: Iterable<Buffer> }
111111
// { path, content: AsyncIterable<Buffer> }
112112
// { path, content: PullStream<Buffer> }
113-
if (typeof input === 'object' && (input.path || input.content)) {
113+
if (isFileObject(input)) {
114114
// eslint-disable-next-line require-await
115115
return (async function * () { yield normalizeTuple(input) })()
116116
}
@@ -128,3 +128,8 @@ module.exports = function normalizeInput (input) {
128128
function normalizeTuple ({ path, content }) {
129129
return { path: path || '', content: content ? toAsyncIterable(content) : null }
130130
}
131+
132+
// An object with a path or content property
133+
function isFileObject (obj) {
134+
return typeof obj === 'object' && (obj.path || obj.content)
135+
}

src/lib/file-data-to-async-iterable.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const toIterator = require('pull-stream-to-async-iterator')
55
const { Buffer } = require('buffer')
66
const blobToAsyncIterable = require('../lib/blob-to-async-iterable')
7+
const isBloby = require('../lib/is-bloby')
78

89
/*
910
Transform one of:
@@ -29,7 +30,7 @@ module.exports = function toAsyncIterable (input) {
2930
}
3031

3132
// Blob|File
32-
if (typeof Blob !== 'undefined' && input instanceof Blob) {
33+
if (isBloby(input)) {
3334
return Object.assign(
3435
blobToAsyncIterable(input),
3536
{ length: input.size }

src/lib/is-bloby.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
/* eslint-env browser */
3+
4+
// Blob|File
5+
module.exports = function isBloby (obj) {
6+
return typeof Blob !== 'undefined' && obj instanceof Blob
7+
}

src/lib/is-bytes.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
const { Buffer } = require('buffer')
4+
5+
// Buffer|ArrayBuffer|TypedArray
6+
module.exports = function isBytes (obj) {
7+
return Buffer.isBuffer(obj) || ArrayBuffer.isView(obj) || obj instanceof ArrayBuffer
8+
}

0 commit comments

Comments
 (0)