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
28 changes: 27 additions & 1 deletion packages/standard-server-fastify/src/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import request from 'supertest'
import { sendStandardResponse } from './response'

const toNodeHttpBodySpy = vi.spyOn(StandardServerNode, 'toNodeHttpBody')
const toNodeHttpHeadersSpy = vi.spyOn(StandardServerNode, 'toNodeHttpHeaders')

beforeEach(() => {
vi.clearAllMocks()
Expand Down Expand Up @@ -39,11 +40,16 @@ describe('sendStandardResponse', () => {
'x-custom-header': 'custom-value',
}, options)

expect(toNodeHttpHeadersSpy).toBeCalledTimes(1)
expect(toNodeHttpHeadersSpy).toBeCalledWith({
'x-custom-header': 'custom-value',
})

expect(sendSpy).toBeCalledTimes(1)
expect(sendSpy).toBeCalledWith(undefined)

expect(res.status).toBe(207)
expect(res.headers['content-type']).toEqual(undefined)
expect(res.headers).not.toHaveProperty('content-type')
expect(res.headers['x-custom-header']).toEqual('custom-value')

expect(res.text).toEqual('')
Expand Down Expand Up @@ -77,6 +83,12 @@ describe('sendStandardResponse', () => {
'x-custom-header': 'custom-value',
}, options)

expect(toNodeHttpHeadersSpy).toBeCalledTimes(1)
expect(toNodeHttpHeadersSpy).toBeCalledWith({
'content-type': 'application/json',
'x-custom-header': 'custom-value',
})

expect(sendSpy).toBeCalledTimes(1)
expect(sendSpy).toBeCalledWith(toNodeHttpBodySpy.mock.results[0]!.value)

Expand Down Expand Up @@ -120,6 +132,14 @@ describe('sendStandardResponse', () => {
'x-custom-header': 'custom-value',
}, options)

expect(toNodeHttpHeadersSpy).toBeCalledTimes(1)
expect(toNodeHttpHeadersSpy).toBeCalledWith({
'content-disposition': 'inline; filename="blob"; filename*=utf-8\'\'blob',
'content-length': '3',
'content-type': 'text/plain',
'x-custom-header': 'custom-value',
})

expect(sendSpy).toBeCalledTimes(1)
expect(sendSpy).toBeCalledWith(toNodeHttpBodySpy.mock.results[0]!.value)

Expand Down Expand Up @@ -170,6 +190,12 @@ describe('sendStandardResponse', () => {
'x-custom-header': 'custom-value',
}, options)

expect(toNodeHttpHeadersSpy).toBeCalledTimes(1)
expect(toNodeHttpHeadersSpy).toBeCalledWith({
'content-type': 'text/event-stream',
'x-custom-header': 'custom-value',
})

expect(sendSpy).toBeCalledTimes(1)
expect(sendSpy).toBeCalledWith(toNodeHttpBodySpy.mock.results[0]!.value)

Expand Down
6 changes: 4 additions & 2 deletions packages/standard-server-fastify/src/response.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { StandardHeaders, StandardResponse } from '@orpc/standard-server'
import type { ToNodeHttpBodyOptions } from '@orpc/standard-server-node'
import type { FastifyReply } from 'fastify'
import { toNodeHttpBody } from '@orpc/standard-server-node'
import { toNodeHttpBody, toNodeHttpHeaders } from '@orpc/standard-server-node'

export interface SendStandardResponseOptions extends ToNodeHttpBodyOptions { }

Expand All @@ -19,7 +19,9 @@ export function sendStandardResponse(
const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options)

reply.status(standardResponse.status)
reply.headers(resHeaders)
// Fastify treats undefined headers as empty string, so remember to use toNodeHttpHeaders
// to filter out undefined headers
reply.headers(toNodeHttpHeaders(resHeaders))
reply.send(resBody)
})
}
17 changes: 17 additions & 0 deletions packages/standard-server-node/src/headers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { toNodeHttpHeaders } from './headers'

describe('toNodeHttpHeaders', () => {
it('filters out undefined values', () => {
const headers = toNodeHttpHeaders({
'x-custom': 'value',
'x-undefined': undefined,
'set-cookie': ['cookie1=value1', 'cookie2=value2'],
})

expect(headers).toEqual({
'x-custom': 'value',
'set-cookie': ['cookie1=value1', 'cookie2=value2'],
})
expect(headers).not.toHaveProperty('x-undefined')
})
})
15 changes: 15 additions & 0 deletions packages/standard-server-node/src/headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { StandardHeaders } from '@orpc/standard-server'
import type { OutgoingHttpHeaders } from 'node:http'

