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