The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * Node.js multipart parsing for HTTP `multipart/form-data` request bodies. |
| 3 | * |
| 4 | * `NodeMultipart` adapts a Node `Readable` plus incoming HTTP headers into |
| 5 | * Effect's shared multipart model. It can expose form parts as a stream or |
| 6 | * collect a complete persisted form by writing file uploads to scoped temporary |
| 7 | * files through the current `FileSystem` and `Path` services. `fileToReadable` |
| 8 | * returns the underlying Node readable stream for file parts produced by this |
| 9 | * parser. |
| 10 | * |
| 11 | * @stability unstable |
| 12 | * @since 4.0.0 |
| 13 | */ |
| 14 | import * as Effect from "effect/Effect" |
| 15 | import type * as FileSystem from "effect/FileSystem" |
| 16 | import * as Multipart from "effect/http/Multipart" |
| 17 | import * as MultipartParser from "effect/http/MultipartParser" |
| 18 | import * as Inspectable from "effect/Inspectable" |
| 19 | import type * as Path from "effect/Path" |
| 20 | import type * as Scope from "effect/Scope" |
| 21 | import * as Stream from "effect/Stream" |
| 22 | import * as NFS from "node:fs" |
| 23 | import type { IncomingHttpHeaders } from "node:http" |
| 24 | import { finished, type Readable } from "node:stream" |
| 25 | import * as NodeStreamP from "node:stream/promises" |
| 26 | import * as MP from "./NodeMultipartParser.ts" |
| 27 | import * as NodeStream from "./NodeStream.ts" |
| 28 | |
| 29 | /** |
| 30 | * Parses multipart data from a Node readable request body and headers into a |
| 31 | * stream of `Multipart.Part` values, converting parser failures to |
| 32 | * `MultipartError`. |
| 33 | * |
| 34 | * @stability unstable |
| 35 | * @category constructors |
| 36 | * @since 4.0.0 |
| 37 | */ |
| 38 | export const stream = ( |
| 39 | source: Readable, |
| 40 | headers: IncomingHttpHeaders |
| 41 | ): Stream.Stream<Multipart.Part, Multipart.MultipartError> => |
| 42 | Multipart.makeConfig(headers as any).pipe( |
| 43 | Effect.map((config) => |
| 44 | NodeStream.fromReadable<MP.Part, Multipart.MultipartError>({ |
| 45 | evaluate() { |
| 46 | const parser = MP.make(config) |
| 47 | // `pipe` does not forward source errors or a premature close. |
| 48 | const cleanup = finished(source, { readable: true, writable: false }, (error) => { |
| 49 | cleanup() |
| 50 | if (error) parser.destroy(error) |
| 51 | }) |
| 52 | source.pipe(parser) |
| 53 | return parser as any |
| 54 | }, |
| 55 | onError: (error) => convertError(error as any) |
| 56 | }) |
| 57 | ), |
| 58 | Stream.unwrap, |
| 59 | Stream.map(convertPart) |
| 60 | ) |
| 61 | |
| 62 | /** |
| 63 | * Parses multipart data from a Node readable request body and persists file |
| 64 | * parts using the current `FileSystem`, `Path`, and `Scope` services. |
| 65 | * |
| 66 | * @stability unstable |
| 67 | * @category constructors |
| 68 | * @since 4.0.0 |
| 69 | */ |
| 70 | export const persisted = ( |
| 71 | source: Readable, |
| 72 | headers: IncomingHttpHeaders |
| 73 | ): Effect.Effect< |
| 74 | Multipart.Persisted, |
| 75 | Multipart.MultipartError, |
| 76 | Scope.Scope | FileSystem.FileSystem | Path.Path |
| 77 | > => |
| 78 | Multipart.toPersisted(stream(source, headers), (path, file) => |
| 79 | Effect.tryPromise({ |
| 80 | try: (signal) => NodeStreamP.pipeline((file as FileImpl).file, NFS.createWriteStream(path), { signal }), |
| 81 | catch: (cause) => Multipart.MultipartError.fromReason("InternalError", cause) |
| 82 | })) |
| 83 | |
| 84 | /** |
| 85 | * Returns the underlying Node readable stream for a multipart file produced by |
| 86 | * the Node multipart parser. |
| 87 | * |
| 88 | * @stability unstable |
| 89 | * @category converting |
| 90 | * @since 4.0.0 |
| 91 | */ |
| 92 | export const fileToReadable = (file: Multipart.File): Readable => (file as FileImpl).file |
| 93 | |
| 94 | // ----------------------------------------------------------------------------- |
| 95 | // Internal |
| 96 | // ----------------------------------------------------------------------------- |
| 97 | |
| 98 | const convertPart = (part: MP.Part): Multipart.Part => |
| 99 | part._tag === "Field" ? new FieldImpl(part.info, part.value) : new FileImpl(part) |
| 100 | |
| 101 | abstract class PartBase extends Inspectable.Class { |
| 102 | readonly [Multipart.TypeId]: typeof Multipart.TypeId |
| 103 | constructor() { |
| 104 | super() |
| 105 | this[Multipart.TypeId] = Multipart.TypeId |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | class FieldImpl extends PartBase implements Multipart.Field { |
| 110 | readonly _tag = "Field" |
| 111 | readonly key: string |
| 112 | readonly contentType: string |
| 113 | readonly value: string |
| 114 | |
| 115 | constructor( |
| 116 | info: MultipartParser.PartInfo, |
| 117 | value: Uint8Array |
| 118 | ) { |
| 119 | super() |
| 120 | this.key = info.name |
| 121 | this.contentType = info.contentType |
| 122 | this.value = MultipartParser.decodeField(info, value) |
| 123 | } |
| 124 | |
| 125 | toJSON(): unknown { |
| 126 | return { |
| 127 | _id: "@effect/platform/Multipart/Part", |
| 128 | _tag: "Field", |
| 129 | key: this.key, |
| 130 | value: this.value, |
| 131 | contentType: this.contentType |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | class FileImpl extends PartBase implements Multipart.File { |
| 137 | readonly _tag = "File" |
| 138 | readonly key: string |
| 139 | readonly name: string |
| 140 | readonly contentType: string |
| 141 | readonly content: Stream.Stream<Uint8Array, Multipart.MultipartError> |
| 142 | readonly contentEffect: Effect.Effect<Uint8Array, Multipart.MultipartError> |
| 143 | readonly file: MP.FileStream |
| 144 | |
| 145 | constructor(file: MP.FileStream) { |
| 146 | super() |
| 147 | this.file = file |
| 148 | this.key = file.info.name |
| 149 | this.name = file.filename ?? file.info.name |
| 150 | this.contentType = file.info.contentType |
| 151 | this.content = NodeStream.fromReadable({ |
| 152 | evaluate: () => file, |
| 153 | onError: (cause) => Multipart.MultipartError.fromReason("InternalError", cause) |
| 154 | }) |
| 155 | this.contentEffect = NodeStream.toUint8Array(() => file, { |
| 156 | onError: (cause) => Multipart.MultipartError.fromReason("InternalError", cause) |
| 157 | }) |
| 158 | } |
| 159 | |
| 160 | toJSON(): unknown { |
| 161 | return { |
| 162 | _id: "@effect/platform/Multipart/Part", |
| 163 | _tag: "File", |
| 164 | key: this.key, |
| 165 | name: this.name, |
| 166 | contentType: this.contentType |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | function convertError(cause: MultipartParser.MultipartError): Multipart.MultipartError { |
| 172 | switch (cause._tag) { |
| 173 | case "ReachedLimit": { |
| 174 | switch (cause.limit) { |
| 175 | case "MaxParts": { |
| 176 | return Multipart.MultipartError.fromReason("TooManyParts", cause) |
| 177 | } |
| 178 | case "MaxFieldSize": { |
| 179 | return Multipart.MultipartError.fromReason("FieldTooLarge", cause) |
| 180 | } |
| 181 | case "MaxPartSize": { |
| 182 | return Multipart.MultipartError.fromReason("FileTooLarge", cause) |
| 183 | } |
| 184 | case "MaxTotalSize": { |
| 185 | return Multipart.MultipartError.fromReason("BodyTooLarge", cause) |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | default: { |
| 190 | return Multipart.MultipartError.fromReason("Parse", cause) |
| 191 | } |
| 192 | } |
| 193 | } |