Stream and Pipe Client Server Request & Response Semantics

Banging my head around streams and pipes and getting results to behave for request and response semantics and need your help.

Here’s my gist

In client.js, the client creates an array of promises and sends the query to all connected peers and wants the first response back. Inside fetchResultViaPeer the client’s query is sent to the peer and the results are collected in a pipe.

In protocol.js, I have my peer protocol handler which uses a pipe and transform to read the query, do a fetch, and return the results back on the same stream.

The query arrives at the peer on the stream, peer gets it and runs the fetch() and also writes the response text back to the stream sink. But, on the client side it ‘hangs’ there. If I kill the peer connection at that point by doing a page refresh, then the client suddenly shows the response, but it is truncated several bytes at the end consistently.

I suspect there’s something amiss to indicate “done writing and receiving” along the line and I’ve tried an empty [] all over the place to indicate an ACK, but I’m still not groking the flow and need help.

Updated gist that works with a nasty hack → inject an EOF marker and padding to the stream so the important data gets through, and client looks for the marker to clean things up.

Ugly, but it works. Undoubtedly I still need help on how to do this right, and if you see what’s amiss please let me know!