Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,8 @@ Complete list of pool options can be found [here](https://github.com/vincit/tarn

In addition to configuration object there is an option to pass config as a connection string. Connection strings are supported.

The number keys `Connection Timeout`, `ConnectRetryInterval`, `Max Pool Size`, `Min Pool Size`, `Packet Size` and `Request Timeout` must have a value that starts with a number. For a value such as `abc`, the parser throws an error that names the key and the value.

##### Classic Connection String

###### Standard configuration using tedious driver
Expand Down
20 changes: 14 additions & 6 deletions lib/base/connection-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ class ConnectionPool extends EventEmitter {
if (value === undefined) {
return config
}
// a number value that parseInt cannot read is a mistake, so a key that is used throws for it
const number = () => {
if (Number.isNaN(value)) {
const prop = [key, ...(NODE_MSSQL_SCHEMA[key].aliases ?? [])].find(k => result.has(k))
throw new Error(`Invalid value for connection string key "${prop}": "${result.get(prop)}" is not a number.`)
}
return value
}
switch (key) {
case 'application name':
break
Expand All @@ -169,7 +177,7 @@ class ConnectionPool extends EventEmitter {
break
case 'connection timeout':
Object.assign(config, {
connectionTimeout: value * 1000
connectionTimeout: number() * 1000
})
break
case 'connection lifetime':
Expand All @@ -178,7 +186,7 @@ class ConnectionPool extends EventEmitter {
break
case 'connectretryinterval':
Object.assign(config.options, {
connectionRetryInterval: value * 1000
connectionRetryInterval: number() * 1000
})
break
case 'context connection':
Expand Down Expand Up @@ -257,12 +265,12 @@ class ConnectionPool extends EventEmitter {
break
case 'max pool size':
Object.assign(config.pool, {
max: value
max: number()
})
break
case 'min pool size':
Object.assign(config.pool, {
min: value
min: number()
})
break
case 'msi endpoint':
Expand All @@ -286,7 +294,7 @@ class ConnectionPool extends EventEmitter {
break
case 'packet size':
Object.assign(config.options, {
packetSize: value
packetSize: number()
})
break
case 'password':
Expand Down Expand Up @@ -355,7 +363,7 @@ class ConnectionPool extends EventEmitter {
break
case 'request timeout':
Object.assign(config, {
requestTimeout: parseInt(value, 10)
requestTimeout: number()
})
break
case 'stream':
Expand Down
42 changes: 42 additions & 0 deletions test/common/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,48 @@ describe('connection string empty values', () => {
})
})

describe('connection string number values that are not a number', () => {
const keys = [
'Connection Timeout',
'ConnectRetryInterval',
'Max Pool Size',
'Min Pool Size',
'Packet Size',
'Request Timeout'
]

for (const key of keys) {
it(`throws for ${key}=abc`, () => {
assert.throws(() => BasePool._parseConnectionString(`Server=localhost;${key}=abc`), {
name: 'Error',
message: `Invalid value for connection string key "${key.toLowerCase()}": "abc" is not a number.`
})
})
}

it('ignores a number key that is not used', () => {
const expected = BasePool._parseConnectionString('Server=localhost')
assert.deepStrictEqual(BasePool._parseConnectionString('Server=localhost;Connection Lifetime=abc'), expected)
})

it('names the alias that the connection string uses', () => {
assert.throws(() => BasePool._parseConnectionString('Server=localhost;Connect Timeout=abc'), {
message: 'Invalid value for connection string key "connect timeout": "abc" is not a number.'
})
})

it('throws from the ConnectionPool constructor', () => {
assert.throws(() => new ConnectionPool('Server=localhost;Max Pool Size=undefined'), {
message: 'Invalid value for connection string key "max pool size": "undefined" is not a number.'
})
})

it('parses a value that starts with a number as that number', () => {
const config = BasePool._parseConnectionString('Server=localhost;Connection Timeout=30s')
assert.strictEqual(config.connectionTimeout, 30000)
})
})

describe('connection string auth - base', () => {
it('parses basic login', () => {
const config = BasePool._parseConnectionString('Server=database.test.com;Database=test;User Id=test;Password=admin')
Expand Down