From 62e6781965a4220432c126e84114576e369c5e30 Mon Sep 17 00:00:00 2001 From: isaac Date: Sun, 19 Sep 2021 23:56:57 -0400 Subject: [PATCH] Add 'this.protocol' attribute to factory classes - allows user to customize protocol class used for factory --- src/client/http.js | 4 ++-- src/client/index.js | 3 +++ src/client/tcp.js | 7 ++++++- src/client/ws.js | 4 ++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/client/http.js b/src/client/http.js index 5fc2d53..caa0c1a 100644 --- a/src/client/http.js +++ b/src/client/http.js @@ -41,11 +41,11 @@ class HttpClientFactory extends JsonRpcClientFactory { ...this.options.headers } }; + this.protocol = HttpClientProtocol; this.headers = this.options.headers; this.encoding = this.options.encoding; this.scheme = this.options.scheme; - - this.pcolInstance = new HttpClientProtocol( + this.pcolInstance = new this.protocol( this, this.options.version, this.options.delimiter, diff --git a/src/client/index.js b/src/client/index.js index 71d1d1e..fffb755 100644 --- a/src/client/index.js +++ b/src/client/index.js @@ -1,4 +1,5 @@ const EventEmitter = require("events"); +const JsonRpcClientProtocol = require("./protocol/base"); /** * Creates an instance of JsonRpcClientFactory. This is the base factory which @@ -18,6 +19,7 @@ class JsonRpcClientFactory extends EventEmitter { * @param {number} [options.timeout=30] Timeout for request response * @param {number} [options.connectionTimeout=5000] Timeout for connection to server * @param {number} [options.retries=2] Number of connection retry attempts + * @property {Object} protocol Instance of [JsonRpcClientProtocol]{@link JsonRpcClientProtocol} to use for managing client connections * @property {class} pcolInstance The [JsonRpcClientProtocol]{@link JsonRpcClientProtocol} instance * @property {object} timeouts Key value pairs of request IDs to `setTimeout` instance * @property {number} requestTimeout Same as `options.timeout` @@ -45,6 +47,7 @@ class JsonRpcClientFactory extends EventEmitter { ...defaults, ...(options || {}) }; + this.protocol = JsonRpcClientProtocol; this.pcolInstance = undefined; this.timeouts = {}; this.requestTimeout = this.options.timeout * 1000; diff --git a/src/client/tcp.js b/src/client/tcp.js index 2e1416a..7bbabd4 100644 --- a/src/client/tcp.js +++ b/src/client/tcp.js @@ -7,9 +7,14 @@ const TcpClientProtocol = require("./protocol/tcp"); * @extends JsonRpcClientFactory */ class TcpClientFactory extends JsonRpcClientFactory { + constructor(options) { + super(options); + this.protocol = TcpClientProtocol; + } + /** @inheritdoc */ buildProtocol() { - this.pcolInstance = new TcpClientProtocol( + this.pcolInstance = new this.protocol( this, this.options.version, this.options.delimiter diff --git a/src/client/ws.js b/src/client/ws.js index 3d4932d..ff8c290 100644 --- a/src/client/ws.js +++ b/src/client/ws.js @@ -29,13 +29,13 @@ class WsClientFactory extends JsonRpcClientFactory { ...defaults, ...(this.options || {}) }; - + this.protocol = WsClientProtocol; this.url = this.options.url; } /** @inheritdoc */ buildProtocol() { - this.pcolInstance = new WsClientProtocol( + this.pcolInstance = new this.protocol( this, this.options.version, this.options.delimiter