diff --git a/index.js b/index.js index 598ad65..ff8fe6b 100644 --- a/index.js +++ b/index.js @@ -1,22 +1,10 @@ /*! stream-to-blob. MIT License. Feross Aboukhadijeh */ /* 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 }) } diff --git a/test/basic.js b/test/basic.js index c90eb76..0c5ac48 100644 --- a/test/basic.js +++ b/test/basic.js @@ -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)