From 464f1b799d1c3553dd6743305b2d7c4d25a4ae9f Mon Sep 17 00:00:00 2001 From: Brad Overton Date: Mon, 29 Jan 2024 13:27:17 +1000 Subject: [PATCH 1/2] fix: passthrough for binary files --- src/create-passthrough.ts | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/create-passthrough.ts b/src/create-passthrough.ts index 0ed7d9a..eaaa745 100644 --- a/src/create-passthrough.ts +++ b/src/create-passthrough.ts @@ -25,7 +25,7 @@ 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; } @@ -37,7 +37,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'); } @@ -55,26 +55,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); - }; + }); } } From 5167433d05aa2e61204827d3d6e34a5dbc955e3e Mon Sep 17 00:00:00 2001 From: Brad Overton Date: Mon, 29 Jan 2024 13:39:00 +1000 Subject: [PATCH 2/2] chore: formatting --- src/create-passthrough.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/create-passthrough.ts b/src/create-passthrough.ts index eaaa745..ef4563f 100644 --- a/src/create-passthrough.ts +++ b/src/create-passthrough.ts @@ -25,7 +25,10 @@ export function createPassthrough(fakeXHR, nativeXMLHttpRequest) { fakeXHR.password ); - if (fakeXHR.responseType === 'blob' || fakeXHR.responseType === 'arraybuffer') { + if ( + fakeXHR.responseType === 'blob' || + fakeXHR.responseType === 'arraybuffer' + ) { lifecycleProps = ['readyState', 'response', 'status', 'statusText']; xhr.responseType = fakeXHR.responseType; } @@ -59,7 +62,7 @@ export function createPassthrough(fakeXHR, nativeXMLHttpRequest) { // set the on- handler on the native xhr for the given eventType function createHandler(eventType) { - const fakeEventKey = 'on'+eventType; + const fakeEventKey = 'on' + eventType; if (fakeXHR[fakeEventKey]) { const fn = fakeXHR[fakeEventKey]; @@ -77,9 +80,9 @@ export function createPassthrough(fakeXHR, nativeXMLHttpRequest) { // the given eventType function createUploadHandler(eventType) { if (xhr.upload && fakeXHR.upload) { - const fakeEventKey = 'on'+eventType; + const fakeEventKey = 'on' + eventType; - if(fakeXHR.upload[fakeEventKey]){ + if (fakeXHR.upload[fakeEventKey]) { const fn = fakeXHR.upload[fakeEventKey]; delete fakeXHR.upload[fakeEventKey]; fakeXHR.upload.addEventListener(eventType, fn);