Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shared.js 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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('read preference 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. };