summaryrefslogtreecommitdiffstats
path: root/client.js
diff options
context:
space:
mode:
authorJannis R <mail@jannisr.de>2020-10-16 14:49:33 +0200
committerJannis R <mail@jannisr.de>2020-10-16 14:54:06 +0200
commit0fdbefbb86bcf959875358b732682af65eddacc2 (patch)
treed90331d804f81ac346ab5470463bee4dd9dd91b4 /client.js
parent756519148e19a310f3f30328cbd5e09d99db2190 (diff)
client & server: improved error handling & timeouts 🐛
Diffstat (limited to 'client.js')
-rw-r--r--client.js25
1 files changed, 16 insertions, 9 deletions
diff --git a/client.js b/client.js
index 4614921..2748da1 100644
--- a/client.js
+++ b/client.js
@@ -3,6 +3,7 @@
const debug = require('debug')('gemini:client')
const {parse: parseUrl} = require('url')
const pem = require('pem')
+const {pipeline: pipe} = require('stream')
const connect = require('./connect')
const createParser = require('./lib/response-parser')
const {
@@ -33,20 +34,26 @@ const _request = (pathOrUrl, opt, cb) => {
}
const res = createParser()
- socket.pipe(res)
- socket.once('error', (err) => {
- socket.unpipe(res)
- res.destroy(err)
- })
+ pipe(
+ socket,
+ res,
+ (err) => {
+ if (err) debug('error receiving response', err)
+ // Control over the socket has been given to the caller
+ // already, so we swallow the error here.
+ if (!timeout) return;
+ if (!err) cb(new Error('socket closed while waiting for header'))
+ },
+ )
- const close = () => {
- socket.destroy()
- res.destroy()
+ const reportTimeout = () => {
+ socket.destroy(new Error('timeout waiting for header'))
}
- let timeout = setTimeout(close, 20 * 1000)
+ let timeout = setTimeout(reportTimeout, 20 * 1000)
res.once('header', (header) => {
clearTimeout(timeout)
+ timeout = null
debug('received header', header)
// prepare res