@bithomp/xrpl-api 3.1.6 → 3.1.7

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.
@@ -3,6 +3,7 @@ export type SortDirection = -1 | 0 | 1;
3
3
  export declare function signum(num: number): SortDirection;
4
4
  export declare function compareTransactions(first: any, second: any): SortDirection;
5
5
  export declare function sleep(ms: number): Promise<void>;
6
+ export declare function getTimestamp(): number;
6
7
  export declare function createMarker(hash: string, marker?: any): any;
7
8
  export declare function parseMarker(marker?: any): any;
8
9
  export declare function dropsToXrp(drops: BigNumber.Value): string;
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.signum = signum;
7
7
  exports.compareTransactions = compareTransactions;
8
8
  exports.sleep = sleep;
9
+ exports.getTimestamp = getTimestamp;
9
10
  exports.createMarker = createMarker;
10
11
  exports.parseMarker = parseMarker;
11
12
  exports.dropsToXrp = dropsToXrp;
@@ -32,6 +33,9 @@ async function sleep(ms) {
32
33
  setTimeout(resolve, ms);
33
34
  });
34
35
  }
36
+ function getTimestamp() {
37
+ return new Date().getTime();
38
+ }
35
39
  function createMarker(hash, marker) {
36
40
  if (marker === undefined) {
37
41
  return undefined;
package/lib/connection.js CHANGED
@@ -88,20 +88,26 @@ class Connection extends events_1.EventEmitter {
88
88
  (request.command === "subscribe" || request.command === "unsubscribe")) {
89
89
  return this.updateSubscriptions(request);
90
90
  }
91
- if (!this.client || !this.isConnected()) {
92
- return { error: "Not connected" };
91
+ const waitTime = (0, utils_1.getTimestamp)() + RECONNECT_TIMEOUT;
92
+ while ((!this.client || !this.isConnected())) {
93
+ await (0, utils_1.sleep)(100);
94
+ if (this.shutdown) {
95
+ return { error: "shutdownConnection", error_message: "Connection is shutdown.", status: "error" };
96
+ }
97
+ if ((0, utils_1.getTimestamp)() > waitTime) {
98
+ return { error: "notConnected", error_message: "Not connected.", status: "error" };
99
+ }
93
100
  }
94
- const startDate = new Date();
101
+ const startTimestamp = (0, utils_1.getTimestamp)();
95
102
  if (this.apiVersion && !request.hasOwnProperty("api_version") && exports.DEFAULT_API_VERSION !== this.apiVersion) {
96
103
  request.api_version = this.apiVersion;
97
104
  }
98
105
  const response = await this.client.connection.request(request);
99
- const endDate = new Date();
100
- this.updateLatency(endDate.getTime() - startDate.getTime());
106
+ this.updateLatency((0, utils_1.getTimestamp)() - startTimestamp);
101
107
  return response;
102
108
  }
103
109
  catch (err) {
104
- this.updateLatency(1000);
110
+ this.updateLatency(RECONNECT_TIMEOUT);
105
111
  this.logger?.debug({
106
112
  service: "Bithomp::XRPL::Connection",
107
113
  function: "request",
@@ -144,7 +150,7 @@ class Connection extends events_1.EventEmitter {
144
150
  }
145
151
  getOnlinePeriodMs() {
146
152
  if (this.isConnected()) {
147
- return this.onlineSince ? new Date().getTime() - this.onlineSince : 0;
153
+ return this.onlineSince ? (0, utils_1.getTimestamp)() - this.onlineSince : 0;
148
154
  }
149
155
  return null;
150
156
  }
@@ -229,7 +235,7 @@ class Connection extends events_1.EventEmitter {
229
235
  url: this.url,
230
236
  });
231
237
  this.emit("connected");
232
- this.onlineSince = new Date().getTime();
238
+ this.onlineSince = (0, utils_1.getTimestamp)();
233
239
  });
234
240
  this.client.connection.on("disconnected", (code) => {
235
241
  this.logger?.debug({
@@ -383,7 +389,7 @@ class Connection extends events_1.EventEmitter {
383
389
  return await this.request(request, { skip_subscription_update: true });
384
390
  }
385
391
  onLedgerClosed(ledgerStream) {
386
- const time = new Date().getTime();
392
+ const time = (0, utils_1.getTimestamp)();
387
393
  const ledgerTime = (0, ledger_1.ledgerTimeToTimestamp)(ledgerStream.ledger_time);
388
394
  if (ledgerTime < time) {
389
395
  this.updateLatency(time - ledgerTime);
@@ -387,7 +387,7 @@ async function waitForFinalTransactionOutcome(txHash, lastLedger) {
387
387
  await (0, utils_1.sleep)(LEDGER_CLOSE_TIME_AWAIT);
388
388
  const tx = await getTransaction(txHash);
389
389
  const error = tx?.error;
390
- if (error === "Not connected") {
390
+ if (error === "notConnected") {
391
391
  return tx;
392
392
  }
393
393
  if (!tx || error === "txnNotFound" || tx.validated !== true) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bithomp/xrpl-api",
3
- "version": "3.1.6",
3
+ "version": "3.1.7",
4
4
  "description": "A Bithomp JavaScript/TypeScript library for interacting with the XRP Ledger",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -76,7 +76,7 @@
76
76
  "eslint-config-prettier": "^9.1.0",
77
77
  "eslint-plugin-chai-friendly": "^1.0.1",
78
78
  "eslint-plugin-import": "^2.30.0",
79
- "eslint-plugin-n": "^17.10.2",
79
+ "eslint-plugin-n": "^17.10.3",
80
80
  "eslint-plugin-promise": "^7.1.0",
81
81
  "mocha": "^10.7.3",
82
82
  "nconf": "^0.12.1",