From d26157339f3896509e45cb5c73aa61164d262d56 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Fri, 26 Jul 2024 17:49:40 +0300 Subject: [PATCH] fix(node-fetch): support empty ctor for `Blob`" --- .changeset/angry-coats-promise.md | 5 ++++ packages/node-fetch/src/Blob.ts | 2 +- packages/node-fetch/tests/Blob.spec.ts | 32 ++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 .changeset/angry-coats-promise.md diff --git a/.changeset/angry-coats-promise.md b/.changeset/angry-coats-promise.md new file mode 100644 index 00000000000..6cb2d6e8ecb --- /dev/null +++ b/.changeset/angry-coats-promise.md @@ -0,0 +1,5 @@ +--- +'@whatwg-node/node-fetch': patch +--- + +Support `new Blob()` diff --git a/packages/node-fetch/src/Blob.ts b/packages/node-fetch/src/Blob.ts index b96743dc1ef..6e19f857e24 100644 --- a/packages/node-fetch/src/Blob.ts +++ b/packages/node-fetch/src/Blob.ts @@ -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'; diff --git a/packages/node-fetch/tests/Blob.spec.ts b/packages/node-fetch/tests/Blob.spec.ts index f47d77ff53d..b19d20824ab 100644 --- a/packages/node-fetch/tests/Blob.spec.ts +++ b/packages/node-fetch/tests/Blob.spec.ts @@ -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()); + }); });