Registry Mirror
Skip to content

File tree

‎packages/server/src/plugins/request-compression.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,56 @@ describe('requestCompressionHandlerPlugin', () => {
207207
expect(procedureHandler).toHaveBeenCalledWith(expect.any(Object), '__MOCKED__')
208208
})
209209

210+
const binaryContentTypes = ['text/csv', 'application/octet-stream', undefined]
211+
212+
it.each(binaryContentTypes)(
213+
'should decode a binary body as a file, content-type: %s',
214+
async (contentType) => {
215+
const body = compress('a,b\n1,2\n', 'gzip')
216+
217+
const { response } = await handler.handle(
218+
new Request('http://localhost', {
219+
method: 'POST',
220+
headers: {
221+
...(contentType && { 'content-type': contentType }),
222+
'content-encoding': 'gzip',
223+
'content-length': body.length.toString(),
224+
},
225+
body,
226+
}),
227+
)
228+
229+
expect(response?.status).toBe(200)
230+
const input = procedureHandler.mock.calls[0]![0].input
231+
expect(input).toBeInstanceOf(File)
232+
expect(await input.text()).toBe('a,b\n1,2\n')
233+
},
234+
)
235+
236+
it.each(binaryContentTypes)(
237+
'should decode a binary body as a file with Node.js adapter, content-type: %s',
238+
async (contentType) => {
239+
const nodeHandler = new NodeRPCHandler(procedure, {
240+
plugins: [new RequestCompressionHandlerPlugin()],
241+
})
242+
243+
const request = supertest((req: any, res: any) => {
244+
void nodeHandler.handle(req, res)
245+
})
246+
.post('/')
247+
.set('content-encoding', 'gzip')
248+
249+
const response = await (contentType ? request.set('content-type', contentType) : request)
250+
.send(compress('a,b\n1,2\n', 'gzip'))
251+
.buffer(true)
252+
253+
expect(response.status).toBe(200)
254+
const input = procedureHandler.mock.calls[0]![0].input
255+
expect(input).toBeInstanceOf(File)
256+
expect(await input.text()).toBe('a,b\n1,2\n')
257+
},
258+
)
259+
210260
it('should work with Node.js adapter', async () => {
211261
const nodeHandler = new NodeRPCHandler(procedure, {
212262
plugins: [new RequestCompressionHandlerPlugin()],

‎packages/server/src/plugins/request-compression.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ORPCError } from '@orpc/client'
22
import { isCompressionEncoding, parseContentEncodings, toArray } from '@orpc/shared'
3-
import { flattenStandardHeader } from '@standard-server/core'
3+
import { flattenStandardHeader, resolveStandardBodyHint } from '@standard-server/core'
44
import { toFetchHeaders, toStandardBody } from '@standard-server/fetch'
55

66
import type {
@@ -82,7 +82,9 @@ export class RequestCompressionHandlerPlugin<
8282
headers: toFetchHeaders(decompressedHeaders),
8383
})
8484

85-
return toStandardBody(response, { hint })
85+
return toStandardBody(response, {
86+
hint: hint ?? resolveStandardBodyHint(interceptorOptions.request.headers),
87+
})
8688
},
8789
},
8890
})

0 commit comments

Comments
 (0)