Skip to content

Commit

Permalink
refactor: remove legacy code for IE in ajax
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `ajax` no longer supports IE
  • Loading branch information
demensky committed Feb 12, 2023
1 parent 7d3c4ec commit 0e11af7
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 94 deletions.
45 changes: 0 additions & 45 deletions spec/observables/dom/ajax-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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<MockXMLHttpRequest> = [];
private static recentRequest: MockXMLHttpRequest;

Expand All @@ -1600,7 +1564,6 @@ class MockXMLHttpRequest extends MockXHREventTarget {
}

static clearRequest(): void {
MockXMLHttpRequest.noResponseProp = false;
MockXMLHttpRequest.requests.length = 0;
MockXMLHttpRequest.recentRequest = null!;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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' });
}
Expand Down
13 changes: 1 addition & 12 deletions src/internal/ajax/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,18 +481,7 @@ export function fromAjax<T>(init: AjaxConfig): Observable<AjaxResponse<T>> {
if (status < 400) {
progressSubscriber?.complete?.();

let response: AjaxResponse<T>;
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);
Expand Down
10 changes: 1 addition & 9 deletions src/internal/ajax/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,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 = getXHRResponse(xhr);
}
);

Expand Down
30 changes: 2 additions & 28 deletions src/internal/ajax/getXHRResponse.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,11 @@
/**
* 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.
* Gets what should be in the `response` property of the XHR.
*
* 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;
}
}
}
return xhr.responseType === 'document' ? xhr.responseXML : xhr.response;
}

0 comments on commit 0e11af7

Please sign in to comment.