Wrapping a bidirectional stream in NodeJS

I would like to wrap a socket into another object which:

transforms output - e.g. turning strings into Base64 transforms input - e.g. turning Base64 into strings

(Note: my use case is not Base64 but is isomorphic to that and would significantly complicate the question.)

It is trivial to do this in the two direction separately - e.g. pipe socket into a Base64 decoder and write into a Base64 encoder which pipes into the socket.

I would like to generate a single new object from a socket, which could be written to and read from (via data events), yet perform the required transformations for both directions.

The solution needs to support Node 0.8.X and 0.10.X.

This seems to be one approach:

https://github.com/ajlopez/ObjectStream/blob/master/lib/objectstream.js