Browse files

better error messages, renamed sync API

  • Loading branch information...
1 parent 1dd41b6 commit 609bda22ae2544c8bde14d99f207f0b323f0cee2 John Hewson committed Sep 30, 2011
Showing with 36 additions and 9 deletions.
  1. +6 −6 README.md
  2. +6 −0 lib/deflate.js
  3. +21 −0 src/deflate.cc
  4. +3 −3 test/node-deflate-test.js
View
12 README.md
@@ -23,17 +23,17 @@ You'll need to start with:
var deflate = require('deflate');
-### Buffer (Quick and Dirty)
+### Buffer (Synchronous)
Synchronously deflate a `Buffer` which contains all data to be compressed:
- var gzip = deflate.deflate(fs.ReadFileSync('example.txt'));
+ var gzip = deflate.deflateSync(fs.ReadFileSync('example.txt'));
Synchronously inflate a `Buffer` which contains all compressed data:
- var data = deflate.inflate(fs.ReadFileSync('example.gz'));
+ var data = deflate.inflateSync(fs.ReadFileSync('example.gz'));
-### Stream (Awesome)
+### Stream (Asynchronous)
Any `ReadableStream` can be wrapped with a `DeflateStream`, which we can `pipe` into a `WritableStream`.
@@ -63,11 +63,11 @@ the low-level API. All low-level functions are synchronous.
## Options
-### deflate(level=6)
+### deflate(buffer, level=6)
Deflates a gzip formatted `Buffer`. `level` sets the compression level from `0` (uncompressed) to `9` (highly compressed),
a smaller number is faster.
-### deflate(format='gzip', level=6)
+### deflate(buffer, format='gzip', level=6)
Deflates a buffer.
`format` can be `gzip`, `zlib`, or `deflate`.
View
6 lib/deflate.js
@@ -28,6 +28,9 @@ function DeflateStream(readStream, format, level, bufferSize) {
util.inherits(DeflateStream, Stream);
DeflateStream.prototype.write = function(data) {
+ if (!Buffer.isBuffer(data)) {
+ throw new Error('expected Buffer');
+ }
this.deflater.write(data);
while (this.deflater.deflate()) {
this.emit('data', this.deflater.read());
@@ -76,6 +79,9 @@ function InflateStream(readStream, format, level, bufferSize) {
util.inherits(InflateStream, Stream);
InflateStream.prototype.write = function(data) {
+ if (!Buffer.isBuffer(data)) {
+ throw new Error('expected Buffer');
+ }
this.inflater.write(data);
while (this.inflater.inflate()) {
this.emit('data', this.inflater.read());
View
21 src/deflate.cc
@@ -414,6 +414,15 @@ static Handle<Value> OnePassDeflate(const Arguments& args) {
}
}
}
+ else {
+ Local<Value> exception = Exception::TypeError(String::New("invalid arguments"));
+ return ThrowException(exception);
+ }
+
+ if (!Buffer::HasInstance(args[0])) {
+ Local<Value> exception = Exception::TypeError(String::New("expected Buffer"));
+ return ThrowException(exception);
+ }
Local<Object> inBuff = args[0]->ToObject();
char* in = Buffer::Data(inBuff);
@@ -498,6 +507,15 @@ static Handle<Value> OnePassInflate(const Arguments& args) {
return ThrowException(exception);
}
}
+ else {
+ Local<Value> exception = Exception::TypeError(String::New("invalid arguments"));
+ return ThrowException(exception);
+ }
+
+ if (!Buffer::HasInstance(args[0])) {
+ Local<Value> exception = Exception::TypeError(String::New("expected Buffer"));
+ return ThrowException(exception);
+ }
Local<Object> inBuff = args[0]->ToObject();
char* in = Buffer::Data(inBuff);
@@ -582,6 +600,9 @@ extern "C" {
Flater::Init(target);
NODE_SET_METHOD(target, "version", GetVersion);
+ NODE_SET_METHOD(target, "deflateSync", OnePassDeflate);
+ NODE_SET_METHOD(target, "inflateSync", OnePassInflate);
+ // deprecated:
NODE_SET_METHOD(target, "deflate", OnePassDeflate);
NODE_SET_METHOD(target, "inflate", OnePassInflate);
}
View
6 test/node-deflate-test.js
@@ -12,7 +12,7 @@ function testOnePassDeflate(format, extension, test) {
fs.unlinkSync(outPath);
}
- fs.writeFileSync(outPath, deflate.deflate(fs.readFileSync(inPath), format));
+ fs.writeFileSync(outPath, deflate.deflateSync(fs.readFileSync(inPath), format));
validateDeflate(test, extension);
test.done();
@@ -68,7 +68,7 @@ function testOnePassInflate(format, extension, test) {
fs.unlinkSync(outPath);
}
- fs.writeFileSync(outPath, deflate.inflate(fs.readFileSync(inPath), format));
+ fs.writeFileSync(outPath, deflate.inflateSync(fs.readFileSync(inPath), format));
validateInflate(test, extension);
test.done();
@@ -162,4 +162,4 @@ exports['streaming ZLIB inflate'] = function(test) {
exports['streaming DEFLATE inflate'] = function(test) {
testStreamingInflate('deflate', 'deflate', test);
-};
+};

0 comments on commit 609bda2

Please sign in to comment.