Permalink
Browse files

Release v0.0.1

  • Loading branch information...
pgte committed Oct 11, 2010
1 parent 5e5583e commit d476059585f04baf60b625489f12249b4f279606
Showing with 338 additions and 0 deletions.
  1. +18 −0 LICENSE
  2. +25 −0 README.markdown
  3. +55 −0 lib/carrier.js
  4. +25 −0 package.json
  5. +32 −0 test/test_concatenation.js
  6. +34 −0 test/test_line_breaks.js
  7. +27 −0 test/test_no_br.js
  8. +27 −0 test/test_one_line_ending_in_nl.js
  9. +46 −0 tools/one_test.js
  10. +49 −0 tools/test.js
View
18 LICENSE
@@ -0,0 +1,18 @@
Copyright 2010 Pedro Teixeira. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
View
@@ -0,0 +1,25 @@
Carrier allows you to implement new-line terminated protocols over node.js.
The client can send you chunks of lines and carrier will only notify you on each completed line.
## Install
$ npm install carrier
## Usage
var net = require('net'),
carrier = require('carrier');
var server;
var port = 4001;
var expected_line = "Hello World"
server = net.createServer(function(conn) {
carrier.carry(conn, function(line) {
console.log('got one line: ' + line);
});
});
server.listen(port);
View
@@ -0,0 +1,55 @@
var sys = require('sys'),
events = require('events');
function Carrier(reader, listener, encoding) {
var self = this;
self.reader = reader;
if (listener) {
self.addListener('line', listener);
}
line = '';
reader.on('data', function(data) {
var decoded = data.toString(encoding);
var lines = decoded.split("\n");
if (decoded.charAt(decoded.length - 1) == "\n") {
// get rid of last "" after last "\n"
lines.pop(1);
}
if (lines.length > 0) {
lines.forEach(function(one_line, index) {
line += one_line;
var emit = true;
if (index == lines.length - 1) {
// processing last line
if (decoded.charAt(decoded.length - 1) != "\n") {
// if it was not terminated by "\n" then the last line was not finished; we just buffer it.
emit = false;
}
}
if (emit) {
self.emit('line', line);
line = '';
}
})
}
});
reader.on('end', function() {
if (line.length > 0) {
self.emit('line', line);
line = '';
}
});
}
sys.inherits(Carrier, events.EventEmitter);
exports.carry = function(reader, listener, encoding) {
return new Carrier(reader, listener, encoding);
}
View
@@ -0,0 +1,25 @@
{ "name" : "carrier"
, "description" : "Evented stream line reader for node.js"
, "version" : "0.0.1"
, "homepage" : "http://github.com/pgte/carrier"
, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com> (http://www.metaduck.com)"
, "contributors" :
[ "Pedro Teixeira <pedro.teixeira@gmail.com>"
]
, "repository" :
{ "type" : "git"
, "url" : "http://github.com/pgte/carrier.git"
}
, "bugs" :
{ "mail" : "pedro.teixeira@gmail.com"
, "web" : "http://github.com/pgte/carrier/issues"
}
, "directories" : { "lib" : "./lib" }
, "engines" : { "node" : ">=0.2.3" }
, "scripts" : {}
, "licenses" :
[ { "type" : "MIT"
, "url" : "http://github.com/pgte/carrier/raw/master/LICENSE"
}
]
}
View
@@ -0,0 +1,32 @@
var net = require('net'),
assert = require('assert'),
carrier = require('../lib/carrier.js');
var server;
var port = 4001;
var to_be_sents = ["Hel", "lo ", "Wor", "ld"]
var expected = to_be_sents.join('');
exports.run = function(next) {
server = net.createServer(function(conn) {
carrier.carry(conn, function(line) {
assert.equal(line, expected);
next();
});
});
server.listen(port);;
var client = net.createConnection(port);
client.on('connect', function() {
to_be_sents.forEach(function(to_be_sent) {
client.write(to_be_sent);
client.flush();
})
client.end();
});
}
exports.teardown = function() {
server.close();
}
View
@@ -0,0 +1,34 @@
var net = require('net'),
assert = require('assert'),
carrier = require('../lib/carrier.js');
var server;
var port = 4001;
var to_be_sents = ["Hel", "lo\n", "Wor", "ld\n", "Glorious", " place"]
var expecteds = to_be_sents.join('').split("\n");
exports.run = function(next) {
server = net.createServer(function(conn) {
var received_lines = -1;
carrier.carry(conn, function(line) {
received_lines ++;
assert.equal(line, expecteds[received_lines]);
if (received_lines == expecteds.length - 1) next();
});
});
server.listen(port);;
var client = net.createConnection(port);
client.on('connect', function() {
to_be_sents.forEach(function(to_be_sent) {
client.write(to_be_sent);
client.flush();
})
client.end();
});
}
exports.teardown = function() {
server.close();
}
View
@@ -0,0 +1,27 @@
var net = require('net'),
assert = require('assert'),
carrier = require('../lib/carrier.js');
var server;
var port = 4001;
var expected_line = "Hello World"
exports.run = function(next) {
server = net.createServer(function(conn) {
carrier.carry(conn, function(line) {
assert.equal(line, expected_line);
next();
});
});
server.listen(port);
var client = net.createConnection(port);
client.on('connect', function() {
client.end(expected_line);
});
}
exports.teardown = function() {
server.close();
}
@@ -0,0 +1,27 @@
var net = require('net'),
assert = require('assert'),
carrier = require('../lib/carrier.js');
var server;
var port = 4001;
var expected_line = "Hello World"
exports.run = function(next) {
server = net.createServer(function(conn) {
carrier.carry(conn, function(line) {
assert.equal(line, expected_line);
next();
});
});
server.listen(port);;
var client = net.createConnection(port);
client.on('connect', function() {
client.write(expected_line + "\n");
});
}
exports.teardown = function() {
server.close();
}
View
@@ -0,0 +1,46 @@
var path = require('path');
var test_path = path.join(__dirname, '..', 'test', process.argv[2] + '.js');
if (!path.existsSync(test_path)) throw "Could not find test path " + test_path;
var test_module = require(test_path);
var test_timeout = test_module.global_timeout ? test_module.global_timeout : 30000;
var exiting = false;
var do_exit = function() {
if (exiting) return;
exiting = true;
if (test_module.teardown) test_module.teardown();
process.removeListener('exit', do_exit);
process.nextTick(function() {
process.exit();
});
}
if (!test_module.run) throw "test module " + module_path + " does not export run() function";
process.on('uncaughtException', function(excp) {
if (excp.message || excp.name) {
if (excp.name) process.stdout.write(excp.name);
if (excp.message) process.stdout.write(excp.message);
if (excp.backtrace) process.stdout.write(excp.backtrace);
if (excp.stack) process.stdout.write(excp.stack);
} else {
sys = require('sys');
process.stdout.write(sys.inspect(excp));
}
process.stdout.write("\n");
do_exit();
});
process.on('exit', do_exit);
if (test_module.setup) {
test_module.setup(function() {
test_module.run(do_exit);
});
} else {
test_module.run(do_exit);
};
View
@@ -0,0 +1,49 @@
process.on('uncaughtException', function(excp) {
if (excp.message || excp.name) {
if (excp.name) process.stdout.write(excp.name);
if (excp.message) process.stdout.write(excp.message);
if (excp.backtrace) process.stdout.write(excp.backtrace);
if (excp.stack) process.stdout.write(excp.stack);
} else {
sys = require('sys');
process.stdout.write(sys.inspect(excp));
}
});
var tests = process.argv.slice(2);
if(tests.length < 1) {
console.log('No tests to run');
process.exit(1);
}
//console.log("running tests "+tests.join(', '));
var path = require('path'),
child_process = require('child_process'),
assert = require('assert');
var test_index = 0;
var do_test = function() {
var test = tests[test_index];
console.log("running test " + test + '...');
var module_path = path.join(__dirname, '..', 'test', test + ".js");
if (!path.existsSync(module_path)) throw "Could not find test path "+module_path;
var run_this = [path.join(__dirname, 'one_test.js'), test];
var test_child = child_process.spawn(process.argv[0], run_this, {env: process.env});
test_child.stdout.on('data', function(data) {
assert.ok(false, data.toString());
});
test_child.stderr.on('data', function(data) {
assert.ok(false, data.toString());
});
test_child.on('exit', function() {
console.log('finished');
test_index ++;
if(test_index < tests.length) {
setTimeout(do_test, 4000);
}
});
}
do_test();

0 comments on commit d476059

Please sign in to comment.