Node.js socket wrapper which provides variable size framing and request/response association
FramingSocket adds variable size framing and simple promise-based request/response notifications to normal sockets.
Note that the FramingSocket assumes a request/response protocol of some form where the first frame bytes are the size of the frame, and each frame response contains an RPC ID to associate it with the sent request.
The user can define their own frame_length reader function and rpc_id reader function to find and parse those values from the frame.
It also includes a simplistic backpressure mechanism based on the number of "falsey" write()s and will emit the common pause/resume events based on this upstream.
Note: Still experimental. Use with caution
var FramingSocket = require'framing-socket';var options =frame_length_size: 4 // frame_length field size in bytes// custom function to read the frame_lengthreturn offset_bufferreadInt32BE; // note that it passes in an OffsetBuffer (see 'offset-buffer' repo)// custom function to read the rpc_id from the bufferreturn offset_bufferreadInt32BE; // The frame_length field is stripped from this OffsetBuffer already;var socket = options;var rpc_id = 123; // Unique RPC ID to identify this request (unique to this connection)socketconnect'localhost' 8888thensocketwriterpc_id 0x01 0x02 0x03then// do something with the returned frame (minus the frame_length bytes)socketclose;;;
npm install framing-socket