@microsoft/applicationinsights-channel-js 2.7.3-nightly.2112-08 → 2.7.3-nightly.2201-01

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.
package/src/Sender.ts CHANGED
@@ -757,7 +757,7 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControlsAI {
757
757
  /**
758
758
  * Send fetch API request
759
759
  * @param payload {string} - The data payload to be sent.
760
- * @param isAsync {boolean} - not used
760
+ * @param isAsync {boolean} - For fetch this identifies whether we are "unloading" (false) or a normal request
761
761
  */
762
762
  function _doFetchSender(payload: string[], isAsync: boolean) {
763
763
  const endPointUrl = _self._senderConfig.endpointUrl();
@@ -765,6 +765,8 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControlsAI {
765
765
  const plainTextBatch = new Blob([batch], { type: "application/json" });
766
766
  let requestHeaders = new Headers();
767
767
  let batchLength = batch.length;
768
+ let ignoreResponse = false;
769
+ let responseHandled = false;
768
770
 
769
771
  // append Sdk-Context request header only in case of breeze endpoint
770
772
  if (isInternalApplicationInsightsEndpoint(endPointUrl)) {
@@ -780,10 +782,13 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControlsAI {
780
782
  headers: requestHeaders,
781
783
  body: plainTextBatch,
782
784
  [DisabledPropertyName]: true // Mark so we don't attempt to track this request
783
- }
785
+ };
784
786
 
785
787
  if (!isAsync) {
786
788
  init.keepalive = true;
789
+ // As a sync request (during unload), it is unlikely that we will get a chance to process the response so
790
+ // just like beacon send assume that the events have been accepted and processed
791
+ ignoreResponse = true;
787
792
  _syncFetchPayload += batchLength;
788
793
  }
789
794
 
@@ -796,34 +801,53 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControlsAI {
796
801
  // cause the request to fail and we no telemetry would be sent
797
802
  }
798
803
 
799
- fetch(request).then(response => {
800
- if (!isAsync) {
801
- _syncFetchPayload -= batchLength;
802
- batchLength = 0;
803
- }
804
+ _self._buffer.markAsSent(payload);
804
805
 
805
- /**
806
- * The Promise returned from fetch() won’t reject on HTTP error status even if the response is an HTTP 404 or 500.
807
- * Instead, it will resolve normally (with ok status set to false), and it will only reject on network failure
808
- * or if anything prevented the request from completing.
809
- */
810
- if (!response.ok) {
811
- _self._onError(payload, response.statusText)
812
- } else {
813
- response.text().then(text => {
814
- _checkResponsStatus(response.status, payload, response.url, payload.length, response.statusText, text);
815
- });
816
- }
817
- }).catch((error: Error) => {
818
- if (!isAsync) {
819
- _syncFetchPayload -= batchLength;
820
- batchLength = 0;
806
+ try {
807
+ fetch(request).then(response => {
808
+ if (!isAsync) {
809
+ _syncFetchPayload -= batchLength;
810
+ batchLength = 0;
811
+ }
812
+
813
+ if (!responseHandled) {
814
+ responseHandled = true;
815
+
816
+ /**
817
+ * The Promise returned from fetch() won’t reject on HTTP error status even if the response is an HTTP 404 or 500.
818
+ * Instead, it will resolve normally (with ok status set to false), and it will only reject on network failure
819
+ * or if anything prevented the request from completing.
820
+ */
821
+ if (!response.ok) {
822
+ _self._onError(payload, response.statusText)
823
+ } else {
824
+ response.text().then(text => {
825
+ _checkResponsStatus(response.status, payload, response.url, payload.length, response.statusText, text);
826
+ });
827
+ }
828
+ }
829
+ }).catch((error: Error) => {
830
+ if (!isAsync) {
831
+ _syncFetchPayload -= batchLength;
832
+ batchLength = 0;
833
+ }
834
+
835
+ if (!responseHandled) {
836
+ responseHandled = true;
837
+ _self._onError(payload, error.message)
838
+ }
839
+ });
840
+ } catch (e) {
841
+ if (!responseHandled) {
842
+ _self._onError(payload, dumpObj(e));
821
843
  }
844
+ }
822
845
 
823
- _self._onError(payload, error.message)
824
- });
825
-
826
- _self._buffer.markAsSent(payload);
846
+ if (ignoreResponse && !responseHandled) {
847
+ // Assume success during unload processing as we most likely won't get the response
848
+ responseHandled = true;
849
+ _self._onSuccess(payload, payload.length);
850
+ }
827
851
  }
828
852
 
829
853
  /**