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