commands.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. 'use strict';
  2. var retrieveBSON = require('./utils').retrieveBSON;
  3. var BSON = retrieveBSON();
  4. var Long = BSON.Long;
  5. const Buffer = require('safe-buffer').Buffer;
  6. // Incrementing request id
  7. var _requestId = 0;
  8. // Wire command operation ids
  9. var opcodes = require('../wireprotocol/shared').opcodes;
  10. // Query flags
  11. var OPTS_TAILABLE_CURSOR = 2;
  12. var OPTS_SLAVE = 4;
  13. var OPTS_OPLOG_REPLAY = 8;
  14. var OPTS_NO_CURSOR_TIMEOUT = 16;
  15. var OPTS_AWAIT_DATA = 32;
  16. var OPTS_EXHAUST = 64;
  17. var OPTS_PARTIAL = 128;
  18. // Response flags
  19. var CURSOR_NOT_FOUND = 1;
  20. var QUERY_FAILURE = 2;
  21. var SHARD_CONFIG_STALE = 4;
  22. var AWAIT_CAPABLE = 8;
  23. /**************************************************************
  24. * QUERY
  25. **************************************************************/
  26. var Query = function(bson, ns, query, options) {
  27. var self = this;
  28. // Basic options needed to be passed in
  29. if (ns == null) throw new Error('ns must be specified for query');
  30. if (query == null) throw new Error('query must be specified for query');
  31. // Validate that we are not passing 0x00 in the collection name
  32. if (ns.indexOf('\x00') !== -1) {
  33. throw new Error('namespace cannot contain a null character');
  34. }
  35. // Basic options
  36. this.bson = bson;
  37. this.ns = ns;
  38. this.query = query;
  39. // Additional options
  40. this.numberToSkip = options.numberToSkip || 0;
  41. this.numberToReturn = options.numberToReturn || 0;
  42. this.returnFieldSelector = options.returnFieldSelector || null;
  43. this.requestId = Query.getRequestId();
  44. // special case for pre-3.2 find commands, delete ASAP
  45. this.pre32Limit = options.pre32Limit;
  46. // Serialization option
  47. this.serializeFunctions =
  48. typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
  49. this.ignoreUndefined =
  50. typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : false;
  51. this.maxBsonSize = options.maxBsonSize || 1024 * 1024 * 16;
  52. this.checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : true;
  53. this.batchSize = self.numberToReturn;
  54. // Flags
  55. this.tailable = false;
  56. this.slaveOk = typeof options.slaveOk === 'boolean' ? options.slaveOk : false;
  57. this.oplogReplay = false;
  58. this.noCursorTimeout = false;
  59. this.awaitData = false;
  60. this.exhaust = false;
  61. this.partial = false;
  62. };
  63. //
  64. // Assign a new request Id
  65. Query.prototype.incRequestId = function() {
  66. this.requestId = _requestId++;
  67. };
  68. //
  69. // Assign a new request Id
  70. Query.nextRequestId = function() {
  71. return _requestId + 1;
  72. };
  73. //
  74. // Uses a single allocated buffer for the process, avoiding multiple memory allocations
  75. Query.prototype.toBin = function() {
  76. var self = this;
  77. var buffers = [];
  78. var projection = null;
  79. // Set up the flags
  80. var flags = 0;
  81. if (this.tailable) {
  82. flags |= OPTS_TAILABLE_CURSOR;
  83. }
  84. if (this.slaveOk) {
  85. flags |= OPTS_SLAVE;
  86. }
  87. if (this.oplogReplay) {
  88. flags |= OPTS_OPLOG_REPLAY;
  89. }
  90. if (this.noCursorTimeout) {
  91. flags |= OPTS_NO_CURSOR_TIMEOUT;
  92. }
  93. if (this.awaitData) {
  94. flags |= OPTS_AWAIT_DATA;
  95. }
  96. if (this.exhaust) {
  97. flags |= OPTS_EXHAUST;
  98. }
  99. if (this.partial) {
  100. flags |= OPTS_PARTIAL;
  101. }
  102. // If batchSize is different to self.numberToReturn
  103. if (self.batchSize !== self.numberToReturn) self.numberToReturn = self.batchSize;
  104. // Allocate write protocol header buffer
  105. var header = Buffer.alloc(
  106. 4 * 4 + // Header
  107. 4 + // Flags
  108. Buffer.byteLength(self.ns) +
  109. 1 + // namespace
  110. 4 + // numberToSkip
  111. 4 // numberToReturn
  112. );
  113. // Add header to buffers
  114. buffers.push(header);
  115. // Serialize the query
  116. var query = self.bson.serialize(this.query, {
  117. checkKeys: this.checkKeys,
  118. serializeFunctions: this.serializeFunctions,
  119. ignoreUndefined: this.ignoreUndefined
  120. });
  121. // Add query document
  122. buffers.push(query);
  123. if (self.returnFieldSelector && Object.keys(self.returnFieldSelector).length > 0) {
  124. // Serialize the projection document
  125. projection = self.bson.serialize(this.returnFieldSelector, {
  126. checkKeys: this.checkKeys,
  127. serializeFunctions: this.serializeFunctions,
  128. ignoreUndefined: this.ignoreUndefined
  129. });
  130. // Add projection document
  131. buffers.push(projection);
  132. }
  133. // Total message size
  134. var totalLength = header.length + query.length + (projection ? projection.length : 0);
  135. // Set up the index
  136. var index = 4;
  137. // Write total document length
  138. header[3] = (totalLength >> 24) & 0xff;
  139. header[2] = (totalLength >> 16) & 0xff;
  140. header[1] = (totalLength >> 8) & 0xff;
  141. header[0] = totalLength & 0xff;
  142. // Write header information requestId
  143. header[index + 3] = (this.requestId >> 24) & 0xff;
  144. header[index + 2] = (this.requestId >> 16) & 0xff;
  145. header[index + 1] = (this.requestId >> 8) & 0xff;
  146. header[index] = this.requestId & 0xff;
  147. index = index + 4;
  148. // Write header information responseTo
  149. header[index + 3] = (0 >> 24) & 0xff;
  150. header[index + 2] = (0 >> 16) & 0xff;
  151. header[index + 1] = (0 >> 8) & 0xff;
  152. header[index] = 0 & 0xff;
  153. index = index + 4;
  154. // Write header information OP_QUERY
  155. header[index + 3] = (opcodes.OP_QUERY >> 24) & 0xff;
  156. header[index + 2] = (opcodes.OP_QUERY >> 16) & 0xff;
  157. header[index + 1] = (opcodes.OP_QUERY >> 8) & 0xff;
  158. header[index] = opcodes.OP_QUERY & 0xff;
  159. index = index + 4;
  160. // Write header information flags
  161. header[index + 3] = (flags >> 24) & 0xff;
  162. header[index + 2] = (flags >> 16) & 0xff;
  163. header[index + 1] = (flags >> 8) & 0xff;
  164. header[index] = flags & 0xff;
  165. index = index + 4;
  166. // Write collection name
  167. index = index + header.write(this.ns, index, 'utf8') + 1;
  168. header[index - 1] = 0;
  169. // Write header information flags numberToSkip
  170. header[index + 3] = (this.numberToSkip >> 24) & 0xff;
  171. header[index + 2] = (this.numberToSkip >> 16) & 0xff;
  172. header[index + 1] = (this.numberToSkip >> 8) & 0xff;
  173. header[index] = this.numberToSkip & 0xff;
  174. index = index + 4;
  175. // Write header information flags numberToReturn
  176. header[index + 3] = (this.numberToReturn >> 24) & 0xff;
  177. header[index + 2] = (this.numberToReturn >> 16) & 0xff;
  178. header[index + 1] = (this.numberToReturn >> 8) & 0xff;
  179. header[index] = this.numberToReturn & 0xff;
  180. index = index + 4;
  181. // Return the buffers
  182. return buffers;
  183. };
  184. Query.getRequestId = function() {
  185. return ++_requestId;
  186. };
  187. /**************************************************************
  188. * GETMORE
  189. **************************************************************/
  190. var GetMore = function(bson, ns, cursorId, opts) {
  191. opts = opts || {};
  192. this.numberToReturn = opts.numberToReturn || 0;
  193. this.requestId = _requestId++;
  194. this.bson = bson;
  195. this.ns = ns;
  196. this.cursorId = cursorId;
  197. };
  198. //
  199. // Uses a single allocated buffer for the process, avoiding multiple memory allocations
  200. GetMore.prototype.toBin = function() {
  201. var length = 4 + Buffer.byteLength(this.ns) + 1 + 4 + 8 + 4 * 4;
  202. // Create command buffer
  203. var index = 0;
  204. // Allocate buffer
  205. var _buffer = Buffer.alloc(length);
  206. // Write header information
  207. // index = write32bit(index, _buffer, length);
  208. _buffer[index + 3] = (length >> 24) & 0xff;
  209. _buffer[index + 2] = (length >> 16) & 0xff;
  210. _buffer[index + 1] = (length >> 8) & 0xff;
  211. _buffer[index] = length & 0xff;
  212. index = index + 4;
  213. // index = write32bit(index, _buffer, requestId);
  214. _buffer[index + 3] = (this.requestId >> 24) & 0xff;
  215. _buffer[index + 2] = (this.requestId >> 16) & 0xff;
  216. _buffer[index + 1] = (this.requestId >> 8) & 0xff;
  217. _buffer[index] = this.requestId & 0xff;
  218. index = index + 4;
  219. // index = write32bit(index, _buffer, 0);
  220. _buffer[index + 3] = (0 >> 24) & 0xff;
  221. _buffer[index + 2] = (0 >> 16) & 0xff;
  222. _buffer[index + 1] = (0 >> 8) & 0xff;
  223. _buffer[index] = 0 & 0xff;
  224. index = index + 4;
  225. // index = write32bit(index, _buffer, OP_GETMORE);
  226. _buffer[index + 3] = (opcodes.OP_GETMORE >> 24) & 0xff;
  227. _buffer[index + 2] = (opcodes.OP_GETMORE >> 16) & 0xff;
  228. _buffer[index + 1] = (opcodes.OP_GETMORE >> 8) & 0xff;
  229. _buffer[index] = opcodes.OP_GETMORE & 0xff;
  230. index = index + 4;
  231. // index = write32bit(index, _buffer, 0);
  232. _buffer[index + 3] = (0 >> 24) & 0xff;
  233. _buffer[index + 2] = (0 >> 16) & 0xff;
  234. _buffer[index + 1] = (0 >> 8) & 0xff;
  235. _buffer[index] = 0 & 0xff;
  236. index = index + 4;
  237. // Write collection name
  238. index = index + _buffer.write(this.ns, index, 'utf8') + 1;
  239. _buffer[index - 1] = 0;
  240. // Write batch size
  241. // index = write32bit(index, _buffer, numberToReturn);
  242. _buffer[index + 3] = (this.numberToReturn >> 24) & 0xff;
  243. _buffer[index + 2] = (this.numberToReturn >> 16) & 0xff;
  244. _buffer[index + 1] = (this.numberToReturn >> 8) & 0xff;
  245. _buffer[index] = this.numberToReturn & 0xff;
  246. index = index + 4;
  247. // Write cursor id
  248. // index = write32bit(index, _buffer, cursorId.getLowBits());
  249. _buffer[index + 3] = (this.cursorId.getLowBits() >> 24) & 0xff;
  250. _buffer[index + 2] = (this.cursorId.getLowBits() >> 16) & 0xff;
  251. _buffer[index + 1] = (this.cursorId.getLowBits() >> 8) & 0xff;
  252. _buffer[index] = this.cursorId.getLowBits() & 0xff;
  253. index = index + 4;
  254. // index = write32bit(index, _buffer, cursorId.getHighBits());
  255. _buffer[index + 3] = (this.cursorId.getHighBits() >> 24) & 0xff;
  256. _buffer[index + 2] = (this.cursorId.getHighBits() >> 16) & 0xff;
  257. _buffer[index + 1] = (this.cursorId.getHighBits() >> 8) & 0xff;
  258. _buffer[index] = this.cursorId.getHighBits() & 0xff;
  259. index = index + 4;
  260. // Return buffer
  261. return _buffer;
  262. };
  263. /**************************************************************
  264. * KILLCURSOR
  265. **************************************************************/
  266. var KillCursor = function(bson, ns, cursorIds) {
  267. this.ns = ns;
  268. this.requestId = _requestId++;
  269. this.cursorIds = cursorIds;
  270. };
  271. //
  272. // Uses a single allocated buffer for the process, avoiding multiple memory allocations
  273. KillCursor.prototype.toBin = function() {
  274. var length = 4 + 4 + 4 * 4 + this.cursorIds.length * 8;
  275. // Create command buffer
  276. var index = 0;
  277. var _buffer = Buffer.alloc(length);
  278. // Write header information
  279. // index = write32bit(index, _buffer, length);
  280. _buffer[index + 3] = (length >> 24) & 0xff;
  281. _buffer[index + 2] = (length >> 16) & 0xff;
  282. _buffer[index + 1] = (length >> 8) & 0xff;
  283. _buffer[index] = length & 0xff;
  284. index = index + 4;
  285. // index = write32bit(index, _buffer, requestId);
  286. _buffer[index + 3] = (this.requestId >> 24) & 0xff;
  287. _buffer[index + 2] = (this.requestId >> 16) & 0xff;
  288. _buffer[index + 1] = (this.requestId >> 8) & 0xff;
  289. _buffer[index] = this.requestId & 0xff;
  290. index = index + 4;
  291. // index = write32bit(index, _buffer, 0);
  292. _buffer[index + 3] = (0 >> 24) & 0xff;
  293. _buffer[index + 2] = (0 >> 16) & 0xff;
  294. _buffer[index + 1] = (0 >> 8) & 0xff;
  295. _buffer[index] = 0 & 0xff;
  296. index = index + 4;
  297. // index = write32bit(index, _buffer, OP_KILL_CURSORS);
  298. _buffer[index + 3] = (opcodes.OP_KILL_CURSORS >> 24) & 0xff;
  299. _buffer[index + 2] = (opcodes.OP_KILL_CURSORS >> 16) & 0xff;
  300. _buffer[index + 1] = (opcodes.OP_KILL_CURSORS >> 8) & 0xff;
  301. _buffer[index] = opcodes.OP_KILL_CURSORS & 0xff;
  302. index = index + 4;
  303. // index = write32bit(index, _buffer, 0);
  304. _buffer[index + 3] = (0 >> 24) & 0xff;
  305. _buffer[index + 2] = (0 >> 16) & 0xff;
  306. _buffer[index + 1] = (0 >> 8) & 0xff;
  307. _buffer[index] = 0 & 0xff;
  308. index = index + 4;
  309. // Write batch size
  310. // index = write32bit(index, _buffer, this.cursorIds.length);
  311. _buffer[index + 3] = (this.cursorIds.length >> 24) & 0xff;
  312. _buffer[index + 2] = (this.cursorIds.length >> 16) & 0xff;
  313. _buffer[index + 1] = (this.cursorIds.length >> 8) & 0xff;
  314. _buffer[index] = this.cursorIds.length & 0xff;
  315. index = index + 4;
  316. // Write all the cursor ids into the array
  317. for (var i = 0; i < this.cursorIds.length; i++) {
  318. // Write cursor id
  319. // index = write32bit(index, _buffer, cursorIds[i].getLowBits());
  320. _buffer[index + 3] = (this.cursorIds[i].getLowBits() >> 24) & 0xff;
  321. _buffer[index + 2] = (this.cursorIds[i].getLowBits() >> 16) & 0xff;
  322. _buffer[index + 1] = (this.cursorIds[i].getLowBits() >> 8) & 0xff;
  323. _buffer[index] = this.cursorIds[i].getLowBits() & 0xff;
  324. index = index + 4;
  325. // index = write32bit(index, _buffer, cursorIds[i].getHighBits());
  326. _buffer[index + 3] = (this.cursorIds[i].getHighBits() >> 24) & 0xff;
  327. _buffer[index + 2] = (this.cursorIds[i].getHighBits() >> 16) & 0xff;
  328. _buffer[index + 1] = (this.cursorIds[i].getHighBits() >> 8) & 0xff;
  329. _buffer[index] = this.cursorIds[i].getHighBits() & 0xff;
  330. index = index + 4;
  331. }
  332. // Return buffer
  333. return _buffer;
  334. };
  335. var Response = function(bson, message, msgHeader, msgBody, opts) {
  336. opts = opts || { promoteLongs: true, promoteValues: true, promoteBuffers: false };
  337. this.parsed = false;
  338. this.raw = message;
  339. this.data = msgBody;
  340. this.bson = bson;
  341. this.opts = opts;
  342. // Read the message header
  343. this.length = msgHeader.length;
  344. this.requestId = msgHeader.requestId;
  345. this.responseTo = msgHeader.responseTo;
  346. this.opCode = msgHeader.opCode;
  347. this.fromCompressed = msgHeader.fromCompressed;
  348. // Read the message body
  349. this.responseFlags = msgBody.readInt32LE(0);
  350. this.cursorId = new Long(msgBody.readInt32LE(4), msgBody.readInt32LE(8));
  351. this.startingFrom = msgBody.readInt32LE(12);
  352. this.numberReturned = msgBody.readInt32LE(16);
  353. // Preallocate document array
  354. this.documents = new Array(this.numberReturned);
  355. // Flag values
  356. this.cursorNotFound = (this.responseFlags & CURSOR_NOT_FOUND) !== 0;
  357. this.queryFailure = (this.responseFlags & QUERY_FAILURE) !== 0;
  358. this.shardConfigStale = (this.responseFlags & SHARD_CONFIG_STALE) !== 0;
  359. this.awaitCapable = (this.responseFlags & AWAIT_CAPABLE) !== 0;
  360. this.promoteLongs = typeof opts.promoteLongs === 'boolean' ? opts.promoteLongs : true;
  361. this.promoteValues = typeof opts.promoteValues === 'boolean' ? opts.promoteValues : true;
  362. this.promoteBuffers = typeof opts.promoteBuffers === 'boolean' ? opts.promoteBuffers : false;
  363. };
  364. Response.prototype.isParsed = function() {
  365. return this.parsed;
  366. };
  367. Response.prototype.parse = function(options) {
  368. // Don't parse again if not needed
  369. if (this.parsed) return;
  370. options = options || {};
  371. // Allow the return of raw documents instead of parsing
  372. var raw = options.raw || false;
  373. var documentsReturnedIn = options.documentsReturnedIn || null;
  374. var promoteLongs =
  375. typeof options.promoteLongs === 'boolean' ? options.promoteLongs : this.opts.promoteLongs;
  376. var promoteValues =
  377. typeof options.promoteValues === 'boolean' ? options.promoteValues : this.opts.promoteValues;
  378. var promoteBuffers =
  379. typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : this.opts.promoteBuffers;
  380. var bsonSize, _options;
  381. // Set up the options
  382. _options = {
  383. promoteLongs: promoteLongs,
  384. promoteValues: promoteValues,
  385. promoteBuffers: promoteBuffers
  386. };
  387. // Position within OP_REPLY at which documents start
  388. // (See https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#wire-op-reply)
  389. this.index = 20;
  390. //
  391. // Parse Body
  392. //
  393. for (var i = 0; i < this.numberReturned; i++) {
  394. bsonSize =
  395. this.data[this.index] |
  396. (this.data[this.index + 1] << 8) |
  397. (this.data[this.index + 2] << 16) |
  398. (this.data[this.index + 3] << 24);
  399. // If we have raw results specified slice the return document
  400. if (raw) {
  401. this.documents[i] = this.data.slice(this.index, this.index + bsonSize);
  402. } else {
  403. this.documents[i] = this.bson.deserialize(
  404. this.data.slice(this.index, this.index + bsonSize),
  405. _options
  406. );
  407. }
  408. // Adjust the index
  409. this.index = this.index + bsonSize;
  410. }
  411. if (this.documents.length === 1 && documentsReturnedIn != null && raw) {
  412. const fieldsAsRaw = {};
  413. fieldsAsRaw[documentsReturnedIn] = true;
  414. _options.fieldsAsRaw = fieldsAsRaw;
  415. const doc = this.bson.deserialize(this.documents[0], _options);
  416. this.documents = [doc];
  417. }
  418. // Set parsed
  419. this.parsed = true;
  420. };
  421. module.exports = {
  422. Query: Query,
  423. GetMore: GetMore,
  424. Response: Response,
  425. KillCursor: KillCursor
  426. };