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

Function pipeTo

packages/platform/node/src/NodeHttpServer.ts:1272–1299  ·  view source on GitHub ↗
(
  source: Readable | ReadableStream,
  nodeResponse: Http.ServerResponse,
  headers: Record<string, string | Array<string>>,
  signal: AbortSignal
)

Source from the content-addressed store, hash-verified

1270// Check lengths before writing: Node's strictContentLength throws uncaught inside pipeline.
1271// Call after `writeHead`, so a rejected head can still cancel an unlocked Web stream.
1272const pipeTo = (
1273 source: Readable | ReadableStream,
1274 nodeResponse: Http.ServerResponse,
1275 headers: Record<string, string | Array<string>>,
1276 signal: AbortSignal
1277): Promise<void> => {
1278 const readable = source instanceof ReadableStream ? Readable.fromWeb(source as any) : source
1279 const contentLength = headers["content-length"]
1280 if (typeof contentLength !== "string") {
1281 return pipeline(readable, nodeResponse, { signal })
1282 }
1283 let remaining = Number(contentLength)
1284 const mismatch = () => new Error(`Response body doesn't match content-length of ${contentLength}`)
1285 return pipeline(
1286 readable,
1287 new Transform({
1288 transform(chunk: Buffer, _encoding, callback) {
1289 remaining -= chunk.length
1290 callback(remaining < 0 ? mismatch() : null, chunk)
1291 },
1292 flush(callback) {
1293 callback(remaining > 0 ? mismatch() : null)
1294 }
1295 }),
1296 nodeResponse,
1297 { signal }
1298 )
1299}
1300
1301const handleCause = (
1302 nodeResponse: Http.ServerResponse,

Callers 1

writeResponseFunction · 0.85

Calls 1

NumberFunction · 0.50

Tested by

no test coverage detected