Skip to content
TypeScript
1.7 KB
Raw Blame
/**
 * Web-standard helpers for parsing HTTP `multipart/form-data` request bodies.
 *
 * This module mirrors `BunMultipart`, adapting a web `Request` body and headers
 * into the shared `Multipart` model. `stream` returns multipart parts as a
 * `Stream`, while `persisted` collects the form and writes file parts to scoped
 * temporary files through the current `FileSystem`, `Path`, and `Scope`
 * services.
 *
 * @stability unstable
 * @since 4.0.0
 */
import type * as Effect from "effect/Effect"
import type { FileSystem } from "effect/FileSystem"
import * as Multipart from "effect/http/Multipart"
import type { Path } from "effect/Path"
import type * as Scope from "effect/Scope"
import * as Stream from "effect/Stream"

/**
 * Parses a web `Request` body as multipart data and returns a stream of multipart parts.
 *
 * @stability unstable
 * @category constructors
 * @since 4.0.0
 */
export const stream = (source: Request): Stream.Stream<Multipart.Part, Multipart.MultipartError> =>
  Stream.fromReadableStream({
    evaluate: () =>
      source.body ?? new ReadableStream({
        start(controller) {
          controller.enqueue(new Uint8Array())
          controller.close()
        }
      }),
    onError: (cause) => Multipart.MultipartError.fromReason("InternalError", cause)
  }).pipe(
    Stream.pipeThroughChannel(Multipart.makeChannel(Object.fromEntries(source.headers)))
  )

/**
 * Parses and persists multipart data from a web `Request`, requiring file-system, path, and scope services.
 *
 * @stability unstable
 * @category constructors
 * @since 4.0.0
 */
export const persisted = (
  source: Request
): Effect.Effect<
  Multipart.Persisted,
  Multipart.MultipartError,
  | FileSystem
  | Path
  | Scope.Scope
> => Multipart.toPersisted(stream(source))