@nsshunt/stsobservability 1.0.126 → 1.0.128

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.
@@ -5404,9 +5404,6 @@ class ObservabilityRESTAPISubscriber {
5404
5404
  #LogErrorMessage(message) {
5405
5405
  this.#options.logger.error(message);
5406
5406
  }
5407
- SubscribeOne(subscription) {
5408
- this.Subscribe([subscription]);
5409
- }
5410
5407
  async Subscribe(subscriptions) {
5411
5408
  subscriptions.map(async (subId) => {
5412
5409
  this.#RemoveKeepAlive(subId);
@@ -5419,9 +5416,6 @@ class ObservabilityRESTAPISubscriber {
5419
5416
  }
5420
5417
  });
5421
5418
  }
5422
- UnSubscribeOne(subscription) {
5423
- this.UnSubscribe([subscription]);
5424
- }
5425
5419
  UnSubscribe(subscriptions) {
5426
5420
  subscriptions.map(async (subId) => {
5427
5421
  this.#RemoveKeepAlive(subId);
@@ -5582,168 +5576,10 @@ class ObservabilityRESTAPISubscriber {
5582
5576
  }
5583
5577
  }
5584
5578
  }
5585
- const byteToHex = [];
5586
- for (let i = 0; i < 256; ++i) {
5587
- byteToHex.push((i + 256).toString(16).slice(1));
5588
- }
5589
- function unsafeStringify(arr, offset = 0) {
5590
- return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
5591
- }
5592
- let getRandomValues;
5593
- const rnds8 = new Uint8Array(16);
5594
- function rng() {
5595
- if (!getRandomValues) {
5596
- if (typeof crypto === "undefined" || !crypto.getRandomValues) {
5597
- throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
5598
- }
5599
- getRandomValues = crypto.getRandomValues.bind(crypto);
5600
- }
5601
- return getRandomValues(rnds8);
5602
- }
5603
- const randomUUID = typeof crypto !== "undefined" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
5604
- const native = { randomUUID };
5605
- function _v4(options, buf, offset) {
5606
- options = options || {};
5607
- const rnds = options.random ?? options.rng?.() ?? rng();
5608
- if (rnds.length < 16) {
5609
- throw new Error("Random bytes length must be >= 16");
5610
- }
5611
- rnds[6] = rnds[6] & 15 | 64;
5612
- rnds[8] = rnds[8] & 63 | 128;
5613
- return unsafeStringify(rnds);
5614
- }
5615
- function v4(options, buf, offset) {
5616
- if (native.randomUUID && true && !options) {
5617
- return native.randomUUID();
5618
- }
5619
- return _v4(options);
5620
- }
5621
- var messageStatus = /* @__PURE__ */ ((messageStatus2) => {
5622
- messageStatus2["INPROGRESS"] = "INPROGRESS";
5623
- messageStatus2["SUCCESS"] = "SUCCESS";
5624
- messageStatus2["FAIL"] = "FAIL";
5625
- messageStatus2["INRETRY"] = "INRETRY";
5626
- messageStatus2["KILLED"] = "KILLED";
5627
- return messageStatus2;
5628
- })(messageStatus || {});
5629
- var messageStatusResponse = /* @__PURE__ */ ((messageStatusResponse2) => {
5630
- messageStatusResponse2["SUBSCRIPTION_SUCCESSFULLY_ADDED"] = "SUBSCRIPTION_SUCCESSFULLY_ADDED";
5631
- messageStatusResponse2["SUBSCRIPTION_SUCCESSFULLY_REMOVED"] = "SUBSCRIPTION_SUCCESSFULLY_REMOVED";
5632
- messageStatusResponse2["SUBSCRIPTION_TIMEOUT_RENEWED"] = "SUBSCRIPTION_TIMEOUT_RENEWED";
5633
- messageStatusResponse2["NO_MATCHED_SUBSCRIPTION"] = "NO_MATCHED_SUBSCRIPTION";
5634
- return messageStatusResponse2;
5635
- })(messageStatusResponse || {});
5636
- class SocketIoMessageSender {
5637
- #messageWithAckPayloadRecords = {};
5638
- #socket;
5639
- #maxReties = 3;
5640
- //@@
5641
- #timeoutValue = 5e3;
5642
- //@@
5643
- #options;
5644
- #LogDebugMessage(message) {
5645
- this.#options.logger.debug(message);
5646
- }
5647
- constructor(options) {
5648
- this.#options = options;
5649
- this.#socket = options.socket;
5650
- }
5651
- get socket() {
5652
- return this.#socket;
5653
- }
5654
- set socket(socket) {
5655
- this.#LogDebugMessage(chalk.cyan(`SocketIoMessageSender:set socket(): new socket assigned`));
5656
- this.#socket = socket;
5657
- }
5658
- AckListener(message, payload) {
5659
- if (message.subscriptionKey.id.localeCompare(payload.id) === 0) {
5660
- clearTimeout(message.timeoutId);
5661
- delete message.timeoutId;
5662
- this.#socket.off(this.#GetSubscribeKeepAliveAckEventName(message.eventName, message.subscriptionKey), message.BoundAckListenerCallback);
5663
- message.endTime = performance.now();
5664
- message.duration = message.endTime - message.startTime;
5665
- message.status = "SUCCESS";
5666
- message.subscriptionKeyResponse = payload;
5667
- delete this.#messageWithAckPayloadRecords[message.id];
5668
- this.#LogDebugMessage(chalk.grey(`SocketIoMessageSender:AckListener(): ack received, event: [${message.eventName}], subscriptionKey: [${message.subscriptionKey.id}]`));
5669
- message.resolve(message);
5670
- }
5671
- }
5672
- ResponseTimout(message) {
5673
- this.#LogDebugMessage(chalk.magenta(`SocketIoMessageSender:ResponseTimout(): timeout reached: [${this.#timeoutValue}], event: [${message.eventName}], subscriptionKey: [${message.subscriptionKey.id}]`));
5674
- delete message.timeoutId;
5675
- this.#socket.off(this.#GetSubscribeKeepAliveAckEventName(message.eventName, message.subscriptionKey), message.BoundAckListenerCallback);
5676
- message.endTime = performance.now();
5677
- if (message.retryCount >= this.#maxReties) {
5678
- message.status = "FAIL";
5679
- this.#LogDebugMessage(chalk.red(`SocketIoMessageSender:ResponseTimout(): timeout reached after ${this.#maxReties} retries, event: [${message.eventName}], subscriptionKey: [${message.subscriptionKey.id}]`));
5680
- delete this.#messageWithAckPayloadRecords[message.id];
5681
- message.reject(message);
5682
- return;
5683
- }
5684
- message.status = "INRETRY";
5685
- message.retryCount++;
5686
- this.#LogDebugMessage(chalk.magenta(`SocketIoMessageSender:ResponseTimout(): timeout reached, retrying. Retries: [${message.retryCount}], event: [${message.eventName}], subscriptionKey: [${message.subscriptionKey.id}]`));
5687
- this.SendMessage(message);
5688
- }
5689
- #GetSubscribeKeepAliveAckEventName(eventName, subscriptionKey) {
5690
- return `${eventName}Ack_${subscriptionKey.id}`;
5691
- }
5692
- SendMessage(message) {
5693
- message.startTime = performance.now();
5694
- message.timeoutId = setTimeout(() => {
5695
- this.ResponseTimout(message);
5696
- }, this.#timeoutValue);
5697
- const AckListenerCallback = (payload) => {
5698
- this.AckListener(message, payload);
5699
- };
5700
- message.BoundAckListenerCallback = AckListenerCallback.bind(this);
5701
- this.#socket.on(this.#GetSubscribeKeepAliveAckEventName(message.eventName, message.subscriptionKey), message.BoundAckListenerCallback);
5702
- this.#LogDebugMessage(chalk.gray(`SocketIoMessageSender:SendMessage(): event: [${message.eventName}], subscriptionKey: [${message.subscriptionKey.id}]`));
5703
- this.#socket.emit(message.eventName, message.subscriptionKey);
5704
- }
5705
- SendMessageWithAck(eventName, subscriptionKey) {
5706
- const promise = new Promise((resolve, reject) => {
5707
- const message = {
5708
- id: v4(),
5709
- subscriptionKey,
5710
- retryCount: 0,
5711
- startTime: performance.now(),
5712
- status: "INPROGRESS",
5713
- eventName,
5714
- resolve,
5715
- reject
5716
- };
5717
- this.#messageWithAckPayloadRecords[message.id] = message;
5718
- this.SendMessage(message);
5719
- });
5720
- return promise;
5721
- }
5722
- KillMessageInFlight(subscriptionKey) {
5723
- const killIds = [];
5724
- for (const [, message] of Object.entries(this.#messageWithAckPayloadRecords)) {
5725
- if (message.subscriptionKey.id.localeCompare(subscriptionKey.id) === 0) {
5726
- this.#LogDebugMessage(chalk.blue(`SocketIoMessageSender:KillMessageInFlight(): event: [${message.eventName}], subscriptionKey: [${message.subscriptionKey.id}]`));
5727
- clearTimeout(message.timeoutId);
5728
- delete message.timeoutId;
5729
- this.#socket.off(this.#GetSubscribeKeepAliveAckEventName(message.eventName, message.subscriptionKey), message.BoundAckListenerCallback);
5730
- killIds.push(message.id);
5731
- message.status = "KILLED";
5732
- message.reject(message);
5733
- }
5734
- }
5735
- killIds.forEach((id) => {
5736
- delete this.#messageWithAckPayloadRecords[id];
5737
- });
5738
- }
5739
- }
5740
5579
  class ObservabilitySocketIOSubscriber {
5741
- #socketSubscribeKeepAlive = {};
5742
5580
  #socket;
5743
5581
  #options;
5744
- #socketIoMessageSender;
5745
- // This is required becuase each call to .bind create a new replica bound function - so we need the same instance for on/off event bindings.
5746
- KeepAliveAckBoundFunction;
5582
+ subscriptions = {};
5747
5583
  #LogDebugMessage(message) {
5748
5584
  this.#options.logger.debug(message);
5749
5585
  }
@@ -5753,95 +5589,98 @@ class ObservabilitySocketIOSubscriber {
5753
5589
  constructor(options) {
5754
5590
  this.#options = options;
5755
5591
  this.#socket = options.socket;
5756
- this.#socketIoMessageSender = new SocketIoMessageSender({
5757
- socket: this.#socket,
5758
- logger: this.#options.logger
5759
- });
5760
5592
  }
