Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export class Response {
body == null ||
typeof body === 'string' ||
typeof (body as ReadableStream)?.getReader !== 'undefined' ||
body instanceof Blob ||
// a typed Blob supplies its own default content-type, which only the native Response applies
(body instanceof Blob && !body.type) ||
body instanceof Uint8Array
) {
;(this as any)[cacheKey] = [init?.status || 200, body ?? null, headers || init?.headers]
Expand Down
9 changes: 9 additions & 0 deletions test/listener.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,15 @@ describe('Abort request - cacheable response path', () => {
}
)

it('should use the type of a Blob body as the default Content-Type', async () => {
const { response } = await runRequestAndCollectOutgoingEvents(
() => new Response(new Blob(['blob-body'], { type: 'image/png' }))
)

expect(response.headers.get('content-type')).toBe('image/png')
expect(await response.text()).toBe('blob-body')
})

it('should attach a close listener and send the body for sync Blob responses', async () => {
const { closeListenerCount, response } = await runRequestAndCollectOutgoingEvents(
() =>
Expand Down
12 changes: 12 additions & 0 deletions test/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,18 @@ describe('Response', () => {
})
})

describe('Blob body', () => {
it('Should fall back to GlobalResponse for a typed Blob so its type is the default content-type', () => {
const res = new Response(new Blob(['x'], { type: 'image/png' }))
expect(cacheKey in res).toBe(false)
expect(res.headers.get('content-type')).toBe('image/png')
})

it('Should keep the lightweight fast path for an untyped Blob', () => {
expect(cacheKey in new Response(new Blob(['x']))).toBe(true)
})
})

describe('util.inspect', () => {
it('should show a lightweight response summary before native Response creation', () => {
const { inspect } = require('node:util')
Expand Down