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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. function applyCommonQueryOptions(queryOptions, options) {
  42. Object.assign(queryOptions, {
  43. raw: typeof options.raw === 'boolean' ? options.raw : false,
  44. promoteLongs: typeof options.promoteLongs === 'boolean' ? options.promoteLongs : true,
  45. promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true,
  46. promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false,
  47. monitoring: typeof options.monitoring === 'boolean' ? options.monitoring : false,
  48. fullResult: typeof options.fullResult === 'boolean' ? options.fullResult : false
  49. });
  50. if (typeof options.socketTimeout === 'number') {
  51. queryOptions.socketTimeout = options.socketTimeout;
  52. }
  53. if (options.session) {
  54. queryOptions.session = options.session;
  55. }
  56. if (typeof options.documentsReturnedIn === 'string') {
  57. queryOptions.documentsReturnedIn = options.documentsReturnedIn;
  58. }
  59. return queryOptions;
  60. }
  61. function isMongos(server) {
  62. if (server.type === 'mongos') return true;
  63. if (server.parent && server.parent.type === 'mongos') return true;
  64. // NOTE: handle unified topology
  65. return false;
  66. }
  67. function databaseNamespace(ns) {
  68. return ns.split('.')[0];
  69. }
  70. function collectionNamespace(ns) {
  71. return ns
  72. .split('.')
  73. .slice(1)
  74. .join('.');
  75. }
  76. module.exports = {
  77. getReadPreference,
  78. MESSAGE_HEADER_SIZE,
  79. opcodes,
  80. parseHeader,
  81. applyCommonQueryOptions,
  82. isMongos,
  83. databaseNamespace,
  84. collectionNamespace
  85. };