Skip to content

Commit

Permalink
refactor(instrumentation-fetch): fix eslint warnings (open-telemetry#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chancancode authored Jan 30, 2025
1 parent c513965 commit 29d0da5
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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');
Expand Down

0 comments on commit 29d0da5

Please # to comment.