Skip to Content
Senri
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

PropertyWhat it is
nameThe form field name
filenameThe file name. undefined for fields that are not files, "" for file fields with no file chosen
contentTypetext/plain if missing
headersThe part headers
streamThe 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 boundary is 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 stream after that is an error, so await the save before moving on
  • After break, nothing more is read
  • Calling it after the body was read (c.body(), uploadLimit(), a route's body schema) 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.

On this page