-
Notifications
You must be signed in to change notification settings - Fork 0
Packet.js
💥 Warning 💥 While this module does support packet creation, and it is will be tested, Mongo may have unknown or unspecified consequences for ill formatted packets. Please use caution if you intend to use this to create packets to send to an actual Mongo instance.
Packet.js is the module that this library uses to parse packets that come in from Mongo in client.js. Originally this library was designed primarily around the packet, and parsing/creating a packet (it wasn't until after that it brought in the pcap client that handles listening to ports locally) and this module is relatively smart. What this means is that every part of the packet is lazily instantiated until it is needed; that is, if you only need to access the parsed header after reading raw data, it will never access the parsed body. It utilizes Object.defineProperty to do this, and for more information please see the source code.
The primary purpose of packet.js is to parse Mongo packets on the line. It's very simple to parse the packet when you have the binary Buffer mongo data:
var packet = new viverrid.packet(rawMongoPacket);Now you have a packet that's ready to be read. You have access to three properties: header, body, and type.
The header always contains the following information:
var header = packet.header;
header.size // size of the raw packet
header.requestId // request id of the packet
header.resposneTo // if this is in response to a request, generally in a reply packet
header.opcode // the opcode of the packet, used by packet.typeAll of these properties are integers.
The body contains different information depending on the packet type.
var body = packet.body;if (packet.type === 'query') {
body.flags // not usually necessary, see API docs
body.collection // name of the collection, such as `application.person`
body.numberToSkip // AKA offset, number of documents to skip in database
body.numberToReturn // AKA limit, number of documents to return
body.query // actual query that mongo runs on specified collection
}if (packet.type === 'insert') {
body.flags // see API docs
body.collection // name of collection
body.documents // array of documents (objects) to insert
}if (packet.type === 'update') {
body.collection // name of collection
body.flags // see API docs
body.selector // query that's used to determine documents to update
body.update // what to update in selected documents
}if (packet.type === 'delete') {
body.collection // name of collection
body.flags // see API docs
body.selector // selector that determines documents to delete
}if (packet.type === 'reply') {
body.flags // see API docs
body.cursorId // cursor id of the reply
body.startingFrom // where in the cursor the reply starts
body.numberReturned // how many documents were returned
body.documents // array of documents returned
}if (packet.type === 'getMore') {
body.collection // name of collection
body.numberToReturn // number of documents to return
body.cursorId // same cursor id that was given in first reply
}Coming soon!