|
| 1 | +--- |
| 2 | +title: "Tmp File Upload Plugin" |
| 3 | +description: "Stream large file uploads into temporary files instead of memory, so requests far larger than available memory are parsed safely." |
| 4 | +sidebar: |
| 5 | + label: "Tmp File Upload" |
| 6 | +--- |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +```package-install |
| 11 | +npm install @orpc/node@beta |
| 12 | +``` |
| 13 | + |
| 14 | +## Setup |
| 15 | + |
| 16 | +Use `TmpFileUploadHandlerPlugin` to parse file uploads into temporary files. Bodies the standard parser would buffer into an in-memory [File](https://developer.mozilla.org/en-US/docs/Web/API/File), and [`multipart/form-data`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types#multipartform-data) file parts, stream to disk instead. Every other body is left to the standard parser. |
| 17 | + |
| 18 | +```ts |
| 19 | +import { TmpFileUploadHandlerPlugin } from '@orpc/node' |
| 20 | +import { RPCHandler } from '@orpc/server/node' |
| 21 | + |
| 22 | +const handler = new RPCHandler(router, { |
| 23 | + plugins: [ |
| 24 | + new TmpFileUploadHandlerPlugin({ |
| 25 | + /** |
| 26 | + * The directory temporary files are created under. Each request that |
| 27 | + * spools an upload gets its own subdirectory inside it, removed when |
| 28 | + * the request finishes. |
| 29 | + * |
| 30 | + * @default os.tmpdir() |
| 31 | + */ |
| 32 | + tmpDir: './uploads', |
| 33 | + }), |
| 34 | + ], |
| 35 | +}) |
| 36 | +``` |
| 37 | + |
| 38 | +:::info |
| 39 | +The `handler` can be any supported oRPC handler, such as [RPCHandler](/docs/rpc/handler), [OpenAPIHandler](/docs/openapi/handler), or a custom one. |
| 40 | +::: |
| 41 | + |
| 42 | +## Working with Uploaded Files |
| 43 | + |
| 44 | +Procedures receive ordinary `File` instances and read them lazily from disk, in constant memory. Each one is a `TmpFile` exposing the `path` of its backing file, so an upload can be kept with a cheap [rename](https://nodejs.org/api/fs.html#fspromisesrenameoldpath-newpath) instead of a copy: |
| 45 | + |
| 46 | +```ts |
| 47 | +import { TmpFile } from '@orpc/node' |
| 48 | +import { rename } from 'node:fs/promises' |
| 49 | + |
| 50 | +const uploadVideo = os |
| 51 | + .input(z.object({ video: z.file() })) |
| 52 | + .handler(async ({ input }) => { |
| 53 | + if (input.video instanceof TmpFile) { |
| 54 | + await rename(input.video.path, `./videos/${crypto.randomUUID()}`) |
| 55 | + } |
| 56 | + }) |
| 57 | +``` |
| 58 | + |
| 59 | +:::warning |
| 60 | +Temporary files are removed when the request finishes. A streaming response body, an event iterator or a raw stream, keeps them alive until it completes. Any other response is transmitted after removal, so a response that embeds the upload itself, as a `File` or inside `FormData`, needs the content copied or the file moved first. A moved or removed file can no longer be read through its `File` instance. |
| 61 | +::: |
| 62 | + |
| 63 | +## Limiting Body Sizes |
| 64 | + |
| 65 | +Use `maxBodySize` to limit each kind of request body by what it actually costs. All three kinds are required together, so none is left unbounded by accident; set a kind to `Number.POSITIVE_INFINITY` to deliberately leave it unlimited. A body over its limit rejects with `PAYLOAD_TOO_LARGE`. |
| 66 | + |
| 67 | +```ts |
| 68 | +const handler = new RPCHandler(router, { |
| 69 | + plugins: [ |
| 70 | + new TmpFileUploadHandlerPlugin({ |
| 71 | + maxBodySize: { |
| 72 | + /** |
| 73 | + * Content parsed into memory: JSON, URL-encoded forms, and the |
| 74 | + * plain fields of a multipart body. Usually the lowest limit. |
| 75 | + */ |
| 76 | + memory: 1024 * 1024, |
| 77 | + |
| 78 | + /** |
| 79 | + * Content streamed into temporary files: file bodies and the |
| 80 | + * file parts of a multipart body combined. |
| 81 | + */ |
| 82 | + file: 10 * 1024 * 1024 * 1024, |
| 83 | + |
| 84 | + /** |
| 85 | + * Content consumed as a stream: event streams and raw binary |
| 86 | + * streams, enforced while the stream is consumed. Usually the |
| 87 | + * highest limit. |
| 88 | + */ |
| 89 | + stream: Number.POSITIVE_INFINITY, |
| 90 | + }, |
| 91 | + }), |
| 92 | + ], |
| 93 | +}) |
| 94 | +``` |
| 95 | + |
| 96 | +A multipart body splits across the first two limits, fields against `memory` and file parts against `file`, and as a whole, framing included, it is bounded by the sum of both. A declared [Content-Length](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Length) over the applicable limit rejects immediately, and enforcement continues while the body streams in, so a lying length cannot bypass it. |
| 97 | + |
| 98 | +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. |
| 99 | + |
| 100 | +## Learn More |
| 101 | + |
| 102 | +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