Created
November 6, 2010 14:01
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: � |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?