summaryrefslogtreecommitdiff
path: root/FakeSocket.js
diff options
context:
space:
mode:
Diffstat (limited to 'FakeSocket.js')
-rw-r--r--FakeSocket.js44
1 files changed, 44 insertions, 0 deletions
diff --git a/FakeSocket.js b/FakeSocket.js
new file mode 100644
index 0000000..af3d74c
--- /dev/null
+++ b/FakeSocket.js
@@ -0,0 +1,44 @@
+function FakeSocket(stdin, stdout, msgHandler) {
+ this.stdin = stdin;
+ this.stdout = stdout;
+ this.msgHandler = msgHandler;
+ this.connectHandler = undefined;
+ this.errorHandler = undefined;
+}
+
+FakeSocket.prototype.setEncoding = function(encoding) {
+ this.stdin.setEncoding(encoding);
+}
+
+FakeSocket.prototype.on = function(event, cb) {
+ switch(event) {
+ case 'data':
+ this.stdin.on('data', function(chunk) {
+ cb(chunk.trim() + '\r\n');
+ });
+ break;
+ case 'connect':
+ this.connectHandler = cb;
+ break;
+ case 'close':
+ this.stdin.on('exit', cb);
+ break;
+ case 'end':
+ case 'error':
+ break;
+ default:
+ process.exit(1);
+ break;
+ }
+}
+
+FakeSocket.prototype.connect = function(server, port) {
+ this.connectHandler();
+}
+
+FakeSocket.prototype.write = function(chunk) {
+ this.stdout.write(chunk.red);
+ this.msgHandler && this.msgHandler(chunk);
+}
+
+module.exports = FakeSocket;