From 23a69d92798c8024c8e41be0070f64f49374499e Mon Sep 17 00:00:00 2001 From: Khafra Date: Sat, 1 Apr 2023 10:51:26 -0400 Subject: [PATCH] buffer: fix blob range error with many chunks PR-URL: https://github.com/nodejs/node/pull/47320 Reviewed-By: James M Snell Reviewed-By: Yagiz Nizipli Reviewed-By: Mohammed Keyvanzadeh --- lib/internal/blob.js | 4 +++- test/parallel/test-blob.js | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/internal/blob.js b/lib/internal/blob.js index 4188d999f7fde7..9c6be6981f5b88 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -69,6 +69,8 @@ const { CountQueuingStrategy, } = require('internal/webstreams/queuingstrategies'); +const { queueMicrotask } = require('internal/process/task_queues'); + const kHandle = Symbol('kHandle'); const kType = Symbol('kType'); const kLength = Symbol('kLength'); @@ -284,7 +286,7 @@ class Blob { } if (buffer !== undefined) buffers.push(buffer); - readNext(); + queueMicrotask(() => readNext()); }); }; readNext(); diff --git a/test/parallel/test-blob.js b/test/parallel/test-blob.js index 68fd2d547a0e80..6b6ce70687660e 100644 --- a/test/parallel/test-blob.js +++ b/test/parallel/test-blob.js @@ -315,3 +315,16 @@ assert.throws(() => new Blob({}), { delete Object.prototype.type; } + +(async () => { + // Refs: https://github.com/nodejs/node/issues/47301 + + const random = Buffer.alloc(256).fill('0'); + const chunks = []; + + for (let i = 0; i < random.length; i += 2) { + chunks.push(random.subarray(i, i + 2)); + } + + await new Blob(chunks).arrayBuffer(); +})().then(common.mustCall());