@chainflip/rpc 2.0.7 → 2.1.0-beta.0
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/dist/Client.cjs +88 -91
- package/dist/Client.d.ts +30 -33
- package/dist/Client.mjs +87 -90
- package/dist/HttpClient.cjs +33 -32
- package/dist/HttpClient.d.ts +5 -10
- package/dist/HttpClient.mjs +32 -31
- package/dist/WsClient.cjs +119 -124
- package/dist/WsClient.d.ts +22 -24
- package/dist/WsClient.mjs +117 -122
- package/dist/_virtual/rolldown_runtime.cjs +19 -0
- package/dist/_virtual/rolldown_runtime.mjs +18 -0
- package/dist/common.cjs +55 -98
- package/dist/common.d.ts +30509 -26329
- package/dist/common.mjs +54 -98
- package/dist/constants.cjs +16 -7
- package/dist/constants.d.ts +11 -1
- package/dist/constants.mjs +12 -9
- package/dist/index.cjs +12 -10
- package/dist/index.d.ts +7 -11
- package/dist/index.mjs +5 -10
- package/dist/parsers.cjs +794 -750
- package/dist/parsers.d.ts +36279 -31216
- package/dist/parsers.mjs +738 -746
- package/dist/types.cjs +0 -1
- package/dist/types.d.ts +6 -9
- package/dist/types.mjs +1 -0
- package/package.json +6 -6
- package/dist/Client.d.cts +0 -43
- package/dist/HttpClient.d.cts +0 -13
- package/dist/WsClient.d.cts +0 -27
- package/dist/common.d.cts +0 -26948
- package/dist/constants-jLrn-AnI.d.cts +0 -13
- package/dist/constants-jLrn-AnI.d.ts +0 -13
- package/dist/constants.d.cts +0 -1
- package/dist/index.d.cts +0 -11
- package/dist/parsers.d.cts +0 -31990
- package/dist/types.d.cts +0 -87
package/dist/WsClient.cjs
CHANGED
|
@@ -1,126 +1,121 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var CONNECTING = "CONNECTING";
|
|
7
|
-
var READY = "READY";
|
|
8
|
-
var DISCONNECT = "DISCONNECT";
|
|
9
|
-
var WsClient = (_class = class extends _Clientcjs2.default {
|
|
10
|
-
|
|
11
|
-
__init() {this.reconnectAttempts = 0}
|
|
12
|
-
__init2() {this.emitter = new EventTarget()}
|
|
13
|
-
__init3() {this.inFlightRequestMap = /* @__PURE__ */ new Map()}
|
|
14
|
-
|
|
15
|
-
__init4() {this.shouldConnect = true}
|
|
16
|
-
constructor(url, { timeout = 3e4, ...opts } = {}) {
|
|
17
|
-
super(url, opts);_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);_class.prototype.__init4.call(this);_class.prototype.__init5.call(this);_class.prototype.__init6.call(this);;
|
|
18
|
-
this.timeout = timeout;
|
|
19
|
-
}
|
|
20
|
-
async close() {
|
|
21
|
-
this.shouldConnect = false;
|
|
22
|
-
if (!this.ws) return;
|
|
23
|
-
await this.handleDisconnect();
|
|
24
|
-
const waitForClose = this.ws.readyState === WebSocket.OPEN;
|
|
25
|
-
this.ws.close();
|
|
26
|
-
if (waitForClose) await _async.once.call(void 0, this.ws, "close");
|
|
27
|
-
this.ws = void 0;
|
|
28
|
-
}
|
|
29
|
-
async connectionReady() {
|
|
30
|
-
if (!this.ws) {
|
|
31
|
-
return this.connect();
|
|
32
|
-
}
|
|
33
|
-
if (this.ws.readyState !== WebSocket.OPEN) {
|
|
34
|
-
await _async.once.call(void 0, this.emitter, READY, { timeout: 3e4 });
|
|
35
|
-
}
|
|
36
|
-
return this.ws;
|
|
37
|
-
}
|
|
38
|
-
__init5() {this.handleDisconnect = async () => {
|
|
39
|
-
this.emitter.dispatchEvent(new Event(DISCONNECT));
|
|
40
|
-
this.inFlightRequestMap.forEach((request) => {
|
|
41
|
-
request.reject(new Error("disconnected"));
|
|
42
|
-
});
|
|
43
|
-
this.inFlightRequestMap.clear();
|
|
44
|
-
if (!this.shouldConnect) return;
|
|
45
|
-
const backoff = 250 * 2 ** this.reconnectAttempts;
|
|
46
|
-
await _async.sleep.call(void 0, backoff);
|
|
47
|
-
await this.connect().catch(() => {
|
|
48
|
-
this.reconnectAttempts = Math.min(this.reconnectAttempts + 1, 6);
|
|
49
|
-
});
|
|
50
|
-
}}
|
|
51
|
-
__init6() {this.handleMessage = (data) => {
|
|
52
|
-
const parsedData = JSON.parse(data.data);
|
|
53
|
-
const responses = _zod.z.array(_commoncjs.rpcResponse).safeParse(parsedData);
|
|
54
|
-
if (!responses.success) return;
|
|
55
|
-
for (const response of responses.data) {
|
|
56
|
-
const { id } = response;
|
|
57
|
-
_optionalChain([this, 'access', _6 => _6.inFlightRequestMap, 'access', _7 => _7.get, 'call', _8 => _8(id), 'optionalAccess', _9 => _9.resolve, 'call', _10 => _10(response)]);
|
|
58
|
-
}
|
|
59
|
-
}}
|
|
60
|
-
async connect() {
|
|
61
|
-
this.shouldConnect = true;
|
|
62
|
-
this.emitter.dispatchEvent(new Event(CONNECTING));
|
|
63
|
-
const socket = new WebSocket(this.url);
|
|
64
|
-
this.ws = socket;
|
|
65
|
-
this.ws.addEventListener("message", this.handleMessage);
|
|
66
|
-
let connected = false;
|
|
67
|
-
const handleConnectionError = () => {
|
|
68
|
-
if (!connected) {
|
|
69
|
-
void this.handleDisconnect();
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
this.ws.addEventListener("close", this.handleDisconnect, { once: true });
|
|
73
|
-
this.ws.addEventListener("error", handleConnectionError, { once: true });
|
|
74
|
-
await _async.once.call(void 0, this.ws, "open", { timeout: this.timeout });
|
|
75
|
-
connected = true;
|
|
76
|
-
this.ws.addEventListener("error", () => {
|
|
77
|
-
socket.close();
|
|
78
|
-
});
|
|
79
|
-
this.emitter.dispatchEvent(new Event(READY));
|
|
80
|
-
this.ws.removeEventListener("error", handleConnectionError);
|
|
81
|
-
this.reconnectAttempts = 0;
|
|
82
|
-
return socket;
|
|
83
|
-
}
|
|
84
|
-
async send(requests, requestMap) {
|
|
85
|
-
const MAX_RETRIES = 5;
|
|
86
|
-
let socket;
|
|
87
|
-
for (let i = 0; i < MAX_RETRIES; i += 1) {
|
|
88
|
-
try {
|
|
89
|
-
socket = await this.connectionReady();
|
|
90
|
-
} catch (e) {
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
if (!socket) {
|
|
95
|
-
this.handleErrorResponse(new Error("failed to connect"), requestMap);
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
socket.send(JSON.stringify(requests));
|
|
99
|
-
const promises = [];
|
|
100
|
-
for (const [id] of requestMap) {
|
|
101
|
-
const result = _async.deferredPromise.call(void 0, );
|
|
102
|
-
const controller = new AbortController();
|
|
103
|
-
this.inFlightRequestMap.set(id, result);
|
|
104
|
-
promises.push(
|
|
105
|
-
Promise.race([
|
|
106
|
-
_async.sleep.call(void 0, this.timeout, { signal: controller.signal }).then(
|
|
107
|
-
() => ({ id, success: false, error: new Error("timeout") })
|
|
108
|
-
),
|
|
109
|
-
result.promise.then(
|
|
110
|
-
(r) => ({ id, success: true, result: r }),
|
|
111
|
-
(error) => ({ id, success: false, error })
|
|
112
|
-
)
|
|
113
|
-
]).then((response) => {
|
|
114
|
-
this.handleResponse(response, requestMap);
|
|
115
|
-
}).finally(() => {
|
|
116
|
-
this.inFlightRequestMap.delete(id);
|
|
117
|
-
controller.abort();
|
|
118
|
-
})
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
await Promise.all(promises);
|
|
122
|
-
}
|
|
123
|
-
}, _class);
|
|
1
|
+
const require_parsers = require('./parsers.cjs');
|
|
2
|
+
require('./common.cjs');
|
|
3
|
+
const require_Client = require('./Client.cjs');
|
|
4
|
+
let _chainflip_utils_async = require("@chainflip/utils/async");
|
|
5
|
+
let zod = require("zod");
|
|
124
6
|
|
|
7
|
+
//#region src/WsClient.ts
|
|
8
|
+
const CONNECTING = "CONNECTING";
|
|
9
|
+
const READY = "READY";
|
|
10
|
+
const DISCONNECT = "DISCONNECT";
|
|
11
|
+
var WsClient = class extends require_Client {
|
|
12
|
+
ws;
|
|
13
|
+
reconnectAttempts = 0;
|
|
14
|
+
emitter = new EventTarget();
|
|
15
|
+
inFlightRequestMap = /* @__PURE__ */ new Map();
|
|
16
|
+
timeout;
|
|
17
|
+
shouldConnect = true;
|
|
18
|
+
constructor(url, { timeout = 3e4, ...opts } = {}) {
|
|
19
|
+
super(url, opts);
|
|
20
|
+
this.timeout = timeout;
|
|
21
|
+
}
|
|
22
|
+
async close() {
|
|
23
|
+
this.shouldConnect = false;
|
|
24
|
+
if (!this.ws) return;
|
|
25
|
+
await this.handleDisconnect();
|
|
26
|
+
const waitForClose = this.ws.readyState === WebSocket.OPEN;
|
|
27
|
+
this.ws.close();
|
|
28
|
+
if (waitForClose) await (0, _chainflip_utils_async.once)(this.ws, "close");
|
|
29
|
+
this.ws = void 0;
|
|
30
|
+
}
|
|
31
|
+
async connectionReady() {
|
|
32
|
+
if (!this.ws) return this.connect();
|
|
33
|
+
if (this.ws.readyState !== WebSocket.OPEN) await (0, _chainflip_utils_async.once)(this.emitter, READY, { timeout: 3e4 });
|
|
34
|
+
return this.ws;
|
|
35
|
+
}
|
|
36
|
+
handleDisconnect = async () => {
|
|
37
|
+
this.emitter.dispatchEvent(new Event(DISCONNECT));
|
|
38
|
+
this.inFlightRequestMap.forEach((request) => {
|
|
39
|
+
request.reject(/* @__PURE__ */ new Error("disconnected"));
|
|
40
|
+
});
|
|
41
|
+
this.inFlightRequestMap.clear();
|
|
42
|
+
if (!this.shouldConnect) return;
|
|
43
|
+
await (0, _chainflip_utils_async.sleep)(250 * 2 ** this.reconnectAttempts);
|
|
44
|
+
await this.connect().catch(() => {
|
|
45
|
+
this.reconnectAttempts = Math.min(this.reconnectAttempts + 1, 6);
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
handleMessage = (data) => {
|
|
49
|
+
const parsedData = JSON.parse(data.data);
|
|
50
|
+
const responses = zod.z.array(require_parsers.rpcResponse).safeParse(parsedData);
|
|
51
|
+
if (!responses.success) return;
|
|
52
|
+
for (const response of responses.data) {
|
|
53
|
+
const { id } = response;
|
|
54
|
+
this.inFlightRequestMap.get(id)?.resolve(response);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
async connect() {
|
|
58
|
+
this.shouldConnect = true;
|
|
59
|
+
this.emitter.dispatchEvent(new Event(CONNECTING));
|
|
60
|
+
const socket = new WebSocket(this.url);
|
|
61
|
+
this.ws = socket;
|
|
62
|
+
this.ws.addEventListener("message", this.handleMessage);
|
|
63
|
+
let connected = false;
|
|
64
|
+
const handleConnectionError = () => {
|
|
65
|
+
if (!connected) this.handleDisconnect();
|
|
66
|
+
};
|
|
67
|
+
this.ws.addEventListener("close", this.handleDisconnect, { once: true });
|
|
68
|
+
this.ws.addEventListener("error", handleConnectionError, { once: true });
|
|
69
|
+
await (0, _chainflip_utils_async.once)(this.ws, "open", { timeout: this.timeout });
|
|
70
|
+
connected = true;
|
|
71
|
+
this.ws.addEventListener("error", () => {
|
|
72
|
+
socket.close();
|
|
73
|
+
});
|
|
74
|
+
this.emitter.dispatchEvent(new Event(READY));
|
|
75
|
+
this.ws.removeEventListener("error", handleConnectionError);
|
|
76
|
+
this.reconnectAttempts = 0;
|
|
77
|
+
return socket;
|
|
78
|
+
}
|
|
79
|
+
async send(requests, requestMap) {
|
|
80
|
+
const MAX_RETRIES = 5;
|
|
81
|
+
let socket;
|
|
82
|
+
for (let i = 0; i < MAX_RETRIES; i += 1) try {
|
|
83
|
+
socket = await this.connectionReady();
|
|
84
|
+
} catch {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (!socket) {
|
|
88
|
+
this.handleErrorResponse(/* @__PURE__ */ new Error("failed to connect"), requestMap);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
socket.send(JSON.stringify(requests));
|
|
92
|
+
const promises = [];
|
|
93
|
+
for (const [id] of requestMap) {
|
|
94
|
+
const result = (0, _chainflip_utils_async.deferredPromise)();
|
|
95
|
+
const controller = new AbortController();
|
|
96
|
+
this.inFlightRequestMap.set(id, result);
|
|
97
|
+
promises.push(Promise.race([(0, _chainflip_utils_async.sleep)(this.timeout, { signal: controller.signal }).then(() => ({
|
|
98
|
+
id,
|
|
99
|
+
success: false,
|
|
100
|
+
error: /* @__PURE__ */ new Error("timeout")
|
|
101
|
+
})), result.promise.then((r) => ({
|
|
102
|
+
id,
|
|
103
|
+
success: true,
|
|
104
|
+
result: r
|
|
105
|
+
}), (error) => ({
|
|
106
|
+
id,
|
|
107
|
+
success: false,
|
|
108
|
+
error
|
|
109
|
+
}))]).then((response) => {
|
|
110
|
+
this.handleResponse(response, requestMap);
|
|
111
|
+
}).finally(() => {
|
|
112
|
+
this.inFlightRequestMap.delete(id);
|
|
113
|
+
controller.abort();
|
|
114
|
+
}));
|
|
115
|
+
}
|
|
116
|
+
await Promise.all(promises);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
125
119
|
|
|
126
|
-
|
|
120
|
+
//#endregion
|
|
121
|
+
module.exports = WsClient;
|
package/dist/WsClient.d.ts
CHANGED
|
@@ -1,27 +1,25 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import '@chainflip/utils/async';
|
|
4
|
-
import '@chainflip/utils/chainflip';
|
|
5
|
-
import '@chainflip/utils/types';
|
|
6
|
-
import 'zod';
|
|
7
|
-
import './parsers.js';
|
|
1
|
+
import { JsonRpcRequest, RpcMethod } from "./common.js";
|
|
2
|
+
import Client, { ClientOpts, RequestMap } from "./Client.js";
|
|
8
3
|
|
|
4
|
+
//#region src/WsClient.d.ts
|
|
9
5
|
declare class WsClient extends Client {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
6
|
+
private ws?;
|
|
7
|
+
private reconnectAttempts;
|
|
8
|
+
private emitter;
|
|
9
|
+
private inFlightRequestMap;
|
|
10
|
+
private readonly timeout;
|
|
11
|
+
private shouldConnect;
|
|
12
|
+
constructor(url: string, {
|
|
13
|
+
timeout,
|
|
14
|
+
...opts
|
|
15
|
+
}?: {
|
|
16
|
+
timeout?: number;
|
|
17
|
+
} & ClientOpts);
|
|
18
|
+
close(): Promise<void>;
|
|
19
|
+
private connectionReady;
|
|
20
|
+
private handleDisconnect;
|
|
21
|
+
private handleMessage;
|
|
22
|
+
private connect;
|
|
23
|
+
protected send(requests: JsonRpcRequest<RpcMethod>[], requestMap: RequestMap): Promise<void>;
|
|
25
24
|
}
|
|
26
|
-
|
|
27
|
-
export { WsClient as default };
|
|
25
|
+
export = WsClient;
|
package/dist/WsClient.mjs
CHANGED
|
@@ -1,126 +1,121 @@
|
|
|
1
|
-
|
|
1
|
+
import { rpcResponse } from "./parsers.mjs";
|
|
2
|
+
import "./common.mjs";
|
|
3
|
+
import Client from "./Client.mjs";
|
|
2
4
|
import { deferredPromise, once, sleep } from "@chainflip/utils/async";
|
|
3
5
|
import { z } from "zod";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
|
|
7
|
+
//#region src/WsClient.ts
|
|
8
|
+
const CONNECTING = "CONNECTING";
|
|
9
|
+
const READY = "READY";
|
|
10
|
+
const DISCONNECT = "DISCONNECT";
|
|
9
11
|
var WsClient = class extends Client {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
this.inFlightRequestMap.delete(id);
|
|
117
|
-
controller.abort();
|
|
118
|
-
})
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
await Promise.all(promises);
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
export {
|
|
125
|
-
WsClient as default
|
|
12
|
+
ws;
|
|
13
|
+
reconnectAttempts = 0;
|
|
14
|
+
emitter = new EventTarget();
|
|
15
|
+
inFlightRequestMap = /* @__PURE__ */ new Map();
|
|
16
|
+
timeout;
|
|
17
|
+
shouldConnect = true;
|
|
18
|
+
constructor(url, { timeout = 3e4, ...opts } = {}) {
|
|
19
|
+
super(url, opts);
|
|
20
|
+
this.timeout = timeout;
|
|
21
|
+
}
|
|
22
|
+
async close() {
|
|
23
|
+
this.shouldConnect = false;
|
|
24
|
+
if (!this.ws) return;
|
|
25
|
+
await this.handleDisconnect();
|
|
26
|
+
const waitForClose = this.ws.readyState === WebSocket.OPEN;
|
|
27
|
+
this.ws.close();
|
|
28
|
+
if (waitForClose) await once(this.ws, "close");
|
|
29
|
+
this.ws = void 0;
|
|
30
|
+
}
|
|
31
|
+
async connectionReady() {
|
|
32
|
+
if (!this.ws) return this.connect();
|
|
33
|
+
if (this.ws.readyState !== WebSocket.OPEN) await once(this.emitter, READY, { timeout: 3e4 });
|
|
34
|
+
return this.ws;
|
|
35
|
+
}
|
|
36
|
+
handleDisconnect = async () => {
|
|
37
|
+
this.emitter.dispatchEvent(new Event(DISCONNECT));
|
|
38
|
+
this.inFlightRequestMap.forEach((request) => {
|
|
39
|
+
request.reject(/* @__PURE__ */ new Error("disconnected"));
|
|
40
|
+
});
|
|
41
|
+
this.inFlightRequestMap.clear();
|
|
42
|
+
if (!this.shouldConnect) return;
|
|
43
|
+
await sleep(250 * 2 ** this.reconnectAttempts);
|
|
44
|
+
await this.connect().catch(() => {
|
|
45
|
+
this.reconnectAttempts = Math.min(this.reconnectAttempts + 1, 6);
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
handleMessage = (data) => {
|
|
49
|
+
const parsedData = JSON.parse(data.data);
|
|
50
|
+
const responses = z.array(rpcResponse).safeParse(parsedData);
|
|
51
|
+
if (!responses.success) return;
|
|
52
|
+
for (const response of responses.data) {
|
|
53
|
+
const { id } = response;
|
|
54
|
+
this.inFlightRequestMap.get(id)?.resolve(response);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
async connect() {
|
|
58
|
+
this.shouldConnect = true;
|
|
59
|
+
this.emitter.dispatchEvent(new Event(CONNECTING));
|
|
60
|
+
const socket = new WebSocket(this.url);
|
|
61
|
+
this.ws = socket;
|
|
62
|
+
this.ws.addEventListener("message", this.handleMessage);
|
|
63
|
+
let connected = false;
|
|
64
|
+
const handleConnectionError = () => {
|
|
65
|
+
if (!connected) this.handleDisconnect();
|
|
66
|
+
};
|
|
67
|
+
this.ws.addEventListener("close", this.handleDisconnect, { once: true });
|
|
68
|
+
this.ws.addEventListener("error", handleConnectionError, { once: true });
|
|
69
|
+
await once(this.ws, "open", { timeout: this.timeout });
|
|
70
|
+
connected = true;
|
|
71
|
+
this.ws.addEventListener("error", () => {
|
|
72
|
+
socket.close();
|
|
73
|
+
});
|
|
74
|
+
this.emitter.dispatchEvent(new Event(READY));
|
|
75
|
+
this.ws.removeEventListener("error", handleConnectionError);
|
|
76
|
+
this.reconnectAttempts = 0;
|
|
77
|
+
return socket;
|
|
78
|
+
}
|
|
79
|
+
async send(requests, requestMap) {
|
|
80
|
+
const MAX_RETRIES = 5;
|
|
81
|
+
let socket;
|
|
82
|
+
for (let i = 0; i < MAX_RETRIES; i += 1) try {
|
|
83
|
+
socket = await this.connectionReady();
|
|
84
|
+
} catch {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (!socket) {
|
|
88
|
+
this.handleErrorResponse(/* @__PURE__ */ new Error("failed to connect"), requestMap);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
socket.send(JSON.stringify(requests));
|
|
92
|
+
const promises = [];
|
|
93
|
+
for (const [id] of requestMap) {
|
|
94
|
+
const result = deferredPromise();
|
|
95
|
+
const controller = new AbortController();
|
|
96
|
+
this.inFlightRequestMap.set(id, result);
|
|
97
|
+
promises.push(Promise.race([sleep(this.timeout, { signal: controller.signal }).then(() => ({
|
|
98
|
+
id,
|
|
99
|
+
success: false,
|
|
100
|
+
error: /* @__PURE__ */ new Error("timeout")
|
|
101
|
+
})), result.promise.then((r) => ({
|
|
102
|
+
id,
|
|
103
|
+
success: true,
|
|
104
|
+
result: r
|
|
105
|
+
}), (error) => ({
|
|
106
|
+
id,
|
|
107
|
+
success: false,
|
|
108
|
+
error
|
|
109
|
+
}))]).then((response) => {
|
|
110
|
+
this.handleResponse(response, requestMap);
|
|
111
|
+
}).finally(() => {
|
|
112
|
+
this.inFlightRequestMap.delete(id);
|
|
113
|
+
controller.abort();
|
|
114
|
+
}));
|
|
115
|
+
}
|
|
116
|
+
await Promise.all(promises);
|
|
117
|
+
}
|
|
126
118
|
};
|
|
119
|
+
|
|
120
|
+
//#endregion
|
|
121
|
+
export { WsClient as default };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) {
|
|
6
|
+
__defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: true
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
if (symbols) {
|
|
12
|
+
__defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
13
|
+
}
|
|
14
|
+
return target;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
|
|
19
|
+
exports.__exportAll = __exportAll;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) {
|
|
6
|
+
__defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: true
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
if (symbols) {
|
|
12
|
+
__defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
13
|
+
}
|
|
14
|
+
return target;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { __exportAll };
|