Created
March 13, 2012 01:48
-
-
Save TooTallNate/2026110 to your computer and use it in GitHub Desktop.
Full featured NodeJS repl server over a TCP socket (for node 0.6.x, uses some wacky server hacks)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tty = require('tty') | |
var net = require('net') | |
var sock = net.createConnection(1337) | |
sock.on('connect', function () { | |
process.stdin.resume(); | |
tty.setRawMode(true) | |
}) | |
process.stdin.pipe(sock) | |
sock.pipe(process.stdout) | |
sock.on('end', done) | |
sock.on('close', done) | |
function done () { | |
if (done.run) return | |
done.run = true | |
tty.setRawMode(false) | |
process.stdin.pause() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var repl = require('repl') | |
var net = require('net') | |
var tty = require('tty') | |
// hack #1 | |
process.stdin.resume() | |
net.createServer(function (socket) { | |
// hack #2 | |
socket.isTTY = true | |
// hack #3 | |
socket.getWindowSize = function () { return [ 80, 40 ] } | |
// hack #4 | |
socket.cursorTo = tty.WriteStream.prototype.cursorTo | |
socket.clearLine = tty.WriteStream.prototype.clearLine | |
socket.moveCursor = tty.WriteStream.prototype.moveCursor | |
// hack #5 | |
socket._emitKey = require('tty').ReadStream.prototype._emitKey | |
socket.on('data', function (b) { | |
socket._emitKey(b) | |
}) | |
var r = repl.start('socket repl > ', socket) | |
r.on && r.on('exit', function () { | |
console.error('repl exit event, closing socket') | |
socket.end() | |
}) | |
}).listen(1337) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment