algolia / faux-jax

NO MORE MAINTAINED: Intercept and respond to requests in the browser (AJAX) and Node.js (http(s) module)
MIT License
95 stars 9 forks source link

Sync XHR support #14

Closed kumavis closed 8 years ago

kumavis commented 8 years ago

This change allows synchronous xhr results to be submitted synchronously if request.respond is called in the same frame as the request event was emitted.

conversely, this also guarantees async xhrs are guaranteed to be async even if request.respond is called in the same frame as the request event was emitted.

Added a test to ensure this behavior. All tests pass.

vvo commented 8 years ago

Hi thanks for the PR. I would say, even if the previous behavior was maybe not the good one, this is probably a breaking change since the ('on', fn(request)) is now called only when send() is called.

What do you think?

vvo commented 8 years ago

Tested it on one of our projects and it works well

vvo commented 8 years ago

tests are failing on node 0.12, I believe this is due to the change between setTimeout and process.nextTick

I would not bare understand the problem fully, node-mitm is pretty hacky already so it might just be a complex timers issue.

Maybe switch back to process.nextTick for now?

vvo commented 8 years ago

Maybe we also need a specific async test, for consistency

kumavis commented 8 years ago

yeah i found the test failures to be a bit mysterious.. seems unrelated but I assume they were passing previously -- however the change I made is only in browser.js so its not run in the node tests, right?

kumavis commented 8 years ago

yeah here is the same failure from 10 days ago https://travis-ci.org/algolia/faux-jax/jobs/104474441 from https://github.com/algolia/faux-jax/compare/89637d387fd4...049d2e6c2d14

must be a race condition?

vvo commented 8 years ago

PR is ok on my side when tested, I upgraded the master branch of faux-jax to handle the thrown exception

vvo commented 8 years ago

Merged, I will add you as a collaborator to the project also, so you can contribute... MOAR

vvo commented 8 years ago

Added you to both github and npm, feel free to do a release:

That's it :)

Where are you using faux-jax?

kumavis commented 8 years ago

Thanks : )

I'm working on a chrome extension that extends the normal web api with support for Ethereum, a blockchain based decentralized app platform. The ecosystem is quite early, and many ethereum apps attempt to connect to the network the same way they've been developed, connecting directly to localhost. I used faux-jax to optionally man-in-the-middle connections to the localhost and handle them normally. There are some chrome apis for doing this from an extension but they fail if CORS preflight fails. To be honest I can't believe you can actually overwrite window.XMLHttpRequest, but that is the world today.

kumavis commented 8 years ago

I also noticed you have an oakland.js shirt -- cool!

vvo commented 8 years ago

To be honest I can't believe you can actually overwrite window.XMLHttpRequest, but that is the world today

Yes, we even wrote something as a tiny module to do so in very old browsers you do not want to know about but here it is: https://github.com/algolia/writable-window-method

I also noticed you have an oakland.js shirt -- cool!

Yes as a "fan" of @substack :)

kumavis commented 8 years ago

writable-window-method

woah. nope, no thanks. I am able to pretend IE doesnt exist and I'm happier for it.