export function toNodeHttpHeaders(headers: StandardHeaders): OutgoingHttpHeaders {
const nodeHttpHeaders: OutgoingHttpHeaders = {}

for (const [key, value] of Object.entries(headers)) {
// Node.js does not allow headers to be undefined
if (value !== undefined) {
nodeHttpHeaders[key] = value
}
}

return nodeHttpHeaders
}
1 change: 1 addition & 0 deletions packages/standard-server-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './body'
export * from './event-iterator'
export * from './headers'
export * from './method'
export * from './request'
export * from './response'
Expand Down
29 changes: 28 additions & 1 deletion packages/standard-server-node/src/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { Buffer } from 'node:buffer'
import Stream from 'node:stream'
import request from 'supertest'
import * as Body from './body'
import * as Headers from './headers'
import { sendStandardResponse } from './response'

const toNodeHttpBodySpy = vi.spyOn(Body, 'toNodeHttpBody')
const toNodeHttpHeadersSpy = vi.spyOn(Headers, 'toNodeHttpHeaders')

beforeEach(() => {
vi.clearAllMocks()
Expand Down Expand Up @@ -34,11 +36,16 @@ describe('sendStandardResponse', () => {
'x-custom-header': 'custom-value',
}, options)

expect(toNodeHttpHeadersSpy).toBeCalledTimes(1)
expect(toNodeHttpHeadersSpy).toBeCalledWith({
'x-custom-header': 'custom-value',
})

expect(endSpy).toBeCalledTimes(1)
expect(endSpy).toBeCalledWith()

expect(res.status).toBe(207)
expect(res.headers['content-type']).toEqual(undefined)
expect(res.headers).not.toHaveProperty('content-type')
expect(res.headers['x-custom-header']).toEqual('custom-value')

expect(res.text).toEqual('')
Expand Down Expand Up @@ -66,6 +73,12 @@ describe('sendStandardResponse', () => {
'x-custom-header': 'custom-value',
}, options)

expect(toNodeHttpHeadersSpy).toBeCalledTimes(1)
expect(toNodeHttpHeadersSpy).toBeCalledWith({
'content-type': 'application/json',
'x-custom-header': 'custom-value',
})

expect(endSpy).toBeCalledTimes(1)
expect(endSpy).toBeCalledWith(toNodeHttpBodySpy.mock.results[0]!.value)

Expand Down Expand Up @@ -103,6 +116,14 @@ describe('sendStandardResponse', () => {
'x-custom-header': 'custom-value',
}, options)

expect(toNodeHttpHeadersSpy).toBeCalledTimes(1)
expect(toNodeHttpHeadersSpy).toBeCalledWith({
'content-disposition': 'inline; filename="blob"; filename*=utf-8\'\'blob',
'content-length': '3',
'content-type': 'text/plain',
'x-custom-header': 'custom-value',
})

expect(endSpy).toBeCalledTimes(1)
expect(endSpy).toBeCalledWith()

Expand Down Expand Up @@ -148,6 +169,12 @@ describe('sendStandardResponse', () => {
'x-custom-header': 'custom-value',
}, options)

expect(toNodeHttpHeadersSpy).toBeCalledTimes(1)
expect(toNodeHttpHeadersSpy).toBeCalledWith({
'content-type': 'text/event-stream',
'x-custom-header': 'custom-value',
})

expect(endSpy).toBeCalledTimes(1)
expect(endSpy).toBeCalledWith()

Expand Down
5 changes: 4 additions & 1 deletion packages/standard-server-node/src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { StandardHeaders, StandardResponse } from '@orpc/standard-server'
import type { ToNodeHttpBodyOptions } from './body'
import type { NodeHttpResponse } from './types'
import { toNodeHttpBody } from './body'
import { toNodeHttpHeaders } from './headers'

export interface SendStandardResponseOptions extends ToNodeHttpBodyOptions {}

Expand All @@ -18,7 +19,9 @@ export function sendStandardResponse(

const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options)

res.writeHead(standardResponse.status, resHeaders)
// Node.js throws an error when a header is undefined, so remember to use toNodeHttpHeaders
// to filter out undefined headers
res.writeHead(standardResponse.status, toNodeHttpHeaders(resHeaders))

if (resBody === undefined) {
res.end()
Expand Down
Loading