MCPcopy Create free account
hub / github.com/middleapi/orpc / toNodeHttpBody

Function toNodeHttpBody

packages/standard-server-node/src/body.ts:60–108  ·  view source on GitHub ↗
(
  body: StandardBody,
  headers: StandardHeaders,
  options: ToNodeHttpBodyOptions = {},
)

Source from the content-addressed store, hash-verified

58 * @param options
59 */
60export 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
110function _streamToFormData(stream: Readable, contentType: string): Promise<FormData> {
111 const response = new Response(stream, {

Callers 4

sendStandardResponseFunction · 0.90
body.test.tsFile · 0.90
sendStandardResponseFunction · 0.90
toLambdaBodyFunction · 0.90

Calls 7

flattenHeaderFunction · 0.90
isAsyncIteratorObjectFunction · 0.90
toEventStreamFunction · 0.90
stringifyJSONFunction · 0.90
toStringMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected