Registry Mirror
Skip to content

File tree

‎packages/node/src/tmp-file-upload-handler-plugin.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import type { IncomingMessage, ServerResponse } from 'node:http'
44
import { createServer } from 'node:http'
55
import { tmpdir } from 'node:os'
66
import path from 'node:path'
7+
import { gzipSync } from 'node:zlib'
78

89
import { RPCSerializer } from '@orpc/client'
910
import type { Context } from '@orpc/server'
1011
import { ORPCError, os } from '@orpc/server'
1112
import { RPCHandler } from '@orpc/server/node'
13+
import { RequestCompressionHandlerPlugin } from '@orpc/server/plugins'
1214
import type { StandardBodyHint, StandardLazyRequest } from '@standard-server/core'
1315
import { generateContentDisposition } from '@standard-server/core'
1416
import request from 'supertest'
@@ -599,7 +601,10 @@ describe('tmpFileUploadHandlerPlugin', () => {
599601
method: 'POST',
600602
url: '/upload',
601603
headers: {},
602-
resolveBody: async () => resolved as never,
604+
resolveBody: async (hint) => {
605+
expect(hint).toBe('none')
606+
return resolved as never
607+
},
603608
},
604609
next: async (nextOptions) => {
605610
// A bodiless request resolves to the none hint and stays with the adapter
@@ -1197,6 +1202,30 @@ describe('tmpFileUploadHandlerPlugin', () => {
11971202
expect(readdirSync(tmpDir)).toHaveLength(0)
11981203
})
11991204

1205+
it('leaves compressed bodies it resolves as none unread', async () => {
1206+
const procedure = vi.fn()
1207+
const handler = new RPCHandler(os.handler(procedure), {
1208+
plugins: [
1209+
new TmpFileUploadHandlerPlugin({
1210+
tmpDir,
1211+
maxBodySize: { memory: 1024, file: 1024, stream: 1024 },
1212+
}),
1213+
new RequestCompressionHandlerPlugin(),
1214+
],
1215+
})
1216+
1217+
const res = await request((req: IncomingMessage, res: ServerResponse) => {
1218+
void handler.handle(req, res)
1219+
})
1220+
.post('/')
1221+
.set('content-encoding', 'gzip')
1222+
.send(gzipSync(Buffer.alloc(64 * 1024, 'a')))
1223+
1224+
expect(res.status).toBe(200)
1225+
expect(procedure.mock.calls[0]![0].input).toBeUndefined()
1226+
expect(readdirSync(tmpDir)).toHaveLength(0)
1227+
})
1228+
12001229
it('limits multipart fields and files against their own categories', async () => {
12011230
const { body, headerSizes, contentSizes } = createMultipartBody([
12021231
{ header: 'Content-Disposition: form-data; name="first"', content: 'x'.repeat(6) },

‎packages/node/src/tmp-file-upload-handler-plugin.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ export class TmpFileUploadHandlerPlugin<T extends Context> implements StandardHa
262262

263263
// A body-less request has nothing to limit or spool
264264
if (resolvedHint === 'none') {
265-
return request.resolveBody(hint)
265+
return request.resolveBody('none')
266266
}
267267

268268
const maxBodySize = await value(this.maxBodySize, interceptorOptions)

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,27 @@ describe('requestCompressionHandlerPlugin', () => {
144144
expect(procedureHandler).not.toHaveBeenCalled()
145145
})
146146

147+
it('should leave the body unread for a none hint', async () => {
148+
const request = new Request('http://localhost', {
149+
method: 'POST',
150+
headers: { 'content-encoding': 'gzip' },
151+
body: compress('input', 'gzip'),
152+
})
153+
const handler = new RPCHandler(procedure, {
154+
plugins: [new RequestCompressionHandlerPlugin()],
155+
routingInterceptors: [
156+
async ({ request }) => {
157+
expect(await request.resolveBody('none')).toBeUndefined()
158+
return { matched: false }
159+
},
160+
],
161+
})
162+
163+
await handler.handle(request)
164+
165+
expect(request.body!.locked).toBe(false)
166+
})
167+
147168
it('should not decompress when any content-encoding in the list is unsupported', async () => {
148169
const payload = JSON.stringify({ json: 'input' })
149170
const body = compress(payload, 'gzip')

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ export class RequestCompressionHandlerPlugin<
5858
...interceptorOptions.request,
5959
headers: decompressedHeaders,
6060
async resolveBody(hint) {
61-
const stream = await interceptorOptions.request.resolveBody('octet-stream')
61+
const stream = await interceptorOptions.request.resolveBody(
62+
hint === 'none' ? 'none' : 'octet-stream',
63+
)
6264

6365
// adapter might not support hint (e.g peer adapter)
6466
if (!(stream instanceof ReadableStream)) {

0 commit comments

Comments
 (0)