Multipart.ts
Multipart.ts overview
Section titled “Multipart.ts overview”Parses and persists HTTP multipart/form-data request bodies.
Multipart turns incoming byte streams into typed form parts. Text parts
become decoded fields, while upload parts stay as streamed files until they
are collected or written to scoped temporary files. The persisted
representation can then be decoded with schemas for handlers that receive
fields and uploaded files together. This module also includes multipart error
types, schema helpers for persisted files, and parser limit settings.
Since v4.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”Parsers
Section titled “Parsers”makeChannel
Section titled “makeChannel”Creates a channel that parses multipart byte chunks into multipart parts.
Details
The channel consumes non-empty batches of Uint8Array chunks and emits
non-empty batches of parsed Part values, failing with MultipartError for
parser and limit failures.
Signature
declare const makeChannel: <IE>( headers: Record<string, string>) => Channel.Channel< Arr.NonEmptyReadonlyArray<Part>, MultipartError | IE, void, Arr.NonEmptyReadonlyArray<Uint8Array>, IE, unknown>Since v4.0.0
configuration
Section titled “configuration”makeConfig
Section titled “makeConfig”Builds the low-level multipart parser configuration from request headers and the current fiber context.
Details
Parser limits are read from the multipart references, including maximum parts, field size, file size, total body size, and field MIME type overrides.
Signature
declare const makeConfig: (headers: Record<string, string>) => Effect.Effect<MP.BaseConfig>Since v4.0.0
converting
Section titled “converting”collectUint8Array
Section titled “collectUint8Array”Runs a channel of byte chunks and collects all output into a single
Uint8Array.
Gotchas
This materializes the full content in memory.
Signature
declare const collectUint8Array: <OE, OD, R>( self: Channel.Channel<Arr.NonEmptyReadonlyArray<Uint8Array>, OE, OD, unknown, unknown, unknown, R>) => Effect.Effect<Uint8Array<ArrayBuffer>, OE, R>Since v4.0.0
toPersisted
Section titled “toPersisted”Persists a stream of multipart parts into a record.
Details
Text fields are collected as strings, and file parts are written to files in a scoped temporary directory.
Gotchas
Persisted file paths remain valid for the lifetime of the scope.
Signature
declare const toPersisted: ( stream: Stream.Stream<Part, MultipartError>, writeFile?: (path: string, file: File) => Effect.Effect<void, MultipartError, FileSystem.FileSystem>) => Effect.Effect<Persisted, MultipartError, FileSystem.FileSystem | Path.Path | Scope.Scope>Since v4.0.0
errors
Section titled “errors”MultipartError (class)
Section titled “MultipartError (class)”Error raised while parsing, streaming, or persisting multipart form data.
Details
The reason field contains the concrete MultipartErrorReason.
Signature
declare class MultipartErrorSince v4.0.0
fromReason (static method)
Section titled “fromReason (static method)”Creates a multipart error from a reason tag and optional cause.
Signature
declare const fromReason: (reason: MultipartErrorReason["_tag"], cause?: unknown) => MultipartErrorSince v4.0.0
[MultipartErrorTypeId] (property)
Section titled “[MultipartErrorTypeId] (property)”Marks this value as a multipart error for runtime guards.
Signature
readonly [MultipartErrorTypeId]: "~effect/http/Multipart/MultipartError"Since v4.0.0
MultipartErrorReason (class)
Section titled “MultipartErrorReason (class)”Error reason carried by a MultipartError.
Details
It identifies parser and limit failures such as oversized files or fields, too many parts, total body size limits, parse errors, and internal errors.
Signature
declare class MultipartErrorReasonSince v4.0.0
guards
Section titled “guards”isField
Section titled “isField”Returns true when a value is a multipart text Field.
Signature
declare const isField: (u: unknown) => u is FieldSince v4.0.0
isFile
Section titled “isFile”Returns true when a value is a multipart File.
Signature
declare const isFile: (u: unknown) => u is FileSince v4.0.0
isPart
Section titled “isPart”Returns true when a value is a multipart Part.
Signature
declare const isPart: (u: unknown) => u is PartSince v4.0.0
isPersistedFile
Section titled “isPersistedFile”Returns true when a value is a persisted multipart file.
Signature
declare const isPersistedFile: (u: unknown) => u is PersistedFileSince v4.0.0
models
Section titled “models”Field (interface)
Section titled “Field (interface)”Multipart form field containing a decoded text value.
Details
The key is the field name, contentType is the part media type, and value
is the decoded field content.
Signature
export interface Field extends Part.Proto { readonly _tag: "Field" readonly key: string readonly contentType: string readonly value: string}Since v4.0.0
File (interface)
Section titled “File (interface)”Multipart file part.
Gotchas
The file content is exposed as a byte stream. contentEffect collects the full
file into memory and should be used only when the file size is acceptable.
Signature
export interface File extends Part.Proto { readonly _tag: "File" readonly key: string readonly name: string readonly contentType: string readonly content: Stream.Stream<Uint8Array, MultipartError> readonly contentEffect: Effect.Effect<Uint8Array, MultipartError>}Since v4.0.0
Part (type alias)
Section titled “Part (type alias)”A parsed multipart part.
Details
A part is either a text Field or a streamed File.
Signature
type Part = Field | FileSince v4.0.0
Persisted (interface)
Section titled “Persisted (interface)”Record representation of persisted multipart data.
Details
Field names map to text values, arrays of text values, or arrays of
PersistedFile values.
Signature
export interface Persisted { readonly [key: string]: ReadonlyArray<PersistedFile> | ReadonlyArray<string> | string}Since v4.0.0
PersistedFile (interface)
Section titled “PersistedFile (interface)”Multipart file part that has been written to the filesystem.
Details
The path points to the persisted file while the scope used to persist the
multipart data remains open.
Signature
export interface PersistedFile extends Part.Proto { readonly _tag: "PersistedFile" readonly key: string readonly name: string readonly contentType: string readonly path: string}Since v4.0.0
references
Section titled “references”FieldMimeTypes
Section titled “FieldMimeTypes”Context reference for MIME type fragments that should be parsed as multipart fields instead of files.
Details
The default treats application/json parts as fields.
Signature
declare const FieldMimeTypes: Context.Reference<ReadonlyArray<string>>Since v4.0.0
MaxFieldSize
Section titled “MaxFieldSize”Context reference for the maximum size of a multipart field value.
Details
The default limit is 10 MiB.
Signature
declare const MaxFieldSize: Context.Reference<FileSystem.SizeInput>Since v4.0.0
MaxFileSize
Section titled “MaxFileSize”Context reference for the maximum size of a multipart file part.
Details
The default is undefined, meaning no explicit per-file limit.
Signature
declare const MaxFileSize: Context.Reference<FileSystem.SizeInput | undefined>Since v4.0.0
MaxParts
Section titled “MaxParts”Context reference for the maximum number of multipart parts allowed.
Details
The default is undefined, meaning no explicit part-count limit.
Signature
declare const MaxParts: Context.Reference<number | undefined>Since v4.0.0
limitsServices
Section titled “limitsServices”Creates a context containing multipart parser limit settings.
Details
The context can provide maximum part count, field size, file size, total body size, and MIME types that should be parsed as fields.
Signature
declare const limitsServices: (options: { readonly maxParts?: number | undefined readonly maxFieldSize?: FileSystem.SizeInput | undefined readonly maxFileSize?: FileSystem.SizeInput | undefined readonly maxTotalSize?: FileSystem.SizeInput | undefined readonly fieldMimeTypes?: ReadonlyArray<string> | undefined}) => Context.Context<never>Since v4.0.0
schemas
Section titled “schemas”FilesSchema
Section titled “FilesSchema”Schema for an array of persisted multipart files.
Signature
declare const FilesSchema: Schema.$Array<PersistedFileSchema>Since v4.0.0
PersistedFileSchema
Section titled “PersistedFileSchema”Schema for persisted multipart files.
Details
The encoded form contains the field key, original file name, content type, and filesystem path.
Signature
declare const PersistedFileSchema: PersistedFileSchemaSince v4.0.0
PersistedFileSchema (interface)
Section titled “PersistedFileSchema (interface)”Schema type for persisted multipart files.
Signature
export interface PersistedFileSchema extends Schema.declare<PersistedFile> {}Since v4.0.0
SingleFileSchema
Section titled “SingleFileSchema”Schema for exactly one persisted multipart file.
Details
The encoded form is a one-element file array, while the decoded value is the
single PersistedFile.
Signature
declare const SingleFileSchema: Schema.decodeTo<PersistedFileSchema, Schema.$Array<PersistedFileSchema>, never, never>Since v4.0.0
schemaJson
Section titled “schemaJson”Creates a decoder for a JSON-encoded field in persisted multipart data.
Details
The selected field is parsed from a JSON string and decoded with the supplied schema.
Signature
declare const schemaJson: <A, I, RD, RE>( schema: Schema.Codec<A, I, RD, RE>, options?: ParseOptions | undefined) => { (field: string): (persisted: Persisted) => Effect.Effect<A, Schema.SchemaError, RD> (persisted: Persisted, field: string): Effect.Effect<A, Schema.SchemaError, RD>}Since v4.0.0
schemaPersisted
Section titled “schemaPersisted”Creates a decoder for persisted multipart data using the supplied schema.
Details
The returned function decodes an unknown input into the schema output and fails
with SchemaError when validation fails.
Signature
declare const schemaPersisted: <A, I extends Partial<Persisted>, RD, RE>( schema: Schema.Codec<A, I, RD, RE>) => (input: unknown, options?: ParseOptions) => Effect.Effect<A, Schema.SchemaError, RD>Since v4.0.0
type IDs
Section titled “type IDs”TypeId
Section titled “TypeId”Type identifier used to brand multipart part values.
Signature
declare const TypeId: "~effect/http/Multipart"Since v4.0.0
Part (namespace)
Section titled “Part (namespace)”Namespace containing shared multipart part model types.
Since v4.0.0
Proto (interface)
Section titled “Proto (interface)”Common protocol implemented by multipart part values.
Details
It provides the multipart type identifier, tag, and inspectable behavior shared by fields, files, and persisted files.
Signature
export interface Proto extends Inspectable.Inspectable { readonly [TypeId]: typeof TypeId readonly _tag: string}Since v4.0.0
withLimits (namespace)
Section titled “withLimits (namespace)”Namespace containing multipart parser limit option types.
Since v4.0.0
Options (type alias)
Section titled “Options (type alias)”Options for overriding multipart parser limits.
Details
These settings control maximum part count, field size, file size, total body size, and MIME types that should be treated as fields instead of files.
Signature
type Options = { readonly maxParts?: number | undefined readonly maxFieldSize?: FileSystem.SizeInput | undefined readonly maxFileSize?: FileSystem.SizeInput | undefined readonly maxTotalSize?: FileSystem.SizeInput | undefined readonly fieldMimeTypes?: ReadonlyArray<string> | undefined}Since v4.0.0