|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const net = require("node:net"); |
| 4 | + |
| 5 | +const HOST = "127.0.0.1"; |
| 6 | +const PROMPT = "> "; |
| 7 | +const SERVER_CLOSED_MESSAGE = "The server was closed after the REPL was exited"; |
| 8 | +const TIMEOUT = 30000; |
| 9 | + |
| 10 | +exports.runReplCommand = async (port, code) => { |
| 11 | + const socket = await connect(port); |
| 12 | + |
| 13 | + return new Promise((resolve, reject) => { |
| 14 | + let output = ""; |
| 15 | + |
| 16 | + const timeout = setTimeout(() => { |
| 17 | + socket.destroy(); |
| 18 | + reject(new Error(`Timed out after ${TIMEOUT}ms waiting for REPL result`)); |
| 19 | + }, TIMEOUT); |
| 20 | + |
| 21 | + socket.setEncoding("utf8"); |
| 22 | + socket.write(`${code}\n`); |
| 23 | + socket.on("data", chunk => { |
| 24 | + output += chunk; |
| 25 | + |
| 26 | + if (output.endsWith(PROMPT)) { |
| 27 | + clearTimeout(timeout); |
| 28 | + socket.end(); |
| 29 | + resolve(extractResult(output)); |
| 30 | + } |
| 31 | + }); |
| 32 | + socket.on("error", err => { |
| 33 | + clearTimeout(timeout); |
| 34 | + reject(err); |
| 35 | + }); |
| 36 | + }); |
| 37 | +}; |
| 38 | + |
| 39 | +exports.exitRepl = async port => { |
| 40 | + const socket = await connect(port); |
| 41 | + |
| 42 | + return new Promise((resolve, reject) => { |
| 43 | + let output = ""; |
| 44 | + let isDone = false; |
| 45 | + const timeout = setTimeout(() => { |
| 46 | + socket.destroy(); |
| 47 | + reject(new Error(`Timed out after ${TIMEOUT}ms waiting for REPL exit`)); |
| 48 | + }, TIMEOUT); |
| 49 | + const done = () => { |
| 50 | + if (isDone) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + isDone = true; |
| 55 | + clearTimeout(timeout); |
| 56 | + socket.destroy(); |
| 57 | + resolve(); |
| 58 | + }; |
| 59 | + |
| 60 | + socket.setEncoding("utf8"); |
| 61 | + socket.write(".exit\n"); |
| 62 | + socket.on("data", chunk => { |
| 63 | + output += chunk; |
| 64 | + |
| 65 | + if (output.includes(SERVER_CLOSED_MESSAGE)) { |
| 66 | + done(); |
| 67 | + } |
| 68 | + }); |
| 69 | + socket.on("close", done); |
| 70 | + socket.on("error", err => { |
| 71 | + if (isDone) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + isDone = true; |
| 76 | + clearTimeout(timeout); |
| 77 | + reject(err); |
| 78 | + }); |
| 79 | + }); |
| 80 | +}; |
| 81 | + |
| 82 | +function connect(port) { |
| 83 | + const startTime = Date.now(); |
| 84 | + |
| 85 | + return new Promise((resolve, reject) => { |
| 86 | + const tryConnect = () => { |
| 87 | + const socket = net.createConnection({ host: HOST, port }); |
| 88 | + |
| 89 | + socket.once("connect", () => resolve(socket)); |
| 90 | + socket.once("error", err => { |
| 91 | + socket.destroy(); |
| 92 | + |
| 93 | + if (Date.now() - startTime >= TIMEOUT) { |
| 94 | + reject(err); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + setTimeout(tryConnect, 100); |
| 99 | + }); |
| 100 | + }; |
| 101 | + |
| 102 | + tryConnect(); |
| 103 | + }); |
| 104 | +} |
| 105 | + |
| 106 | +function extractResult(rawOutput) { |
| 107 | + let result = rawOutput; |
| 108 | + |
| 109 | + if (result.startsWith(PROMPT)) { |
| 110 | + result = result.slice(PROMPT.length); |
| 111 | + } |
| 112 | + |
| 113 | + if (result.endsWith(PROMPT)) { |
| 114 | + result = result.slice(0, -PROMPT.length); |
| 115 | + } |
| 116 | + |
| 117 | + return result.replace(/\n$/, ""); |
| 118 | +} |
| 119 | + |
| 120 | +if (require.main === module) { |
| 121 | + const [, , portArg, ...codeParts] = process.argv; |
| 122 | + const port = Number(portArg); |
| 123 | + |
| 124 | + if (!Number.isInteger(port) || codeParts.length === 0) { |
| 125 | + console.error("Usage: node repl-client.js <port> <code>"); |
| 126 | + process.exit(1); |
| 127 | + } |
| 128 | + |
| 129 | + exports |
| 130 | + .runReplCommand(port, codeParts.join(" ")) |
| 131 | + .then(result => process.stdout.write(result)) |
| 132 | + .catch(err => { |
| 133 | + console.error(err.message); |
| 134 | + process.exit(1); |
| 135 | + }); |
| 136 | +} |
0 commit comments