Skip to content

Commit 9430899

Browse files
committed
fix(server): Compression Plugin (fetch) response invalid status
1 parent b953b88 commit 9430899

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

packages/server/src/adapters/fetch/compression-plugin.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ORPCError } from '@orpc/client'
12
import { os } from '../../builder'
23
import { CompressionPlugin } from './compression-plugin'
34
import { RPCHandler } from './rpc-handler'
@@ -617,4 +618,31 @@ describe('compressionPlugin', () => {
617618
expect(text).toContain('event1')
618619
expect(text).toContain('event2')
619620
})
621+
622+
it('can compress error response', async () => {
623+
const error = new ORPCError('UNAUTHORIZED')
624+
625+
const handler = new RPCHandler(os.handler(() => {
626+
throw error
627+
}), {
628+
plugins: [
629+
new CompressionPlugin(),
630+
],
631+
})
632+
633+
const { response } = await handler.handle(new Request('https://example.com/', {
634+
method: 'POST',
635+
headers: {
636+
'content-type': 'application/json',
637+
'accept-encoding': 'gzip',
638+
},
639+
body: JSON.stringify({}),
640+
}))
641+
642+
const decompressed = response?.body?.pipeThrough(new DecompressionStream('gzip'))
643+
const text = await new Response(decompressed).text()
644+
expect(text).toContain(error.code)
645+
expect(response?.headers.get('content-encoding')).toBe('gzip')
646+
expect(response?.status).toBe(error.status)
647+
})
620648
})

packages/server/src/adapters/fetch/compression-plugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ export class CompressionPlugin<T extends Context> implements FetchHandlerPlugin<
119119
return {
120120
...result,
121121
response: new Response(compressedBody, {
122-
...response,
122+
status: response.status,
123+
statusText: response.statusText,
123124
headers: compressedHeaders,
124125
}),
125126
}

0 commit comments

Comments
 (0)