Skip to content

Instantly share code, notes, and snippets.

@mape
Created November 6, 2010 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mape/665446 to your computer and use it in GitHub Desktop.
Save mape/665446 to your computer and use it in GitHub Desktop.
Simple test to show that node-redis corrupts data if the data contains special chars.
var redis = require('redis-node');
var redcli = redis.createClient();
redcli.select(1);
var testObj = {'a': 'ö'}
var jsonString = JSON.stringify(testObj);
console.log('Origin JSON string: '+jsonString);
console.log('JSON.parse(jsonString): ');
console.log(JSON.parse(jsonString));
redcli.set('testJson', jsonString, function(err, status) {
if (err) throw err;
if (status) {
redcli.get('testJson', function(err, jsonStringFromRedis) {
if (err) throw err;
if (jsonStringFromRedis) {
console.log('\nRedis JSON string: '+jsonStringFromRedis);
try {
JSON.parse(jsonStringFromRedis);
} catch(e) {
console.log('JSON.parse(jsonStringFromRedis) fails since output is corrupted.\n');
}
}
});
}
});
// Ugly hack to format output easily.
setTimeout(function() {
var testStr = 'ä';
console.log('Origin test string: '+testStr);
redcli.set('testStr', testStr, function(err, status) {
if (err) throw err;
if (status) {
redcli.get('testStr', function(err, testStrFromRedis) {
if (err) throw err;
if (testStrFromRedis) {
console.log('Redis test string: '+testStrFromRedis);
}
});
}
});
}, 150);
Origin JSON string: {"a":"ö"}
JSON.parse(jsonString):
{ a: 'ö' }
Redis JSON string: {"a":"ö"
JSON.parse(jsonStringFromRedis) fails since output is corrupted.
Origin test string: ä
Redis test string: �
@bnoguchi
Copy link

Try this
In particular, the major change is:
var buf = new Buffer(32),
size = buf.write(testStr, 0, "utf8");
testStr = buf.slice(0, size);

I think testing for multi-byte characters on all incoming data to the libraray from INSIDE redis-node will sacrifice speed/performance.
It might be better for each user to determine the intent of the input string/buffer and act accordingly

Thoughts?

var redis = require('redis-node');
var redcli = redis.createClient();
redcli.select(1);

var testObj = {'a': 'ö'}
var jsonString = JSON.stringify(testObj);
console.log('Origin JSON string: '+jsonString);
console.log('JSON.parse(jsonString): ');
console.log(JSON.parse(jsonString));
redcli.set('testJson', jsonString, function(err, status) {
  if (err) throw err;

  if (status) {
    redcli.get('testJson', function(err, jsonStringFromRedis) {
      if (err) throw err;

      if (jsonStringFromRedis) {
        console.log('\nRedis JSON string: '+jsonStringFromRedis);
        try {
          JSON.parse(jsonStringFromRedis);
        } catch(e) {
          console.log('JSON.parse(jsonStringFromRedis) fails since output is corrupted.\n');
        }   
      }   
    }); 
  }
});

// Ugly hack to format output easily.
setTimeout(function() {
  var testStr = 'ä';
  console.log('Origin test string: '+testStr);

  var buf = new Buffer(32),
      size = buf.write(testStr, 0, "utf8");
  testStr = buf.slice(0, size);
  redcli.set('testStr', testStr, function(err, status) {
    if (err) throw err;

    if (status) {
      redcli.get('testStr', function(err, testStrFromRedis) {
        if (err) throw err;

        if (testStrFromRedis) {
          console.log('Redis test string: '+testStrFromRedis.toString("binary"));
        }   
      }); 
    }   
  }); 
}, 150);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment