Repository navigation
Expand file tree
/
Copy pathconsole.js
More file actions
70 lines (60 loc) · 1.4 KB
/
Copy pathconsole.js
File metadata and controls
70 lines (60 loc) · 1.4 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var repl = require('repl')
var net = require('net')
exports.createServer = function (context) {
return net.createServer(function (socket) {
var r = repl.start({
prompt: '>'
, input: socket
, output: socket
, terminal: true
, ignoreUndefined: true
, useGlobal: false
})
r.on('exit', function () {
socket.end()
})
if(context)
for (var k in context) {
r.context[k] = (
'function' == typeof context[k]
? context[k].bind(context)
: context[k]
)
}
})
}
exports.connect = function () {
var args = [].slice.call(arguments)
var sock = net.connect.apply(net, args)
process.stdin.pipe(sock)
sock.pipe(process.stdout)
sock.on('connect', function () {
process.stdin.resume();
process.stdin.setRawMode(true)
})
sock.on('close', function done () {
process.stdin.setRawMode(false)
process.stdin.pause()
sock.removeListener('close', done)
})
process.stdin.on('end', function () {
sock.destroy()
console.log()
})
process.stdin.on('data', function (b) {
if (b.length === 1 && b[0] === 4) {
process.stdin.emit('end')
}
})
return sock
}
//for testing.
if(!module.parent) {
var args = process.argv.slice(2)
var type = args.shift()
var port = args.shift() || 1337
if('server' == type)
exports.createServer(global).listen(port)
else
exports.connect(port)
}