From edddf58269d7398785aa28fe66c6a300b15408b0 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Wed, 29 Jan 2025 18:34:19 -0800 Subject: [PATCH] refactor(instrumentation-xhr): fix eslint warnings ``` /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 --- .../src/types.ts | 2 +- .../src/utils.ts | 21 ++++++++++++------- .../test/xhr.test.ts | 4 ++-- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/types.ts b/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/types.ts index 430e9e217bc..8014e71e9d1 100644 --- a/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/types.ts +++ b/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/types.ts @@ -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; diff --git a/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/utils.ts b/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/utils.ts index c15e84efdd1..b4884bb8968 100644 --- a/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/utils.ts @@ -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'); diff --git a/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts b/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts index 823d6f942e9..e2f74d48b74 100644 --- a/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts @@ -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