GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — File blob is not invalidated if the file was initially empty then written to · Issue #47161 · nodejs/node

File blob is not invalidated if the file was initially empty then written to #47161

New issue
Copy link
New issue
Copy link
Closed
#47199
Closed
File blob is not invalidated if the file was initially empty then written to#47161
#47199
Copy link
Labels
fsIssues and PRs related to file-system APIs and the fs module.Issues and PRs related to file-system APIs and the fs module.

Description

@Jamesernator
Jamesernator
opened on Mar 19, 2023
Issue body actions

Version

v19.8.1

Platform

Linux 5.19.0-35-generic #36~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Feb 17 15:17:25 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

fs

What steps will reproduce the bug?

The following should produce an exception when reading the blob, but currently doesn't:

import fs from "node:fs";

// Create an empty file
fs.writeFileSync("./test.txt", "");

const blob = await fs.openAsBlob("./test.txt");

// write to the file, which should invalidate the blob
fs.writeFileSync("./test.txt", "foobar");

// This should throw an error, but returns the empty string instead
const result = await blob.text();

console.log(JSON.stringify(result)); 

How often does it reproduce? Is there a required condition?

This happens consistently.

What is the expected behavior? Why is that the expected behavior?

It should throw a DOMException, this works fine if the file was initially not empty, i.e. this throws an error as expected:

import fs from "node:fs";

// Create an empty file
fs.writeFileSync("./test.txt", "NOT_EMPTY");

const blob = await fs.openAsBlob("./test.txt");

// write to the file, which should invalidate the blob
fs.writeFileSync("./test.txt", "foobar");

// This should throw an error, but returns the empty string instead
const result = await blob.text();

console.log(JSON.stringify(result)); 

What do you see instead?

No exception is thrown.

Additional information

The Chrome behaviour for empty files being read after modification is the same as for non-empty files — i.e. an error is thrown in all cases.

(Curiously the Firefox behaviour allows reading the latest contents of the file regardless of modification status, however this seems buggy as the blob .size never actually changes).

Activity

  1. bnoordhuis commented on Mar 19, 2023

    @bnoordhuis
    bnoordhuis
    on Mar 19, 2023
    Member
    More actions

    It's probably because of this check (.text() calls .arrayBuffer()):

    node/lib/internal/blob.js

    Lines 269 to 271 in 0fd14e4

    if (this.size === 0) {
    return PromiseResolve(new ArrayBuffer(0));
    }

    And it's arguably wrong because special files (like files under /proc on linux) are often "zero-sized" while actually containing readable data.

    There's another instance of that pattern inside the stream() method:

    node/lib/internal/blob.js

    Lines 319 to 323 in 0fd14e4

    if (this.size === 0) {
    return new lazyReadableStream({
    start(c) { c.close(); },
    });
    }

    Pull request welcome.

  2. bnoordhuis
    added
    fsIssues and PRs related to file-system APIs and the fs module.Issues and PRs related to file-system APIs and the fs module.
    on Mar 19, 2023
  3. debadree25
    added a commit that references this issue on Mar 21, 2023

    fs: invalidate blob created from empty file when written to

    312e6b1
  4. nodejs-github-bot
    closed this as completedin #47199on Mar 23, 2023
  5. nodejs-github-bot
    added a commit that references this issue on Mar 23, 2023

    fs: invalidate blob created from empty file when written to

    73645d6
  6. RafaelGSS
    added 2 commits that reference this issue on Apr 5, 2023

    fs: invalidate blob created from empty file when written to

    bd72de6

    fs: invalidate blob created from empty file when written to

    2c2b07c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to file-system APIs and the fs module.Issues and PRs related to file-system APIs and the fs module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.