Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 1f90d16262cc18f16510eca0df8c8cb256203979 1 parent 8563d1c
David Björklund authored

Showing 3 changed files with 39 additions and 27 deletions. Show diff stats Hide diff stats

  1. +10 4 lib/snappy.js
  2. +21 11 src/binding.cc
  3. +8 12 test/all.js
14 lib/snappy.js
@@ -46,8 +46,11 @@ var parsers = exports.parsers = {
46 46 * JSON.stringify.
47 47 */
48 48 exports.compress = function(input, callback) {
49   - if (!(typeof input === 'string' || Buffer.isBuffer(input))) {
50   - input = JSON.stringify(input);
  49 + if(!Buffer.isBuffer(input)){
  50 + if (!(typeof input === 'string')) {
  51 + input = JSON.stringify(input);
  52 + }
  53 + input = new Buffer(input);
51 54 }
52 55 return binding.compress(input, callback);
53 56 };
@@ -58,8 +61,11 @@ exports.compress = function(input, callback) {
58 61 * JSON.stringify.
59 62 */
60 63 exports.compressSync = function(input, callback) {
61   - if (!(typeof input === 'string' || Buffer.isBuffer(input))) {
62   - input = JSON.stringify(input);
  64 + if(!Buffer.isBuffer(input)){
  65 + if (!(typeof input === 'string')) {
  66 + input = JSON.stringify(input);
  67 + }
  68 + input = new Buffer(input);
63 69 }
64 70 return binding.compressSync(input, callback);
65 71 };
32 src/binding.cc
@@ -25,10 +25,14 @@
25 25
26 26 #include <string.h> // memcpy
27 27
  28 +#include <string>
  29 +
28 30 namespace nodesnappy {
29 31 template<class T> SnappyRequest<T>::SnappyRequest(const v8::Arguments& args){
30   - v8::String::Utf8Value data(args[0]->ToString());
31   - input = std::string(*data, data.length());
  32 + v8::Local<v8::Object> object = args[0]->ToObject();
  33 + size_t length = node::Buffer::Length(object);
  34 + const char *data = node::Buffer::Data(object);
  35 + input = std::string(data, length);
32 36 v8::Local<v8::Function> local = v8::Local<v8::Function>::Cast(args[1]);
33 37 callback = v8::Persistent<v8::Function>::New(local);
34 38 err = NULL;
@@ -75,9 +79,9 @@ inline void
75 79 CompressUncompressBase::CallOkCallback(const v8::Handle<v8::Function>& callback,
76 80 const std::string& str) {
77 81 v8::Handle<v8::Value> err = v8::Local<v8::Value>::New(v8::Null());
78   - v8::Handle<v8::Value> res =
79   - node::Buffer::New(v8::String::New(str.data(), str.length()));
80   - CallCallback(callback, err, res);
  82 + node::Buffer* res = node::Buffer::New(str.length());
  83 + memcpy(node::Buffer::Data(res), str.c_str(), str.length());
  84 + CallCallback(callback, err, res->handle_);
81 85 }
82 86
83 87 // CompressBinding
@@ -92,9 +96,11 @@ v8::Handle<v8::Value> CompressBinding::Async(const v8::Arguments& args) {
92 96
93 97 v8::Handle<v8::Value> CompressBinding::Sync(const v8::Arguments& args) {
94 98 v8::HandleScope scope;
95   - v8::String::Utf8Value data(args[0]->ToString());
  99 + v8::Local<v8::Object> input = args[0]->ToObject();
  100 + size_t length = node::Buffer::Length(input);
  101 + char *data = node::Buffer::Data(input);
96 102 std::string dst;
97   - snappy::Compress(*data, data.length(), &dst);
  103 + snappy::Compress(data, length, &dst);
98 104 CallOkCallback(v8::Local<v8::Function>::Cast(args[1]), dst);
99 105 return scope.Close(v8::Undefined());
100 106 }
@@ -123,9 +129,11 @@ v8::Handle<v8::Value> UncompressBinding::Async(const v8::Arguments& args) {
123 129 v8::Handle<v8::Value> UncompressBinding::Sync(const v8::Arguments& args) {
124 130 v8::HandleScope scope;
125 131 std::string dst;
126   - v8::String::Utf8Value data(args[0]->ToString());
  132 + v8::Local<v8::Object> input = args[0]->ToObject();
  133 + size_t length = node::Buffer::Length(input);
  134 + char *data = node::Buffer::Data(input);
127 135 v8::Handle<v8::Function> callback = v8::Local<v8::Function>::Cast(args[1]);
128   - if (snappy::Uncompress(*data, data.length(), &dst)) {
  136 + if (snappy::Uncompress(data, length, &dst)) {
129 137 CallOkCallback(callback, dst);
130 138 } else {
131 139 CallErrCallback(callback, SnappyErrors::kInvalidInput);
@@ -164,8 +172,10 @@ v8::Handle<v8::Value>
164 172 IsValidCompressedBinding::Sync(const v8::Arguments& args) {
165 173 v8::HandleScope scope;
166 174 std::string dst;
167   - v8::String::Utf8Value data(args[0]->ToString());
168   - bool valid = snappy::IsValidCompressedBuffer(*data, data.length());
  175 + v8::Local<v8::Object> input = args[0]->ToObject();
  176 + size_t length = node::Buffer::Length(input);
  177 + char *data = node::Buffer::Data(input);
  178 + bool valid = snappy::IsValidCompressedBuffer(data, length);
169 179 CallOkCallback(v8::Local<v8::Function>::Cast(args[1]), valid);
170 180 return scope.Close(v8::Undefined());
171 181 }
20 test/all.js
@@ -24,17 +24,14 @@ var snappy = require('../lib/snappy');
24 24 var vows = require('vows');
25 25 var assert = require('assert');
26 26
27   -// Convenient helper methods
28   -assert.isBuffer = function(buf) {
29   - return assert.instanceOf(buf, Buffer);
30   -};
  27 +// Convenient helper method
31 28 assert.isError = function(err) {
32 29 return assert.instanceOf(err, Error);
33 30 };
34 31
35 32 // Test data
36 33 var string = "foo foo foo Fasfa daos asd foo foo foo asdasf bar bar aarr";
37   -var buffer = new Buffer(string);
  34 +var buffer = new Buffer([255, 200, 100, 3, 0, 256, 80]);
38 35 var json = {
39 36 "foo": "bar",
40 37 "fou": 0,
@@ -67,7 +64,7 @@ var json = {
67 64 return assert.isNull(err);
68 65 },
69 66 'should result in a buffer': function(err, compressed) {
70   - return assert.isBuffer(compressed);
  67 + return Buffer.isBuffer(compressed);
71 68 },
72 69 'and isValidCompressed': {
73 70 topic: function(compressed) {
@@ -91,7 +88,6 @@ var json = {
91 88 return assert.isString(result);
92 89 },
93 90 'should equal the original when parsed': function(err, result) {
94   - console.log("'", result, " ", buffer.toString("utf8"), "'");
95 91 return assert.strictEqual(result, buffer.toString("utf8"));
96 92 }
97 93 },
@@ -103,7 +99,7 @@ var json = {
103 99 return assert.isNull(err);
104 100 },
105 101 'should result in a buffer': function(err, result) {
106   - return assert.isBuffer(result);
  102 + return Buffer.isBuffer(result);
107 103 },
108 104 'should equal the original': function(err, result) {
109 105 return assert.strictEqual(result.toString("utf8"), buffer.toString("utf8"));
@@ -118,7 +114,7 @@ var json = {
118 114 return assert.isNull(err);
119 115 },
120 116 'should result in a buffer': function(err, compressed) {
121   - return assert.isBuffer(compressed);
  117 + return Buffer.isBuffer(compressed);
122 118 },
123 119 'and isValidCompressed': {
124 120 topic: function(compressed) {
@@ -167,7 +163,7 @@ var json = {
167 163 return assert.isNull(err);
168 164 },
169 165 'should result in a buffer': function(err, result) {
170   - return assert.isBuffer(result);
  166 + return Buffer.isBuffer(result);
171 167 },
172 168 'should equal the original when parsed': function(err, result) {
173 169 return assert.deepEqual(JSON.parse(result), json);
@@ -182,7 +178,7 @@ var json = {
182 178 return assert.isNull(err);
183 179 },
184 180 'should result in a buffer': function(err, compressed) {
185   - return assert.isBuffer(compressed);
  181 + return Buffer.isBuffer(compressed);
186 182 },
187 183 'and isValidCompressed': {
188 184 topic: function(compressed) {
@@ -217,7 +213,7 @@ var json = {
217 213 return assert.isNull(err);
218 214 },
219 215 'should result in a Buffer': function(err, result) {
220   - return assert.isBuffer(result);
  216 + return Buffer.isBuffer(result);
221 217 },
222 218 'should equal the original when parsed': function(err, result) {
223 219 var string2;

0 comments on commit 1f90d16

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