in channels/applicationinsights-channel-js/src/Sender.ts [762:851]
function _doFetchSender(payload: string[], isAsync: boolean) {
const endPointUrl = _self._senderConfig.endpointUrl();
const batch = _self._buffer.batchPayloads(payload);
const plainTextBatch = new Blob([batch], { type: "application/json" });
let requestHeaders = new Headers();
let batchLength = batch.length;
let ignoreResponse = false;
let responseHandled = false;
// append Sdk-Context request header only in case of breeze endpoint
if (isInternalApplicationInsightsEndpoint(endPointUrl)) {
requestHeaders.append(RequestHeaders.sdkContextHeader, RequestHeaders.sdkContextHeaderAppIdRequest);
}
arrForEach(objKeys(_headers), (headerName) => {
requestHeaders.append(headerName, _headers[headerName]);
});
const init: RequestInit = {
method: "POST",
headers: requestHeaders,
body: plainTextBatch,
[DisabledPropertyName]: true // Mark so we don't attempt to track this request
};
if (!isAsync) {
init.keepalive = true;
// As a sync request (during unload), it is unlikely that we will get a chance to process the response so
// just like beacon send assume that the events have been accepted and processed
ignoreResponse = true;
_syncFetchPayload += batchLength;
}
const request = new Request(endPointUrl, init);
try {
// Also try and tag the request (just in case the value in init is not copied over)
request[DisabledPropertyName] = true;
} catch(e) {
// If the environment has locked down the XMLHttpRequest (preventExtensions and/or freeze), this would
// cause the request to fail and we no telemetry would be sent
}
_self._buffer.markAsSent(payload);
try {
fetch(request).then(response => {
if (!isAsync) {
_syncFetchPayload -= batchLength;
batchLength = 0;
}
if (!responseHandled) {
responseHandled = true;
/**
* The Promise returned from fetch() won’t reject on HTTP error status even if the response is an HTTP 404 or 500.
* Instead, it will resolve normally (with ok status set to false), and it will only reject on network failure
* or if anything prevented the request from completing.
*/
if (!response.ok) {
_self._onError(payload, response.statusText)
} else {
response.text().then(text => {
_checkResponsStatus(response.status, payload, response.url, payload.length, response.statusText, text);
});
}
}
}).catch((error: Error) => {
if (!isAsync) {
_syncFetchPayload -= batchLength;
batchLength = 0;
}
if (!responseHandled) {
responseHandled = true;
_self._onError(payload, error.message)
}
});
} catch (e) {
if (!responseHandled) {
_self._onError(payload, dumpObj(e));
}
}
if (ignoreResponse && !responseHandled) {
// Assume success during unload processing as we most likely won't get the response
responseHandled = true;
_self._onSuccess(payload, payload.length);
}
}