5761
5593
  get socket() {
5762
5594
  return this.#socket;
5763
5595
  }
5764
5596
  set socket(socket) {
5765
5597
  this.#LogDebugMessage(chalk.cyan(`ObservabilitySocketIOSubscriber:set socket(): new socket assigned`));
5598
+ this.#socket.off("subscriptionData");
5599
+ this.subscriptions = {};
5600
+ if (this.#socket.connected) {
5601
+ this.#socket.disconnect();
5602
+ }
5766
5603
  this.#socket = socket;
5767
- this.#socketIoMessageSender.socket = socket;
5768
- }
5769
- SubscribeOne(subscription) {
5770
- this.Subscribe([subscription]);
5604
+ this.#socket.on("subscriptionData", (data, cb) => {
5605
+ const subId = data.subscriptionKey.id;
5606
+ if (this.subscriptions[subId]) {
5607
+ this.subscriptions[subId].cb(data);
5608
+ cb(`subscriptionData Response: [${data}]`);
5609
+ } else {
5610
+ cb(`subscriptionData Response: Not subscription found in subscriptions: [${data}]`);
5611
+ }
5612
+ });
5771
5613
  }
5772
5614
  async Subscribe(subscriptions) {
5773
- const socket = this.#socket;
5774
5615
  subscriptions.map(async (subId) => {
5775
- this.#RemoveKeepAlive(subId);
5776
- this.#LogDebugMessage(chalk.yellow(`ObservabilitySocketIOSubscriber:Subscribe(): Sending subscribe: subscriptionKey: [${subId.subscriptionKey.id}]`));
5777
5616
  try {
5778
- const retVal = await this.#socketIoMessageSender.SendMessageWithAck("subscribe", subId.subscriptionKey);
5779
- this.#LogDebugMessage(chalk.green(`ObservabilitySocketIOSubscriber:Subscribe(): SendMessageWithAck response: [${JSON.stringify(retVal)}]`));
5780
- this.#AddKeepAlive(socket, subId);
5781
- socket.on(GetSocketIOSubscriptionKey(subId.subscriptionKey), subId.cb);
5617
+ this.#LogDebugMessage(chalk.yellow(`ObservabilitySocketIOSubscriber:Subscribe(): Sending subscribe: subscriptionKey: [${subId.subscriptionKey.id}]`));
5618
+ this.#socket.timeout(1e3).emit("subscribe", subId.subscriptionKey, (error, data) => {
5619
+ if (error) {
5620
+ this.#LogErrorMessage(chalk.red(`ObservabilitySocketIOSubscriber:Subscribe(): Error: [${error}], data: [${data}]`));
5621
+ } else {
5622
+ this.#LogDebugMessage(chalk.green(`ObservabilitySocketIOSubscriber:Subscribe(): Response: [${JSON.stringify(data)}]`));
5623
+ this.subscriptions[subId.subscriptionKey.id] = subId;
5624
+ }
5625
+ });
5782
5626
  } catch (error) {
5783
5627
  this.#LogErrorMessage(chalk.red(`ObservabilitySocketIOSubscriber:Subscribe(): Error: response: [${JSON.stringify(error)}]`));
5784
- socket.off(GetSocketIOSubscriptionKey(subId.subscriptionKey), subId.cb);
5785
- this.#RemoveKeepAlive(subId);
5786
5628
  }
5787
5629
  });
