2828// };
2929
3030const opcodes = require ( '../wireprotocol/shared' ) . opcodes ;
31+ const databaseNamespace = require ( '../wireprotocol/shared' ) . databaseNamespace ;
3132
3233// Incrementing request id
3334let _requestId = 0 ;
@@ -38,13 +39,15 @@ const OPTS_MORE_TO_COME = 2;
3839const OPTS_EXHAUST_ALLOWED = 1 >> 16 ;
3940
4041class Msg {
41- constructor ( bson , query , options ) {
42+ constructor ( bson , ns , command , options ) {
4243 // Basic options needed to be passed in
43- if ( query == null ) throw new Error ( 'query must be specified for query' ) ;
44+ if ( command == null ) throw new Error ( 'query must be specified for query' ) ;
4445
4546 // Basic options
4647 this . bson = bson ;
47- this . query = Array . isArray ( query ) ? query : [ query ] ;
48+ this . ns = ns ;
49+ this . command = command ;
50+ this . command . $db = databaseNamespace ( ns ) ;
4851
4952 // Ensure empty options
5053 this . options = options || { } ;
@@ -91,24 +94,21 @@ class Msg {
9194
9295 let totalLength = header . length ;
9396
94- for ( let i = 0 ; i < this . query . length ; ++ i ) {
95- const query = this . query [ i ] ;
96-
97- const nameArgumentPair = getValidSegmentListNamePairs ( query ) ;
98- if ( nameArgumentPair ) {
99- // TODO: Add support for payload type 1
100- const argument = nameArgumentPair . argument ;
101-
102- // Add initial type 0 segment with arguments pulled up
103- const clonedQuery = Object . assign ( { } , query ) ;
104- delete clonedQuery [ argument ] ;
105- totalLength += this . makeDocumentSegment ( buffers , clonedQuery ) ;
106-
107- // Create type 1 query
108- totalLength += this . makeSequenceSegment ( buffers , argument , query [ argument ] ) ;
109- } else {
110- totalLength += this . makeDocumentSegment ( buffers , query ) ;
111- }
97+ const command = this . command ;
98+ const nameArgumentPair = validSegmentListNamePairs ( command ) ;
99+ if ( nameArgumentPair ) {
100+ // TODO: Add support for payload type 1
101+ const argument = nameArgumentPair . argument ;
102+
103+ // Add initial type 0 segment with arguments pulled up
104+ const clonedQuery = Object . assign ( { } , command ) ;
105+ delete clonedQuery [ argument ] ;
106+ totalLength += this . makeDocumentSegment ( buffers , clonedQuery ) ;
107+
108+ // Create type 1 query
109+ totalLength += this . makeSequenceSegment ( buffers , argument , command [ argument ] ) ;
110+ } else {
111+ totalLength += this . makeDocumentSegment ( buffers , command ) ;
112112 }
113113
114114 writeInt32ListToUint8Buffer ( header , [ totalLength , this . requestId , 0 , opcodes . OP_MSG , flags ] ) ;
@@ -183,6 +183,7 @@ class BinMsg {
183183 this . requestId = msgHeader . requestId ;
184184 this . responseTo = msgHeader . responseTo ;
185185 this . opCode = msgHeader . opCode ;
186+ this . fromCompressed = msgHeader . fromCompressed ;
186187
187188 // Read response flags
188189 this . responseFlags = msgBody . readInt32LE ( 0 ) ;
@@ -266,7 +267,7 @@ const VALID_NAME_ARGUMENT_MAPS = {
266267 delete : 'deletes'
267268} ;
268269
269- function getValidSegmentListNamePairs ( query ) {
270+ function validSegmentListNamePairs ( query ) {
270271 for ( let name in VALID_NAME_ARGUMENT_MAPS ) {
271272 if ( name in query ) {
272273 const argument = VALID_NAME_ARGUMENT_MAPS [ name ] ;
0 commit comments