Skip to content

Commit

Permalink
fs: invalidate blob created from empty file when written to
Browse files Browse the repository at this point in the history
Fixes: #47161
PR-URL: #47199
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
debadree25 authored and RafaelGSS committed Apr 7, 2023
1 parent 3f49da5 commit 2c2b07c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
12 changes: 0 additions & 12 deletions lib/internal/blob.js
@@ -1,14 +1,12 @@
'use strict';

const {
ArrayBuffer,
ArrayFrom,
MathMax,
MathMin,
ObjectDefineProperties,
ObjectDefineProperty,
PromiseReject,
PromiseResolve,
ReflectConstruct,
RegExpPrototypeExec,
RegExpPrototypeSymbolReplace,
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-blob-file-backed.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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());

0 comments on commit 2c2b07c

Please sign in to comment.