Skip to content
Merged
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
1 change: 1 addition & 0 deletions packages/node/src/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export function toNodeHttpBody(

if (body instanceof FormData) {
const response = new Response(body)
// Read before the body: Bun drops the header once a body containing a File is consumed
headers['content-type'] = response.headers.get('content-type')!
return [Readable.fromWeb(response.body!), headers]
}
Expand Down
42 changes: 42 additions & 0 deletions packages/peer/src/body.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,48 @@ describe('encodeAtomicStandardBody', () => {
expect(binary).toBeInstanceOf(Blob)
})

it('keeps the case of a mixed-case FormData boundary', async () => {
const boundary = '----WebKitFormBoundaryQLr0YLsTkVq7MA89'
const OriginalResponse = Response
vi.stubGlobal(
'Response',
class extends OriginalResponse {
constructor(...args: ConstructorParameters<typeof Response>) {
if (args[0] instanceof FormData) {
super(
`--${boundary}\r\nContent-Disposition: form-data; name="key"\r\n\r\nvalue\r\n--${boundary}--\r\n`,
{ headers: { 'content-type': `multipart/form-data; boundary=${boundary}` } },
)
} else {
super(...args)
}
}
},
)
onTestFinished(() => {
vi.unstubAllGlobals()
})

const form = new FormData()
form.append('key', 'value')
const encoded = await encodeAtomicStandardBody(form, {})

expect(encoded.headers['content-type']).toBe(`multipart/form-data; boundary=${boundary}`)

const { resolveBody } = toStandardBody(
{
id: '1',
kind: 'request',
json: { url: '/upload', headers: encoded.headers, body: encoded.jsonBody },
binary: encoded.binary,
},
vi.fn(),
)

const received = (await resolveBody()) as FormData
expect(received.get('key')).toBe('value')
})

it('encodes Blob body', async () => {
const blob = new Blob(['data'], { type: 'text/plain' })
const { jsonBody, headers, binary } = await encodeAtomicStandardBody(blob, {})
Expand Down
3 changes: 2 additions & 1 deletion packages/peer/src/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,12 @@ export async function encodeAtomicStandardBody(

if (body instanceof FormData) {
const res = new Response(body)
// Read before the body: Bun drops the header once a body containing a File is consumed
headers['content-type'] = res.headers.get('content-type')!
const blob = await res.blob()

// standard-server used to distinguish file vs. form-data
headers['standard-server'] = 'form-data' satisfies StandardBodyHint
headers['content-type'] = blob.type
headers['content-length'] = blob.size.toString()

return { jsonBody: undefined, headers, binary: blob }
Expand Down
Loading