Skip to content

Commit cc041e5

Browse files
authored
fix(node): don't crash on Bun when encoding an empty-named File (#98)
Running the node adapter on Bun could crash while sending a file. It happened when a request's `content-disposition: attachment; filename=""` came back out as a response. Bun gives an empty-named `File` a `name` of `undefined`, so `generateContentDisposition` threw before any headers were sent. The fetch and peer adapters already fall back to `''`, and now the node adapter does too. ## Fixes - Empty-named files now encode on Bun instead of throwing, so the node adapter handles them the same way as fetch and peer. ## Testing - Added a regression test that simulates Bun's `undefined` file name. It fails without the fix and passes with it. The node body suite passes (34/34), and lint and type-check are clean. - Note: the Bun runtime suite in `tests/bun` only covers the fetch and peer adapters, which is why this wasn't caught earlier.
1 parent 7b35a15 commit cc041e5

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎packages/node/src/body.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,21 @@ describe('toNodeHttpBody', () => {
547547
expect(generateContentDispositionSpy).toHaveBeenCalledWith('foo.pdf')
548548
})
549549

550+
it('file with undefined name (Bun compatibility)', async () => {
551+
// Bun returns `undefined` for an empty File name
552+
const file = new File(['foo'], '', { type: 'application/pdf' })
553+
Object.defineProperty(file, 'name', { value: undefined })
554+
555+
generateContentDispositionSpy.mockReturnValue('inline; filename="__mocked__"')
556+
557+
const [body, headers] = toNodeHttpBody(file, baseHeaders, {})
558+
559+
expect(body).toBeInstanceOf(Readable)
560+
expect(headers['content-disposition']).toBe('inline; filename="__mocked__"')
561+
expect(generateContentDispositionSpy).toHaveBeenCalledTimes(1)
562+
expect(generateContentDispositionSpy).toHaveBeenCalledWith('')
563+
})
564+
550565
it('event stream', async () => {
551566
async function* gen() {
552567
yield 123

‎packages/node/src/body.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ export function toNodeHttpBody(
115115
headers['standard-server'] ??= 'file' satisfies StandardBodyHint // A File is also a Blob
116116

117117
headers['content-type'] = body.type
118-
headers['content-disposition'] ??= generateContentDisposition(body instanceof File ? body.name : 'blob')
118+
// FIX: Bun returns `undefined` for an empty File name, despite the spec requiring a string
119+
headers['content-disposition'] ??= generateContentDisposition(body instanceof File ? body.name ?? '' : 'blob')
119120

120121
// BunS3 can use NaN for the size
121122
if (Number.isFinite(body.size)) {

0 commit comments

Comments
 (0)