@solana/web3.js 1.92.3 → 1.93.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.iife.js CHANGED
@@ -16193,8 +16193,6 @@ var solanaWeb3 = (function (exports) {
16193
16193
 
16194
16194
  var fetchImpl = globalThis.fetch;
16195
16195
 
16196
- var client = {};
16197
-
16198
16196
  var eventemitter3 = {exports: {}};
16199
16197
 
16200
16198
  (function (module) {
@@ -16536,430 +16534,368 @@ var solanaWeb3 = (function (exports) {
16536
16534
  } (eventemitter3));
16537
16535
 
16538
16536
  var eventemitter3Exports = eventemitter3.exports;
16539
-
16540
- var utils = {};
16541
-
16542
- Object.defineProperty(utils, "__esModule", { value: true });
16543
- utils.createError = utils.DefaultDataPack = void 0;
16544
- const errors = new Map([
16545
- [-32000, "Event not provided"],
16546
- [-32600, "Invalid Request"],
16547
- [-32601, "Method not found"],
16548
- [-32602, "Invalid params"],
16549
- [-32603, "Internal error"],
16550
- [-32604, "Params not found"],
16551
- [-32605, "Method forbidden"],
16552
- [-32606, "Event forbidden"],
16553
- [-32700, "Parse error"]
16554
- ]);
16555
- class DefaultDataPack {
16556
- encode(value) {
16557
- return JSON.stringify(value);
16558
- }
16559
- decode(value) {
16560
- return JSON.parse(value);
16561
- }
16562
- }
16563
- utils.DefaultDataPack = DefaultDataPack;
16564
- /**
16565
- * Creates a JSON-RPC 2.0-compliant error.
16566
- * @param {Number} code - error code
16567
- * @param {String} details - error details
16568
- * @return {Object}
16569
- */
16570
- function createError(code, details) {
16571
- const error = {
16572
- code: code,
16573
- message: errors.get(code) || "Internal Server Error"
16537
+ var EventEmitter = /*@__PURE__*/getDefaultExportFromCjs(eventemitter3Exports);
16538
+
16539
+ // node_modules/esbuild-plugin-polyfill-node/polyfills/buffer.js
16540
+ var WebSocketBrowserImpl = class extends EventEmitter {
16541
+ socket;
16542
+ /** Instantiate a WebSocket class
16543
+ * @constructor
16544
+ * @param {String} address - url to a websocket server
16545
+ * @param {(Object)} options - websocket options
16546
+ * @param {(String|Array)} protocols - a list of protocols
16547
+ * @return {WebSocketBrowserImpl} - returns a WebSocket instance
16548
+ */
16549
+ constructor(address, options, protocols) {
16550
+ super();
16551
+ this.socket = new window.WebSocket(address, protocols);
16552
+ this.socket.onopen = () => this.emit("open");
16553
+ this.socket.onmessage = (event) => this.emit("message", event.data);
16554
+ this.socket.onerror = (error) => this.emit("error", error);
16555
+ this.socket.onclose = (event) => {
16556
+ this.emit("close", event.code, event.reason);
16574
16557
  };
16575
- if (details)
16576
- error["data"] = details;
16577
- return error;
16578
- }
16579
- utils.createError = createError;
16580
-
16581
- /**
16582
- * "Client" wraps "ws" or a browser-implemented "WebSocket" library
16583
- * according to the environment providing JSON RPC 2.0 support on top.
16584
- * @module Client
16585
- */
16586
- Object.defineProperty(client, "__esModule", { value: true });
16587
- // @ts-ignore
16588
- const eventemitter3_1$1 = eventemitter3Exports;
16589
- const utils_cjs_1 = utils;
16590
- class CommonClient extends eventemitter3_1$1.EventEmitter {
16591
- address;
16592
- rpc_id;
16593
- queue;
16594
- options;
16595
- autoconnect;
16596
- ready;
16597
- reconnect;
16598
- reconnect_timer_id;
16599
- reconnect_interval;
16600
- max_reconnects;
16601
- rest_options;
16602
- current_reconnects;
16603
- generate_request_id;
16604
- socket;
16605
- webSocketFactory;
16606
- dataPack;
16607
- /**
16608
- * Instantiate a Client class.
16609
- * @constructor
16610
- * @param {webSocketFactory} webSocketFactory - factory method for WebSocket
16611
- * @param {String} address - url to a websocket server
16612
- * @param {Object} options - ws options object with reconnect parameters
16613
- * @param {Function} generate_request_id - custom generation request Id
16614
- * @param {DataPack} dataPack - data pack contains encoder and decoder
16615
- * @return {CommonClient}
16616
- */
16617
- constructor(webSocketFactory, address = "ws://localhost:8080", { autoconnect = true, reconnect = true, reconnect_interval = 1000, max_reconnects = 5, ...rest_options } = {}, generate_request_id, dataPack) {
16618
- super();
16619
- this.webSocketFactory = webSocketFactory;
16620
- this.queue = {};
16621
- this.rpc_id = 0;
16622
- this.address = address;
16623
- this.autoconnect = autoconnect;
16624
- this.ready = false;
16625
- this.reconnect = reconnect;
16626
- this.reconnect_timer_id = undefined;
16627
- this.reconnect_interval = reconnect_interval;
16628
- this.max_reconnects = max_reconnects;
16629
- this.rest_options = rest_options;
16630
- this.current_reconnects = 0;
16631
- this.generate_request_id = generate_request_id || (() => ++this.rpc_id);
16632
- if (!dataPack)
16633
- this.dataPack = new utils_cjs_1.DefaultDataPack();
16634
- else
16635
- this.dataPack = dataPack;
16636
- if (this.autoconnect)
16637
- this._connect(this.address, {
16638
- autoconnect: this.autoconnect,
16639
- reconnect: this.reconnect,
16640
- reconnect_interval: this.reconnect_interval,
16641
- max_reconnects: this.max_reconnects,
16642
- ...this.rest_options
16643
- });
16644
- }
16645
- /**
16646
- * Connects to a defined server if not connected already.
16647
- * @method
16648
- * @return {Undefined}
16649
- */
16650
- connect() {
16651
- if (this.socket)
16652
- return;
16653
- this._connect(this.address, {
16654
- autoconnect: this.autoconnect,
16655
- reconnect: this.reconnect,
16656
- reconnect_interval: this.reconnect_interval,
16657
- max_reconnects: this.max_reconnects,
16658
- ...this.rest_options
16659
- });
16660
- }
16661
- /**
16662
- * Calls a registered RPC method on server.
16663
- * @method
16664
- * @param {String} method - RPC method name
16665
- * @param {Object|Array} params - optional method parameters
16666
- * @param {Number} timeout - RPC reply timeout value
16667
- * @param {Object} ws_opts - options passed to ws
16668
- * @return {Promise}
16669
- */
16670
- call(method, params, timeout, ws_opts) {
16671
- if (!ws_opts && "object" === typeof timeout) {
16672
- ws_opts = timeout;
16673
- timeout = null;
16674
- }
16675
- return new Promise((resolve, reject) => {
16676
- if (!this.ready)
16677
- return reject(new Error("socket not ready"));
16678
- const rpc_id = this.generate_request_id(method, params);
16679
- const message = {
16680
- jsonrpc: "2.0",
16681
- method: method,
16682
- params: params || undefined,
16683
- id: rpc_id
16684
- };
16685
- this.socket.send(this.dataPack.encode(message), ws_opts, (error) => {
16686
- if (error)
16687
- return reject(error);
16688
- this.queue[rpc_id] = { promise: [resolve, reject] };
16689
- if (timeout) {
16690
- this.queue[rpc_id].timeout = setTimeout(() => {
16691
- delete this.queue[rpc_id];
16692
- reject(new Error("reply timeout"));
16693
- }, timeout);
16694
- }
16695
- });
16696
- });
16697
- }
16698
- /**
16699
- * Logins with the other side of the connection.
16700
- * @method
16701
- * @param {Object} params - Login credentials object
16702
- * @return {Promise}
16703
- */
16704
- async login(params) {
16705
- const resp = await this.call("rpc.login", params);
16706
- if (!resp)
16707
- throw new Error("authentication failed");
16708
- return resp;
16709
- }
16710
- /**
16711
- * Fetches a list of client's methods registered on server.
16712
- * @method
16713
- * @return {Array}
16714
- */
16715
- async listMethods() {
16716
- return await this.call("__listMethods");
16717
- }
16718
- /**
16719
- * Sends a JSON-RPC 2.0 notification to server.
16720
- * @method
16721
- * @param {String} method - RPC method name
16722
- * @param {Object} params - optional method parameters
16723
- * @return {Promise}
16724
- */
16725
- notify(method, params) {
16726
- return new Promise((resolve, reject) => {
16727
- if (!this.ready)
16728
- return reject(new Error("socket not ready"));
16729
- const message = {
16730
- jsonrpc: "2.0",
16731
- method: method,
16732
- params
16733
- };
16734
- this.socket.send(this.dataPack.encode(message), (error) => {
16735
- if (error)
16736
- return reject(error);
16737
- resolve();
16738
- });
16739
- });
16740
- }
16741
- /**
16742
- * Subscribes for a defined event.
16743
- * @method
16744
- * @param {String|Array} event - event name
16745
- * @return {Undefined}
16746
- * @throws {Error}
16747
- */
16748
- async subscribe(event) {
16749
- if (typeof event === "string")
16750
- event = [event];
16751
- const result = await this.call("rpc.on", event);
16752
- if (typeof event === "string" && result[event] !== "ok")
16753
- throw new Error("Failed subscribing to an event '" + event + "' with: " + result[event]);
16754
- return result;
16755
- }
16756
- /**
16757
- * Unsubscribes from a defined event.
16758
- * @method
16759
- * @param {String|Array} event - event name
16760
- * @return {Undefined}
16761
- * @throws {Error}
16762
- */
16763
- async unsubscribe(event) {
16764
- if (typeof event === "string")
16765
- event = [event];
16766
- const result = await this.call("rpc.off", event);
16767
- if (typeof event === "string" && result[event] !== "ok")
16768
- throw new Error("Failed unsubscribing from an event with: " + result);
16769
- return result;
16770
- }
16771
- /**
16772
- * Closes a WebSocket connection gracefully.
16773
- * @method
16774
- * @param {Number} code - socket close code
16775
- * @param {String} data - optional data to be sent before closing
16776
- * @return {Undefined}
16777
- */
16778
- close(code, data) {
16779
- this.socket.close(code || 1000, data);
16780
- }
16781
- /**
16782
- * Enable / disable automatic reconnection.
16783
- * @method
16784
- * @param {Boolean} reconnect - enable / disable reconnection
16785
- * @return {Undefined}
16786
- */
16787
- setAutoReconnect(reconnect) {
16788
- this.reconnect = reconnect;
16789
- }
16790
- /**
16791
- * Set the interval between reconnection attempts.
16792
- * @method
16793
- * @param {Number} interval - reconnection interval in milliseconds
16794
- * @return {Undefined}
16795
- */
16796
- setReconnectInterval(interval) {
16797
- this.reconnect_interval = interval;
16798
- }
16799
- /**
16800
- * Set the maximum number of reconnection attempts.
16801
- * @method
16802
- * @param {Number} max_reconnects - maximum reconnection attempts
16803
- * @return {Undefined}
16804
- */
16805
- setMaxReconnects(max_reconnects) {
16806
- this.max_reconnects = max_reconnects;
16807
- }
16808
- /**
16809
- * Connection/Message handler.
16810
- * @method
16811
- * @private
16812
- * @param {String} address - WebSocket API address
16813
- * @param {Object} options - ws options object
16814
- * @return {Undefined}
16815
- */
16816
- _connect(address, options) {
16817
- clearTimeout(this.reconnect_timer_id);
16818
- this.socket = this.webSocketFactory(address, options);
16819
- this.socket.addEventListener("open", () => {
16820
- this.ready = true;
16821
- this.emit("open");
16822
- this.current_reconnects = 0;
16823
- });
16824
- this.socket.addEventListener("message", ({ data: message }) => {
16825
- if (message instanceof ArrayBuffer)
16826
- message = Buffer.from(message).toString();
16827
- try {
16828
- message = this.dataPack.decode(message);
16829
- }
16830
- catch (error) {
16831
- return;
16832
- }
16833
- // check if any listeners are attached and forward event
16834
- if (message.notification && this.listeners(message.notification).length) {
16835
- if (!Object.keys(message.params).length)
16836
- return this.emit(message.notification);
16837
- const args = [message.notification];
16838
- if (message.params.constructor === Object)
16839
- args.push(message.params);
16840
- else
16841
- // using for-loop instead of unshift/spread because performance is better
16842
- for (let i = 0; i < message.params.length; i++)
16843
- args.push(message.params[i]);
16844
- // run as microtask so that pending queue messages are resolved first
16845
- // eslint-disable-next-line prefer-spread
16846
- return Promise.resolve().then(() => { this.emit.apply(this, args); });
16847
- }
16848
- if (!this.queue[message.id]) {
16849
- // general JSON RPC 2.0 events
16850
- if (message.method) {
16851
- // run as microtask so that pending queue messages are resolved first
16852
- return Promise.resolve().then(() => {
16853
- this.emit(message.method, message?.params);
16854
- });
16855
- }
16856
- return;
16857
- }
16858
- // reject early since server's response is invalid
16859
- if ("error" in message === "result" in message)
16860
- this.queue[message.id].promise[1](new Error("Server response malformed. Response must include either \"result\"" +
16861
- " or \"error\", but not both."));
16862
- if (this.queue[message.id].timeout)
16863
- clearTimeout(this.queue[message.id].timeout);
16864
- if (message.error)
16865
- this.queue[message.id].promise[1](message.error);
16866
- else
16867
- this.queue[message.id].promise[0](message.result);
16868
- delete this.queue[message.id];
16869
- });
16870
- this.socket.addEventListener("error", (error) => this.emit("error", error));
16871
- this.socket.addEventListener("close", ({ code, reason }) => {
16872
- if (this.ready) // Delay close event until internal state is updated
16873
- setTimeout(() => this.emit("close", code, reason), 0);
16874
- this.ready = false;
16875
- this.socket = undefined;
16876
- if (code === 1000)
16877
- return;
16878
- this.current_reconnects++;
16879
- if (this.reconnect && ((this.max_reconnects > this.current_reconnects) ||
16880
- this.max_reconnects === 0))
16881
- this.reconnect_timer_id = setTimeout(() => this._connect(address, options), this.reconnect_interval);
16882
- });
16558
+ }
16559
+ /**
16560
+ * Sends data through a websocket connection
16561
+ * @method
16562
+ * @param {(String|Object)} data - data to be sent via websocket
16563
+ * @param {Object} optionsOrCallback - ws options
16564
+ * @param {Function} callback - a callback called once the data is sent
16565
+ * @return {Undefined}
16566
+ */
16567
+ send(data, optionsOrCallback, callback) {
16568
+ const cb = callback || optionsOrCallback;
16569
+ try {
16570
+ this.socket.send(data);
16571
+ cb();
16572
+ } catch (error) {
16573
+ cb(error);
16883
16574
  }
16575
+ }
16576
+ /**
16577
+ * Closes an underlying socket
16578
+ * @method
16579
+ * @param {Number} code - status code explaining why the connection is being closed
16580
+ * @param {String} reason - a description why the connection is closing
16581
+ * @return {Undefined}
16582
+ * @throws {Error}
16583
+ */
16584
+ close(code, reason) {
16585
+ this.socket.close(code, reason);
16586
+ }
16587
+ addEventListener(type, listener, options) {
16588
+ this.socket.addEventListener(type, listener, options);
16589
+ }
16590
+ };
16591
+ function WebSocket(address, options) {
16592
+ return new WebSocketBrowserImpl(address, options);
16884
16593
  }
16885
- var _default$1 = client.default = CommonClient;
16886
16594
 
16887
- var websocket_browser = {};
16595
+ // src/lib/utils.ts
16596
+ var DefaultDataPack = class {
16597
+ encode(value) {
16598
+ return JSON.stringify(value);
16599
+ }
16600
+ decode(value) {
16601
+ return JSON.parse(value);
16602
+ }
16603
+ };
16888
16604
 
16889
- /**
16890
- * WebSocket implements a browser-side WebSocket specification.
16891
- * @module Client
16892
- */
16893
- Object.defineProperty(websocket_browser, "__esModule", { value: true });
16894
- const eventemitter3_1 = eventemitter3Exports;
16895
- class WebSocketBrowserImpl extends eventemitter3_1.EventEmitter {
16896
- socket;
16897
- /** Instantiate a WebSocket class
16898
- * @constructor
16899
- * @param {String} address - url to a websocket server
16900
- * @param {(Object)} options - websocket options
16901
- * @param {(String|Array)} protocols - a list of protocols
16902
- * @return {WebSocketBrowserImpl} - returns a WebSocket instance
16903
- */
16904
- constructor(address, options, protocols) {
16905
- super();
16906
- this.socket = new window.WebSocket(address, protocols);
16907
- this.socket.onopen = () => this.emit("open");
16908
- this.socket.onmessage = (event) => this.emit("message", event.data);
16909
- this.socket.onerror = (error) => this.emit("error", error);
16910
- this.socket.onclose = (event) => {
16911
- this.emit("close", event.code, event.reason);
16912
- };
16605
+ // src/lib/client.ts
16606
+ var CommonClient = class extends EventEmitter {
16607
+ address;
16608
+ rpc_id;
16609
+ queue;
16610
+ options;
16611
+ autoconnect;
16612
+ ready;
16613
+ reconnect;
16614
+ reconnect_timer_id;
16615
+ reconnect_interval;
16616
+ max_reconnects;
16617
+ rest_options;
16618
+ current_reconnects;
16619
+ generate_request_id;
16620
+ socket;
16621
+ webSocketFactory;
16622
+ dataPack;
16623
+ /**
16624
+ * Instantiate a Client class.
16625
+ * @constructor
16626
+ * @param {webSocketFactory} webSocketFactory - factory method for WebSocket
16627
+ * @param {String} address - url to a websocket server
16628
+ * @param {Object} options - ws options object with reconnect parameters
16629
+ * @param {Function} generate_request_id - custom generation request Id
16630
+ * @param {DataPack} dataPack - data pack contains encoder and decoder
16631
+ * @return {CommonClient}
16632
+ */
16633
+ constructor(webSocketFactory, address = "ws://localhost:8080", {
16634
+ autoconnect = true,
16635
+ reconnect = true,
16636
+ reconnect_interval = 1e3,
16637
+ max_reconnects = 5,
16638
+ ...rest_options
16639
+ } = {}, generate_request_id, dataPack) {
16640
+ super();
16641
+ this.webSocketFactory = webSocketFactory;
16642
+ this.queue = {};
16643
+ this.rpc_id = 0;
16644
+ this.address = address;
16645
+ this.autoconnect = autoconnect;
16646
+ this.ready = false;
16647
+ this.reconnect = reconnect;
16648
+ this.reconnect_timer_id = void 0;
16649
+ this.reconnect_interval = reconnect_interval;
16650
+ this.max_reconnects = max_reconnects;
16651
+ this.rest_options = rest_options;
16652
+ this.current_reconnects = 0;
16653
+ this.generate_request_id = generate_request_id || (() => ++this.rpc_id);
16654
+ if (!dataPack) this.dataPack = new DefaultDataPack();
16655
+ else this.dataPack = dataPack;
16656
+ if (this.autoconnect)
16657
+ this._connect(this.address, {
16658
+ autoconnect: this.autoconnect,
16659
+ reconnect: this.reconnect,
16660
+ reconnect_interval: this.reconnect_interval,
16661
+ max_reconnects: this.max_reconnects,
16662
+ ...this.rest_options
16663
+ });
16664
+ }
16665
+ /**
16666
+ * Connects to a defined server if not connected already.
16667
+ * @method
16668
+ * @return {Undefined}
16669
+ */
16670
+ connect() {
16671
+ if (this.socket) return;
16672
+ this._connect(this.address, {
16673
+ autoconnect: this.autoconnect,
16674
+ reconnect: this.reconnect,
16675
+ reconnect_interval: this.reconnect_interval,
16676
+ max_reconnects: this.max_reconnects,
16677
+ ...this.rest_options
16678
+ });
16679
+ }
16680
+ /**
16681
+ * Calls a registered RPC method on server.
16682
+ * @method
16683
+ * @param {String} method - RPC method name
16684
+ * @param {Object|Array} params - optional method parameters
16685
+ * @param {Number} timeout - RPC reply timeout value
16686
+ * @param {Object} ws_opts - options passed to ws
16687
+ * @return {Promise}
16688
+ */
16689
+ call(method, params, timeout, ws_opts) {
16690
+ if (!ws_opts && "object" === typeof timeout) {
16691
+ ws_opts = timeout;
16692
+ timeout = null;
16913
16693
  }
16914
- /**
16915
- * Sends data through a websocket connection
16916
- * @method
16917
- * @param {(String|Object)} data - data to be sent via websocket
16918
- * @param {Object} optionsOrCallback - ws options
16919
- * @param {Function} callback - a callback called once the data is sent
16920
- * @return {Undefined}
16921
- */
16922
- send(data, optionsOrCallback, callback) {
16923
- const cb = callback || optionsOrCallback;
16924
- try {
16925
- this.socket.send(data);
16926
- cb();
16694
+ return new Promise((resolve, reject) => {
16695
+ if (!this.ready) return reject(new Error("socket not ready"));
16696
+ const rpc_id = this.generate_request_id(method, params);
16697
+ const message = {
16698
+ jsonrpc: "2.0",
16699
+ method,
16700
+ params: params || void 0,
16701
+ id: rpc_id
16702
+ };
16703
+ this.socket.send(this.dataPack.encode(message), ws_opts, (error) => {
16704
+ if (error) return reject(error);
16705
+ this.queue[rpc_id] = { promise: [resolve, reject] };
16706
+ if (timeout) {
16707
+ this.queue[rpc_id].timeout = setTimeout(() => {
16708
+ delete this.queue[rpc_id];
16709
+ reject(new Error("reply timeout"));
16710
+ }, timeout);
16927
16711
  }
16928
- catch (error) {
16929
- cb(error);
16712
+ });
16713
+ });
16714
+ }
16715
+ /**
16716
+ * Logins with the other side of the connection.
16717
+ * @method
16718
+ * @param {Object} params - Login credentials object
16719
+ * @return {Promise}
16720
+ */
16721
+ async login(params) {
16722
+ const resp = await this.call("rpc.login", params);
16723
+ if (!resp) throw new Error("authentication failed");
16724
+ return resp;
16725
+ }
16726
+ /**
16727
+ * Fetches a list of client's methods registered on server.
16728
+ * @method
16729
+ * @return {Array}
16730
+ */
16731
+ async listMethods() {
16732
+ return await this.call("__listMethods");
16733
+ }
16734
+ /**
16735
+ * Sends a JSON-RPC 2.0 notification to server.
16736
+ * @method
16737
+ * @param {String} method - RPC method name
16738
+ * @param {Object} params - optional method parameters
16739
+ * @return {Promise}
16740
+ */
16741
+ notify(method, params) {
16742
+ return new Promise((resolve, reject) => {
16743
+ if (!this.ready) return reject(new Error("socket not ready"));
16744
+ const message = {
16745
+ jsonrpc: "2.0",
16746
+ method,
16747
+ params
16748
+ };
16749
+ this.socket.send(this.dataPack.encode(message), (error) => {
16750
+ if (error) return reject(error);
16751
+ resolve();
16752
+ });
16753
+ });
16754
+ }
16755
+ /**
16756
+ * Subscribes for a defined event.
16757
+ * @method
16758
+ * @param {String|Array} event - event name
16759
+ * @return {Undefined}
16760
+ * @throws {Error}
16761
+ */
16762
+ async subscribe(event) {
16763
+ if (typeof event === "string") event = [event];
16764
+ const result = await this.call("rpc.on", event);
16765
+ if (typeof event === "string" && result[event] !== "ok")
16766
+ throw new Error(
16767
+ "Failed subscribing to an event '" + event + "' with: " + result[event]
16768
+ );
16769
+ return result;
16770
+ }
16771
+ /**
16772
+ * Unsubscribes from a defined event.
16773
+ * @method
16774
+ * @param {String|Array} event - event name
16775
+ * @return {Undefined}
16776
+ * @throws {Error}
16777
+ */
16778
+ async unsubscribe(event) {
16779
+ if (typeof event === "string") event = [event];
16780
+ const result = await this.call("rpc.off", event);
16781
+ if (typeof event === "string" && result[event] !== "ok")
16782
+ throw new Error("Failed unsubscribing from an event with: " + result);
16783
+ return result;
16784
+ }
16785
+ /**
16786
+ * Closes a WebSocket connection gracefully.
16787
+ * @method
16788
+ * @param {Number} code - socket close code
16789
+ * @param {String} data - optional data to be sent before closing
16790
+ * @return {Undefined}
16791
+ */
16792
+ close(code, data) {
16793
+ this.socket.close(code || 1e3, data);
16794
+ }
16795
+ /**
16796
+ * Enable / disable automatic reconnection.
16797
+ * @method
16798
+ * @param {Boolean} reconnect - enable / disable reconnection
16799
+ * @return {Undefined}
16800
+ */
16801
+ setAutoReconnect(reconnect) {
16802
+ this.reconnect = reconnect;
16803
+ }
16804
+ /**
16805
+ * Set the interval between reconnection attempts.
16806
+ * @method
16807
+ * @param {Number} interval - reconnection interval in milliseconds
16808
+ * @return {Undefined}
16809
+ */
16810
+ setReconnectInterval(interval) {
16811
+ this.reconnect_interval = interval;
16812
+ }
16813
+ /**
16814
+ * Set the maximum number of reconnection attempts.
16815
+ * @method
16816
+ * @param {Number} max_reconnects - maximum reconnection attempts
16817
+ * @return {Undefined}
16818
+ */
16819
+ setMaxReconnects(max_reconnects) {
16820
+ this.max_reconnects = max_reconnects;
16821
+ }
16822
+ /**
16823
+ * Connection/Message handler.
16824
+ * @method
16825
+ * @private
16826
+ * @param {String} address - WebSocket API address
16827
+ * @param {Object} options - ws options object
16828
+ * @return {Undefined}
16829
+ */
16830
+ _connect(address, options) {
16831
+ clearTimeout(this.reconnect_timer_id);
16832
+ this.socket = this.webSocketFactory(address, options);
16833
+ this.socket.addEventListener("open", () => {
16834
+ this.ready = true;
16835
+ this.emit("open");
16836
+ this.current_reconnects = 0;
16837
+ });
16838
+ this.socket.addEventListener("message", ({ data: message }) => {
16839
+ if (message instanceof ArrayBuffer)
16840
+ message = buffer.Buffer.from(message).toString();
16841
+ try {
16842
+ message = this.dataPack.decode(message);
16843
+ } catch (error) {
16844
+ return;
16845
+ }
16846
+ if (message.notification && this.listeners(message.notification).length) {
16847
+ if (!Object.keys(message.params).length)
16848
+ return this.emit(message.notification);
16849
+ const args = [message.notification];
16850
+ if (message.params.constructor === Object) args.push(message.params);
16851
+ else
16852
+ for (let i = 0; i < message.params.length; i++)
16853
+ args.push(message.params[i]);
16854
+ return Promise.resolve().then(() => {
16855
+ this.emit.apply(this, args);
16856
+ });
16857
+ }
16858
+ if (!this.queue[message.id]) {
16859
+ if (message.method) {
16860
+ return Promise.resolve().then(() => {
16861
+ this.emit(message.method, message?.params);
16862
+ });
16930
16863
  }
16931
- }
16932
- /**
16933
- * Closes an underlying socket
16934
- * @method
16935
- * @param {Number} code - status code explaining why the connection is being closed
16936
- * @param {String} reason - a description why the connection is closing
16937
- * @return {Undefined}
16938
- * @throws {Error}
16939
- */
16940
- close(code, reason) {
16941
- this.socket.close(code, reason);
16942
- }
16943
- addEventListener(type, listener, options) {
16944
- this.socket.addEventListener(type, listener, options);
16945
- }
16946
- }
16947
- /**
16948
- * factory method for common WebSocket instance
16949
- * @method
16950
- * @param {String} address - url to a websocket server
16951
- * @param {(Object)} options - websocket options
16952
- * @return {Undefined}
16953
- */
16954
- function default_1(address, options) {
16955
- return new WebSocketBrowserImpl(address, options);
16956
- }
16957
- var _default = websocket_browser.default = default_1;
16864
+ return;
16865
+ }
16866
+ if ("error" in message === "result" in message)
16867
+ this.queue[message.id].promise[1](
16868
+ new Error(
16869
+ 'Server response malformed. Response must include either "result" or "error", but not both.'
16870
+ )
16871
+ );
16872
+ if (this.queue[message.id].timeout)
16873
+ clearTimeout(this.queue[message.id].timeout);
16874
+ if (message.error) this.queue[message.id].promise[1](message.error);
16875
+ else this.queue[message.id].promise[0](message.result);
16876
+ delete this.queue[message.id];
16877
+ });
16878
+ this.socket.addEventListener("error", (error) => this.emit("error", error));
16879
+ this.socket.addEventListener("close", ({ code, reason }) => {
16880
+ if (this.ready)
16881
+ setTimeout(() => this.emit("close", code, reason), 0);
16882
+ this.ready = false;
16883
+ this.socket = void 0;
16884
+ if (code === 1e3) return;
16885
+ this.current_reconnects++;
16886
+ if (this.reconnect && (this.max_reconnects > this.current_reconnects || this.max_reconnects === 0))
16887
+ this.reconnect_timer_id = setTimeout(
16888
+ () => this._connect(address, options),
16889
+ this.reconnect_interval
16890
+ );
16891
+ });
16892
+ }
16893
+ };
16958
16894
 
16959
- class RpcWebSocketClient extends _default$1 {
16895
+ class RpcWebSocketClient extends CommonClient {
16960
16896
  constructor(address, options, generate_request_id) {
16961
16897
  const webSocketFactory = url => {
16962
- const rpc = _default(url, {
16898
+ const rpc = WebSocket(url, {
16963
16899
  autoconnect: true,
16964
16900
  max_reconnects: 5,
16965
16901
  reconnect: true,