| 827 | * contents are streamed into the placeholder parts. |
| 828 | */ |
| 829 | const formDataBody = (formData: FormData, request: HttpClientRequest) => { |
| 830 | const transportError = (cause: unknown) => |
| 831 | new Error.HttpClientError({ reason: new Error.TransportError({ request, cause }) }) |
| 832 | const entries = Array.from(formData) |
| 833 | const placeholders = new globalThis.FormData() |
| 834 | for (const [key, value] of entries) { |
| 835 | if (typeof value === "string") placeholders.append(key, value) |
| 836 | // A `Blob` with a file name, as the global `File` is missing on Node 18. |
| 837 | else placeholders.append(key, new Blob([], { type: value.type }), value.name) |
| 838 | } |
| 839 | const response = new globalThis.Response(placeholders) |
| 840 | const contentType = response.headers.get("content-type")! |
| 841 | const delimiter = `--${contentType.slice(contentType.indexOf("boundary=") + 9)}` |
| 842 | const stream = Stream.unwrap(Effect.map(Effect.promise(() => response.arrayBuffer()), (buffer) => { |
| 843 | const encoded = Buffer.from(buffer) |
| 844 | const streams: Array<Stream.Stream<Uint8Array, Error.HttpClientError>> = [] |
| 845 | let start = 0 |
| 846 | let delimiterAt = 0 |
| 847 | for (const [, value] of entries) { |
| 848 | delimiterAt = encoded.indexOf(delimiter, delimiterAt + delimiter.length) |
| 849 | if (typeof value === "string") continue |
| 850 | // The file contents go before the CRLF that ends its empty placeholder part. |
| 851 | const end = delimiterAt - 2 |
| 852 | streams.push( |
| 853 | Stream.succeed(encoded.subarray(start, end)), |
| 854 | Stream.unwrap(Effect.map( |
| 855 | Effect.try({ try: () => value.stream(), catch: transportError }), |
| 856 | (readable) => Stream.fromReadableStream({ evaluate: () => readable, onError: transportError }) |
| 857 | )) |
| 858 | ) |
| 859 | start = end |
| 860 | } |
| 861 | streams.push(Stream.succeed(encoded.subarray(start))) |
| 862 | return Stream.flatten(Stream.fromArray(streams)) |
| 863 | })) |
| 864 | return { contentType, stream } as const |
| 865 | } |
| 866 | |
| 867 | const waitForResponse = (nodeRequest: Http.ClientRequest, request: HttpClientRequest, signal: AbortSignal) => |
| 868 | Effect.callback<readonly [Http.IncomingMessage, Duplex?], Error.HttpClientError>((resume) => { |