You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(node): resolve tmp file upload body limits per request (#1881)
`TmpFileUploadHandlerPlugin`'s `maxBodySize` now accepts a function,
sync or async, alongside the fixed object it already took. The function
receives the routing interceptor options (`context`, `request`,
`prefix`), so an upload allowance can follow the request that carries
it, such as a larger file limit for an authenticated user than for a
guest.
## Behavior
- A fixed `maxBodySize` object behaves exactly as before, and remains
the default when the option is omitted.
- The resolver runs only when a body is actually parsed, so a request
that carries no body never pays for the lookup.
- It receives the request as it arrived, before the plugin wraps body
resolution.
- All three limits now come from one resolution per parsed body, so a
multipart body enforces `memory`, `file`, and their combined total from
a single consistent snapshot.
## Types
`TmpFileUploadHandlerPluginOptions` is now generic in the handler
context, matching `BatchHandlerPluginOptions`. Existing call sites that
pass a fixed object keep compiling untouched; `new
TmpFileUploadHandlerPlugin<AppContext>({ ... })` types `context` inside
the resolver.
## Testing
Three cases cover context-driven limits rejecting a guest and admitting
an authenticated user for memory-parsed bodies and for spooled files,
with temporary files still cleaned up on rejection, plus a case
asserting the resolver is skipped when no body is parsed. 145 tests pass
across `packages/node`; root type check and lint are clean.
Copy file name to clipboardExpand all lines: apps/content/docs/plugins/tmp-file-upload.mdx
+20Lines changed: 20 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -101,6 +101,26 @@ A multipart body splits across the first two limits, fields against `memory` and
101
101
102
102
With all three limits configured, the plugin subsumes the [Request Limit Plugin](/docs/plugins/request-limit). When the [Request Compression Plugin](/docs/plugins/request-compression) is present, limits apply to the decompressed payload rather than the compressed wire size.
103
103
104
+
## Per-Request Limits
105
+
106
+
`maxBodySize` also accepts a function, sync or async, receiving the handler `context`, `request`, and `prefix`, so limits can follow who is uploading:
107
+
108
+
```ts
109
+
const handler =newRPCHandler(router, {
110
+
plugins: [
111
+
newTmpFileUploadHandlerPlugin({
112
+
maxBodySize: async ({ context }) => ({
113
+
memory: 1024*1024, // 1MB
114
+
file: context.user===undefined
115
+
?10*1024*1024// 10MB for guests
116
+
:2*1024*1024*1024, // 2GB for authenticated users
117
+
stream: Number.POSITIVE_INFINITY,
118
+
}),
119
+
}),
120
+
],
121
+
})
122
+
```
123
+
104
124
## Learn More
105
125
106
126
For implementation details, see the [source code](https://github.com/middleapi/orpc/blob/main/packages/node/src/tmp-file-upload-handler-plugin.ts).
0 commit comments