(
body: StandardBody,
headers: StandardHeaders,
options: ToNodeHttpBodyOptions = {},
)
| 58 | * @param options |
| 59 | */ |
| 60 | export function toNodeHttpBody( |
| 61 | body: StandardBody, |
| 62 | headers: StandardHeaders, |
| 63 | options: ToNodeHttpBodyOptions = {}, |
| 64 | ): Readable | undefined | string { |
| 65 | if (body instanceof ReadableStream) { |
| 66 | return Readable.fromWeb(body) |
| 67 | } |
| 68 | |
| 69 | const currentContentDisposition = flattenHeader(headers['content-disposition']) |
| 70 | |
| 71 | delete headers['content-type'] |
| 72 | delete headers['content-disposition'] |
| 73 | |
| 74 | if (body === undefined) { |
| 75 | return |
| 76 | } |
| 77 | |
| 78 | if (body instanceof Blob) { |
| 79 | headers['content-type'] = body.type |
| 80 | headers['content-length'] = body.size.toString() |
| 81 | headers['content-disposition'] = currentContentDisposition ?? generateContentDisposition(body instanceof File ? body.name : 'blob') |
| 82 | |
| 83 | return Readable.fromWeb(body.stream()) |
| 84 | } |
| 85 | |
| 86 | if (body instanceof FormData) { |
| 87 | const response = new Response(body) |
| 88 | headers['content-type'] = response.headers.get('content-type')! |
| 89 | |
| 90 | return Readable.fromWeb(response.body!) |
| 91 | } |
| 92 | |
| 93 | if (body instanceof URLSearchParams) { |
| 94 | headers['content-type'] = 'application/x-www-form-urlencoded' |
| 95 | |
| 96 | return body.toString() |
| 97 | } |
| 98 | |
| 99 | if (isAsyncIteratorObject(body)) { |
| 100 | headers['content-type'] = 'text/event-stream' |
| 101 | |
| 102 | return toEventStream(body, options) |
| 103 | } |
| 104 | |
| 105 | headers['content-type'] = 'application/json' |
| 106 | |
| 107 | return stringifyJSON(body) |
| 108 | } |
| 109 | |
| 110 | function _streamToFormData(stream: Readable, contentType: string): Promise<FormData> { |
| 111 | const response = new Response(stream, { |
no test coverage detected