Skip to content

Commit

Permalink
fix: port is ignored when path set to null
Browse files Browse the repository at this point in the history
Close #668
  • Loading branch information
luin committed Aug 2, 2018
1 parent 5eb4198 commit d40a99e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
19 changes: 12 additions & 7 deletions lib/connectors/StandaloneConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {CONNECTION_CLOSED_ERROR_MSG} from '../utils'
import AbstractConnector, {ErrorEmitter} from './AbstractConnector'

export function isIIpcConnectionOptions (value: any): value is IIpcConnectionOptions {
return value.hasOwnProperty('path')
return value.path
}

export interface ITcpConnectionOptions extends TcpNetConnectOpts {
Expand All @@ -30,17 +30,22 @@ export default class StandaloneConnector extends AbstractConnector {
path: options.path
}
} else {
connectionOptions = {
port: options.port,
host: options.host,
family: options.family
connectionOptions = {}
if (options.port != null) {
connectionOptions.port = options.port
}
if (options.host != null) {
connectionOptions.host = options.host
}
if (options.family != null) {
connectionOptions.family = options.family
}
}

if (options.tls) {
Object.assign(connectionOptions, options.tls)
}

process.nextTick(() => {
if (!this.connecting) {
callback(new Error(CONNECTION_CLOSED_ERROR_MSG))
Expand All @@ -63,4 +68,4 @@ export default class StandaloneConnector extends AbstractConnector {
callback(null, stream)
})
}
}
}
19 changes: 16 additions & 3 deletions test/unit/connectors/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,30 @@ describe('StandaloneConnector', function () {
stub(net, 'createConnection');
var connector = new StandaloneConnector({ port: 6379, path: '/tmp' });
connector.connect(function () {
net.createConnection.calledWith({ path: '/tmp' });
expect(net.createConnection.calledOnce).to.eql(true);
expect(net.createConnection.firstCall.args[0]).to.eql({path: '/tmp'});
net.createConnection.restore();
done();
});
});

it('ignore path when port is set and path is null', function (done) {
stub(net, 'createConnection');
var connector = new StandaloneConnector({ port: 6379, path: null });
connector.connect(function () {
expect(net.createConnection.calledOnce).to.eql(true);
expect(net.createConnection.firstCall.args[0]).to.eql({port: 6379});
net.createConnection.restore();
done();
});
});

it('supports tls', function (done) {
stub(tls, 'connect');
var connector = new StandaloneConnector({ port: 6379, tls: 'on' });
var connector = new StandaloneConnector({ port: 6379, tls: {ca: 'on'} });
connector.connect(function () {
tls.connect.calledWith({ port: 6379, tls: 'on' });
expect(tls.connect.calledOnce).to.eql(true);
expect(tls.connect.firstCall.args[0]).to.eql({ port: 6379, ca: 'on' });
tls.connect.restore();
done();
});
Expand Down

0 comments on commit d40a99e

Please sign in to comment.