multipartkit

CI Gitleaks Trivy OSV-Scanner Hex Hex Docs Hex Downloads License OpenSSF Scorecard

Parse and build multipart bodies, such as multipart/form-data file uploads, in Gleam on Erlang and JavaScript.

parse turns a request body and its Content-Type into parts that field and file look up by name, and encode_form writes the parts that new_field and new_file build. Parsing stops at limits on body size, part size, part count and header size. multipartkit/stream reads a body a chunk at a time when it is too large to hold in memory. The package is pure Gleam and needs gleam_yielder and mimetype besides the standard library.

gleam add multipartkit@0

Usage

This is examples/quick_start, which CI runs.

import gleam/bit_array
import gleam/int
import gleam/io
import gleam/option
import multipartkit

pub fn main() {
  let assert Ok(title) = multipartkit.new_field(name: "title", value: "hello")
  let assert Ok(avatar) =
    multipartkit.new_file(
      name: "avatar",
      filename: "cat.png",
      content_type: "image/png",
      body: <<137, 80, 78, 71>>,
    )
  let #(content_type, body) = multipartkit.encode_form([title, avatar])

  let assert Ok(parts) =
    multipartkit.parse(body, content_type, multipartkit.default_limits)
  let assert Ok(title) = multipartkit.field(parts, "title")
  let assert Ok(avatar) = multipartkit.file(parts, "avatar")

  io.println("Content-Type: " <> content_type)
  io.println("title=" <> title)
  io.println(
    "avatar filename=" <> option.unwrap(multipartkit.filename(avatar), ""),
  )
  let size = bit_array.byte_size(multipartkit.body(avatar))
  io.println("avatar size=" <> int.to_string(size))
}
Content-Type: multipart/form-data; boundary=----multipartkit-y9o53iydy0zm2dxbmbhvbsi9
title=hello
avatar filename=cat.png
avatar size=4

In a request handler, chain the lookups with result.try and use tighter limits than default_limits on a public endpoint:

pub fn read_upload(body: BitArray, content_type: String) {
  let limits = multipartkit.Limits(..multipartkit.default_limits, max_parts: 10)
  use parts <- result.try(multipartkit.parse(body, content_type, limits))
  use title <- result.try(multipartkit.field(parts, "title"))
  use avatar <- result.try(multipartkit.file(parts, "avatar"))
  use avatar <- result.try(
    multipartkit.allowed_content_types(avatar, ["image/png", "image/jpeg"]),
  )
  Ok(#(title, avatar))
}

multipartkit.infer_content_type(filename, bytes) picks a content type for new_file from the file name or its first bytes. stream.parse takes a Yielder(BitArray) of chunks and hands out each part body as chunks of up to 64 KiB; see examples/streaming_parse.

The API reference is on HexDocs.

Notes

How the package is tested is described in doc/testing.md.

Verifying releases

Each GitHub Release attaches the Hex package tarball (multipartkit-<version>.tar), checksums.txt, a cosign signature bundle (checksums.txt.sigstore.json) and build provenance (multipartkit-<version>.intoto.jsonl). Download the assets, then:

gh attestation verify multipartkit-<version>.tar --repo nao1215/multipartkit

cosign verify-blob \
  --bundle checksums.txt.sigstore.json \
  --certificate-identity-regexp '^https://github\.com/nao1215/multipartkit/\.github/workflows/release\.yml@refs/tags/v' \
  --certificate-oidc-issuer https://token.actions.githubusercontent.com \
  checksums.txt
sha256sum --check checksums.txt

License

MIT