Skip to content

Commit 73d6861

Browse files
authored
Merge pull request #1520 from dhensby/pulls/named-instance-port
Support named instances with ports
2 parents f384e0f + 011f4c0 commit 73d6861

File tree

6 files changed

+71
-20
lines changed

6 files changed

+71
-20
lines changed

CHANGELOG.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
v9.1.1 (2023-01-??)
1+
v9.1.2 (2023-08-01)
2+
-------------------
3+
[fix] Support more named instance formats ([#1520](https://github.com/tediousjs/node-mssql/pull/1520))
4+
[refactor] Stop using deprecated regex symbols ([#1520](https://github.com/tediousjs/node-mssql/pull/1520))
5+
6+
v9.1.1 (2023-01-19)
27
-------------------
38
[revert] Add support for AAD authentication via connection string ((#1436)[https://github.com/tediousjs/node-mssql/pull/1436])
49

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Microsoft SQL Server client for Node.js
44

5-
[![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![Travis CI][travis-image]][travis-url] [![Appveyor CI][appveyor-image]][appveyor-url] [![Join the chat at https://gitter.im/patriksimek/node-mssql](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/patriksimek/node-mssql?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
5+
[![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![Appveyor CI][appveyor-image]][appveyor-url] [![Join the chat at https://gitter.im/patriksimek/node-mssql](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/patriksimek/node-mssql?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
66

77
Supported TDS drivers:
88
- [Tedious][tedious-url] (pure JavaScript - Windows/macOS/Linux, default)
@@ -2093,8 +2093,6 @@ to create new connections or not
20932093
[downloads-url]: https://www.npmjs.com/package/mssql
20942094
[david-image]: https://img.shields.io/david/tediousjs/node-mssql.svg?style=flat-square
20952095
[david-url]: https://david-dm.org/tediousjs/node-mssql
2096-
[travis-image]: https://img.shields.io/travis/tediousjs/node-mssql/master.svg?style=flat-square&label=unit
2097-
[travis-url]: https://travis-ci.org/tediousjs/node-mssql
20982096
[appveyor-image]: https://ci.appveyor.com/api/projects/status/e5gq1a0ujwams9t7/branch/master?svg=true
20992097
[appveyor-url]: https://ci.appveyor.com/project/tediousjs/node-mssql
21002098

lib/base/connection-pool.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ class ConnectionPool extends EventEmitter {
6464
this.config.arrayRowMode = this.config.arrayRowMode || false
6565
this.config.validateConnection = 'validateConnection' in this.config ? this.config.validateConnection : true
6666

67-
if (/^(.*)\\(.*)$/.exec(this.config.server)) {
68-
this.config.server = RegExp.$1
69-
this.config.options.instanceName = RegExp.$2
67+
const namedServer = /^(.*)\\(.*)$/.exec(this.config.server)
68+
if (namedServer) {
69+
this.config.server = namedServer[1]
70+
this.config.options.instanceName = namedServer[2]
7071
}
7172

7273
if (typeof this.config.options.useColumnNames !== 'undefined' && this.config.options.useColumnNames !== true) {
@@ -149,13 +150,21 @@ class ConnectionPool extends EventEmitter {
149150
if (/^tcp:/i.test(server)) {
150151
server = server.substr(4)
151152
}
152-
if (/^(.*)\\(.*)$/.exec(server)) {
153-
server = RegExp.$1
154-
instanceName = RegExp.$2
153+
const namedServerParts = /^(.*)\\(.*)$/.exec(server)
154+
if (namedServerParts) {
155+
server = namedServerParts[1].trim()
156+
instanceName = namedServerParts[2].trim()
155157
}
156-
if (/^(.*),(.*)$/.exec(server)) {
157-
server = RegExp.$1.trim()
158-
port = parseInt(RegExp.$2.trim(), 10)
158+
const serverParts = /^(.*),(.*)$/.exec(server)
159+
if (serverParts) {
160+
server = serverParts[1].trim()
161+
port = parseInt(serverParts[2].trim(), 10)
162+
} else {
163+
const instanceParts = /^(.*),(.*)$/.exec(instanceName)
164+
if (instanceParts) {
165+
instanceName = instanceParts[1].trim()
166+
port = parseInt(instanceParts[2].trim(), 10)
167+
}
159168
}
160169
if (server === '.' || server === '(.)' || server.toLowerCase() === '(localdb)' || server.toLowerCase() === '(local)') {
161170
server = 'localhost'
@@ -239,9 +248,10 @@ class ConnectionPool extends EventEmitter {
239248
case 'user id': {
240249
let user = value
241250
let domain
242-
if (/^(.*)\\(.*)$/.exec(user)) {
243-
domain = RegExp.$1
244-
user = RegExp.$2
251+
const domainUser = /^(.*)\\(.*)$/.exec(user)
252+
if (domainUser) {
253+
domain = domainUser[1]
254+
user = domainUser[2]
245255
}
246256
Object.assign(config, {
247257
domain,

lib/error/request-error.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ class RequestError extends MSSQLError {
4343
}
4444

4545
this.name = 'RequestError'
46-
if ((/^\[Microsoft\]\[SQL Server Native Client 11\.0\](?:\[SQL Server\])?([\s\S]*)$/).exec(this.message)) {
47-
this.message = RegExp.$1
46+
const parsedMessage = (/^\[Microsoft\]\[SQL Server Native Client 11\.0\](?:\[SQL Server\])?([\s\S]*)$/).exec(this.message)
47+
if (parsedMessage) {
48+
this.message = parsedMessage[1]
4849
}
4950
}
5051
}

lib/msnodesqlv8/request.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,9 @@ class Request extends BaseRequest {
531531
})
532532

533533
req.on('info', msg => {
534-
if ((/^\[Microsoft\]\[SQL Server Native Client 11\.0\](?:\[SQL Server\])?([\s\S]*)$/).exec(msg.message)) {
535-
msg.message = RegExp.$1
534+
const parsedMessage = (/^\[Microsoft\]\[SQL Server Native Client 11\.0\](?:\[SQL Server\])?([\s\S]*)$/).exec(msg.message)
535+
if (parsedMessage) {
536+
msg.message = parsedMessage[1]
536537
}
537538

538539
this.emit('info', {

test/common/unit.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,39 @@ describe('value handlers', () => {
386386
assert.strictEqual(sql.valueHandler.size, 0)
387387
})
388388
})
389+
390+
describe('connection string parser', () => {
391+
it('parses named instance and port', () => {
392+
const config = BasePool.parseConnectionString('Data source=instance\\database,1234')
393+
assert.deepStrictEqual(config, {
394+
options: {
395+
instanceName: 'database'
396+
},
397+
pool: {},
398+
port: 1234,
399+
server: 'instance'
400+
})
401+
})
402+
it('parses named instance and port (with instance port)', () => {
403+
const config = BasePool.parseConnectionString('Data source=instance,1234\\database')
404+
assert.deepStrictEqual(config, {
405+
options: {
406+
instanceName: 'database'
407+
},
408+
pool: {},
409+
port: 1234,
410+
server: 'instance'
411+
})
412+
})
413+
it('parses named instance', () => {
414+
const config = BasePool.parseConnectionString('Data source=instance\\database')
415+
assert.deepStrictEqual(config, {
416+
options: {
417+
instanceName: 'database'
418+
},
419+
pool: {},
420+
port: 1433,
421+
server: 'instance'
422+
})
423+
})
424+
})

0 commit comments

Comments
 (0)