5788
5630
  }
5789
- UnSubscribeOne(subscription) {
5790
- this.UnSubscribe([subscription]);
5791
- }
5792
5631
  UnSubscribe(subscriptions) {
5793
- const socket = this.#socket;
5794
5632
  subscriptions.map(async (subId) => {
5795
- socket.off(GetSocketIOSubscriptionKey(subId.subscriptionKey), subId.cb);
5796
- this.#RemoveKeepAlive(subId);
5797
- this.#socketIoMessageSender.KillMessageInFlight(subId.subscriptionKey);
5798
- this.#LogDebugMessage(chalk.yellow(`ObservabilitySocketIOSubscriber:UnSubscribe(): Sending unsubscribe: subscriptionKey: [${subId.subscriptionKey.id}]`));
5799
5633
  try {
5800
- const retVal = await this.#socketIoMessageSender.SendMessageWithAck("unsubscribe", subId.subscriptionKey);
5801
- this.#LogDebugMessage(chalk.green(`ObservabilitySocketIOSubscriber:UnSubscribe(): SendMessageWithAck response: [${JSON.stringify(retVal)}]`));
5634
+ this.#LogDebugMessage(chalk.yellow(`ObservabilitySocketIOSubscriber:UnSubscribe(): Sending unsubscribe: subscriptionKey: [${subId.subscriptionKey.id}]`));
5635
+ this.#socket.timeout(1e3).emit("unsubscribe", subId.subscriptionKey, (error, data) => {
5636
+ if (error) {
5637
+ this.#LogErrorMessage(chalk.red(`ObservabilitySocketIOSubscriber:UnSubscribe(): Error: [${error}], data: [${data}]`));
5638
+ } else {
5639
+ this.#LogDebugMessage(chalk.green(`ObservabilitySocketIOSubscriber:UnSubscribe(): Response: [${JSON.stringify(data)}]`));
5640
+ delete this.subscriptions[subId.subscriptionKey.id];
5641
+ }
5642
+ });
5802
5643
  } catch (error) {
5803
5644
  this.#LogErrorMessage(chalk.red(`ObservabilitySocketIOSubscriber:UnSubscribe(): Error response: [${JSON.stringify(error)}]`));
5804
5645
  }
5805
5646
  });
