MCPcopy Create free account
hub / github.com/Effect-TS/effect / writeResponse

Function writeResponse

packages/platform/node/src/NodeHttpServer.ts:1006–1107  ·  view source on GitHub ↗
(
  request: HttpServerRequest,
  response: HttpServerResponse,
  nodeResponse: Http.ServerResponse,
  headers: Record<string, string | Array<string>>
)

Source from the content-addressed store, hash-verified

1004}
1005
1006const writeResponse = (
1007 request: HttpServerRequest,
1008 response: HttpServerResponse,
1009 nodeResponse: Http.ServerResponse,
1010 headers: Record<string, string | Array<string>>
1011): Effect.Effect<unknown, HttpServerError> => {
1012 // Nothing can reach a disconnected client, and writing would throw or never finish
1013 if (nodeResponse.destroyed) {
1014 return cancelResponseBody(response.body)
1015 }
1016 if (request.method === "HEAD") {
1017 writeHead(nodeResponse, response, headers)
1018 return Effect.andThen(cancelResponseBody(response.body), endAndAwait(nodeResponse))
1019 }
1020 if (Response.omitsBody(response)) {
1021 writeHead(nodeResponse, response, headers)
1022 nodeResponse.end()
1023 return cancelResponseBody(response.body)
1024 }
1025 nodeResponse.strictContentLength = true
1026 const body = response.body
1027 switch (body._tag) {
1028 case "Empty": {
1029 writeHead(nodeResponse, response, headers)
1030 return end(nodeResponse, response)
1031 }
1032 case "Raw": {
1033 const raw = body.body
1034 const piped = raw instanceof ReadableStream ||
1035 (typeof raw === "object" && raw !== null && "pipe" in raw && typeof raw.pipe === "function")
1036 writeHead(nodeResponse, response, headers, piped)
1037 if (piped) {
1038 return Effect.tryPromise({
1039 try: (signal) => pipeTo(raw as Readable | ReadableStream, nodeResponse, headers, signal),
1040 catch: (cause) =>
1041 new HttpServerError({
1042 reason: new ResponseError({
1043 request,
1044 response,
1045 description: "Error writing raw response",
1046 cause
1047 })
1048 })
1049 }).pipe(
1050 Effect.interruptible,
1051 Effect.tapCause(handleCause(nodeResponse, response))
1052 )
1053 }
1054 return endAndAwait(nodeResponse, raw).pipe(Effect.tapCause(handleCause(nodeResponse, response)))
1055 }
1056 case "Uint8Array": {
1057 writeHead(nodeResponse, response, headers)
1058 // If the body is less than 1MB, we skip the callback
1059 if (body.contentLength < 1024 * 1024) {
1060 // Writing text directly lets Node flush headers and body together.
1061 return end(nodeResponse, response, body.text ?? body.body)
1062 }
1063 return endAndAwait(nodeResponse, body.text ?? body.body).pipe(

Callers 10

handleResponseFunction · 0.85
McpTestPeer.tsFile · 0.85
routeFunction · 0.85
McpServer.tsFile · 0.85
onResponseFunction · 0.85
acknowledgeFunction · 0.85
broadcastFunction · 0.85
processDataFunction · 0.85
sendFunction · 0.85
makeProtocolWorkerFunction · 0.85

Calls 12

writeHeadFunction · 0.85
endAndAwaitFunction · 0.85
pipeToFunction · 0.85
cancelResponseBodyFunction · 0.70
endFunction · 0.70
handleCauseFunction · 0.70
endMethod · 0.65
pipeMethod · 0.65
onMethod · 0.65
openUnsafeMethod · 0.65
writeMethod · 0.65
closeUnsafeMethod · 0.65

Tested by

no test coverage detected