Initial MultiPay project

This commit is contained in:
Alex committed 2026-11-02 09:50:37 +08:00
commit 91abad4066
488 files changed
+76727

No files matched your search

+1970
View File
File diff suppressed because it is too large. Load diff
+39
View File
@@ -0,0 +1,39 @@
{
"name": "postgres",
"version": "4.0.0",
"description": "Fastest full featured PostgreSQL client for Node.js, Bun and Cloudflare Workers",
"type": "module",
"main": "src/index.js",
"types": "types/index.d.ts",
"exports": {
".": {
"types": "./types/index.d.ts",
"workerd": "./src/cf.js",
"default": "./src/index.js"
}
},
"engines": {
"node": ">=20"
},
"scripts": {
"test": "node tests/unit && node tests/index.js && node tests/simulate.js",
"test:unit": "node tests/unit",
"test:integration": "node tests/index.js",
"test:simulate": "node tests/simulate.js",
"coverage": "node tests/coverage.js",
"test:types": "cd types && npm install && npm test",
"test:cf": "sh tests/cf/run.sh",
"lint": "eslint src tests"
},
"files": [
"/src/*.js",
"/types/index.d.ts"
],
"author": "Rasmus Porsager <rasmus@porsager.com> (https://www.porsager.com)",
"funding": {
"type": "individual",
"url": "https://github.com/sponsors/porsager"
},
"license": "Unlicense",
"repository": "porsager/postgres"
}
+292
View File
@@ -0,0 +1,292 @@
import { Errors } from './errors.js'
import { Parameter, infer } from './types.js'
export const isQuery = Symbol('query')
class NotTagged {
then() { notTagged() }
catch() { notTagged() }
finally() { notTagged() }
}
export class Identifier extends NotTagged {
constructor(value) {
super()
this.value = escapeIdentifier(value)
}
}
export class Join {
constructor(separator, items) {
this.separator = separator
this.items = items
}
}
export const joinStrings = Object.assign(['', ''], { raw: ['', ''] })
export class Builder extends NotTagged {
constructor(first, rest) {
super()
this.first = first
this.rest = rest
}
}
export function build(query, options) {
const values = []
, oids = []
, o = { transform: options.transform, infer: options.infer !== false, resolve: options.resolve, values, oids, dynamic: !query.tagged }
const text = query.tagged
? stringify(query, o)
: (query.args.forEach(x => value(x, o)), '' + query.strings[0])
if (values.length >= 65534)
throw Errors.generic('MAX_PARAMETERS_EXCEEDED', 'Max number of parameters (65534) exceeded')
const types = oids.join()
return { strings: query.strings, dynamic: o.dynamic, types, text, values, oids }
}
function stringify(q, o) {
let string = q.strings[0]
for (let i = 1; i < q.strings.length; i++)
string += part(string, q.args[i - 1], o, q.strings[i]) + q.strings[i]
return string
}
function part(before, x, o, after = '') {
if (x instanceof Builder || x instanceof Identifier || isFragment(x) || (Array.isArray(x) && isFragment(x[0])))
o.dynamic = true
return isFragment(x) && x.strings === joinStrings ? joined(x.args[0], o, before, after)
: x instanceof Join ? joined(x, o, '', '')
: x instanceof Builder ? helper(before, x, o)
: isFragment(x) ? fragment(x, o)
: x instanceof Identifier ? x.value
: Array.isArray(x) && isFragment(x[0]) ? x.reduce((acc, x) => acc + ' ' + fragment(x, o), '')
: value(x, o)
}
function joined(join, o, before, after) {
let text = ''
join.items.forEach((item, i) => {
i > 0 && (text = spaced(text, fragment(join.separator, o)))
text = spaced(text, part('', item, o))
})
before && text && !/\s$/.test(before) && !/^\s/.test(text) && (text = ' ' + text)
after && text && !/^\s/.test(after) && !/\s$/.test(text) && (text = text + ' ')
return text
}
function spaced(a, b) {
return a && b && !/\s$/.test(a) && !/^\s/.test(b) ? a + ' ' + b : a + b
}
function isFragment(x) {
return !!x && x[isQuery] === true
}
function fragment(q, o) {
return q.tagged ? stringify(q, o) : q.strings[0]
}
export function value(x, o) {
let v = x instanceof Parameter ? x.value : x
if (v === undefined) {
v = o.transform.undefined
if (v === undefined)
throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')
x instanceof Parameter || (x = v)
}
o.values.push(v)
o.oids.push(x instanceof Parameter ? o.resolve(x.type) : o.infer ? infer(x) : 0)
return '$' + o.values.length
}
const builders = {
values,
in: (...xs) => {
const x = values(...xs)
return x === '()' ? '(null)' : x
},
select,
update,
insert,
call
}
function call(first, rest, o) {
if (first === null || typeof first !== 'object' || Array.isArray(first) || isFragment(first))
return select(first, rest, o)
return columns(first, rest).map(x =>
escapeIdentifier(column(x, o)) + ' => ' + part('values', first[x], o)
).join(',')
}
const clauses = { in: 'in', values: 'values', select: 'select', returning: 'select', set: 'update', distinct: 'select' }
, stops = new Set([
'from', 'where', 'on', 'to', 'join', 'group', 'order', 'by', 'having', 'limit', 'offset', 'union',
'intersect', 'except', 'using', 'and', 'or', 'when', 'then', 'else', 'with', 'table', 'grant',
'revoke', 'delete', 'update', 'case', 'not', 'like', 'ilike', 'between', 'is', 'exists', 'conflict',
'do', 'nothing', 'window', 'over', 'partition', 'filter', 'lateral', 'cross', 'inner', 'left', 'right',
'full', 'outer', 'natural', 'into'
])
function helper(before, x, o) {
const kind = context(before)
return kind ? builders[kind](x.first, x.rest, o) : identifiers(typeof x.first === 'string' ? [x.first].concat(x.rest) : x.first, o)
}
function context(sql) {
const words = []
let i = sql.length - 1
, depth = 0
while (i >= 0) {
const c = sql[i]
if (c === ')') {
depth++
i--
} else if (c === '(') {
if (depth === 0) {
const word = previous(sql, i - 1)
if (words.length === 0)
return word === 'in' || word === 'values' ? word : word && !clauses[word] && !stops.has(word) ? 'call' : 'select'
return 'select'
}
depth--
i--
} else if (c === '\'' || c === '"') {
const end = i
i--
while (i >= 0 && !(sql[i] === c && sql[i - 1] !== c))
sql[i] === c ? i -= 2 : i--
i--
if (sql[i] === '.')
i--
else
depth === 0 && c === '"' && words.push(sql.slice(i + 1, end + 1))
} else if (/[a-z_0-9$]/i.test(c)) {
const end = i + 1
while (i >= 0 && /[a-z_0-9$.]/i.test(sql[i]))
i--
const word = depth === 0 && sql.slice(i + 1, end).toLowerCase()
if (word && clauses[word])
return clauses[word]
if (word === 'into')
return insert_into(words, sql, i) ? 'insert' : null
if (word && stops.has(word))
return null
word && words.push(word)
} else {
i--
}
}
return null
}
function previous(sql, i) {
while (i >= 0 && /\s/.test(sql[i]))
i--
if (sql[i] === '"')
return '"'
const end = i + 1
while (i >= 0 && /[a-z_0-9$.]/i.test(sql[i]))
i--
return sql.slice(i + 1, end).toLowerCase()
}
function insert_into(words, sql, i) {
const target = words.length === 1 || (words.length === 3 && words[1] === 'as')
return target && previous(sql, i) === 'insert'
}
function valuesBuilder(first, columns, o) {
return first.map(row =>
'(' + columns.map(x => part('values', row[x], o)).join(',') + ')'
).join(',')
}
function values(first, rest, o) {
const rows = Array.isArray(first) && first.length > 0 && first[0] !== null && typeof first[0] === 'object' && !isFragment(first[0])
, picked = columns(rows ? first[0] : first, rest)
return valuesBuilder(rows ? first : [first], picked, o)
}
function update(first, rest, o) {
const picked = columns(first, rest)
picked.length || empty('update')
return picked.map(x =>
escapeIdentifier(column(x, o)) + '=' + part('values', first[x], o)
)
}
function insert(first, rest, o) {
if (Array.isArray(first) && first.every(x => typeof x === 'string'))
return '(' + escapeIdentifiers(first.concat(rest.flat()), o) + ')'
const picked = columns(Array.isArray(first) ? first[0] || {} : first, rest)
picked.length || empty('insert')
return '(' + escapeIdentifiers(picked, o) + ')values' + valuesBuilder(Array.isArray(first) ? first : [first], picked, o)
}
function columns(first, rest) {
return rest.length ? rest.flat() : Object.keys(first)
}
function empty(x) {
throw Errors.generic('NO_COLUMNS', 'The ' + x + ' helper got no columns')
}
function identifiers(first, o) {
return Array.isArray(first)
? first.map(x => member(x, o)).join(',')
: member(first, o)
}
function member(x, o) {
return isFragment(x) ? fragment(x, o) : x instanceof Identifier ? x.value : escapeIdentifier(column(x, o))
}
function select(first, rest, o) {
typeof first === 'string' && (first = [first].concat(rest))
if (Array.isArray(first))
return identifiers(first, o)
return columns(first, rest).map(x => {
const v = first[x]
return (
isFragment(v) ? fragment(v, o) :
v instanceof Identifier ? v.value :
value(v, o)
) + ' as ' + escapeIdentifier(column(x, o))
}).join(',')
}
function column(x, o) {
return o.transform.column.to ? o.transform.column.to(x) : x
}
function escapeIdentifiers(xs, o) {
return xs.map(x => escapeIdentifier(column(x, o))).join(',')
}
export function escapeIdentifier(x) {
return '"' + x.replace(/"/g, '""').replace(/\./g, '"."') + '"'
}
export function notTagged() {
throw Errors.generic('NOT_TAGGED_CALL', 'Query not called as a tagged template literal')
}
+151
View File
@@ -0,0 +1,151 @@
import { connect } from 'cloudflare:sockets'
import { EventEmitter } from 'node:events'
import { Buffer } from 'node:buffer'
import postgres, { PostgresError } from './index.js'
import { Errors } from './errors.js'
import { secured, direct } from './socket.js'
export default function Postgres(a, b) {
return typeof a === 'string' || !a
? postgres(a, { socket, ...b })
: postgres({ socket, ...a })
}
Object.assign(Postgres, postgres)
export { PostgresError }
export async function socket(options, host, port) {
if (direct(options))
throw Errors.generic('SSL_NEGOTIATION_NOT_SUPPORTED', 'sslnegotiation: direct needs ALPN, which Cloudflare Workers sockets do not offer')
const tls = secured(options)
let raw = connect({ hostname: host, port }, { secureTransport: tls ? 'starttls' : 'off', allowHalfOpen: false })
await raw.opened
let secure = false
if (tls) {
const writer = raw.writable.getWriter()
, reader = raw.readable.getReader()
await writer.write(sslRequest)
const { value, done } = await reader.read()
writer.releaseLock()
reader.releaseLock()
if (done)
throw Errors.connection('CONNECTION_CLOSED', options, host, port)
if (value[0] === 83) {
raw = raw.startTls()
await raw.opened
secure = true
} else if (options.ssl !== 'prefer' && options.ssl !== 'allow') {
raw.close()
throw Errors.generic('SSL_NOT_SUPPORTED', 'The server does not support SSL connections')
}
}
return Stream(raw, secure, host, port)
}
const sslRequest = Buffer.from([0, 0, 0, 8, 4, 210, 22, 47])
, chunk = 16384
function Stream(raw, secure, host, port) {
const x = new EventEmitter()
, writer = raw.writable.getWriter()
, reader = raw.readable.getReader()
let paused = false
, wake = null
, closed = false
, pending = 0
, answered = !secure
const written = () => --pending === 0 && x.emit('drain')
Object.assign(x, {
secure,
negotiated: true,
destroyed: false,
write(data) {
if (closed)
return false
for (let i = 0; i < data.length; i += chunk) {
pending++
writer.write(data.subarray(i, i + chunk)).then(written, fail)
}
return pending < 16
},
end(data) {
data && x.write(data)
writer.close().catch(() => {})
},
destroy() {
x.destroyed = true
raw.close().catch(() => {})
finish()
},
pause() {
paused = true
},
resume() {
paused = false
wake && (wake(), wake = null)
},
setKeepAlive() {},
setNoDelay() {}
})
read()
return x
function resumed() {
return new Promise(r => wake = r)
}
async function read() {
try {
while (true) {
paused && await resumed()
const { value, done } = await reader.read()
if (done)
break
answered = true
x.emit('data', Buffer.from(value.buffer, value.byteOffset, value.byteLength))
}
} catch (error) {
closed || x.emit('error', handshake(error))
}
finish()
}
function fail(error) {
closed || x.emit('error', handshake(error))
x.destroy()
}
function finish() {
if (closed)
return
answered || x.destroyed || x.emit('error', handshake())
closed = true
x.emit('close')
}
function handshake(error) {
if (answered)
return error
answered = true
return Object.assign(Errors.generic('TLS_HANDSHAKE_FAILED',
'The TLS connection to ' + host + ':' + port + ' was lost before the server answered. '
+ 'Workers could not complete the TLS handshake, most often because the server certificate is not trusted by a public CA'
), { cause: error })
}
}
+1226
View File
File diff suppressed because it is too large. Load diff
+73
View File
@@ -0,0 +1,73 @@
import dc from 'diagnostics_channel'
export const channels = {
query: tracing('postgres:query'),
connect: tracing('postgres:connect'),
transaction: tracing('postgres:transaction'),
pool: plain('postgres:pool'),
notice: plain('postgres:notice')
}
const done = Symbol('done')
export const now = () => performance.now()
export const userName = options => typeof options.user === 'function' ? null : options.user
export function active(t) {
return !!t && (t.hasSubscribers !== undefined
? t.hasSubscribers
: t.start.hasSubscribers || t.end.hasSubscribers || t.asyncStart.hasSubscribers
|| t.asyncEnd.hasSubscribers || t.error.hasSubscribers)
}
export function listened(channel) {
return !!channel && channel.hasSubscribers
}
export function start(t, context, fn) {
return typeof t.start.runStores === 'function'
? t.start.runStores(context, run)
: (t.start.publish(context), run())
function run() {
try {
return fn()
} finally {
t.end.publish(context)
}
}
}
export function finish(t, context, error, result) {
if (context[done])
return
context[done] = true
if (error) {
context.error = error
t.error.publish(context)
} else {
context.result = result
}
t.asyncStart.publish(context)
t.asyncEnd.publish(context)
}
function tracing(name) {
try {
return typeof dc.tracingChannel === 'function' ? dc.tracingChannel(name) : null
} catch (error) {
return null
}
}
function plain(name) {
try {
return dc.channel(name)
} catch (error) {
return null
}
}
+88
View File
@@ -0,0 +1,88 @@
export class PostgresError extends Error {
constructor(x) {
super(typeof x === 'string' ? x : x && x.message)
this.name = this.constructor.name
x && typeof x === 'object' && Object.assign(this, x)
}
toJSON() {
return { ...this, message: this.message }
}
}
export const Errors = {
connection,
postgres,
generic,
notSupported
}
function connection(code, options, host, port) {
const address = options.path || host
, error = Object.assign(
new Error('write ' + code + ' ' + (options.path || host + ':' + port)),
{ code, errno: code, address },
options.path ? {} : { port }
)
Error.captureStackTrace(error, connection)
return error
}
function postgres(x) {
const error = new PostgresError(x)
Error.captureStackTrace(error, postgres)
return error
}
function generic(code, message) {
const error = Object.assign(new Error(code + ': ' + message), { code })
Error.captureStackTrace(error, generic)
return error
}
function notSupported(x) {
const error = Object.assign(new Error(x + ' (B) is not supported'), { code: 'MESSAGE_NOT_SUPPORTED', name: x })
Error.captureStackTrace(error, notSupported)
return error
}
const places = locations()
export const ours = line => places.some(here => line.includes(here) && !line.slice(line.indexOf(here) + here.length).split(':')[0].includes('/'))
function locations() {
try {
const url = new URL('.', import.meta.url)
return url.protocol === 'file:' ? [url.href, decodeURIComponent(url.pathname)] : [url.href]
} catch (error) {
return []
}
}
export function withOrigin(error, origin) {
origin && Object.defineProperty(error, 'stack', {
value: [error.stack.split('\n')[0], ...origin.split('\n').slice(1).filter(x => !ours(x))].join('\n'),
writable: true,
configurable: true,
enumerable: false
})
return error
}
export function queryError(error, q, debug) {
if (!error || typeof error !== 'object')
error = new Error(error)
if (!q || 'query' in error)
return error
const built = q.built
Object.defineProperties(error, {
query: { value: built ? built.text : q.strings[0], enumerable: !!debug, configurable: true, writable: true },
parameters: { value: built ? built.values : [], enumerable: !!debug, configurable: true, writable: true },
args: { value: q.args, enumerable: !!debug, configurable: true, writable: true },
types: { value: built ? built.oids : [], enumerable: !!debug, configurable: true, writable: true }
})
return withOrigin(error, q.origin)
}
+617
View File
@@ -0,0 +1,617 @@
import os from 'os'
import fs from 'fs'
import {
Types,
Parameter,
toPascal,
pascal,
toCamel,
camel,
toKebab,
kebab,
fromPascal,
fromCamel,
fromKebab
} from './types.js'
import { Identifier, Builder } from './build.js'
import { Query, CLOSE } from './query.js'
import { Pool } from './pool.js'
import { Errors, PostgresError } from './errors.js'
import Subscribe from './subscribe.js'
import largeObject from './large.js'
import { channels, active, start, finish, userName } from './diagnostics.js'
Object.assign(Postgres, {
PostgresError,
toPascal,
pascal,
toCamel,
camel,
toKebab,
kebab,
fromPascal,
fromCamel,
fromKebab,
BigInt: {
to: 20,
from: [20],
parse: x => BigInt(x),
serialize: x => x.toString()
}
})
export default Postgres
export { PostgresError }
function Postgres(a, b) {
const options = parseOptions(a, b)
, types = options.shared_types || Types(options)
, pool = Pool(options, types)
, subscribe = options.no_subscribe || Subscribe(Postgres, { ...options, shared_types: types })
, listener = Listen(options)
if (types.named && options.fetch_types === false)
throw Errors.generic('TYPE_NOT_FOUND', 'Custom types given by name need fetch_types to look them up')
options.resolve = types.resolve
options.parsers = view(types.parsers, types.parser)
options.serializers = view(types.serializers, types.serializer)
const sql = Sql(handler)
Object.defineProperties(sql, {
parameters: { get: () => options.parameters, enumerable: true },
stats: { get: () => pool.stats, enumerable: true }
})
Object.assign(sql, {
largeObject: largeObject.bind(null, sql),
subscribe,
CLOSE,
END: CLOSE,
PostgresError,
options,
reserve,
listen: listener.listen,
begin,
close,
end
})
Symbol.asyncDispose && (sql[Symbol.asyncDispose] = () => end())
return sql
function handler(q) {
pool.query(q)
}
function Sql(handler) {
handler.debug = options.debug
handler.options = options
handler.cancel = handler.cancel || pool.cancel
const typed = (value, type) => new Parameter(value, type)
Object.entries(options.types).forEach(([name, type]) => typed[name] = x => new Parameter(x, type.to))
Object.assign(sql, {
types: typed,
typed,
unsafe,
notify,
array,
json,
file
})
return sql
function sql(strings, ...args) {
return strings && Array.isArray(strings.raw)
? new Query(strings, args, handler)
: typeof strings === 'string' && !args.length
? new Identifier(options.transform.column.to ? options.transform.column.to(strings) : strings)
: new Builder(strings, args)
}
function unsafe(string, args = [], options = {}) {
arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])
return new Query([string], args, handler, {
prepare: false,
...options,
simple: 'simple' in options ? options.simple : args.length === 0
})
}
async function notify(channel, payload) {
return await sql`select pg_notify(${ channel }, ${ '' + payload })`
}
function file(path, args = [], options = {}) {
arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])
const fileHandler = query => fs.readFile(path, 'utf8', (err, string) => {
if (err)
return query.reject(err)
query.strings = [string]
handler(query)
})
Object.assign(fileHandler, handler)
return new Query([], args, fileHandler, {
...options,
simple: 'simple' in options ? options.simple : args.length === 0
})
}
}
function json(x) {
return new Parameter(x, 3802)
}
function array() {
throw Errors.generic('ARRAY_REMOVED', 'sql.array() is removed in v4, pass the array as it is')
}
async function reserve() {
const lease = await pool.lease('reserve')
, reserved = Sql(Object.assign(q => lease.send(q), { cancel: pool.cancel }))
reserved.release = () => lease.release()
Symbol.dispose && (reserved[Symbol.dispose] = reserved.release)
Symbol.asyncDispose && (reserved[Symbol.asyncDispose] = async() => reserved.release())
return reserved
}
function begin(mode, fn) {
!fn && (fn = mode, mode = '')
if (!active(channels.transaction))
return transaction(mode, fn, null)
const context = {
mode,
database: options.database,
user: userName(options),
connection: null,
host: null,
port: null,
outcome: null
}
let promise
start(channels.transaction, context, () => promise = transaction(mode, fn, context))
return promise.then(
result => (finish(channels.transaction, context, null, result), result),
error => {
finish(channels.transaction, context, error)
throw error
}
)
}
async function transaction(mode, fn, context) {
const lease = await pool.lease('transaction')
let savepoints = 0
, prepare = null
context && Object.assign(context, {
connection: lease.connection.id,
host: lease.connection.host,
port: lease.connection.port
})
try {
await control(lease, 'begin ' + mode.replace(/[^a-z ]/ig, ''))
const done = scope(fn)
done.catch(() => {})
return await Promise.race([done, lease.closed])
} finally {
lease.release()
}
async function scope(fn, name, inside, above = () => false) {
let uncaught
, result
, rolled = null
, after = null
if (above())
throw rolledBack()
const stopped = () => !!rolled || above()
, scoped = Sql(Object.assign(q => {
if (stopped())
return q.reject(after || (after = rolledBack()))
q.catch(e => uncaught || (uncaught = e))
lease.send(q)
}, { cancel: pool.cancel }))
scoped.savepoint = savepoint
scoped.begin = nested
scoped.rollback = () => rolled || (rolled = control(lease, name ? 'rollback to ' + identifier(name) : 'rollback').then(() => {}))
scoped.prepare = x => {
if (inside)
throw Errors.generic('NESTED_TRANSACTION_MODE', 'A nested transaction can not be prepared, only the outermost one')
prepare = x.replace(/[^a-z0-9$\-_. ]/gi, '')
}
name && await control(lease, 'savepoint ' + identifier(name))
try {
result = await new Promise((resolve, reject) => {
const x = fn(scoped)
Promise.resolve(Array.isArray(x) ? Promise.all(x) : x).then(resolve, reject)
})
if (uncaught && !stopped())
throw uncaught
} catch (error) {
context && !name && (context.outcome = 'rollback')
stopped()
? rolled && await rolled.catch(() => {})
: lease.connection.closed || await control(lease, name ? 'rollback to ' + identifier(name) : 'rollback')
throw error instanceof PostgresError && error.code === '25P02' && uncaught || error
}
if (stopped()) {
rolled && await rolled
context && !name && (context.outcome = 'rollback')
if (after)
throw after
return result
}
if (!name) {
const done = await control(lease, prepare ? 'prepare transaction \'' + prepare.replace(/'/g, '\'\'') + '\'' : 'commit')
context && (context.outcome = prepare ? 'prepare' : done.command === 'COMMIT' ? 'commit' : 'rollback')
if (!prepare && done.command !== 'COMMIT')
throw Errors.generic('TRANSACTION_ROLLED_BACK', 'The server rolled back the transaction instead of committing it')
}
return result
function savepoint(name, fn) {
if (name && Array.isArray(name.raw))
return savepoint(sql => sql.apply(sql, arguments))
arguments.length === 1 && (fn = name, name = null)
return scope(fn, 's' + savepoints++ + (name ? '_' + name : ''), false, stopped)
}
async function nested(inner, fn) {
!fn && (fn = inner, inner = '')
if (normal(inner) && normal(inner) !== normal(mode))
throw Errors.generic('NESTED_TRANSACTION_MODE', 'A nested transaction runs in a savepoint, which can not change the transaction mode')
return scope(fn, 's' + savepoints++, true, stopped)
}
}
}
function rolledBack() {
return Errors.generic('TRANSACTION_ROLLED_BACK', 'The query was not sent, the transaction was rolled back with sql.rollback()')
}
function normal(x) {
return (x || '').toLowerCase().replace(/[^a-z ]/g, '').replace(/\s+/g, ' ').trim()
}
function identifier(x) {
return '"' + x.replace(/"/g, '""') + '"'
}
function control(lease, text) {
const q = new Query([text], [], Object.assign(q => lease.send(q), { options, cancel: pool.cancel }), { simple: true })
return q.execute()
}
async function end(options) {
await Promise.all([
listener.end(),
subscribe.sql ? subscribe.sql.end({ timeout: 0 }) : null
])
return pool.end(options)
}
function close() {
return pool.close()
}
}
function Listen(options) {
const channels = new Map()
let sql = null
, closing = null
return {
listen,
end: () => closing || (sql ? (closing = sql.end({ timeout: 0 })) : Promise.resolve())
}
function connection() {
return sql || (sql = Postgres({
...options,
max: 1,
idle_timeout: null,
max_lifetime: null,
fetch_types: false,
no_subscribe: true,
onclose() {
if (closing)
return
channels.forEach((channel, name) => {
channel.result = start(name)
channel.result.then(result => {
Object.assign(channel.state, result.state)
channel.listeners.forEach(l => l.onlisten && l.onlisten())
}, () => {})
})
},
onnotify(channel, payload) {
channels.has(channel) && [...channels.get(channel).listeners].forEach(l => l.fn(payload))
}
}))
}
function start(name) {
const x = connection()
return x`listen ${ x.unsafe('"' + name.replace(/"/g, '""') + '"') }`
}
async function listen(name, fn, onlisten) {
const listener = { fn, onlisten }
if (!channels.has(name))
channels.set(name, { listeners: new Set(), result: start(name), state: {} })
const channel = channels.get(name)
channel.listeners.add(listener)
let result
try {
result = await channel.result
} catch (error) {
channel.listeners.delete(listener)
channel.listeners.size || channels.delete(name)
throw error
}
Object.assign(channel.state, result.state)
listener.onlisten && listener.onlisten()
return { state: channel.state, unlisten }
async function unlisten() {
if (!channel.listeners.delete(listener) || channel.listeners.size)
return
channels.get(name) === channel && channels.delete(name)
const x = sql
if (closing || !x)
return
await x`unlisten ${ x.unsafe('"' + name.replace(/"/g, '""') + '"') }`.catch(e => {
if (!/^CONNECTION_/.test(e.code))
throw e
})
if (channels.size === 0 && sql === x) {
sql = null
await x.end()
}
}
}
}
function parseOptions(a, b) {
if (a && a.shared)
return a
const env = process.env
, o = (!a || typeof a === 'string' ? b : a) || {}
, url = parseUrl(a)
, query = [...url.url.searchParams].reduce((a, [b, c]) => (a[b] = c, a), {})
, targets = hosts(o, query, url, env)
, port = o.port || query.port || env.PGPORT || 5432
, user = o.user || o.username || url.url.username || env.PGUSERNAME || env.PGUSER || osUsername()
o.no_prepare && (o.prepare = false)
query.sslmode && (query.ssl = query.sslmode, delete query.sslmode)
'timeout' in o && (console.log('The timeout option is deprecated, use idle_timeout instead'), o.idle_timeout = o.timeout)
query.sslrootcert === 'system' && (query.ssl = 'verify-full')
const ints = ['idle_timeout', 'connect_timeout', 'max_lifetime', 'max_pipeline', 'backoff', 'keep_alive', 'min', 'queue_timeout']
const defaults = {
max : globalThis.Cloudflare ? 3 : 10,
min : 0,
ssl : false,
sslnegotiation : null,
channel_binding : 'prefer',
idle_timeout : null,
connect_timeout : 30,
queue_timeout : 0,
max_lifetime : max_lifetime,
max_pipeline : 100,
scheduler : true,
backoff : backoff,
keep_alive : 60,
prepare : true,
debug : false,
fetch_types : true,
infer : false,
publications : 'alltables',
slot : null,
target_session_attrs: null
}
const options = {
host : targets.map(x => x[0]),
port : Array.isArray(port) ? port : targets.map(x => o.port || query.port ? +port : x[1] || +port),
path : o.path || targets[0][0].indexOf('/') > -1 && targets[0][0] + '/.s.PGSQL.' + (targets[0][1] || port),
database : o.database || o.db || (url.url.pathname || '').slice(1) || env.PGDATABASE || (typeof user === 'function' ? null : user),
user : user,
pass : o.pass || o.password || url.url.password || env.PGPASSWORD || '',
...Object.entries(defaults).reduce(
(acc, [k, d]) => {
const value = k in o && o[k] !== undefined ? o[k] : k in query
? (query[k] === 'disable' || query[k] === 'false' ? false : query[k])
: env['PG' + k.toUpperCase()] || d
acc[k] = typeof value === 'string' && ints.includes(k)
? +value
: value
return acc
},
{}
),
connection : {
application_name: env.PGAPPNAME || 'postgres.js',
...o.connection,
...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || k === 'host' || k === 'port' || (acc[k] = v), acc), {})
},
types : o.types || {},
target_session_attrs: tsa(o, url.url, env),
channel_binding : binding(o, url.url, env),
onnotice : o.onnotice,
onnotify : o.onnotify,
onclose : o.onclose,
onparameter : o.onparameter,
socket : o.socket,
onread : o.onread !== false,
open : o.open,
no_subscribe : o.no_subscribe,
unref_sockets : !!o.unref_sockets,
passfile : passfile(env),
shared_types : o.shared_types,
transform : parseTransform(o.transform || { undefined: undefined }),
parameters : {},
shared : { retries: 0 },
timers : o.timers || {
setTimeout,
clearTimeout,
setImmediate,
clearImmediate,
now: () => performance.now()
}
}
options.infer = options.infer === true || options.infer === 'true'
options.max === 1 && (options.scheduler = false)
return options
}
function tsa(o, url, env) {
const x = o.target_session_attrs || url.searchParams.get('target_session_attrs') || env.PGTARGETSESSIONATTRS
if (!x || ['read-write', 'read-only', 'primary', 'standby', 'prefer-standby'].includes(x))
return x
throw new Error('target_session_attrs ' + x + ' is not supported')
}
function binding(o, url, env) {
const x = o.channel_binding || url.searchParams.get('channel_binding') || env.PGCHANNELBINDING || 'prefer'
if (['disable', 'prefer', 'require'].includes(x))
return x
throw new Error('channel_binding ' + x + ' is not supported')
}
function backoff(retries) {
return (0.5 + Math.random() / 2) * Math.min(3 ** retries / 100, 20)
}
function max_lifetime() {
return 60 * (30 + Math.random() * 30)
}
function parseTransform(x) {
return {
undefined: x.undefined,
column: {
from: typeof x.column === 'function' ? x.column : x.column && x.column.from,
to: x.column && x.column.to
},
value: {
from: typeof x.value === 'function' ? x.value : x.value && x.value.from,
to: x.value && x.value.to
},
row: {
from: typeof x.row === 'function' ? x.row : x.row && x.row.from,
to: x.row && x.row.to
}
}
}
function parseUrl(url) {
if (!url || typeof url !== 'string')
return { url: { searchParams: new Map() }, targets: [] }
const rest = url.slice(url.indexOf('://') + 3)
, end = rest.search(/[/?]/)
, authority = end === -1 ? rest : rest.slice(0, end)
, at = authority.lastIndexOf('@')
, userinfo = at === -1 ? '' : authority.slice(0, at)
, colon = userinfo.indexOf(':')
, tail = new URL('postgres://x' + (end === -1 ? '' : rest.slice(end)))
return {
url: {
username: decode(colon === -1 ? userinfo : userinfo.slice(0, colon)),
password: colon === -1 ? '' : decode(userinfo.slice(colon + 1)),
pathname: decode(tail.pathname),
searchParams: tail.searchParams
},
targets: authority.slice(at + 1) ? split(authority.slice(at + 1), decode) : []
}
}
function decode(x) {
try {
return decodeURIComponent(x)
} catch (_) {
return x
}
}
function hosts(o, query, url, env) {
const given = o.hostname || o.host || query.host
if (given)
return Array.isArray(given) ? given.map(x => [x, null]) : split(given)
if (url.targets.some(([x]) => x))
return url.targets
return split(env.PGHOST || 'localhost').map(([x, p], i) => [x, p || (url.targets[i] && url.targets[i][1]) || null])
}
function split(x, decoded = x => x) {
return x.split(',').filter(Boolean).map(x => {
const m = x.match(/^\[([^\]]*)\](?::(\d*))?$/) || x.match(/^([^:]*)(?::(\d*))?$/) || [x, x]
return [decoded(m[1]), m[2] ? +m[2] : null]
})
}
function passfile(env) {
try {
return env.PGPASSFILE || (process.platform === 'win32'
? (env.APPDATA || '') + '\\postgresql\\pgpass.conf'
: os.homedir() + '/.pgpass')
} catch (_) {
return null
}
}
function osUsername() {
try {
return os.userInfo().username
} catch (_) {
return process.env.USERNAME || process.env.USER || process.env.LOGNAME
}
}
function view(known, get) {
return new Proxy(known, {
get: (x, oid) => typeof oid === 'symbol' ? x[oid] : get(+oid)
})
}
+72
View File
@@ -0,0 +1,72 @@
import Stream from 'stream'
export default function largeObject(sql, oid, mode = 0x00020000 | 0x00040000) {
return new Promise(async(resolve, reject) => {
await sql.begin(async sql => {
let finish
!oid && ([{ oid }] = await sql`select lo_creat(-1) as oid`)
const [{ fd }] = await sql`select lo_open(${ oid }, ${ mode }) as fd`
const lo = {
writable,
readable,
close : () => sql`select lo_close(${ fd })`.then(finish),
tell : () => sql`select lo_tell64(${ fd })`,
read : (x) => sql`select loread(${ fd }, ${ x }) as data`,
write : (x) => sql`select lowrite(${ fd }, ${ x })`,
truncate : (x) => sql`select lo_truncate64(${ fd }, ${ x })`,
seek : (x, whence = 0) => sql`select lo_lseek64(${ fd }, ${ x }, ${ whence })`,
size : () => sql`
select
lo_lseek64(${ fd }, location, 0) as position,
seek.size
from (
select
lo_lseek64($1, 0, 2) as size,
tell.location
from (select lo_tell64($1) as location) tell
) seek
`
}
resolve(lo)
return new Promise(async r => finish = r)
async function readable({
highWaterMark = 2048 * 8,
start = 0,
end = Infinity
} = {}) {
let max = end - start
start && await lo.seek(start)
return new Stream.Readable({
highWaterMark,
async read(size) {
const l = Math.min(size, max)
max -= l
const [{ data }] = await lo.read(l)
data.length && this.push(data)
if (data.length < l || max === 0)
this.push(null)
}
})
}
async function writable({
highWaterMark = 2048 * 8,
start = 0
} = {}) {
start && await lo.seek(start)
return new Stream.Writable({
highWaterMark,
write(chunk, encoding, callback) {
lo.write(chunk).then(() => callback(), callback)
}
})
}
}).catch(reject)
})
}
+814
View File
@@ -0,0 +1,814 @@
import { open as startup } from './startup.js'
import { Connection, slow, notable } from './connection.js'
import { Query } from './query.js'
import { Errors, PostgresError, queryError } from './errors.js'
import { channels, listened } from './diagnostics.js'
const begins = /\bbegin\b/i
, starts = /\bstart\b/gi
, transaction = /^transaction\b/i
export function Pool(options, types) {
const { setTimeout, clearTimeout, now } = options.timers
, statements = Statements(options.scheduler)
, connections = new Set()
, waiting = Queue()
, events = {
parser: types.parser,
decoder: types.decoder,
serializer: types.serializer,
onready,
onclose,
onnotify: options.onnotify,
onparameter,
ontimed
}
, work = { fast: 0, slow: 0, at: 0 }
const signal = { aborted: false, sockets: new Set() }
, replacing = []
let opening = 0
, ending = null
, used = false
, endTimer = null
, retries = 0
, failingSince = null
, nextAttempt = 0
, retryTimer = null
, typesLoaded = options.fetch_types === false
, typesError = null
, typesFailed = 0
, failure = null
, typesLoading = false
const pool = {
query,
lease,
cancel,
end,
close,
get stats() {
return {
connections: connections.size,
opening,
idle: [...connections].filter(x => x.inflight === 0 && !x.lease).length,
leased: [...connections].filter(x => x.lease).length,
waiting: waiting.length
}
}
}
return pool
function query(q, lease) {
if (ending && !lease)
return q.reject(queryError(Errors.connection('CONNECTION_ENDED', options, options.host[0], options.port[0]), q, options.debug))
let built
try {
built = q.build()
} catch (error) {
return unbuilt(q, error, lease)
}
const free = !lease && options.max !== 1
if (q.options.simple && !built.values.length) {
const known = (free || options.scheduler) && statements.known(built.text)
q.alone = free && alone(known)
options.scheduler && (q.cost = known)
return place({ q, simple: built.text, exclusive: !!q.streaming || q.alone }, lease)
}
if (q.onlyDescribe)
return describeOnly(q, built, lease)
const statement = statements.get(built, options.prepare && q.options.prepare !== false)
q.alone = free && alone(statement.live ? statement : statements.known(built.text))
q.wire = { statement, built }
options.scheduler && (q.cost = statement.cost)
place({ q, statement, exclusive: !!q.cursorFn || !!q.streaming || q.alone }, lease)
}
function describeOnly(q, built, lease) {
const statement = { name: '', text: built.text, oids: built.oids, columns: null }
, run = c => (q.connection = c, c.describe(statement, built.oids).then(
({ params, columns }) => q.resolve({ string: built.text, types: params, name: '', columns }),
error => q.reject(queryError(error, q, options.debug))
), q.onwrite && q.onwrite())
lease ? run(lease.connection) : place({ run, q, reject: error => q.reject(queryError(error, q, options.debug)) })
}
function unbuilt(q, error, lease) {
error = queryError(error, q, options.debug)
if (!lease || lease.released || lease.connection.closed)
return q.reject(error)
q.connection = lease.connection
lease.connection.local(q, error)
q.onwrite && q.onwrite()
}
function place(job, lease) {
used = true
const missing = !lease && retype()
if (missing)
return reject(job, missing)
if (job.q && job.q.cancelled)
return job.q.reject(queryError(Errors.generic('57014', 'canceling statement due to user request'), job.q, options.debug))
if (lease) {
return lease.connection.closed ? job.q.reject(queryError(lease.connection.closed, job.q, options.debug))
: lease.released ? job.q.reject(queryError(ended(lease), job.q, options.debug))
: run(lease.connection, job)
}
if (signal.aborted)
return reject(job, Errors.connection('CONNECTION_DESTROYED', options, options.host[0], options.port[0]))
if (failingFast())
return reject(job, copy(failure.error))
const c = waiting.length === 0 && pick(job)
if (c)
return run(c, job)
waiting.push(wait(job))
drain()
}
function wait(job) {
if (!options.queue_timeout)
return job
job.timer = setTimeout(() => {
job.timer = null
waiting.remove(x => x === job) && reject(job, Errors.generic(
'QUEUE_TIMEOUT', 'Waited more than ' + options.queue_timeout + ' seconds for a connection'
))
}, options.queue_timeout * 1000)
typeof job.timer.unref === 'function' && job.timer.unref()
return job
}
function started(job) {
job.timer && (clearTimeout(job.timer), job.timer = null)
}
function run(c, job) {
started(job)
hold(c)
if (job.run)
return job.run(c)
job.q.connection = c
job.simple !== undefined
? c.simple(job.q, job.simple)
: c.send(job.q)
job.q.onwrite && job.q.onwrite()
}
function drain() {
while (waiting.length) {
const job = waiting.peek()
, c = pick(job)
if (!c && !copying(job))
break
waiting.shift()
!c ? reject(job, Errors.generic('COPY_IN_PROGRESS', 'You cannot execute queries during copy'))
: job.lease ? grant(c, job)
: run(c, job)
}
waiting.length && listened(channels.pool) && event('wait', null, { waiting: waiting.length })
grow()
}
function plain(job) {
return !!job.q && !job.lease && !job.exclusive && !job.run
}
function copying(job) {
if (!job.q || connections.size + opening < options.max || connections.size === 0)
return false
for (const c of connections) {
if (!c.copying)
return false
}
return true
}
function usable(c) {
return !c.closed && !c.closing && !c.lease && !c.releasing && !c.retiring && !c.exclusive && !c.loading
}
function pick(job) {
const statement = job.statement
, any = !statement || !statement.name
let best = null
for (const c of connections) {
const free = usable(c)
if (free && c.inflight === 0) {
if (any || c.prepared.has(statement.name))
return c
best = best && best.inflight === 0 ? best : c
} else if (free && !job.lease && !job.exclusive
&& c.inflight < options.max_pipeline && (!best || c.inflight < best.inflight)) {
best = c
}
}
if (best && best.inflight === 0)
return best
if (connections.size + opening < options.max && !failingSince)
return null
best && options.scheduler && job.q && needed(job) && (best = scheduled(job) || best)
return best && opening && best.inflight >= Math.ceil(waiting.length / (opening + 1))
? null
: best
}
function needed(job) {
if (!job.q.cost || job.q.cost.est === undefined || job.q.cost.est >= notable)
return true
for (const c of connections) {
if (c.notable)
return true
}
return false
}
function scheduled(job) {
const t = now()
, e = job.q.cost ? job.q.cost.est : undefined
if (e === undefined || e < slow)
return soonest(t, Infinity, false)
let heavy = 0
for (const c of connections)
c.inflight && c.heavy() && heavy++
decay(t)
const reserve = Math.max(1, Math.min(Math.ceil(work.fast / 2000 * 1.5) + 1, connections.size - Math.ceil(work.slow / 2000)))
return heavy < connections.size - reserve
? soonest(t, Infinity, false)
: soonest(t, 2 * e, true) || soonest(t, Infinity, false)
}
function soonest(t, most, heavy) {
let best = null
, value = Infinity
, ties = 0
for (const c of connections) {
const v = usable(c) && c.inflight < options.max_pipeline && (!heavy || c.heavy()) ? c.backlog(t) : Infinity
if (v <= most && v < value)
(best = c, value = v, ties = 1)
else if (v <= most && v === value && v !== Infinity && Math.random() * ++ties < 1)
best = c
}
return best
}
function ontimed(sample, t) {
decay(t)
sample < slow ? work.fast += sample : work.slow += sample
}
function decay(t) {
const d = Math.exp((work.at - t) / 2000)
work.fast *= d
work.slow *= d
work.at = t
}
function grow() {
if (ending && waiting.length === 0)
return
if (failingSince !== null) {
if (opening || !waiting.length || retryTimer)
return
const wait = nextAttempt - now()
return wait > 0
? (retryTimer = setTimeout(() => (retryTimer = null, grow()), wait))
: connect()
}
let loading = 0
for (const c of connections)
c.loading && loading++
const wanted = Math.max(
Math.min(waiting.length - opening - loading, options.max - connections.size - opening),
used && !ending ? Math.min(options.min, options.max) - connections.size - opening : 0
)
for (let i = 0; i < wanted; i++)
connect()
}
function connect() {
opening++
retryTimer && (clearTimeout(retryTimer), retryTimer = null)
;(options.open || startup)(options, signal, replacing.length ? replacing.shift() : 0).then(opened, failed)
}
function copy(error) {
if (!error || typeof error !== 'object')
return error
const x = Object.create(Object.getPrototypeOf(error), Object.getOwnPropertyDescriptors(error))
Object.defineProperty(x, 'stack', { value: error.stack, writable: true, configurable: true })
;['query', 'parameters', 'args', 'types'].forEach(k => delete x[k])
return x
}
function failingFast() {
return !!failure && retries > 1 && opening === 0 && now() < nextAttempt && stuck()
}
function stuck() {
for (const c of connections) {
if (!c.lease)
return false
}
return true
}
function opened(ready) {
opening--
retries = 0
failingSince = null
failure = null
if (signal.aborted || (ending && waiting.length === 0)) {
ready.socket.destroy()
return finishing()
}
Object.entries(ready.parameters).forEach(([k, v]) => onparameter(k, v))
const c = Connection(ready, options, events)
c.index = ready.index || 0
connections.add(c)
lifetime(c)
listened(channels.pool) && event('open', c)
typesLoaded || typesLoading ? (c.loading = !typesLoaded) : loadTypes(c)
drain()
}
function retype() {
if (!typesError)
return null
if (now() - typesFailed < 1000)
return copy(typesError)
typesError = null
connections.forEach(x => x.loading = true)
;[...connections].some(x => !x.closed && (loadTypes(x), true))
return null
}
function loadTypes(c) {
hold(c)
typesLoading = true
c.loading = true
types.load(text => new Promise((resolve, reject) => {
const q = new Query([text], [], Object.assign(() => {}, { options }), { simple: true })
q.executed = true
q.then(resolve, reject)
c.simple(q, text)
})).then(() => typesLoaded = true, error => error instanceof PostgresError
? typesLoaded = true
: error && error.code === 'TYPE_NOT_FOUND' && (typesError = error, typesFailed = now())
).then(() => {
typesLoading = false
connections.forEach(x => x.loading = !typesLoaded && !typesError)
typesError
? waiting.take().forEach(job => reject(job, copy(typesError)))
: typesLoaded || [...connections].some(x => !x.closed && (loadTypes(x), true))
drain()
})
}
function failed(error) {
opening--
if (signal.aborted)
return finishing()
failingSince === null && (failingSince = now())
retries++
nextAttempt = now() + 1000 * (typeof options.backoff === 'function' ? options.backoff(retries) : options.backoff)
const transient = !!error && (error.code === 'CONNECTION_CLOSED' || error.code === '53300')
, expired = now() - failingSince >= options.connect_timeout * 1000
failure = !transient || expired ? { error } : null
stuck() && opening === 0 && (failure || ending) && waiting.take().forEach((job, i) => reject(job, i ? copy(error) : error))
ending ? finishing() : grow()
}
function reject(job, error) {
started(job)
job.lease ? job.reject(error) : job.q ? job.q.reject(queryError(error, job.q, options.debug)) : job.reject(error)
}
function lifetime(c) {
const life = typeof options.max_lifetime === 'function' ? options.max_lifetime() : options.max_lifetime
life && (c.lifeTimer = unref(setTimeout(() => {
c.retiring = true
c.inflight === 0 && !c.lease && c.close()
}, life * 1000)))
idle(c)
}
function event(name, c, extra) {
channels.pool.publish({
event: name,
connection: c ? c.id : null,
host: c ? c.host : null,
port: c ? c.port : null,
database: options.database,
stats: pool.stats,
...extra
})
}
function idle(c) {
listened(channels.pool) && event('idle', c)
if (c.inflight || c.lease)
return
options.unref_sockets && !c.loading && waiting.length === 0 && c.unref()
if (!options.idle_timeout)
return
c.idleSince = now()
c.idleTimer || (c.idleTimer = unref(setTimeout(() => idled(c), options.idle_timeout * 1000)))
}
function hold(c) {
options.unref_sockets && c.ref()
}
function unref(timer) {
options.unref_sockets && timer && typeof timer.unref === 'function' && timer.unref()
return timer
}
function idled(c) {
c.idleTimer = null
if (c.inflight || c.lease || c.closed)
return
if (connections.size <= options.min)
return
const left = options.idle_timeout * 1000 - (now() - c.idleSince)
left >= 1
? c.idleTimer = unref(setTimeout(() => idled(c), left))
: c.close()
}
function onready(c) {
if (c.releasing) {
if (c.inflight)
return
c.releasing = false
if (c.status !== 'I')
return c.close()
}
if (c.inflight === 0 && !c.lease && (c.retiring || (ending && waiting.length === 0)))
return c.close()
idle(c)
while (waiting.length > connections.size && usable(c) && c.inflight < options.max_pipeline && plain(waiting.peek()))
run(c, waiting.shift())
waiting.length && !c.lease && c.inflight < options.max_pipeline && drain()
ending && waiting.length === 0 && finishing()
}
function onclose(c, error) {
connections.delete(c)
ending || replacing.push((c.index + 1) % (options.path ? 1 : options.host.length))
replacing.length > options.max && replacing.shift()
c.retired && c.retired.forEach(r => r())
c.idleTimer && clearTimeout(c.idleTimer)
c.lifeTimer && clearTimeout(c.lifeTimer)
c.lease && c.lease.onclose && c.lease.onclose(error.code === 'CONNECTION_DESTROYED' ? Errors.connection('CONNECTION_CLOSED', options, c.host, c.port) : error)
options.onclose && options.onclose(c.id)
listened(channels.pool) && event('close', c, { error })
connections.size === 0 && opening === 0 && statements.clear()
drain()
ending && finishing()
}
function onparameter(k, v) {
options.parameters[k] !== v && (options.parameters[k] = v, options.onparameter && options.onparameter(k, v))
}
async function lease(kind) {
used = true
await 1
if (ending)
throw Errors.connection('CONNECTION_ENDED', options, options.host[0], options.port[0])
if (failingFast())
throw copy(failure.error)
const missing = retype()
if (missing)
throw missing
return new Promise((resolve, reject) => {
waiting.push(wait({ lease: true, kind, resolve, reject, queued: listened(channels.pool) ? now() : 0 }))
drain()
})
}
function grant(c, job) {
started(job)
hold(c)
c.idleTimer && (clearTimeout(c.idleTimer), c.idleTimer = null)
let chain = Promise.resolve()
, released = false
, lost
const closed = new Promise((_, reject) => lost = reject)
closed.catch(() => {})
const x = {
kind: job.kind,
connection: c,
closed,
get released() { return released },
send(q) {
if (c.closed)
return q.reject(queryError(c.closed, q, options.debug))
if (released)
return q.reject(queryError(ended(x), q, options.debug))
chain = chain.then(() => new Promise(resolve => {
q.onwrite = resolve
q.then(resolve, resolve)
query(q, x)
}))
},
release() {
if (released)
return
released = true
c.lease === x && (c.lease = null)
listened(channels.pool) && event('release', c, { kind: job.kind })
if (c.closed)
return
if (c.inflight)
return (c.releasing = true)
c.status === 'I' ? onready(c) : c.close()
},
onclose(error) {
lost(error)
}
}
c.lease = x
listened(channels.pool) && event('lease', c, { kind: job.kind, waited: job.queued ? now() - job.queued : 0 })
job.resolve(x)
}
function ended(lease) {
return Errors.generic('UNSAFE_TRANSACTION', 'The ' + lease.kind + ' has ended')
}
function cancel(q) {
if (waiting.remove(x => x.q === q && (started(x), true))) {
q.reject(queryError(Errors.generic('57014', 'canceling statement due to user request'), q, options.debug))
return Promise.resolve()
}
if (q.connection)
return q.connection.cancel(q)
q.cancelled = true
return Promise.resolve()
}
function end({ timeout = null } = {}) {
if (ending)
return ending
ending = new Promise(resolve => {
pool.ended = resolve
timeout !== null && (endTimer = setTimeout(destroy, timeout * 1000))
retryTimer && (clearTimeout(retryTimer), retryTimer = null)
Promise.resolve().then(finishing)
})
return ending
}
function finishing() {
if (!pool.ended)
return
if (waiting.length === 0)
connections.forEach(c => c.inflight === 0 && !c.lease && c.close())
if (connections.size === 0 && opening === 0 && waiting.length) {
const error = Errors.connection('CONNECTION_ENDED', options, options.host[0], options.port[0])
waiting.take().forEach(job => reject(job, error))
}
if (connections.size === 0 && (opening === 0 || signal.aborted) && waiting.length === 0) {
endTimer && clearTimeout(endTimer)
retryTimer && clearTimeout(retryTimer)
const resolve = pool.ended
pool.ended = null
resolve()
}
}
function destroy() {
endTimer = null
signal.aborted = true
signal.sockets.forEach(x => x.destroy())
const error = Errors.connection('CONNECTION_DESTROYED', options, options.host[0], options.port[0])
waiting.take().forEach(job => reject(job, error))
connections.forEach(c => c.destroy())
connections.size === 0 && finishing()
}
function close() {
used = false
return Promise.all([...connections].map(c => c.lease
? new Promise(r => (c.retiring = true, (c.retired || (c.retired = [])).push(r)))
: c.close()
))
}
}
export function Queue() {
let xs = []
, head = 0
return {
get length() { return xs.length - head },
push: x => xs.push(x),
peek: () => xs[head],
shift() {
const x = xs[head]
xs[head++] = undefined
head === xs.length ? (xs = [], head = 0) : head > 1024 && head * 2 > xs.length && (xs = xs.slice(head), head = 0)
return x
},
take() {
const all = xs.slice(head)
xs = []
head = 0
return all
},
remove(fn) {
for (let i = head; i < xs.length; i++) {
if (fn(xs[i]))
return (xs.splice(i, 1), true)
}
return false
}
}
}
const limit = 2000
function alone(x) {
return x.alone === undefined
? x.alone = opens(x.text)
: x.alone
}
function opens(text) {
if (begins.test(text))
return true
starts.lastIndex = 0
let match
while ((match = starts.exec(text))) {
const from = match.index + 5
let i = from
while (true) {
while (i < text.length && /\s/.test(text[i]))
i++
if (text[i] !== '/' || text[i + 1] !== '*')
break
const end = text.indexOf('*/', i + 2)
if (end === -1)
return false
i = end + 2
}
if (i > from && transaction.test(text.slice(i, i + 12)))
return true
starts.lastIndex = Math.max(starts.lastIndex, i)
}
return false
}
function Statements(scheduler) {
const prefix = Math.random().toString(36).slice(2, 10)
, map = new Map()
, templates = new WeakMap()
let counter = 1
, clock = 0
const texts = new Map()
return { get, clear, known }
function known(text) {
let x = texts.get(text)
if (x)
return x
texts.set(text, x = { text, est: undefined, alone: undefined })
texts.size > limit && texts.delete(texts.keys().next().value)
return x
}
function get(built, prepare) {
if (!prepare)
return unnamed(built.text)
const tagged = !built.dynamic && (templates.get(built.strings) || set(built.strings))
, cached = tagged && tagged.get(built.types)
if (cached && cached.live)
return (cached.used = clock++, cached)
const key = built.types + '\0' + built.text
let statement = map.get(key)
statement ? statement.used = clock++ : statement = add(key, built.text)
tagged && tagged.set(built.types, statement)
return statement
}
function unnamed(text) {
return {
key: null, live: false, name: '', text, ends: undefined, shape: undefined, version: 0, used: 0,
cost: scheduler ? known(text) : null
}
}
function add(key, text) {
const statement = {
key, live: true, name: prefix + counter++, text, ends: undefined, shape: undefined, version: 0, used: clock++,
cost: { est: undefined }, alone: undefined
}
map.set(key, statement)
map.size > limit && evict()
return statement
}
function evict() {
let oldest = null
for (const x of map.values())
(!oldest || x.used < oldest.used) && (oldest = x)
oldest.live = false
map.delete(oldest.key)
}
function set(strings) {
const x = new Map()
templates.set(strings, x)
return x
}
function clear() {
map.forEach(x => x.live = false)
map.clear()
}
}
+458
View File
@@ -0,0 +1,458 @@
export function Writer(size = 16384) {
let b = Buffer.allocUnsafe(size)
, i = 0
, at = 0
const w = {
get length() { return i },
type(x) {
fit(5)
at = i
b[i] = x
i += 5
return w
},
untyped() {
fit(4)
at = i
i += 4
return w
},
end() {
b.writeInt32BE(i - at - 1, at + 1)
return w
},
endUntyped() {
b.writeInt32BE(i - at, at)
return w
},
str(x) {
fit(x.length * 3 + 1)
i += b.write(x, i)
b[i++] = 0
return w
},
bytes(x) {
if (typeof x === 'string') {
fit(x.length * 3)
i += b.write(x, i)
} else {
fit(x.length)
b.set(x, i)
i += x.length
}
return w
},
value(x) {
if (x === null || x === undefined)
return w.i32(-1)
const n = i
i += 4
w.bytes(x)
b.writeInt32BE(i - n - 4, n)
return w
},
u8(x) {
fit(1)
b[i++] = x
return w
},
i16(x) {
fit(2)
b.writeInt16BE(x, i)
i += 2
return w
},
u16(x) {
fit(2)
b.writeUInt16BE(x, i)
i += 2
return w
},
i32(x) {
fit(4)
b.writeInt32BE(x, i)
i += 4
return w
},
truncate(n) {
i = n
return w
},
take() {
const out = i < 4096 ? Buffer.from(b.subarray(0, i)) : b.subarray(0, i)
i >= 4096 && (b = Buffer.allocUnsafe(size))
i = at = 0
return out
}
}
return w
function fit(n) {
if (b.length - i >= n)
return
const prev = b
b = Buffer.allocUnsafe(Math.max(size, prev.length * 2, i + n))
prev.copy(b, 0, 0, i)
}
}
const own = Writer(1024)
const S = 83
, H = 72
, X = 88
, c = 99
, Q = 81
, p = 112
, d = 100
, f = 102
, D = 68
, C = 67
, E = 69
, P = 80
, B = 66
export const sync = w => w.type(S).end()
export const flush = w => w.type(H).end()
export const terminate = w => w.type(X).end()
export const copyDone = w => w.type(c).end()
export const sslRequest = w => w.untyped().i32(80877103).endUntyped()
export const query = (w, text) => w.type(Q).str(text).end()
export const password = (w, x) => w.type(p).str(x).end()
export const saslResponse = (w, x) => w.type(p).bytes(x).end()
export const copyData = (w, x) => w.type(d).bytes(x).end()
export const copyFail = (w, x) => w.type(f).str(x).end()
export const describe = (w, kind, name) => w.type(D).u8(kind.charCodeAt(0)).str(name).end()
export const close = (w, kind, name) => w.type(C).u8(kind.charCodeAt(0)).str(name).end()
export const execute = (w, portal, rows = 0) => w.type(E).str(portal).i32(rows).end()
export const cancel = (w, pid, secret) => w.untyped().i32(80877102).i32(pid).i32(secret).endUntyped()
export const Sync = one(sync)
export const Flush = one(flush)
export const Terminate = one(terminate)
export const CopyDone = one(copyDone)
export const SSLRequest = one(sslRequest)
export const Query = one(query)
export const Password = one(password)
export const SASLResponse = one(saslResponse)
export const CopyData = one(copyData)
export const CopyFail = one(copyFail)
export const Describe = one(describe)
export const Close = one(close)
export const Execute = one(execute)
export const Cancel = one(cancel)
export const Startup = one(startup)
export const SASL = one(sasl)
export const Parse = one(parse)
export const Bind = one(bind)
export const Run = one(run)
function one(fn) {
return (...xs) => {
try {
fn(own, ...xs)
} catch (error) {
own.truncate(0)
throw error
}
return own.take()
}
}
export function startup(w, parameters) {
w.untyped().i32(196608)
for (const k in parameters)
parameters[k] && w.str(unbroken(k, k)).str(unbroken(k, '' + parameters[k]))
return w.u8(0).endUntyped()
}
function unbroken(k, x) {
if (x.indexOf('\0') !== -1)
throw Object.assign(new Error('The connection parameter ' + k + ' contains a NUL character'), { code: 'NUL_CHARACTER' })
return x
}
export function sasl(w, mechanism, initial) {
return w.type(p).str(mechanism).i32(Buffer.byteLength(initial)).bytes(initial).end()
}
export function parse(w, name, text, oids = []) {
w.type(P).str(name).str(text).u16(oids.length)
for (let j = 0; j < oids.length; j++)
w.i32(oids[j] | 0)
return w.end()
}
export function bind(w, portal, name, values = [], formats = [], resultFormats = []) {
w.type(B).str(portal).str(name).i16(formats.length)
for (let j = 0; j < formats.length; j++)
w.i16(formats[j])
w.u16(values.length)
for (let j = 0; j < values.length; j++)
w.value(values[j])
w.i16(resultFormats.length)
for (let j = 0; j < resultFormats.length; j++)
w.i16(resultFormats[j])
return w.end()
}
export function run(w, portal, name, values, rows, flushing) {
bind(w, portal, name, values)
execute(w, portal, rows)
return flushing ? flush(w) : sync(w)
}
const largest = 0x40000000
export function Frames(onframe, shared = false, onerror = null) {
const chunks = []
, owned = new WeakSet()
let data = null
, owning = false
, at = 0
, part = null
, filled = 0
, paused = false
, running = false
, broken = false
return {
push(x) {
if (broken)
return
chunks.push(shared && (paused || owning) ? own(x) : x)
run()
},
get paused() {
return paused
},
owns(x) {
return !shared || owned.has(x)
},
own(x) {
owning = x
x && settle()
},
pause() {
paused = true
settle()
},
resume() {
paused = false
run()
}
}
function own(x) {
const copy = Buffer.from(x)
owned.add(copy)
return copy
}
function settle() {
if (!shared)
return
data && at < data.length && !owned.has(data) && (data = own(data.subarray(at)), at = 0)
for (let i = 0; i < chunks.length; i++)
owned.has(chunks[i]) || (chunks[i] = own(chunks[i]))
}
function run() {
if (running)
return
running = true
try {
while (true) {
if (broken || paused || !(data && at < data.length || next()))
break
part ? fill() : frame()
}
} finally {
running = false
}
}
function invalid(length) {
if (length >= 4 && length <= largest)
return false
broken = true
chunks.length = 0
data = part = null
at = filled = 0
const error = Object.assign(new Error('The server sent a message with an invalid length (' + length + ')'), { code: 'PROTOCOL_ERROR' })
onerror && onerror(error)
return true
}
function next() {
data = chunks.shift() || null
at = 0
return data !== null
}
function frame() {
const left = data.length - at
if (left < 5)
return start(5)
const length = data.readInt32BE(at + 1)
if (invalid(length))
return
const end = at + 1 + length
if (end > data.length)
return start(end - at)
const begin = at
at = end
onframe(data[begin], data, begin, end)
}
function start(length) {
part = Buffer.allocUnsafe(length)
shared && owned.add(part)
filled = data.copy(part, 0, at)
at = data.length
}
function fill() {
const n = data.copy(part, filled, at, at + part.length - filled)
filled += n
at += n
if (filled < part.length)
return
if (part.length === 5) {
const head = part
, length = head.readInt32BE(1)
if (invalid(length))
return
part = Buffer.allocUnsafe(1 + length)
shared && owned.add(part)
filled = head.copy(part)
return filled === part.length && done()
}
done()
}
function done() {
const x = part
part = null
onframe(x[0], x, 0, x.length)
}
}
const errorFields = {
83: 'severity_local',
86: 'severity',
67: 'code',
77: 'message',
68: 'detail',
72: 'hint',
80: 'position',
112: 'internal_position',
113: 'internal_query',
87: 'where',
115: 'schema_name',
116: 'table_name',
99: 'column_name',
100: 'data_type_name',
110: 'constraint_name',
70: 'file',
76: 'line',
82: 'routine'
}
export function error(x, start, end) {
const fields = {}
for (let i = start + 5; i < end - 1;) {
const zero = x.indexOf(0, i + 1)
fields[errorFields[x[i]] || String.fromCharCode(x[i])] = x.toString('utf8', i + 1, zero)
i = zero + 1
}
return fields
}
export const text = typeof Buffer.prototype.utf8Slice === 'function'
? (x, start, end) => x.utf8Slice(start, end)
: (x, start, end) => x.toString('utf8', start, end)
const u32 = (x, i) => ((x[i] << 24) | (x[i + 1] << 16) | (x[i + 2] << 8) | x[i + 3]) >>> 0
, u16 = (x, i) => (x[i] << 8) | x[i + 1]
, field = (name, table, number, type) => ({ name, table, number, type })
export function rowDescription(x, start, end, make = field) {
const columns = new Array((x[start + 5] << 8) | x[start + 6])
for (let i = start + 7, j = 0; j < columns.length; j++) {
let zero = i
while (x[zero] !== 0)
zero++
columns[j] = make(text(x, i, zero), u32(x, zero + 1), u16(x, zero + 5), u32(x, zero + 7))
i = zero + 19
}
return columns
}
export function parameterDescription(x, start) {
const oids = new Array(x.readUInt16BE(start + 5))
for (let j = 0; j < oids.length; j++)
oids[j] = x.readUInt32BE(start + 7 + j * 4)
return oids
}
export function commandComplete(x, start, end) {
const first = start + 5
let e = end - 1
, count = null
while (true) {
let s = e - 1
while (s >= first && x[s] >= 48 && x[s] <= 57)
s--
if (s === e - 1 || s < first || x[s] !== 32)
break
count === null && (count = +text(x, s + 1, e))
e = s
}
return { command: text(x, first, e), count }
}
export function readyForQuery(x, start) {
return String.fromCharCode(x[start + 5])
}
export function notification(x, start) {
const zero = x.indexOf(0, start + 9)
return {
pid: x.readInt32BE(start + 5),
channel: x.toString('utf8', start + 9, zero),
payload: x.toString('utf8', zero + 1, x.indexOf(0, zero + 1))
}
}
export function parameterStatus(x, start) {
const zero = x.indexOf(0, start + 5)
return [x.toString('utf8', start + 5, zero), x.toString('utf8', zero + 1, x.indexOf(0, zero + 1))]
}
export function backendKeyData(x, start) {
return { pid: x.readInt32BE(start + 5), secret: x.readInt32BE(start + 9) }
}
+298
View File
@@ -0,0 +1,298 @@
import util from 'util'
import { build, isQuery, Join, joinStrings } from './build.js'
import { ours } from './errors.js'
import { channels, active, start, finish, now, userName } from './diagnostics.js'
import { statement } from './result.js'
const origins = new WeakMap()
export const CLOSE = {}
export class Query extends Promise {
constructor(strings, args, handler, options = {}) {
let resolve
, reject
super((a, b) => {
resolve = a
reject = b
})
this.fulfil = resolve
this.fail = reject
this[isQuery] = true
this.tagged = Array.isArray(strings.raw)
this.strings = strings
this.args = args
this.handler = handler
this.options = options
this.state = null
this.source = null
this.active = false
this.executed = false
this.settled = false
this.built = null
this.trace = null
this.aborts = null
this.originError = handler.debug ? new Error() : null
this.tagged && !handler.debug && !origins.has(strings) && origins.set(strings, located())
}
static get [Symbol.species]() {
return Promise
}
join(items) {
const kept = [...items].filter(x => x !== false && x !== null && x !== undefined)
, joined = new Query(joinStrings, [new Join(this, kept)], this.handler)
Object.defineProperty(joined, 'length', { value: kept.length })
return joined
}
resolve(x) {
this.aborts && this.aborts()
this.active = false
this.settled = true
this.trace && traced(this, null, x)
this.fulfil(x)
}
reject(x) {
this.aborts && this.aborts()
this.active = false
this.settled = true
this.trace && traced(this, x)
this.fail(x)
}
get origin() {
return this.originError
? this.originError.stack
: this.tagged && origins.get(this.strings) || ''
}
get statement() {
return this.source && statement(this.source)
}
build() {
return this.built || (this.built = build(this, this.handler.options))
}
get text() {
return this.build().text
}
get parameters() {
return this.build().values
}
[util.inspect.custom](depth, options, inspect) {
if (!this.handler.debug)
return 'Query {}'
let x
try {
x = this.build()
} catch (error) {
return 'Query { ' + error.message + ' }'
}
return 'Query ' + inspect({ text: x.text, parameters: x.values }, options)
}
cancel() {
return this.handler.cancel ? this.handler.cancel(this) : Promise.resolve()
}
signal(signal) {
if (!signal || this.settled)
return this
if (signal.aborted)
return (this.cancel(), this)
const abort = () => this.cancel()
signal.addEventListener('abort', abort, { once: true })
this.aborts = () => (this.aborts = null, signal.removeEventListener('abort', abort))
return this
}
simple() {
this.options.simple = true
this.options.prepare = false
return this
}
async readable() {
this.simple()
this.streaming = true
return this
}
async writable() {
this.simple()
this.streaming = true
return this
}
cursor(rows = 1, fn) {
this.options.simple = false
if (typeof rows === 'function') {
fn = rows
rows = 1
}
this.cursorRows = rows
if (typeof fn === 'function')
return (this.cursorFn = fn, this)
let prev
return {
[Symbol.asyncIterator]: () => ({
next: () => {
if (this.executed && !this.active)
return { done: true }
prev && prev()
const promise = new Promise((resolve, reject) => {
this.cursorFn = value => {
resolve({ value, done: false })
return new Promise(r => prev = r)
}
this.resolve = x => (this.active = false, this.settled = true, this.trace && traced(this, null, x), resolve({ done: true }))
this.reject = x => (this.active = false, this.settled = true, this.trace && traced(this, x), reject(x))
})
this.execute()
return promise
},
return() {
prev && prev(CLOSE)
return { done: true }
}
})
}
}
describe() {
this.options.simple = false
this.onlyDescribe = this.options.prepare = true
return this
}
stream() {
throw new Error('.stream has been renamed to .forEach')
}
forEach(fn) {
this.forEachFn = fn
this.handle()
return this
}
raw() {
this.isRaw = true
return this
}
values() {
this.isRaw = 'values'
return this
}
handle() {
if (this.executed)
return
this.executed = true
queueMicrotask(() => this.run())
}
run() {
active(channels.query)
? start(channels.query, this.trace = context(this), () => this.handler(this))
: this.handler(this)
}
execute() {
this.handle()
return this
}
then(fulfilled, rejected) {
this.handle()
return super.then(fulfilled, rejected)
}
catch(rejected) {
this.handle()
return super.catch(rejected)
}
finally(fn) {
this.handle()
return super.finally(fn)
}
}
function context(q) {
const options = q.handler.options || {}
let text = null
if (q.strings.length) {
try {
text = q.text
} catch (error) {
text = null
}
}
return {
query: text,
parameters: undefined,
database: options.database,
user: userName(options),
host: null,
port: null,
connection: null,
statement: null,
command: null,
count: null,
queued: now(),
sent: null,
firstByte: null,
complete: null
}
}
function traced(q, error, result) {
const x = q.trace
q.trace = null
x.complete = now()
if (result && typeof result === 'object' && 'command' in result) {
x.command = result.command
x.count = result.count
}
finish(channels.query, x, error, result)
}
function located() {
const limit = Error.stackTraceLimit
Error.stackTraceLimit = 8
try {
return trim(String(new Error().stack))
} catch (error) {
return ''
} finally {
Error.stackTraceLimit = limit
}
}
function trim(stack) {
const lines = stack.split('\n')
, i = lines.findIndex((x, i) => i > 0 && !ours(x))
return i === -1 ? stack : lines.slice(0, i + 1).join('\n')
}
+36
View File
@@ -0,0 +1,36 @@
export default class Result extends Array {
#count = null
#state = null
#command = null
#columns = null
#statement = null
get count() { return this.#count }
set count(x) { this.#count = x }
get state() { return this.#state }
set state(x) { this.#state = x }
get command() { return this.#command }
set command(x) { this.#command = x }
get columns() { return this.#columns }
set columns(x) { this.#columns = x }
get statement() {
const x = this.#statement
return x && x.kind ? (this.#statement = statement(x)) : x
}
set statement(x) { this.#statement = x }
static get [Symbol.species]() {
return Array
}
}
export function statement(item) {
const x = item.statement
return {
name: x ? x.name : '',
string: x ? x.text : item.text || '',
types: item.params || (item.entry && item.entry.oids) || (x && x.oids) || [],
columns: item.columns || []
}
}
+149
View File
@@ -0,0 +1,149 @@
import net from 'net'
import tls from 'tls'
const noop = () => {}
, agent = globalThis.navigator && globalThis.navigator.userAgent || ''
, workers = agent.startsWith('Cloudflare-Workers')
, onreadSafe = !agent || agent.startsWith('Node.js') || agent.startsWith('Bun') || agent.startsWith('Deno')
export async function Socket(options, host, port, track = noop) {
const raw = options.socket
? await options.socket(options, host, port)
: await connect(options, host, port, track)
return wrap(raw, host)
}
export function secureOptions(ssl) {
return ssl === 'require' || ssl === 'allow' || ssl === 'prefer'
? workers ? {} : { rejectUnauthorized: false }
: typeof ssl === 'object' ? ssl : {}
}
export const secured = options => !!options.ssl && options.ssl !== 'disable'
export const direct = options => secured(options) && options.sslnegotiation === 'direct'
function connect(options, host, port, track) {
return new Promise((resolve, reject) => {
const tlsNow = direct(options)
, plain = !secured(options)
, reads = (plain || tlsNow) && onreadSafe && options.onread !== false && {
buffer: Buffer.allocUnsafe(65536),
callback: (n, b) => socket.ondata(b.subarray(0, n))
}
, target = options.path ? { path: options.path } : { port, host }
, socket = tlsNow
? tls.connect({
...target,
servername: net.isIP(host || '') || options.path ? undefined : host,
...secureOptions(options.ssl),
ALPNProtocols: ['postgresql'],
...(reads ? { onread: reads } : {})
})
: net.connect({ ...target, ...(reads ? { onread: reads } : {}) })
track(socket)
socket.shared = !!reads
socket.secure = tlsNow
socket.ondata = noop
socket.once('error', reject)
socket.once(tlsNow ? 'secureConnect' : 'connect', () => {
socket.off('error', reject)
socket.setNoDelay(true)
resolve(socket)
})
})
}
function wrap(socket, host) {
let closed = false
, failure = null
socket.shared && (socket.ondata = data)
const x = {
ondata: noop,
ondrain: noop,
onclose: noop,
get raw() { return socket },
get closed() { return closed },
get shared() { return !!socket.shared },
get negotiated() { return !!socket.secure || !!socket.negotiated },
certificate() {
const x = typeof socket.getPeerCertificate === 'function' && socket.getPeerCertificate()
return x && x.raw || null
},
write: data => !closed && socket.write(data),
upgrade,
pause: () => socket.pause(),
resume: () => socket.resume(),
ref: () => socket.ref && socket.ref(),
unref: () => socket.unref && socket.unref(),
end(data) {
closed || (data ? socket.end(data) : socket.end())
},
destroy(error) {
error && !failure && (failure = error)
socket.destroy()
},
keepAlive(seconds) {
seconds && socket.setKeepAlive && socket.setKeepAlive(true, 1000 * seconds)
}
}
listen()
return x
function listen() {
socket.on('data', data)
socket.on('drain', drain)
socket.on('error', error)
socket.on('close', close)
}
function unlisten() {
socket.off('data', data)
socket.off('drain', drain)
socket.off('error', error)
socket.off('close', close)
}
function data(buffer) { x.ondata(buffer) }
function drain() { x.ondrain() }
function error(err) {
failure || (failure = err)
socket.destroy()
close()
}
function close() {
if (closed)
return
closed = true
x.onclose(failure)
}
function upgrade(options = {}) {
return new Promise((resolve, reject) => {
unlisten()
const secure = tls.connect({
socket,
servername: net.isIP(host || '') ? undefined : host,
...options
})
const fail = err => {
secure.off('secureConnect', ready)
reject(err)
}
function ready() {
secure.off('error', fail)
socket = secure
listen()
resolve(x)
}
secure.once('secureConnect', ready)
secure.once('error', fail)
})
}
}
+469
View File
@@ -0,0 +1,469 @@
import crypto from 'crypto'
import fs from 'fs'
import * as p from './protocol.js'
import { Socket, secured, secureOptions } from './socket.js'
import { Errors } from './errors.js'
import { channels, active, start, finish, userName } from './diagnostics.js'
const noop = () => {}
const R = 82
, S = 83
, K = 75
, E = 69
, N = 78
, D = 68
, Z = 90
export async function open(options, signal = { aborted: false, sockets: new Set() }, first = 0) {
const hosts = options.path ? [null] : options.host
, wanted = options.target_session_attrs
, passes = wanted === 'prefer-standby' ? ['standby', 'any'] : [wanted || 'any']
let last
for (const want of passes) {
for (let n = 0; n < hosts.length; n++) {
const i = (first + n) % hosts.length
, host = hosts[i]
, port = options.port[i] || options.port[0]
, [failed, error, ready] = await traced(options, host, port, signal).then(x => [false, null, x], x => [true, x])
if (!failed && fits(want, ready.attributes))
return (ready.index = i, ready)
failed || ready.socket.end(p.Terminate())
last = failed ? error : Errors.connection('CONNECTION_DESTROYED', options, host, port)
if (signal.aborted)
throw last
}
}
throw last
}
function traced(options, host, port, signal) {
if (!active(channels.connect))
return attempt(options, host, port, signal)
const context = {
host: options.path ? null : host,
port: options.path ? null : port,
path: options.path || null,
database: options.database,
user: userName(options),
ssl: secured(options),
sslnegotiation: options.sslnegotiation || 'postgres'
}
let promise
start(channels.connect, context, () => promise = attempt(options, host, port, signal))
return promise.then(ready => {
finish(channels.connect, context, null, {
pid: ready.backend.pid,
status: ready.status,
readOnly: ready.attributes.readOnly,
standby: ready.attributes.standby
})
return ready
}, error => {
finish(channels.connect, context, error)
throw error
})
}
function fits(want, { readOnly, standby }) {
return want === 'any'
|| (want === 'read-write' && !readOnly)
|| (want === 'read-only' && readOnly)
|| (want === 'primary' && !standby)
|| (want === 'standby' && standby)
}
function attempt(options, host, port, signal) {
const { setTimeout, clearTimeout } = options.timers
, fail = code => Errors.connection(code, options, host, port)
return new Promise((resolve, reject) => {
const timer = options.connect_timeout && setTimeout(() => done(true, fail('CONNECT_TIMEOUT')), options.connect_timeout * 1000)
let socket = null
, raw = null
, settled = false
session().then(x => done(false, null, x), e => done(true, e))
function done(failed, error, ready) {
if (settled)
return
settled = true
timer && clearTimeout(timer)
socket && signal.sockets.delete(socket)
signal.aborted && !failed && (failed = true, error = fail('CONNECTION_DESTROYED'))
if (failed) {
socket ? socket.destroy() : raw && raw.destroy()
return reject(error)
}
ready.socket.onclose = () => {}
resolve(ready)
}
async function session() {
socket = await Socket(options, host, port, x => raw = x)
if (settled || signal.aborted)
return (socket.destroy(), done(true, fail('CONNECTION_DESTROYED')))
signal.sockets.add(socket)
socket.onclose = error => done(true, signal.aborted ? fail('CONNECTION_DESTROYED') : error && error.code ? error : fail('CONNECTION_CLOSED'))
socket.keepAlive(options.keep_alive)
await negotiate(socket, options)
const user = typeof options.user === 'function' ? await options.user() : options.user
return startup(user, options.database || user)
}
function startup(user, database) {
return new Promise((resolve, reject) => {
const stored = () => pgpass(options.passfile, options.path ? 'localhost' : host, port, database, user) || ''
, auth = Auth({ user, pass: options.pass || stored, binding: options.channel_binding, socket })
, parameters = {}
, backend = { pid: null, secret: null }
, frames = p.Frames(frame, false, error => (reject(error), socket.destroy()))
, rows = []
let probing = false
, status = null
, authing = Promise.resolve()
socket.ondata = x => frames.push(x)
socket.write(p.Startup({ user, database, client_encoding: 'UTF8', ...options.connection }))
function frame(type, x, start, end) {
try {
type === R ? authenticate(Buffer.from(x.subarray(start, end)))
: type === S ? (([k, v]) => parameters[k] = v)(p.parameterStatus(x, start))
: type === K ? Object.assign(backend, p.backendKeyData(x, start))
: type === E ? reject(Errors.postgres(p.error(x, start, end)))
: type === N ? notice(p.error(x, start, end))
: type === D ? rows.push(x.toString('utf8', start + 11, start + 11 + x.readInt32BE(start + 7)))
: type === Z && settled(p.readyForQuery(x, start), Buffer.from(x.subarray(end)))
} catch (error) {
reject(error)
}
}
function notice(x) {
try {
options.onnotice ? options.onnotice(x) : options.onnotice === false || console.log(x)
} catch (error) {
noop(error)
}
}
function authenticate(x) {
authing = authing.then(() => auth(x)).then(reply => reply && socket.write(reply))
authing.catch(error => (reject(error), socket.destroy()))
}
function settled(x, rest) {
authing.then(() => ready(x, rest), () => {})
}
function ready(x, rest) {
if (!auth.authenticated)
return reject(Errors.generic('AUTHENTICATION_INCOMPLETE', 'The server was ready before it accepted the authentication'))
status = x
const known = 'in_hot_standby' in parameters && 'default_transaction_read_only' in parameters
if (options.target_session_attrs && !known && !probing) {
probing = true
return socket.write(p.Query('show transaction_read_only; select pg_catalog.pg_is_in_recovery()'))
}
frames.pause()
socket.ondata = () => {}
socket.pause()
resolve({
socket,
rest,
backend,
parameters,
status,
attributes: {
readOnly: (known ? parameters.default_transaction_read_only : rows[0]) === 'on',
standby: known ? parameters.in_hot_standby === 'on' : rows[1] === 't'
},
host,
port,
cancel: () => cancel(options, host, port, backend)
})
}
})
}
})
}
async function negotiate(socket, options) {
if (!secured(options) || socket.negotiated)
return
const ssl = options.ssl
, tls = secureOptions(ssl)
if (options.sslnegotiation === 'direct')
return socket.upgrade({ ...tls, ALPNProtocols: ['postgresql'] })
const answer = new Promise(r => socket.ondata = x => r(x[0]))
socket.write(p.SSLRequest())
if (await answer === S)
return socket.upgrade(tls)
if (ssl !== 'prefer' && ssl !== 'allow')
throw Errors.generic('SSL_NOT_SUPPORTED', 'The server does not support SSL connections')
}
async function cancel(options, host, port, { pid, secret }) {
const socket = await Socket(options, host, port)
try {
await negotiate(socket, options)
} catch (error) {
socket.destroy()
throw error
}
return new Promise(resolve => {
socket.onclose = () => resolve()
socket.end(p.Cancel(pid, secret))
})
}
const salted = new Map()
function Auth({ user, pass, binding = 'prefer', socket }) {
let nonce
, signature
, verified = true
, bound = false
, header = 'n,,'
respond.authenticated = false
return respond
async function respond(x) {
const type = x.readInt32BE(5)
if ((type === 3 || type === 5) && binding === 'require')
throw unbound('the server asked for ' + (type === 3 ? 'a cleartext' : 'an MD5') + ' password')
return type === 0 ? ok()
: type === 3 ? p.Password(await password())
: type === 5 ? p.Password('md5' + md5(Buffer.concat([Buffer.from(md5(await password() + user)), x.subarray(9, 13)])))
: type === 10 ? start(x)
: type === 11 ? proceed(x, await password())
: type === 12 ? finish(x)
: unsupported(type)
}
function start(x) {
const offered = x.toString('utf8', 9).split('\0')
, certificate = binding !== 'disable' && socket && socket.certificate && socket.certificate()
, hash = certificate && endpoint(certificate)
, plus = !!hash && offered.includes('SCRAM-SHA-256-PLUS')
if (binding === 'require' && !plus)
throw unbound(offered.includes('SCRAM-SHA-256-PLUS') ? 'the server certificate could not be hashed for it' : 'the server did not offer it')
if (!plus && !offered.includes('SCRAM-SHA-256'))
throw Errors.generic('SASL_MECHANISM_NOT_SUPPORTED', 'The server offered no supported SASL mechanism')
header = plus ? 'p=tls-server-end-point,,' : hash ? 'y,,' : 'n,,'
bound = plus ? hash : false
nonce = crypto.randomBytes(18).toString('base64')
verified = false
return p.SASL(plus ? 'SCRAM-SHA-256-PLUS' : 'SCRAM-SHA-256', header + 'n=*,r=' + nonce)
}
function proceed(x, pass) {
const res = x.toString('utf8', 9).split(',').reduce((acc, x) => (acc[x[0]] = x.slice(2), acc), {})
, key = sha256(pass + '\0' + res.s + '\0' + res.i).toString('base64')
if (!res.r.startsWith(nonce))
throw Errors.generic('SASL_NONCE_MISMATCH', 'The server nonce does not start with the client nonce')
if (!salted.has(key)) {
salted.size >= 64 && salted.delete(salted.keys().next().value)
salted.set(key, crypto.pbkdf2Sync(pass, Buffer.from(res.s, 'base64'), +res.i, 32, 'sha256'))
}
const saltedPassword = salted.get(key)
, clientKey = hmac(saltedPassword, 'Client Key')
, channel = (bound ? Buffer.concat([Buffer.from(header), bound]) : Buffer.from(header)).toString('base64')
, auth = 'n=*,r=' + nonce + ',r=' + res.r + ',s=' + res.s + ',i=' + res.i + ',c=' + channel + ',r=' + res.r
signature = hmac(hmac(saltedPassword, 'Server Key'), auth).toString('base64')
return p.SASLResponse('c=' + channel + ',r=' + res.r + ',p=' + xor(clientKey, hmac(sha256(clientKey), auth)).toString('base64'))
}
function finish(x) {
if (!signature || x.toString('utf8', 9).split('\0', 1)[0].slice(2) !== signature)
throw Errors.generic('SASL_SIGNATURE_MISMATCH', 'The server did not return the correct signature')
verified = true
return null
}
function ok() {
if (!verified)
throw Errors.generic('SASL_SIGNATURE_MISMATCH', 'The server ended SCRAM authentication without proving it knows the password')
if (binding === 'require' && !bound)
throw unbound('the server authenticated without it')
respond.authenticated = true
return null
}
function unsupported(type) {
throw Errors.generic('AUTH_TYPE_NOT_IMPLEMENTED', 'Authentication type ' + type + ' is not supported')
}
function password() {
return Promise.resolve(typeof pass === 'function' ? pass() : pass)
}
}
function unbound(reason) {
return Errors.generic('CHANNEL_BINDING_REQUIRED', 'channel_binding is require, but ' + reason)
}
const signatures = {
'2a864886f70d010104': 'sha256',
'2a864886f70d010105': 'sha256',
'2a864886f70d01010e': 'sha224',
'2a864886f70d01010b': 'sha256',
'2a864886f70d01010c': 'sha384',
'2a864886f70d01010d': 'sha512',
'2a8648ce3d0401': 'sha256',
'2a8648ce3d040301': 'sha224',
'2a8648ce3d040302': 'sha256',
'2a8648ce3d040303': 'sha384',
'2a8648ce3d040304': 'sha512',
'2a8648ce380403': 'sha256',
'608648016503040302': 'sha256'
}
const digests = {
'2b0e03021a': 'sha256',
'608648016503040201': 'sha256',
'608648016503040202': 'sha384',
'608648016503040203': 'sha512',
'608648016503040204': 'sha224'
}
function endpoint(certificate) {
try {
const x = Buffer.from(certificate)
, algorithm = signature(x)
return algorithm ? crypto.createHash(algorithm).update(x).digest() : null
} catch (_) {
return null
}
}
function signature(x) {
const certificate = der(x, 0)
, tbs = der(x, certificate.body)
, algorithm = der(x, tbs.end)
, oid = der(x, algorithm.body)
, name = x[oid.start] === 0x06 && x.toString('hex', oid.body, oid.end)
if (name !== '2a864886f70d01010a')
return signatures[name]
if (oid.end >= algorithm.end)
return 'sha256'
const params = der(x, oid.end)
, first = params.body < params.end && der(x, params.body)
if (!first || x[first.start] !== 0xa0)
return 'sha256'
const hash = der(x, der(x, first.body).body)
return digests[x.toString('hex', hash.body, hash.end)]
}
function der(x, start) {
let length = x[start + 1]
, body = start + 2
if (length & 0x80) {
const n = length & 0x7f
length = 0
for (let i = 0; i < n; i++)
length = length * 256 + x[body + i]
body += n
}
if (body + length > x.length)
throw new Error('Truncated certificate')
return { start, body, end: body + length }
}
function md5(x) {
return crypto.createHash('md5').update(x).digest('hex')
}
function hmac(key, x) {
return crypto.createHmac('sha256', key).update(x).digest()
}
function sha256(x) {
return crypto.createHash('sha256').update(x).digest()
}
function xor(a, b) {
const x = Buffer.allocUnsafe(a.length)
for (let i = 0; i < a.length; i++)
x[i] = a[i] ^ b[i]
return x
}
function pgpass(file, host, port, database, user) {
if (!file)
return null
let text
try {
const stat = fs.statSync(file)
if (!stat.isFile() || (process.platform !== 'win32' && (stat.mode & 0o077)))
return null
text = fs.readFileSync(file, 'utf8')
} catch (_) {
return null
}
const wanted = [host, String(port), database, user]
for (const line of text.split(/\r?\n/)) {
const fields = line && line[0] !== '#' ? split(line) : []
if (fields.length === 5 && wanted.every((x, i) => fields[i] === '*' || fields[i] === x))
return fields[4]
}
return null
}
function split(line) {
const fields = []
let field = ''
for (let i = 0; i < line.length; i++) {
const c = line[i]
if (c === '\\' && i + 1 < line.length)
field += line[++i]
else if (c === ':' && fields.length < 4)
fields.push(field), field = ''
else
field += c
}
fields.push(field)
return fields
}
+403
View File
@@ -0,0 +1,403 @@
import { own } from './connection.js'
const noop = () => {}
, statusInterval = 10000
, epoch = BigInt(Date.UTC(2000, 0, 1)) * 1000n
const lsnToString = x => (x >> 32n).toString(16).toUpperCase() + '/' + (x & 0xFFFFFFFFn).toString(16).toUpperCase()
export default function Subscribe(postgres, options) {
const subscribers = new Map()
, persistent = !!options.slot
, slot = options.slot || 'postgresjs_' + Math.random().toString(36).slice(2)
, state = {}
, timers = options.timers
let connection
, current = null
, ended = false
, retries = 0
const sql = subscribe.sql = postgres({
...options,
transform: { column: {}, value: {}, row: {} },
max: 1,
idle_timeout: null,
max_lifetime: null,
connection: {
...options.connection,
replication: 'database'
},
onclose: () => {
current && current.stop()
current = null
state.pid = state.secret = undefined
ended || restart()
},
no_subscribe: true
})
const end = sql.end
, close = sql.close
sql.end = async() => {
ended = true
current && await current.finish()
return end()
}
sql.close = async() => {
current && await current.finish()
return close()
}
return subscribe
async function subscribe(event, fn, onsubscribe = noop, onerror) {
event = parseEvent(event)
connection || (connection = init())
const subscriber = { fn, onsubscribe, onerror }
const fns = subscribers.has(event)
? subscribers.get(event).add(subscriber)
: subscribers.set(event, new Set([subscriber])).get(event)
const unsubscribe = () => {
fns.delete(subscriber)
fns.size === 0 && subscribers.delete(event)
}
return connection.then(() => {
onsubscribe()
return { unsubscribe, state, sql }
}, error => {
unsubscribe()
connection = null
throw error
})
}
function restart() {
const delay = Math.min(100 * 2 ** retries++, 10000)
connection = new Promise(r => timers.setTimeout(r, retries === 1 ? 0 : delay)).then(() => ended || init()).then(
x => {
retries = 0
ended || subscribers.forEach(event => event.forEach(({ onsubscribe }) => onsubscribe()))
return x
},
error => {
report(error)
ended || restart()
}
)
connection.catch(noop)
}
async function init() {
if (!options.publications)
throw new Error('Missing publication names')
await sql.unsafe(`CREATE_REPLICATION_SLOT ${ slot }${ persistent ? '' : ' TEMPORARY' } LOGICAL pgoutput NOEXPORT_SNAPSHOT`).catch(error => {
if (!persistent || error.code !== '42710')
throw error
})
const query = sql.unsafe(`START_REPLICATION SLOT ${ slot } LOGICAL 0/0 (proto_version '1', publication_names '${ options.publications }')`)
query.catch(noop)
const stream = await query.writable()
current = Stream(stream)
state.pid = query.state && query.state.pid
state.secret = query.state && query.state.secret
return current
}
function Stream(stream) {
const relations = {}
let acked = 0n
, received = 0n
, transaction = null
, queue = null
, failed = false
, stopped = false
, timer = timers.setTimeout(tick, statusInterval)
, soon = null
stream.on('data', data)
stream.on('error', error => failed || report(error))
stream.on('close', () => {
const unexpected = !stopped
stop()
unexpected && close()
})
return { stop, finish }
function data(x) {
if (x[0] === 0x77) {
received = max(received, x.readBigUInt64BE(9))
parse(x.subarray(25), relations, sql.options.parsers, message, options.transform)
} else if (x[0] === 0x6b) {
received = max(received, x.readBigUInt64BE(1))
idle() && (acked = max(acked, received))
x[17] && status()
}
}
function message(kind, x, info) {
if (kind === 'begin')
return transaction = { lsn: x.lsn, end: null, time: x.time, xid: x.xid }
if (kind === 'commit') {
const done = transaction
done.end = x.end
transaction = null
return run(() => commit(done))
}
const at = transaction || {}
info.lsn = at.lsn && lsnToString(at.lsn)
info.time = at.time
info.xid = at.xid
run(() => handle(x, info))
}
function run(fn) {
if (failed || stopped)
return
if (queue) {
queue = queue.then(() => failed || stopped ? null : fn())
return
}
const result = fn()
if (result && typeof result.then === 'function') {
queue = result
stream.pause()
drain()
}
}
function drain() {
const at = queue
at.then(() => {
if (queue !== at)
return queue && drain()
queue = null
stopped || failed || stream.resume()
}, noop)
}
function commit(done) {
acked = max(acked, done.end)
persistent && !soon && (soon = timers.setImmediate(() => (soon = null, status())))
}
function handle(row, info) {
const path = info.relation.schema + '.' + info.relation.table
, key = row && info.relation.keys.length ? '=' + info.relation.keys.map(x => row[x.name]) : ''
, pending = []
call(pending, '*', row, info)
call(pending, '*:' + path, row, info)
key && call(pending, '*:' + path + key, row, info)
call(pending, info.command, row, info)
call(pending, info.command + ':' + path, row, info)
key && call(pending, info.command + ':' + path + key, row, info)
return pending.length ? Promise.all(pending) : null
}
function call(pending, event, row, info) {
subscribers.has(event) && subscribers.get(event).forEach(subscriber => {
let result
try {
result = subscriber.fn(row, info, event)
} catch (error) {
return fail(error, subscriber)
}
result && typeof result.then === 'function' && pending.push(result.catch(error => fail(error, subscriber)))
})
}
function fail(error, subscriber) {
report(error, subscriber)
if (!persistent || failed)
return
failed = true
queue = null
stop()
stream.destroy(new Error('A subscriber failed, so the transaction is delivered again'))
close()
}
function idle() {
return !transaction && !queue
}
function tick() {
timer = null
if (stopped)
return
status()
timer = timers.setTimeout(tick, statusInterval)
}
function status() {
if (stopped || stream.destroyed || !stream.writable)
return
const x = Buffer.alloc(34)
x[0] = 0x72
x.writeBigUInt64BE(acked, 1)
x.writeBigUInt64BE(acked, 9)
x.writeBigUInt64BE(acked, 17)
x.writeBigInt64BE(BigInt(Date.now()) * 1000n - epoch, 25)
stream.write(x)
}
async function finish() {
queue && await queue.catch(noop)
if (!failed) {
idle() && (acked = max(acked, received))
status()
}
stop()
stream.destroyed || await new Promise(r => (stream.once('close', r), stream.end()))
}
function stop() {
if (stopped)
return
stopped = true
timer && timers.clearTimeout(timer)
soon && timers.clearImmediate(soon)
timer = soon = null
}
}
function report(error, subscriber) {
const handlers = subscriber
? [subscriber.onerror]
: [...subscribers.values()].flatMap(x => [...x].map(x => x.onerror))
const listeners = handlers.filter(Boolean)
listeners.length
? listeners.forEach(fn => fn(error))
: console.error('postgres.js: logical replication error', error)
}
}
function max(a, b) {
return a > b ? a : b
}
function Time(x) {
return new Date(Date.UTC(2000, 0, 1) + Number(x / 1000n))
}
function parse(x, relations, parsers, message, transform) {
const type = x[0]
if (type === 82) {
let i = 1
const r = relations[x.readUInt32BE(i)] = {
schema: x.toString('utf8', i += 4, i = x.indexOf(0, i)) || 'pg_catalog',
table: x.toString('utf8', i + 1, i = x.indexOf(0, i + 1)),
columns: Array(x.readUInt16BE(i += 2)),
keys: []
}
i += 2
let columnIndex = 0
, column
while (i < x.length) {
column = r.columns[columnIndex++] = {
key: x[i++],
name: transform.column.from
? transform.column.from(x.toString('utf8', i, i = x.indexOf(0, i)))
: x.toString('utf8', i, i = x.indexOf(0, i)),
type: x.readUInt32BE(i += 1),
parser: parsers[x.readUInt32BE(i)],
atttypmod: x.readUInt32BE(i += 4)
}
column.key && r.keys.push(column)
i += 4
}
} else if (type === 66) {
message('begin', { lsn: x.readBigUInt64BE(1), time: Time(x.readBigInt64BE(9)), xid: x.readUInt32BE(17) })
} else if (type === 67) {
message('commit', { lsn: x.readBigUInt64BE(2), end: x.readBigUInt64BE(10) })
} else if (type === 73) {
const relation = relations[x.readUInt32BE(1)]
message('change', tuples(x, relation.columns, 8, transform).row, { command: 'insert', relation })
} else if (type === 68) {
let i = 1
const relation = relations[x.readUInt32BE(i)]
i += 4
const key = x[i] === 75
message('change', key || x[i] === 79
? tuples(x, relation.columns, i += 3, transform).row
: null
, { command: 'delete', relation, key })
} else if (type === 85) {
let i = 1
const relation = relations[x.readUInt32BE(i)]
i += 4
const key = x[i] === 75
const xs = key || x[i] === 79
? tuples(x, relation.columns, i += 3, transform)
: null
xs && (i = xs.i)
const { row } = tuples(x, relation.columns, i + 3, transform)
message('change', row, { command: 'update', relation, key, old: xs && xs.row })
} else if (type === 84) {
const count = x.readUInt32BE(1)
, flags = x[5]
for (let i = 0; i < count; i++) {
message('change', null, {
command: 'truncate',
relation: relations[x.readUInt32BE(6 + i * 4)],
cascade: (flags & 1) === 1,
restartIdentity: (flags & 2) === 2
})
}
}
}
function tuples(x, columns, xi, transform) {
let type
, column
, value
const row = {}
for (let i = 0; i < columns.length; i++) {
type = x[xi++]
column = columns[i]
value = type === 110
? null
: type === 117
? undefined
: column.parser(x.toString('utf8', xi + 4, xi += 4 + x.readUInt32BE(xi)))
value = transform.value.from ? transform.value.from(value, column) : value
column.name === '__proto__' ? own(row, column.name, value) : row[column.name] = value
}
return { i: xi, row: transform.row.from ? transform.row.from(row) : row }
}
function parseEvent(x) {
const [, command, path, key] = x.match(/^(\*|insert|update|delete|truncate)?:?([^.]+?\.?[^=]+)?=?(.+)?/i)
return (command || '*')
+ (path ? ':' + (path.indexOf('.') === -1 ? 'public.' + path : path) : '')
+ (key ? '=' + key : '')
}
+349
View File
@@ -0,0 +1,349 @@
import { Errors } from './errors.js'
const number = x => +x
, bool = x => x === 't'
, json = x => JSON.parse(x)
, string = x => x instanceof Date ? serializeDate(x) : Array.isArray(x) ? serializeArray(x, string) : literal(x) ? JSON.stringify(x) : '' + x
, literal = x => x !== null && typeof x === 'object' && (Object.getPrototypeOf(x) === Object.prototype || Object.getPrototypeOf(x) === null)
const arrays = {
16: 1000, 17: 1001, 18: 1002, 19: 1003, 20: 1016, 21: 1005, 22: 1006, 23: 1007, 24: 1008,
25: 1009, 26: 1028, 27: 1010, 28: 1011, 29: 1012, 30: 1013, 114: 199, 142: 143, 600: 1017,
601: 1018, 602: 1019, 603: 1020, 604: 1027, 628: 629, 650: 651, 700: 1021, 701: 1022, 718: 719,
774: 775, 790: 791, 829: 1040, 869: 1041, 1033: 1034, 1042: 1014, 1043: 1015, 1082: 1182,
1083: 1183, 1114: 1115, 1184: 1185, 1186: 1187, 1266: 1270, 1560: 1561, 1562: 1563, 1700: 1231,
1790: 2201, 2202: 2207, 2203: 2208, 2204: 2209, 2205: 2210, 2206: 2211, 2249: 2287, 2275: 1263,
2950: 2951, 2970: 2949, 3220: 3221, 3614: 3643, 3615: 3645, 3734: 3735, 3769: 3770, 3802: 3807,
3904: 3905, 3906: 3907, 3908: 3909, 3910: 3911, 3912: 3913, 3926: 3927, 4072: 4073, 4089: 4090,
4096: 4097, 5038: 5039, 5069: 271
}
export const types = {
string: { to: 25, from: null, serialize: string },
number: { to: 0, from: [21, 23, 26, 700, 701], serialize: string, parse: number },
json: { to: 114, from: [114, 3802], serialize: x => JSON.stringify(x), parse: json },
boolean: { to: 16, from: [16], serialize: x => x === true ? 't' : 'f', parse: bool },
date: { to: 1184, from: [1082, 1114, 1184], serialize: serializeDate, parse: null },
bytea: { to: 17, from: [17], serialize: x => '\\x' + Buffer.from(x).toString('hex'), parse: x => Buffer.from(x.slice(2), 'hex') }
}
const dates = { 1082: parseDate, 1114: parseTimestamp, 1184: parseTimestamptz }
function integer(x, i, end) {
const negative = x[i] === 45
let n = 0
for (negative && i++; i < end; i++)
n = n * 10 + x[i] - 48
return negative ? -n : n
}
function boolean(x, i) {
return x[i] === 116
}
const shaped = (is, serialize) => x => is(x) ? serialize(x) : string(x)
const shapedTypes = {
...types,
boolean: { ...types.boolean, serialize: shaped(x => typeof x === 'boolean', types.boolean.serialize) },
date: { ...types.date, serialize: shaped(x => x instanceof Date, types.date.serialize) },
bytea: { ...types.bytea, serialize: shaped(x => x instanceof Uint8Array, types.bytea.serialize) }
}
export function Types(options = {}) {
const parsers = {}
, serializers = {}
, elements = {}
, undefinedValue = options.transform && options.transform.undefined
Object.entries(arrays).forEach(([oid, array]) => elements[array] = [+oid, +oid === 603 ? ';' : ','])
register(options.infer === false ? shapedTypes : types)
Object.assign(parsers, dates)
options.infer === false && (parsers[1114] = parseDate)
const custom = Object.values(options.types || {})
, named = custom.filter(x => [x.to].concat(x.from || []).some(y => typeof y === 'string'))
, oids = {}
register(custom.filter(x => !named.includes(x)))
const bases = {}
return { parser, decoder, serializer, load, infer, resolve, named: named.length > 0, parsers, serializers }
function resolve(type) {
return typeof type === 'string' ? oids[type] || 0 : type
}
function decoder(oid, parse) {
return parse === number && (oid === 21 || oid === 23 || oid === 26) ? integer
: parse === bool && oid === 16 ? boolean
: null
}
function register(xs) {
Object.values(xs).forEach(x => {
const from = [].concat(x.from || [])
x.parse && from.forEach(oid => parsers[oid] = x.parse)
if (x.serialize) {
serializers[x.to] = x.serialize
from.forEach(oid => serializers[oid] = x.serialize)
}
})
}
function parser(oid) {
if (oid in parsers)
return parsers[oid]
if (oid in bases)
return parsers[oid] = parser(bases[oid])
if (oid in elements) {
const [element, delimiter] = elements[oid]
, parse = parser(element)
return parsers[oid] = x => parseArray(x, parse === identity ? null : parse, delimiter)
}
return identity
}
function serializer(oid) {
if (oid in serializers)
return serializers[oid]
if (oid in bases)
return serializers[oid] = serializer(bases[oid])
if (oid in elements) {
const [element, delimiter] = elements[oid]
, serialize = serializer(element)
return serializers[oid] = xs => Array.isArray(xs) ? serializeArray(xs, serialize, delimiter, undefinedValue) : serialize(xs)
}
return string
}
function load(run) {
return Promise.all([fetched(run), named.length && names(run)])
}
async function names(run) {
const wanted = [...new Set(named.flatMap(x => [x.to].concat(x.from || [])).filter(x => typeof x === 'string'))]
, list = wanted.map(x => '\'' + x.split('.').pop().replace(/'/g, '\'\'') + '\'').join(',')
, rows = await run(
'select t.oid::int, t.typarray::int as array, t.typdelim as delimiter, t.typname as name, n.nspname as schema, '
+ 'pg_catalog.pg_type_is_visible(t.oid) as visible from pg_catalog.pg_type t '
+ 'join pg_catalog.pg_namespace n on n.oid = t.typnamespace where t.typname in (' + list + ')'
)
, missing = []
wanted.forEach(x => {
const [schema, name] = x.includes('.') ? x.split('.') : [null, x]
, row = rows.find(r => r.name === name && (schema ? r.schema === schema : r.visible))
row ? (oids[x] = row.oid, row.array && !(row.array in elements) && (elements[row.array] = [row.oid, row.delimiter])) : missing.push(x)
})
if (missing.length) {
throw Errors.generic('TYPE_NOT_FOUND', missing.length > 1
? 'The types ' + missing.join(', ') + ' do not exist'
: 'The type ' + missing[0] + ' does not exist')
}
register(named.map(x => ({ ...x, to: resolve(x.to), from: [].concat(x.from || []).map(resolve) })))
}
function fetched(run) {
return run(
'select oid::int, typelem::int as element, typdelim as delimiter, typbasetype::int as base, typtype as kind '
+ 'from pg_catalog.pg_type where typtype = \'d\' or (typcategory = \'A\' and oid >= 16384)'
).then(rows => rows.forEach(({ oid, element, delimiter, base, kind }) => {
if (oid in parsers || oid in serializers)
return
kind === 'd'
? bases[oid] = base
: element && !(oid in elements) && (elements[oid] = [element, delimiter])
}))
}
}
const identity = x => x
identity.identity = true
export class Parameter {
constructor(value, type) {
this.value = value
this.type = type
}
}
export function infer(x) {
return (
x instanceof Parameter ? x.type :
x instanceof Date ? 1184 :
x instanceof Uint8Array ? 17 :
(x === true || x === false) ? 16 :
typeof x === 'bigint' ? 20 :
Array.isArray(x) ? arrays[infer(x[0])] || 0 :
0
)
}
export function parseArray(x, parse, delimiter = ',') {
let i = x[0] === '[' ? x.indexOf('=') + 1 : 0
return level()
function level() {
const xs = []
i++
while (i < x.length && x[i] !== '}') {
if (x[i] === '{') {
xs.push(level())
} else if (x[i] === '"') {
let s = ''
for (i++; x[i] !== '"'; i++)
s += x[i] === '\\' ? x[++i] : x[i]
i++
xs.push(parse ? parse(s) : s)
} else {
const start = i
while (x[i] !== delimiter && x[i] !== '}')
i++
const s = x.slice(start, i)
xs.push(s === 'NULL' ? null : parse ? parse(s) : s)
}
x[i] === delimiter && i++
}
i++
return xs
}
}
export function serializeArray(xs, serialize = string, delimiter = ',', undefinedValue) {
return '{' + xs.map(x => {
const typed = x instanceof Parameter
typed && (x = x.value)
x === undefined && (x = undefinedValue)
if (x === undefined)
throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')
return Array.isArray(x) && !typed
? serializeArray(x, serialize, delimiter, undefinedValue)
: x === null
? 'NULL'
: '"' + ('' + serialize(x)).replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"'
}).join(delimiter) + '}'
}
function serializeDate(x) {
return x === Infinity ? 'infinity'
: x === -Infinity ? '-infinity'
: (x instanceof Date ? x : new Date(x)).toISOString()
}
const timestamp = /^(\d{4,})-(\d\d)-(\d\d)(?: (\d\d):(\d\d):(\d\d)(?:\.(\d+))?)?(?:([+-])(\d\d)(?::(\d\d))?(?::(\d\d))?)?( BC)?$/
function parts(x) {
const m = x.match(timestamp)
if (!m)
return null
const year = m[12] ? 1 - m[1] : +m[1]
, ms = m[7] ? +(m[7] + '00').slice(0, 3) : 0
, offset = m[8] ? (m[8] === '-' ? -1 : 1) * ((m[9] * 60 + +(m[10] || 0)) * 60 + +(m[11] || 0)) * 1000 : 0
return { year, month: m[2] - 1, day: +m[3], hour: +(m[4] || 0), minute: +(m[5] || 0), second: +(m[6] || 0), ms, offset }
}
function infinite(x) {
return x === 'infinity' ? Infinity : x === '-infinity' ? -Infinity : null
}
function utc(x) {
const date = new Date(Date.UTC(2000, x.month, x.day, x.hour, x.minute, x.second, x.ms))
date.setUTCFullYear(x.year)
return date
}
function plain(x) {
const n = x.length
return x.charCodeAt(0) !== 48 && x.charCodeAt(n - 1) !== 67 && !(x.charCodeAt(n - 3) === 58 && x.charCodeAt(n - 6) === 58)
&& x.charCodeAt(0) !== 45 && x.charCodeAt(0) !== 105
}
function parseTimestamptz(x) {
if (plain(x))
return new Date(x)
const p = infinite(x) || parts(x)
if (!p)
return new Date(x)
if (typeof p === 'number')
return p
const date = utc(p)
date.setTime(date.getTime() - p.offset)
return date
}
function parseDate(x) {
const p = infinite(x) || parts(x)
return !p ? new Date(x) : typeof p === 'number' ? p : utc(p)
}
function parseTimestamp(x) {
const p = infinite(x) || parts(x)
if (!p || typeof p === 'number')
return p || new Date(x)
const date = new Date(2000, p.month, p.day, p.hour, p.minute, p.second, p.ms)
date.setFullYear(p.year)
return date
}
export const toCamel = x => {
let str = x[0]
for (let i = 1; i < x.length; i++)
str += x[i] === '_' ? x[++i].toUpperCase() : x[i]
return str
}
export const toPascal = x => {
let str = x[0].toUpperCase()
for (let i = 1; i < x.length; i++)
str += x[i] === '_' ? x[++i].toUpperCase() : x[i]
return str
}
export const toKebab = x => x.replace(/_/g, '-')
export const fromCamel = x => x.replace(/([A-Z])/g, '_$1').toLowerCase()
export const fromPascal = x => (x.slice(0, 1) + x.slice(1).replace(/([A-Z])/g, '_$1')).toLowerCase()
export const fromKebab = x => x.replace(/-/g, '_')
function createJsonTransform(fn) {
return function jsonTransform(x, column) {
return typeof x === 'object' && x !== null && (column.type === 114 || column.type === 3802)
? Array.isArray(x)
? x.map(x => jsonTransform(x, column))
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: jsonTransform(v, column) }), {})
: x
}
}
toCamel.column = { from: toCamel }
toCamel.value = { from: createJsonTransform(toCamel) }
fromCamel.column = { to: fromCamel }
export const camel = { ...toCamel }
camel.column.to = fromCamel
toPascal.column = { from: toPascal }
toPascal.value = { from: createJsonTransform(toPascal) }
fromPascal.column = { to: fromPascal }
export const pascal = { ...toPascal }
pascal.column.to = fromPascal
toKebab.column = { from: toKebab }
toKebab.value = { from: createJsonTransform(toKebab) }
fromKebab.column = { to: fromKebab }
export const kebab = { ...toKebab }
kebab.column.to = fromKebab
+908
View File
@@ -0,0 +1,908 @@
import type { Readable, Writable } from 'node:stream'
import type { ConnectionOptions as TlsOptions } from 'node:tls'
type ParsedTypes<T extends Record<string, postgres.PostgresType>> =
Record<string, postgres.PostgresType> extends T
? {}
: { [K in keyof T]: T[K] extends postgres.PostgresType<infer R> ? R : never }
declare function postgres<T extends Record<string, postgres.PostgresType> = {}>(
options?: postgres.Options<T>
): postgres.Sql<ParsedTypes<T>>
declare function postgres<T extends Record<string, postgres.PostgresType> = {}>(
url: string,
options?: postgres.Options<T>
): postgres.Sql<ParsedTypes<T>>
declare const PRIVATE: unique symbol
declare class NotAPromise {
private [PRIVATE]: never
/** @deprecated Not a query, and so not a promise: use the tagged template, ```await sql`...` ``` */
private then(): never
/** @deprecated Not a query, and so not a promise: use the tagged template, ```await sql`...` ``` */
private catch(): never
/** @deprecated Not a query, and so not a promise: use the tagged template, ```await sql`...` ``` */
private finally(): never
}
type UnwrapPromiseArray<T> = T extends readonly any[]
? { -readonly [K in keyof T]: Awaited<T[K]> }
: T
type Keys = string
type SerializableObject<T, K extends readonly any[], TT> =
number extends K['length']
? {}
: Partial<
Record<Keys & keyof T & (K['length'] extends 0 ? string : K[number]), postgres.ParameterOrJSON<TT> | undefined>
& Record<string, any>
>
type First<T, K extends readonly any[], TT> =
T extends TemplateStringsArray ? TemplateStringsArray :
T extends string ? string :
T extends readonly any[][] ? readonly postgres.EscapableArray[] :
T extends readonly (object & infer R)[]
? R extends postgres.SerializableParameter<TT>
? readonly postgres.SerializableParameter<TT>[]
: readonly SerializableObject<R, K, TT>[]
: T extends readonly any[] ? readonly postgres.SerializableParameter<TT>[] :
T extends object ? SerializableObject<T, K, TT> :
never
type Rest<T> =
T extends TemplateStringsArray ? never :
T extends string ? readonly string[] :
T extends readonly any[][] ? readonly [] :
T extends readonly (object & infer R)[]
? readonly (Keys & keyof R)[] | [readonly (Keys & keyof R)[]] | [readonly string[]]
: T extends readonly any[] ? readonly [] :
T extends object ? readonly (Keys & keyof T)[] | [readonly (Keys & keyof T)[]] | [readonly string[]] :
any
type Return<T, K extends readonly any[]> =
[T] extends [TemplateStringsArray]
? [unknown] extends [T]
? postgres.Helper<T, K>
: [TemplateStringsArray] extends [T] ? postgres.PendingQuery<postgres.Row[]> : postgres.Helper<T, K>
: postgres.Helper<T, K>
declare namespace postgres {
/** An error the server sent */
class PostgresError extends globalThis.Error {
constructor(message?: string | Partial<PostgresError>)
name: 'PostgresError'
severity_local: string
severity: string
code: string
position: string
file: string
line: string
routine: string
detail?: string | undefined
hint?: string | undefined
internal_position?: string | undefined
internal_query?: string | undefined
where?: string | undefined
schema_name?: string | undefined
table_name?: string | undefined
column_name?: string | undefined
data?: string | undefined
data_type_name?: string | undefined
constraint_name?: string | undefined
/** The text of the query, when the query is known */
query: string
/** The parameters of the query; only with `debug` */
parameters: any[]
/** The types of the parameters; only with `debug` */
types?: number[] | undefined
}
/** snake_case to PascalCase */
function toPascal(str: string): string
namespace toPascal {
namespace column { function from(str: string): string }
namespace value { function from(str: unknown, column: Column<string>): string }
}
/** PascalCase to snake_case */
function fromPascal(str: string): string
namespace fromPascal {
namespace column { function to(str: string): string }
}
/** snake_case to and from PascalCase, as a transform */
namespace pascal {
namespace column {
function from(str: string): string
function to(str: string): string
}
namespace value { function from(str: unknown, column: Column<string>): string }
}
/** snake_case to camelCase */
function toCamel(str: string): string
namespace toCamel {
namespace column { function from(str: string): string }
namespace value { function from(str: unknown, column: Column<string>): string }
}
/** camelCase to snake_case */
function fromCamel(str: string): string
namespace fromCamel {
namespace column { function to(str: string): string }
}
/** snake_case to and from camelCase, as a transform */
namespace camel {
namespace column {
function from(str: string): string
function to(str: string): string
}
namespace value { function from(str: unknown, column: Column<string>): string }
}
/** snake_case to kebab-case */
function toKebab(str: string): string
namespace toKebab {
namespace column { function from(str: string): string }
namespace value { function from(str: unknown, column: Column<string>): string }
}
/** kebab-case to snake_case */
function fromKebab(str: string): string
namespace fromKebab {
namespace column { function to(str: string): string }
}
/** snake_case to and from kebab-case, as a transform */
namespace kebab {
namespace column {
function from(str: string): string
function to(str: string): string
}
namespace value { function from(str: unknown, column: Column<string>): string }
}
/** int8 as bigint: `types: { bigint: postgres.BigInt }` */
const BigInt: PostgresType<bigint>
interface PostgresType<T = any> {
/** The oid values of this type are sent as, or the type's name, looked up when the pool starts */
to: number | string
/** The oids or type names parsed with `parse` */
from: (number | string)[]
serialize: (value: T) => unknown
parse: (raw: any) => T
}
interface ConnectionParameters {
/** @default 'postgres.js' */
application_name: string
default_transaction_isolation: 'read uncommitted' | 'read committed' | 'repeatable read' | 'serializable'
default_transaction_read_only: boolean
default_transaction_deferrable: boolean
statement_timeout: number
lock_timeout: number
idle_in_transaction_session_timeout: number
idle_session_timeout: number
DateStyle: string
IntervalStyle: string
TimeZone: string
[name: string]: string | number | boolean
}
type SSL = boolean | 'require' | 'allow' | 'prefer' | 'verify-full' | 'disable' | TlsOptions
/** A socket of your own: anything with the events and methods of a net.Socket that are used */
interface SocketLike {
write(data: Uint8Array): boolean
end(data?: Uint8Array): unknown
destroy(error?: Error): unknown
pause(): unknown
resume(): unknown
on(event: 'data', fn: (data: Uint8Array) => void): unknown
on(event: 'drain' | 'close', fn: () => void): unknown
on(event: 'error', fn: (error: Error) => void): unknown
off(event: string, fn: (...args: any[]) => void): unknown
setKeepAlive?(enable: boolean, delay: number): unknown
/** Set when the socket did the TLS negotiation itself, so it is not done again */
negotiated?: boolean
secure?: boolean
}
interface Options<T extends Record<string, PostgresType> = {}> {
/** Host or hosts, with `host:port` allowed; tried in order @default process.env.PGHOST || 'localhost' */
host?: string | string[] | undefined
/** An alias for `host` */
hostname?: string | string[] | undefined
/** @default process.env.PGPORT || 5432 */
port?: number | number[] | undefined
/** A unix socket directory, like '/tmp' */
path?: string | undefined
/** @default process.env.PGDATABASE || user */
database?: string | undefined
/** An alias for `database` */
db?: string | undefined
/** A string, or a function giving one each time a connection opens @default process.env.PGUSERNAME || process.env.PGUSER || os.userInfo().username */
user?: string | (() => string | Promise<string>) | undefined
/** An alias for `user` */
username?: string | (() => string | Promise<string>) | undefined
/** A string, or a function giving one each time a connection opens @default process.env.PGPASSWORD, then PGPASSFILE or ~/.pgpass */
password?: string | (() => string | Promise<string>) | undefined
/** An alias for `password` */
pass?: string | (() => string | Promise<string>) | undefined
/**
* TLS: false, a mode as in libpq's sslmode, or options for tls.connect.
* 'require', 'allow' and 'prefer' do not verify the certificate; Cloudflare Workers always do.
* @default false
*/
ssl?: SSL | undefined
/**
* 'direct' starts TLS on the first byte instead of asking first (PostgreSQL 17 and later).
* On Node it is also the fastest TLS, as it reads into the shared buffer.
* @default 'postgres'
*/
sslnegotiation?: 'postgres' | 'direct' | null | undefined
/**
* SCRAM-SHA-256-PLUS over TLS, binding the login to the server certificate, as libpq's channel_binding.
* 'require' rejects a connection that can not be bound with CHANNEL_BINDING_REQUIRED.
* @default 'prefer'
*/
channel_binding?: 'disable' | 'prefer' | 'require' | undefined
/** Most connections at a time @default 10 */
max?: number | undefined
/** Connections kept open once the pool is used, even when idle @default 0 */
min?: number | undefined
/** Most queries pipelined on one connection @default 100 */
max_pipeline?: number | undefined
/**
* Pipeline a query on the busy connection where it should be done soonest, keeping slow queries together
* so quick ones don't wait behind them; false pipelines on the connection with fewest queries in flight
* @default true
*/
scheduler?: boolean | undefined
/** Seconds before an idle connection closes; 0, null or false to keep it @default null */
idle_timeout?: number | null | false | undefined
/** Seconds a query, transaction or reserve may wait for a connection before QUEUE_TIMEOUT; 0 to wait as long as it takes @default 0 */
queue_timeout?: number | undefined
/** Seconds to connect before CONNECT_TIMEOUT; 0, null or false to wait as long as it takes @default 30 */
connect_timeout?: number | null | false | undefined
/** Seconds a connection lives at most; a function gives a number per connection; 0, null or false for no limit @default 30 to 60 minutes */
max_lifetime?: number | null | false | (() => number) | undefined
/** Seconds before reconnecting after a failure; a function of the number of attempts @default exponential */
backoff?: boolean | number | ((attempts: number) => number) | undefined
/** Seconds of TCP keep-alive; 0 or null for none @default 60 */
keep_alive?: number | null | false | undefined
/** Named prepared statements @default true */
prepare?: boolean | undefined
/**
* false leaves every parameter to the server, as libpq does, and writes each value for the type it
* asks for. true types parameters from their values as v3 did: a Date as timestamptz, a Uint8Array as
* bytea, a boolean, a bigint as int8 and an array by its first element.
* @default false
*/
infer?: boolean | undefined
/** Fetches array types and the types of columns it does not know @default true */
fetch_types?: boolean | undefined
/** Custom types; see the README */
types?: T | undefined
/** Called with each notice; false to drop them @default console.log */
onnotice?: false | ((notice: Notice) => void) | undefined
/** Called with each notification, whether listened to or not */
onnotify?: ((channel: string, payload: string, pid: number) => void) | undefined
/** Called when a server parameter changes */
onparameter?: ((key: string, value: string) => void) | undefined
/** Called with the id of each connection that closes */
onclose?: ((connection: number) => void) | undefined
/** true, or a function called with every query */
debug?: boolean | ((connection: number, query: string, parameters: any[], types: number[]) => void) | undefined
/** Transforms of column names, values and rows */
transform?: TransformOptions | undefined
/** Run-time parameters sent at startup */
connection?: Partial<ConnectionParameters> | undefined
/** For several hosts: which kind of server to settle on @default process.env.PGTARGETSESSIONATTRS */
target_session_attrs?: 'read-write' | 'read-only' | 'primary' | 'standby' | 'prefer-standby' | null | undefined
/** Publications for `sql.subscribe` @default 'alltables' */
publications?: string | undefined
/**
* A permanent replication slot for `sql.subscribe`, created if it does not exist. Streaming starts
* where the slot was last confirmed, and a transaction is confirmed once its handlers finish.
* Without one, subscribe uses a temporary slot.
*/
slot?: string | null | undefined
/** A socket of your own for each connection */
socket?: ((options: ParsedOptions, host: string, port: number) => SocketLike | Promise<SocketLike>) | undefined
/** Reads plain and direct TLS connections into one shared buffer per connection (Node, Bun and Deno) @default true */
onread?: boolean | undefined
/** Idle connections do not keep the process alive, so it can exit without sql.end() @default false */
unref_sockets?: boolean | undefined
/** @deprecated use `prepare: false` */
no_prepare?: boolean | undefined
/** @deprecated use `idle_timeout` */
timeout?: number | undefined
}
interface TransformOptions {
/** What undefined is sent as, instead of failing with UNDEFINED_VALUE */
undefined?: any
column?: ((column: string) => string) | {
/** Column names in results */
from?: ((column: string) => string) | undefined
/** Column names given to helpers */
to?: ((column: string) => string) | undefined
} | undefined
value?: ((value: any) => any) | {
/** Values in results */
from?: ((value: unknown, column: Column<string>) => any) | undefined
} | undefined
row?: ((row: Row) => any) | {
/** Whole rows in results */
from?: ((row: Row) => any) | undefined
} | undefined
}
interface Transform {
undefined: any
column: {
from: ((column: string) => string) | undefined
to: ((column: string) => string) | undefined
}
value: {
from: ((value: any, column?: Column<string>) => any) | undefined
to: undefined
}
row: {
from: ((row: Row) => any) | undefined
to: undefined
}
}
interface ParsedOptions<T extends Record<string, unknown> = {}> {
host: string[]
port: number[]
path: string | false
database: string | null
user: string | (() => string | Promise<string>)
pass: string | (() => string | Promise<string>)
ssl: SSL
sslnegotiation: 'postgres' | 'direct' | null
channel_binding: 'disable' | 'prefer' | 'require'
max: number
min: number
max_pipeline: number
scheduler: boolean
idle_timeout: number | null
connect_timeout: number
queue_timeout: number
max_lifetime: number | null | (() => number)
backoff: boolean | number | ((attempts: number) => number)
keep_alive: number | null
prepare: boolean
infer: boolean
fetch_types: boolean
debug: Options['debug']
publications: string
slot: string | null
onread: boolean
unref_sockets: boolean
target_session_attrs: Options['target_session_attrs']
connection: Partial<ConnectionParameters>
types: { [K in keyof T]: PostgresType<T[K]> }
transform: Transform
parameters: Record<string, string>
serializers: Record<number, (value: any) => unknown>
parsers: Record<number, (value: any) => unknown>
}
interface Notice {
[field: string]: string
}
interface Parameter<T = SerializableParameter> extends NotAPromise {
/** The oid of the type */
type: number
/** The value as sent */
value: string | null
/** The value as given */
raw: T | null
}
interface ConnectionError extends globalThis.Error {
code:
| 'CONNECTION_DESTROYED'
| 'CONNECT_TIMEOUT'
| 'CONNECTION_CLOSED'
| 'CONNECTION_ENDED'
errno: this['code']
address: string
port?: number | undefined
}
interface NotSupportedError extends globalThis.Error {
code: 'MESSAGE_NOT_SUPPORTED'
name: string
}
interface GenericError extends globalThis.Error {
code:
| '57014'
| 'NOT_TAGGED_CALL'
| 'UNDEFINED_VALUE'
| 'MAX_PARAMETERS_EXCEEDED'
| 'ARRAY_REMOVED'
| 'COPY_IN_PROGRESS'
| 'UNSAFE_TRANSACTION'
| 'TRANSACTION_ROLLED_BACK'
| 'SSL_NOT_SUPPORTED'
| 'CHANNEL_BINDING_REQUIRED'
| 'PROTOCOL_ERROR'
| 'NUL_CHARACTER'
| 'SSL_NEGOTIATION_NOT_SUPPORTED'
| 'SASL_MECHANISM_NOT_SUPPORTED'
| 'SASL_NONCE_MISMATCH'
| 'SASL_SIGNATURE_MISMATCH'
| 'TLS_HANDSHAKE_FAILED'
| 'NO_COLUMNS'
| 'QUEUE_TIMEOUT'
| 'TYPE_NOT_FOUND'
| 'NESTED_TRANSACTION_MODE'
message: string
}
interface AuthNotImplementedError extends globalThis.Error {
code: 'AUTH_TYPE_NOT_IMPLEMENTED'
message: string
}
type Error =
| PostgresError
| ConnectionError
| NotSupportedError
| GenericError
| AuthNotImplementedError
interface ColumnInfo {
key: number
name: string
type: number
parser?(raw: string): unknown
atttypmod: number
}
interface RelationInfo {
schema: string
table: string
columns: ColumnInfo[]
keys: ColumnInfo[]
}
interface TransactionInfo {
/** The LSN of the transaction, like '0/1A2B3C4' */
lsn: string | undefined
/** When it committed */
time: Date | undefined
xid: number | undefined
}
type ReplicationEvent = TransactionInfo & (
| { command: 'insert', relation: RelationInfo }
| { command: 'update', relation: RelationInfo, key: boolean, old: Row | null }
| { command: 'delete', relation: RelationInfo, key: boolean }
| { command: 'truncate', relation: RelationInfo, cascade: boolean, restartIdentity: boolean }
)
interface SubscriptionHandle {
unsubscribe(): void
state: { pid: number | undefined, secret: number | undefined }
sql: Sql
}
interface LargeObject {
writable(options?: {
highWaterMark?: number | undefined
start?: number | undefined
}): Promise<Writable>
readable(options?: {
highWaterMark?: number | undefined
start?: number | undefined
end?: number | undefined
}): Promise<Readable>
close(): Promise<void>
tell(): Promise<void>
read(size: number): Promise<void>
write(buffer: Uint8Array): Promise<[{ data: Uint8Array }]>
truncate(size: number): Promise<void>
seek(offset: number, whence?: number): Promise<void>
size(): Promise<[{ position: bigint, size: bigint }]>
}
type EscapableArray = readonly (Serializable | Helper<any, readonly any[]> | Parameter<any> | readonly SerializableParameter[])[]
type Serializable =
| null
| boolean
| number
| bigint
| string
| Date
| Uint8Array
type SerializableParameter<T = never> =
| T
| Serializable
| Helper<any, readonly any[]>
| Parameter<any>
| readonly SerializableParameter<T>[]
type JSONValue =
| null
| string
| number
| boolean
| Date
| readonly JSONValue[]
| { toJSON(): any }
| { readonly [prop: string | number]: undefined | JSONValue | ((...args: any) => any) }
| object
interface Row {
[column: string]: any
}
type MaybeRow = Row | undefined
interface Column<T extends string> {
name: T
type: number
table: number
number: number
parser?: ((raw: string) => unknown) | undefined
}
type ColumnList<T> = (T extends string ? Column<T> : never)[]
interface State {
status: string
pid: number
secret: number
}
interface Statement {
/** The name it is prepared as, '' when unnamed */
name: string
/** The text sent */
string: string
/** The types of its parameters */
types: number[]
columns: ColumnList<string>
}
interface ResultMeta<T extends number | null> {
count: T
command: string
statement: Statement
state: State
}
interface ResultQueryMeta<T extends number | null, U> extends ResultMeta<T> {
columns: ColumnList<U>
}
type ExecutionResult<T> = [] & ResultQueryMeta<number, keyof NonNullable<T>>
type ValuesRowList<T extends readonly any[]> =
T[number][keyof T[number]][][] & ResultQueryMeta<T['length'], keyof T[number]>
type RawRowList<T extends readonly any[]> =
Buffer[][] & Iterable<Buffer[][]> & ResultQueryMeta<T['length'], keyof T[number]>
type RowList<T extends readonly any[]> =
T & Iterable<NonNullable<T[number]>> & ResultQueryMeta<T['length'], keyof T[number]>
interface PendingQueryModifiers<TRow extends readonly any[]> {
/** Sends it with the simple protocol, which allows several statements */
simple(): this
/** For `copy ... to stdout` */
readable(): Promise<Readable>
/** For `copy ... from stdin` */
writable(): Promise<Writable>
/** Sends it now instead of when awaited */
execute(): this
/** Cancels it, sent or not */
cancel(): Promise<void>
/** Cancels it when the signal aborts: `.signal(AbortSignal.timeout(5000))` */
signal(signal: AbortSignal | null | undefined): this
/** @deprecated renamed to `.forEach` */
stream(cb: (row: NonNullable<TRow[number]>, result: ExecutionResult<TRow[number]>) => void): never
/** Calls back with each row as it arrives, instead of collecting them */
forEach(cb: (row: NonNullable<TRow[number]>, result: ExecutionResult<TRow[number]>) => void): Promise<ExecutionResult<TRow[number]>>
/** Rows in batches: `for await (const rows of sql`...`.cursor(10))` */
cursor(rows?: number): AsyncIterable<NonNullable<TRow[number]>[]>
/** One row at a time; return a promise to wait, or `sql.CLOSE` to stop */
cursor(cb: (row: [NonNullable<TRow[number]>]) => unknown): Promise<ExecutionResult<TRow[number]>>
/** Batches of rows; return a promise to wait, or `sql.CLOSE` to stop */
cursor(rows: number, cb: (rows: NonNullable<TRow[number]>[]) => unknown): Promise<ExecutionResult<TRow[number]>>
}
interface PendingDescribeQuery extends Promise<Statement> {}
interface PendingValuesQuery<TRow extends readonly MaybeRow[]>
extends Promise<ValuesRowList<TRow>>, PendingQueryModifiers<TRow[number][keyof TRow[number]][][]> {
describe(): PendingDescribeQuery
}
interface PendingRawQuery<TRow extends readonly MaybeRow[]>
extends Promise<RawRowList<TRow>>, PendingQueryModifiers<Buffer[][]> {}
interface PendingQuery<TRow extends readonly MaybeRow[]>
extends Promise<RowList<TRow>>, PendingQueryModifiers<TRow> {
/** Parses and describes it without running it */
describe(): PendingDescribeQuery
/** Rows as arrays of values */
values(): PendingValuesQuery<TRow>
/** Rows as arrays of the raw bytes */
raw(): PendingRawQuery<TRow>
/** As a separator: ``sql` and `.join(fragments)``; falsy items are left out, and spaces are added where missing */
join(items: readonly (ParameterOrFragment<any> | false | null | undefined)[]): PendingQuery<Row[]> & { readonly length: number }
/** The text it will be sent as */
readonly text: string
/** The values it will be sent with */
readonly parameters: any[]
}
interface PendingRequest extends Promise<[] & ResultMeta<null>> {}
interface ListenRequest extends Promise<ListenMeta> {}
interface ListenMeta {
state: State
unlisten(): Promise<void>
}
interface Helper<T, U extends readonly any[] = T[]> extends NotAPromise {
first: T
rest: U
}
type Fragment = PendingQuery<any>
type ParameterOrJSON<T> =
| SerializableParameter<T>
| JSONValue
type ParameterOrFragment<T> =
| SerializableParameter<T>
| Fragment
| Fragment[]
interface UnsafeQueryOptions {
/** As a named prepared statement @default false */
prepare?: boolean | undefined
/** With the simple protocol @default true without parameters */
simple?: boolean | undefined
}
/** The context of the `postgres:query` tracing channel */
interface QueryTrace {
/** The text sent, with $1, $2 for the parameters */
query: string | null
/** The values; only with `debug` */
parameters: any[] | undefined
/** The name it is prepared as, '' if unnamed; null until sent */
statement: string | null
database: string
user: string
connection: number | null
host: string | null
port: number | null
command: string | null
count: number | null
/** Times from performance.now() */
queued: number
sent: number | null
firstByte: number | null
complete: number | null
result?: unknown
error?: Error | globalThis.Error
}
/** The context of the `postgres:connect` tracing channel */
interface ConnectTrace {
host: string | null
port: number | null
path: string | null
database: string
user: string
ssl: boolean
sslnegotiation: 'postgres' | 'direct'
result?: { pid: number, status: string, readOnly: boolean, standby: boolean }
error?: Error | globalThis.Error
}
/** The context of the `postgres:transaction` tracing channel */
interface TransactionTrace {
mode: string
database: string
user: string
connection: number | null
host: string | null
port: number | null
outcome: 'commit' | 'rollback' | 'prepare' | null
result?: unknown
error?: Error | globalThis.Error
}
/** A message on the `postgres:pool` channel */
interface PoolEvent {
event: 'open' | 'idle' | 'wait' | 'lease' | 'release' | 'close'
connection: number | null
host: string | null
port: number | null
database: string
stats: PoolStats
kind?: string
waited?: number
waiting?: number
error?: Error | globalThis.Error
}
/** A message on the `postgres:notice` channel */
interface NoticeEvent {
notice: Notice
connection: number
host: string
port: number
database: string
}
interface PoolStats {
connections: number
opening: number
idle: number
leased: number
waiting: number
}
interface ISql<TTypes extends Record<string, unknown> = {}> {
/**
* A helper: an identifier, a list of values, or the columns and values of objects
* @example sql`insert into users ${ sql(user, 'name', 'age') }`
*/
<T, K extends Rest<T>>(first: T & First<T, K, TTypes[keyof TTypes]>, ...rest: K): Return<T, K>
/**
* A query
* @example const users = await sql<User[]>`select * from users where id = ${ id }`
*/
<T extends readonly (object | undefined)[] = Row[]>(
template: TemplateStringsArray,
...parameters: readonly ParameterOrFragment<TTypes[keyof TTypes]>[]
): PendingQuery<T>
types: this['typed']
/** A value with the oid to send it as, or one of the custom types by name */
typed: (<T>(value: T, oid: number) => Parameter<T>) & {
[name in keyof TTypes]: (value: TTypes[name]) => Parameter<TTypes[name]>
}
/** A query from a string: never with values from users in the string itself */
unsafe<T extends any[] = (Row & Iterable<Row>)[]>(
query: string,
parameters?: ParameterOrJSON<TTypes[keyof TTypes]>[],
options?: UnsafeQueryOptions
): PendingQuery<T>
unsafe<T extends any[] = (Row & Iterable<Row>)[]>(query: string, options: UnsafeQueryOptions): PendingQuery<T>
notify(channel: string, payload: string): PendingRequest
/** @deprecated removed in v4: pass the array as it is, and cast it if needed (`${ xs }::int[]`) */
array(value: any, type?: number): never
/** A query from a file */
file<T extends readonly any[] = Row[]>(path: string | Buffer | URL | number, options?: { cache?: boolean | undefined }): PendingQuery<T>
file<T extends readonly any[] = Row[]>(
path: string | Buffer | URL | number,
args: ParameterOrJSON<TTypes[keyof TTypes]>[],
options?: { cache?: boolean | undefined }
): PendingQuery<T>
/** A value sent as json */
json(value: JSONValue): Parameter
}
interface Sql<TTypes extends Record<string, unknown> = {}> extends ISql<TTypes> {
/** Return from a cursor callback to stop it */
CLOSE: {}
END: this['CLOSE']
PostgresError: typeof PostgresError
options: ParsedOptions<TTypes>
/** The server's parameters, as last reported */
parameters: ConnectionParameters
/** What the pool is doing right now */
readonly stats: PoolStats
/** Waits for queries to finish, then closes every connection; queries after it reject */
end(options?: { timeout?: number | undefined }): Promise<void>
/** Closes every connection once its queries finish; the pool stays usable */
close(): Promise<void>
listen(channel: string, onnotify: (value: string) => void, onlisten?: () => void): ListenRequest
/**
* Logical replication. A handler may return a promise: handlers run one after the other, in
* commit order, and with a `slot` a transaction is confirmed only once they all finish.
* @param event 'insert', 'update', 'delete', 'truncate' or '*', then optionally ':table' and '=key'
*/
subscribe(
event: string,
cb: (row: Row | null, info: ReplicationEvent, event: string) => unknown,
onsubscribe?: () => void,
onerror?: (error: globalThis.Error) => void
): Promise<SubscriptionHandle>
largeObject(oid?: number, /** @default 0x00020000 | 0x00040000 */ mode?: number): Promise<LargeObject>
begin<T>(cb: (sql: TransactionSql<TTypes>) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>
begin<T>(options: string, cb: (sql: TransactionSql<TTypes>) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>
/** A connection of your own until `release()` */
reserve(): Promise<ReservedSql<TTypes>>
/** `await using sql = postgres()` ends the pool when it goes out of scope */
[Symbol.asyncDispose](): Promise<void>
}
interface TransactionSql<TTypes extends Record<string, unknown> = {}> extends ISql<TTypes> {
/** A savepoint; a mode other than the transaction's throws NESTED_TRANSACTION_MODE */
begin<T>(cb: (sql: TransactionSql<TTypes>) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>
begin<T>(options: string, cb: (sql: TransactionSql<TTypes>) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>
savepoint<T>(cb: (sql: TransactionSql<TTypes>) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>
savepoint<T>(name: string, cb: (sql: TransactionSql<TTypes>) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>
/** Ends with PREPARE TRANSACTION name instead of COMMIT */
prepare(name: string): string
/** Rolls back the transaction, or the savepoint it is called in, at once; later queries are not sent and reject with TRANSACTION_ROLLED_BACK */
rollback(): Promise<void>
}
interface ReservedSql<TTypes extends Record<string, unknown> = {}> extends ISql<TTypes> {
release(): void
/** `using reserved = await sql.reserve()` releases it when it goes out of scope */
[Symbol.dispose](): void
[Symbol.asyncDispose](): Promise<void>
}
}
export default postgres
export type Sql<TTypes extends Record<string, unknown> = {}> = postgres.Sql<TTypes>
export type TransactionSql<TTypes extends Record<string, unknown> = {}> = postgres.TransactionSql<TTypes>
export type ReservedSql<TTypes extends Record<string, unknown> = {}> = postgres.ReservedSql<TTypes>
export type Options<T extends Record<string, postgres.PostgresType> = {}> = postgres.Options<T>
export type ParsedOptions<T extends Record<string, unknown> = {}> = postgres.ParsedOptions<T>
export type PendingQuery<T extends readonly postgres.MaybeRow[]> = postgres.PendingQuery<T>
export type RowList<T extends readonly any[]> = postgres.RowList<T>
export type Row = postgres.Row
export type Fragment = postgres.Fragment
export type Helper<T, U extends readonly any[] = T[]> = postgres.Helper<T, U>
export type Parameter<T = postgres.SerializableParameter> = postgres.Parameter<T>
export type PostgresType<T = any> = postgres.PostgresType<T>
export type ReplicationEvent = postgres.ReplicationEvent
export type Notice = postgres.Notice
export declare const PostgresError: typeof postgres.PostgresError
export type PostgresError = postgres.PostgresError
/** What Sql, TransactionSql and ReservedSql have in common: take this to accept any of them */
export type ISql<TTypes extends Record<string, unknown> = {}> = postgres.ISql<TTypes>
export type Error = postgres.Error
export type QueryTrace = postgres.QueryTrace
export type ConnectTrace = postgres.ConnectTrace
export type TransactionTrace = postgres.TransactionTrace
export type PoolEvent = postgres.PoolEvent
export type NoticeEvent = postgres.NoticeEvent