From 4fc4890dc50e7269ee5aa635a93496bb063c82ab Mon Sep 17 00:00:00 2001 From: demensky Date: Mon, 13 Feb 2023 01:30:46 +0200 Subject: [PATCH] refactor: remove legacy code for IE in `ajax` BREAKING CHANGE: `ajax` no longer supports IE --- spec/observables/dom/ajax-spec.ts | 45 ----------------------------- src/internal/ajax/AjaxResponse.ts | 3 +- src/internal/ajax/ajax.ts | 13 +-------- src/internal/ajax/errors.ts | 11 +------ src/internal/ajax/getXHRResponse.ts | 37 ------------------------ 5 files changed, 3 insertions(+), 106 deletions(-) delete mode 100644 src/internal/ajax/getXHRResponse.ts diff --git a/spec/observables/dom/ajax-spec.ts b/spec/observables/dom/ajax-spec.ts index 761449be69..5f105429de 100644 --- a/spec/observables/dom/ajax-spec.ts +++ b/spec/observables/dom/ajax-spec.ts @@ -330,37 +330,6 @@ describe('ajax', () => { expect(complete).to.be.true; }); - it('should fail if fails to parse response in older IE', () => { - let error: any; - const obj: AjaxConfig = { - url: '/flibbertyJibbet', - method: '', - }; - - // No `response` property on the object (for older IE). - MockXMLHttpRequest.noResponseProp = true; - - ajax(obj).subscribe({ - next: () => { - throw new Error('should not next'); - }, - error: (err: any) => { - error = err; - }, - complete: () => { - throw new Error('should not complete'); - }, - }); - - MockXMLHttpRequest.mostRecent.respondWith({ - status: 207, - responseText: 'Wee! I am text, but should be valid JSON!', - }); - - expect(error instanceof SyntaxError).to.be.true; - expect(error.message).to.equal('Unexpected token W in JSON at position 0'); - }); - it('should fail on 404', () => { let error: any; const obj: AjaxConfig = { @@ -1583,11 +1552,6 @@ class MockXHREventTarget { class MockXMLHttpRequest extends MockXHREventTarget { static readonly DONE = 4; - /** - * Set to `true` to test IE code paths. - */ - static noResponseProp = false; - private static requests: Array = []; private static recentRequest: MockXMLHttpRequest; @@ -1600,7 +1564,6 @@ class MockXMLHttpRequest extends MockXHREventTarget { } static clearRequest(): void { - MockXMLHttpRequest.noResponseProp = false; MockXMLHttpRequest.requests.length = 0; MockXMLHttpRequest.recentRequest = null!; } @@ -1639,9 +1602,6 @@ class MockXMLHttpRequest extends MockXHREventTarget { super(); MockXMLHttpRequest.recentRequest = this; MockXMLHttpRequest.requests.push(this); - if (MockXMLHttpRequest.noResponseProp) { - delete this['response']; - } } // @ts-ignore: Property has no initializer and is not definitely assigned @@ -1755,11 +1715,6 @@ class MockXMLHttpRequest extends MockXHREventTarget { break; } - // We're testing old IE, forget all of that response property stuff. - if (MockXMLHttpRequest.noResponseProp) { - delete this['response']; - } - this.triggerEvent('load', { type: 'load', total: response.total ?? 0, loaded: response.loaded ?? 0 }); this.triggerEvent('readystatechange', { type: 'readystatechange' }); } diff --git a/src/internal/ajax/AjaxResponse.ts b/src/internal/ajax/AjaxResponse.ts index c9ca915bea..e0c1a26bc5 100644 --- a/src/internal/ajax/AjaxResponse.ts +++ b/src/internal/ajax/AjaxResponse.ts @@ -1,5 +1,4 @@ import { AjaxRequest, AjaxResponseType } from './types'; -import { getXHRResponse } from './getXHRResponse'; /** * A normalized response from an AJAX request. To get the data from the response, @@ -116,7 +115,7 @@ export class AjaxResponse { }, {}) : {}; - this.response = getXHRResponse(xhr); + this.response = xhr.response; const { loaded, total } = originalEvent; this.loaded = loaded; this.total = total; diff --git a/src/internal/ajax/ajax.ts b/src/internal/ajax/ajax.ts index b1628dad81..268b9ead6e 100644 --- a/src/internal/ajax/ajax.ts +++ b/src/internal/ajax/ajax.ts @@ -481,18 +481,7 @@ export function fromAjax(init: AjaxConfig): Observable> { if (status < 400) { progressSubscriber?.complete?.(); - let response: AjaxResponse; - try { - // This can throw in IE, because we end up needing to do a JSON.parse - // of the response in some cases to produce object we'd expect from - // modern browsers. - response = createResponse(DOWNLOAD, event); - } catch (err) { - destination.error(err); - return; - } - - destination.next(response); + destination.next(createResponse(DOWNLOAD, event)); destination.complete(); } else { progressSubscriber?.error?.(event); diff --git a/src/internal/ajax/errors.ts b/src/internal/ajax/errors.ts index bb220a2481..f64f1df1c4 100644 --- a/src/internal/ajax/errors.ts +++ b/src/internal/ajax/errors.ts @@ -1,5 +1,4 @@ import { AjaxRequest } from './types'; -import { getXHRResponse } from './getXHRResponse'; import { createErrorClass } from '../util/createErrorClass'; /** @@ -63,15 +62,7 @@ export const AjaxError: AjaxErrorCtor = createErrorClass( this.request = request; this.status = xhr.status; this.responseType = xhr.responseType; - let response: any; - try { - // This can throw in IE, because we have to do a JSON.parse of - // the response in some cases to get the expected response property. - response = getXHRResponse(xhr); - } catch (err) { - response = xhr.responseText; - } - this.response = response; + this.response = xhr.response; } ); diff --git a/src/internal/ajax/getXHRResponse.ts b/src/internal/ajax/getXHRResponse.ts deleted file mode 100644 index 34d7031a49..0000000000 --- a/src/internal/ajax/getXHRResponse.ts +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Gets what should be in the `response` property of the XHR. However, - * since we still support the final versions of IE, we need to do a little - * checking here to make sure that we get the right thing back. Consequently, - * we need to do a JSON.parse() in here, which *could* throw if the response - * isn't valid JSON. - * - * This is used both in creating an AjaxResponse, and in creating certain errors - * that we throw, so we can give the user whatever was in the response property. - * - * @param xhr The XHR to examine the response of - */ -export function getXHRResponse(xhr: XMLHttpRequest) { - switch (xhr.responseType) { - case 'json': { - if ('response' in xhr) { - return xhr.response; - } else { - // IE - const ieXHR: any = xhr; - return JSON.parse(ieXHR.responseText); - } - } - case 'document': - return xhr.responseXML; - case 'text': - default: { - if ('response' in xhr) { - return xhr.response; - } else { - // IE - const ieXHR: any = xhr; - return ieXHR.responseText; - } - } - } -}