Requests and responses
Large uploads
Read multipart bodies one part at a time with multipart(), without buffering them in memory
import { } from "@senrijs/senri/multipart";
const = ();
.("/upload", async (, ) => {
for await (const of (, { : 100 * 1024 * 1024 })) {
if (. !== ) await (.(), .);
else .(., await .());
}
return "ok";
});It reads multipart/form-data one part at a time. It does not read the whole body into memory, so large files can be passed on to storage (files, object storage and so on) as they arrive.
Parts
| Property | What it is |
|---|---|
name | The form field name |
filename | The file name. undefined for fields that are not files, "" for file fields with no file chosen |
contentType | text/plain if missing |
headers | The part headers |
stream | The content as a stream |
text(), bytes() | Read all the content |
filename and contentType are exactly what was sent. Do not use them in storage paths: choose the name yourself (with crypto.randomUUID() and so on) or pass it through safeFileName().
Limits
Prop
Type
- Over a limit, the response is 413. A broken body or a missing
boundaryis 400, and a body that is not multipart is 415 - On routes that use
multipart(), these limits apply instead of the app's default limit (1 MiB)
Rules for reading
- When the loop moves on, unread parts are skipped. Reading the previous part's
streamafter that is an error, soawaitthe save before moving on - After
break, nothing more is read - Calling it after the body was read (
c.body(),uploadLimit(), a route'sbodyschema) or a second time is an error
To take small files as File and check their types, use uploadLimit(). To stream large files, use multipart(). Choose per route.