Skip to content

Commit

Permalink
Merge branch 'master' into NewTLSSupport
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurschreiber committed Jun 7, 2023
2 parents f2e45ad + ed02374 commit a6d414a
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 20 deletions.
23 changes: 17 additions & 6 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,38 +194,49 @@ jobs:
run: |
Push-Location C:\temp
$exe_link, $box_link, $update_link = '', '', ''
$exe_link, $box_link, $update_link, $update_download_link = '', '', '', ''
switch (${{ matrix.mssql-version }}) {
'2022' {
$exe_link = 'https://download.microsoft.com/download/3/8/d/38de7036-2433-4207-8eae-06e247e17b25/SQLServer2022-DEV-x64-ENU.exe'
$box_link = 'https://download.microsoft.com/download/3/8/d/38de7036-2433-4207-8eae-06e247e17b25/SQLServer2022-DEV-x64-ENU.box'
$update_link = 'https://download.microsoft.com/download/9/6/8/96819b0c-c8fb-4b44-91b5-c97015bbda9f/SQLServer2022-KB5023127-x64.exe'
$update_link = 'https://www.microsoft.com/en-us/download/confirmation.aspx?id=105013'
}
'2019' {
$exe_link = 'https://download.microsoft.com/download/8/4/c/84c6c430-e0f5-476d-bf43-eaaa222a72e0/SQLServer2019-DEV-x64-ENU.exe'
$box_link = 'https://download.microsoft.com/download/8/4/c/84c6c430-e0f5-476d-bf43-eaaa222a72e0/SQLServer2019-DEV-x64-ENU.box'
$update_link = 'https://download.microsoft.com/download/6/e/7/6e72dddf-dfa4-4889-bc3d-e5d3a0fd11ce/SQLServer2019-KB5023049-x64.exe'
$update_link = 'https://www.microsoft.com/en-us/download/confirmation.aspx?id=100809'
}
'2017' {
$exe_link = 'https://download.microsoft.com/download/E/F/2/EF23C21D-7860-4F05-88CE-39AA114B014B/SQLServer2017-DEV-x64-ENU.exe'
$box_link = 'https://download.microsoft.com/download/E/F/2/EF23C21D-7860-4F05-88CE-39AA114B014B/SQLServer2017-DEV-x64-ENU.box'
$update_link = 'https://download.microsoft.com/download/C/4/F/C4F908C9-98ED-4E5F-88D5-7D6A5004AEBD/SQLServer2017-KB5016884-x64.exe'
$update_link = 'https://www.microsoft.com/en-us/download/confirmation.aspx?id=56128'
}
'2016' {
$exe_link = 'https://download.microsoft.com/download/4/1/A/41AD6EDE-9794-44E3-B3D5-A1AF62CD7A6F/sql16_sp2_dlc/en-us/SQLServer2016SP2-FullSlipstream-DEV-x64-ENU.exe'
$box_link = 'https://download.microsoft.com/download/4/1/A/41AD6EDE-9794-44E3-B3D5-A1AF62CD7A6F/sql16_sp2_dlc/en-us/SQLServer2016SP2-FullSlipstream-DEV-x64-ENU.box'
$update_link = 'https://download.microsoft.com/download/a/7/7/a77b5753-8fe7-4804-bfc5-591d9a626c98/SQLServer2016SP3-KB5003279-x64-ENU.exe'
$update_download_link = 'https://download.microsoft.com/download/a/7/7/a77b5753-8fe7-4804-bfc5-591d9a626c98/SQLServer2016SP3-KB5003279-x64-ENU.exe'
}
default {
Write-Error "Invalid SQL Server version specified: ${{ matrix.mssql-version }}"
Exit 1
}
}
if (${{ matrix.mssql-version }} -ne '2016') {
$dl_links = ((Invoke-WebRequest -Uri $update_link).Links | Where-Object {$_.href -like 'https://download.microsoft.com/*'})
if ($dl_links.count -gt 0) {
$update_download_link = [System.Uri]::new($dl_links[0].href)
} else {
Write-Error "Download link not found"
Exit 1
}
echo $update_download_link
}
Invoke-WebRequest -Uri $update_download_link -Outfile sqlupdate.exe
Invoke-WebRequest -Uri $exe_link -OutFile sqlsetup.exe
Invoke-WebRequest -Uri $box_link -OutFile sqlsetup.box
Invoke-WebRequest -Uri $update_link -Outfile sqlupdate.exe
Start-Process -Wait -FilePath ./sqlsetup.exe -ArgumentList /qs, /x:setup
Expand Down
30 changes: 30 additions & 0 deletions examples/custom-connector.js
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');
});
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"js-md4": "^0.3.2",
"jsbi": "^4.3.0",
"native-duplexpair": "^1.0.0",
"node-abort-controller": "^3.0.1",
"node-abort-controller": "^3.1.1",
"punycode": "^2.1.0",
"sprintf-js": "^1.1.2"
},
Expand All @@ -64,11 +64,11 @@
"@commitlint/cli": "^16.2.4",
"@commitlint/config-conventional": "^16.2.4",
"@commitlint/travis-cli": "^16.2.4",
"@types/es-aggregate-error": "^1.0.2",
"@types/async": "^3.2.13",
"@types/bl": "^5.0.2",
"@types/chai": "^4.3.1",
"@types/depd": "^1.1.32",
"@types/es-aggregate-error": "^1.0.2",
"@types/lru-cache": "^5.1.0",
"@types/mocha": "^9.1.1",
"@types/node": "^12.20.50",
Expand Down
29 changes: 24 additions & 5 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ export interface InternalConnectionOptions {
columnEncryptionSetting: boolean;
columnNameReplacer: undefined | ((colName: string, index: number, metadata: Metadata) => string);
connectionRetryInterval: number;
connector: undefined | (() => Promise<Socket>);

Check failure on line 347 in src/connection.ts

View workflow job for this annotation

GitHub Actions / Linting

Cannot find name 'Socket'.
connectTimeout: number;
connectionIsolationLevel: typeof ISOLATION_LEVEL[keyof typeof ISOLATION_LEVEL];
cryptoCredentialsDetails: SecureContextOptions;
Expand Down Expand Up @@ -536,6 +537,13 @@ export interface ConnectionOptions {
*/
connectionRetryInterval?: number;

/**
* Custom connector factory method.
*
* (default: `undefined`)
*/
connector?: () => Promise<Socket>;

Check failure on line 545 in src/connection.ts

View workflow job for this annotation

GitHub Actions / Linting

Cannot find name 'Socket'.

/**
* The number of milliseconds before the attempt to connect is considered failed
*
Expand Down Expand Up @@ -759,7 +767,9 @@ export interface ConnectionOptions {
readOnlyIntent?: boolean;

/**
* The number of milliseconds before a request is considered failed, or `0` for no timeout
* The number of milliseconds before a request is considered failed, or `0` for no timeout.
*
* As soon as a response is received, the timeout is cleared. This means that queries that immediately return a response have ability to run longer than this timeout.
*
* (default: `15000`).
*/
Expand Down Expand Up @@ -1228,6 +1238,7 @@ class Connection extends EventEmitter {
columnNameReplacer: undefined,
connectionRetryInterval: DEFAULT_CONNECT_RETRY_INTERVAL,
connectTimeout: DEFAULT_CONNECT_TIMEOUT,
connector: undefined,
connectionIsolationLevel: ISOLATION_LEVEL.READ_COMMITTED,
cryptoCredentialsDetails: {},
database: undefined,
Expand Down Expand Up @@ -1336,6 +1347,14 @@ class Connection extends EventEmitter {
this.config.options.connectTimeout = config.options.connectTimeout;
}

if (config.options.connector !== undefined) {
if (typeof config.options.connector !== 'function') {
throw new TypeError('The "config.options.connector" property must be a function.');
}

this.config.options.connector = config.options.connector;
}

if (config.options.cryptoCredentialsDetails !== undefined) {
if (typeof config.options.cryptoCredentialsDetails !== 'object' || config.options.cryptoCredentialsDetails === null) {
throw new TypeError('The "config.options.cryptoCredentialsDetails" property must be of type Object.');
Expand Down Expand Up @@ -1898,7 +1917,7 @@ class Connection extends EventEmitter {
const signal = this.createConnectTimer();

if (this.config.options.port) {
return this.connectOnPort(this.config.options.port, this.config.options.multiSubnetFailover, signal);
return this.connectOnPort(this.config.options.port, this.config.options.multiSubnetFailover, signal, this.config.options.connector);
} else {
return instanceLookup({
server: this.config.server,
Expand All @@ -1907,7 +1926,7 @@ class Connection extends EventEmitter {
signal: signal
}).then((port) => {
process.nextTick(() => {
this.connectOnPort(port, this.config.options.multiSubnetFailover, signal);
this.connectOnPort(port, this.config.options.multiSubnetFailover, signal, this.config.options.connector);
});
}, (err) => {
this.clearConnectTimer();
Expand Down Expand Up @@ -2012,14 +2031,14 @@ class Connection extends EventEmitter {
});
}

connectOnPort(port: number, multiSubnetFailover: boolean, signal: AbortSignal) {
connectOnPort(port: number, multiSubnetFailover: boolean, signal: AbortSignal, customConnector?: () => Promise<Socket>) {

Check failure on line 2034 in src/connection.ts

View workflow job for this annotation

GitHub Actions / Linting

Cannot find name 'Socket'.
const connectOpts = {
host: this.routingData ? this.routingData.server : this.config.server,
port: this.routingData ? this.routingData.port : port,
localAddress: this.config.options.localAddress
};

const connect = multiSubnetFailover ? connectInParallel : connectInSequence;
const connect = customConnector || (multiSubnetFailover ? connectInParallel : connectInSequence);

(async () => {
let socket = await connect(connectOpts, dns.lookup, signal);
Expand Down
62 changes: 62 additions & 0 deletions test/unit/custom-connector.js
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();
});
});

0 comments on commit a6d414a

Please sign in to comment.