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

Function handleResponse

packages/platform-node/src/internal/httpServer.ts:375–494  ·  view source on GitHub ↗
(request: ServerRequest.HttpServerRequest, response: ServerResponse.HttpServerResponse)

Source from the content-addressed store, hash-verified

373 )
374
375const handleResponse = (request: ServerRequest.HttpServerRequest, response: ServerResponse.HttpServerResponse) =>
376 Effect.suspend((): Effect.Effect<void, Error.ResponseError> => {
377 const nodeResponse = (request as ServerRequestImpl).resolvedResponse
378 if (nodeResponse.writableEnded) {
379 return Effect.void
380 }
381
382 let headers: Record<string, string | Array<string>> = response.headers
383 if (!Cookies.isEmpty(response.cookies)) {
384 headers = { ...headers }
385 const toSet = Cookies.toSetCookieHeaders(response.cookies)
386 if (headers["set-cookie"] !== undefined) {
387 toSet.push(headers["set-cookie"] as string)
388 }
389 headers["set-cookie"] = toSet
390 }
391
392 if (request.method === "HEAD") {
393 nodeResponse.writeHead(response.status, headers)
394 return Effect.async<void>((resume) => {
395 nodeResponse.end(() => resume(Effect.void))
396 })
397 }
398 const body = response.body
399 switch (body._tag) {
400 case "Empty": {
401 nodeResponse.writeHead(response.status, headers)
402 nodeResponse.end()
403 return Effect.void
404 }
405 case "Raw": {
406 nodeResponse.writeHead(response.status, headers)
407 if (
408 typeof body.body === "object" && body.body !== null && "pipe" in body.body &&
409 typeof body.body.pipe === "function"
410 ) {
411 return Effect.tryPromise({
412 try: (signal) => pipeline(body.body as any, nodeResponse, { signal, end: true }),
413 catch: (cause) =>
414 new Error.ResponseError({
415 request,
416 response,
417 reason: "Decode",
418 cause
419 })
420 }).pipe(
421 Effect.interruptible,
422 Effect.tapErrorCause(handleCause(nodeResponse, response))
423 )
424 }
425 return Effect.async<void>((resume) => {
426 nodeResponse.end(body.body, () => resume(Effect.void))
427 })
428 }
429 case "Uint8Array": {
430 nodeResponse.writeHead(response.status, headers)
431 return Effect.async<void>((resume) => {
432 nodeResponse.end(body.body, () => resume(Effect.void))

Callers 2

sendFunction · 0.85
toHandledFunction · 0.85

Calls 10

handleCauseFunction · 0.85
unsafeOpenMethod · 0.80
unsafeCloseMethod · 0.80
syncMethod · 0.80
isEmptyMethod · 0.65
endMethod · 0.65
pipeMethod · 0.65
failMethod · 0.65
writeMethod · 0.65
resumeFunction · 0.50

Tested by

no test coverage detected