-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add connector factory method (#1540)
This adds a new `connector` config option that may be used to define a custom socket factory method, providing much more flexible control of the socket connection creation.
- Loading branch information
Showing
3 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var net = require('net'); | ||
var Connection = require('../lib/tedious').Connection; | ||
|
||
var config = { | ||
server: '192.168.1.212', | ||
authentication: { | ||
type: 'default', | ||
options: { | ||
userName: 'test', | ||
password: 'test' | ||
} | ||
}, | ||
options: { | ||
connector: async () => net.connect({ | ||
host: '192.168.1.212', | ||
port: 1433, | ||
}) | ||
} | ||
}; | ||
|
||
const connection = new Connection(config); | ||
|
||
connection.connect((err) => { | ||
if (err) { | ||
console.log('Connection Failed'); | ||
throw err; | ||
} | ||
|
||
console.log('Custom connection Succeeded'); | ||
}); |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const net = require('net'); | ||
const assert = require('chai').assert; | ||
|
||
const { Connection } = require('../../src/tedious'); | ||
|
||
describe('custom connector', function() { | ||
let server; | ||
|
||
beforeEach(function(done) { | ||
server = net.createServer(); | ||
server.listen(0, '127.0.0.1', done); | ||
}); | ||
|
||
afterEach(() => { | ||
server.close(); | ||
}); | ||
|
||
it('connection using a custom connector', function(done) { | ||
let attemptedConnection = false; | ||
let customConnectorCalled = false; | ||
|
||
server.on('connection', async (connection) => { | ||
attemptedConnection = true; | ||
// no need to test auth/login, just end the connection sooner | ||
connection.end(); | ||
}); | ||
|
||
const host = server.address().address; | ||
const port = server.address().port; | ||
const connection = new Connection({ | ||
server: host, | ||
options: { | ||
connector: async () => { | ||
customConnectorCalled = true; | ||
return net.connect({ | ||
host, | ||
port, | ||
}); | ||
}, | ||
port | ||
}, | ||
}); | ||
|
||
connection.on('end', (err) => { | ||
// validates the connection was stablished using the custom connector | ||
assert.isOk(attemptedConnection); | ||
assert.isOk(customConnectorCalled); | ||
|
||
connection.close(); | ||
done(); | ||
}); | ||
|
||
connection.on('error', (err) => { | ||
// Connection lost errors are expected due to ending connection sooner | ||
if (!/Connection lost/.test(err)) { | ||
throw err; | ||
} | ||
}); | ||
|
||
connection.connect(); | ||
}); | ||
}); |