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

fix: passthrough for binary files #363

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 26 additions & 10 deletions src/create-passthrough.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export function createPassthrough(fakeXHR, nativeXMLHttpRequest) {
fakeXHR.password
);

if (fakeXHR.responseType === 'arraybuffer') {
if (
fakeXHR.responseType === 'blob' ||
fakeXHR.responseType === 'arraybuffer'
) {
lifecycleProps = ['readyState', 'response', 'status', 'statusText'];
xhr.responseType = fakeXHR.responseType;
}
Expand All @@ -37,7 +40,7 @@ export function createPassthrough(fakeXHR, nativeXMLHttpRequest) {

// add progress event for async calls
// avoid using progress events for sync calls, they will hang https://bugs.webkit.org/show_bug.cgi?id=40996.
if (fakeXHR.async && fakeXHR.responseType !== 'arraybuffer') {
if (fakeXHR.async) {
evts.push('progress');
uploadEvents.push('progress');
}
Expand All @@ -55,26 +58,39 @@ export function createPassthrough(fakeXHR, nativeXMLHttpRequest) {
// fire fake event on `eventable`
function dispatchEvent(eventable, eventType, event) {
eventable.dispatchEvent(event);
if (eventable['on' + eventType]) {
eventable['on' + eventType](event);
}
}

// set the on- handler on the native xhr for the given eventType
function createHandler(eventType) {
xhr['on' + eventType] = function (event) {
const fakeEventKey = 'on' + eventType;

if (fakeXHR[fakeEventKey]) {
const fn = fakeXHR[fakeEventKey];
delete fakeXHR[fakeEventKey];
fakeXHR.addEventListener(eventType, fn);
}

xhr.addEventListener(eventType, function (event) {
copyLifecycleProperties(lifecycleProps, xhr, fakeXHR);
dispatchEvent(fakeXHR, eventType, event);
};
});
}

// set the on- handler on the native xhr's `upload` property for
// the given eventType
function createUploadHandler(eventType) {
if (xhr.upload && fakeXHR.upload && fakeXHR.upload['on' + eventType]) {
xhr.upload['on' + eventType] = function (event) {
if (xhr.upload && fakeXHR.upload) {
const fakeEventKey = 'on' + eventType;

if (fakeXHR.upload[fakeEventKey]) {
const fn = fakeXHR.upload[fakeEventKey];
delete fakeXHR.upload[fakeEventKey];
fakeXHR.upload.addEventListener(eventType, fn);
}

xhr.upload.addEventListener(eventType, function (event) {
dispatchEvent(fakeXHR.upload, eventType, event);
};
});
}
}

Expand Down