Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(instrumentation-xhr): fix eslint warnings #5402

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any particular reason you moved the order of the checks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi yes, it's notionally the same change from #5401. The type started out (for TypeScript) as a bunch of possibilities union-ed together (string | ... | ArrayBuffer | ArrayBufferView) so by arranging the branches in a strategic order (eliminating those possibilities in order) we will get the desired result without having to explicitly tell TypeScript what's up

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