aboutsummaryrefslogtreecommitdiff
path: root/src/lib/server.js
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2021-03-07 19:41:43 +0300
committerEvgeny Zinoviev <me@ch1p.io>2021-03-07 19:41:43 +0300
commitdb7e1be9b58ba92556d579cd4b814ae083602bc9 (patch)
tree36b8a67e7f0c87286616b61d6794ff6e58726f8e /src/lib/server.js
parente19982e9736cebb3f52a146fbc5f0579b70827e9 (diff)
jobctl
Diffstat (limited to 'src/lib/server.js')
-rw-r--r--src/lib/server.js30
1 files changed, 25 insertions, 5 deletions
diff --git a/src/lib/server.js b/src/lib/server.js
index 618ca8c..051b8be 100644
--- a/src/lib/server.js
+++ b/src/lib/server.js
@@ -285,12 +285,19 @@ class Connection extends EventEmitter {
*/
this._requestPromises = {}
+ /**
+ * @type {Promise}
+ * @private
+ */
+ this._connectPromise = null
+
this._setLogger()
}
/**
* @param {string} host
* @param {number} port
+ * @return {Promise}
*/
connect(host, port) {
if (this.socket !== null)
@@ -298,14 +305,18 @@ class Connection extends EventEmitter {
this._isOutgoing = true
+ this.logger.trace(`Connecting to ${host}:${port}`)
+
this.socket = new net.Socket()
- this.socket.connect({host, port})
+ this.socket.connect(port, host)
this.remoteAddress = host
this.remotePort = port
this._setLogger()
this._setSocketEvents()
+
+ return this._connectPromise = createCallablePromise()
}
/**
@@ -616,14 +627,19 @@ class Connection extends EventEmitter {
}
for (const no in this._requestPromises) {
- this._requestPromises[no].reject(new Error('socket is closed'))
+ this._requestPromises[no].reject(new Error('Socket is closed'))
}
this._requestPromises = {}
}
onConnect = () => {
- this.logger.debug('connection established')
+ if (this._connectPromise) {
+ this._connectPromise.resolve()
+ this._connectPromise = null
+ }
+
+ this.logger.debug('Connection established.')
this.emit('connect')
}
@@ -642,12 +658,16 @@ class Connection extends EventEmitter {
onClose = (hadError) => {
this._handleClose()
- this.logger.debug(`socket closed` + (hadError ? ` with error` : ''))
+ this.logger.debug(`Socket closed` + (hadError ? ` with error` : ''))
}
onError = (error) => {
+ if (this._connectPromise) {
+ this._connectPromise.reject(error)
+ this._connectPromise = null
+ }
this._handleClose()
- this.logger.warn(`socket error:`, error)
+ this.logger.warn(`Socket error:`, error)
}
}