GithubHelp home page GithubHelp logo

xhr.upload is null about interceptors HOT 4 CLOSED

mswjs avatar mswjs commented on May 16, 2024 1
xhr.upload is null

from interceptors.

Comments (4)

kettanaito avatar kettanaito commented on May 16, 2024 2

Hey, @jameskerr. Thank you for raising this up.

The upload property of the XMLHttpRequest override class is currently set to null in the constructor and isn't modifier since then:

According to the documentation, XMLHttpRequest.upload is an XMLHttpRequestEventTarget with the support for certain progress events. My initial thought is to introduce a custom event handling for the upload property, similar to how the regular xhr.addEventListner() events are collected and executed at the right stage of the request:

public addEventListener<
K extends keyof InternalXMLHttpRequestEventTargetEventMap
>(name: K, listener: XMLHttpRequestEventHandler) {
debug('addEventListener', name, listener)
this._events.push({
name,
listener,
})
}
public removeEventListener<K extends keyof XMLHttpRequestEventMap>(
name: K,
listener: (event?: XMLHttpRequestEventMap[K]) => void
): void {
debug('removeEventListener', name, listener)
this._events = this._events.filter((storedEvent) => {
return storedEvent.name !== name && storedEvent.listener !== listener
})
}

trigger<
K extends keyof (XMLHttpRequestEventTargetEventMap & {
readystatechange: ProgressEvent<XMLHttpRequestEventTarget>
})
>(eventName: K, options?: ProgressEventInit) {
debug('trigger "%s" (%d)', eventName, this.readyState)
debug('resolve listener for event "%s"', eventName)
// @ts-expect-error XMLHttpRequest class has no index signature.
const callback = this[`on${eventName}`] as XMLHttpRequestEventHandler
callback?.call(this, createEvent(this, eventName, options))
for (const event of this._events) {
if (event.name === eventName) {
debug(
'calling mock event listener "%s" (%d)',
eventName,
this.readyState
)
event.listener.call(this, createEvent(this, eventName, options))
}
}
return this
}

What do you think about this approach?

from interceptors.

jameskerr avatar jameskerr commented on May 16, 2024 1

Thanks for the update. Sorry I never responded. I hadn't figured out my github notifications back then!

from interceptors.

kettanaito avatar kettanaito commented on May 16, 2024

We may want to put this issue on hold. It's possible we'll rewrite the XHR interception from the ground-up to extend the native XMLHttpRequest class instead of patching it (similar to what we did with ClientRequest in #164).

from interceptors.

kettanaito avatar kettanaito commented on May 16, 2024

@johnsonlin has kindly provided us with a temporary fix where this.upload is set to an empty object until we decide to implement a proper polyfill for it (see #187). I'm closing this issue in favor of the said polyfill proposal.

from interceptors.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.