Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[fix] better memory use, detach()
  • Loading branch information
bmeck committed Sep 3, 2012
1 parent ab41b1f commit 39f4abd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
5 changes: 3 additions & 2 deletions bin/rrepl
@@ -1,13 +1,14 @@
#!/usr/bin/env node
var net = require('net')

var sock = net.connect(process.argv[2] || 1337, process.argv[3])
var sock = net.connect(process.argv[2] || 1337, process.argv[3]);

process.stdin.pipe(sock);
sock.pipe(process.stdout);

sock.on('connect', function () {
process.stdin.resume();
process.stdin.setRawMode(true);
process.stdin.resume();
});

sock.on('close', function done () {
Expand Down
13 changes: 13 additions & 0 deletions example/server
@@ -0,0 +1,13 @@
#!/usr/bin/env node
//
// just fire up rrepl,
// don't even use and app
// doesn't require flatiron
//
var rrepl = require('../lib/repl');
rrepl.attach.call({}, {
terminal: true,
excludeApp: true,
excludeProcess: true,
useColors: true
});
39 changes: 20 additions & 19 deletions lib/repl.js
@@ -1,6 +1,7 @@
var net = require('net');
var repl = require('repl');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var assert = require('assert');
//
// Simple plugin to add a dumb debugging repl
Expand All @@ -16,12 +17,13 @@ var assert = require('assert');
//
exports.name = 'repl';
exports.attach = function (options) {
var app = this;
options = options || {};
options.port = options.port !== null && options.port || 1337;
options.columns = options.columns || 80;
options.host = options.host || '127.0.0.1';
var app = this;
net.createServer(function (conn) {
app.repl = {};
app.repl.server = net.createServer(function (conn) {
//
// Check a whitelist of places we are allowed to connect from if specified
//
Expand All @@ -34,7 +36,6 @@ exports.attach = function (options) {
conn.destroy();
return;
}
conn.write(new Buffer([0xff, 0xfb, 0x01, 0xff, 0xfb, 0x03, 0xff, 0xfd, 0x0f3]));
remoteAddress = null;
conn.columns = options.columns || 80;
var prompt = repl.start({
Expand All @@ -44,31 +45,24 @@ exports.attach = function (options) {
useColors: options.useColors,
useGlobal: false
});
prompt.on('exit', function () {
conn.end();
});
prompt.on('exit', conn.end.bind(conn));
//
// Process can be dangerous but no real alterative that is liked for getting various data (not going to wrap it)
//
if (!options.excludeProcess) prompt.context.process = process;
prompt.context.app = app;
if (!options.excludeApp) prompt.context.app = app;
//
// Repl should have console forwarded to it rather than printed on server
//
var times = {};
function log() {
conn.write([].slice.apply(arguments).map(util.inspect).join(' ') + '\n');
}
prompt.context.console = {
log: function () {
conn.write([].slice.apply(arguments).map(util.inspect).join(' ') + '\n');
},
error: function () {
conn.write([].slice.apply(arguments).map(util.inspect).join(' ') + '\n');
},
warn: function () {
conn.write([].slice.apply(arguments).map(util.inspect).join(' ') + '\n');
},
info: function () {
conn.write([].slice.apply(arguments).map(util.inspect).join(' ') + '\n');
},
log: log,
error: log,
warn: log,
info: log,
dir: function (obj) {
conn.write(util.inspect(obj) + '\n');
},
Expand Down Expand Up @@ -100,3 +94,10 @@ exports.attach = function (options) {
prompt = null;
}).listen(options.port, options.host);
}

exports.detach = function detach() {
var app = this;
app.repl.server.close();
delete app.repl;
delete require.cache[__filename];
}

0 comments on commit 39f4abd

Please sign in to comment.