refactor(instrumentation-fetch): fix eslint warnings (#5401)

This commit is contained in:
Godfrey Chan 2025-01-30 08:40:10 -08:00 committed by GitHub
parent c513965062
commit 29d0da559b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 8 deletions

View File

@ -117,6 +117,10 @@ function _getBodyNonDestructively(body: ReadableStream) {
};
}
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
@ -125,17 +129,17 @@ function _getBodyNonDestructively(body: ReadableStream) {
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) {
@ -146,8 +150,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');