framing-socket

FramingSocket adds variable size framing 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. It uses the
FramingBuffer internally to handle this framing.
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 back pressure mechanism based on the socket.bufferSize in two stages. warn_buffer_bytes
is the first watermark to start telling a user to back off via 'pause' and 'resume' events. max_buffer_bytes is
the second watermark where writes will be rejected completely.
Note: Still experimental.
Example
var FramingSocket = require('framing-socket');
var socket = new FramingSocket();
var rpc_id = 123;
function on_frame(err, data) {
socket.close();
}
function on_connect(err) {
if (err) {
return;
}
socket.write(rpc_id, new Buffer([0x01, 0x02, 0x03]), on_frame);
}
socket.connect('localhost', 8888, on_connect);
If you want control of how the fields are read and written:
var FramingSocket = require('framing-socket');
var options = {
timeout_ms: 5000,
warn_buffer_bytes: 524288,
max_buffer_bytes: 1048576,
frame_length_size: 4,
frame_length_writer: function(offset_buffer, frame_length) {
offset_buffer.writeInt32BE(frame_length);
},
frame_length_reader: function(offset_buffer) {
return offset_buffer.readInt32BE();
},
rpc_id_size: 4,
rpc_id_writer: function(offset_buffer, rpc_id) {
offset_buffer.writeInt32BE(rpc_id);
},
rpc_id_reader: function(offset_buffer) {
return offset_buffer.readInt32BE();
}
};
var socket = new FramingSocket(options);
Usage
connect(host, port, callback)
Creates a new socket calls the callback when the connection is complete.
framing_socket.connect(host, port, function on_connect(err) {
if (!err) {
}
});
close()
Closes an existing socket. This is a synchronous operation.
framing_socket.close();
write(rpc_id, data, callback)
Write data to the socket and calls the callback for the response frame.
The RPC ID is used to match the request and response together.
var rpc_id = 123;
var data = new Buffer('abcd', 'utf8');
framing_socket.write(rpc_id, data, function on_frame(err, frame) {
if (!err) {
}
});
fail_pending_rpcs()
Fails (rejects) all pending RPCs and calls their callbacks with an err.
var rpc_id = 123;
var data = new Buffer('abcd', 'utf8');
framing_socket.write(rpc_id, data, function on_frame(err, frame) {
if (err) {
console.log('Oops!');
} else {
}
});
framing_socket.fail_pending_rpcs();
event: disconnected(host, port, last_err)
When the connection has an unplanned disconnection. It includes the last detected error object.
It is not emitted when close() is called.
framing_socket.on('disconnected', function on_disconnected(host, port, last_err) {
});
event: timeout(host, port)
When the socket times out. The user can decide to what to do after this.
framing_socket.on('timeout', function on_timeout(host, port) {
});
event: pause(host, port)
Sent when it wants the user to pause upstream events including the server's host/port.
framing_socket.on('pause', function on_pause(host, port) {
});
event: resume(host, port)
Sent when the user can continue - including the server's host/port for reference.
framing_socket.on('resume', function on_resume(host, port) {
});
Install
npm install framing-socket
Tests
npm test
License
MIT License