5806
5647
  }
5807
- #AddKeepAlive(socket, subscription) {
5808
- this.#RemoveKeepAlive(subscription);
5809
- this.#socketSubscribeKeepAlive[subscription.subscriptionKey.id] = {
5810
- id: subscription.subscriptionKey.id,
5811
- timeout: setTimeout(async () => {
5812
- this.#LogDebugMessage(chalk.yellow(`ObservabilitySocketIOSubscriber:#AddKeepAlive:timeoutCheck(): Sending keepAlive: subscriptionKey: [${subscription.subscriptionKey.id}]`));
5813
- try {
5814
- const retVal = await this.#socketIoMessageSender.SendMessageWithAck("subscribeKeepAlive", subscription.subscriptionKey);
5815
- this.#LogDebugMessage(chalk.green(`ObservabilitySocketIOSubscriber:#AddKeepAlive(): SendMessageWithAck response: [${JSON.stringify(retVal)}]`));
5816
- this.#AddKeepAlive(socket, subscription);
5817
- } catch (error) {
5818
- this.#LogErrorMessage(chalk.red(`ObservabilitySocketIOSubscriber:#AddKeepAlive(): Error response: [${JSON.stringify(error)}]`));
5819
- socket.off(GetSocketIOSubscriptionKey(subscription.subscriptionKey), subscription.cb);
5820
- this.#RemoveKeepAlive(subscription);
5821
- }
5822
- }, this.#options.keepAlive)
5823
- };
5824
- }
5825
- #RemoveKeepAlive(subscription) {
5826
- if (this.#socketSubscribeKeepAlive[subscription.subscriptionKey.id]) {
5827
- this.#LogDebugMessage(chalk.gray(`ObservabilitySocketIOSubscriber:#RemoveKeepAlive(): subscriptionKey: [${subscription.subscriptionKey.id}]`));
5828
- clearTimeout(this.#socketSubscribeKeepAlive[subscription.subscriptionKey.id].timeout);
5829
- delete this.#socketSubscribeKeepAlive[subscription.subscriptionKey.id];
5648
+ }
5649
+ const byteToHex = [];
5650
+ for (let i = 0; i < 256; ++i) {
5651
+ byteToHex.push((i + 256).toString(16).slice(1));
5652
+ }
5653
+ function unsafeStringify(arr, offset = 0) {
5654
+ return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
5655
+ }
5656
+ let getRandomValues;
5657
+ const rnds8 = new Uint8Array(16);
5658
+ function rng() {
5659
+ if (!getRandomValues) {
5660
+ if (typeof crypto === "undefined" || !crypto.getRandomValues) {
5661
+ throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
5830
5662
  }
5663
+ getRandomValues = crypto.getRandomValues.bind(crypto);
5831
5664
  }
5832
- /*
5833
- override SocketIOConnect(socket: Socket<ServerToClientEvents, ClientToServerEvents>): void {
5834
- if (this.#observabilitySocketIOSubscriber instanceof ObservabilitySocketIOSubscriber) {
5835
- if (socket.id.localeCompare((this.#observabilitySocketIOSubscriber as ObservabilitySocketIOSubscriber).socket.id) === 0) {
5836
- //this.#LogMessage(chalk.gray(`Socket ids are the same - ignoring`));
5837
- } else {
5838
- //this.#LogMessage(chalk.yellow(`Socket ids are different - update with new socket`));
5839
- (this.#observabilitySocketIOSubscriber as ObservabilitySocketIOSubscriber).socket = socket;
5840
- this.#observabilitySubscriptionManager.Start();
5841
- }
5842
- }
5665
+ return getRandomValues(rnds8);
5666
+ }
5667
+ const randomUUID = typeof crypto !== "undefined" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
5668
+ const native = { randomUUID };
5669
+ function _v4(options, buf, offset) {
5670
+ options = options || {};
5671
+ const rnds = options.random ?? options.rng?.() ?? rng();
5672
+ if (rnds.length < 16) {
5673
+ throw new Error("Random bytes length must be >= 16");
5843
5674
  }
5844
- */
5675
+ rnds[6] = rnds[6] & 15 | 64;
5676
+ rnds[8] = rnds[8] & 63 | 128;
5677
+ return unsafeStringify(rnds);
5678
+ }
5679
+ function v4(options, buf, offset) {
5680
+ if (native.randomUUID && true && !options) {
5681
+ return native.randomUUID();
5682
+ }
5683
+ return _v4(options);
5845
5684
  }
5846
5685
  var runStates = /* @__PURE__ */ ((runStates2) => {
5847
5686
  runStates2["idle"] = "idle";
@@ -5919,7 +5758,7 @@ class ObservabilitySubscriptionManager {
5919
5758
  for (let i = 0; i < this.subscriptions.length; i++) {
5920
5759
  if (this.subscriptions[i].subscriptionKey.key?.localeCompare(keyToFind) === 0) {
5921
5760
  if (this.#observabilitySocketIOSubscriber) {
5922
- this.#observabilitySocketIOSubscriber.UnSubscribeOne(this.subscriptions[i]);
5761
+ this.#observabilitySocketIOSubscriber.UnSubscribe([this.subscriptions[i]]);
5923
5762
  }
5924
5763
  } else {
5925
5764
  newSubscriptions.push(this.subscriptions[i]);
@@ -6439,15 +6278,12 @@ export {
6439
6278
  RequestLoggerMiddlewareEventName,
6440
6279
  STSInstrumentControllerPlugin,
6441
6280
  STSInstrumentControllerPluginKey,
6442
- SocketIoMessageSender,
6443
6281
  StopInstruments,
6444
6282
  SubscriptionTopic,
6445
6283
  TransportType,
6446
6284
  ePublishState,
6447
6285
  globalServiceDefinitions,
6448
6286
  influxDBDataType,
6449
- messageStatus,
6450
- messageStatusResponse,
6451
6287
  runStates,
6452
6288
  useSTSInstrumentControllerPlugin
6453
6289
  };