@chainflip/rpc 1.8.6 → 1.8.8
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 +52 -21
- package/dist/Client.d.cts +17 -11
- package/dist/Client.d.ts +17 -11
- package/dist/Client.mjs +52 -21
- package/dist/HttpClient.cjs +12 -62
- package/dist/HttpClient.d.cts +4 -8
- package/dist/HttpClient.d.ts +4 -8
- package/dist/HttpClient.mjs +9 -59
- package/dist/WsClient.cjs +73 -60
- package/dist/WsClient.d.cts +10 -7
- package/dist/WsClient.d.ts +10 -7
- package/dist/WsClient.mjs +71 -58
- package/dist/common.cjs +4 -2
- package/dist/common.d.cts +1013 -918
- package/dist/common.d.ts +1013 -918
- package/dist/common.mjs +5 -3
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/parsers.cjs +10 -8
- package/dist/parsers.d.cts +1187 -1093
- package/dist/parsers.d.ts +1187 -1093
- package/dist/parsers.mjs +9 -7
- package/package.json +5 -5
package/dist/Client.cjs
CHANGED
|
@@ -1,41 +1,72 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); var _class;// src/Client.ts
|
|
2
|
-
var
|
|
3
|
-
|
|
2
|
+
var _async = require('@chainflip/utils/async');
|
|
4
3
|
|
|
5
4
|
|
|
6
5
|
var _commoncjs = require('./common.cjs');
|
|
7
6
|
var Client = (_class = class {
|
|
8
|
-
constructor(url) {;_class.prototype.__init.call(this);
|
|
7
|
+
constructor(url) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);
|
|
9
8
|
this.url = url;
|
|
10
9
|
}
|
|
11
10
|
__init() {this.lastRequestId = 0}
|
|
11
|
+
__init2() {this.timer = null}
|
|
12
|
+
__init3() {this.requestMap = /* @__PURE__ */ new Map()}
|
|
12
13
|
getRequestId() {
|
|
13
|
-
|
|
14
|
+
try {
|
|
15
|
+
return crypto.randomUUID();
|
|
16
|
+
} catch (e) {
|
|
17
|
+
return String(++this.lastRequestId);
|
|
18
|
+
}
|
|
14
19
|
}
|
|
15
20
|
formatRequest(method, params) {
|
|
16
21
|
return { jsonrpc: "2.0", id: this.getRequestId(), method, params };
|
|
17
22
|
}
|
|
18
|
-
|
|
23
|
+
handleResponse(response, clonedMap) {
|
|
24
|
+
const clonedItem = clonedMap.get(response.id);
|
|
25
|
+
if (!clonedItem) return;
|
|
26
|
+
clonedMap.delete(response.id);
|
|
19
27
|
if (!response.success) {
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
const parseResult = _commoncjs.rpcResponse.safeParse(response.result);
|
|
23
|
-
if (!parseResult.success) {
|
|
24
|
-
throw new Error("Malformed RPC response received");
|
|
28
|
+
return clonedItem.deferred.reject(response.error);
|
|
25
29
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
const rpcResponse = response.result;
|
|
31
|
+
if ("error" in rpcResponse) {
|
|
32
|
+
return clonedItem.deferred.reject(
|
|
33
|
+
new Error(`RPC error [${rpcResponse.error.code}]: ${rpcResponse.error.message}`)
|
|
29
34
|
);
|
|
30
35
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
const parseResult = _commoncjs.rpcResult[clonedItem.method].safeParse(rpcResponse.result);
|
|
37
|
+
if (parseResult.error) {
|
|
38
|
+
return clonedItem.deferred.reject(parseResult.error);
|
|
39
|
+
}
|
|
40
|
+
clonedItem.deferred.resolve(parseResult.data);
|
|
41
|
+
}
|
|
42
|
+
handleErrorResponse(error, clonedMap) {
|
|
43
|
+
for (const [id] of clonedMap) {
|
|
44
|
+
this.handleResponse({ id, success: false, error }, clonedMap);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async handleBatch() {
|
|
48
|
+
const clonedMap = new Map(this.requestMap);
|
|
49
|
+
this.requestMap.clear();
|
|
50
|
+
const requests = [...clonedMap.values()].map((item) => item.body);
|
|
51
|
+
await this.send(requests, clonedMap);
|
|
52
|
+
clonedMap.forEach((item) => {
|
|
53
|
+
item.deferred.reject(new Error("Could not find the result for the request"));
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
sendRequest(method, ...params) {
|
|
57
|
+
if (!_commoncjs.rpcResult[method]) {
|
|
58
|
+
return Promise.reject(new Error(`Unknown method: ${method}`));
|
|
59
|
+
}
|
|
60
|
+
const deferred = _async.deferredPromise.call(void 0, );
|
|
61
|
+
const body = this.formatRequest(method, params);
|
|
62
|
+
this.requestMap.set(body.id, { deferred, body, method });
|
|
63
|
+
if (!this.timer) {
|
|
64
|
+
this.timer = setTimeout(() => {
|
|
65
|
+
this.timer = null;
|
|
66
|
+
void this.handleBatch();
|
|
67
|
+
}, 0);
|
|
68
|
+
}
|
|
69
|
+
return deferred.promise;
|
|
39
70
|
}
|
|
40
71
|
methods() {
|
|
41
72
|
return Object.keys(_commoncjs.rpcResult).sort();
|
package/dist/Client.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DeferredPromise } from '@chainflip/utils/async';
|
|
2
|
+
import { RpcMethod, JsonRpcRequest, RpcResult, JsonRpcResponse, RpcRequest } from './common.cjs';
|
|
2
3
|
import '@chainflip/utils/types';
|
|
3
4
|
import 'zod';
|
|
4
5
|
import './parsers.cjs';
|
|
@@ -6,26 +7,31 @@ import './parsers.cjs';
|
|
|
6
7
|
type Response = {
|
|
7
8
|
success: true;
|
|
8
9
|
id: string;
|
|
9
|
-
result:
|
|
10
|
+
result: JsonRpcResponse;
|
|
10
11
|
} | {
|
|
11
12
|
success: false;
|
|
12
13
|
id: string;
|
|
13
14
|
error: Error;
|
|
14
15
|
};
|
|
16
|
+
type RequestMap = Map<string, {
|
|
17
|
+
deferred: DeferredPromise<RpcResult<RpcMethod>>;
|
|
18
|
+
body: JsonRpcRequest<RpcMethod>;
|
|
19
|
+
method: RpcMethod;
|
|
20
|
+
}>;
|
|
15
21
|
declare abstract class Client {
|
|
16
22
|
protected readonly url: string;
|
|
17
23
|
private lastRequestId;
|
|
24
|
+
private timer;
|
|
25
|
+
private requestMap;
|
|
18
26
|
constructor(url: string);
|
|
19
|
-
protected abstract send<const T extends RpcMethod>(data: JsonRpcRequest<T>[]): Promise<
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
protected
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
result?: any;
|
|
26
|
-
};
|
|
27
|
+
protected abstract send<const T extends RpcMethod>(data: JsonRpcRequest<T>[], clonedMap: RequestMap): Promise<void>;
|
|
28
|
+
private getRequestId;
|
|
29
|
+
private formatRequest;
|
|
30
|
+
protected handleResponse(response: Response, clonedMap: RequestMap): void;
|
|
31
|
+
protected handleErrorResponse(error: Error, clonedMap: RequestMap): void;
|
|
32
|
+
private handleBatch;
|
|
27
33
|
sendRequest<const T extends RpcMethod>(method: T, ...params: RpcRequest[T]): Promise<RpcResult<T>>;
|
|
28
34
|
methods(): RpcMethod[];
|
|
29
35
|
}
|
|
30
36
|
|
|
31
|
-
export { type Response, Client as default };
|
|
37
|
+
export { type RequestMap, type Response, Client as default };
|
package/dist/Client.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DeferredPromise } from '@chainflip/utils/async';
|
|
2
|
+
import { RpcMethod, JsonRpcRequest, RpcResult, JsonRpcResponse, RpcRequest } from './common.js';
|
|
2
3
|
import '@chainflip/utils/types';
|
|
3
4
|
import 'zod';
|
|
4
5
|
import './parsers.js';
|
|
@@ -6,26 +7,31 @@ import './parsers.js';
|
|
|
6
7
|
type Response = {
|
|
7
8
|
success: true;
|
|
8
9
|
id: string;
|
|
9
|
-
result:
|
|
10
|
+
result: JsonRpcResponse;
|
|
10
11
|
} | {
|
|
11
12
|
success: false;
|
|
12
13
|
id: string;
|
|
13
14
|
error: Error;
|
|
14
15
|
};
|
|
16
|
+
type RequestMap = Map<string, {
|
|
17
|
+
deferred: DeferredPromise<RpcResult<RpcMethod>>;
|
|
18
|
+
body: JsonRpcRequest<RpcMethod>;
|
|
19
|
+
method: RpcMethod;
|
|
20
|
+
}>;
|
|
15
21
|
declare abstract class Client {
|
|
16
22
|
protected readonly url: string;
|
|
17
23
|
private lastRequestId;
|
|
24
|
+
private timer;
|
|
25
|
+
private requestMap;
|
|
18
26
|
constructor(url: string);
|
|
19
|
-
protected abstract send<const T extends RpcMethod>(data: JsonRpcRequest<T>[]): Promise<
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
protected
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
result?: any;
|
|
26
|
-
};
|
|
27
|
+
protected abstract send<const T extends RpcMethod>(data: JsonRpcRequest<T>[], clonedMap: RequestMap): Promise<void>;
|
|
28
|
+
private getRequestId;
|
|
29
|
+
private formatRequest;
|
|
30
|
+
protected handleResponse(response: Response, clonedMap: RequestMap): void;
|
|
31
|
+
protected handleErrorResponse(error: Error, clonedMap: RequestMap): void;
|
|
32
|
+
private handleBatch;
|
|
27
33
|
sendRequest<const T extends RpcMethod>(method: T, ...params: RpcRequest[T]): Promise<RpcResult<T>>;
|
|
28
34
|
methods(): RpcMethod[];
|
|
29
35
|
}
|
|
30
36
|
|
|
31
|
-
export { type Response, Client as default };
|
|
37
|
+
export { type RequestMap, type Response, Client as default };
|
package/dist/Client.mjs
CHANGED
|
@@ -1,41 +1,72 @@
|
|
|
1
1
|
// src/Client.ts
|
|
2
|
-
import {
|
|
2
|
+
import { deferredPromise } from "@chainflip/utils/async";
|
|
3
3
|
import {
|
|
4
|
-
rpcResult
|
|
5
|
-
rpcResponse
|
|
4
|
+
rpcResult
|
|
6
5
|
} from "./common.mjs";
|
|
7
6
|
var Client = class {
|
|
8
7
|
constructor(url) {
|
|
9
8
|
this.url = url;
|
|
10
9
|
}
|
|
11
10
|
lastRequestId = 0;
|
|
11
|
+
timer = null;
|
|
12
|
+
requestMap = /* @__PURE__ */ new Map();
|
|
12
13
|
getRequestId() {
|
|
13
|
-
|
|
14
|
+
try {
|
|
15
|
+
return crypto.randomUUID();
|
|
16
|
+
} catch {
|
|
17
|
+
return String(++this.lastRequestId);
|
|
18
|
+
}
|
|
14
19
|
}
|
|
15
20
|
formatRequest(method, params) {
|
|
16
21
|
return { jsonrpc: "2.0", id: this.getRequestId(), method, params };
|
|
17
22
|
}
|
|
18
|
-
|
|
23
|
+
handleResponse(response, clonedMap) {
|
|
24
|
+
const clonedItem = clonedMap.get(response.id);
|
|
25
|
+
if (!clonedItem) return;
|
|
26
|
+
clonedMap.delete(response.id);
|
|
19
27
|
if (!response.success) {
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
const parseResult = rpcResponse.safeParse(response.result);
|
|
23
|
-
if (!parseResult.success) {
|
|
24
|
-
throw new Error("Malformed RPC response received");
|
|
28
|
+
return clonedItem.deferred.reject(response.error);
|
|
25
29
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
const rpcResponse = response.result;
|
|
31
|
+
if ("error" in rpcResponse) {
|
|
32
|
+
return clonedItem.deferred.reject(
|
|
33
|
+
new Error(`RPC error [${rpcResponse.error.code}]: ${rpcResponse.error.message}`)
|
|
29
34
|
);
|
|
30
35
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
const parseResult = rpcResult[clonedItem.method].safeParse(rpcResponse.result);
|
|
37
|
+
if (parseResult.error) {
|
|
38
|
+
return clonedItem.deferred.reject(parseResult.error);
|
|
39
|
+
}
|
|
40
|
+
clonedItem.deferred.resolve(parseResult.data);
|
|
41
|
+
}
|
|
42
|
+
handleErrorResponse(error, clonedMap) {
|
|
43
|
+
for (const [id] of clonedMap) {
|
|
44
|
+
this.handleResponse({ id, success: false, error }, clonedMap);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async handleBatch() {
|
|
48
|
+
const clonedMap = new Map(this.requestMap);
|
|
49
|
+
this.requestMap.clear();
|
|
50
|
+
const requests = [...clonedMap.values()].map((item) => item.body);
|
|
51
|
+
await this.send(requests, clonedMap);
|
|
52
|
+
clonedMap.forEach((item) => {
|
|
53
|
+
item.deferred.reject(new Error("Could not find the result for the request"));
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
sendRequest(method, ...params) {
|
|
57
|
+
if (!rpcResult[method]) {
|
|
58
|
+
return Promise.reject(new Error(`Unknown method: ${method}`));
|
|
59
|
+
}
|
|
60
|
+
const deferred = deferredPromise();
|
|
61
|
+
const body = this.formatRequest(method, params);
|
|
62
|
+
this.requestMap.set(body.id, { deferred, body, method });
|
|
63
|
+
if (!this.timer) {
|
|
64
|
+
this.timer = setTimeout(() => {
|
|
65
|
+
this.timer = null;
|
|
66
|
+
void this.handleBatch();
|
|
67
|
+
}, 0);
|
|
68
|
+
}
|
|
69
|
+
return deferred.promise;
|
|
39
70
|
}
|
|
40
71
|
methods() {
|
|
41
72
|
return Object.keys(rpcResult).sort();
|
package/dist/HttpClient.cjs
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
2
|
-
var _async = require('@chainflip/utils/async');
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// src/HttpClient.ts
|
|
3
2
|
var _Clientcjs = require('./Client.cjs'); var _Clientcjs2 = _interopRequireDefault(_Clientcjs);
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var _commoncjs = require('./common.cjs');
|
|
7
|
-
var HttpClient = (_class = class extends _Clientcjs2.default {constructor(...args) { super(...args); _class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this); }
|
|
8
|
-
__init() {this.timer = null}
|
|
9
|
-
__init2() {this.batchDuration = 100}
|
|
10
|
-
__init3() {this.requestMap = /* @__PURE__ */ new Map()}
|
|
11
|
-
async send(request) {
|
|
3
|
+
var HttpClient = class extends _Clientcjs2.default {
|
|
4
|
+
async send(request, requestMap) {
|
|
12
5
|
let res;
|
|
13
6
|
try {
|
|
14
7
|
res = await fetch(this.url, {
|
|
@@ -19,66 +12,23 @@ var HttpClient = (_class = class extends _Clientcjs2.default {constructor(...arg
|
|
|
19
12
|
}
|
|
20
13
|
});
|
|
21
14
|
} catch (error) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
success: false,
|
|
25
|
-
error: new Error("Network error", { cause: error })
|
|
26
|
-
}));
|
|
15
|
+
this.handleErrorResponse(new Error("Network error", { cause: error }), requestMap);
|
|
16
|
+
return;
|
|
27
17
|
}
|
|
28
18
|
if (!res.ok) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
success: false,
|
|
32
|
-
error: new Error(`HTTP error: ${res.status}`)
|
|
33
|
-
}));
|
|
19
|
+
this.handleErrorResponse(new Error(`HTTP error: ${res.status}`), requestMap);
|
|
20
|
+
return;
|
|
34
21
|
}
|
|
35
22
|
try {
|
|
36
23
|
const jsonRpcResponse = await res.json();
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return request.map((r) => ({
|
|
40
|
-
id: r.id,
|
|
41
|
-
success: false,
|
|
42
|
-
error: new Error("Invalid JSON response", { cause })
|
|
43
|
-
}));
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
sendRequest(method, ...params) {
|
|
47
|
-
const deferred = _async.deferredPromise.call(void 0, );
|
|
48
|
-
const body = this.formatRequest(method, params);
|
|
49
|
-
this.requestMap.set(body.id, { deferred, body, method });
|
|
50
|
-
if (this.timer) clearTimeout(this.timer);
|
|
51
|
-
this.timer = setTimeout(() => this.sendBatch(), this.batchDuration);
|
|
52
|
-
return deferred.promise;
|
|
53
|
-
}
|
|
54
|
-
async sendBatch() {
|
|
55
|
-
const clonedMap = new Map(this.requestMap);
|
|
56
|
-
this.requestMap.clear();
|
|
57
|
-
const requests = [...clonedMap.values()].map((item) => item.body);
|
|
58
|
-
const responses = await this.send(requests);
|
|
59
|
-
for (const response of responses) {
|
|
60
|
-
const clonedItem = clonedMap.get(response.id);
|
|
61
|
-
if (!clonedItem) {
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
if (!response.success) {
|
|
65
|
-
clonedItem.deferred.reject(response.error);
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
try {
|
|
69
|
-
const parseResult = this.parseSingleResponse(response);
|
|
70
|
-
clonedItem.deferred.resolve(_commoncjs.rpcResult[clonedItem.method].parse(parseResult.result));
|
|
71
|
-
} catch (e) {
|
|
72
|
-
clonedItem.deferred.reject(e);
|
|
73
|
-
} finally {
|
|
74
|
-
clonedMap.delete(response.id);
|
|
24
|
+
for (const r of jsonRpcResponse) {
|
|
25
|
+
this.handleResponse({ id: r.id, success: true, result: r }, requestMap);
|
|
75
26
|
}
|
|
27
|
+
} catch (cause) {
|
|
28
|
+
this.handleErrorResponse(new Error("Invalid JSON response", { cause }), requestMap);
|
|
76
29
|
}
|
|
77
|
-
clonedMap.forEach((item) => {
|
|
78
|
-
item.deferred.reject(new Error("Could not find the result for the request"));
|
|
79
|
-
});
|
|
80
30
|
}
|
|
81
|
-
}
|
|
31
|
+
};
|
|
82
32
|
|
|
83
33
|
|
|
84
34
|
exports.default = HttpClient;
|
package/dist/HttpClient.d.cts
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
import Client, {
|
|
2
|
-
import { RpcMethod, JsonRpcRequest
|
|
1
|
+
import Client, { RequestMap } from './Client.cjs';
|
|
2
|
+
import { RpcMethod, JsonRpcRequest } from './common.cjs';
|
|
3
|
+
import '@chainflip/utils/async';
|
|
3
4
|
import '@chainflip/utils/types';
|
|
4
5
|
import 'zod';
|
|
5
6
|
import './parsers.cjs';
|
|
6
7
|
|
|
7
8
|
declare class HttpClient extends Client {
|
|
8
|
-
|
|
9
|
-
private batchDuration;
|
|
10
|
-
private requestMap;
|
|
11
|
-
protected send<const T extends RpcMethod>(request: JsonRpcRequest<T>[]): Promise<Response[]>;
|
|
12
|
-
sendRequest<const T extends RpcMethod>(method: T, ...params: RpcRequest[T]): Promise<RpcResult<T>>;
|
|
13
|
-
private sendBatch;
|
|
9
|
+
protected send<const T extends RpcMethod>(request: JsonRpcRequest<T>[], requestMap: RequestMap): Promise<void>;
|
|
14
10
|
}
|
|
15
11
|
|
|
16
12
|
export { HttpClient as default };
|
package/dist/HttpClient.d.ts
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
import Client, {
|
|
2
|
-
import { RpcMethod, JsonRpcRequest
|
|
1
|
+
import Client, { RequestMap } from './Client.js';
|
|
2
|
+
import { RpcMethod, JsonRpcRequest } from './common.js';
|
|
3
|
+
import '@chainflip/utils/async';
|
|
3
4
|
import '@chainflip/utils/types';
|
|
4
5
|
import 'zod';
|
|
5
6
|
import './parsers.js';
|
|
6
7
|
|
|
7
8
|
declare class HttpClient extends Client {
|
|
8
|
-
|
|
9
|
-
private batchDuration;
|
|
10
|
-
private requestMap;
|
|
11
|
-
protected send<const T extends RpcMethod>(request: JsonRpcRequest<T>[]): Promise<Response[]>;
|
|
12
|
-
sendRequest<const T extends RpcMethod>(method: T, ...params: RpcRequest[T]): Promise<RpcResult<T>>;
|
|
13
|
-
private sendBatch;
|
|
9
|
+
protected send<const T extends RpcMethod>(request: JsonRpcRequest<T>[], requestMap: RequestMap): Promise<void>;
|
|
14
10
|
}
|
|
15
11
|
|
|
16
12
|
export { HttpClient as default };
|
package/dist/HttpClient.mjs
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
1
|
// src/HttpClient.ts
|
|
2
|
-
import { deferredPromise } from "@chainflip/utils/async";
|
|
3
2
|
import Client from "./Client.mjs";
|
|
4
|
-
import {
|
|
5
|
-
rpcResult
|
|
6
|
-
} from "./common.mjs";
|
|
7
3
|
var HttpClient = class extends Client {
|
|
8
|
-
|
|
9
|
-
batchDuration = 100;
|
|
10
|
-
requestMap = /* @__PURE__ */ new Map();
|
|
11
|
-
async send(request) {
|
|
4
|
+
async send(request, requestMap) {
|
|
12
5
|
let res;
|
|
13
6
|
try {
|
|
14
7
|
res = await fetch(this.url, {
|
|
@@ -19,64 +12,21 @@ var HttpClient = class extends Client {
|
|
|
19
12
|
}
|
|
20
13
|
});
|
|
21
14
|
} catch (error) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
success: false,
|
|
25
|
-
error: new Error("Network error", { cause: error })
|
|
26
|
-
}));
|
|
15
|
+
this.handleErrorResponse(new Error("Network error", { cause: error }), requestMap);
|
|
16
|
+
return;
|
|
27
17
|
}
|
|
28
18
|
if (!res.ok) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
success: false,
|
|
32
|
-
error: new Error(`HTTP error: ${res.status}`)
|
|
33
|
-
}));
|
|
19
|
+
this.handleErrorResponse(new Error(`HTTP error: ${res.status}`), requestMap);
|
|
20
|
+
return;
|
|
34
21
|
}
|
|
35
22
|
try {
|
|
36
23
|
const jsonRpcResponse = await res.json();
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return request.map((r) => ({
|
|
40
|
-
id: r.id,
|
|
41
|
-
success: false,
|
|
42
|
-
error: new Error("Invalid JSON response", { cause })
|
|
43
|
-
}));
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
sendRequest(method, ...params) {
|
|
47
|
-
const deferred = deferredPromise();
|
|
48
|
-
const body = this.formatRequest(method, params);
|
|
49
|
-
this.requestMap.set(body.id, { deferred, body, method });
|
|
50
|
-
if (this.timer) clearTimeout(this.timer);
|
|
51
|
-
this.timer = setTimeout(() => this.sendBatch(), this.batchDuration);
|
|
52
|
-
return deferred.promise;
|
|
53
|
-
}
|
|
54
|
-
async sendBatch() {
|
|
55
|
-
const clonedMap = new Map(this.requestMap);
|
|
56
|
-
this.requestMap.clear();
|
|
57
|
-
const requests = [...clonedMap.values()].map((item) => item.body);
|
|
58
|
-
const responses = await this.send(requests);
|
|
59
|
-
for (const response of responses) {
|
|
60
|
-
const clonedItem = clonedMap.get(response.id);
|
|
61
|
-
if (!clonedItem) {
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
if (!response.success) {
|
|
65
|
-
clonedItem.deferred.reject(response.error);
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
try {
|
|
69
|
-
const parseResult = this.parseSingleResponse(response);
|
|
70
|
-
clonedItem.deferred.resolve(rpcResult[clonedItem.method].parse(parseResult.result));
|
|
71
|
-
} catch (e) {
|
|
72
|
-
clonedItem.deferred.reject(e);
|
|
73
|
-
} finally {
|
|
74
|
-
clonedMap.delete(response.id);
|
|
24
|
+
for (const r of jsonRpcResponse) {
|
|
25
|
+
this.handleResponse({ id: r.id, success: true, result: r }, requestMap);
|
|
75
26
|
}
|
|
27
|
+
} catch (cause) {
|
|
28
|
+
this.handleErrorResponse(new Error("Invalid JSON response", { cause }), requestMap);
|
|
76
29
|
}
|
|
77
|
-
clonedMap.forEach((item) => {
|
|
78
|
-
item.deferred.reject(new Error("Could not find the result for the request"));
|
|
79
|
-
});
|
|
80
30
|
}
|
|
81
31
|
};
|
|
82
32
|
export {
|