Skip to content

Commit f077471

Browse files
authored
fix: avoid reading files when the size is empty (#134)
* 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
1 parent dc29588 commit f077471

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

from.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class BlobDataItem {
6565
this.#start = options.start
6666
this.size = options.size
6767
this.lastModified = options.lastModified
68+
this.originalSize = options.originalSize === undefined
69+
? options.size
70+
: options.originalSize
6871
}
6972

7073
/**
@@ -75,16 +78,19 @@ class BlobDataItem {
7578
return new BlobDataItem({
7679
path: this.#path,
7780
lastModified: this.lastModified,
81+
originalSize: this.originalSize,
7882
size: end - start,
7983
start: this.#start + start
8084
})
8185
}
8286

8387
async * stream () {
84-
const { mtimeMs } = await stat(this.#path)
85-
if (mtimeMs > this.lastModified) {
88+
const { mtimeMs, size } = await stat(this.#path)
89+
90+
if (mtimeMs > this.lastModified || this.originalSize !== size) {
8691
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')
8792
}
93+
8894
yield * createReadStream(this.#path, {
8995
start: this.#start,
9096
end: this.#start + this.size - 1

index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,12 @@ const _Blob = class Blob {
8383
part = encoder.encode(`${element}`)
8484
}
8585

86-
this.#size += ArrayBuffer.isView(part) ? part.byteLength : part.size
87-
this.#parts.push(part)
86+
const size = ArrayBuffer.isView(part) ? part.byteLength : part.size
87+
// Avoid pushing empty parts into the array to better GC them
88+
if (size) {
89+
this.#size += size
90+
this.#parts.push(part)
91+
}
8892
}
8993

9094
this.#endings = `${options.endings === undefined ? 'transparent' : options.endings}`

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fetch-blob",
3-
"version": "3.1.4",
3+
"version": "3.1.5",
44
"description": "Blob & File implementation in Node.js, originally from node-fetch.",
55
"main": "index.js",
66
"type": "module",

test/own-misc-test.js

+6
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ promise_test(async () => {
189189
assert_equals(await (await fileFrom('./LICENSE')).text(), license.toString())
190190
}, 'blob part backed up by filesystem slice correctly')
191191

192+
promise_test(async () => {
193+
fs.writeFileSync('temp', '')
194+
await blobFromSync('./temp').text()
195+
fs.unlinkSync('./temp')
196+
}, 'can read empty files')
197+
192198
test(async () => {
193199
const blob = blobFromSync('./LICENSE')
194200
await new Promise(resolve => setTimeout(resolve, 2000))

0 commit comments

Comments
 (0)