substack/seaport — pull request #41 by dominictarr (2013-08-26 20:24:14+00:00)
Title: handle incoming end without feeding messages back into itself

this was causing quite a crazy problem over here:

https://github.com/dominictarr/scuttlebutt/issues/27#issuecomment-23280191

The streams where literally getting crossed. Sometimes a very large packet would come in
and it would be cut in half with a packet from from it self.

since `tr.write = function (buf) { s.write(buf) };` and `s.pipe(tr)` it was actually feeding it's own output back into it's input.

So, if an incoming message is so large that it's split, and meanwhile an update occurs,
then that will update will write a message into it's input, concatenating it with the half a message that is already there.

This caused stream-serializer to throw, and crash.

I've also added an error listener to the scuttlebutt stream.
This will safely destroy the stream and log a message to stderr.
(this should pretty much never happen, of course)

Also, I've patched a race condition in scuttlebutt.
Now, the update listener is not registered until the history is queued,
So messages cannot be out of order.

the seaport tests pass, with the new scuttlebutt version.

Also:

![image](https://f.cloud.github.com/assets/259374/1028826/6b091c08-0e8d-11e3-970d-b407f4d320b1.png)