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