The best way to control Claude Code, Codex and more from any computer or phone

main
ts 56 lines 1.76 KB
Raw
1 /**
2 * Bun-specific helpers for parsing HTTP `multipart/form-data` request bodies.
3 *
4 * This module adapts a Bun `Request` body and headers into the shared
5 * `Multipart` model. `stream` returns multipart parts as a `Stream`, while
6 * `persisted` collects the form and writes file parts to scoped temporary files
7 * through the current `FileSystem`, `Path`, and `Scope` services.
8 *
9 * @stability unstable
10 * @since 4.0.0
11 */
12 import type * as Effect from "effect/Effect"
13 import type { FileSystem } from "effect/FileSystem"
14 import * as Multipart from "effect/http/Multipart"
15 import type { Path } from "effect/Path"
16 import type * as Scope from "effect/Scope"
17 import * as Stream from "effect/Stream"
18 import * as BunStream from "./BunStream.ts"
19
20 /**
21 * Parses a Bun `Request` body as multipart data and returns a stream of multipart parts.
22 *
23 * @stability unstable
24 * @category constructors
25 * @since 4.0.0
26 */
27 export const stream = (source: Request): Stream.Stream<Multipart.Part, Multipart.MultipartError> =>
28 BunStream.fromReadableStream({
29 evaluate: () =>
30 source.body ?? new ReadableStream({
31 start(controller) {
32 controller.enqueue(new Uint8Array())
33 controller.close()
34 }
35 }),
36 onError: (cause) => Multipart.MultipartError.fromReason("InternalError", cause)
37 }).pipe(
38 Stream.pipeThroughChannel(Multipart.makeChannel(Object.fromEntries(source.headers)))
39 )
40
41 /**
42 * Parses and persists multipart data from a Bun `Request`, requiring file-system, path, and scope services.
43 *
44 * @stability unstable
45 * @category constructors
46 * @since 4.0.0
47 */
48 export const persisted = (
49 source: Request
50 ): Effect.Effect<
51 Multipart.Persisted,
52 Multipart.MultipartError,
53 | FileSystem
54 | Path
55 | Scope.Scope
56 > => Multipart.toPersisted(stream(source))