Skip to content

Instantly share code, notes, and snippets.

@dshaw
Created July 12, 2012 00:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dshaw/3094870 to your computer and use it in GitHub Desktop.
Save dshaw/3094870 to your computer and use it in GitHub Desktop.
Add a repl to Node v0.6 or v0.8
var fs = require("fs")
, net = require("net")
, repl = require("repl")
module.exports = function replify (name, ctx) {
var repl_path = "/tmp/" + name + ".sock"
ctx || (ctx = {})
fs.unlink(repl_path, function () {
// intentionally not listening for the error. either way, we're good.
net.createServer(function repl_onrequest(socket) {
var repl_opt = {
prompt: name + "> "
, input: socket
, output: socket
, terminal: true
, useGlobal: false
},
r = null
socket.columns = 132 // Set screen width for autocomplete. You can modify this locally in your repl.
if (fs.exists) { // if `fs.exists` exists, then it's node v0.8
r = repl.start(repl_opt)
r.on('exit', function () {
socket.end()
})
} else {
r = repl.start(repl_opt.prompt, socket)
}
r.context.socket = socket
Object.keys(ctx).forEach(function (key) {
// don't pave me, bro!
if (!r.context[key]) r.context[key] = ctx[key]
})
}).listen(repl_path)
})
}
@dshaw
Copy link
Author

dshaw commented Jul 12, 2012

Key considerations:

  1. v0.8 repl does not unlink (remove) the domain socket file. You have to do that yourself now. See node #3540.
  2. v0.6 repl does not accept an options variable. v0.8 will actually accept both the old school parameters or an options object.
  3. v0.6 repl is not an instanceof EventEmitter.

@3rd-Eden
Copy link

You would almost create a module just for this inconsistent behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment