Skip to content
Browse files

v0.0.2

  • Loading branch information...
0 parents commit 636206dcd25c2fef80d8f033872fc1b54431d51f Gabriel Llamas committed Apr 11, 2012
Showing with 296 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +21 −0 LICENSE
  3. +38 −0 README.md
  4. +1 −0 build/buffered-reader.js
  5. +22 −0 build/package.json
  6. +13 −0 examples/binary.js
  7. +31 −0 examples/characters.js
  8. +21 −0 examples/lorem ipsum
  9. +1 −0 examples/lorem ipsum 2
  10. +10 −0 examples/readLine.js
  11. +113 −0 src/buffered-reader.js
  12. +22 −0 src/package.json
  13. +1 −0 test/playground.js
2 .gitignore
@@ -0,0 +1,2 @@
+doc
+manage
21 LICENSE
@@ -0,0 +1,21 @@
+MIT License.
+
+Copyright (c) 2012. Gabriel Llamas.
+
+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.
38 README.md
@@ -0,0 +1,38 @@
+<a name="start"></a>
+
+Node BufferedReader
+===================
+
+#### Fully configurable buffered reader for node.js ####
+
+[Availability](#availability) | [Compatibility](#compatibility) | [Documentation](#documentation)
+
+Version: 0.0.2
+
+<a name="availability"></a>
+#### Availability [](#start) ####
+
+Via npm:
+
+```
+npm install buffered-reader
+```
+
+Or simply copying `build/buffered-reader.js` into your project's directory and `require()` accordingly.
+
+***
+
+<a name="compatibility"></a>
+#### Compatibility [](#start) ####
+
+✔ Node *
+
+***
+
+<a name="documentation"></a>
+#### Documentation [](#start) ####
+
+[Reference](https://github.com/Gagle/Node-BufferedReader/wiki/Reference)
+[Examples](https://github.com/Gagle/Node-BufferedReader/tree/master/examples)
+[Change log](https://github.com/Gagle/Node-BufferedReader/wiki/Change-log)
+[MIT License](https://github.com/Gagle/Node-BufferedReader/blob/master/LICENSE)
1 build/buffered-reader.js
@@ -0,0 +1 @@
+"use strict";var EVENTS=require("events"),FS=require("fs"),PATH=require("path"),BUFFER_SIZE=16384,SLASH=PATH.normalize("/"),getFileName=function(a){var b=process.mainModule.filename,c=b.substring(0,b.lastIndexOf(SLASH)),d=PATH.relative(process.cwd(),c);return PATH.join(d,a)},BufferedReader=function(a,b,c){EVENTS.EventEmitter.call(this);var d=arguments.length;d===1?(b=BUFFER_SIZE,c=null):d===2&&typeof b=="string"&&(c=b,b=BUFFER_SIZE),this._stream=FS.createReadStream(getFileName(a),{bufferSize:b,encoding:c});var e=this;this._stream.on("error",function(a){e.emit("error",a)})};BufferedReader.prototype=Object.create(EVENTS.EventEmitter.prototype),BufferedReader.prototype.constructor=BufferedReader,BufferedReader.prototype.read=function(){var a,b,c=this,d=this.listeners("character").length!==0||this.listeners("line").length!==0||this.listeners("byte").length!==0;this._stream.on("data",function(e){b=e;var f=0,g,h,i=e.length;if(d){for(var j=0;j<i;j++){h=e[j];if(!c._stream.encoding){c.emit("byte",h);continue}c.emit("character",h);var k=h==="\n"||h==="\r";k&&(g=e.slice(f,j++),f=j,a&&(g=a.concat(g),a=null),j+1!==i&&h==="\r"&&e[j+1]==="\n"&&j++,c.emit("line",g))}if(c._stream.encoding&&f!==i){var l=f===0?e:e.slice(f);a=a?a.concat(l):l}}c.emit("buffer",e)}),this._stream.on("end",function(){d&&a&&c.emit("line",a),c.emit("end")})},module.exports.BufferedReader=BufferedReader;
22 build/package.json
@@ -0,0 +1,22 @@
+{
+ "name": "buffered-reader",
+ "author": {
+ "name": "Gabriel Llamas"
+ },
+ "version": "0.0.2",
+ "description": "Fully configurable buffered reader.",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/Gagle/Node-BufferedReader.git"
+ },
+ "engines": {
+ "node": "*"
+ },
+ "keywords": ["buffer", "reader", "line", "read line", "read file", "read text file",
+ "file"],
+ "licenses": [{
+ "type": "MIT",
+ "url": "http://www.opensource.org/licenses/mit-license.html"
+ }],
+ "main": "buffered-reader"
+}
13 examples/binary.js
@@ -0,0 +1,13 @@
+var BufferedReader = require ("../build/buffered-reader").BufferedReader;
+
+new BufferedReader ("lorem ipsum 2")
+ .on ("error", function (error){
+ console.log ("error: " + error);
+ })
+ .on ("byte", function (b){
+ console.log ("byte: " + b);
+ })
+ .on ("buffer", function (buffer){
+ console.log ("buffer: " + buffer);
+ })
+ .read ();
31 examples/characters.js
@@ -0,0 +1,31 @@
+var BufferedReader = require ("../build/buffered-reader").BufferedReader;
+
+/**
+Two ways to read one character at a time:
+- using "buffer" event with a buffer size of 1. Don't do this, it's very inefficient, it's just
+ to give an example of a buffer event and size. Default size is 16KB.
+- using "character" event.
+*/
+
+console.log ("\"buffer\" event: ");
+
+new BufferedReader ("lorem ipsum 2", 1, "utf8")
+ .on ("error", function (error){
+ console.log ("error: " + error);
+ })
+ .on ("buffer", function (buffer){
+ console.log ("buffer: " + buffer);
+ })
+ .on ("end", function (){
+ console.log ("\n\"character\" event: ");
+
+ new BufferedReader ("lorem ipsum 2", "utf8")
+ .on ("error", function (error){
+ console.log ("error: " + error);
+ })
+ .on ("character", function (character){
+ console.log ("character: " + character);
+ })
+ .read ();
+ })
+ .read ();
21 examples/lorem ipsum
@@ -0,0 +1,21 @@
+Lorem ipsum dolor sit amet, consectetur adipiscing elit.
+Morbi convallis nibh massa, eu varius felis.
+
+Phasellus ultrices ligula sed odio ultricies egestas.
+Curabitur pretium magna in diam accumsan dignissim.
+Phasellus et tortor eu orci suscipit vehicula.
+Phasellus pulvinar mauris in purus consequat vel congue orci hendrerit.
+Pellentesque eget arcu magna, suscipit imperdiet eros.
+
+Fusce ut neque sit amet nibh pretium lobortis vitae sed magna.
+Etiam vel mauris sit amet metus blandit consectetur.
+
+Vestibulum eget velit vitae libero fringilla malesuada malesuada vitae urna.
+In at purus non mauris rutrum venenatis quis sed lorem.
+Nulla pharetra neque sit amet dui tristique ut sollicitudin enim ullamcorper.
+Etiam a purus sed ligula posuere convallis eget eget sem.
+Mauris tincidunt arcu sit amet quam pellentesque sodales.
+
+Aenean sed risus diam, id dapibus velit.
+Duis aliquet eros in nisi aliquet id tristique diam ultricies.
+Maecenas eu nunc diam, sit amet varius erat.
1 examples/lorem ipsum 2
@@ -0,0 +1 @@
+Lorem ipsum dolor sit amet, consectetur adipiscing elit.
10 examples/readLine.js
@@ -0,0 +1,10 @@
+var BufferedReader = require ("../build/buffered-reader").BufferedReader;
+
+new BufferedReader ("lorem ipsum", "utf8")
+ .on ("error", function (error){
+ console.log ("error: " + error);
+ })
+ .on ("line", function (line){
+ console.log ("line: " + line);
+ })
+ .read ();
113 src/buffered-reader.js
@@ -0,0 +1,113 @@
+/**
+ * @name BufferedReader.
+ * @description Fully configurable buffered reader for node.js.
+ *
+ * @author Gabriel Llamas
+ * @created 10/04/2012
+ * @modified 11/04/2012
+ * @version 0.0.2
+ */
+"use strict";
+
+var EVENTS = require ("events");
+var FS = require ("fs");
+var PATH = require ("path");
+
+var BUFFER_SIZE = 16384;
+var SLASH = PATH.normalize ("/");
+
+var getFileName = function (fileName){
+ var main = process.mainModule.filename;
+ var cwd = main.substring (0, main.lastIndexOf (SLASH));
+ var relative = PATH.relative (process.cwd (), cwd);
+ return PATH.join (relative, fileName);
+};
+
+var BufferedReader = function (fileName, bufferSize, encoding){
+ EVENTS.EventEmitter.call (this);
+
+ var argsLen = arguments.length;
+ if (argsLen === 1){
+ bufferSize = BUFFER_SIZE;
+ encoding = null;
+ }else if (argsLen === 2 && typeof bufferSize === "string"){
+ encoding = bufferSize;
+ bufferSize = BUFFER_SIZE;
+ }
+
+ this._stream = FS.createReadStream (getFileName (fileName), {
+ bufferSize: bufferSize,
+ encoding: encoding
+ });
+
+ var me = this;
+ this._stream.on ("error", function (error){
+ me.emit ("error", error);
+ });
+};
+
+BufferedReader.prototype = Object.create (EVENTS.EventEmitter.prototype);
+BufferedReader.prototype.constructor = BufferedReader;
+
+BufferedReader.prototype.read = function (){
+ var lastChunk;
+ var buffer;
+ var me = this;
+
+ var loop = this.listeners ("character").length !== 0 || this.listeners ("line").length !== 0 ||
+ this.listeners ("byte").length !== 0;
+
+ this._stream.on ("data", function (data){
+ buffer = data;
+ var offset = 0;
+ var chunk;
+ var character;
+ var len = data.length;
+
+ if (loop){
+ for (var i=0; i<len; i++){
+ character = data[i];
+ if (me._stream.encoding){
+ me.emit ("character", character);
+ }else{
+ me.emit ("byte", character);
+ continue;
+ }
+
+ var eol = character === "\n" || character === "\r";
+ if (eol){
+ chunk = data.slice (offset, i++);
+ offset = i;
+
+ if (lastChunk){
+ chunk = lastChunk.concat (chunk);
+ lastChunk = null;
+ }
+
+ if (i+1 !== len && character === "\r" && data[i+1] === "\n"){
+ i++;
+ }
+
+ me.emit ("line", chunk);
+ }
+ }
+
+ if (me._stream.encoding && offset !== len){
+ var s = offset === 0 ? data : data.slice (offset);
+ lastChunk = lastChunk ? lastChunk.concat (s) : s;
+ }
+ }
+
+ me.emit ("buffer", data);
+ });
+
+ this._stream.on ("end", function (){
+ if (loop && lastChunk){
+ me.emit ("line", lastChunk);
+ }
+
+ me.emit ("end");
+ });
+};
+
+module.exports.BufferedReader = BufferedReader;
22 src/package.json
@@ -0,0 +1,22 @@
+{
+ "name": "buffered-reader",
+ "author": {
+ "name": "Gabriel Llamas"
+ },
+ "version": "0.0.2",
+ "description": "Fully configurable buffered reader.",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/Gagle/Node-BufferedReader.git"
+ },
+ "engines": {
+ "node": "*"
+ },
+ "keywords": ["buffer", "reader", "line", "read line", "read file", "read text file",
+ "file"],
+ "licenses": [{
+ "type": "MIT",
+ "url": "http://www.opensource.org/licenses/mit-license.html"
+ }],
+ "main": "buffered-reader"
+}
1 test/playground.js
@@ -0,0 +1 @@
+var BufferedReader = require ("../build/buffered-reader").BufferedReader;

0 comments on commit 636206d

Please sign in to comment.
Something went wrong with that request. Please try again.