Skip to content

Commit 8e92a0b

Browse files
committed
fix: skip config validation when using connector
This changeset fixes a problem in the original custom `connector` factory method implementation that required the `server` config property to be defined even though its value is ignored. Removing the validation when `config.options.connector` is defined fixes the problem. Ref: #1540 Fixes: #1541 Signed-off-by: Ruy Adorno <[email protected]>
1 parent e4eadf8 commit 8e92a0b

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

src/connection.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ class Connection extends EventEmitter {
10521052
throw new TypeError('The "config" argument is required and must be of type Object.');
10531053
}
10541054

1055-
if (typeof config.server !== 'string') {
1055+
if (typeof config.server !== 'string' && !config.options!.connector) {
10561056
throw new TypeError('The "config.server" property is required and must be of type string.');
10571057
}
10581058

@@ -1343,8 +1343,15 @@ class Connection extends EventEmitter {
13431343
if (typeof config.options.connector !== 'function') {
13441344
throw new TypeError('The "config.options.connector" property must be a function.');
13451345
}
1346+
if (config.server) {
1347+
throw new Error('Server and connector are mutually exclusive, but ' + config.server + ' and a connector function were provided');
1348+
}
1349+
if (config.options.port) {
1350+
throw new Error('Port and connector are mutually exclusive, but ' + config.options.port + ' and a connector function were provided');
1351+
}
13461352

13471353
this.config.options.connector = config.options.connector;
1354+
this.config.options.port = undefined;
13481355
}
13491356

13501357
if (config.options.cryptoCredentialsDetails !== undefined) {
@@ -1513,6 +1520,9 @@ class Connection extends EventEmitter {
15131520
this.config.options.fallbackToDefaultDb = config.options.fallbackToDefaultDb;
15141521
}
15151522

1523+
if (config.options.connector !== undefined) {
1524+
}
1525+
15161526
if (config.options.instanceName !== undefined) {
15171527
if (typeof config.options.instanceName !== 'string') {
15181528
throw new TypeError('The "config.options.instanceName" property must be of type string.');
@@ -1900,7 +1910,10 @@ class Connection extends EventEmitter {
19001910
initialiseConnection() {
19011911
const signal = this.createConnectTimer();
19021912

1903-
if (this.config.options.port) {
1913+
if (this.config.options.connector) {
1914+
// port and multiSubnetFailover are not used when using a custom connector
1915+
return this.connectOnPort(0, false, signal, this.config.options.connector);
1916+
} else if (this.config.options.port) {
19041917
return this.connectOnPort(this.config.options.port, this.config.options.multiSubnetFailover, signal, this.config.options.connector);
19051918
} else {
19061919
return instanceLookup({
@@ -1995,7 +2008,11 @@ class Connection extends EventEmitter {
19952008
this.socket = socket;
19962009

19972010
this.closed = false;
1998-
this.debug.log('connected to ' + this.config.server + ':' + this.config.options.port);
2011+
2012+
const connectedMsg = customConnector ?
2013+
'connected via custom connector' :
2014+
'connected to ' + this.config.server + ':' + this.config.options.port;
2015+
this.debug.log(connectedMsg);
19992016

20002017
this.sendPreLogin();
20012018
this.transitionTo(this.STATE.SENT_PRELOGIN);
@@ -2073,7 +2090,8 @@ class Connection extends EventEmitter {
20732090
*/
20742091
connectTimeout() {
20752092
const message = `Failed to connect to ${this.config.server}${this.config.options.port ? `:${this.config.options.port}` : `\\${this.config.options.instanceName}`} in ${this.config.options.connectTimeout}ms`;
2076-
this.debug.log(message);
2093+
const customConnectorMessage = `Failed to connect using custom connector in ${this.config.options.connectTimeout}ms`;
2094+
this.debug.log(this.config.options.connector ? customConnectorMessage : message);
20772095
this.emit('connect', new ConnectionError(message, 'ETIMEOUT'));
20782096
this.connectTimer = undefined;
20792097
this.dispatchEvent('connectTimeout');
@@ -2202,7 +2220,8 @@ class Connection extends EventEmitter {
22022220
socketError(error: Error) {
22032221
if (this.state === this.STATE.CONNECTING || this.state === this.STATE.SENT_TLSSSLNEGOTIATION) {
22042222
const message = `Failed to connect to ${this.config.server}:${this.config.options.port} - ${error.message}`;
2205-
this.debug.log(message);
2223+
const customConnectorMessage = `Failed to connect using custom connector - ${error.message}`;
2224+
this.debug.log(this.config.options.connector ? customConnectorMessage : message);
22062225
this.emit('connect', new ConnectionError(message, 'ESOCKET'));
22072226
} else {
22082227
const message = `Connection lost - ${error.message}`;
@@ -2228,15 +2247,21 @@ class Connection extends EventEmitter {
22282247
* @private
22292248
*/
22302249
socketClose() {
2231-
this.debug.log('connection to ' + this.config.server + ':' + this.config.options.port + ' closed');
2250+
const message = 'connection to ' + this.config.server + ':' + this.config.options.port + ' closed';
2251+
const customConnectorMessage = 'connection closed';
2252+
this.debug.log(this.config.options.connector ? customConnectorMessage : message);
22322253
if (this.state === this.STATE.REROUTING) {
2233-
this.debug.log('Rerouting to ' + this.routingData!.server + ':' + this.routingData!.port);
2254+
const message = 'Rerouting to ' + this.routingData!.server + ':' + this.routingData!.port;
2255+
const customConnectorMessage = 'Rerouting';
2256+
this.debug.log(this.config.options.connector ? customConnectorMessage : message);
22342257

22352258
this.dispatchEvent('reconnect');
22362259
} else if (this.state === this.STATE.TRANSIENT_FAILURE_RETRY) {
22372260
const server = this.routingData ? this.routingData.server : this.config.server;
22382261
const port = this.routingData ? this.routingData.port : this.config.options.port;
2239-
this.debug.log('Retry after transient failure connecting to ' + server + ':' + port);
2262+
const message = 'Retry after transient failure connecting to ' + server + ':' + port;
2263+
const customConnectorMessage = 'Retry after transient failure connecting';
2264+
this.debug.log(this.config.options.connector ? customConnectorMessage : message);
22402265

22412266
this.dispatchEvent('retry');
22422267
} else {

test/unit/custom-connector.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ describe('custom connector', function() {
2828
const host = server.address().address;
2929
const port = server.address().port;
3030
const connection = new Connection({
31-
server: host,
3231
options: {
3332
connector: async () => {
3433
customConnectorCalled = true;
@@ -37,7 +36,6 @@ describe('custom connector', function() {
3736
port,
3837
});
3938
},
40-
port
4139
},
4240
});
4341

@@ -59,4 +57,28 @@ describe('custom connector', function() {
5957

6058
connection.connect();
6159
});
60+
61+
it('should not define both server and connector options', function(done) {
62+
assert.throws(() => {
63+
new Connection({
64+
server: '0.0.0.0',
65+
options: {
66+
connector: async () => {},
67+
},
68+
});
69+
}, Error, 'Server and connector are mutually exclusive, but 0.0.0.0 and a connector function were provided');
70+
done();
71+
});
72+
73+
it('should not define both port and connector options', function(done) {
74+
assert.throws(() => {
75+
new Connection({
76+
options: {
77+
connector: async () => {},
78+
port: 8080,
79+
},
80+
});
81+
}, Error, 'Port and connector are mutually exclusive, but 8080 and a connector function were provided');
82+
done();
83+
});
6284
});

0 commit comments

Comments
 (0)