core-3nweb-client-lib 0.43.5 → 0.43.6

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.
@@ -1,6 +1,7 @@
1
1
  import { Observable } from 'rxjs';
2
2
  import { SubscribingClient } from './generic-ipc';
3
3
  import * as WebSocket from 'ws';
4
+ import type { ConnectException, HTTPException } from '../exceptions/http';
4
5
  export { RequestEnvelope, RequestHandler, EventfulServer, makeEventfulServer, SubscribingClient } from './generic-ipc';
5
6
  export interface WSException extends web3n.RuntimeException {
6
7
  type: 'websocket';
@@ -26,3 +27,14 @@ export declare function makeSubscriber(ws: WebSocket, ipcChannel: string | undef
26
27
  heartbeat: Observable<ConnectionStatus>;
27
28
  };
28
29
  export declare function addToStatus<T extends ConnectionStatus>(status: ConnectionStatus, params: Partial<T>): T;
30
+ export declare function shouldRestartWSAfterErr(exc: WSException | ConnectException | HTTPException): boolean;
31
+ export declare class WebSocketListening {
32
+ readonly restartWaitSecs: number;
33
+ private readonly startWSProc;
34
+ private listeningProc;
35
+ constructor(restartWaitSecs: number, startWSProc: () => Observable<unknown>);
36
+ startListening(): void;
37
+ private sleepAndRestart;
38
+ close(): void;
39
+ get isListening(): boolean;
40
+ }
@@ -15,13 +15,15 @@
15
15
  You should have received a copy of the GNU General Public License along with
16
16
  this program. If not, see <http://www.gnu.org/licenses/>. */
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.makeEventfulServer = void 0;
18
+ exports.WebSocketListening = exports.makeEventfulServer = void 0;
19
19
  exports.makeWSException = makeWSException;
20
20
  exports.makeSubscriber = makeSubscriber;
21
21
  exports.addToStatus = addToStatus;
22
+ exports.shouldRestartWSAfterErr = shouldRestartWSAfterErr;
22
23
  const rxjs_1 = require("rxjs");
23
24
  const runtime_1 = require("../exceptions/runtime");
24
25
  const generic_ipc_1 = require("./generic-ipc");
26
+ const sleep_1 = require("../processes/sleep");
25
27
  var generic_ipc_2 = require("./generic-ipc");
26
28
  Object.defineProperty(exports, "makeEventfulServer", { enumerable: true, get: function () { return generic_ipc_2.makeEventfulServer; } });
27
29
  function makeWSException(params, flags) {
@@ -127,4 +129,67 @@ function addToStatus(status, params) {
127
129
  }
128
130
  return status;
129
131
  }
132
+ function shouldRestartWSAfterErr(exc) {
133
+ if (!exc.runtimeException) {
134
+ return false;
135
+ }
136
+ if (exc.type === 'connect') {
137
+ return true;
138
+ }
139
+ else if (exc.type === 'http-request') {
140
+ return false;
141
+ }
142
+ else if (exc.type === 'websocket') {
143
+ return true;
144
+ }
145
+ else {
146
+ return false;
147
+ }
148
+ }
149
+ class WebSocketListening {
150
+ constructor(restartWaitSecs, startWSProc) {
151
+ this.restartWaitSecs = restartWaitSecs;
152
+ this.startWSProc = startWSProc;
153
+ this.listeningProc = undefined;
154
+ Object.seal(this);
155
+ }
156
+ startListening() {
157
+ if (this.listeningProc) {
158
+ return;
159
+ }
160
+ const clearListeningProc = () => {
161
+ if (this.listeningProc === sub) {
162
+ this.listeningProc = undefined;
163
+ }
164
+ };
165
+ const sub = this.startWSProc()
166
+ .subscribe({
167
+ complete: () => {
168
+ clearListeningProc();
169
+ },
170
+ error: async (exc) => {
171
+ clearListeningProc();
172
+ if (shouldRestartWSAfterErr(exc)) {
173
+ this.sleepAndRestart();
174
+ }
175
+ }
176
+ });
177
+ this.listeningProc = sub;
178
+ }
179
+ async sleepAndRestart() {
180
+ await (0, sleep_1.sleep)(this.restartWaitSecs * 1000);
181
+ this.startListening();
182
+ }
183
+ close() {
184
+ var _a;
185
+ (_a = this.listeningProc) === null || _a === void 0 ? void 0 : _a.unsubscribe();
186
+ this.listeningProc = undefined;
187
+ }
188
+ get isListening() {
189
+ return !!this.listeningProc;
190
+ }
191
+ }
192
+ exports.WebSocketListening = WebSocketListening;
193
+ Object.freeze(WebSocketListening.prototype);
194
+ Object.freeze(WebSocketListening);
130
195
  Object.freeze(exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "core-3nweb-client-lib",
3
- "version": "0.43.5",
3
+ "version": "0.43.6",
4
4
  "description": "3NWeb client core library, embeddable into different environments",
5
5
  "main": "build/lib-index.js",
6
6
  "types": "build/lib-index.d.ts",