diff --git a/README.md b/README.md index 84d0cf5d..f3103d52 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/base/connection-pool.js b/lib/base/connection-pool.js index e11be7f6..23037556 100644 --- a/lib/base/connection-pool.js +++ b/lib/base/connection-pool.js @@ -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 @@ -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': @@ -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': @@ -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': @@ -286,7 +294,7 @@ class ConnectionPool extends EventEmitter { break case 'packet size': Object.assign(config.options, { - packetSize: value + packetSize: number() }) break case 'password': @@ -355,7 +363,7 @@ class ConnectionPool extends EventEmitter { break case 'request timeout': Object.assign(config, { - requestTimeout: parseInt(value, 10) + requestTimeout: number() }) break case 'stream': diff --git a/test/common/unit.js b/test/common/unit.js index a1d4a95d..7fb4663f 100644 --- a/test/common/unit.js +++ b/test/common/unit.js @@ -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')