Skip to content

Commit 418383a

Browse files
committed
Add tests
1 parent 21eabd1 commit 418383a

File tree

8 files changed

+492
-28
lines changed

8 files changed

+492
-28
lines changed

.eslintrc.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"no-extra-label": 2,
7676
"no-fallthrough": 2,
7777
"no-floating-decimal": 2,
78-
"no-implicit-coercion": 2,
78+
"no-implicit-coercion": 0,
7979
"no-implicit-globals": 2,
8080
"no-implied-eval": 2,
8181
"no-invalid-this": 2,
@@ -129,7 +129,7 @@
129129
"no-shadow-restricted-names": 2,
130130
"no-undef": 2,
131131
"no-undef-init": 2,
132-
"no-unused-vars": 0,
132+
"no-unused-vars": 2,
133133
"no-use-before-define": [
134134
2,
135135
{

cjs/backend.js

+30-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const char = (acc, [k, v]) => (acc[k.charCodeAt(0)] = v, acc)
66
module.exports = Backend
77

88
function Backend({
9+
onparse,
910
onparameter,
1011
parsers,
1112
onauth,
@@ -53,7 +54,10 @@ function Backend({
5354

5455
return backend
5556

56-
function ParseComplete() { /* No handling needed */ }
57+
function ParseComplete() {
58+
onparse()
59+
}
60+
5761
function BindComplete() { /* No handling needed */ }
5862
function CloseComplete() { /* No handling needed */ }
5963

@@ -75,7 +79,7 @@ function Backend({
7579

7680
for (let i = x.length - 1; i > 0; i--) {
7781
if (x[i] === 32 && x[i + 1] < 58 && backend.query.result.count === null)
78-
backend.query.result.count = +x.utf8Slice(i + 1, x.length - 1) // eslint-disable-line
82+
backend.query.result.count = +x.utf8Slice(i + 1, x.length - 1)
7983
if (x[i - 1] >= 65) {
8084
backend.query.result.command = x.utf8Slice(5, i)
8185
break
@@ -88,7 +92,7 @@ function Backend({
8892
)
8993
}
9094

91-
function CopyDone(x) { /* No handling needed */ }
95+
function CopyDone() { /* No handling needed */ }
9296

9397
function DataRow(x) {
9498
let index = 7
@@ -115,20 +119,24 @@ function Backend({
115119
: backend.query.result.push(row)
116120
}
117121

118-
function CopyData(x) { /* No handling needed until implemented */ }
122+
/* c8 ignore next */
123+
function CopyData() { /* No handling needed until implemented */ }
119124

120125
function ErrorResponse(x) {
121126
reject(errors.generic(error(x)))
122127
}
123128

129+
/* c8 ignore next */
124130
function CopyInResponse() {
125131
reject(errors.notSupported('CopyInResponse'))
126132
}
127133

134+
/* c8 ignore next */
128135
function CopyOutResponse() {
129136
reject(errors.notSupported('CopyOutResponse'))
130137
}
131138

139+
/* c8 ignore next */
132140
function EmptyQueryResponse() { /* No handling needed */ }
133141

134142
function BackendKeyData(x) {
@@ -139,26 +147,32 @@ function Backend({
139147
function NoticeResponse(x) {
140148
onnotice
141149
? onnotice(error(x))
142-
: console.log(error(x))
150+
: console.log(error(x)) // eslint-disable-line
143151
}
144152

145-
function NoData(x) { /* No handling needed */ }
153+
function NoData() { /* No handling needed */ }
146154

147155
function Authentication(x) {
148156
const type = x.readInt32BE(5)
149-
type !== 0 && onauth(type, x)
157+
try {
158+
type !== 0 && onauth(type, x)
159+
} catch (err) {
160+
reject(err)
161+
}
150162
}
151163

152164
function ParameterStatus(x) {
153165
const [k, v] = x.utf8Slice(5, x.length - 1).split(N)
154166
onparameter(k, v)
155167
}
156168

157-
function PortalSuspended(x) {
169+
/* c8 ignore next */
170+
function PortalSuspended() {
158171
reject(errors.notSupported('PortalSuspended'))
159172
}
160173

161-
function ParameterDescription(x) {
174+
/* c8 ignore next */
175+
function ParameterDescription() {
162176
reject(errors.notSupported('ParameterDescription'))
163177
}
164178

@@ -185,22 +199,24 @@ function Backend({
185199
}
186200
}
187201

188-
function FunctionCallResponse(x) {
202+
/* c8 ignore next */
203+
function FunctionCallResponse() {
189204
reject(errors.notSupported('FunctionCallResponse'))
190205
}
191206

192-
function NegotiateProtocolVersion(x) {
207+
/* c8 ignore next */
208+
function NegotiateProtocolVersion() {
193209
reject(errors.notSupported('NegotiateProtocolVersion'))
194210
}
195211

196-
function CopyBothResponse(x) {
212+
/* c8 ignore next */
213+
function CopyBothResponse() {
197214
reject(errors.notSupported('CopyBothResponse'))
198215
}
199216

200-
function ReadyForQuery(x) {
217+
function ReadyForQuery() {
201218
onready()
202219
}
203-
204220
}
205221

206222
function error(x) {

cjs/connection.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ module.exports = function Connection(options = {}) {
101101
: (backend.query = query)
102102

103103
const buffer = query.simple
104-
? frontend.Query(str)
104+
? simple(str, query)
105105
: statements.has(sig)
106106
? prepared(statements.get(sig), args, query)
107107
: prepare(sig, str, args, query)
@@ -111,6 +111,11 @@ module.exports = function Connection(options = {}) {
111111
: (messages.push(buffer), socket.connect())
112112
}
113113

114+
function simple(str, query) {
115+
query.statement = {}
116+
return frontend.Query(str)
117+
}
118+
114119
function prepared(statement, args, query) {
115120
query.statement = statement
116121
return frontend.Bind(statement.name, args)

cjs/frontend.js

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ function SASLFinal(type, x, options) {
122122
}
123123
}
124124

125+
/* c8 ignore next */
125126
function authNotImplemented(type) {
126127
throw errors.generic({
127128
message: 'Auth type ' + (authNames[type] || type) + ' not implemented',

cjs/types.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ const types = module.exports.types = {
55
string: {
66
to: 25,
77
from: null, // defaults to string
8-
serialize: x => '' + x // eslint-disable-line
8+
serialize: x => '' + x
99
},
1010
number: {
1111
to: 1700,
1212
from: [20, 21, 23, 26, 700, 701, 790, 1700],
13-
serialize: x => '' + x, // eslint-disable-line
14-
parse: x => +x // eslint-disable-line
13+
serialize: x => '' + x,
14+
parse: x => +x
1515
},
1616
json: {
1717
to: 3802,
1818
from: [114, 3802],
1919
serialize: x => JSON.stringify(x),
20-
parse: x => JSON.parse(JSON.parse(x))
20+
parse: x => JSON.parse(x)
2121
},
2222
boolean: {
2323
to: 16,
@@ -28,7 +28,7 @@ const types = module.exports.types = {
2828
date: {
2929
to: 1184,
3030
from: [1082, 1083, 1114, 1184],
31-
serialize: x => x.toJSON(),
31+
serialize: x => x.toISOString(),
3232
parse: x => new Date(x)
3333
},
3434
bytea: {
@@ -53,7 +53,7 @@ module.exports.mergeUserTypes = function mergeUserTypes(types) {
5353
}
5454

5555
function typeHandlers(types) {
56-
return Object.entries(types).reduce((acc, [name, type]) => {
56+
return Object.entries(types).reduce((acc, [, type]) => {
5757
type.from && type.from.forEach(x => acc.parsers[x] = type.parse)
5858
acc.serializers[type.to] = type.serialize
5959
return acc
@@ -67,13 +67,13 @@ const type = {
6767
}
6868

6969
module.exports.inferType = function inferType(x) {
70-
return x instanceof Date
70+
return x.type || (x instanceof Date
7171
? 1184
7272
: Array.isArray(x)
7373
? inferType(x[0])
7474
: x instanceof Buffer
7575
? 17
76-
: type[typeof x] || 25
76+
: type[typeof x] || 25)
7777
}
7878

7979
const escapeBackslash = /\\/g
@@ -91,10 +91,12 @@ module.exports.arraySerializer = function arraySerializer(xs, serializer) {
9191

9292
const first = xs[0]
9393

94-
if (Array.isArray(first))
94+
if (Array.isArray(first) && !first.type)
9595
return '{' + xs.map(x => arraySerializer(x, serializer)).join(',') + '}'
9696

97-
return '{' + xs.map(x => '"' + arrayEscape(serializer(x)) + '"').join(',') + '}'
97+
return '{' + xs.map(x =>
98+
'"' + arrayEscape(serializer ? serializer(x.type ? x.value : x) : '' + x) + '"'
99+
).join(',') + '}'
98100
}
99101

100102
const arrayParserState = {

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "lib/index.js",
66
"type": "module",
77
"scripts": {
8-
"prepublishOnly": "npm run build",
8+
"test": "node tests/index.js",
9+
"prepublishOnly": "eslint lib && npm run build",
910
"build": "node cjs/transpile.js"
1011
},
1112
"files": [
@@ -16,6 +17,7 @@
1617
"license": "WTFPL",
1718
"repository": "porsager/postgres",
1819
"keywords": [
20+
"driver",
1921
"postgresql",
2022
"postgres",
2123
"postgre",

0 commit comments

Comments
 (0)