Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0cb2981

Browse files
committed
Support sslmode prefer and require
1 parent ce4501a commit 0cb2981

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const sql = postgres('postgres://username:password@host:port/database', {
5151
database : '', // Name of database to connect to
5252
username : '', // Username of database user
5353
password : '', // Password of database user
54-
ssl : false, // True, or options for tls.connect
54+
ssl : false, // true, prefer, require, tls.connect options
5555
max : 10, // Max number of connections
5656
idle_timeout : 0, // Idle connection timeout in seconds
5757
connect_timeout : 30, // Connect timeout in seconds

lib/connection.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,19 @@ function postgresSocket(options, {
287287
socket.removeListener('error', error)
288288
socket.removeListener('close', onclose)
289289
x.toString() === 'S'
290-
? attach(tls.connect(Object.assign({ socket }, options.ssl)))
291-
: /* c8 ignore next */ error('Server does not support SSL')
290+
? attach(tls.connect(Object.assign({ socket }, ssl(options.ssl))))
291+
: options.ssl === 'prefer'
292+
? (attach(socket), ready())
293+
: /* c8 ignore next */ error('Server does not support SSL')
292294
})
293295
}
294296

297+
function ssl(x) {
298+
return x === 'require' || x === 'allow' || x === 'prefer'
299+
? { rejectUnauthorized: false }
300+
: x
301+
}
302+
295303
function attach(x) {
296304
socket = x
297305
socket.on('data', data)

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ function parseOptions(a, b) {
551551
pass : o.pass || o.password || auth[1] || env.PGPASSWORD || '',
552552
max : o.max || url.query.max || 10,
553553
types : o.types || {},
554-
ssl : o.ssl || url.ssl || false,
554+
ssl : o.ssl || url.sslmode || url.ssl || false,
555555
idle_timeout : o.idle_timeout || url.query.idle_timeout || env.PGIDLE_TIMEOUT || warn(o.timeout),
556556
connect_timeout : o.connect_timeout || url.query.connect_timeout || env.PGCONNECT_TIMEOUT || 30,
557557
no_prepare : o.no_prepare,

tests/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require('./bootstrap.js')
44

55
const { t, not, ot } = require('./test.js') // eslint-disable-line
6+
const cp = require('child_process')
67
const path = require('path')
78
const net = require('net')
89

@@ -294,6 +295,31 @@ t('Connect using SSL', async() =>
294295
}))]
295296
)
296297

298+
t('Connect using SSL require', async() =>
299+
[true, (await new Promise((resolve, reject) => {
300+
postgres({
301+
ssl: 'require',
302+
idle_timeout: options.idle_timeout
303+
})`select 1`.then(() => resolve(true), reject)
304+
}))]
305+
)
306+
307+
t('Connect using SSL prefer', async() => {
308+
cp.execSync('psql -c "alter system set ssl=off"')
309+
cp.execSync('psql -c "select pg_reload_conf()"')
310+
311+
const sql = postgres({
312+
ssl: 'prefer',
313+
idle_timeout: options.idle_timeout
314+
})
315+
316+
return [
317+
1, (await sql`select 1 as x`)[0].x,
318+
cp.execSync('psql -c "alter system set ssl=on"'),
319+
cp.execSync('psql -c "select pg_reload_conf()"')
320+
]
321+
})
322+
297323
t('Login without password', async() => {
298324
return [true, (await postgres({ ...options, ...login })`select true as x`)[0].x]
299325
})

0 commit comments

Comments
 (0)