From bd72de645318f16f84a7f3f390f7387bfa4c2f74 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Thu, 23 Mar 2023 23:58:43 +0530 Subject: [PATCH] fs: invalidate blob created from empty file when written to MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/nodejs/node/issues/47161 PR-URL: https://github.com/nodejs/node/pull/47199 Reviewed-By: Ben Noordhuis Reviewed-By: Michaƫl Zasso --- lib/internal/blob.js | 12 ------------ test/parallel/test-blob-file-backed.js | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/internal/blob.js b/lib/internal/blob.js index 226a071b6d1a79..4188d999f7fde7 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -1,14 +1,12 @@ 'use strict'; const { - ArrayBuffer, ArrayFrom, MathMax, MathMin, ObjectDefineProperties, ObjectDefineProperty, PromiseReject, - PromiseResolve, ReflectConstruct, RegExpPrototypeExec, RegExpPrototypeSymbolReplace, @@ -266,10 +264,6 @@ class Blob { if (!isBlob(this)) return PromiseReject(new ERR_INVALID_THIS('Blob')); - if (this.size === 0) { - return PromiseResolve(new ArrayBuffer(0)); - } - const { promise, resolve, reject } = createDeferredPromise(); const reader = this[kHandle].getReader(); const buffers = []; @@ -316,12 +310,6 @@ class Blob { if (!isBlob(this)) throw new ERR_INVALID_THIS('Blob'); - if (this.size === 0) { - return new lazyReadableStream({ - start(c) { c.close(); }, - }); - } - const reader = this[kHandle].getReader(); return new lazyReadableStream({ start(c) { diff --git a/test/parallel/test-blob-file-backed.js b/test/parallel/test-blob-file-backed.js index 7271fef2f3ce46..225b660c7bbdb3 100644 --- a/test/parallel/test-blob-file-backed.js +++ b/test/parallel/test-blob-file-backed.js @@ -20,12 +20,14 @@ const { Blob } = require('buffer'); const tmpdir = require('../common/tmpdir'); const testfile = path.join(tmpdir.path, 'test-file-backed-blob.txt'); const testfile2 = path.join(tmpdir.path, 'test-file-backed-blob2.txt'); +const testfile3 = path.join(tmpdir.path, 'test-file-backed-blob3.txt'); tmpdir.refresh(); const data = `${'a'.repeat(1000)}${'b'.repeat(2000)}`; writeFileSync(testfile, data); writeFileSync(testfile2, data.repeat(100)); +writeFileSync(testfile3, ''); (async () => { const blob = await openAsBlob(testfile); @@ -79,3 +81,21 @@ writeFileSync(testfile2, data.repeat(100)); await unlink(testfile2); })().then(common.mustCall()); + +(async () => { + const blob = await openAsBlob(testfile3); + strictEqual(blob.size, 0); + strictEqual(await blob.text(), ''); + writeFileSync(testfile3, 'abc'); + await rejects(blob.text(), { name: 'NotReadableError' }); + await unlink(testfile3); +})().then(common.mustCall()); + +(async () => { + const blob = await openAsBlob(testfile3); + strictEqual(blob.size, 0); + writeFileSync(testfile3, 'abc'); + const stream = blob.stream(); + const reader = stream.getReader(); + await rejects(() => reader.read(), { name: 'NotReadableError' }); +})().then(common.mustCall());