mswjs / interceptors

Low-level network interception library.
https://npm.im/@mswjs/interceptors
MIT License
539 stars 123 forks source link

XMLHttpRequest upload (upload.onprogress) event is not supported #573

Closed MeetinaXD closed 1 month ago

MeetinaXD commented 4 months ago

Hi, I noticed that #292 mentioned that interceptor has added support for the upload event of XMLHttpRequest, but I tried to use XHR to upload files in the latest version of msw, and the progress event was not triggered.

By the way, #292 mentions adding support for the progress event, but this only works in response.

I'm wondering if it's possible to add support for the progress event in upload?


Environment

To Reproduce

const xhr = new XMLHttpRequest()

// codes...
xhr.upload.onprogress =({ loaded, total }) => {
    console.log(loaded, total) // not triggered
};

xhr.send(someFormData)
pantoninho commented 1 month ago

I am experiencing the same issue. I'm using axios within a node environment and onUploadProgress is not being called either.

kettanaito commented 1 month ago

Hi! Thanks for raising this.

I've opened a fix at #613, and the XMLHttpRequestInterceptor now correctly emits upload events for requests with body (tested on multipart/form-data file uploads).

[!IMPORTANT] This will not be supported in JSDOM. JSDOM does not implement the upload events at all, and I always get a single progress event where both loaded and total are always 0. That is a different behavior from the browser, where the loaded and total are calculated correctly. We are going to use browser as the source of truth from now on and disregard any discrepancies JSOM may introduce. If you encounter any, please raise them with the JSDOM maintainers to provide proper implementation.

kettanaito commented 1 month ago

Released: v0.34.2 🎉

This has been released in v0.34.2!

Make sure to always update to the latest version (npm i @mswjs/interceptors@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

MeetinaXD commented 1 month ago

thank you for explaining the behavior of jsom. it works now. that's amazing and very kind of you, many thanks @kettanaito