@microsoft/applicationinsights-offlinechannel-js 0.1.1-nightly3.2403-01 → 0.1.1-nightly3.2403-02

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.
Files changed (29) hide show
  1. package/dist/es5/applicationinsights-offlinechannel-js.js +698 -510
  2. package/dist/es5/applicationinsights-offlinechannel-js.js.map +1 -1
  3. package/dist/es5/applicationinsights-offlinechannel-js.min.js +2 -2
  4. package/dist/es5/applicationinsights-offlinechannel-js.min.js.map +1 -1
  5. package/dist-es5/Helpers/Utils.js +5 -5
  6. package/dist-es5/Helpers/Utils.js.map +1 -1
  7. package/dist-es5/InMemoryBatch.js +1 -1
  8. package/dist-es5/Interfaces/IInMemoryBatch.js +1 -1
  9. package/dist-es5/Interfaces/IOfflineBatch.js +1 -1
  10. package/dist-es5/Interfaces/IOfflineIndexDb.js +1 -1
  11. package/dist-es5/Interfaces/IOfflineProvider.js +1 -1
  12. package/dist-es5/Interfaces/ISender.js +1 -1
  13. package/dist-es5/OfflineBatchHandler.js +4 -4
  14. package/dist-es5/OfflineBatchHandler.js.map +1 -1
  15. package/dist-es5/OfflineChannel.js +4 -4
  16. package/dist-es5/OfflineChannel.js.map +1 -1
  17. package/dist-es5/PayloadHelper.js +1 -1
  18. package/dist-es5/Providers/IndexDbHelper.js +4 -4
  19. package/dist-es5/Providers/IndexDbHelper.js.map +1 -1
  20. package/dist-es5/Providers/IndexDbProvider.js +1 -1
  21. package/dist-es5/Providers/WebStorageProvider.js +1 -1
  22. package/dist-es5/Sender.js +55 -400
  23. package/dist-es5/Sender.js.map +1 -1
  24. package/dist-es5/__DynamicConstants.js +10 -13
  25. package/dist-es5/__DynamicConstants.js.map +1 -1
  26. package/dist-es5/applicationinsights-offlinechannel-js.js +1 -1
  27. package/package.json +3 -3
  28. package/types/applicationinsights-offlinechannel-js.d.ts +1 -1
  29. package/types/applicationinsights-offlinechannel-js.namespaced.d.ts +6 -1
@@ -1,61 +1,36 @@
1
1
  /*
2
- * Application Insights JavaScript SDK - Offline Channel, 0.1.1-nightly3.2403-01
2
+ * Application Insights JavaScript SDK - Offline Channel, 0.1.1-nightly3.2403-02
3
3
  * Copyright (c) Microsoft and contributors. All rights reserved.
4
4
  */
5
5
 
6
6
 
7
7
  import dynamicProto from "@microsoft/dynamicproto-js";
8
- import { BreezeChannelIdentifier, DisabledPropertyName, utlSetStoragePrefix } from "@microsoft/applicationinsights-common";
9
- import { _throwInternal, arrForEach, createProcessTelemetryContext, dumpObj, getExceptionName, getJSON, getLocation, getNavigator, getWindow, isArray, isBeaconsSupported, isFetchSupported, isXhrSupported, objKeys, onConfigChange, useXDomainRequest } from "@microsoft/applicationinsights-core-js";
10
- import { createPromise, doAwaitResponse } from "@nevware21/ts-async";
11
- import { isFunction, isNumber } from "@nevware21/ts-utils";
12
- import { _DYN_DATA, _DYN_INITIALIZE, _DYN_IS_COMPLETELY_IDLE, _DYN_LENGTH, _DYN_ONERROR, _DYN_REASON, _DYN_REJECTED, _DYN_REPLACE, _DYN_SEND_POST, _DYN_SET_REQUEST_HEADER, _DYN_STATUS, _DYN_URL_STRING, _DYN_VALUE } from "./__DynamicConstants";
8
+ import { BreezeChannelIdentifier, SenderPostManager, formatErrorMessageXdr, getResponseText, parseResponse, prependTransports, utlSetStoragePrefix } from "@microsoft/applicationinsights-common";
9
+ import { _throwInternal, createProcessTelemetryContext, onConfigChange } from "@microsoft/applicationinsights-core-js";
10
+ import { isFunction } from "@nevware21/ts-utils";
11
+ import { _DYN_ENABLE_SEND_PROMISE, _DYN_GET_XHR_INST, _DYN_INITIALIZE, _DYN_IS_COMPLETELY_IDLE, _DYN_SEND_POST } from "./__DynamicConstants";
13
12
  var DefaultOfflineIdentifier = "OfflineChannel";
