A MozRepl for node.js
I’ve long been a fan of MozRepl, a Firefox
plugin which allows you to open a REPL in your running Firefox that you can connect
to via a socket. Why would you want to do a crazy thing like
that,
you ask? Mainly for the Emacs
integration. Here’s the scenario:
- Load page in browser.
- Start MozRepl in Firefox.
- Connect to MozRepl from the Emacs you’re editing the page’s JavaScript in.
- Exercise some functionality on the page.
- Find out that some function doesn’t work.
- Make a change to the relevant JS function.
- C-M-x
- Return to step 4.
Notice how we don’t have to redeploy the JavaScript in question, nor hit reload in the browser?1 This is rad. As I’ve been playing with node.js, I’ve found myself wanting the same functionality for live-tweaking my node applications during development.
Node ships
with a node-repl
command, which you could run
as
an inferior process in Emacs. The problem is that you then
have to invoke your server code from within the inferior REPL,
so your sever will only live as long as your Emacs
process.2
What I’d really like is for the Node REPL to listen on a
socket for connections from Emacs; that way, I could use
MozRepl’s moz.el
to talk to Node directly. Unfortunately, Node’s REPL
implementation assumes it speaks to the user via stdin,
stdout, and stderr, as provided in its process.stdio
object.
So I built an object that pretends to be
process.stdio
but is actually a
socket. I figured a quick process.stdio = new SocketStdio();
before my var
repl = require('repl');
should do the trick. Alas,
this breaks things rather thoroughly.
more