Skip to content

Commit 3704ebd

Browse files
committed
Merge pull request #1021 from NodeRedis/string_numbers
Add string_numbers option to handle very big numbers Fixes #994 Fixes #339
2 parents 2e68a7a + 0c5947b commit 3704ebd

File tree

8 files changed

+63
-90
lines changed

8 files changed

+63
-90
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ If the redis server runs on the same machine as the client consider using unix s
184184
* `path`: *null*; The unix socket string to connect to
185185
* `url`: *null*; The redis url to connect to (`[redis:]//[user][:password@][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]` For more info check [IANA](http://www.iana.org/assignments/uri-schemes/prov/redis))
186186
* `parser`: *hiredis*; Which Redis protocol reply parser to use. If `hiredis` is not installed it will fallback to `javascript`.
187+
* `string_numbers`: *boolean*; pass true to get numbers back as strings instead of js numbers. This is necessary if you want to handle big numbers (above `Number.MAX_SAFE_INTEGER` === 2^53). If passed, the js parser is automatically choosen as parser no matter if the parser is set to hiredis or not, as hiredis is not capable of doing this.
187188
* `return_buffers`: *false*; If set to `true`, then all replies will be sent to callbacks as Buffers instead of Strings.
188189
* `detect_buffers`: *false*; If set to `true`, then replies will be sent to callbacks as Buffers. Please be aware that this can't work properly with the pubsub mode. A subscriber has to either always return strings or buffers.
189190
if any of the input arguments to the original command were Buffers.

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Features
99
- All commands that were send after a connection loss are now going to be send after reconnecting
1010
- Activating monitor mode does now work together with arbitrary commands including pub sub mode
1111
- Pub sub mode is completly rewritten and all known issues fixed
12+
- Added `string_numbers` option to get back strings instead of numbers
1213

1314
Bugfixes
1415

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ function RedisClient (options, stream) {
147147
returnReply: function (data) {
148148
self.return_reply(data);
149149
},
150-
returnError: function (data) {
151-
self.return_error(data);
150+
returnError: function (err) {
151+
self.return_error(err);
152152
},
153153
returnFatalError: function (err) {
154154
// Error out all fired commands. Otherwise they might rely on faulty data. We have to reconnect to get in a working state again
@@ -157,7 +157,8 @@ function RedisClient (options, stream) {
157157
self.return_error(err);
158158
},
159159
returnBuffers: this.buffers,
160-
name: options.parser
160+
name: options.parser,
161+
stringNumbers: options.string_numbers
161162
});
162163
this.create_stream();
163164
// The listeners will not be attached right away, so let's print the deprecation message while the listener is attached

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
},
2727
"dependencies": {
2828
"double-ended-queue": "^2.1.0-0",
29-
"redis-commands": "^1.0.1",
30-
"redis-parser": "^1.1.0"
29+
"redis-commands": "^1.1.0",
30+
"redis-parser": "^1.2.0"
3131
},
3232
"engines": {
3333
"node": ">=0.10.0"

test/commands/incr.spec.js

Lines changed: 46 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -10,101 +10,65 @@ describe("The 'incr' method", function () {
1010
helper.allTests(function (parser, ip, args) {
1111

1212
describe('using ' + parser + ' and ' + ip, function () {
13-
var key = 'sequence';
1413

15-
describe('when not connected', function () {
16-
var client;
14+
describe('when connected and a value in Redis', function () {
1715

18-
beforeEach(function (done) {
19-
client = redis.createClient.apply(null, args);
20-
client.once('ready', function () {
21-
client.set(key, '9007199254740992', function (err, res) {
22-
helper.isNotError()(err, res);
23-
client.quit();
24-
});
25-
});
26-
client.on('end', done);
27-
});
16+
var client;
17+
var key = 'ABOVE_SAFE_JAVASCRIPT_INTEGER';
18+
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991; // Backwards compatible
2819

2920
afterEach(function () {
3021
client.end(true);
3122
});
3223

33-
it('reports an error', function (done) {
34-
client.incr(function (err, res) {
35-
assert(err.message.match(/The connection has already been closed/));
36-
done();
37-
});
38-
});
39-
});
40-
41-
describe('when connected and a value in Redis', function () {
42-
var client;
43-
44-
before(function (done) {
45-
/*
46-
9007199254740992 -> 9007199254740992
47-
9007199254740993 -> 9007199254740992
48-
9007199254740994 -> 9007199254740994
49-
9007199254740995 -> 9007199254740996
50-
9007199254740996 -> 9007199254740996
51-
9007199254740997 -> 9007199254740996
52-
*/
24+
/*
25+
Number.MAX_SAFE_INTEGER === Math.pow(2, 53) - 1 === 9007199254740991
26+
27+
9007199254740992 -> 9007199254740992
28+
9007199254740993 -> 9007199254740992
29+
9007199254740994 -> 9007199254740994
30+
9007199254740995 -> 9007199254740996
31+
9007199254740996 -> 9007199254740996
32+
9007199254740997 -> 9007199254740996
33+
...
34+
*/
35+
it('count above the safe integers as numbers', function (done) {
5336
client = redis.createClient.apply(null, args);
54-
client.once('error', done);
55-
client.once('ready', function () {
56-
client.set(key, '9007199254740992', function (err, res) {
57-
helper.isNotError()(err, res);
58-
done();
59-
});
60-
});
61-
});
62-
63-
after(function () {
64-
client.end(true);
65-
});
66-
67-
it('changes the last digit from 2 to 3', function (done) {
37+
// Set a value to the maximum safe allowed javascript number (2^53) - 1
38+
client.set(key, MAX_SAFE_INTEGER, helper.isNotError());
39+
client.INCR(key, helper.isNumber(MAX_SAFE_INTEGER + 1));
40+
client.INCR(key, helper.isNumber(MAX_SAFE_INTEGER + 2));
41+
client.INCR(key, helper.isNumber(MAX_SAFE_INTEGER + 3));
42+
client.INCR(key, helper.isNumber(MAX_SAFE_INTEGER + 4));
43+
client.INCR(key, helper.isNumber(MAX_SAFE_INTEGER + 5));
6844
client.INCR(key, function (err, res) {
69-
helper.isString('9007199254740993')(err, res);
70-
done(err);
45+
helper.isNumber(MAX_SAFE_INTEGER + 6)(err, res);
46+
assert.strictEqual(typeof res, 'number');
7147
});
48+
client.INCR(key, helper.isNumber(MAX_SAFE_INTEGER + 7));
49+
client.INCR(key, helper.isNumber(MAX_SAFE_INTEGER + 8));
50+
client.INCR(key, helper.isNumber(MAX_SAFE_INTEGER + 9));
51+
client.INCR(key, helper.isNumber(MAX_SAFE_INTEGER + 10, done));
7252
});
7353

74-
describe('and we call it again', function () {
75-
it('changes the last digit from 3 to 4', function (done) {
76-
client.incr(key, function (err, res) {
77-
helper.isString('9007199254740994')(err, res);
78-
done(err);
79-
});
80-
});
81-
82-
describe('and again', function () {
83-
it('changes the last digit from 4 to 5', function (done) {
84-
client.incr(key, function (err, res) {
85-
helper.isString('9007199254740995')(err, res);
86-
done(err);
87-
});
88-
});
89-
90-
describe('and again', function () {
91-
it('changes the last digit from 5 to 6', function (done) {
92-
client.incr(key, function (err, res) {
93-
helper.isString('9007199254740996')(err, res);
94-
done(err);
95-
});
96-
});
97-
98-
describe('and again', function () {
99-
it('changes the last digit from 6 to 7', function (done) {
100-
client.incr(key, function (err, res) {
101-
helper.isString('9007199254740997')(err, res);
102-
done(err);
103-
});
104-
});
105-
});
106-
});
54+
it('count above the safe integers as strings', function (done) {
55+
args[2].string_numbers = true;
56+
client = redis.createClient.apply(null, args);
57+
// Set a value to the maximum safe allowed javascript number (2^53)
58+
client.set(key, MAX_SAFE_INTEGER, helper.isNotError());
59+
client.incr(key, helper.isString('9007199254740992'));
60+
client.incr(key, helper.isString('9007199254740993'));
61+
client.incr(key, helper.isString('9007199254740994'));
62+
client.incr(key, helper.isString('9007199254740995'));
63+
client.incr(key, helper.isString('9007199254740996'));
64+
client.incr(key, function (err, res) {
65+
helper.isString('9007199254740997')(err, res);
66+
assert.strictEqual(typeof res, 'string');
10767
});
68+
client.incr(key, helper.isString('9007199254740998'));
69+
client.incr(key, helper.isString('9007199254740999'));
70+
client.incr(key, helper.isString('9007199254741000'));
71+
client.incr(key, helper.isString('9007199254741001', done));
10872
});
10973
});
11074
});

test/commands/script.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("The 'script' method", function () {
3434
});
3535

3636
it('allows a loaded script to be evaluated', function (done) {
37-
client.evalsha(commandSha, 0, helper.isString('99', done));
37+
client.evalsha(commandSha, 0, helper.isNumber(99, done));
3838
});
3939

4040
it('allows a script to be loaded as part of a chained transaction', function (done) {

test/detect_buffers.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ describe('detect_buffers', function () {
3838
});
3939

4040
it('returns a string when executed as part of transaction', function (done) {
41-
client.multi().get('string key 1').exec(helper.isString('string value', done));
41+
client.multi().get('string key 1').exec(function (err, res) {
42+
helper.isString('string value', done)(err, res[0]);
43+
});
4244
});
4345
});
4446

test/helper.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@ module.exports = {
5959
};
6060
},
6161
isString: function (str, done) {
62+
str = '' + str; // Make sure it's a string
6263
return function (err, results) {
6364
assert.strictEqual(null, err, "expected string '" + str + "', got error: " + err);
64-
assert.equal(str, results, str + ' does not match ' + results);
65+
if (Buffer.isBuffer(results)) { // If options are passed to return either strings or buffers...
66+
results = results.toString();
67+
}
68+
assert.strictEqual(str, results, str + ' does not match ' + results);
6569
if (done) done();
6670
};
6771
},

0 commit comments

Comments
 (0)