@nsshunt/stsfhirclient 2.0.36 → 2.0.37

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.
@@ -10245,7 +10245,6 @@ var globalThisShim = (() => {
10245
10245
  else return Function("return this")();
10246
10246
  })();
10247
10247
  var defaultBinaryType = "arraybuffer";
10248
- function createCookieJar() {}
10249
10248
  //#endregion
10250
10249
  //#region node_modules/engine.io-client/build/esm/util.js
10251
10250
  function pick(obj, ...attr) {
@@ -11112,7 +11111,7 @@ var SocketWithoutUpgrade = class SocketWithoutUpgrade extends Emitter {
11112
11111
  OFFLINE_EVENT_LISTENERS.push(this._offlineEventListener);
11113
11112
  }
11114
11113
  }
11115
- if (this.opts.withCredentials) this._cookieJar = /* @__PURE__ */ createCookieJar();
11114
+ if (this.opts.withCredentials) this._cookieJar = void 0;
11116
11115
  this._open();
11117
11116
  }
11118
11117
  /**
@@ -13185,6 +13184,7 @@ var SocketIoClient = class extends import_tiny_emitter.TinyEmitter {
13185
13184
  #socket;
13186
13185
  #reconnectTimeout = 2e3;
13187
13186
  #ackTimeout = 5e3;
13187
+ #reconnectTimeoutVal = void 0;
13188
13188
  constructor(name) {
13189
13189
  super();
13190
13190
  this.#name = name;
@@ -13257,7 +13257,7 @@ var SocketIoClient = class extends import_tiny_emitter.TinyEmitter {
13257
13257
  return this;
13258
13258
  }
13259
13259
  SetupSocket() {
13260
- if (!this.#address) throw new Error(`SocketIoClientHelper:SetupSocket(): Error: [address not provided]`);
13260
+ if (!this.#address) throw new Error(`SocketIoClient:SetupSocket(): Error: [address not provided]`);
13261
13261
  this.#EstablishSocketConnect();
13262
13262
  return this;
13263
13263
  }
@@ -13267,17 +13267,20 @@ var SocketIoClient = class extends import_tiny_emitter.TinyEmitter {
13267
13267
  EngineReconnect(attempt) {}
13268
13268
  #EstablishSocketConnect() {
13269
13269
  if (this.#socket !== void 0) {
13270
- if (this.#socket.connected === true) this.#socket.disconnect();
13271
- this.#socket = void 0;
13272
- if (isNode) setTimeout(() => this.#EstablishSocketConnect(), this.#reconnectTimeout).unref();
13273
- else setTimeout(() => this.#EstablishSocketConnect(), this.#reconnectTimeout);
13270
+ this.ResetSocket();
13271
+ if (isNode) this.#reconnectTimeoutVal = setTimeout(() => this.#EstablishSocketConnect(), this.#reconnectTimeout).unref();
13272
+ else this.#reconnectTimeoutVal = setTimeout(() => this.#EstablishSocketConnect(), this.#reconnectTimeout);
13274
13273
  return;
13275
13274
  }
13276
13275
  let socketOptions;
13277
13276
  if (isNode) {
13278
13277
  socketOptions = { transports: ["websocket"] };
13279
13278
  if (this.#agentManager) {
13280
- if (!this.#address) throw new Error(`SocketIoClient:SetupSocket(): Error: [address not provided when using agentManager]`);
13279
+ if (!this.#address) {
13280
+ const message = `SocketIoClient:#EstablishSocketConnect(): Error: [address not provided when using agentManager]`;
13281
+ this.LogErrorMessage(message);
13282
+ throw new Error(message);
13283
+ }
13281
13284
  socketOptions.agent = this.#agentManager.GetAgent(this.#address);
13282
13285
  }
13283
13286
  } else socketOptions = { transports: ["websocket"] };
@@ -13288,30 +13291,30 @@ var SocketIoClient = class extends import_tiny_emitter.TinyEmitter {
13288
13291
  if (this.#socketIoCustomPath && this.#socketIoCustomPath.localeCompare("") !== 0) socketOptions.path = this.#socketIoCustomPath;
13289
13292
  this.#socket = lookup(this.#address, socketOptions);
13290
13293
  this.#socket.io.on("error", (err) => {
13291
- this.LogErrorMessage(`socketDetail.socket.io.on('error'): [${err}] Address: [${this.#address}]`);
13294
+ this.LogErrorMessage(`SocketIoClient.socket.io.on('error'): [${err}] Address: [${this.#address}]`);
13292
13295
  this.EngineError(err);
13293
13296
  });
13294
13297
  this.#socket.io.on("reconnect_error", (err) => {
13295
- this.LogErrorMessage(`socketDetail.socket.io.on('reconnect_error'): [${err}] Address: [${this.#address}]`);
13298
+ this.LogErrorMessage(`SocketIoClient.socket.io.on('reconnect_error'): [${err}] Address: [${this.#address}]`);
13296
13299
  this.EngineReconnectError(err);
13297
13300
  });
13298
13301
  this.#socket.on("connect_error", (err) => {
13299
- this.LogErrorMessage(`socketDetail.socket.on('connect_error'): [${err}] Address: [${this.#address}]`);
13302
+ this.LogErrorMessage(`SocketIoClient.socket.on('connect_error'): [${err}] Address: [${this.#address}]`);
13300
13303
  this.EngineConnectError(err);
13301
13304
  });
13302
13305
  this.#socket.io.on("reconnect", (attempt) => {
13303
- this.LogErrorMessage(`socketDetail.socket.io.on('reconnect'): Number: [${attempt}] Address: [${this.#address}]`);
13306
+ this.LogErrorMessage(`SocketIoClient:socket.io.on('reconnect'): Number: [${attempt}] Address: [${this.#address}]`);
13304
13307
  this.EngineReconnect(attempt);
13305
13308
  });
13306
13309
  this.#socket.on("connect", () => {
13307
13310
  if (this.#socket) {
13308
- this.LogDebugMessage(`Socket: [${this.#socket.id}]: connected, Address: [${this.#address}]`);
13311
+ this.LogDebugMessage(`SocketIoClient:on("connect"): Socket: [${this.#socket.id}]: connected, Address: [${this.#address}]`);
13309
13312
  setTimeout(() => {
13310
13313
  this.SocketConnect(this.#socket);
13311
13314
  }, 0);
13312
13315
  this.SetupSocketEvents(this.#socket);
13313
13316
  } else {
13314
- const errorMessage = "Could not get socket object from socket.io, Address: [${socketDetail.address}]";
13317
+ const errorMessage = "SocketIoClient:on(\"connect\"): Could not get socket object from socket.io, Address: [${socketDetail.address}]";
13315
13318
  this.LogErrorMessage(errorMessage);
13316
13319
  this.SocketConnectError(new Error(errorMessage));
13317
13320
  }
@@ -13321,33 +13324,50 @@ var SocketIoClient = class extends import_tiny_emitter.TinyEmitter {
13321
13324
  this.SocketDisconnect(reason);
13322
13325
  switch (reason) {
13323
13326
  case "io server disconnect":
13324
- this.LogDebugMessage("The server disconnected using disconnectSockets, i.e. normal safe shutdown from explicit disconnection by the server.");
13325
- this.LogDebugMessage("The connection will be re-established when the server becomes available.");
13326
- this.#socket = void 0;
13327
- if (isNode) {
13328
- if (this.#agentManager) this.#agentManager.ResetAgent();
13329
- setTimeout(() => this.#EstablishSocketConnect(), this.#reconnectTimeout).unref();
13330
- } else setTimeout(() => this.#EstablishSocketConnect(), this.#reconnectTimeout);
13327
+ this.LogDebugMessage("SocketIoClient:on(\"disconnect\"): The server disconnected using disconnectSockets, i.e. normal safe shutdown from explicit disconnection by the server.");
13328
+ this.LogDebugMessage("SocketIoClient:on(\"disconnect\"): The connection will be re-established when the server becomes available.");
13329
+ this.ResetSocket();
13330
+ if (isNode) this.#reconnectTimeoutVal = setTimeout(() => this.#EstablishSocketConnect(), this.#reconnectTimeout).unref();
13331
+ else this.#reconnectTimeoutVal = setTimeout(() => this.#EstablishSocketConnect(), this.#reconnectTimeout);
13331
13332
  break;
13332
13333
  case "io client disconnect":
13333
- this.LogDebugMessage("The client disconnected using disconnectSockets, i.e. normal safe disconnection from explicit disconnection by the client.");
13334
- this.LogDebugMessage("The connection will not be re-established automatically.");
13334
+ this.LogDebugMessage("SocketIoClient:on(\"disconnect\"): The client disconnected using disconnectSockets, i.e. normal safe disconnection from explicit disconnection by the client.");
13335
+ this.LogDebugMessage("SocketIoClient:on(\"disconnect\"): The connection will not be re-established automatically.");
13336
+ this.ResetSocket();
13335
13337
  break;
13336
13338
  case "transport close":
13337
13339
  case "ping timeout":
13338
13340
  case "transport error":
13339
- this.LogDebugMessage(`Server unexpectedly disconnected. Reason: [${reason}]`);
13340
- this.LogDebugMessage("The connection will be re-established when the server becomes available.");
13341
- if (this.#socket) this.#socket.disconnect();
13342
- this.#socket = void 0;
13343
- if (isNode) {
13344
- if (this.#agentManager) this.#agentManager?.ResetAgent();
13345
- setTimeout(() => this.#EstablishSocketConnect(), this.#reconnectTimeout).unref();
13346
- } else setTimeout(() => this.#EstablishSocketConnect(), this.#reconnectTimeout);
13341
+ this.LogDebugMessage(`SocketIoClient:on("disconnect"): Server unexpectedly disconnected. Reason: [${reason}]`);
13342
+ this.LogDebugMessage("SocketIoClient:on(\"disconnect\"): The connection will be re-established when the server becomes available.");
13343
+ this.ResetSocket();
13344
+ if (isNode) this.#reconnectTimeoutVal = setTimeout(() => this.#EstablishSocketConnect(), this.#reconnectTimeout).unref();
13345
+ else this.#reconnectTimeoutVal = setTimeout(() => this.#EstablishSocketConnect(), this.#reconnectTimeout);
13347
13346
  break;
13348
13347
  }
13349
13348
  });
13350
13349
  }
13350
+ ResetSocket = () => {
13351
+ try {
13352
+ if (this.#reconnectTimeoutVal !== void 0) {
13353
+ clearTimeout(this.#reconnectTimeoutVal);
13354
+ this.#reconnectTimeoutVal = void 0;
13355
+ }
13356
+ if (this.#socket) {
13357
+ this.#socket.removeAllListeners();
13358
+ this.#socket.io.removeAllListeners();
13359
+ if (this.#socket.connected === true) this.#socket.disconnect();
13360
+ if (isNode) {
13361
+ if (this.#agentManager) this.#agentManager.ResetAgent();
13362
+ }
13363
+ this.#socket = void 0;
13364
+ }
13365
+ } catch (error) {
13366
+ const errorMessage = `SocketIoClient:ResetSocket(): Error: [${error}]`;
13367
+ this.LogErrorMessage(errorMessage);
13368
+ this.SocketConnectError(new Error(errorMessage));
13369
+ }
13370
+ };
13351
13371
  };
13352
13372
  //#endregion
13353
13373
  //#region src/cv2/fhirSocketClient.ts
@@ -13365,23 +13385,45 @@ var FhirSocketClient = class extends SocketIoClient {
13365
13385
  if (this.options.joinRooms.length > 0 && socket) socket.emit("__STSjoinRoom", this.options.joinRooms);
13366
13386
  }
13367
13387
  SocketError(error) {
13368
- this.LogErrorMessage(`FhirSocketClient:SocketError(): Error clientName: [${this.name}]: SetupClientSideSocket call back: Error: [${error}]`);
13388
+ this.LogErrorMessage(`FhirSocketClient:SocketError(): Error clientName: [${this.name}]: Error: [${error}]`);
13389
+ }
13390
+ SocketConnectError(error) {
13391
+ this.LogErrorMessage(`FhirSocketClient:SocketConnectError(): Error clientName: [${this.name}]: Error: [${error}]`);
13392
+ }
13393
+ SocketDisconnect(reason) {
13394
+ this.LogDebugMessage(`FhirSocketClient:SocketDisconnect(): Error clientName: [${this.name}]: Reason: [${reason}]`);
13395
+ }
13396
+ SetupSocketEvents(socket) {
13397
+ this.LogDebugMessage(`FhirSocketClient:SetupSocketEvents(): Error clientName: [${this.name}]`);
13369
13398
  }
13370
- SocketConnectError(error) {}
13371
- SocketDisconnect(reason) {}
13372
- SetupSocketEvents(socket) {}
13373
13399
  SetupWSSClient = async () => {
13374
- this.LogDebugMessage(chalk.yellow(`FhirSocketClient:SetupWSSClient(): ID: [${this.id}] clientName: [${this.name}] Starting ...`));
13375
- const cstr = `${this.options.fhirServerEndpoint}:${this.options.fhirServerPort}/nsstsfhir/`;
13376
- this.LogDebugMessage(chalk.yellow(`FhirSocketClient:SetupWSSClient(): Socket connection string: [${cstr}]`));
13377
- this.WithAddress(cstr).WithSocketIoCustomPath(this.options.socketIoCustomPath).WithAuthToken(this.options.authToken);
13378
- if (this.options.agentManager) this.WithAgentManager(this.options.agentManager);
13379
- if (this.options.logger) this.WithLogger(this.options.logger);
13380
- this.SetupSocket();
13381
- this.LogDebugMessage(chalk.yellow(`FhirSocketClient:SetupWSSClient(): ID: [${this.id}] clientName: [${this.name}] Started`));
13400
+ try {
13401
+ this.LogDebugMessage(chalk.yellow(`FhirSocketClient:SetupWSSClient(): ID: [${this.id}] clientName: [${this.name}] Starting ...`));
13402
+ const cstr = `${this.options.fhirServerEndpoint}:${this.options.fhirServerPort}/nsstsfhir/`;
13403
+ this.LogDebugMessage(chalk.yellow(`FhirSocketClient:SetupWSSClient(): Socket connection string: [${cstr}]`));
13404
+ let connectionAccessToken;
13405
+ try {
13406
+ connectionAccessToken = await this.options.GetConnectionAccessToken();
13407
+ } catch (error) {
13408
+ const message = `FhirSocketClient:SetupWSSClient():GetConnectionAccessToken(): Error clientName: [${this.name}]: Error: [${error}]`;
13409
+ this.LogErrorMessage(message);
13410
+ throw new Error(message);
13411
+ }
13412
+ this.WithAddress(cstr).WithSocketIoCustomPath(this.options.socketIoCustomPath).WithAuthToken(connectionAccessToken);
13413
+ if (this.options.agentManager) this.WithAgentManager(this.options.agentManager);
13414
+ if (this.options.logger) this.WithLogger(this.options.logger);
13415
+ this.SetupSocket();
13416
+ this.LogDebugMessage(chalk.yellow(`FhirSocketClient:SetupWSSClient(): ID: [${this.id}] clientName: [${this.name}] Started`));
13417
+ } catch (error) {
13418
+ const message = `FhirSocketClient:SetupWSSClient(): Error clientName: [${this.name}]: Error: [${error}]`;
13419
+ this.LogErrorMessage(message);
13420
+ throw new Error(message);
13421
+ }
13382
13422
  };
13383
13423
  WaitForSocketConnected = async () => {
13384
- const maxAttempts = 50;
13424
+ const maxAttempts = 20;
13425
+ const resetAfterAttempts = 10;
13426
+ const retryDelay = 250;
13385
13427
  if (this.socket && this.socket.connected === true) return true;
13386
13428
  else {
13387
13429
  this.LogDebugMessage(chalk.yellow(`FhirSocketClient:WaitForSocketConnected(): ID: [${this.id}] clientName: [${this.name}] Waiting for connection ...`));
@@ -13393,9 +13435,20 @@ var FhirSocketClient = class extends SocketIoClient {
13393
13435
  this.LogDebugMessage(chalk.yellow(`FhirSocketClient:WaitForSocketConnected(): ID: [${this.id}] clientName: [${this.name}] Connection completed after: [${attempts}] attempts.`));
13394
13436
  return true;
13395
13437
  }
13396
- await Sleep(100);
13438
+ if (attempts === resetAfterAttempts) {
13439
+ this.LogDebugMessage(chalk.rgb(255, 165, 0)(`FhirSocketClient:WaitForSocketConnected(): ID: [${this.id}] clientName: [${this.name}] Resetting Socket. Attempt Number: [${attempts}]`));
13440
+ this.ResetSocket();
13441
+ this.LogDebugMessage(chalk.rgb(255, 165, 0)(`FhirSocketClient:WaitForSocketConnected(): ID: [${this.id}] clientName: [${this.name}] Socket Reset. Attempt Number: [${attempts}]`));
13442
+ await Sleep(retryDelay);
13443
+ this.LogDebugMessage(chalk.rgb(255, 165, 0)(`FhirSocketClient:WaitForSocketConnected(): ID: [${this.id}] clientName: [${this.name}] SetupSocket. Attempt Number: [${attempts}]`));
13444
+ this.SetupSocket();
13445
+ await Sleep(retryDelay);
13446
+ }
13447
+ await Sleep(retryDelay);
13397
13448
  }
13398
- throw new Error(`FhirSocketClient:WaitForSocketConnected(): Error: [Could not connect after ${maxAttempts} connection attempts]`);
13449
+ const message = `FhirSocketClient:WaitForSocketConnected(): Error: [Could not connect after ${maxAttempts} connection attempts]`;
13450
+ this.LogErrorMessage(message);
13451
+ throw new Error(message);
13399
13452
  }
13400
13453
  };
13401
13454
  WithAccessToken = (accessToken) => {