-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
executable file
·49 lines (37 loc) · 1.25 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env node
/* jshint camelcase:false */
// stolen from https://gist.github.com/jakwings/7772580
"use strict";
var addr = (
process.argv[2] ||
process.env.DYN_REPL_SOCK ||
process.env.npm_package_config_dynReplSocket
);
var net = require('net');
var socket = net.connect(addr);
process.stdin.pipe(socket);
/// For backwards compatibility with Node program older than v0.10,
/// readable streams switch into "flowing mode" when a 'data' event handler
/// is added, or when the pause() or resume() methods are called.
process.stdin.on('data', function (buffer) {
if (buffer.length === 1 && buffer[0] === 0x04) { // EOT
process.stdin.emit('end'); // process.stdin will be destroyed
process.stdin.setRawMode(false);
process.stdin.pause(); // stop emitting 'data' event
}
});
/// this event won't be fired if REPL is exited by '.exit' command
process.stdin.on('end', function () {
console.log('.exit');
socket.destroy();
});
socket.pipe(process.stdout);
socket.on('connect', function () {
console.log('Connected.');
//process.stdin.resume(); // already in flowing mode
process.stdin.setRawMode(true);
});
socket.on('close', function close() {
console.log('Disconnected.');
socket.removeListener('close', close);
});