-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(node-fetch): handle buffers correctly in blobs
- Loading branch information
Showing
3 changed files
with
110 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@whatwg-node/node-fetch': patch | ||
--- | ||
|
||
Handle Buffers correctly in Blobs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Blob as NodeBlob } from 'buffer'; | ||
import { isArrayBuffer, PonyfillBlob } from '../src/Blob'; | ||
|
||
describe('Blob', () => { | ||
const blobParts: Record<string, BlobPart> = { | ||
string: 'string', | ||
globalBlob: new Blob(['globalBlob']), | ||
nodeBlob: new NodeBlob(['nodeBlob']) as Blob, | ||
arrayBuffer: Buffer.from('arrayBuffer'), | ||
}; | ||
for (const [name, blobPart] of Object.entries(blobParts)) { | ||
describe(name, () => { | ||
describe('arrayBuffer', () => { | ||
it('content', async () => { | ||
const blob = new PonyfillBlob([blobPart]); | ||
const buffer = await blob.arrayBuffer(); | ||
expect(isArrayBuffer(buffer)).toBe(true); | ||
expect(Buffer.from(buffer, undefined, buffer.byteLength).toString('utf-8')).toBe(name); | ||
}); | ||
it('size', async () => { | ||
const blob = new PonyfillBlob([blobPart]); | ||
const buffer = await blob.arrayBuffer(); | ||
expect(blob.size).toBe(buffer.byteLength); | ||
}); | ||
}); | ||
describe('text', () => { | ||
it('content', async () => { | ||
const blob = new PonyfillBlob([blobPart]); | ||
const text = await blob.text(); | ||
expect(typeof text).toBe('string'); | ||
expect(text).toBe(name); | ||
}); | ||
it('size', async () => { | ||
const blob = new PonyfillBlob([blobPart]); | ||
const text = await blob.text(); | ||
expect(blob.size).toBe(Buffer.byteLength(text)); | ||
}); | ||
}); | ||
describe('stream', () => { | ||
it('content', async () => { | ||
const blob = new PonyfillBlob([blobPart]); | ||
const stream = blob.stream(); | ||
expect(typeof stream[Symbol.asyncIterator]).toBe('function'); | ||
const chunks: Buffer[] = []; | ||
for await (const chunk of stream) { | ||
chunks.push(chunk); | ||
} | ||
expect(Buffer.concat(chunks).toString('utf-8')).toBe(name); | ||
}); | ||
it('size', async () => { | ||
const blob = new PonyfillBlob([blobPart]); | ||
const stream = blob.stream(); | ||
let size = 0; | ||
for await (const chunk of stream) { | ||
size += chunk.length; | ||
} | ||
expect(blob.size).toBe(size); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); |