Skip to content

Commit a19e436

Browse files
committed
More README
1 parent 25b4644 commit a19e436

File tree

6 files changed

+123
-154
lines changed

6 files changed

+123
-154
lines changed

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,17 @@ const postgres = require('postgres/cjs')
3737

3838
## Connection options
3939

40+
You can use either a postgres:// url connection string or the options to define your database connection properties.
41+
4042
```js
4143

4244
const sql = postgres('postgres://user:pass@host:port/database', {
45+
host : // or hostname
46+
port : // Postgres server port
47+
path : // unix socket path (usually /tmp)
48+
database : // Database to connect to
49+
username : // or username
50+
password : // or password
4351
ssl : false, // True, or an object with options to tls.connect
4452
max : 10, // Max number of connections
4553
timeout : 0, // Idle connection timeout in seconds
@@ -130,7 +138,7 @@ sql.notify('news', JSON.stringify({ no: 'this', is: 'news' }))
130138
Due to the nature of sql and Postgres types various helpers are available to simplify queries.
131139

132140
#### Object to row `row(Object, ...columns)`
133-
Sometimes the number of columns can be quite large, so typing out
141+
Sometimes the number of columns can be quite large, so this is shorter.
134142

135143
```js
136144

@@ -350,7 +358,7 @@ There are also the following errors specifically for this library.
350358
##### MESSAGE_NOT_SUPPORTED
351359
> X (X) is not supported
352360
353-
Whenever a mesage is received which is not supported by this library. Feel free to file an issue if you think something is missing.
361+
Whenever a message is received from Postgres which is not supported by this library. Feel free to file an issue if you think something is missing.
354362

355363
##### SASL_SIGNATURE_MISMATCH
356364
> Message type X not supported

