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

Function rawExchange

packages/platform/node/test/NodeHttpServer.test.ts:2806–2831  ·  view source on GitHub ↗
(port: number, input: string | ReadonlyArray<string>)

Source from the content-addressed store, hash-verified

2804
2805// Writes `input`, half-closes, and resolves with everything the server sent.
2806const rawExchange = (port: number, input: string | ReadonlyArray<string>): Promise<string> =>
2807 new Promise((resolve, reject) => {
2808 const socket = Net.createConnection({ host: "127.0.0.1", port })
2809 const timer = setTimeout(() => {
2810 socket.destroy()
2811 reject(new Error("the exchange did not finish in time"))
2812 }, 5000)
2813 let stream = Buffer.Buffer.alloc(0)
2814 // Pause between parts so the server reads them separately.
2815 const parts = typeof input === "string" ? [input] : [...input]
2816 const writeNext = () => {
2817 const part = parts.shift()!
2818 if (parts.length === 0) return socket.end(part)
2819 socket.write(part)
2820 setTimeout(writeNext, 50)
2821 }
2822 socket.on("connect", writeNext)
2823 socket.on("data", (data) => {
2824 stream = Buffer.Buffer.concat([stream, Buffer.Buffer.from(data)])
2825 })
2826 socket.on("error", constVoid)
2827 socket.on("close", () => {
2828 clearTimeout(timer)
2829 resolve(stream.toString())
2830 })
2831 })
2832
2833const rawUpgradeRequest = (port: number, path: string): Promise<string> =>
2834 new Promise((resolve, reject) => {

Callers 1

Calls 6

fromMethod · 0.80
onMethod · 0.65
toStringMethod · 0.65
rejectFunction · 0.50
resolveFunction · 0.50
destroyMethod · 0.45

Tested by

no test coverage detected