/node_modules/mongoose/node_modules/mongodb-core/lib/wireprotocol/shared.js

https://bitbucket.org/coleman333/smartsite · JavaScript · 55 lines · 41 code · 9 blank · 5 comment · 4 complexity · 65c4cab1457af6608425e2eaf6f9aba2 MD5 · raw file

  1. 'use strict';
  2. var ReadPreference = require('../topologies/read_preference'),
  3. MongoError = require('../error').MongoError;
  4. var MESSAGE_HEADER_SIZE = 16;
  5. // OPCODE Numbers
  6. // Defined at https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#request-opcodes
  7. var opcodes = {
  8. OP_REPLY: 1,
  9. OP_UPDATE: 2001,
  10. OP_INSERT: 2002,
  11. OP_QUERY: 2004,
  12. OP_GETMORE: 2005,
  13. OP_DELETE: 2006,
  14. OP_KILL_CURSORS: 2007,
  15. OP_COMPRESSED: 2012
  16. };
  17. var getReadPreference = function(cmd, options) {
  18. // Default to command version of the readPreference
  19. var readPreference = cmd.readPreference || new ReadPreference('primary');
  20. // If we have an option readPreference override the command one
  21. if (options.readPreference) {
  22. readPreference = options.readPreference;
  23. }
  24. if (typeof readPreference === 'string') {
  25. readPreference = new ReadPreference(readPreference);
  26. }
  27. if (!(readPreference instanceof ReadPreference)) {
  28. throw new MongoError('readPreference must be a ReadPreference instance');
  29. }
  30. return readPreference;
  31. };
  32. // Parses the header of a wire protocol message
  33. var parseHeader = function(message) {
  34. return {
  35. length: message.readInt32LE(0),
  36. requestId: message.readInt32LE(4),
  37. responseTo: message.readInt32LE(8),
  38. opCode: message.readInt32LE(12)
  39. };
  40. };
  41. module.exports = {
  42. getReadPreference: getReadPreference,
  43. MESSAGE_HEADER_SIZE: MESSAGE_HEADER_SIZE,
  44. opcodes: opcodes,
  45. parseHeader: parseHeader
  46. };