in packages/rum-core/src/common/patching/fetch-patch.js [73:146]
window.fetch = function (input, init) {
var fetchSelf = this
var args = arguments
var request, url
var isURL = input instanceof URL
if (typeof input === 'string' || isURL) {
request = new Request(input, init)
if (isURL) {
url = request.url
} else {
// when the input is a string, the url value should be copied from it, rather than from request.url
// this is important, there are existing users using relative urls when using fetch, which generates a span.name like 'GET /path-example'
// switching to request.url like we do now with the introduction of URL objects support would start changing existing spans
// the name would start being something like 'GET http://the-url-of-the-web-page.tld/path-example' which would cause unexpected results in the Kibana side
url = input
}
} else if (input) {
request = input
url = request.url
} else {
return nativeFetch.apply(fetchSelf, args)
}
const task = {
source: FETCH,
state: '',
type: 'macroTask',
data: {
target: request,
method: request.method,
url,
aborted: false
}
}
return new Promise(function (resolve, reject) {
globalState.fetchInProgress = true
scheduleTask(task)
var promise
try {
promise = nativeFetch.apply(fetchSelf, [request])
} catch (error) {
reject(error)
task.data.error = error
invokeTask(task)
globalState.fetchInProgress = false
return
}
promise.then(
response => {
let clonedResponse = response.clone ? response.clone() : {}
resolve(response)
// invokeTask in the next execution cycle to let the promise resolution complete
scheduleMicroTask(() => {
task.data.response = response
const { body } = clonedResponse
if (body) {
readStream(body, task)
} else {
invokeTask(task)
}
})
},
error => {
reject(error)
scheduleMicroTask(() => {
handleResponseError(task, error)
})
}
)
globalState.fetchInProgress = false
})
}