Permalink
Browse files

v0.2.1

  • Loading branch information...
1 parent 9a1b7f5 commit 48628811560448a172b7b418a3bb436cda52209f Gabriel Llamas committed Dec 9, 2012
Showing with 65 additions and 12 deletions.
  1. +4 −1 CHANGES
  2. +22 −2 README.md
  3. +9 −2 buffered-writer.js
  4. +2 −2 package.json
  5. +28 −5 tests/buffered-writer.js
View
@@ -1,4 +1,7 @@
-v0.2.0 (xx Dec 2012)
+v0.2.1 (09 Dec 2012)
+ An INVALID_DATA error is emitted if the data to write is invalid.
+
+v0.2.0 (09 Dec 2012)
Complete code revision and refactor.
Removed "touch()".
Added "open()".
View
@@ -5,7 +5,7 @@ _Node.js project_
#### Writes buffered data ####
-Version: 0.2.0
+Version: 0.2.1
Node.js streams are not buffered, that is, when you write data to them you're doing multiple I/O calls. This module buffers the data that has to be written to disk and eases the buffer manipulation when you need to write data from different nature (strings, numbers, arrays or buffers).
@@ -33,4 +33,24 @@ bw.open ("file")
#### Methods ####
-todo
+When you call to `open()` a stream a `Writer` instance is returned. This object inherits from `EventEmitter` and wraps a `WriteStream`. Only an `error` event is emitted. When this ocurrs, the `Writer` is closed automatically, you don't need to close it explicitly (if you try to do so you'll get another error, you cannot close twice).
+
+- [bw.open(file[, settings])](#open)
+- [Writer#close([callback])](#close)
+- [Writer#line()](#line)
+- [Writer#write(buffer[, offset[, length]])](#write)
+
+<a name="open"></a>
+__bw.open(file[, settings])__
+
+
+<a name="close"></a>
+__Writer#close([callback])__
+
+
+<a name="line"></a>
+__Writer#line()__
+
+
+<a name="write"></a>
+__Writer#write(buffer[, offset[, length]])__
View
@@ -11,6 +11,8 @@ errno.create (errno.getNextAvailableErrno (), "STREAM_CLOSED",
errno.create (errno.getNextAvailableErrno (), "INVALID_OFFSET_LENGTH",
"The offset or length parameters are not valid (offset={offset}, " +
"length={length}, length-offset<0).");
+errno.create (errno.getNextAvailableErrno (), "INVALID_DATA",
+ "The data can only be a Number, String, Array or Buffer");
var BUFFER_SIZE = 16384;
var EOL = process.platform === "win32"
@@ -138,9 +140,14 @@ Writer.prototype.write = function (buffer, offset, length){
}else if (Array.isArray (buffer)){
buffer = new Buffer (buffer);
length = length || buffer.length - offset;
- }else{
- //Buffer
+ }else if (Buffer.isBuffer (buffer)){
length = length || buffer.length - offset;
+ }else{
+ var me = this;
+ this.close (function (){
+ me._error (errno.get ("INVALID_DATA"));
+ });
+ return;
}
if (length < 0){
View
@@ -1,7 +1,7 @@
{
"name": "buffered-writer",
- "version": "0.2.0",
- "description": "Writes buffered data",
+ "version": "0.2.1",
+ "description": "Writes buffered data to files",
"keywords": ["buffer", "write", "stream", "file", "binary", "data"],
"author": {
"name": "Gabriel Llamas",
@@ -327,18 +327,41 @@ describe ("buffered-writer", function (){
var out = bw.open ("file");
//Hack
out._error = function (error){
- ASSERT.ok (error.code, "INVALID_OFFSET_LENGTH");
+ ASSERT.equal (error.code, "INVALID_OFFSET_LENGTH");
done ();
};
out.write ("asd", 5, -1);
});
+ it ("should emit an INVALID_DATA error if the data is not a Number, " +
+ "String, Array or Buffer",
+ function (done){
+ var out = bw.open ("file");
+ out.on ("error", function (error){
+ ASSERT.equal (error.code, "INVALID_DATA");
+ ASSERT.ok (out._closed);
+ done ();
+ }).write (function (){});
+ });
+
+ it ("should throw an INVALID_DATA exception if the data is not a " +
+ "Number, String, Array or Buffer and there's no error listener",
+ function (done){
+ var out = bw.open ("file");
+ //Hack
+ out._error = function (error){
+ ASSERT.equal (error.code, "INVALID_DATA");
+ done ();
+ };
+ out.write (function (){});
+ });
+
it ("should write data from multiple nature", function (done){
bw.open ("file")
.write ([0x00, 0x01, 0x02])
.write (new Buffer ([0x03, 0x04]), 1, 1)
.write (0x0506)
- .write ("a↑b", 1)
+ .write ("↑a", 1)
.close (function (){
FS.readFile ("file", function (error, data){
if (error) return done (error);
@@ -349,9 +372,9 @@ describe ("buffered-writer", function (){
data[3] === 0x04 &&
data[4] === 0x05 &&
data[5] === 0x06 &&
- data[6] === 0xe2 &&
- data[7] === 0x86 &&
- data[8] === 0x91
+ data[6] === 0x86 &&
+ data[7] === 0x91 &&
+ data[8] === 0x61
);
done ();
});

0 comments on commit 4862881

Please sign in to comment.