Permalink
Browse files

Handle non utf8-data correctly (closes #5, and possibly also #6)

  • Loading branch information...
1 parent 8563d1c commit 1f90d16262cc18f16510eca0df8c8cb256203979 David Björklund committed Sep 8, 2011
Showing with 39 additions and 27 deletions.
  1. +10 −4 lib/snappy.js
  2. +21 −11 src/binding.cc
  3. +8 −12 test/all.js
View
14 lib/snappy.js
@@ -46,8 +46,11 @@ var parsers = exports.parsers = {
* JSON.stringify.
*/
exports.compress = function(input, callback) {
- if (!(typeof input === 'string' || Buffer.isBuffer(input))) {
- input = JSON.stringify(input);
+ if(!Buffer.isBuffer(input)){
+ if (!(typeof input === 'string')) {
+ input = JSON.stringify(input);
+ }
+ input = new Buffer(input);
}
return binding.compress(input, callback);
};
@@ -58,8 +61,11 @@ exports.compress = function(input, callback) {
* JSON.stringify.
*/
exports.compressSync = function(input, callback) {
- if (!(typeof input === 'string' || Buffer.isBuffer(input))) {
- input = JSON.stringify(input);
+ if(!Buffer.isBuffer(input)){
+ if (!(typeof input === 'string')) {
+ input = JSON.stringify(input);
+ }
+ input = new Buffer(input);
}
return binding.compressSync(input, callback);
};
View
32 src/binding.cc
@@ -25,10 +25,14 @@
#include <string.h> // memcpy
+#include <string>
+
namespace nodesnappy {
template<class T> SnappyRequest<T>::SnappyRequest(const v8::Arguments& args){
- v8::String::Utf8Value data(args[0]->ToString());
- input = std::string(*data, data.length());
+ v8::Local<v8::Object> object = args[0]->ToObject();
+ size_t length = node::Buffer::Length(object);
+ const char *data = node::Buffer::Data(object);
+ input = std::string(data, length);
v8::Local<v8::Function> local = v8::Local<v8::Function>::Cast(args[1]);
callback = v8::Persistent<v8::Function>::New(local);
err = NULL;
@@ -75,9 +79,9 @@ inline void
CompressUncompressBase::CallOkCallback(const v8::Handle<v8::Function>& callback,
const std::string& str) {
v8::Handle<v8::Value> err = v8::Local<v8::Value>::New(v8::Null());
- v8::Handle<v8::Value> res =
- node::Buffer::New(v8::String::New(str.data(), str.length()));
- CallCallback(callback, err, res);
+ node::Buffer* res = node::Buffer::New(str.length());
+ memcpy(node::Buffer::Data(res), str.c_str(), str.length());
+ CallCallback(callback, err, res->handle_);
}
// CompressBinding
@@ -92,9 +96,11 @@ v8::Handle<v8::Value> CompressBinding::Async(const v8::Arguments& args) {
v8::Handle<v8::Value> CompressBinding::Sync(const v8::Arguments& args) {
v8::HandleScope scope;
- v8::String::Utf8Value data(args[0]->ToString());
+ v8::Local<v8::Object> input = args[0]->ToObject();
+ size_t length = node::Buffer::Length(input);
+ char *data = node::Buffer::Data(input);
std::string dst;
- snappy::Compress(*data, data.length(), &dst);
+ snappy::Compress(data, length, &dst);
CallOkCallback(v8::Local<v8::Function>::Cast(args[1]), dst);
return scope.Close(v8::Undefined());
}
@@ -123,9 +129,11 @@ v8::Handle<v8::Value> UncompressBinding::Async(const v8::Arguments& args) {
v8::Handle<v8::Value> UncompressBinding::Sync(const v8::Arguments& args) {
v8::HandleScope scope;
std::string dst;
- v8::String::Utf8Value data(args[0]->ToString());
+ v8::Local<v8::Object> input = args[0]->ToObject();
+ size_t length = node::Buffer::Length(input);
+ char *data = node::Buffer::Data(input);
v8::Handle<v8::Function> callback = v8::Local<v8::Function>::Cast(args[1]);
- if (snappy::Uncompress(*data, data.length(), &dst)) {
+ if (snappy::Uncompress(data, length, &dst)) {
CallOkCallback(callback, dst);
} else {
CallErrCallback(callback, SnappyErrors::kInvalidInput);
@@ -164,8 +172,10 @@ v8::Handle<v8::Value>
IsValidCompressedBinding::Sync(const v8::Arguments& args) {
v8::HandleScope scope;
std::string dst;
- v8::String::Utf8Value data(args[0]->ToString());
- bool valid = snappy::IsValidCompressedBuffer(*data, data.length());
+ v8::Local<v8::Object> input = args[0]->ToObject();
+ size_t length = node::Buffer::Length(input);
+ char *data = node::Buffer::Data(input);
+ bool valid = snappy::IsValidCompressedBuffer(data, length);
CallOkCallback(v8::Local<v8::Function>::Cast(args[1]), valid);
return scope.Close(v8::Undefined());
}
View
20 test/all.js
@@ -24,17 +24,14 @@ var snappy = require('../lib/snappy');
var vows = require('vows');
var assert = require('assert');
-// Convenient helper methods
-assert.isBuffer = function(buf) {
- return assert.instanceOf(buf, Buffer);
-};
+// Convenient helper method
assert.isError = function(err) {
return assert.instanceOf(err, Error);
};
// Test data
var string = "foo foo foo Fasfa daos asd foo foo foo asdasf bar bar aarr";
-var buffer = new Buffer(string);
+var buffer = new Buffer([255, 200, 100, 3, 0, 256, 80]);
var json = {
"foo": "bar",
"fou": 0,
@@ -67,7 +64,7 @@ var json = {
return assert.isNull(err);
},
'should result in a buffer': function(err, compressed) {
- return assert.isBuffer(compressed);
+ return Buffer.isBuffer(compressed);
},
'and isValidCompressed': {
topic: function(compressed) {
@@ -91,7 +88,6 @@ var json = {
return assert.isString(result);
},
'should equal the original when parsed': function(err, result) {
- console.log("'", result, " ", buffer.toString("utf8"), "'");
return assert.strictEqual(result, buffer.toString("utf8"));
}
},
@@ -103,7 +99,7 @@ var json = {
return assert.isNull(err);
},
'should result in a buffer': function(err, result) {
- return assert.isBuffer(result);
+ return Buffer.isBuffer(result);
},
'should equal the original': function(err, result) {
return assert.strictEqual(result.toString("utf8"), buffer.toString("utf8"));
@@ -118,7 +114,7 @@ var json = {
return assert.isNull(err);
},
'should result in a buffer': function(err, compressed) {
- return assert.isBuffer(compressed);
+ return Buffer.isBuffer(compressed);
},
'and isValidCompressed': {
topic: function(compressed) {
@@ -167,7 +163,7 @@ var json = {
return assert.isNull(err);
},
'should result in a buffer': function(err, result) {
- return assert.isBuffer(result);
+ return Buffer.isBuffer(result);
},
'should equal the original when parsed': function(err, result) {
return assert.deepEqual(JSON.parse(result), json);
@@ -182,7 +178,7 @@ var json = {
return assert.isNull(err);
},
'should result in a buffer': function(err, compressed) {
- return assert.isBuffer(compressed);
+ return Buffer.isBuffer(compressed);
},
'and isValidCompressed': {
topic: function(compressed) {
@@ -217,7 +213,7 @@ var json = {
return assert.isNull(err);
},
'should result in a Buffer': function(err, result) {
- return assert.isBuffer(result);
+ return Buffer.isBuffer(result);
},
'should equal the original when parsed': function(err, result) {
var string2;

0 comments on commit 1f90d16

Please sign in to comment.