Skip to content

Commit 7d43a4a

Browse files
committed
cjs build
1 parent 20284ef commit 7d43a4a

File tree

6 files changed

+166
-119
lines changed

6 files changed

+166
-119
lines changed

cjs/backend.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,28 @@ function Backend({
9898
let index = 7
9999
let length
100100
let column
101+
let value
101102

102103
const row = {}
103104
for (let i = 0; i < backend.query.statement.columns.length; i++) {
104105
column = backend.query.statement.columns[i]
105106
length = x.readInt32BE(index)
106107
index += 4
107108

108-
row[column.n] = length === -1
109+
value = length === -1
109110
? null
110111
: column.p === undefined
111112
? x.utf8Slice(index, index += length)
112113
: column.p.array === true
113114
? column.p(x.utf8Slice(index + 1, index += length))
114115
: column.p(x.utf8Slice(index, index += length))
116+
117+
row[column.n] = transform.value ? transform.value(value) : value
115118
}
116119

117120
backend.query.stream
118-
? backend.query.stream(row, rows++)
119-
: backend.query.result.push(row)
121+
? backend.query.stream(transform.row ? transform.row(row) : row, rows++)
122+
: backend.query.result.push(transform.row ? transform.row(row) : row)
120123
}
121124

122125
/* c8 ignore next */
@@ -192,7 +195,9 @@ function Backend({
192195
start = index
193196
while (x[index++] !== 0);
194197
backend.query.statement.columns[i] = {
195-
n: transform(x.utf8Slice(start, index - 1)),
198+
n: transform.column
199+
? transform.column(x.utf8Slice(start, index - 1))
200+
: x.utf8Slice(start, index - 1),
196201
p: parsers[x.readInt32BE(index + 6)]
197202
}
198203
index += 18

cjs/connection.js

+20-22
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ module.exports = Connection;function Connection(options = {}) {
2121
let timer
2222
let id = 1
2323
let ended
24+
let ready = false
2425

2526
const queries = Queue()
2627
, statements = new Map()
27-
, connection = { send, end, destroy, ready: false, active: false }
28+
, connection = { send, end, destroy }
2829

2930
const socket = postgresSocket(options, {
31+
ready: () => socket.write(frontend.connect(options)),
3032
data,
31-
ready,
3233
error,
3334
close
3435
})
@@ -60,13 +61,13 @@ module.exports = Connection;function Connection(options = {}) {
6061
function resolve(x) {
6162
backend.query.resolve(x)
6263
backend.query = null
63-
timeout && connection.active && queries.length === 0 && idle()
64+
timeout && queries.length === 0 && idle()
6465
}
6566

6667
function reject(err) {
6768
backend.query ? backend.query.reject(err) : error(err)
6869
backend.query = null
69-
timeout && connection.active && queries.length === 0 && idle()
70+
timeout && queries.length === 0 && idle()
7071
}
7172

7273
function end() {
@@ -96,19 +97,21 @@ module.exports = Connection;function Connection(options = {}) {
9697
query.result = []
9798
query.result.count = null
9899
timeout && clearTimeout(timer)
99-
!connection.ready || backend.query
100-
? queries.push(query)
101-
: (backend.query = query)
102100

103101
const buffer = query.simple
104102
? simple(str, query)
105103
: statements.has(sig)
106104
? prepared(statements.get(sig), args, query)
107105
: prepare(sig, str, args, query)
108106

109-
connection.ready
110-
? socket.write(buffer)
111-
: (messages.push(buffer), socket.connect())
107+
if (ready && !backend.query) {
108+
backend.query = query
109+
socket.write(buffer)
110+
} else {
111+
queries.push(query)
112+
messages.push(buffer)
113+
!ready && socket.connect()
114+
}
112115
}
113116

114117
function simple(str, query) {
@@ -135,18 +138,17 @@ module.exports = Connection;function Connection(options = {}) {
135138
}
136139

137140
function onready() {
138-
if (!backend.query)
139-
backend.query = queries.shift()
140-
141141
if (!backend.query && queries.length === 0 && ended)
142142
return ended()
143143

144-
if (!connection.ready) {
144+
if (!ready) {
145145
messages.forEach(socket.write)
146146
messages = []
147-
connection.ready = true
148-
connection.onconnect && connection.onconnect()
147+
ready = true
149148
}
149+
150+
if (!backend.query)
151+
backend.query = queries.shift()
150152
}
151153

152154
function data(x) {
@@ -164,14 +166,10 @@ module.exports = Connection;function Connection(options = {}) {
164166
}
165167
}
166168

167-
function ready() {
168-
socket.write(frontend.connect(options))
169-
}
170-
171169
function close() {
172170
error(errors.connection('CLOSED', options))
173171
statements.clear()
174-
connection.ready = connection.active = false
172+
ready = false
175173
}
176174

177175
function unknown() {
@@ -196,7 +194,7 @@ function postgresSocket(options, {
196194
return Promise.resolve()
197195
},
198196
end: () => {
199-
return new Promise(r => socket.end(r))
197+
return new Promise(r => socket && socket.end(r))
200198
},
201199
connect
202200
}

0 commit comments

Comments
 (0)