Comparing version 0.0.3 to 0.0.4
@@ -9,14 +9,28 @@ var repl = require('repl') | ||
function Repl(config) { | ||
var self = this | ||
if (!(this instanceof Repl)) return new Repl(config) | ||
if (!(this instanceof Repl)) return new Repl(config) | ||
this._config = config | ||
this.clientCount = 0 | ||
} | ||
net.createServer(function (socket) { | ||
util.inherits(Repl, EE) | ||
Repl.prototype.clients = function (n) { | ||
if (!n) n = 0 | ||
this.clientCount += n | ||
return this.clientCount | ||
} | ||
Repl.prototype.start = function () { | ||
var self = this | ||
this._server = net.createServer(function (socket) { | ||
socket.on('error', function (e) { | ||
console.error('Socket error:', e.message); | ||
self.emit('error', e) | ||
socket.end(); | ||
}) | ||
socket.on('end', close) | ||
@@ -29,7 +43,7 @@ | ||
} | ||
socket.setKeepAlive(true, 1000) | ||
var r = repl.start({ | ||
prompt: config.prompt || '> ', | ||
prompt: self._config.prompt || '> ', | ||
input: socket, | ||
@@ -41,6 +55,6 @@ output: socket, | ||
.on('exit', close) | ||
if (config.context) { | ||
Object.keys(config.context).forEach(function (key) { | ||
r.context[key] = config.context[key] | ||
if (self._config.context) { | ||
Object.keys(self._config.context).forEach(function (key) { | ||
r.context[key] = self._config.context[key] | ||
}) | ||
@@ -53,16 +67,15 @@ } | ||
}) | ||
.listen(config.port, config.host || 'localhost', function() { | ||
self.emit('replStarted') | ||
.listen(self._config.port, self._config.host || 'localhost', function() { | ||
self.emit('replStarted') | ||
}) | ||
} | ||
util.inherits(Repl, EE) | ||
Repl.prototype.clients = function (n) { | ||
if (!n) n = 0 | ||
this.clientCount += n | ||
Repl.prototype.stop = function (cb) { | ||
var self = this | ||
return this.clientCount | ||
} | ||
this._server.close(function () { | ||
self._server = null | ||
self.emit('replStopped') | ||
cb && cb() | ||
}) | ||
} |
{ | ||
"name": "replize", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Simple REPL for embedding into applications", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -26,2 +26,9 @@ ### REPLize | ||
### Events | ||
- `replConnected` `(clientCount)` Client connection is created | ||
- `replDisconnected` `(clientCount)` Client connection is ended | ||
- `error (error)` Socket error | ||
- `replStarted` TCP server is bound | ||
- `replStopped` TCP server is shutdown | ||
### Example | ||
@@ -28,0 +35,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
94580
30
195
65
2