14
- //const FetchSyncRequestSizeLimitBytes = 65000; // approx 64kb (the current Edge, Firefox and Chrome max limit)
15
13
  var PostChannelId = "PostChannel";
16
- function _getResponseText(xhr) {
17
- try {
18
- return xhr.responseText;
19
- }
20
- catch (e) {
21
- // Best effort, as XHR may throw while XDR wont so just ignore
22
- }
23
- return null;
24
- }
25
14
  function isOverrideFn(httpXHROverride) {
26
15
  return httpXHROverride && httpXHROverride[_DYN_SEND_POST /* @min:%2esendPOST */];
27
16
  }
28
- function _prependTransports(theTransports, newTransports) {
29
- if (newTransports) {
30
- if (isNumber(newTransports)) {
31
- theTransports = [newTransports].concat(theTransports);
32
- }
33
- else if (isArray(newTransports)) {
34
- theTransports = newTransports.concat(theTransports);
35
- }
36
- }
37
- return theTransports;
38
- }
39
17
  var Sender = /** @class */ (function () {
40
18
  function Sender() {
41
19
  var _consecutiveErrors; // How many times in a row a retryable error condition has occurred.
42
20
  var _retryAt; // The time to retry at in milliseconds from 1970/01/01 (this makes the timer calculation easy).
43
- //let _lastSend: number; // The time of the last send operation.
44
21
  var _paused; // Flag indicating that the sending should be paused
45
- var _stamp_specific_redirects;
46
- var _syncFetchPayload = 0; // Keep track of the outstanding sync fetch payload total (as sync fetch has limits)
47
22
  var _enableSendPromise;
48
23
  var _alwaysUseCustomSend;
49
- var _disableXhr;
50
- var _fallbackSend;
51
24
  var _isInitialized;
52
25
  var _diagLog;
53
26
  var _core;
54
27
  var _httpInterface;
55
28
  var _onlineChannelId;
56
29
  var _isOneDs;
30
+ var _sendPostMgr;
31
+ var _disableCredentials;
57
32
  dynamicProto(Sender, this, function (_self, _base) {
58
- var _sendCredentials = true; // for 1ds
33
+ //let _sendCredentials = true; // for 1ds
59
34
  _initDefaults();
60
35
  _self.pause = function () {
61
36
  _clearScheduledTimer();
@@ -67,7 +42,7 @@ var Sender = /** @class */ (function () {
67
42
  _retryAt = null;
68
43
  }
69
44
  };
70
- _self.getXhrInst = function (sync) {
45
+ _self[_DYN_GET_XHR_INST /* @min:%2egetXhrInst */] = function (sync) {
71
46
  // unload events will be saved. so not return unload interface
72
47
  return _httpInterface;
73
48
  };
@@ -79,7 +54,6 @@ var Sender = /** @class */ (function () {
79
54
  _core = core;
80
55
  _consecutiveErrors = 0;
81
56
  _retryAt = null;
82
- _stamp_specific_redirects = 0;
83
57
  // This function will be re-called whenever any referenced configuration is changed
84
58
  var hook = onConfigChange(config, function (details) {
85
59
  var config = details.cfg;
@@ -96,30 +70,30 @@ var Sender = /** @class */ (function () {
96
70
  }
97
71
  _alwaysUseCustomSend = offlineSenderCfg.alwaysUseXhrOverride;
98
72
  // default true
99
- _enableSendPromise = !(senderConfig.enableSendPromise === false);
73
+ _enableSendPromise = !(senderConfig[_DYN_ENABLE_SEND_PROMISE /* @min:%2eenableSendPromise */] === false);
100
74
  var xhrOverride = offlineSenderCfg.httpXHROverride || senderConfig.httpXHROverride;
101
75
  var customInterface = isOverrideFn(xhrOverride) ? xhrOverride : null;
102
- if (!customInterface && _isOneDs) {
103
- var location_1 = getLocation();
104
- if (location_1 && location_1.protocol && location_1.protocol.toLowerCase() === "file:") {
105
- // Special case where a local html file fails with a CORS error on Chromium browsers
106
- _sendCredentials = false;
107
- }
76
+ _disableCredentials = !customInterface && _isOneDs;
77
+ var sendPostMgrConfig = _getSendPostMgrConfig();
78
+ if (!_sendPostMgr) {
79
+ _sendPostMgr = new SenderPostManager();
80
+ _sendPostMgr[_DYN_INITIALIZE /* @min:%2einitialize */](sendPostMgrConfig, _diagLog);
81
+ }
82
+ else {
83
+ _sendPostMgr.SetConfig(sendPostMgrConfig);
108
84
  }
109
85
  var httpInterface = null;
110
86
  var customTransPorts = offlineSenderCfg.transports || senderConfig.transports || [];
111
- // User requested transport(s) values > Beacon > Fetch > XHR
112
- // Beacon would be filtered out if user has set disableBeaconApi to true at _getSenderInterface
113
- var theTransports = _prependTransports([1 /* TransportType.Xhr */, 2 /* TransportType.Fetch */, 3 /* TransportType.Beacon */], customTransPorts);
114
- httpInterface = _getSenderInterface(theTransports, false);
115
- var xhrInterface = { sendPOST: _xhrSender };
116
- _fallbackSend = _xhrSender;
87
+ var theTransports = prependTransports([1 /* TransportType.Xhr */, 2 /* TransportType.Fetch */, 3 /* TransportType.Beacon */], customTransPorts);
88
+ httpInterface = _sendPostMgr[_DYN_GET_XHR_INST /* @min:%2egetXhrInst */](theTransports, false);
89
+ var xhrInterface = _sendPostMgr.getFallbackInst();
117
90
  httpInterface = _alwaysUseCustomSend ? customInterface : (httpInterface || customInterface || xhrInterface);
118
91
  _httpInterface = httpInterface || xhrInterface;
119
92
  });
120
93
  unloadHookContainer && unloadHookContainer.add(hook);
121
94
  };
122
95
  _self[_DYN_IS_COMPLETELY_IDLE /* @min:%2eisCompletelyIdle */] = function () {
96
+ var syncPayload = 0;
123
97
  try {
124
98
  var senderPlugin = _core.getPlugin(_onlineChannelId).plugin;
125
99
  if (senderPlugin && isFunction(senderPlugin[_DYN_IS_COMPLETELY_IDLE /* @min:%2eisCompletelyIdle */])) {
@@ -127,12 +101,16 @@ var Sender = /** @class */ (function () {
127
101
  return false;
128
102
  }
129
103
  }
104
+ if (_sendPostMgr) {
105
+ syncPayload = _sendPostMgr.getSyncFetchPayload();
106
+ }
130
107
  }
131
108
  catch (e) {
132
109
  // if can't get idle status of online sender, then isidle status only depends on offine sender idle status
133
110
  }
134
- return !_paused && _syncFetchPayload === 0;
111
+ return !_paused && syncPayload === 0;
135
112
  };
113
+ // partial 206, parse payload (not send again)
136
114
  _self._doTeardown = function (unloadCtx, unloadState) {
137
115
  _initDefaults();
138
116
  };
@@ -142,6 +120,16 @@ var Sender = /** @class */ (function () {
142
120
  function _onSuccess(res, onComplete) {
143
121
  _doOnComplete(onComplete, 200, {}, res);
144
122
  }
123
+ function _getSendPostMgrConfig() {
124
+ var _a;
125
+ var config = (_a = {},
126
+ _a[_DYN_ENABLE_SEND_PROMISE /* @min:enableSendPromise */] = _enableSendPromise,
127
+ _a.isOneDs = _isOneDs,
128
+ _a.disableCredentials = _disableCredentials,
129
+ _a.senderOnCompleteCallBack = _getOnCompleteFuncs(),
130
+ _a);
131
+ return config;
132
+ }
145
133
  /**
146
134
  * error handler
147
135
  */
@@ -149,54 +137,21 @@ var Sender = /** @class */ (function () {
149
137
  _throwInternal(_diagLog, 2 /* eLoggingSeverity.WARNING */, 26 /* _eInternalMessageId.OnError */, "Failed to send telemetry.", { message: message });
150
138
  _doOnComplete(onComplete, 400, {});
151
139
  }
152
- function _onNoPayloadUrl(onComplete) {
153
- _onError("No endpoint url is provided for the batch", onComplete);
154
- }
155
- /**
156
- * partial success handler
157
- */
158
- //TODO: partial success handler
159
- // function _onPartialSuccess (payload: string[], results: IBackendResponse, onComplete?: OnCompleteCallback) {
160
- // }
161
- function _getSenderInterface(transports, syncSupport) {
162
- var _a;
163
- var transportType = null;
164
- var sendPostFunc = null;
165
- var lp = 0;
166
- while (sendPostFunc == null && lp < transports[_DYN_LENGTH /* @min:%2elength */]) {
167
- transportType = transports[lp];
168
- if (!_disableXhr && transportType === 1 /* TransportType.Xhr */) {
169
- if (useXDomainRequest()) {
170
- // IE 8 and 9
171
- sendPostFunc = _xdrSender;
172
- }
173
- else if (isXhrSupported()) {
174
- sendPostFunc = _xhrSender;
175
- }
176
- }
177
- else if (transportType === 2 /* TransportType.Fetch */ && isFetchSupported(syncSupport)) {
178
- sendPostFunc = _fetchSender;
179
- }
180
- else if (transportType === 3 /* TransportType.Beacon */ && isBeaconsSupported()) {
181
- sendPostFunc = _beaconSender;
140
+ function _getOnCompleteFuncs() {
141
+ var funcs = {
142
+ xdrOnComplete: function (response, oncomplete) {
143
+ return _xdrOnLoad(response, oncomplete);
144
+ },
145
+ fetchOnComplete: function (response, onComplete, resValue) {
146
+ var status = response.status;
147
+ return _handleResponse(onComplete, status, {}, resValue);
148
+ },
149
+ xhrOnComplete: function (request, oncomplete) {
150
+ var response = getResponseText(request);
151
+ return _handleResponse(oncomplete, request.status, {}, response);
182
152
  }
183
- lp++;
184
- }
185
- if (sendPostFunc) {
186
- return _a = {},
187
- _a[_DYN_SEND_POST /* @min:sendPOST */] = sendPostFunc,
188
- _a;
189
- }
190
- return null;
191
- }
192
- /**
193
- * Send fetch API request
194
- * @param payload - {string} - The data payload to be sent.
195
- * @param oncomplete - {function} on complete function
196
- * @param sync - {boolean} - not used
197
- */
198
- function _fetchSender(payload, oncomplete, sync) {
199
- return _doFetchSender(payload, oncomplete, false);
153
+ };
154
+ return funcs;
200
155
  }
201
156
  function _doOnComplete(oncomplete, status, headers, response) {
202
157
  try {
@@ -206,212 +161,6 @@ var Sender = /** @class */ (function () {
206
161
  // eslint-disable-next-line no-empty
207
162
  }
208
163
  }
209
- function _doBeaconSend(payload, oncomplete) {
210
- var nav = getNavigator();
211
- var url = payload[_DYN_URL_STRING /* @min:%2eurlString */];
212
- if (!url) {
213
- _onNoPayloadUrl(oncomplete);
214
- // return true here, because we don't want to retry it with fallback sender
215
- return true;
216
- }
217
- var data = payload[_DYN_DATA /* @min:%2edata */];
218
- // Chrome only allows CORS-safelisted values for the sendBeacon data argument
219
- // see: https://bugs.chromium.org/p/chromium/issues/detail?id=720283
220
- //const batch = buffer.batchPayloads(payload);
221
- // Chrome only allows CORS-safelisted values for the sendBeacon data argument
222
- // see: https://bugs.chromium.org/p/chromium/issues/detail?id=720283
223
- var plainTextBatch = new Blob([data], { type: "text/plain;charset=UTF-8" });
224
- // The sendBeacon method returns true if the user agent is able to successfully queue the data for transfer. Otherwise it returns false.
225
- var queued = nav.sendBeacon(url, plainTextBatch);
226
- if (queued) {
227
- _onSuccess(null, oncomplete);
228
- }
229
- return queued;
230
- }
231
- /**
232
- * Send Beacon API request
233
- * @param payload - {string} - The data payload to be sent.
234
- * @param sync - {boolean} - not used
235
- * Note: Beacon API does not support custom headers and we are not able to get
236
- * appId from the backend for the correct correlation.
237
- */
238
- function _beaconSender(payload, oncomplete, sync) {
239
- var data = payload[_DYN_DATA /* @min:%2edata */];
240
- if (data) {
241
- // The sendBeacon method returns true if the user agent is able to successfully queue the data for transfer. Otherwise it returns false.
242
- if (!_doBeaconSend(payload, oncomplete)) {
243
- _fallbackSend && _fallbackSend(payload, oncomplete, true);
244
- _throwInternal(_diagLog, 2 /* eLoggingSeverity.WARNING */, 40 /* _eInternalMessageId.TransmissionFailed */, ". " + "Failed to send telemetry with Beacon API, retried with normal sender.");
245
- }
246
- }
247
- return;
248
- }
249
- /**
250
- * Send XMLHttpRequest
251
- * @param payload - {string} - The data payload to be sent.
252
- * @param sync - {boolean} - Indicates if the request should be sent synchronously
253
- */
254
- function _xhrSender(payload, oncomplete, sync) {
255
- //let internalPayload = payload as IInternalPayloadData;
256
- var thePromise;
257
- var resolveFunc;
258
- var rejectFunc;
259
- var headers = payload.headers || {};
260
- if (!sync && _enableSendPromise) {
261
- thePromise = createPromise(function (resolve, reject) {
262
- resolveFunc = resolve;
263
- rejectFunc = reject;
264
- });
265
- }
266
- var xhr = new XMLHttpRequest();
267
- var endPointUrl = payload[_DYN_URL_STRING /* @min:%2eurlString */];
268
- if (!endPointUrl) {
269
- _onNoPayloadUrl(oncomplete);
270
- resolveFunc && resolveFunc(false);
271
- return;
272
- }
273
- try {
274
- xhr[DisabledPropertyName] = true;
275
- }
276
- catch (e) {
277
- // If the environment has locked down the XMLHttpRequest (preventExtensions and/or freeze), this would
278
- // cause the request to fail and we no telemetry would be sent
279
- }
280
- xhr.open("POST", endPointUrl, !sync);
281
- xhr[_DYN_SET_REQUEST_HEADER /* @min:%2esetRequestHeader */]("Content-type", "application/json");
282
- arrForEach(objKeys(headers), function (headerName) {
283
- xhr[_DYN_SET_REQUEST_HEADER /* @min:%2esetRequestHeader */](headerName, headers[headerName]);
284
- });
285
- xhr.onreadystatechange = function () {
286
- var response = _getResponseText(xhr);
287
- if (xhr.readyState !== 4) {
288
- //TODO: this should not need, add in case
289
- _handleResponse(oncomplete, xhr[_DYN_STATUS /* @min:%2estatus */], {}, response);
290
- resolveFunc && resolveFunc(false);
291
- }
292
- _handleResponse(oncomplete, xhr[_DYN_STATUS /* @min:%2estatus */], {}, response);
293
- resolveFunc && resolveFunc(true);
294
- };
295
- xhr[_DYN_ONERROR /* @min:%2eonerror */] = function (event) {
296
- _doOnComplete(oncomplete, 400, {}, _formatErrorMessageXhr(xhr));
297
- rejectFunc && rejectFunc(event);
298
- };
299
- xhr.ontimeout = function () {
300
- _doOnComplete(oncomplete, 500, {}, _formatErrorMessageXhr(xhr));
301
- resolveFunc && resolveFunc(false);
302
- };
303
- xhr.send(payload[_DYN_DATA /* @min:%2edata */]);
304
- return thePromise;
305
- }
306
- /**
307
- * Send fetch API request
308
- * @param payload - {string} - The data payload to be sent.
309
- * @param sync - {boolean} - For fetch this identifies whether we are "unloading" (false) or a normal request
310
- */
311
- function _doFetchSender(payload, oncomplete, sync) {
312
- var _a;
313
- var endPointUrl = payload[_DYN_URL_STRING /* @min:%2eurlString */];
314
- var batch = payload[_DYN_DATA /* @min:%2edata */];
315
- var plainTextBatch = new Blob([batch], { type: "application/json" });
316
- var thePromise;
317
- var resolveFunc;
318
- var rejectFunc;
319
- var requestHeaders = new Headers();
320
- var batchLength = batch[_DYN_LENGTH /* @min:%2elength */];
321
- var ignoreResponse = false;
322
- var responseHandled = false;
323
- var headers = payload.headers || [];
324
- //TODO: handle time out for 1ds
325
- arrForEach(objKeys(headers), function (headerName) {
326
- requestHeaders.append(headerName, headers[headerName]);
327
- });
328
- var init = (_a = {
329
- method: "POST",
330
- headers: requestHeaders,
331
- body: plainTextBatch
332
- },
333
- _a[DisabledPropertyName] = true // Mark so we don't attempt to track this request
334
- ,
335
- _a);
336
- if (_sendCredentials && _isOneDs) {
337
- // for 1ds, Don't send credentials when URL is file://
338
- init.credentials = "include";
339
- }
340
- if (sync) {
341
- // since offline will not trigger sync call
342
- // this will not be called, add it here in case
343
- init.keepalive = true;
344
- ignoreResponse = true;
345
- _syncFetchPayload += batchLength;
346
- }
347
- var request = new Request(endPointUrl, init);
348
- try {
349
- // Also try and tag the request (just in case the value in init is not copied over)
350
- request[DisabledPropertyName] = true;
351
- }
352
- catch (e) {
353
- // If the environment has locked down the XMLHttpRequest (preventExtensions and/or freeze), this would
354
- // cause the request to fail and we no telemetry would be sent
355
- }
356
- if (!sync && _enableSendPromise) {
357
- thePromise = createPromise(function (resolve, reject) {
358
- resolveFunc = resolve;
359
- rejectFunc = reject;
360
- });
361
- }
362
- if (!endPointUrl) {
363
- _onNoPayloadUrl(oncomplete);
364
- resolveFunc && resolveFunc(false);
365
- return;
366
- }
367
- try {
368
- doAwaitResponse(fetch(request), function (result) {
369
- if (sync) {
370
- _syncFetchPayload -= batchLength;
371
- batchLength = 0;
372
- }
373
- if (!responseHandled) {
374
- responseHandled = true;
375
- if (!result[_DYN_REJECTED /* @min:%2erejected */]) {
376
- var response_1 = result[_DYN_VALUE /* @min:%2evalue */];
377
- /**
378
- * The Promise returned from fetch() won’t reject on HTTP error status even if the response is an HTTP 404 or 500.
379
- * Instead, it will resolve normally (with ok status set to false), and it will only reject on network failure
380
- * or if anything prevented the request from completing.
381
- */
382
- if (!response_1.ok) {
383
- _doOnComplete(oncomplete, 400, {}, response_1.statusText);
384
- resolveFunc && resolveFunc(false);
385
- }
386
- else {
387
- doAwaitResponse(response_1.text(), function (resp) {
388
- var status = response_1[_DYN_STATUS /* @min:%2estatus */];
389
- _handleResponse(oncomplete, status, {}, response_1.statusText);
390
- resolveFunc && resolveFunc(true);
391
- });
392
- }
393
- }
394
- else {
395
- _doOnComplete(oncomplete, 400, {}, result[_DYN_REASON /* @min:%2ereason */] && result[_DYN_REASON /* @min:%2ereason */].message);
396
- rejectFunc && rejectFunc(result[_DYN_REASON /* @min:%2ereason */]);
397
- }
398
- }
399
- });
400
- }
401
- catch (e) {
402
- if (!responseHandled) {
403
- _doOnComplete(oncomplete, 400, {}, dumpObj(e));
404
- rejectFunc && rejectFunc(e);
405
- }
406
- }
407
- if (ignoreResponse && !responseHandled) {
408
- // Assume success during unload processing as we most likely won't get the response
409
- responseHandled = true;
410
- _doOnComplete(oncomplete, 200, {});
411
- resolveFunc && resolveFunc(true);
412
- }
413
- return thePromise;
414
- }
415
164
  function _handleResponse(oncomplete, status, headers, response) {
416
165
  if (status == 206 && !_isOneDs) {
417
166
  // for breeze, 206 is partially success, currently consider success
@@ -426,132 +175,38 @@ var Sender = /** @class */ (function () {
426
175
  _doOnComplete(oncomplete, status, headers, response);
427
176
  }
428
177
  }
429
- /**
430
- * Parses the response from the backend.
431
- * @param response - XMLHttpRequest or XDomainRequest response
432
- */
433
- function _parseResponse(response) {
434
- try {
435
- if (response && response !== "") {
436
- var result = getJSON().parse(response);
437
- if (_isOneDs) {
438
- return result;
439
- }
440
- // TODO: handle partial success
441
- // if (result && result.itemsReceived && result.itemsReceived >= result.itemsAccepted &&
442
- // result.itemsReceived - result.itemsAccepted === result.errors.length) {
443
- // return result;
444
- // }
445
- if (result && result.itemsReceived) {
446
- return result;
447
- }
448
- }
449
- }
450
- catch (e) {
451
- _throwInternal(_diagLog, 1 /* eLoggingSeverity.CRITICAL */, 43 /* _eInternalMessageId.InvalidBackendResponse */, "Cannot parse the response. " + getExceptionName(e), {
452
- response: response
453
- });
454
- }
455
- return null;
456
- }
457
178
  function _clearScheduledTimer() {
458
179
  _retryAt = null;
459
180
  }
460
- function _formatErrorMessageXhr(xhr, message) {
461
- if (xhr) {
462
- return "XMLHttpRequest,Status:" + xhr[_DYN_STATUS /* @min:%2estatus */] + ",Response:" + _getResponseText(xhr) || xhr.response || "";
463
- }
464
- return message;
465
- }
466
- /**
467
- * Send XDomainRequest
468
- * @param payload - {string} - The data payload to be sent.
469
- * @param sync - {boolean} - Indicates if the request should be sent synchronously
470
- *
471
- * Note: XDomainRequest does not support sync requests. This 'isAsync' parameter is added
472
- * to maintain consistency with the xhrSender's contract
473
- * Note: XDomainRequest does not support custom headers and we are not able to get
474
- * appId from the backend for the correct correlation.
475
- */
476
- function _xdrSender(payload, oncomplete, sync) {
477
- // It doesn't support custom headers, so no action is taken with current requestHeaders
478
- var _window = getWindow();
479
- var xdr = new XDomainRequest();
480
- var data = payload[_DYN_DATA /* @min:%2edata */];
481
- xdr.onload = function () {
482
- var response = _getResponseText(xdr);
483
- if (_isOneDs) {
484
- // for 1ds. we will assume onload means the request succeeded.
485
- _doOnComplete(oncomplete, 200, {}, response);
486
- }
487
- else {
488
- _xdrOnLoad(xdr, oncomplete);
489
- }
490
- };
491
- xdr[_DYN_ONERROR /* @min:%2eonerror */] = function () {
492
- _doOnComplete(oncomplete, 400, {}, _formatErrorMessageXdr(xdr));
493
- };
494
- xdr.ontimeout = function () {
495
- _doOnComplete(oncomplete, 500, {});
496
- };
497
- xdr.onprogress = function () { };
498
- // XDomainRequest requires the same protocol as the hosting page.
499
- // If the protocol doesn't match, we can't send the telemetry :(.
500
- var hostingProtocol = _window && _window.location && _window.location.protocol || "";
501
- var endpoint = payload[_DYN_URL_STRING /* @min:%2eurlString */];
502
- if (!endpoint) {
503
- _onNoPayloadUrl(oncomplete);
504
- return;
505
- }
506
- if (endpoint.lastIndexOf(hostingProtocol, 0) !== 0) {
507
- _throwInternal(_diagLog, 2 /* eLoggingSeverity.WARNING */, 40 /* _eInternalMessageId.TransmissionFailed */, ". " +
508
- "Cannot send XDomain request. The endpoint URL protocol doesn't match the hosting page protocol.");
509
- return;
510
- }
511
- var endpointUrl = endpoint[_DYN_REPLACE /* @min:%2ereplace */](/^(https?:)/, "");
512
- xdr.open("POST", endpointUrl);
513
- xdr.send(data);
514
- }
515
181
  /**
516
182
  * xdr state changes
517
183
  */
518
184
  function _xdrOnLoad(xdr, oncomplete) {
519
- var responseText = _getResponseText(xdr);
185
+ var responseText = getResponseText(xdr);
520
186
  if (xdr && (responseText + "" === "200" || responseText === "")) {
521
187
  _consecutiveErrors = 0;
522
188
  _onSuccess(responseText, oncomplete);
523
189
  }
524
190
  else {
525
- var results = _parseResponse(responseText);
191
+ var results = parseResponse(responseText, _diagLog);
526
192
  if (results && results.itemsAccepted) {
527
193
  // TODO: onPartial success for appInsights
528
194
  _onSuccess(responseText, oncomplete);
529
195
  }
530
196
  else {
531
- _onError(_formatErrorMessageXdr(xdr), oncomplete);
197
+ _onError(formatErrorMessageXdr(xdr), oncomplete);
532
198
  }
533
199
  }
534
200
  }
535
- function _formatErrorMessageXdr(xdr, message) {
536
- if (xdr) {
537
- return "XDomainRequest,Response:" + _getResponseText(xdr) || "";
538
- }
539
- return message;
540
- }
541
- // TDOD: add notification manager
542
- // TODO: handler one collector "MSFPC"
543
201
  function _initDefaults() {
544
202
  _self._appId = null;
545
203
  _consecutiveErrors = 0;
546
204
  _retryAt = null;
547
205
  _paused = false;
548
- _stamp_specific_redirects = 0;
549
- _syncFetchPayload = 0;
550
- _disableXhr = false;
551
206
  _isInitialized = false;
552
- _fallbackSend = null;
553
207
  _core = null;
554
208
  _onlineChannelId = null;
209
+ _sendPostMgr = null;
555
210
  }
556
211
  });
557
212
  }