From cec3e6c96e10166f411b792477a5640ac04faebb Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Fri, 3 Sep 2021 11:00:06 +0200 Subject: [PATCH] add socket option everywhere and streamline internals --- lib/io.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/io.js b/lib/io.js index faba7d8..b5d93ce 100644 --- a/lib/io.js +++ b/lib/io.js @@ -237,25 +237,26 @@ class Request { reply (value, opts = {}) { const socket = opts.socket || this.socket const to = opts.to || this.from - this._sendReply(0, value || null, opts.token !== false, this.target !== null && opts.closerNodes !== false, to, socket) + this._sendReply(0, value || null, opts.token !== false, opts.closerNodes !== false, to, socket) } error (code, opts = {}) { const socket = opts.socket || this.socket const to = opts.to || this.from - this._sendReply(code, null, opts.token === true, this.target !== null && opts.closerNodes !== false, to, socket) + this._sendReply(code, null, opts.token === true, opts.closerNodes !== false, to, socket) } - relay (value, to) { - const buffer = this._encodeRequest(null, value) - this.socket.send(buffer, 0, buffer.byteLength, to.port, to.host) + relay (value, to, opts) { + const socket = (opts && opts.socket) || this.socket + const buffer = this._encodeRequest(null, value, socket) + socket.send(buffer, 0, buffer.byteLength, to.port, to.host) } send (force = false) { if (this.destroyed) return if (this.socket === null) return - if (this._buffer === null) this._buffer = this._encodeRequest(this.token, this.value) + if (this._buffer === null) this._buffer = this._encodeRequest(this.token, this.value, this.socket) if (!force && this._io.congestion.isFull()) { this._io._pending.push(this) @@ -295,7 +296,7 @@ class Request { if (socket === null || this.destroyed) return const id = this._io.ephemeral === false && socket === this._io.serverSocket - const closerNodes = hasCloserNodes ? this._io.table.closest(this.target) : EMPTY_ARRAY + const closerNodes = (this.target !== null && hasCloserNodes) ? this._io.table.closest(this.target) : EMPTY_ARRAY const state = { start: 0, end: 1 + 1 + 6 + 2, buffer: null } // (type | version) + flags + to + tid if (id) state.end += 32 @@ -320,8 +321,8 @@ class Request { socket.send(state.buffer, 0, state.buffer.byteLength, from.port, from.host) } - _encodeRequest (token, value) { - const id = this._io.ephemeral === false && this.socket === this._io.serverSocket + _encodeRequest (token, value, socket) { + const id = this._io.ephemeral === false && socket === this._io.serverSocket const state = { start: 0, end: 1 + 1 + 6 + 2, buffer: null } // (type | version) + flags + to + tid if (id) state.end += 32