Skip to content

Commit

Permalink
refactor(instrumentation-fetch): fix eslint warnings
Browse files Browse the repository at this point in the history
```
/home/runner/work/opentelemetry-js/opentelemetry-js/experimental/packages/opentelemetry-instrumentation-fetch/src/utils.ts
  137:16  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any
  138:21  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any
```

This can be avoided by re-ordering the branches so that TypeScript
can narrow the type of the value progressively.

Ref #5365
  • Loading branch information
chancancode committed Jan 30, 2025
1 parent 3c040c4 commit c1fb762
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 c1fb762

Please sign in to comment.