Skip to content

Commit

Permalink
v0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Llamas committed Apr 11, 2012
0 parents commit 636206d
Show file tree
Hide file tree
Showing 13 changed files with 296 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
doc
manage
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions build/buffered-reader.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions build/package.json
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions examples/binary.js
Original file line number Diff line number Diff line change
@@ -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 changes: 31 additions & 0 deletions examples/characters.js
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions examples/lorem ipsum
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions examples/lorem ipsum 2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
10 changes: 10 additions & 0 deletions examples/readLine.js
Original file line number Diff line number Diff line change
@@ -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 changes: 113 additions & 0 deletions src/buffered-reader.js
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions src/package.json
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions test/playground.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var BufferedReader = require ("../build/buffered-reader").BufferedReader;

0 comments on commit 636206d

Please sign in to comment.