| 2804 | |
| 2805 | // Writes `input`, half-closes, and resolves with everything the server sent. |
| 2806 | const 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 | |
| 2833 | const rawUpgradeRequest = (port: number, path: string): Promise<string> => |
| 2834 | new Promise((resolve, reject) => { |