PageRenderTime 15ms CodeModel.GetById 11ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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