-
Notifications
You must be signed in to change notification settings - Fork 26
Home
Currently working through Concerns and Decisions on the implementation.
None yet. Would like to create a MySQL synopsis to drive home the value.
Thank you to the following people who unwittingly contributed to this project through the generous, open-source licensing of their works.
- Joshua Bell ~ Provided implementation of IEEE 754 floating point serialization through his blog post Javascript and IEEE754 Redux under the MIT license attached to TypedArray in the LLSD type system.
Design artifacts.
- Peter Renshaw ~ The punch card in the node-packet landing page.
Prior Art.
- node-mysql by Felix Geisendörfer ~ Example of binary packet parsing of MySQL packets that inspired the attempt.
- Perl's pack ~ For the idea of a string representation of a packet.
A structure is an object that both reads from and writes to a buffer synchronously. When reading, buffer must contain the entire contents of the structure. When writing, the buffer must have enough space to accomdoate the structure.
Below is an example of a binary structure that associates an UUID witha file position.
// Oh, let's say... an object uuid followed by a file position.
var struct = require("packet").struct("l128l64");
// Create an I/O object around the structure.
var io = {
size: struct.sizeOf(),
read: function (buffer, index, callback) {
struct.read(buffer, index * io.size, function (uuid, position) {
callback(uuid, position);
});
},
write: function (buffer, index, uuid, position) {
struct.write(buffer, index * io.size, uuid, position);
}
};
// Now we can treat a buffer as an array of structures.
var buffer = [];
io.write(buffer, 3, "38ce9a6cdc5ff8e9b83ecffa58cd937e", 783189192);
io.read(buffer, 3, function (uuid, position) {
console.log("Object " + uuid + " is at file position " + position + ".");
});Creates a structure from a pattern.
The read method accepts a buffer with an optional offset. The number of arguments is determined by the structure packet pattern, and must match the number of arguments expected by the packet pattern.
The callback will be called with the fields read from the buffer, with the actual count of bytes read as the last parameter.
Write the arguments to the buffer at the optional offset. The arguments are determined by the structure bit pattern. Returns the number of bytes written.
Get the size of the structure for the given variable length values. A structure can have 0 or more variable length values.
The sizeOf method does not expect and will not correctly calculate the size of
the structure if fixed size value are given.
Parsers read and write synchrnously.
It is intended that separate parsers are used for
The language is inspired by Perl's pack and unpack functions, but generalized for packets and bytes. A field is defined by endianess followed by a count of bits. The count of bits must be divisible by bytes. Bits are used because the count of bits is easier to read out of the compressed pattern format than a count of bytes. The familiar values 8, 16, 24, 32, 64 stand out.
b16 - Big-endian 32 bit number.
l16 - Little-endian 32 bit number.
b8 - Endianess of a single byte is irrelevant.