Skip to content

Commit

Permalink
Iterate over blob parts correctly in File.stream
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Sep 5, 2023
1 parent f4bcd43 commit 67ed782
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-bugs-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@whatwg-node/node-fetch': patch
---

Iterate over blob parts correctly
15 changes: 9 additions & 6 deletions packages/node-fetch/src/Blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,21 @@ export class PonyfillBlob implements Blob {
},
});
}
let partQueue: BlobPart[] = [];
let blobPartIterator: Iterator<BlobPart> | undefined;
return new PonyfillReadableStream({
start: controller => {
partQueue = [...this.blobParts];
if (partQueue.length === 0) {
if (this.blobParts.length === 0) {
controller.close();
return;
}
blobPartIterator = this.blobParts[Symbol.iterator]();
},
pull: controller => {
const blobPart = partQueue.pop();
const { value: blobPart, done } = blobPartIterator!.next();
if (done) {
controller.close();
return;
}
if (blobPart) {
if (isBlob(blobPart)) {
return blobPart.arrayBuffer().then(arrayBuffer => {
Expand All @@ -145,8 +150,6 @@ export class PonyfillBlob implements Blob {
const buf = getBlobPartAsBuffer(blobPart);
controller.enqueue(buf);
}
} else {
controller.close();
}
},
});
Expand Down

0 comments on commit 67ed782

Please sign in to comment.