@bithomp/xrpl-api 3.7.3 → 3.7.4

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.
Files changed (2) hide show
  1. package/lib/connection.js +38 -27
  2. package/package.json +2 -2
package/lib/connection.js CHANGED
@@ -102,8 +102,9 @@ class Connection extends events_1.EventEmitter {
102
102
  url: this.url,
103
103
  error: err?.message || err?.name || err,
104
104
  });
105
+ this.removeClient();
105
106
  }
106
- this.connectionValidation();
107
+ this.connectionValidation("connect");
107
108
  }
108
109
  async disconnect() {
109
110
  this.logger?.debug({
@@ -125,7 +126,16 @@ class Connection extends events_1.EventEmitter {
125
126
  const result = await this._request(request, options);
126
127
  let validResponse = true;
127
128
  if (result?.error) {
128
- if (result.error === "timeout") {
129
+ if (result.error === "slowDown" || result.error === "Unexpected server response: 429") {
130
+ this.logger?.debug({
131
+ service: "Bithomp::XRPL::Connection",
132
+ function: "request",
133
+ url: this.url,
134
+ error: `Received slowdown error, reconnecting...`,
135
+ });
136
+ validResponse = false;
137
+ }
138
+ else if (result.error === "timeout") {
129
139
  const timeouts = this.latency.filter((info) => info.delta >= this.timeout).length;
130
140
  if (timeouts >= 3) {
131
141
  this.logger?.debug({
@@ -135,10 +145,9 @@ class Connection extends events_1.EventEmitter {
135
145
  error: `Too many timeouts (${timeouts}) in last ${this.latency.length} requests, reconnecting...`,
136
146
  });
137
147
  validResponse = false;
138
- this.removeClient();
139
148
  }
140
149
  }
141
- else if (result.error.startsWith("websocket was closed")) {
150
+ else if (result.error.toLowerCase().startsWith("websocket was closed")) {
142
151
  this.logger?.debug({
143
152
  service: "Bithomp::XRPL::Connection",
144
153
  function: "request",
@@ -146,11 +155,13 @@ class Connection extends events_1.EventEmitter {
146
155
  error: "Websocket was closed, reconnecting...",
147
156
  });
148
157
  validResponse = false;
149
- this.removeClient();
150
158
  }
151
159
  }
152
160
  if (validResponse) {
153
- this.connectionValidation();
161
+ this.connectionValidation("request");
162
+ }
163
+ else {
164
+ this.removeClient();
154
165
  }
155
166
  return result;
156
167
  }
@@ -294,7 +305,6 @@ class Connection extends events_1.EventEmitter {
294
305
  if (!this.shutdown) {
295
306
  this.emit("reconnect");
296
307
  try {
297
- this.emit("disconnected", 1000);
298
308
  this.removeClient();
299
309
  this.resetTypes();
300
310
  this.serverInfoUpdating = false;
@@ -311,15 +321,16 @@ class Connection extends events_1.EventEmitter {
311
321
  error: e.message,
312
322
  });
313
323
  }
314
- this.connectionValidation();
324
+ this.connectionValidation("reconnect");
315
325
  }
316
326
  }
317
- removeClient() {
327
+ removeClient(code = 1000) {
318
328
  try {
319
329
  if (this.client) {
320
330
  this.client.removeAllListeners();
321
331
  this.client.disconnect();
322
332
  this.client = undefined;
333
+ this.emit("disconnected", code);
323
334
  }
324
335
  }
325
336
  catch (_err) {
@@ -345,9 +356,10 @@ class Connection extends events_1.EventEmitter {
345
356
  code,
346
357
  url: this.url,
347
358
  });
348
- this.onlineSince = 0;
349
- this.serverInfo = null;
350
- this.streamsSubscribed = false;
359
+ if (this.client) {
360
+ this.client.removeAllListeners();
361
+ this.client = undefined;
362
+ }
351
363
  this.emit("disconnected", code);
352
364
  });
353
365
  this.client.on("error", (source, message, error) => {
@@ -369,7 +381,6 @@ class Connection extends events_1.EventEmitter {
369
381
  error: err?.message || err?.name || err,
370
382
  });
371
383
  }
372
- this.connectionValidation();
373
384
  });
374
385
  this.client.on("ledgerClosed", (ledgerStream) => {
375
386
  this.onLedgerClosed(ledgerStream);
@@ -484,7 +495,7 @@ class Connection extends events_1.EventEmitter {
484
495
  request.accounts = accounts;
485
496
  }
486
497
  const result = await this.request(request, { skip_subscription_update: true });
487
- if (result.result) {
498
+ if (result.result && !result.error) {
488
499
  this.streamsSubscribed = true;
489
500
  }
490
501
  return result;
@@ -528,7 +539,7 @@ class Connection extends events_1.EventEmitter {
528
539
  else {
529
540
  this.updateServerInfo();
530
541
  }
531
- this.connectionValidation();
542
+ this.connectionValidation("ledgerClosed");
532
543
  }
533
544
  async updateServerInfo() {
534
545
  if (this.serverInfoUpdating || this.shutdown) {
@@ -556,10 +567,11 @@ class Connection extends events_1.EventEmitter {
556
567
  }
557
568
  this.serverInfoUpdating = false;
558
569
  }
559
- connectionValidation() {
570
+ connectionValidation(event) {
560
571
  this.logger?.debug({
561
572
  service: "Bithomp::XRPL::Connection",
562
573
  function: "connectionValidation",
574
+ event,
563
575
  url: this.url,
564
576
  shutdown: this.shutdown,
565
577
  });
@@ -567,17 +579,16 @@ class Connection extends events_1.EventEmitter {
567
579
  clearTimeout(this.connectionWatchTimer);
568
580
  this.connectionWatchTimer = null;
569
581
  }
570
- if (!this.shutdown) {
571
- if (this.streamsSubscribed === false) {
572
- this.subscribe();
573
- }
574
- if (this.serverInfo === null && this.isConnected()) {
575
- this.updateServerInfo();
576
- }
577
- this.connectionWatchTimer = setTimeout(this.bindConnectionWatchTimeout, LEDGER_CLOSED_TIMEOUT);
578
- }
579
- else {
582
+ if (this.shutdown) {
580
583
  this.removeClient();
584
+ return;
585
+ }
586
+ this.connectionWatchTimer = setTimeout(this.bindConnectionWatchTimeout, LEDGER_CLOSED_TIMEOUT);
587
+ if (this.streamsSubscribed === false) {
588
+ this.subscribe();
589
+ }
590
+ if (this.serverInfo === null && this.isConnected()) {
591
+ this.updateServerInfo();
581
592
  }
582
593
  }
583
594
  async connectionWatchTimeout() {
@@ -600,7 +611,7 @@ class Connection extends events_1.EventEmitter {
600
611
  url: this.url,
601
612
  error: e.message,
602
613
  });
603
- this.connectionValidation();
614
+ this.connectionValidation("connectionWatchTimeout");
604
615
  }
605
616
  }
606
617
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bithomp/xrpl-api",
3
- "version": "3.7.3",
3
+ "version": "3.7.4",
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",
@@ -42,7 +42,7 @@
42
42
  "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
43
43
  "lint": "eslint",
44
44
  "prepare": "npm run build",
45
- "prepublishOnly": "npm test && npm run lint",
45
+ "-prepublishOnly": "npm test && npm run lint",
46
46
  "preversion": "npm run lint",
47
47
  "version": "npm run format && git add -A src",
48
48
  "postversion": "git push && git push --tags"