Sage/streamlinejs — Issue #72: server-stream HttpClientRequest.write does not propagate error
Author: encoder
Created: 2012-02-25 21:56:30+00:00
State: open

The following code hangs (intentionally connecting to an unserviced port):

var request = streams.httpRequest({
url: 'http://www.github.com:81',
method: 'POST',
});

var response = request.write(_, "test").end().response(_);

The "Connection refused" error is only propagated to response(_), not to write(_), and thus write(_) hangs as the drain event is never called. The following does not work either:

var response = request.response(_);
request.write(_, "test").end();

because although it works in the "connection refused" case, if the connection is not refused, request.response(_) hangs (legitimately). I cannot seem to find a way to solve this.

The problem arises from the fact that WriteableStream's write(_) only installs a drain callback, and HttpClientRequest's error listener propagates the error to the response callback.

Ideally WriteableStream should listen for the error event and set a global variable which should be propagated to all callbacks, i.e. write(_), response(_), etc. should all throw.