Skip to content

Commit 95651b6

Browse files
committed
Fix graceful end
1 parent 1c66a2a commit 95651b6

File tree

6 files changed

+56
-37
lines changed

6 files changed

+56
-37
lines changed

cjs/connection.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = function Connection(options = {}) {
2424

2525
const queries = Queue()
2626
, statements = new Map()
27-
, connection = { send, end, ready: false, active: false }
27+
, connection = { send, end, destroy, ready: false, active: false }
2828

2929
const socket = postgresSocket(options, {
3030
data,
@@ -62,16 +62,19 @@ module.exports = function Connection(options = {}) {
6262
}
6363

6464
function end() {
65-
return new Promise((resolve) => {
66-
ended = function() {
67-
socket.end()
68-
resolve()
69-
}
65+
clearTimeout(timer)
66+
const promise = new Promise((resolve) => {
67+
ended = () => resolve(socket.end())
7068
})
69+
70+
if (!backend.query && queries.length === 0)
71+
ended()
72+
73+
return promise
7174
}
7275

7376
function destroy() {
74-
error(errors.connection('DESTROYED'))
77+
error(errors.connection('DESTROYED', options))
7578
socket.destroy()
7679
}
7780

@@ -171,9 +174,14 @@ function postgresSocket(options, {
171174
return {
172175
ready: false,
173176
write: x => socket.write(x),
177+
destroy: () => {
178+
ended = true
179+
socket.destroy()
180+
return Promise.resolve()
181+
},
174182
end: () => {
175183
ended = true
176-
socket.end()
184+
return new Promise(r => socket.end(r))
177185
},
178186
connect
179187
}

cjs/index.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,16 @@ module.exports = function Postgres(url, options) {
289289
let c
290290
let destroy
291291

292+
if (timeout === 0)
293+
return ended = Promise.all(all.map(c => c.destroy()))
294+
292295
return ended = Promise.race([
293-
Promise.all(timeout === 0
294-
? all.map(c => c.destroy())
295-
: all.map(c => c.end())
296-
)
297-
.concat(timeout > 0
298-
? Promise.resolve(r => destroy = setTimeout(() => all.map(c => c.destroy()), timeout * 1000))
296+
all.map(c => c.end())
297+
].concat(
298+
timeout > 0
299+
? new Promise(r => destroy = setTimeout(() => (all.map(c => c.destroy()), r()), timeout * 1000))
299300
: []
300-
)
301-
])
301+
))
302302
.then(() => clearTimeout(destroy))
303303
}
304304

cjs/package.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"name": "postgres",
3-
"version": "0.0.2",
2+
"name": "peegee",
3+
"version": "0.0.3",
44
"description": "Fastest full featured PostgreSQL client for Node.js",
55
"main": "index.js",
6-
6+
"type": "commonjs",
77
"scripts": {
88
"prepublishOnly": "npm run build",
99
"build": "node cjs/transpile.js"
@@ -24,5 +24,8 @@
2424
"db",
2525
"pg",
2626
"database"
27-
]
27+
],
28+
"dependencies": {
29+
"wtfnode": "0.8.0"
30+
}
2831
}

cjs/transpile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ fs.readdirSync(path.join('lib')).forEach(name =>
1616
fs.writeFileSync(
1717
path.join('cjs', 'package.json'),
1818
fs.readFileSync('package.json', 'utf8')
19-
.replace('"type": "module",', '')
19+
.replace('"type": "module"', '"type": "commonjs"')
2020
.replace('"main": "lib/index.js"', '"main": "index.js"')
2121
)

lib/connection.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default function Connection(options = {}) {
2424

2525
const queries = Queue()
2626
, statements = new Map()
27-
, connection = { send, end, ready: false, active: false }
27+
, connection = { send, end, destroy, ready: false, active: false }
2828

2929
const socket = postgresSocket(options, {
3030
data,
@@ -62,16 +62,19 @@ export default function Connection(options = {}) {
6262
}
6363

6464
function end() {
65-
return new Promise((resolve) => {
66-
ended = function() {
67-
socket.end()
68-
resolve()
69-
}
65+
clearTimeout(timer)
66+
const promise = new Promise((resolve) => {
67+
ended = () => resolve(socket.end())
7068
})
69+
70+
if (!backend.query && queries.length === 0)
71+
ended()
72+
73+
return promise
7174
}
7275

7376
function destroy() {
74-
error(errors.connection('DESTROYED'))
77+
error(errors.connection('DESTROYED', options))
7578
socket.destroy()
7679
}
7780

@@ -171,9 +174,14 @@ function postgresSocket(options, {
171174
return {
172175
ready: false,
173176
write: x => socket.write(x),
177+
destroy: () => {
178+
ended = true
179+
socket.destroy()
180+
return Promise.resolve()
181+
},
174182
end: () => {
175183
ended = true
176-
socket.end()
184+
return new Promise(r => socket.end(r))
177185
},
178186
connect
179187
}

lib/index.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,16 @@ export default function Postgres(url, options) {
289289
let c
290290
let destroy
291291

292+
if (timeout === 0)
293+
return ended = Promise.all(all.map(c => c.destroy()))
294+
292295
return ended = Promise.race([
293-
Promise.all(timeout === 0
294-
? all.map(c => c.destroy())
295-
: all.map(c => c.end())
296-
)
297-
.concat(timeout > 0
298-
? Promise.resolve(r => destroy = setTimeout(() => all.map(c => c.destroy()), timeout * 1000))
296+
all.map(c => c.end())
297+
].concat(
298+
timeout > 0
299+
? new Promise(r => destroy = setTimeout(() => (all.map(c => c.destroy()), r()), timeout * 1000))
299300
: []
300-
)
301-
])
301+
))
302302
.then(() => clearTimeout(destroy))
303303
}
304304

0 commit comments

Comments
 (0)