Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid reading files when the size is empty #134

Merged
merged 5 commits into from
Mar 10, 2022

Conversation

jimmywarting
Copy link
Contributor

@jimmywarting jimmywarting commented Mar 10, 2022

The purpose of this PR is:

... to fix a range error when it tries to read a empty file:
"RangeError [ERR_OUT_OF_RANGE]: The value of "end" is out of range. It must be >= 0 && <= 9007199254740991. Received -1"

This is what had to change:

... Avoid pushing empty chunks into the bucket and avoid reading them all together.

This is what like reviewers to know:

... this affected the blob backed up by the filesystem


  • I prefixed the PR-title with docs: , fix(area): , feat(area): or breaking(area):
  • I Added unit test(s)

@codecov

This comment was marked as resolved.

@jimmywarting
Copy link
Contributor Author

also made sure it checks the stat.size if it has changed for good measures... some application can change the content without modifying the date

@jimmywarting jimmywarting requested review from gr2m and LinusU March 10, 2022 11:05
@@ -65,6 +65,9 @@ class BlobDataItem {
this.#start = options.start
this.size = options.size
this.lastModified = options.lastModified
this.originalSize = options.originalSize === undefined
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally try to treat null and undefined the same in the code, to avoid unexpected behaviour. This also lets us transition to the ?? operator when we drop support for Node.js 12.x, without changing the behaviour of the code.

Suggested change
this.originalSize = options.originalSize === undefined
this.originalSize = options.originalSize == null

(x == null is the same as x === undefined || x === null)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i know that, i also try to avoid null from time to times. and i can't wait until we can start using ??=

but i wanted to avoid using == instead of === and breaking any compatibility with nullish stuff
also since there is no occurrences of null in the code, then i tought it would be fine to just check for undefined

could also just do this.originalSize = options.originalSize || options.size

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options.originalSize || options.size will use options.size if options.originalSize is 0 though, that's probably not what we want

Copy link
Contributor Author

@jimmywarting jimmywarting Mar 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought about it too, but after commit 568b309 then that might not be needed after all, cuz 0-sized blob and typedarrays will not be in the source anymore

Copy link
Member

@LinusU LinusU left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat 👍

@jimmywarting jimmywarting merged commit f077471 into node-fetch:main Mar 10, 2022
@jimmywarting jimmywarting deleted the read-empty-files branch March 10, 2022 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants