Created
July 28, 2010 11:30
-
-
Save Floby/494136 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 65c619b6c0e988406e4d338ecd7cb49e472c5b51 Mon Sep 17 00:00:00 2001 | |
From: Florent Jaby <[email protected]> | |
Date: Wed, 28 Jul 2010 13:09:27 +0200 | |
Subject: [PATCH] Added an experimental support for lines with streams | |
+ added function sys.lines(stream) which sets up a few utilities for streams | |
++ adds a function stream.writeln(string) which is a shorthand for stream.write(string + "\n") | |
++ add a listener on data events on this stream | |
+++ do nothing if the encoding is binary | |
+++ bufferize characters other than "\n" | |
+++ emit a 'line' event on this stream upon encounter of "\n" with the bufferized data and clean the buffer | |
+ added file test/simple/test-stream-lines.js which exits succesfully after use of the functionnalities listed above | |
--- | |
lib/sys.js | 33 ++++++++++++++++++++++++++++++++ | |
test/simple/test-stream-lines.js | 39 ++++++++++++++++++++++++++++++++++++++ | |
2 files changed, 72 insertions(+), 0 deletions(-) | |
create mode 100644 test/simple/test-stream-lines.js | |
diff --git a/lib/sys.js b/lib/sys.js | |
index 867b5a9..e18e8c6 100644 | |
--- a/lib/sys.js | |
+++ b/lib/sys.js | |
@@ -312,6 +312,39 @@ exports.pump = function (readStream, writeStream, callback) { | |
}); | |
}; | |
+ | |
+// experimental function that adds supports for lines using streams | |
+// only works if the stream is encoded with either 'ascii' or 'utf8' | |
+// adds a writeln function which is a shorthand for write(str + "\n") | |
+// adds a 'line' event to readable streams which buffers any data until it encounters a "\n" | |
+// TODO add support for other newline systems (unix, DOS, macOS) | |
+exports.lines = function(stream) { | |
+ if (stream.readable) { | |
+ var buffer = ""; | |
+ var last = ""; | |
+ var res = stream.on("data", function(data) { | |
+ if(typeof(data) == 'string') { | |
+ for(var i=0 ; i<data.length ; ++i) { | |
+ if(data[i] !== "\n") { | |
+ if(data[i] !== "\r") { | |
+ buffer += data[i]; | |
+ } | |
+ } | |
+ else { | |
+ var line = buffer; | |
+ buffer = ""; | |
+ this.emit("line", line); | |
+ } | |
+ } | |
+ } | |
+ }); | |
+ } | |
+ stream.writeln = function(str) { | |
+ this.write(str + "\n"); | |
+ }; | |
+}; | |
+ | |
+ | |
/** | |
* Inherit the prototype methods from one constructor into another. | |
* | |
diff --git a/test/simple/test-stream-lines.js b/test/simple/test-stream-lines.js | |
new file mode 100644 | |
index 0000000..4066730 | |
--- /dev/null | |
+++ b/test/simple/test-stream-lines.js | |
@@ -0,0 +1,39 @@ | |
+#!/usr/bin/env node | |
+ | |
+var sys = require('sys'); | |
+var net = require('net'); | |
+ | |
+var server; | |
+server = net.createServer(function(socket) { | |
+ socket.setEncoding('utf8'); | |
+ sys.lines(socket); | |
+ socket.write("Hello World!"); | |
+ socket.write(" testing lines\n"); | |
+ socket.writeln("This is a line"); | |
+ socket.writeln("this is another line"); | |
+ for (var i = 0; i < 20; ++i) { | |
+ socket.write(""+i); | |
+ }; | |
+ var self = this; | |
+ setTimeout(function() { | |
+ socket.writeln("last line"); | |
+ socket.end(); | |
+ self.close(); | |
+ }, 1000); | |
+}).listen(9000); | |
+ | |
+var client = net.createConnection(9000); | |
+client.on('connect', function() { | |
+ this.setEncoding('utf8'); | |
+ sys.lines(client); | |
+}) | |
+client.on('data', function(data) { | |
+ sys.puts("new data: " + data); | |
+}); | |
+client.on('line', function(line) { | |
+ sys.puts('new line: ' + line); | |
+}); | |
+client.on('end', function() { | |
+ this.end(); | |
+}); | |
+ | |
-- | |
1.7.0.4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment