Skip to content

Commit

Permalink
fix: avoid reading files when the size is empty (#134)
Browse files Browse the repository at this point in the history
* fix: avoid reading files when the size is empty

* also use stat.size for checking if it is modified

* bump minor version number

* Don't push empty chunks into the array (creates faster reads)

* reverted back to avoid making a if check
  • Loading branch information
jimmywarting committed Mar 10, 2022
1 parent dc29588 commit f077471
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
10 changes: 8 additions & 2 deletions from.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class BlobDataItem {
this.#start = options.start
this.size = options.size
this.lastModified = options.lastModified
this.originalSize = options.originalSize === undefined
? options.size
: options.originalSize
}

/**
Expand All @@ -75,16 +78,19 @@ class BlobDataItem {
return new BlobDataItem({
path: this.#path,
lastModified: this.lastModified,
originalSize: this.originalSize,
size: end - start,
start: this.#start + start
})
}

async * stream () {
const { mtimeMs } = await stat(this.#path)
if (mtimeMs > this.lastModified) {
const { mtimeMs, size } = await stat(this.#path)

if (mtimeMs > this.lastModified || this.originalSize !== size) {
throw new DOMException('The requested file could not be read, typically due to permission problems that have occurred after a reference to a file was acquired.', 'NotReadableError')
}

yield * createReadStream(this.#path, {
start: this.#start,
end: this.#start + this.size - 1
Expand Down
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ const _Blob = class Blob {
part = encoder.encode(`${element}`)
}

this.#size += ArrayBuffer.isView(part) ? part.byteLength : part.size
this.#parts.push(part)
const size = ArrayBuffer.isView(part) ? part.byteLength : part.size
// Avoid pushing empty parts into the array to better GC them
if (size) {
this.#size += size
this.#parts.push(part)
}
}

this.#endings = `${options.endings === undefined ? 'transparent' : options.endings}`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fetch-blob",
"version": "3.1.4",
"version": "3.1.5",
"description": "Blob & File implementation in Node.js, originally from node-fetch.",
"main": "index.js",
"type": "module",
Expand Down
6 changes: 6 additions & 0 deletions test/own-misc-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ promise_test(async () => {
assert_equals(await (await fileFrom('./LICENSE')).text(), license.toString())
}, 'blob part backed up by filesystem slice correctly')

promise_test(async () => {
fs.writeFileSync('temp', '')
await blobFromSync('./temp').text()
fs.unlinkSync('./temp')
}, 'can read empty files')

test(async () => {
const blob = blobFromSync('./LICENSE')
await new Promise(resolve => setTimeout(resolve, 2000))
Expand Down

0 comments on commit f077471

Please sign in to comment.