From 90865cd1980bc38852bb051f845eb35428b7fa8e Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Mon, 6 Apr 2020 23:03:10 +0200 Subject: [PATCH] Rename timeout option to idle_timeout --- README.md | 2 +- lib/connection.js | 8 ++++---- lib/index.js | 7 ++++++- tests/index.js | 14 ++++++-------- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 3623a0d5..566a3146 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ const sql = postgres('postgres://username:password@host:port/database', { password : '', // Password of database user ssl : false, // True, or options for tls.connect max : 10, // Max number of connections - timeout : 0, // Idle connection timeout in seconds + idle_timeout: 0, // Idle connection timeout in seconds types : [], // Array of custom types, see more below onnotice : fn // Defaults to console.log onparameter : fn // (key, value) when server param change diff --git a/lib/connection.js b/lib/connection.js index 93e69149..8a6273a1 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -13,7 +13,7 @@ function Connection(options = {}) { const { onparameter, transform, - timeout, + idle_timeout, onnotify, onnotice, parsers @@ -113,7 +113,7 @@ function Connection(options = {}) { query.args = args query.result = [] query.result.count = null - timeout && clearTimeout(timer) + idle_timeout && clearTimeout(timer) typeof options.debug === 'function' && options.debug(id, str, args) const buffer = query.simple @@ -157,7 +157,7 @@ function Connection(options = {}) { function idle() { clearTimeout(timer) - timer = setTimeout(socket.end, timeout * 1000) + timer = setTimeout(socket.end, idle_timeout * 1000) } function onready(err) { @@ -181,7 +181,7 @@ function Connection(options = {}) { } backend.query = backend.error = null - timeout && queries.length === 0 && idle() + idle_timeout && queries.length === 0 && idle() if (!open) { messages.forEach(socket.write) diff --git a/lib/index.js b/lib/index.js index 356d5124..3069d5ee 100644 --- a/lib/index.js +++ b/lib/index.js @@ -494,7 +494,7 @@ function parseOptions(a, b) { max : o.max || url.query.max || 10, types : o.types || {}, ssl : o.ssl || url.ssl || false, - timeout : o.timeout, + idle_timeout: o.idle_timeout || warn(o.timeout, 'The timeout option is deprecated, use idle_timeout instead'), onnotice : o.onnotice, onparameter : o.onparameter, transform : Object.assign({}, o.transform), @@ -505,6 +505,11 @@ function parseOptions(a, b) { ) } +function warn(x, message) { + typeof x !== 'undefined' && console.log(message) + return x +} + function osUsername() { try { return require('os').userInfo().username // eslint-disable-line diff --git a/tests/index.js b/tests/index.js index 72137cfc..b63108ab 100644 --- a/tests/index.js +++ b/tests/index.js @@ -29,7 +29,7 @@ const options = { db: 'postgres_js_test', user: login.user, pass: login.pass, - timeout: 0.5, + idle_timeout: 0.2, max: 1 } @@ -273,7 +273,7 @@ t('Throw syntax error', async() => t('Connect using uri', async() => [true, await new Promise((resolve, reject) => { const sql = postgres('postgres://' + login.user + ':' + (login.pass || '') + '@localhost:5432/' + options.db, { - timeout: 0.1 + idle_timeout: options.idle_timeout }) sql`select 1`.then(() => resolve(true), reject) })] @@ -282,7 +282,7 @@ t('Connect using uri', async() => t('Fail with proper error on no host', async() => ['ECONNREFUSED', (await new Promise((resolve, reject) => { const sql = postgres('postgres://localhost:33333/' + options.db, { - timeout: 0.1 + idle_timeout: options.idle_timeout }) sql`select 1`.then(reject, resolve) })).code] @@ -292,7 +292,7 @@ t('Connect using SSL', async() => [true, (await new Promise((resolve, reject) => { postgres({ ssl: { rejectUnauthorized: false }, - timeout: 0.1 + idle_timeout: options.idle_timeout })`select 1`.then(() => resolve(true), reject) }))] ) @@ -741,9 +741,7 @@ t('notice works', async() => { notice = x } - const sql = postgres({ - ...options - }) + const sql = postgres(options) await sql`create table if not exists users()` await sql`create table if not exists users()` @@ -904,7 +902,7 @@ t('Async stack trace', async() => { }) t('Debug has long async stack trace', async() => { - const sql = postgres({ debug: true }) + const sql = postgres({ ...options, debug: true }) return [ 'watyo',