Skip to content

Commit d858bd8

Browse files
author
Ruben Bridgewater
committed
Passing a stream as second parameter in the constructor
To support private streams this is implemented. This needs some monkey patching to work, so it is not officially supported and might be removed at any time! Fixes #950 Closes #951
1 parent 2913eac commit d858bd8

File tree

2 files changed

+54
-21
lines changed

2 files changed

+54
-21
lines changed

index.js

+27-12
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,20 @@ function handle_detect_buffers_reply (reply, command, buffer_args) {
3434

3535
exports.debug_mode = /\bredis\b/i.test(process.env.NODE_DEBUG);
3636

37-
function RedisClient (options) {
37+
// Attention: The second parameter might be removed at will and is not officially supported.
38+
// Do not rely on this
39+
function RedisClient (options, stream) {
3840
// Copy the options so they are not mutated
3941
options = utils.clone(options);
4042
EventEmitter.call(this);
4143
var cnx_options = {};
4244
var self = this;
43-
if (options.path) {
45+
if (stream) {
46+
// The stream from the outside is used so no connection from this side is triggered but from the server this client should talk to
47+
// Reconnect etc won't work with this. This requires monkey patching to work, so it is not officially supported
48+
options.stream = stream;
49+
this.address = '"Private stream"';
50+
} else if (options.path) {
4451
cnx_options.path = options.path;
4552
this.address = options.path;
4653
} else {
@@ -174,17 +181,25 @@ RedisClient.connection_id = 0;
174181
RedisClient.prototype.create_stream = function () {
175182
var self = this;
176183

177-
// On a reconnect destroy the former stream and retry
178-
if (this.stream) {
179-
this.stream.removeAllListeners();
180-
this.stream.destroy();
181-
}
182-
183-
/* istanbul ignore if: travis does not work with stunnel atm. Therefor the tls tests are skipped on travis */
184-
if (this.options.tls) {
185-
this.stream = tls.connect(this.connection_options);
184+
if (this.options.stream) {
185+
// Only add the listeners once in case of a reconnect try (that won't work)
186+
if (this.stream) {
187+
return;
188+
}
189+
this.stream = this.options.stream;
186190
} else {
187-
this.stream = net.createConnection(this.connection_options);
191+
// On a reconnect destroy the former stream and retry
192+
if (this.stream) {
193+
this.stream.removeAllListeners();
194+
this.stream.destroy();
195+
}
196+
197+
/* istanbul ignore if: travis does not work with stunnel atm. Therefor the tls tests are skipped on travis */
198+
if (this.options.tls) {
199+
this.stream = tls.connect(this.connection_options);
200+
} else {
201+
this.stream = net.createConnection(this.connection_options);
202+
}
188203
}
189204

190205
if (this.options.connect_timeout) {

test/connection.spec.js

+27-9
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,38 @@ var config = require("./lib/config");
55
var helper = require('./helper');
66
var redis = config.redis;
77
var intercept = require('intercept-stdout');
8+
var net = require('net');
9+
var client;
810

911
describe("connection tests", function () {
10-
helper.allTests(function(parser, ip, args) {
1112

12-
describe("using " + parser + " and " + ip, function () {
13+
beforeEach(function () {
14+
client = null;
15+
});
16+
afterEach(function () {
17+
client.end(true);
18+
});
19+
20+
it('unofficially support for a private stream', function () {
21+
// While using a private stream, reconnection and other features are not going to work properly.
22+
// Besides that some functions also have to be monkey patched to be safe from errors in this case.
23+
// Therefor this is not officially supported!
24+
var socket = new net.Socket();
25+
client = new redis.RedisClient({
26+
prefix: 'test'
27+
}, socket);
28+
assert.strictEqual(client.stream, socket);
29+
assert.strictEqual(client.stream.listeners('error').length, 1);
30+
assert.strictEqual(client.address, '"Private stream"');
31+
// Pretent a reconnect event
32+
client.create_stream();
33+
assert.strictEqual(client.stream, socket);
34+
assert.strictEqual(client.stream.listeners('error').length, 1);
35+
});
1336

14-
var client;
37+
helper.allTests(function(parser, ip, args) {
1538

16-
beforeEach(function () {
17-
client = null;
18-
});
19-
afterEach(function () {
20-
client.end(true);
21-
});
39+
describe("using " + parser + " and " + ip, function () {
2240

2341
describe("on lost connection", function () {
2442
it("emit an error after max retry attempts and do not try to reconnect afterwards", function (done) {

0 commit comments

Comments
 (0)