1 | "use strict";
|
2 | var __assign = (this && this.__assign) || Object.assign || function(t) {
|
3 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
4 | s = arguments[i];
|
5 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
6 | t[p] = s[p];
|
7 | }
|
8 | return t;
|
9 | };
|
10 | var __rest = (this && this.__rest) || function (s, e) {
|
11 | var t = {};
|
12 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
13 | t[p] = s[p];
|
14 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
15 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
|
16 | t[p[i]] = s[p[i]];
|
17 | return t;
|
18 | };
|
19 | Object.defineProperty(exports, "__esModule", { value: true });
|
20 | var json_rpc_client_1 = require("./internal/json-rpc-client");
|
21 | var ws_rpc_client_1 = require("./internal/ws-rpc-client");
|
22 | var http_rpc_client_1 = require("./internal/http-rpc-client");
|
23 | var dual_rpc_client_1 = require("./internal/dual-rpc-client");
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function createJSONRPCClient(opts) {
|
35 | var protocols = opts.protocols, _a = opts.autoConnect, autoConnect = _a === void 0 ? true : _a, requestTimeout = opts.requestTimeout, generateRequestId = opts.generateRequestId;
|
36 | if (protocols.length === 1) {
|
37 | var _b = protocols[0], url = _b.url, otherOpts = __rest(_b, ["url"]);
|
38 | var protocol = selectProtocol(url);
|
39 | if (protocol === json_rpc_client_1.JSONRPCProtocol.HTTP) {
|
40 | return new http_rpc_client_1.HTTPRPCClient(url, { requestTimeout: requestTimeout, generateRequestId: generateRequestId });
|
41 | }
|
42 | else if (protocol === json_rpc_client_1.JSONRPCProtocol.WS) {
|
43 | return new ws_rpc_client_1.WSRPCClient(url, __assign({ autoConnect: autoConnect, requestTimeout: requestTimeout, generateRequestId: generateRequestId }, otherOpts));
|
44 | }
|
45 | }
|
46 | else if (protocols.length === 2) {
|
47 | var p1 = selectProtocol(protocols[0].url);
|
48 | var p2 = selectProtocol(protocols[1].url);
|
49 | if (p1 === json_rpc_client_1.JSONRPCProtocol.HTTP && p2 === json_rpc_client_1.JSONRPCProtocol.WS) {
|
50 | var _c = protocols[1], reconnectInterval = _c.reconnectInterval, maxReconnects = _c.maxReconnects;
|
51 | return new dual_rpc_client_1.DualRPCClient({
|
52 | httpUrl: protocols[0].url,
|
53 | wsUrl: protocols[1].url,
|
54 | autoConnect: autoConnect,
|
55 | protocol: p1,
|
56 | requestTimeout: requestTimeout,
|
57 | generateRequestId: generateRequestId,
|
58 | reconnectInterval: reconnectInterval,
|
59 | maxReconnects: maxReconnects
|
60 | });
|
61 | }
|
62 | else if (p2 === json_rpc_client_1.JSONRPCProtocol.HTTP && p1 === json_rpc_client_1.JSONRPCProtocol.WS) {
|
63 | var _d = protocols[0], reconnectInterval = _d.reconnectInterval, maxReconnects = _d.maxReconnects;
|
64 | return new dual_rpc_client_1.DualRPCClient({
|
65 | httpUrl: protocols[1].url,
|
66 | wsUrl: protocols[0].url,
|
67 | autoConnect: autoConnect,
|
68 | protocol: p1,
|
69 | requestTimeout: requestTimeout,
|
70 | generateRequestId: generateRequestId,
|
71 | reconnectInterval: reconnectInterval,
|
72 | maxReconnects: maxReconnects
|
73 | });
|
74 | }
|
75 | }
|
76 | throw new Error('Failed to create JSON-RPC client: invalid protocol configuration');
|
77 | }
|
78 | exports.createJSONRPCClient = createJSONRPCClient;
|
79 | function selectProtocol(url) {
|
80 | if (url.startsWith('http://') || url.startsWith('https://')) {
|
81 | return json_rpc_client_1.JSONRPCProtocol.HTTP;
|
82 | }
|
83 | else if (url.startsWith('ws://') || url.startsWith('wss://')) {
|
84 | return json_rpc_client_1.JSONRPCProtocol.WS;
|
85 | }
|
86 | else {
|
87 | throw new Error('Invalid URL');
|
88 | }
|
89 | }
|
90 |
|
\ | No newline at end of file |