@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 CHANGED
@@ -1,45 +1,77 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/Client.ts
2
- var _crypto = require('crypto');
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
- return _crypto.randomUUID.call(void 0, );
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
- parseSingleResponse(response) {
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
- throw response.error;
28
+ return clonedItem.deferred.reject(response.error);
17
29
  }
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}`
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
- if (parseResult.data.result) {
28
- return parseResult.data;
36
+ const parseResult = _commoncjs.rpcResult[clonedItem.method].safeParse(rpcResponse.result);
37
+ if (parseResult.error) {
38
+ return clonedItem.deferred.reject(parseResult.error);
29
39
  }
30
- throw new Error("Malformed RPC response received");
40
+ clonedItem.deferred.resolve(parseResult.data);
31
41
  }
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);
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 { RpcMethod, JsonRpcRequest, RpcRequest, RpcResult } from './common.cjs';
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: unknown;
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<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
- };
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 { RpcMethod, JsonRpcRequest, RpcRequest, RpcResult } from './common.js';
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: unknown;
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<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
- };
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 { randomUUID } from "crypto";
3
- import { rpcResult, rpcResponse } from "./common.mjs";
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
- return randomUUID();
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
- parseSingleResponse(response) {
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
- throw response.error;
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
- 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
- if (parseResult.data.result) {
28
- return parseResult.data;
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
- 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);
69
+ return deferred.promise;
38
70
  }
39
71
  methods() {
40
72
  return Object.keys(rpcResult).sort();
@@ -1,76 +1,34 @@
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');
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) {
12
- const res = await fetch(this.url, {
13
- body: JSON.stringify(request),
14
- method: "POST",
15
- headers: {
16
- "Content-Type": "application/json"
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
- return request.map((r) => ({
21
- id: r.id,
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
- return jsonRpcResponse.map((r) => ({ id: r.id, success: true, result: r }));
29
- } catch (cause) {
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
- }, _class);
31
+ };
74
32
 
75
33
 
76
34
  exports.default = HttpClient;
@@ -1,16 +1,12 @@
1
- import Client, { Response } from './Client.cjs';
2
- import { RpcMethod, JsonRpcRequest, RpcRequest, RpcResult } from './common.cjs';
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
- 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;
9
+ protected send<const T extends RpcMethod>(request: JsonRpcRequest<T>[], requestMap: RequestMap): Promise<void>;
14
10
  }
15
11
 
16
12
  export { HttpClient as default };
@@ -1,16 +1,12 @@
1
- import Client, { Response } from './Client.js';
2
- import { RpcMethod, JsonRpcRequest, RpcRequest, RpcResult } from './common.js';
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
- 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;
9
+ protected send<const T extends RpcMethod>(request: JsonRpcRequest<T>[], requestMap: RequestMap): Promise<void>;
14
10
  }
15
11
 
16
12
  export { HttpClient as default };
@@ -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
- 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
- });
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
- return request.map((r) => ({
21
- id: r.id,
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
- return jsonRpcResponse.map((r) => ({ id: r.id, success: true, result: r }));
29
- } catch (cause) {
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 {