Compare commits

..
1 Commits
Author SHA1 Message Date
Jimmy Wärting f077471a23 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
2022-03-10 15:11:10 +01:00
4 changed files with 21 additions and 5 deletions
+8 -2
View File
@@ -65,6 +65,9 @@ class BlobDataItem {
this.#start = options.start this.#start = options.start
this.size = options.size this.size = options.size
this.lastModified = options.lastModified this.lastModified = options.lastModified
this.originalSize = options.originalSize === undefined
? options.size
: options.originalSize
} }
/** /**
@@ -75,16 +78,19 @@ class BlobDataItem {
return new BlobDataItem({ return new BlobDataItem({
path: this.#path, path: this.#path,
lastModified: this.lastModified, lastModified: this.lastModified,
originalSize: this.originalSize,
size: end - start, size: end - start,
start: this.#start + start start: this.#start + start
}) })
} }
async * stream () { async * stream () {
const { mtimeMs } = await stat(this.#path) const { mtimeMs, size } = await stat(this.#path)
if (mtimeMs > this.lastModified) {
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') 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, { yield * createReadStream(this.#path, {
start: this.#start, start: this.#start,
end: this.#start + this.size - 1 end: this.#start + this.size - 1
+6 -2
View File
@@ -83,8 +83,12 @@ const _Blob = class Blob {
part = encoder.encode(`${element}`) part = encoder.encode(`${element}`)
} }
this.#size += ArrayBuffer.isView(part) ? part.byteLength : part.size const size = ArrayBuffer.isView(part) ? part.byteLength : part.size
this.#parts.push(part) // 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}` this.#endings = `${options.endings === undefined ? 'transparent' : options.endings}`
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "fetch-blob", "name": "fetch-blob",
"version": "3.1.4", "version": "3.1.5",
"description": "Blob & File implementation in Node.js, originally from node-fetch.", "description": "Blob & File implementation in Node.js, originally from node-fetch.",
"main": "index.js", "main": "index.js",
"type": "module", "type": "module",
+6
View File
@@ -189,6 +189,12 @@ promise_test(async () => {
assert_equals(await (await fileFrom('./LICENSE')).text(), license.toString()) assert_equals(await (await fileFrom('./LICENSE')).text(), license.toString())
}, 'blob part backed up by filesystem slice correctly') }, '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 () => { test(async () => {
const blob = blobFromSync('./LICENSE') const blob = blobFromSync('./LICENSE')
await new Promise(resolve => setTimeout(resolve, 2000)) await new Promise(resolve => setTimeout(resolve, 2000))