greim / hoxy

Web-hacking proxy API for node
http://greim.github.io/hoxy/
MIT License
599 stars 97 forks source link

Phase transition idea. #76

Open greim opened 8 years ago

greim commented 8 years ago

Idea: add a "phaseless" feature which would allow you to do:

proxy.intercept({
  // same options, except "phase" is now optional
}, function*(request, response) {
  // if you omitted the phase option, then you must
  // manually step from phase to phase.

  // you start out in the 'request' phase
  yield request.toServer();
  // you are now in the 'request-sent' phase
  yield response.fromServer();
  // you are now in the 'response' phase
  yield response.toClient();
  // you are now in the 'response-sent' phase
});

This would allow handling errors that happen between phases within the scope of an intercept, which currently isn't possible. Example:

proxy.intercept({
  host: 'example.com'
}, function*(request, response) {
  yield request.toServer();
  try { yield response.fromServer(); }
  catch(ex) { response.statusCode = 502; }
  yield response.toClient();
});
greim commented 8 years ago

@mitchhentges @sholladay @nerdbeere thoughts?

mitchhentges commented 8 years ago

That seems way more flexible than before, so it sounds good to me! Looking forward to nerdbeere and sholladay's input, though.

sholladay commented 8 years ago

It certainly is flexible. And that is an excellent use of yield, which is pretty cool.

As for the example, I'm inclined to think that hoxy should be responsible for complying with the HTTP spec for the most part and I shouldn't have to worry about implementing that. I'm sure there are other awesome use cases, though. The only other hangup for me is that, to my eyes, this exacerbates the confusion I tried to describe in #72 related to what the request/response actually represents.

In the example, when you do response.fromServer(), you're not really going to have access to the response from the server, but instead a potentially mutated copy populated with stuff for the client. The kinds of proxies described in #48 benefit a lot from that distinction being clear and fluent.