-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
config.js
192 lines (159 loc) · 5.06 KB
/
config.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict';
const url = require('url');
const qs = require('querystring');
const util = require('util');
const MongoClient = require('../../lib/mongo_client');
const TopologyType = require('../../lib/core/sdam/topology_description').TopologyType;
const core = require('../../lib/core');
class NativeConfiguration {
constructor(parsedURI, context) {
this.topologyType = context.topologyType;
this.options = Object.assign(
{
hosts: parsedURI.hosts,
host: parsedURI.hosts[0] ? parsedURI.hosts[0].host : 'localhost',
port: parsedURI.hosts[0] ? parsedURI.hosts[0].port : 27017,
db: parsedURI.auth && parsedURI.auth.db ? parsedURI.auth.db : 'integration_tests'
},
parsedURI.options
);
this.mongo = this.require = require('../..');
this.writeConcern = function() {
return { w: 1 };
};
}
get host() {
return this.options.host;
}
get port() {
return this.options.port;
}
set db(_db) {
this.options.db = _db;
}
get db() {
return this.options.db;
}
// legacy accessors, consider for removal
get replicasetName() {
return this.options.replicaSet;
}
get setName() {
return this.options.replicaSet;
}
usingUnifiedTopology() {
return !!process.env.MONGODB_UNIFIED_TOPOLOGY;
}
newClient(dbOptions, serverOptions) {
// support MongoClient contructor form (url, options) for `newClient`
if (typeof dbOptions === 'string') {
return new MongoClient(
dbOptions,
this.usingUnifiedTopology()
? Object.assign({ useUnifiedTopology: true }, serverOptions)
: serverOptions
);
}
dbOptions = dbOptions || {};
serverOptions = Object.assign({}, { haInterval: 100 }, serverOptions);
if (this.usingUnifiedTopology()) {
serverOptions.useUnifiedTopology = true;
}
// Fall back
let dbHost = (serverOptions && serverOptions.host) || this.options.host;
const dbPort = (serverOptions && serverOptions.port) || this.options.port;
if (dbHost.indexOf('.sock') !== -1) {
dbHost = qs.escape(dbHost);
}
if (this.options.replicaSet) {
Object.assign(dbOptions, { replicaSet: this.options.replicaSet, auto_reconnect: false });
}
const connectionString = url.format({
protocol: 'mongodb',
slashes: true,
hostname: dbHost,
port: dbPort,
query: dbOptions,
pathname: '/'
});
return new MongoClient(connectionString, serverOptions);
}
newTopology(host, port, options) {
options = Object.assign({}, options);
host = host || this.options.host;
port = port || this.options.port;
const hosts = host == null ? [].concat(this.options.hosts) : [{ host, port }];
if (this.usingUnifiedTopology()) {
return new core.Topology(hosts, options);
}
if (this.topologyType === TopologyType.ReplicaSetWithPrimary) {
options.poolSize = 1;
options.autoReconnect = false;
return new core.ReplSet(hosts, options);
}
if (this.topologyType === TopologyType.Sharded) {
return new core.Mongos(hosts, options);
}
return new core.Server(Object.assign({ host, port }, options));
}
url(username, password) {
const query = {};
if (this.options.replicaSet) {
Object.assign(query, { replicaSet: this.options.replicaSet, auto_reconnect: false });
}
let multipleHosts;
if (this.options.hosts.length > 1) {
multipleHosts = this.options.hosts
.reduce((built, host) => {
built.push(`${host.host}:${host.port}`);
return built;
}, [])
.join(',');
}
const urlObject = {
protocol: 'mongodb',
slashes: true,
pathname: `/${this.options.db}`,
query
};
if (multipleHosts) {
Object.assign(urlObject, { hostname: '%s' });
} else {
Object.assign(urlObject, {
hostname: this.options.host,
port: this.options.port
});
}
if (username || password) {
urlObject.auth = password == null ? username : `${username}:${password}`;
}
if (multipleHosts) {
return util.format(url.format(urlObject), multipleHosts);
}
return url.format(urlObject);
}
writeConcernMax() {
if (this.topologyType !== TopologyType.Single) {
return { w: 'majority', wtimeout: 30000 };
}
return { w: 1 };
}
// server37631Workaround() {
// console.log('[applying SERVER-37631 workaround]');
// const configServers = this.manager.configurationServers.managers;
// const proxies = this.manager.proxies;
// const configServersPromise = configServers.reduce((result, server) => {
// return result.then(() =>
// server.executeCommand('admin.$cmd', { refreshLogicalSessionCacheNow: 1 })
// );
// }, Promise.resolve());
// return configServersPromise.then(() => {
// return proxies.reduce((promise, proxy) => {
// return promise.then(() =>
// proxy.executeCommand('admin.$cmd', { refreshLogicalSessionCacheNow: 1 })
// );
// }, Promise.resolve());
// });
// }
}
module.exports = NativeConfiguration;