@@ -30,7 +30,13 @@ export function instrumentXHR(): void {
3030
3131 // eslint-disable-next-line @typescript-eslint/unbound-method
3232 xhrproto . open = new Proxy ( xhrproto . open , {
33- apply ( originalOpen , xhrOpenThisArg : XMLHttpRequest & SentryWrappedXMLHttpRequest , xhrOpenArgArray ) {
33+ apply (
34+ originalOpen ,
35+ xhrOpenThisArg : XMLHttpRequest & SentryWrappedXMLHttpRequest ,
36+ xhrOpenArgArray :
37+ | [ method : string , url : string | URL ]
38+ | [ method : string , url : string | URL , async : boolean , username ?: string | null , password ?: string | null ] ,
39+ ) {
3440 // NOTE: If you are a Sentry user, and you are seeing this stack frame,
3541 // it means the error, that was caused by your XHR call did not
3642 // have a stack trace. If you are using HttpClient integration,
@@ -43,7 +49,7 @@ export function instrumentXHR(): void {
4349 // open() should always be called with two or more arguments
4450 // But to be on the safe side, we actually validate this and bail out if we don't have a method & url
4551 const method = isString ( xhrOpenArgArray [ 0 ] ) ? xhrOpenArgArray [ 0 ] . toUpperCase ( ) : undefined ;
46- const url = parseUrl ( xhrOpenArgArray [ 1 ] ) ;
52+ const url = parseXhrUrlArg ( xhrOpenArgArray [ 1 ] ) ;
4753
4854 if ( ! method || ! url ) {
4955 return originalOpen . apply ( xhrOpenThisArg , xhrOpenArgArray ) ;
@@ -147,16 +153,23 @@ export function instrumentXHR(): void {
147153 } ) ;
148154}
149155
150- function parseUrl ( url : string | unknown ) : string | undefined {
156+ /**
157+ * Parses the URL argument of a XHR method to a string.
158+ *
159+ * See: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/open#url
160+ * url: A string or any other object with a stringifier — including a URL object — that provides the URL of the resource to send the request to.
161+ *
162+ * @param url - The URL argument of an XHR method
163+ * @returns The parsed URL string or undefined if the URL is invalid
164+ */
165+ function parseXhrUrlArg ( url : unknown ) : string | undefined {
151166 if ( isString ( url ) ) {
152167 return url ;
153168 }
154169
155170 try {
156- // url can be a string or URL
157- // but since URL is not available in IE11, we do not check for it,
158- // but simply assume it is an URL and return `toString()` from it (which returns the full URL)
159- // If that fails, we just return undefined
171+ // If the passed in argument is not a string, it should have a `toString` method as a stringifier.
172+ // If that fails, we just return undefined (like in IE11 where URL is not available)
160173 return ( url as URL ) . toString ( ) ;
161174 } catch { } // eslint-disable-line no-empty
162175
0 commit comments