Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: accept browser readable streams as input #21

Merged
merged 1 commit into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/files/normalise-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ module.exports = function normaliseInput (input) {
})()
}

// window.ReadableStream
if (typeof input.getReader === 'function') {
return (async function * () {
for await (const obj of browserStreamToIt(input)) {
yield toFileObject(obj)
}
})()
}

// AsyncIterable<?>
if (input[Symbol.asyncIterator]) {
return (async function * () {
Expand Down Expand Up @@ -171,6 +180,11 @@ function toAsyncIterable (input) {
return blobToAsyncGenerator(input)
}

// Browser stream
if (typeof input.getReader === 'function') {
return browserStreamToIt(input)
}

// Iterator<?>
if (input[Symbol.iterator]) {
return (async function * () { // eslint-disable-line require-await
Expand Down Expand Up @@ -232,14 +246,14 @@ function isFileObject (obj) {
function blobToAsyncGenerator (blob) {
if (typeof blob.stream === 'function') {
// firefox < 69 does not support blob.stream()
return streamBlob(blob)
return browserStreamToIt(blob.stream())
}

return readBlob(blob)
}

async function * streamBlob (blob) {
const reader = blob.stream().getReader()
async function * browserStreamToIt (stream) {
const reader = stream.getReader()

while (true) {
const result = await reader.read()
Expand Down
54 changes: 35 additions & 19 deletions test/files/normalise-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@ const globalThis = require('../../src/globalthis')
chai.use(dirtyChai)
const expect = chai.expect

const STRING = 'hello world'
const BUFFER = Buffer.from(STRING)
const ARRAY = Array.from(BUFFER)
const TYPEDARRAY = Uint8Array.from(ARRAY)
const STRING = () => 'hello world'
const BUFFER = () => Buffer.from(STRING())
const ARRAY = () => Array.from(BUFFER())
const TYPEDARRAY = () => Uint8Array.from(ARRAY())
let BLOB
let WINDOW_READABLE_STREAM

if (supportsFileReader) {
BLOB = new globalThis.Blob([
STRING
BLOB = () => new globalThis.Blob([
STRING()
])

WINDOW_READABLE_STREAM = () => new globalThis.ReadableStream({
start (controller) {
controller.enqueue(BUFFER())
controller.close()
}
})
}

async function verifyNormalisation (input) {
Expand All @@ -31,7 +39,7 @@ async function verifyNormalisation (input) {
chai.assert.fail('Content should have been an iterable or an async iterable')
}

expect(await all(input[0].content)).to.deep.equal([BUFFER])
expect(await all(input[0].content)).to.deep.equal([BUFFER()])
expect(input[0].path).to.equal('')
}

Expand All @@ -54,56 +62,56 @@ function asyncIterableOf (thing) {
describe('normalise-input', function () {
function testInputType (content, name, isBytes) {
it(name, async function () {
await testContent(content)
await testContent(content())
})

if (isBytes) {
it(`Iterable<${name}>`, async function () {
await testContent(iterableOf(content))
await testContent(iterableOf(content()))
})

it(`AsyncIterable<${name}>`, async function () {
await testContent(asyncIterableOf(content))
await testContent(asyncIterableOf(content()))
})
}

it(`{ path: '', content: ${name} }`, async function () {
await testContent({ path: '', content })
await testContent({ path: '', content: content() })
})

if (isBytes) {
it(`{ path: '', content: Iterable<${name}> }`, async function () {
await testContent({ path: '', content: iterableOf(content) })
await testContent({ path: '', content: iterableOf(content()) })
})

it(`{ path: '', content: AsyncIterable<${name}> }`, async function () {
await testContent({ path: '', content: asyncIterableOf(content) })
await testContent({ path: '', content: asyncIterableOf(content()) })
})
}

it(`Iterable<{ path: '', content: ${name} }`, async function () {
await testContent(iterableOf({ path: '', content }))
await testContent(iterableOf({ path: '', content: content() }))
})

it(`AsyncIterable<{ path: '', content: ${name} }`, async function () {
await testContent(asyncIterableOf({ path: '', content }))
await testContent(asyncIterableOf({ path: '', content: content() }))
})

if (isBytes) {
it(`Iterable<{ path: '', content: Iterable<${name}> }>`, async function () {
await testContent(iterableOf({ path: '', content: iterableOf(content) }))
await testContent(iterableOf({ path: '', content: iterableOf(content()) }))
})

it(`Iterable<{ path: '', content: AsyncIterable<${name}> }>`, async function () {
await testContent(iterableOf({ path: '', content: asyncIterableOf(content) }))
await testContent(iterableOf({ path: '', content: asyncIterableOf(content()) }))
})

it(`AsyncIterable<{ path: '', content: Iterable<${name}> }>`, async function () {
await testContent(asyncIterableOf({ path: '', content: iterableOf(content) }))
await testContent(asyncIterableOf({ path: '', content: iterableOf(content()) }))
})

it(`AsyncIterable<{ path: '', content: AsyncIterable<${name}> }>`, async function () {
await testContent(asyncIterableOf({ path: '', content: asyncIterableOf(content) }))
await testContent(asyncIterableOf({ path: '', content: asyncIterableOf(content()) }))
})
}
}
Expand All @@ -124,6 +132,14 @@ describe('normalise-input', function () {
testInputType(BLOB, 'Blob', false)
})

describe('window.ReadableStream', () => {
if (!supportsFileReader) {
return
}

testInputType(WINDOW_READABLE_STREAM, 'window.ReadableStream', false)
})

describe('Iterable<Number>', () => {
testInputType(ARRAY, 'Iterable<Number>', false)
})
Expand Down