Skip to content

Commit df3799f

Browse files
committed
build cjs
1 parent aee4f0c commit df3799f

File tree

5 files changed

+162
-99
lines changed

5 files changed

+162
-99
lines changed

cjs/backend.js

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

88
function Backend({
9+
onparameter,
910
parsers,
1011
onauth,
1112
onready,
1213
resolve,
1314
reject,
15+
transform,
1416
onnotice,
1517
onnotify
1618
}) {
17-
let result = null
18-
let columns = null
1919
let rows = 0
2020

2121
const backend = Object.entries({
@@ -47,24 +47,15 @@ function Backend({
4747

4848
const state = backend.state = {
4949
status : 'I',
50-
settings : {},
5150
pid : null,
5251
secret : null
5352
}
5453

5554
return backend
5655

57-
function ParseComplete() {
58-
// No handling needed
59-
}
60-
61-
function BindComplete() {
62-
// No handling needed
63-
}
64-
65-
function CloseComplete() {
66-
// No handling needed
67-
}
56+
function ParseComplete() { /* No handling needed */ }
57+
function BindComplete() { /* No handling needed */ }
58+
function CloseComplete() { /* No handling needed */ }
6859

6960
function NotificationResponse(x) {
7061
if (!onnotify)
@@ -79,27 +70,34 @@ function Backend({
7970
}
8071

8172
function CommandComplete(x) {
82-
backend.query && resolve(backend.query.stream
83-
? rows + 1
84-
: result
73+
if (!backend.query)
74+
return
75+
76+
for (let i = x.length - 1; i > 0; i--) {
77+
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
79+
if (x[i - 1] >= 65) {
80+
backend.query.result.command = x.utf8Slice(5, i)
81+
break
82+
}
83+
}
84+
85+
resolve(backend.query.stream
86+
? backend.query.result.count
87+
: backend.query.result
8588
)
86-
result = null
87-
columns = null
88-
rows = 0
8989
}
9090

91-
function CopyDone(x) {
92-
// No handling needed
93-
}
91+
function CopyDone(x) { /* No handling needed */ }
9492

9593
function DataRow(x) {
9694
let index = 7
9795
let length
9896
let column
9997

10098
const row = {}
101-
for (let i = 0; i < columns.length; i++) {
102-
column = columns[i]
99+
for (let i = 0; i < backend.query.statement.columns.length; i++) {
100+
column = backend.query.statement.columns[i]
103101
length = x.readInt32BE(index)
104102
index += 4
105103

@@ -114,12 +112,10 @@ function Backend({
114112

115113
backend.query.stream
116114
? backend.query.stream(row, rows++)
117-
: result.push(row)
115+
: backend.query.result.push(row)
118116
}
119117

120-
function CopyData(x) {
121-
// No handling needed until implemented
122-
}
118+
function CopyData(x) { /* No handling needed until implemented */ }
123119

124120
function ErrorResponse(x) {
125121
reject(errors.generic(error(x)))
@@ -133,22 +129,20 @@ function Backend({
133129
reject(errors.notSupported('CopyOutResponse'))
134130
}
135131

136-
function EmptyQueryResponse() {
137-
// No handling needed
138-
}
132+
function EmptyQueryResponse() { /* No handling needed */ }
139133

140134
function BackendKeyData(x) {
141135
state.pid = x.readInt32BE(5)
142136
state.secret = x.readInt32BE(9)
143137
}
144138

145139
function NoticeResponse(x) {
146-
onnotice(error(x))
140+
onnotice
141+
? onnotice(error(x))
142+
: console.log(error(x))
147143
}
148144

149-
function NoData(x) {
150-
// No handling needed
151-
}
145+
function NoData(x) { /* No handling needed */ }
152146

153147
function Authentication(x) {
154148
const type = x.readInt32BE(5)
@@ -157,7 +151,7 @@ function Backend({
157151

158152
function ParameterStatus(x) {
159153
const [k, v] = x.utf8Slice(5, x.length - 1).split(N)
160-
state.settings[k] = v
154+
onparameter(k, v)
161155
}
162156

163157
function PortalSuspended(x) {
@@ -169,19 +163,22 @@ function Backend({
169163
}
170164

171165
function RowDescription(x) {
166+
rows = 0
167+
168+
if (backend.query.statement.columns)
169+
return
170+
172171
const length = x.readInt16BE(5)
173172
let index = 7
174173
let start
175174

176-
columns = Array(length)
177-
result = []
178-
rows = 0
175+
backend.query.statement.columns = Array(length)
179176

180177
for (let i = 0; i < length; ++i) {
181178
start = index
182179
while (x[index++] !== 0);
183-
columns[i] = {
184-
n: x.utf8Slice(start, index - 1),
180+
backend.query.statement.columns[i] = {
181+
n: transform(x.utf8Slice(start, index - 1)),
185182
p: parsers[x.readInt32BE(index + 6)]
186183
}
187184
index += 18

cjs/connection.js

+20-15
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const { errors } = require('./types.js')
77

88
module.exports = function Connection(options = {}) {
99
const {
10-
database,
11-
user,
10+
onparameter,
11+
transform,
1212
timeout,
1313
onnotify,
1414
onnotice,
@@ -34,6 +34,8 @@ module.exports = function Connection(options = {}) {
3434
})
3535

3636
const backend = Backend({
37+
onparameter,
38+
transform,
3739
parsers,
3840
resolve,
3941
reject,
@@ -84,31 +86,34 @@ module.exports = function Connection(options = {}) {
8486
q.reject(err)
8587
}
8688

87-
function send(query, { str, args = [] }) {
89+
function send(query, { sig, str, args = [] }) {
90+
query.result = []
91+
query.result.count = null
8892
timeout && clearTimeout(timer)
8993
!connection.ready || backend.query
9094
? queries.push(query)
9195
: (backend.query = query)
9296

93-
const buffer = statements.has(str)
94-
? prepared(statements.get(str), str, args)
95-
: prepare(str, args)
97+
const buffer = statements.has(sig)
98+
? prepared(statements.get(sig), args, query)
99+
: prepare(sig, str, args, query)
96100

97101
connection.ready
98102
? socket.write(buffer)
99103
: (messages.push(buffer), socket.connect())
100104
}
101105

102-
function prepared(name, str, args) {
103-
return frontend.Bind(name, args)
106+
function prepared(statement, args, query) {
107+
query.statement = statement
108+
return frontend.Bind(statement.name, args)
104109
}
105110

106-
function prepare(str, args) {
107-
const name = 'p' + id++
108-
statements.set(str, name)
111+
function prepare(sig, str, args, query) {
112+
query.statement = { name: sig ? 'p' + id++ : '' }
113+
sig && statements.set(sig, query.statement)
109114
return Buffer.concat([
110-
frontend.Parse(name, str, args),
111-
frontend.Bind(name, args)
115+
frontend.Parse(query.statement.name, str, args),
116+
frontend.Bind(query.statement.name, args)
112117
])
113118
}
114119

@@ -141,13 +146,13 @@ module.exports = function Connection(options = {}) {
141146
if (length >= buffer.length)
142147
break
143148

144-
(backend[buffer[0]] || unknown)(buffer)
149+
(backend[buffer[0]] || unknown)(buffer.slice(0, length + 1))
145150
buffer = buffer.slice(length + 1)
146151
}
147152
}
148153

149154
function ready() {
150-
socket.write(frontend.connect({ user, database }))
155+
socket.write(frontend.connect(options))
151156
}
152157

153158
function close() {

0 commit comments

Comments
 (0)