This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
/
shared.js
142 lines (121 loc) · 3.36 KB
/
shared.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict';
const EventEmitter = require('events');
function executeCommand(configuration, db, cmd, options, cb) {
var Pool = require('../../../lib/connection/pool'),
f = require('util').format,
bson = require('bson'),
Query = require('../../../lib/connection/commands').Query;
// Optional options
if (typeof options === 'function') (cb = options), (options = {});
// Set the default options object if none passed in
options = options || {};
// Alternative options
var host = options.host || configuration.host;
var port = options.port || configuration.port;
// Attempt to connect
var pool = new Pool(null, {
host: host,
port: port,
bson: new bson()
});
// Add event listeners
pool.on('connect', function(_pool) {
var query = new Query(new bson(), f('%s.$cmd', db), cmd, {
numberToSkip: 0,
numberToReturn: 1
});
_pool.write(
query,
{
command: true
},
function(err, result) {
if (err) console.log(err.stack);
// Close the pool
_pool.destroy();
// If we have an error return
if (err) return cb(err);
// Return the result
cb(null, result.result);
}
);
});
pool.connect.apply(pool, options.auth);
}
function locateAuthMethod(configuration, cb) {
var Pool = require('../../../lib/connection/pool'),
bson = require('bson'),
f = require('util').format,
Query = require('../../../lib/connection/commands').Query;
// Set up operations
var db = 'admin';
var cmd = { ismaster: true };
// Attempt to connect
var pool = new Pool(null, {
host: configuration.host,
port: configuration.port,
bson: new bson()
});
// Add event listeners
pool.on('connect', function(_pool) {
var query = new Query(new bson(), f('%s.$cmd', db), cmd, {
numberToSkip: 0,
numberToReturn: 1
});
_pool.write(
query,
{
command: true
},
function(err, result) {
if (err) console.log(err.stack);
// Close the pool
_pool.destroy();
// If we have an error return
if (err) return cb(err);
// Establish the type of auth method
if (!result.result.maxWireVersion || result.result.maxWireVersion === 2) {
cb(null, 'mongocr');
} else {
cb(null, 'scram-sha-1');
}
}
);
});
pool.connect.apply(pool);
}
const delay = function(timeout) {
return new Promise(function(resolve) {
setTimeout(function() {
resolve();
}, timeout);
});
};
class ConnectionSpy extends EventEmitter {
constructor() {
super();
this.connections = {};
}
addConnection(id, connection) {
// console.log(`=== added connection ${id} :: ${connection.port}`);
this.connections[id] = connection;
this.emit('connectionAdded');
}
deleteConnection(id) {
// console.log(
// `=== deleted connection ${id} :: ${this.connections[id] ? this.connections[id].port : ''}`
// );
delete this.connections[id];
this.emit('connectionRemoved');
if (this.connectionCount() === 0) {
this.emit('drained');
}
}
connectionCount() {
return Object.keys(this.connections).length;
}
}
module.exports.executeCommand = executeCommand;
module.exports.locateAuthMethod = locateAuthMethod;
module.exports.delay = delay;
module.exports.ConnectionSpy = ConnectionSpy;