refactor(instrumentation-xhr): fix eslint warnings (#5402)

This commit is contained in:
Godfrey Chan 2025-02-06 15:43:34 -08:00 committed by GitHub
parent 6508845fa6
commit e744798957
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 11 deletions

View File

@ -63,7 +63,7 @@ export interface XhrMem {
entries: PerformanceResourceTiming[];
};
// callback to remove events from xhr once the span ends
callbackToRemoveEvents?: Function;
callbackToRemoveEvents?: () => void;
}
export type PropagateTraceHeaderCorsUrl = string | RegExp;

View File

@ -24,6 +24,10 @@ const DIAG_LOGGER = api.diag.createComponentLogger({
'@opentelemetry/opentelemetry-instrumentation-xml-http-request/utils',
});
function isDocument(value: unknown): value is Document {
return typeof Document !== 'undefined' && value instanceof Document;
}
/**
* Helper function to determine payload content length for XHR requests
* @param body
@ -32,17 +36,17 @@ const DIAG_LOGGER = api.diag.createComponentLogger({
export function getXHRBodyLength(
body: Document | XMLHttpRequestBodyInit
): number | undefined {
if (typeof Document !== 'undefined' && body instanceof Document) {
if (isDocument(body)) {
return new XMLSerializer().serializeToString(document).length;
}
// XMLHttpRequestBodyInit expands to the following:
if (body instanceof Blob) {
return body.size;
if (typeof body === 'string') {
return getByteLength(body);
}
// ArrayBuffer | ArrayBufferView
if ((body as any).byteLength !== undefined) {
return (body as any).byteLength as number;
if (body instanceof Blob) {
return body.size;
}
if (body instanceof FormData) {
@ -53,8 +57,9 @@ export function getXHRBodyLength(
return getByteLength(body.toString());
}
if (typeof body === 'string') {
return getByteLength(body);
// ArrayBuffer | ArrayBufferView
if (body.byteLength !== undefined) {
return body.byteLength;
}
DIAG_LOGGER.warn('unknown body type');

View File

@ -66,7 +66,7 @@ const XHR_TIMEOUT = 2000;
const getData = (
req: XMLHttpRequest,
url: string,
callbackAfterSend: Function,
callbackAfterSend: () => void,
async?: boolean
) => {
// eslint-disable-next-line no-async-promise-executor
@ -101,7 +101,7 @@ const postData = (
req: XMLHttpRequest,
url: string,
data: Document | XMLHttpRequestBodyInit,
callbackAfterSend: Function,
callbackAfterSend: () => void,
async?: boolean
) => {
// eslint-disable-next-line no-async-promise-executor