Skip to content

Commit

Permalink
fix(node-fetch): support empty ctor for Blob"
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jul 26, 2024
1 parent 3357fc8 commit d261573
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/angry-coats-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@whatwg-node/node-fetch': patch
---

Support `new Blob()`
2 changes: 1 addition & 1 deletion packages/node-fetch/src/Blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class PonyfillBlob implements Blob {
private encoding: BufferEncoding;
private _size: number | null = null;
constructor(
private blobParts: BlobPart[],
private blobParts: BlobPart[] = [],
options?: BlobOptions,
) {
this.type = options?.type || 'application/octet-stream';
Expand Down
32 changes: 32 additions & 0 deletions packages/node-fetch/tests/Blob.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,36 @@ describe('Blob', () => {
});
});
}
it('together', async () => {
const blob = new PonyfillBlob([
blobParts.string,
blobParts.globalBlob,
blobParts.nodeBlob,
blobParts.arrayBuffer,
]);
const text = await blob.text();
expect(text).toBe('stringglobalBlobnodeBlobarrayBuffer');
const buffer = await blob.arrayBuffer();
expect(Buffer.from(buffer, undefined, buffer.byteLength).toString('utf-8')).toBe(
'stringglobalBlobnodeBlobarrayBuffer',
);
const stream = blob.stream();
const chunks: Buffer[] = [];
for await (const chunk of stream) {
chunks.push(chunk);
}
expect(Buffer.concat(chunks).toString('utf-8')).toBe('stringglobalBlobnodeBlobarrayBuffer');
});
it('empty', async () => {
const blob = new PonyfillBlob();
expect(blob.size).toBe(0);
expect(await blob.text()).toBe('');
expect(Buffer.from(await blob.arrayBuffer()).toString()).toBe('');
const stream = blob.stream();
const chunks: Buffer[] = [];
for await (const chunk of stream) {
chunks.push(chunk);
}
expect(Buffer.concat(chunks).toString());
});
});

0 comments on commit d261573

Please sign in to comment.