From 0a01d130f9181534c7c6bd29e9b41a785e3585b2 Mon Sep 17 00:00:00 2001 From: Khafra <42794878+KhafraDev@users.noreply.github.com> Date: Thu, 30 Mar 2023 10:38:28 -0400 Subject: [PATCH] buffer: fix blob range error with many chunks --- 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());