(sock: net.Socket, key: string)
| 116 | }; |
| 117 | |
| 118 | setUpSocket(sock: net.Socket, key: string) { |
| 119 | if (!process.stdin.isTTY) { |
| 120 | return this.setUpSocketForSingleUse(sock, key); |
| 121 | } |
| 122 | |
| 123 | // Put STDIN into "flowing mode": |
| 124 | // http://nodejs.org/api/stream.html#stream_compatibility_with_older_node_versions |
| 125 | process.stdin.resume(); |
| 126 | |
| 127 | const onConnect = () => { |
| 128 | this.firstTimeConnecting = false; |
| 129 | this.reconnectCount = 0; |
| 130 | this.connected = true; |
| 131 | |
| 132 | // Sending a JSON-stringified options object (even just an empty |
| 133 | // object) over the socket is required to start the REPL session. |
| 134 | sock.write(JSON.stringify({ |
| 135 | columns: process.stdout.columns, |
| 136 | terminal: !isEmacs(), |
| 137 | key: key |
| 138 | }) + "\n"); |
| 139 | |
| 140 | process.stderr.write(shellBanner()); |
| 141 | process.stdin.pipe(sock); |
| 142 | if (process.stdin.setRawMode) { // https://github.com/joyent/node/issues/8204 |
| 143 | process.stdin.setRawMode(true); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | const onClose = () => { |
| 148 | tearDown(); |
| 149 | |
| 150 | // If we received the special EXITING_MESSAGE just before the socket |
| 151 | // closed, then exit the shell instead of reconnecting. |
| 152 | if (this.exitOnClose) { |
| 153 | process.exit(0); |
| 154 | } else { |
| 155 | this.reconnect(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | const onError = () => { |
| 160 | tearDown(); |
| 161 | this.reconnect(); |
| 162 | } |
| 163 | |
| 164 | const tearDown = () => { |
| 165 | this.connected = false; |
| 166 | |
| 167 | if (process.stdin.setRawMode) { // https://github.com/joyent/node/issues/8204 |
| 168 | process.stdin.setRawMode(false); |
| 169 | } |
| 170 | |
| 171 | process.stdin.unpipe(sock); |
| 172 | sock.unpipe(process.stdout); |
| 173 | sock.removeListener("connect", onConnect); |
| 174 | sock.removeListener("close", onClose); |
| 175 | sock.removeListener("error", onError); |
no test coverage detected