Skip to content

Commit aa3d13e

Browse files
committed
build
1 parent 26b23c1 commit aa3d13e

File tree

9 files changed

+118
-54
lines changed

9 files changed

+118
-54
lines changed

cf/src/connection.js

+20-16
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
387387
}
388388

389389
function queryError(query, err) {
390-
query.reject(Object.create(err, {
390+
Object.defineProperties(err, {
391391
stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug },
392392
query: { value: query.string, enumerable: options.debug },
393393
parameters: { value: query.parameters, enumerable: options.debug },
394394
args: { value: query.args, enumerable: options.debug },
395395
types: { value: query.statement && query.statement.types, enumerable: options.debug }
396-
}))
396+
})
397+
query.reject(err)
397398
}
398399

399400
function end() {
@@ -442,7 +443,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
442443
closedDate = performance.now()
443444
hadError && options.shared.retries++
444445
delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000
445-
onclose(connection)
446+
onclose(connection, Errors.connection('CONNECTION_CLOSED', options, socket))
446447
}
447448

448449
/* Handlers */
@@ -658,27 +659,30 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
658659

659660
/* c8 ignore next 5 */
660661
async function AuthenticationCleartextPassword() {
662+
const payload = await Pass()
661663
write(
662-
b().p().str(await Pass()).z(1).end()
664+
b().p().str(payload).z(1).end()
663665
)
664666
}
665667

666668
async function AuthenticationMD5Password(x) {
667-
write(
668-
b().p().str(
669-
'md5' +
670-
(await md5(Buffer.concat([
669+
const payload = 'md5' + (
670+
await md5(
671+
Buffer.concat([
671672
Buffer.from(await md5((await Pass()) + user)),
672673
x.subarray(9)
673-
])))
674-
).z(1).end()
674+
])
675+
)
676+
)
677+
write(
678+
b().p().str(payload).z(1).end()
675679
)
676680
}
677681

678682
async function SASL() {
683+
nonce = (await crypto.randomBytes(18)).toString('base64')
679684
b().p().str('SCRAM-SHA-256' + b.N)
680685
const i = b.i
681-
nonce = (await crypto.randomBytes(18)).toString('base64')
682686
write(b.inc(4).str('n,,n=*,r=' + nonce).i32(b.i - i - 4, i).end())
683687
}
684688

@@ -700,12 +704,12 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
700704

701705
serverSignature = (await hmac(await hmac(saltedPassword, 'Server Key'), auth)).toString('base64')
702706

707+
const payload = 'c=biws,r=' + res.r + ',p=' + xor(
708+
clientKey, Buffer.from(await hmac(await sha256(clientKey), auth))
709+
).toString('base64')
710+
703711
write(
704-
b().p().str(
705-
'c=biws,r=' + res.r + ',p=' + xor(
706-
clientKey, Buffer.from(await hmac(await sha256(clientKey), auth))
707-
).toString('base64')
708-
).end()
712+
b().p().str(payload).end()
709713
)
710714
}
711715

cf/src/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,10 @@ function Postgres(a, b) {
240240

241241
try {
242242
await sql.unsafe('begin ' + options.replace(/[^a-z ]/ig, ''), [], { onexecute }).execute()
243-
return await scope(connection, fn)
243+
return await Promise.race([
244+
scope(connection, fn),
245+
new Promise((_, reject) => connection.onclose = reject)
246+
])
244247
} catch (error) {
245248
throw error
246249
}
@@ -415,9 +418,10 @@ function Postgres(a, b) {
415418
: move(c, full)
416419
}
417420

418-
function onclose(c) {
421+
function onclose(c, e) {
419422
move(c, closed)
420423
c.reserved = null
424+
c.onclose && (c.onclose(e), c.onclose = null)
421425
options.onclose && options.onclose(c.id)
422426
queries.length && connect(c, queries.shift())
423427
}
@@ -438,6 +442,7 @@ function parseOptions(a, b) {
438442
o.no_prepare && (o.prepare = false)
439443
query.sslmode && (query.ssl = query.sslmode, delete query.sslmode)
440444
'timeout' in o && (console.log('The timeout option is deprecated, use idle_timeout instead'), o.idle_timeout = o.timeout) // eslint-disable-line
445+
query.sslrootcert === 'system' && (query.ssl = 'verify-full')
441446

442447
const ints = ['idle_timeout', 'connect_timeout', 'max_lifetime', 'max_pipeline', 'backoff', 'keep_alive']
443448
const defaults = {

cjs/src/connection.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -385,13 +385,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
385385
}
386386

387387
function queryError(query, err) {
388-
query.reject(Object.create(err, {
388+
Object.defineProperties(err, {
389389
stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug },
390390
query: { value: query.string, enumerable: options.debug },
391391
parameters: { value: query.parameters, enumerable: options.debug },
392392
args: { value: query.args, enumerable: options.debug },
393393
types: { value: query.statement && query.statement.types, enumerable: options.debug }
394-
}))
394+
})
395+
query.reject(err)
395396
}
396397

397398
function end() {
@@ -440,7 +441,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
440441
closedDate = performance.now()
441442
hadError && options.shared.retries++
442443
delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000
443-
onclose(connection)
444+
onclose(connection, Errors.connection('CONNECTION_CLOSED', options, socket))
444445
}
445446

446447
/* Handlers */

cjs/src/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ function Postgres(a, b) {
239239

240240
try {
241241
await sql.unsafe('begin ' + options.replace(/[^a-z ]/ig, ''), [], { onexecute }).execute()
242-
return await scope(connection, fn)
242+
return await Promise.race([
243+
scope(connection, fn),
244+
new Promise((_, reject) => connection.onclose = reject)
245+
])
243246
} catch (error) {
244247
throw error
245248
}
@@ -414,9 +417,10 @@ function Postgres(a, b) {
414417
: move(c, full)
415418
}
416419

417-
function onclose(c) {
420+
function onclose(c, e) {
418421
move(c, closed)
419422
c.reserved = null
423+
c.onclose && (c.onclose(e), c.onclose = null)
420424
options.onclose && options.onclose(c.id)
421425
queries.length && connect(c, queries.shift())
422426
}
@@ -437,6 +441,7 @@ function parseOptions(a, b) {
437441
o.no_prepare && (o.prepare = false)
438442
query.sslmode && (query.ssl = query.sslmode, delete query.sslmode)
439443
'timeout' in o && (console.log('The timeout option is deprecated, use idle_timeout instead'), o.idle_timeout = o.timeout) // eslint-disable-line
444+
query.sslrootcert === 'system' && (query.ssl = 'verify-full')
440445

441446
const ints = ['idle_timeout', 'connect_timeout', 'max_lifetime', 'max_pipeline', 'backoff', 'keep_alive']
442447
const defaults = {

cjs/tests/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -2348,6 +2348,17 @@ t('Ensure reconnect after max_lifetime with transactions', { timeout: 5 }, async
23482348
return [true, true]
23492349
})
23502350

2351+
2352+
t('Ensure transactions throw if connection is closed dwhile there is no query', async() => {
2353+
const sql = postgres(options)
2354+
const x = await sql.begin(async() => {
2355+
setTimeout(() => sql.end({ timeout: 0 }), 10)
2356+
await new Promise(r => setTimeout(r, 200))
2357+
return sql`select 1`
2358+
}).catch(x => x)
2359+
return ['CONNECTION_CLOSED', x.code]
2360+
})
2361+
23512362
t('Custom socket', {}, async() => {
23522363
let result
23532364
const sql = postgres({

0 commit comments

Comments
 (0)