@chainflip/rpc 1.6.9 → 1.6.10

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,12 +1,13 @@
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 _assertion = require('@chainflip/utils/assertion');
3
3
  var _commoncjs = require('./common.cjs');
4
- var Client = class {
5
- constructor(url) {
4
+ var Client = (_class = class {
5
+ constructor(url) {;_class.prototype.__init.call(this);
6
6
  this.url = url;
7
7
  }
8
+ __init() {this.lastRequestId = 0}
8
9
  getRequestId() {
9
- return _crypto.randomUUID.call(void 0, );
10
+ return String(++this.lastRequestId);
10
11
  }
11
12
  formatRequest(method, params) {
12
13
  return { jsonrpc: "2.0", id: this.getRequestId(), method, params };
@@ -24,22 +25,19 @@ var Client = class {
24
25
  `RPC error [${parseResult.data.error.code}]: ${parseResult.data.error.message}`
25
26
  );
26
27
  }
27
- if (parseResult.data.result) {
28
- return parseResult.data;
29
- }
30
- throw new Error("Malformed RPC response received");
28
+ _assertion.assert.call(void 0, "result" in parseResult.data);
29
+ return parseResult.data;
31
30
  }
32
31
  async sendRequest(method, ...params) {
33
32
  const [response] = await this.send([this.formatRequest(method, params)]);
34
- if (!response.success)
35
- throw response.error;
33
+ if (!response.success) throw response.error;
36
34
  const parseResult = this.parseSingleResponse(response);
37
35
  return _commoncjs.rpcResult[method].parse(parseResult.result);
38
36
  }
39
37
  methods() {
40
38
  return Object.keys(_commoncjs.rpcResult).sort();
41
39
  }
42
- };
40
+ }, _class);
43
41
 
44
42
 
45
43
  exports.default = Client;
package/dist/Client.d.cts CHANGED
@@ -14,9 +14,10 @@ type Response = {
14
14
  };
