Skip to content

Commit f632a36

Browse files
committed
Support down to node 6
1 parent f314ef2 commit f632a36

File tree

5 files changed

+42
-36
lines changed

5 files changed

+42
-36
lines changed

lib/backend.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { errorFields, errors } = require('./types.js')
1+
const { errorFields, errors, entries } = require('./types.js')
22

33
const char = (acc, [k, v]) => (acc[k.charCodeAt(0)] = v, acc)
44
, N = '\u0000'
@@ -18,7 +18,7 @@ function Backend({
1818
}) {
1919
let rows = 0
2020

21-
const backend = Object.entries({
21+
const backend = entries({
2222
1: ParseComplete,
2323
2: BindComplete,
2424
3: CloseComplete,

lib/connection.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,10 @@ function postgresSocket(options, {
200200
}
201201

202202
function onclose() {
203-
socket.off('data', data)
204-
socket.off('error', error)
205-
socket.off('ready', ready)
206-
socket.off('secureConnect', ready)
203+
socket.removeListener('data', data)
204+
socket.removeListener('error', error)
205+
socket.removeListener('connect', ready)
206+
socket.removeListener('secureConnect', ready)
207207
closed = true
208208
close()
209209
}
@@ -221,14 +221,14 @@ function postgresSocket(options, {
221221
if (!options.ssl)
222222
return attach(socket)
223223

224-
socket.once('ready', () => socket.write(Buffer.from('0000000804d2162f', 'hex')))
224+
socket.once('connect', () => socket.write(Buffer.from('0000000804d2162f', 'hex')))
225225
socket.once('error', error)
226226
socket.once('close', onclose)
227227
socket.once('data', x => {
228-
socket.off('error', error)
229-
socket.off('close', onclose)
228+
socket.removeListener('error', error)
229+
socket.removeListener('close', onclose)
230230
x.toString() === 'S'
231-
? attach(tls.connect({ socket, ...options.ssl }))
231+
? attach(tls.connect(Object.assign({ socket }, options.ssl)))
232232
: error('Server does not support SSL')
233233
})
234234
}
@@ -237,7 +237,7 @@ function postgresSocket(options, {
237237
socket = x
238238
socket.on('data', data)
239239
socket.once('error', error)
240-
socket.once('ready', ready)
240+
socket.once('connect', ready)
241241
socket.once('secureConnect', ready)
242242
socket.once('close', onclose)
243243
}

lib/frontend.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const crypto = require('crypto')
22
const bytes = require('./bytes.js')
3-
const { errors } = require('./types.js')
3+
const { errors, entries } = require('./types.js')
44

55
const N = String.fromCharCode(0)
66
const execute = bytes
@@ -46,12 +46,13 @@ function connect({ user, database, connection }) {
4646
.inc(4)
4747
.i16(3)
4848
.z(2)
49-
.str(Object.entries({
49+
.str(entries(Object.assign({
5050
user,
5151
database,
5252
client_encoding: '\'utf-8\'',
53-
...connection
54-
}).filter(([, v]) => v).map(([k, v]) => k + N + v).join(N))
53+
},
54+
connection
55+
)).filter(([, v]) => v).map(([k, v]) => k + N + v).join(N))
5556
.z(2)
5657
.end(0)
5758
}

lib/index.js

+17-15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
arrayParser,
1111
inferType,
1212
toPascal,
13+
entries,
1314
toCamel,
1415
toKebab,
1516
errors,
@@ -54,8 +55,8 @@ function Postgres(url, options) {
5455
, isInsert = /(^|[^)(])\s*insert\s+into\s+[^\s]+\s*$/i
5556
, isSelect = /(^|[^)(])\s*select\s*$/i
5657

57-
function postgres(xs, ...args) {
58-
return query({}, getConnection(), xs, args)
58+
function postgres(xs) {
59+
return query({}, getConnection(), xs, Array.from(arguments).slice(1))
5960
}
6061

6162
Object.assign(postgres, {
@@ -137,8 +138,8 @@ function Postgres(url, options) {
137138
next()
138139
})
139140

140-
function scoped(xs, ...args) {
141-
return query({}, connection, xs, args)
141+
function scoped(xs) {
142+
return query({}, connection, xs, Array.from(arguments).slice(1))
142143
}
143144
}
144145

@@ -284,7 +285,7 @@ function Postgres(url, options) {
284285
return promise
285286
}
286287

287-
options.types && Object.entries(options.types).forEach(([name, type]) => {
288+
options.types && entries(options.types).forEach(([name, type]) => {
288289
if (name in sql)
289290
throw errors.generic({ message: name + ' is a reserved method name', code: 'RESERVED_METHOD_NAME' })
290291

@@ -307,10 +308,10 @@ function Postgres(url, options) {
307308
if (listener)
308309
return listener
309310

310-
listener = Connection({
311-
...options,
312-
onnotify: (c, x) => c in listeners && listeners[c].forEach(fn => fn(x))
313-
})
311+
listener = Connection(Object.assign({},
312+
options,
313+
{ onnotify: (c, x) => c in listeners && listeners[c].forEach(fn => fn(x)) }
314+
))
314315
all.push(listener)
315316
return listener
316317
}
@@ -385,7 +386,7 @@ function Postgres(url, options) {
385386
}
386387

387388
function selectHelper(first, columns, xargs, types) {
388-
return Object.entries(first).reduce((acc, [k, v]) =>
389+
return entries(first).reduce((acc, [k, v]) =>
389390
acc + (!columns.length || columns.indexOf(k) > -1
390391
? (acc ? ',' : '') + parseValue(v, xargs, types) + ' as ' + escape(k)
391392
: ''
@@ -453,7 +454,7 @@ function parseOptions(uri = {}, options) {
453454
, host = o.hostname || o.host || url.hostname || env.PGHOST || 'localhost'
454455
, port = o.port || url.port || env.PGPORT || 5432
455456

456-
return {
457+
return Object.assign({
457458
host,
458459
port,
459460
path : o.path || host.indexOf('/') > -1 && host + '/.s.PGSQL.' + port,
@@ -467,9 +468,10 @@ function parseOptions(uri = {}, options) {
467468
onnotice : o.onnotice,
468469
onparameter : o.onparameter,
469470
nonce : crypto.randomBytes(18).toString('base64'),
470-
transform : { ...o.transform },
471-
connection : { application_name: 'postgres.js', ...o.connection },
471+
transform : Object.assign({}, o.transform),
472+
connection : Object.assign({ application_name: 'postgres.js' }, o.connection),
472473
debug : o.debug,
473-
...mergeUserTypes(o.types)
474-
}
474+
},
475+
mergeUserTypes(o.types)
476+
)
475477
}

lib/types.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const char = (acc, [k, v]) => (acc[k.charCodeAt(0)] = v, acc)
2+
const entries = o => Object.keys(o).map(x => [x, o[x]])
23

34
// These were the fastest ways to do it in Node.js v12.11.1 (add tests to revise if this changes)
45
const types = module.exports.types = {
@@ -44,18 +45,20 @@ const defaultHandlers = typeHandlers(types)
4445
const serializers = module.exports.serializers = defaultHandlers.serializers
4546
const parsers = module.exports.parsers = defaultHandlers.parsers
4647

48+
module.exports.entries = entries
49+
4750
module.exports.mergeUserTypes = function(types) {
4851
const user = typeHandlers(types || {})
4952
return {
50-
serializers: { ...serializers, ...user.serializers },
51-
parsers: { ...parsers, ...user.parsers }
53+
serializers: Object.assign({}, serializers, user.serializers),
54+
parsers: Object.assign({}, parsers, user.parsers)
5255
}
5356
}
5457

5558
function typeHandlers(types) {
56-
return Object.entries(types).reduce((acc, [, type]) => {
57-
type.from && type.from.forEach(x => acc.parsers[x] = type.parse)
58-
acc.serializers[type.to] = type.serialize
59+
return Object.keys(types).reduce((acc, k) => {
60+
types[k].from && types[k].from.forEach(x => acc.parsers[x] = types[k].parse)
61+
acc.serializers[types[k].to] = types[k].serialize
5962
return acc
6063
}, { parsers: {}, serializers: {} })
6164
}
@@ -206,7 +209,7 @@ module.exports.errors = {
206209
)
207210
}
208211

209-
module.exports.errorFields = Object.entries({
212+
module.exports.errorFields = entries({
210213
S: 'severity_local',
211214
V: 'severity',
212215
C: 'code',

0 commit comments

Comments
 (0)