From 971382d8c91e09ea842cabe0bdc84a1bd87809a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Sun, 23 May 2021 23:07:10 +0200 Subject: [PATCH 1/2] use async await loop --- index.js | 22 +++++----------------- test/basic.js | 4 +++- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 598ad65..0b3decf 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) From 36b665072a28d0c925de68834eaed68c184a95b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Sun, 23 May 2021 23:08:26 +0200 Subject: [PATCH 2/2] default to empty string --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 0b3decf..ff8fe6b 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ module.exports = iteratorToBlob -async function iteratorToBlob (stream, type) { +async function iteratorToBlob (stream, type = '') { const chunks = [] for await (const chunk of stream) chunks.push(chunk) return new Blob(chunks, { type })