15
15
  declare abstract class Client {
16
16
  protected readonly url: string;
17
+ private lastRequestId;
17
18
  constructor(url: string);
18
19
  protected abstract send<const T extends RpcMethod>(data: JsonRpcRequest<T>[]): Promise<Response[]>;
19
- protected getRequestId(): `${string}-${string}-${string}-${string}-${string}`;
20
+ protected getRequestId(): string;
20
21
  protected formatRequest<T extends RpcMethod>(method: T, params: RpcRequest[T]): JsonRpcRequest<T>;
21
22
  protected parseSingleResponse(response: Response): {
22
23
  id: string | number;
package/dist/Client.d.ts CHANGED
@@ -14,9 +14,10 @@ type Response = {
14
14
  };
15
15
  declare abstract class Client {
16
16
  protected readonly url: string;
17
+ private lastRequestId;
17
18
  constructor(url: string);
18
19
  protected abstract send<const T extends RpcMethod>(data: JsonRpcRequest<T>[]): Promise<Response[]>;
19
- protected getRequestId(): `${string}-${string}-${string}-${string}-${string}`;
20
+ protected getRequestId(): string;
20
21
  protected formatRequest<T extends RpcMethod>(method: T, params: RpcRequest[T]): JsonRpcRequest<T>;
21
22
  protected parseSingleResponse(response: Response): {
22
23
  id: string | number;
package/dist/Client.mjs CHANGED
@@ -1,12 +1,13 @@
1
1
  // src/Client.ts
2
- import { randomUUID } from "crypto";
2
+ import { assert } from "@chainflip/utils/assertion";
3
3
  import { rpcResult, rpcResponse } from "./common.mjs";
4
4
  var Client = class {
5
5
  constructor(url) {
6
6
  this.url = url;
7
7
  }
8
+ lastRequestId = 0;
8
9
  getRequestId() {
9
- return randomUUID();
10
+ return String(++this.lastRequestId);
10
11
  }
11
12
  formatRequest(method, params) {
12
13
  return { jsonrpc: "2.0", id: this.getRequestId(), method, params };
@@ -24,15 +25,12 @@ var Client = class {
24
25
  `RPC error [${parseResult.data.error.code}]: ${parseResult.data.error.message}`
25
26
  );
26
27
  }
27
- if (parseResult.data.result) {
28
- return parseResult.data;
29
- }
30
- throw new Error("Malformed RPC response received");
28
+ assert("result" in parseResult.data);
29
+ return parseResult.data;
31
30
  }
32
31
  async sendRequest(method, ...params) {
33
32
  const [response] = await this.send([this.formatRequest(method, params)]);
34
- if (!response.success)
35
- throw response.error;
33
+ if (!response.success) throw response.error;
36
34
  const parseResult = this.parseSingleResponse(response);
37
35
  return rpcResult[method].parse(parseResult.result);
38
36
  }
@@ -38,8 +38,7 @@ var HttpClient = (_class = class extends _Clientcjs2.default {constructor(...arg
38
38
  const deferred = _async.deferredPromise.call(void 0, );
39
39
  const body = this.formatRequest(method, params);
40
40
  this.requestMap.set(body.id, { deferred, body, method });
41
- if (this.timer)
42
- clearTimeout(this.timer);
41
+ if (this.timer) clearTimeout(this.timer);
43
42
  this.timer = setTimeout(() => this.sendBatch(), this.batchDuration);
44
43
  return deferred.promise;
45
44
  }
@@ -38,8 +38,7 @@ var HttpClient = class extends Client {
38
38
  const deferred = deferredPromise();
39
39
  const body = this.formatRequest(method, params);
40
40
  this.requestMap.set(body.id, { deferred, body, method });
41
- if (this.timer)
42
- clearTimeout(this.timer);
41
+ if (this.timer) clearTimeout(this.timer);
43
42
  this.timer = setTimeout(() => this.sendBatch(), this.batchDuration);
44
43
  return deferred.promise;
45
44
  }
package/dist/WsClient.cjs CHANGED
@@ -17,8 +17,7 @@ var WsClient = (_class = class extends _Clientcjs2.default {
17
17
  await this.handleClose();
18
18
  }
19
19
  async handleClose() {
20
- if (!this.ws)
21
- return;
20
+ if (!this.ws) return;
22
21
  this.ws.removeEventListener("close", this.handleDisconnect);
23
22
  this.ws.close();
24
23
  if (this.ws.readyState !== this.WebSocket.CLOSED) {
@@ -49,8 +48,7 @@ var WsClient = (_class = class extends _Clientcjs2.default {
49
48
  __init5() {this.handleMessage = (data) => {
50
49
  const parsedData = JSON.parse(data.data);
51
50
  const response = _commoncjs.rpcResponse.safeParse(parsedData);
52
- if (!response.success)
53
- return;
51
+ if (!response.success) return;
54
52
  const { id } = response.data;
55
53
  _optionalChain([this, 'access', _6 => _6.requestMap, 'access', _7 => _7.get, 'call', _8 => _8(id), 'optionalAccess', _9 => _9.resolve, 'call', _10 => _10(response.data)]);
56
54
  }}
package/dist/WsClient.mjs CHANGED
@@ -17,8 +17,7 @@ var WsClient = class extends Client {
17
17
  await this.handleClose();
18
18
  }
19
19
  async handleClose() {
20
- if (!this.ws)
21
- return;
20
+ if (!this.ws) return;
22
21
  this.ws.removeEventListener("close", this.handleDisconnect);
23
22
  this.ws.close();
24
23
  if (this.ws.readyState !== this.WebSocket.CLOSED) {
@@ -49,8 +48,7 @@ var WsClient = class extends Client {
49
48
  handleMessage = (data) => {
50
49
  const parsedData = JSON.parse(data.data);
51
50
  const response = rpcResponse.safeParse(parsedData);
52
- if (!response.success)
53
- return;
51
+ if (!response.success) return;
54
52
  const { id } = response.data;
55
53
  this.requestMap.get(id)?.resolve(response.data);
56
54
  };