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
8 changes: 0 additions & 8 deletions packages/standard-server-fetch/src/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ import { generateContentDisposition, getFilenameFromContentDisposition } from '@
import { toEventIterator, toEventStream } from './event-iterator'

export async function toStandardBody(re: Request | Response): Promise<StandardBody> {
/**
* In native environments like React Native, the body may be `undefined` due to lack of streaming support.
* Therefore, we explicitly check for `null` to indicate an intentionally empty body.
*/
if (re.body === null) {
return undefined
}

const contentDisposition = re.headers.get('content-disposition')

if (typeof contentDisposition === 'string') {
Expand Down
7 changes: 7 additions & 0 deletions packages/standard-server-fetch/src/event-iterator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ describe('toEventIterator', () => {
await expect(stream.getReader().closed).resolves.toBe(undefined)
})

it('with empty stream', async () => {
const generator = toEventIterator(null)
expect(generator).toSatisfy(isAsyncIteratorObject)
expect(await generator.next()).toEqual({ done: true, value: undefined })
expect(await generator.next()).toEqual({ done: true, value: undefined })
})

it('with error event', async () => {
const stream = new ReadableStream<string>({
async pull(controller) {
Expand Down
12 changes: 8 additions & 4 deletions packages/standard-server-fetch/src/event-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ import {
} from '@orpc/standard-server'

export function toEventIterator(
stream: ReadableStream<Uint8Array>,
stream: ReadableStream<Uint8Array> | null,
): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void> {
const eventStream = stream
.pipeThrough(new TextDecoderStream())
?.pipeThrough(new TextDecoderStream())
.pipeThrough(new EventDecoderStream())

const reader = eventStream.getReader()
const reader = eventStream?.getReader()

return createAsyncIteratorObject(async () => {
while (true) {
if (reader === undefined) {
return { done: true, value: undefined }
}

const { done, value } = await reader.read()

if (done) {
Expand Down Expand Up @@ -57,7 +61,7 @@ export function toEventIterator(
}
}
}, async () => {
await reader.cancel()
await reader?.cancel()
})
}

Expand Down
6 changes: 0 additions & 6 deletions packages/standard-server-node/src/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import { flattenHeader, generateContentDisposition, getFilenameFromContentDispos
import { toEventIterator, toEventStream } from './event-iterator'

export async function toStandardBody(req: NodeHttpRequest): Promise<StandardBody> {
const method = req.method ?? 'GET'

if (method === 'GET' || method === 'HEAD') {
return undefined
}

const contentDisposition = req.headers['content-disposition']
const contentType = req.headers['content-type']

Expand Down