Skip to content

Commit

Permalink
refactor(instrumentation-xhr): 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-xml-http-request/src/types.ts
  66:28  warning  Don't use `Function` as a type  @typescript-eslint/ban-types

/home/runner/work/opentelemetry-js/opentelemetry-js/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/utils.ts
  44:16  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any
  45:21  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any

/home/runner/work/opentelemetry-js/opentelemetry-js/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts
  69:22  warning  Don't use `Function` as a type  @typescript-eslint/ban-types
  104:22  warning  Don't use `Function` as a type  @typescript-eslint/ban-types
```

The middle ones are the same as #5401, the rest are replacing
`Function` with a more specific call signature, and they are either
in tests or private API, so no breakages expected.

Ref #5365
  • Loading branch information
chancancode committed Jan 30, 2025
1 parent 3c040c4 commit edddf58
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit edddf58

Please sign in to comment.