cjs/bytes.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ const b = {
3030
b.i = 5
3131
return b
3232
},
33-
dec(x) {
34-
b.i -= x
35-
return b
36-
},
3733
inc(x) {
3834
b.i += x
3935
return b
@@ -44,11 +40,7 @@ const b = {
4440
b.i += buffer.utf8Write(x, b.i, length)
4541
return b
4642
},
47-
i16(x, i) {
48-
if (i || i === 0) {
49-
buffer.writeUInt16BE(x, i)
50-
return b
51-
}
43+
i16(x) {
5244
fit(2)
5345
buffer.writeUInt16BE(x, b.i)
5446
b.i += 2
@@ -70,12 +62,6 @@ const b = {
7062
b.i += x
7163
return b
7264
},
73-
push(x) {
74-
fit(x.length)
75-
x.copy(buffer, b.i)
76-
b.i += x.length
77-
return b
78-
},
7965
end(at = 1) {
8066
buffer.writeUInt32BE(b.i - at, at)
8167
const out = buffer.slice(0, b.i)

cjs/connection.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = function Connection(options = {}) {
3434
})
3535

3636
const backend = Backend({
37+
onparse,
3738
onparameter,
3839
transform,
3940
parsers,
@@ -47,6 +48,11 @@ module.exports = function Connection(options = {}) {
4748

4849
return connection
4950

51+
function onparse() {
52+
if (backend.query && backend.query.statement.name)
53+
statements[backend.query.statement.sig] = backend.query.statement.name
54+
}
55+
5056
function onauth(type, x) {
5157
socket.write(frontend.auth(type, x, options))
5258
}
@@ -94,9 +100,11 @@ module.exports = function Connection(options = {}) {
94100
? queries.push(query)
95101
: (backend.query = query)
96102

97-
const buffer = statements.has(sig)
98-
? prepared(statements.get(sig), args, query)
99-
: prepare(sig, str, args, query)
103+
const buffer = query.simple
104+
? frontend.Query(str)
105+
: statements.has(sig)
106+
? prepared(statements.get(sig), args, query)
107+
: prepare(sig, str, args, query)
100108

101109
connection.ready
102110
? socket.write(buffer)
@@ -109,8 +117,7 @@ module.exports = function Connection(options = {}) {
109117
}
110118

111119
function prepare(sig, str, args, query) {
112-
query.statement = { name: sig ? 'p' + id++ : '' }
113-
sig && statements.set(sig, query.statement)
120+
query.statement = { name: sig ? 'p' + id++ : '', sig }
114121
return Buffer.concat([
115122
frontend.Parse(query.statement.name, str, args),
116123
frontend.Bind(query.statement.name, args)
@@ -133,6 +140,7 @@ module.exports = function Connection(options = {}) {
133140
messages.forEach(socket.write)
134141
messages = []
135142
connection.ready = true
143+
connection.onconnect && connection.onconnect()
136144
}
137145
}
138146

@@ -161,8 +169,8 @@ module.exports = function Connection(options = {}) {
161169
connection.ready = connection.active = false
162170
}
163171

164-
function unknown(buffer) {
165-
// console.log('Unknown Message', buffer[0])
172+
function unknown() {
173+
// console.log('Unknown Message', x[0])
166174
}
167175
}
168176

@@ -174,18 +182,15 @@ function postgresSocket(options, {
174182
}) {
175183
let socket
176184
let closed = true
177-
let ended = false
178185

179186
return {
180187
ready: false,
181188
write: x => socket.write(x),
182189
destroy: () => {
183-
ended = true
184190
socket.destroy()
185191
return Promise.resolve()
186192
},
187193
end: () => {
188-
ended = true
189194
return new Promise(r => socket.end(r))
190195
},
191196
connect

cjs/frontend.js

+16-41
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,13 @@ const authNames = {
2525
}
2626

2727
const auths = {
28-
2 : authNotImplemented,
29-
3 : authNotImplemented,
28+
3 : AuthenticationCleartextPassword,
3029
5 : AuthenticationMD5Password,
31-
6 : authNotImplemented,
32-
7 : authNotImplemented,
33-
8 : authNotImplemented,
34-
9 : authNotImplemented,
3530
10: SASL,
3631
11: SASLContinue,
3732
12: SASLFinal
3833
}
3934

40-
const messages = {
41-
B : 'Bind',
42-
C : 'Close',
43-
D : 'Describe',
44-
E : 'Execute',
45-
F : 'FunctionCall',
46-
f : 'CopyFail',
47-
H : 'Flush',
48-
P : 'Parse',
49-
p : 'PasswordMessage',
50-
S : 'Sync',
51-
Q : 'Query',
52-
X : 'Terminate'
53-
}
5435

5536
module.exports = {
5637
connect,
@@ -60,20 +41,6 @@ module.exports = {
6041
Query
6142
}
6243

63-
/* Other messages
64-
65-
Close (C)
66-
Describe (D)
67-
Execute (F)
68-
FunctionCall (F)
69-
CopyFail (f)
70-
Flush (H)
71-
PasswordMessage (p)
72-
Sync (S)
73-
Terminate (X)
74-
75-
*/
76-
7744
function connect({ user, database, connection }) {
7845
return bytes
7946
.inc(4)
@@ -84,19 +51,27 @@ function connect({ user, database, connection }) {
8451
database,
8552
client_encoding: '\'utf-8\'',
8653
...connection
87-
}).filter(([k, v]) => v).map(([k, v]) => k + N + v).join(N))
54+
}).filter(([, v]) => v).map(([k, v]) => k + N + v).join(N))
8855
.z(2)
8956
.end(0)
9057
}
9158

9259
function auth(type, x, options) {
93-
return auths[type](type, x, options) || ''
60+
return (auths[type] || authNotImplemented)(type, x, options) || ''
61+
}
62+
63+
function AuthenticationCleartextPassword(type, x, { pass }) {
64+
return bytes
65+
.p()
66+
.str(pass)
67+
.z(1)
68+
.end()
9469
}
9570

96-
function AuthenticationMD5Password(type, x, { user, password }) {
71+
function AuthenticationMD5Password(type, x, { user, pass }) {
9772
return bytes
9873
.p()
99-
.str('md5' + md5(Buffer.concat([Buffer.from(md5(password + user)), x.slice(9)])))
74+
.str('md5' + md5(Buffer.concat([Buffer.from(md5(pass + user)), x.slice(9)])))
10075
.z(1)
10176
.end()
10277
}
@@ -119,7 +94,7 @@ function SASLContinue(type, x, options) {
11994
const res = x.utf8Slice(9).split(',').reduce((acc, x) => (acc[x[0]] = x.slice(2), acc), {})
12095

12196
const saltedPassword = crypto.pbkdf2Sync(
122-
options.password,
97+
options.pass,
12398
Buffer.from(res.s, 'base64'),
12499
parseInt(res.i), 32,
125100
'sha256'
@@ -139,7 +114,7 @@ function SASLContinue(type, x, options) {
139114
}
140115

141116
function SASLFinal(type, x, options) {
142-
if (x.utf8Slice(9).split(N, 1)[0].slice(2, -1) !== options.serverSignature) {
117+
if (x.utf8Slice(9).split(N, 1)[0].slice(2) !== options.serverSignature) {
143118
throw errors.generic({
144119
message: 'The server did not return the correct signature',
145120
code: 'SASL_SIGNATURE_MISMATCH'
@@ -172,7 +147,7 @@ function Bind(name, args) {
172147
.i16(0)
173148
.i16(args.length)
174149

175-
args.forEach((x, i) => {
150+
args.forEach(x => {
176151
if (x.value == null)
177152
return bytes.i32(0xFFFFFFFF)
178153

0 commit comments

Comments
 (0)