Skip to content
Open
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
22 changes: 5 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
/*! stream-to-blob. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
/* global Blob */

module.exports = streamToBlob
module.exports = iteratorToBlob

function streamToBlob (stream, mimeType) {
if (mimeType != null && typeof mimeType !== 'string') {
throw new Error('Invalid mimetype, expected string.')
}
return new Promise((resolve, reject) => {
const chunks = []
stream
.on('data', chunk => chunks.push(chunk))
.once('end', () => {
const blob = mimeType != null
? new Blob(chunks, { type: mimeType })
: new Blob(chunks)
resolve(blob)
})
.once('error', reject)
})
async function iteratorToBlob (stream, type = '') {
const chunks = []
for await (const chunk of stream) chunks.push(chunk)
return new Blob(chunks, { type })
}
4 changes: 3 additions & 1 deletion test/basic.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const test = require('tape')
const toBlob = require('../')
// Could perhaps be replace with blob.text()
const toBuffer = require('blob-to-buffer')
// TODO: replace with stream.Readable.from
const toStream = require('string-to-stream')
const toBlob = require('../index.js')

test('basic usage with Promise', t => {
t.plan(2)
Expand Down