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

Function teardownBody

packages/platform/node/src/NodeHttpServer.ts:1192–1213  ·  view source on GitHub ↗
(request: Http.IncomingMessage, response: Http.ServerResponse)

Source from the content-addressed store, hash-verified

1190// destroys a closing socket as soon as the response is flushed, which resets the
1191// connection while body bytes are queued and can lose the response.
1192const teardownBody = (request: Http.IncomingMessage, response: Http.ServerResponse): void => {
1193 if (request.complete || request.destroyed) {
1194 return
1195 }
1196 const socket = request.socket
1197 if ((response as unknown as ServerResponseInternals)._last) {
1198 // Skips Node's `_dump`. Unlike `_dumped`, it keeps the parser applying backpressure.
1199 ;(request as unknown as IncomingMessageInternals)._consuming = true
1200 ;(response as unknown as ServerResponseInternals)._last = false
1201 return closeSocket(socket)
1202 }
1203 let bytes = 0
1204 request.on("data", (chunk: Buffer) => {
1205 bytes += chunk.length
1206 if (bytes > maxDrainBytes) {
1207 // Pausing stops further `data` events, so this runs once.
1208 request.pause()
1209 closeSocket(socket)
1210 }
1211 })
1212 request.resume()
1213}
1214
1215const closeSocket = (socket: Net.Socket): void => {
1216 if (socket.destroyed) {

Callers 1

makeHandlerFunction · 0.85

Calls 4

closeSocketFunction · 0.70
onMethod · 0.65
pauseMethod · 0.65
resumeMethod · 0.65

Tested by

no test coverage detected