mongodb 3.2.5 → 3.3.0-beta2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HISTORY.md +0 -10
- package/index.js +4 -4
- package/lib/admin.js +56 -56
- package/lib/aggregation_cursor.js +7 -3
- package/lib/bulk/common.js +18 -13
- package/lib/change_stream.js +196 -89
- package/lib/collection.js +217 -169
- package/lib/command_cursor.js +17 -7
- package/lib/core/auth/auth_provider.js +158 -0
- package/lib/core/auth/defaultAuthProviders.js +29 -0
- package/lib/core/auth/gssapi.js +241 -0
- package/lib/core/auth/mongo_credentials.js +81 -0
- package/lib/core/auth/mongocr.js +51 -0
- package/lib/core/auth/plain.js +35 -0
- package/lib/core/auth/scram.js +293 -0
- package/lib/core/auth/sspi.js +131 -0
- package/lib/core/auth/x509.js +26 -0
- package/lib/core/connection/apm.js +236 -0
- package/lib/core/connection/command_result.js +36 -0
- package/lib/core/connection/commands.js +507 -0
- package/lib/core/connection/connect.js +370 -0
- package/lib/core/connection/connection.js +624 -0
- package/lib/core/connection/logger.js +246 -0
- package/lib/core/connection/msg.js +219 -0
- package/lib/core/connection/pool.js +1285 -0
- package/lib/core/connection/utils.js +57 -0
- package/lib/core/cursor.js +752 -0
- package/lib/core/error.js +186 -0
- package/lib/core/index.js +50 -0
- package/lib/core/sdam/monitoring.js +228 -0
- package/lib/core/sdam/server.js +467 -0
- package/lib/core/sdam/server_description.js +163 -0
- package/lib/core/sdam/server_selectors.js +244 -0
- package/lib/core/sdam/srv_polling.js +135 -0
- package/lib/core/sdam/topology.js +1151 -0
- package/lib/core/sdam/topology_description.js +408 -0
- package/lib/core/sessions.js +711 -0
- package/lib/core/tools/smoke_plugin.js +61 -0
- package/lib/core/topologies/mongos.js +1337 -0
- package/lib/core/topologies/read_preference.js +202 -0
- package/lib/core/topologies/replset.js +1507 -0
- package/lib/core/topologies/replset_state.js +1121 -0
- package/lib/core/topologies/server.js +984 -0
- package/lib/core/topologies/shared.js +453 -0
- package/lib/core/transactions.js +167 -0
- package/lib/core/uri_parser.js +631 -0
- package/lib/core/utils.js +165 -0
- package/lib/core/wireprotocol/command.js +170 -0
- package/lib/core/wireprotocol/compression.js +73 -0
- package/lib/core/wireprotocol/constants.js +13 -0
- package/lib/core/wireprotocol/get_more.js +86 -0
- package/lib/core/wireprotocol/index.js +18 -0
- package/lib/core/wireprotocol/kill_cursors.js +70 -0
- package/lib/core/wireprotocol/query.js +224 -0
- package/lib/core/wireprotocol/shared.js +115 -0
- package/lib/core/wireprotocol/write_command.js +50 -0
- package/lib/cursor.js +40 -46
- package/lib/db.js +141 -95
- package/lib/dynamic_loaders.js +32 -0
- package/lib/error.js +12 -10
- package/lib/gridfs/chunk.js +2 -2
- package/lib/gridfs/grid_store.js +31 -25
- package/lib/gridfs-stream/index.js +4 -4
- package/lib/gridfs-stream/upload.js +1 -1
- package/lib/mongo_client.js +37 -15
- package/lib/operations/add_user.js +96 -0
- package/lib/operations/aggregate.js +24 -13
- package/lib/operations/aggregate_operation.js +127 -0
- package/lib/operations/bulk_write.js +104 -0
- package/lib/operations/close.js +47 -0
- package/lib/operations/collection_ops.js +28 -287
- package/lib/operations/collections.js +55 -0
- package/lib/operations/command.js +120 -0
- package/lib/operations/command_v2.js +43 -0
- package/lib/operations/common_functions.js +372 -0
- package/lib/operations/{mongo_client_ops.js → connect.js} +185 -157
- package/lib/operations/count.js +72 -0
- package/lib/operations/count_documents.js +46 -0
- package/lib/operations/create_collection.js +118 -0
- package/lib/operations/create_index.js +92 -0
- package/lib/operations/create_indexes.js +61 -0
- package/lib/operations/cursor_ops.js +3 -4
- package/lib/operations/db_ops.js +15 -12
- package/lib/operations/delete_many.js +25 -0
- package/lib/operations/delete_one.js +25 -0
- package/lib/operations/distinct.js +85 -0
- package/lib/operations/drop.js +53 -0
- package/lib/operations/drop_index.js +42 -0
- package/lib/operations/drop_indexes.js +23 -0
- package/lib/operations/estimated_document_count.js +33 -0
- package/lib/operations/execute_db_admin_command.js +34 -0
- package/lib/operations/execute_operation.js +165 -0
- package/lib/operations/explain.js +23 -0
- package/lib/operations/find_and_modify.js +98 -0
- package/lib/operations/find_one.js +33 -0
- package/lib/operations/find_one_and_delete.js +16 -0
- package/lib/operations/find_one_and_replace.js +18 -0
- package/lib/operations/find_one_and_update.js +19 -0
- package/lib/operations/geo_haystack_search.js +79 -0
- package/lib/operations/has_next.js +40 -0
- package/lib/operations/index_exists.js +39 -0
- package/lib/operations/index_information.js +23 -0
- package/lib/operations/indexes.js +22 -0
- package/lib/operations/insert_many.js +63 -0
- package/lib/operations/insert_one.js +75 -0
- package/lib/operations/is_capped.js +19 -0
- package/lib/operations/list_indexes.js +66 -0
- package/lib/operations/map_reduce.js +189 -0
- package/lib/operations/next.js +32 -0
- package/lib/operations/operation.js +63 -0
- package/lib/operations/options_operation.js +32 -0
- package/lib/operations/profiling_level.js +31 -0
- package/lib/operations/re_index.js +28 -0
- package/lib/operations/remove_user.js +52 -0
- package/lib/operations/rename.js +61 -0
- package/lib/operations/replace_one.js +47 -0
- package/lib/operations/set_profiling_level.js +48 -0
- package/lib/operations/stats.js +45 -0
- package/lib/operations/to_array.js +68 -0
- package/lib/operations/update_many.js +29 -0
- package/lib/operations/update_one.js +44 -0
- package/lib/operations/validate_collection.js +40 -0
- package/lib/read_concern.js +55 -0
- package/lib/topologies/mongos.js +3 -3
- package/lib/topologies/native_topology.js +22 -2
- package/lib/topologies/replset.js +3 -3
- package/lib/topologies/server.js +4 -4
- package/lib/topologies/topology_base.js +6 -6
- package/lib/url_parser.js +4 -3
- package/lib/utils.js +46 -59
- package/lib/write_concern.js +66 -0
- package/package.json +15 -6
- package/lib/.DS_Store +0 -0
|
@@ -0,0 +1,631 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const URL = require('url');
|
|
3
|
+
const qs = require('querystring');
|
|
4
|
+
const dns = require('dns');
|
|
5
|
+
const MongoParseError = require('./error').MongoParseError;
|
|
6
|
+
const ReadPreference = require('./topologies/read_preference');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The following regular expression validates a connection string and breaks the
|
|
10
|
+
* provide string into the following capture groups: [protocol, username, password, hosts]
|
|
11
|
+
*/
|
|
12
|
+
const HOSTS_RX = /(mongodb(?:\+srv|)):\/\/(?: (?:[^:]*) (?: : ([^@]*) )? @ )?([^/?]*)(?:\/|)(.*)/;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Determines whether a provided address matches the provided parent domain in order
|
|
16
|
+
* to avoid certain attack vectors.
|
|
17
|
+
*
|
|
18
|
+
* @param {String} srvAddress The address to check against a domain
|
|
19
|
+
* @param {String} parentDomain The domain to check the provided address against
|
|
20
|
+
* @return {Boolean} Whether the provided address matches the parent domain
|
|
21
|
+
*/
|
|
22
|
+
function matchesParentDomain(srvAddress, parentDomain) {
|
|
23
|
+
const regex = /^.*?\./;
|
|
24
|
+
const srv = `.${srvAddress.replace(regex, '')}`;
|
|
25
|
+
const parent = `.${parentDomain.replace(regex, '')}`;
|
|
26
|
+
return srv.endsWith(parent);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Lookup a `mongodb+srv` connection string, combine the parts and reparse it as a normal
|
|
31
|
+
* connection string.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} uri The connection string to parse
|
|
34
|
+
* @param {object} options Optional user provided connection string options
|
|
35
|
+
* @param {function} callback
|
|
36
|
+
*/
|
|
37
|
+
function parseSrvConnectionString(uri, options, callback) {
|
|
38
|
+
const result = URL.parse(uri, true);
|
|
39
|
+
|
|
40
|
+
if (result.hostname.split('.').length < 3) {
|
|
41
|
+
return callback(new MongoParseError('URI does not have hostname, domain name and tld'));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
result.domainLength = result.hostname.split('.').length;
|
|
45
|
+
if (result.pathname && result.pathname.match(',')) {
|
|
46
|
+
return callback(new MongoParseError('Invalid URI, cannot contain multiple hostnames'));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (result.port) {
|
|
50
|
+
return callback(new MongoParseError(`Ports not accepted with '${PROTOCOL_MONGODB_SRV}' URIs`));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Resolve the SRV record and use the result as the list of hosts to connect to.
|
|
54
|
+
const lookupAddress = result.host;
|
|
55
|
+
dns.resolveSrv(`_mongodb._tcp.${lookupAddress}`, (err, addresses) => {
|
|
56
|
+
if (err) return callback(err);
|
|
57
|
+
|
|
58
|
+
if (addresses.length === 0) {
|
|
59
|
+
return callback(new MongoParseError('No addresses found at host'));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
for (let i = 0; i < addresses.length; i++) {
|
|
63
|
+
if (!matchesParentDomain(addresses[i].name, result.hostname, result.domainLength)) {
|
|
64
|
+
return callback(
|
|
65
|
+
new MongoParseError('Server record does not share hostname with parent URI')
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Convert the original URL to a non-SRV URL.
|
|
71
|
+
result.protocol = 'mongodb';
|
|
72
|
+
result.host = addresses.map(address => `${address.name}:${address.port}`).join(',');
|
|
73
|
+
|
|
74
|
+
// Default to SSL true if it's not specified.
|
|
75
|
+
if (
|
|
76
|
+
!('ssl' in options) &&
|
|
77
|
+
(!result.search || !('ssl' in result.query) || result.query.ssl === null)
|
|
78
|
+
) {
|
|
79
|
+
result.query.ssl = true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Resolve TXT record and add options from there if they exist.
|
|
83
|
+
dns.resolveTxt(lookupAddress, (err, record) => {
|
|
84
|
+
if (err) {
|
|
85
|
+
if (err.code !== 'ENODATA') {
|
|
86
|
+
return callback(err);
|
|
87
|
+
}
|
|
88
|
+
record = null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (record) {
|
|
92
|
+
if (record.length > 1) {
|
|
93
|
+
return callback(new MongoParseError('Multiple text records not allowed'));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
record = qs.parse(record[0].join(''));
|
|
97
|
+
if (Object.keys(record).some(key => key !== 'authSource' && key !== 'replicaSet')) {
|
|
98
|
+
return callback(
|
|
99
|
+
new MongoParseError('Text record must only set `authSource` or `replicaSet`')
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
Object.assign(result.query, record);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Set completed options back into the URL object.
|
|
107
|
+
result.search = qs.stringify(result.query);
|
|
108
|
+
|
|
109
|
+
const finalString = URL.format(result);
|
|
110
|
+
parseConnectionString(finalString, options, (err, ret) => {
|
|
111
|
+
if (err) {
|
|
112
|
+
callback(err);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
callback(null, Object.assign({}, ret, { srvHost: lookupAddress }));
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Parses a query string item according to the connection string spec
|
|
124
|
+
*
|
|
125
|
+
* @param {string} key The key for the parsed value
|
|
126
|
+
* @param {Array|String} value The value to parse
|
|
127
|
+
* @return {Array|Object|String} The parsed value
|
|
128
|
+
*/
|
|
129
|
+
function parseQueryStringItemValue(key, value) {
|
|
130
|
+
if (Array.isArray(value)) {
|
|
131
|
+
// deduplicate and simplify arrays
|
|
132
|
+
value = value.filter((v, idx) => value.indexOf(v) === idx);
|
|
133
|
+
if (value.length === 1) value = value[0];
|
|
134
|
+
} else if (value.indexOf(':') > 0) {
|
|
135
|
+
value = value.split(',').reduce((result, pair) => {
|
|
136
|
+
const parts = pair.split(':');
|
|
137
|
+
result[parts[0]] = parseQueryStringItemValue(key, parts[1]);
|
|
138
|
+
return result;
|
|
139
|
+
}, {});
|
|
140
|
+
} else if (value.indexOf(',') > 0) {
|
|
141
|
+
value = value.split(',').map(v => {
|
|
142
|
+
return parseQueryStringItemValue(key, v);
|
|
143
|
+
});
|
|
144
|
+
} else if (value.toLowerCase() === 'true' || value.toLowerCase() === 'false') {
|
|
145
|
+
value = value.toLowerCase() === 'true';
|
|
146
|
+
} else if (!Number.isNaN(value) && !STRING_OPTIONS.has(key)) {
|
|
147
|
+
const numericValue = parseFloat(value);
|
|
148
|
+
if (!Number.isNaN(numericValue)) {
|
|
149
|
+
value = parseFloat(value);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return value;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Options that are known boolean types
|
|
157
|
+
const BOOLEAN_OPTIONS = new Set([
|
|
158
|
+
'slaveok',
|
|
159
|
+
'slave_ok',
|
|
160
|
+
'sslvalidate',
|
|
161
|
+
'fsync',
|
|
162
|
+
'safe',
|
|
163
|
+
'retrywrites',
|
|
164
|
+
'j'
|
|
165
|
+
]);
|
|
166
|
+
|
|
167
|
+
// Known string options, only used to bypass Number coercion in `parseQueryStringItemValue`
|
|
168
|
+
const STRING_OPTIONS = new Set(['authsource', 'replicaset']);
|
|
169
|
+
|
|
170
|
+
// Supported text representations of auth mechanisms
|
|
171
|
+
// NOTE: this list exists in native already, if it is merged here we should deduplicate
|
|
172
|
+
const AUTH_MECHANISMS = new Set([
|
|
173
|
+
'GSSAPI',
|
|
174
|
+
'MONGODB-X509',
|
|
175
|
+
'MONGODB-CR',
|
|
176
|
+
'DEFAULT',
|
|
177
|
+
'SCRAM-SHA-1',
|
|
178
|
+
'SCRAM-SHA-256',
|
|
179
|
+
'PLAIN'
|
|
180
|
+
]);
|
|
181
|
+
|
|
182
|
+
// Lookup table used to translate normalized (lower-cased) forms of connection string
|
|
183
|
+
// options to their expected camelCase version
|
|
184
|
+
const CASE_TRANSLATION = {
|
|
185
|
+
replicaset: 'replicaSet',
|
|
186
|
+
connecttimeoutms: 'connectTimeoutMS',
|
|
187
|
+
sockettimeoutms: 'socketTimeoutMS',
|
|
188
|
+
maxpoolsize: 'maxPoolSize',
|
|
189
|
+
minpoolsize: 'minPoolSize',
|
|
190
|
+
maxidletimems: 'maxIdleTimeMS',
|
|
191
|
+
waitqueuemultiple: 'waitQueueMultiple',
|
|
192
|
+
waitqueuetimeoutms: 'waitQueueTimeoutMS',
|
|
193
|
+
wtimeoutms: 'wtimeoutMS',
|
|
194
|
+
readconcern: 'readConcern',
|
|
195
|
+
readconcernlevel: 'readConcernLevel',
|
|
196
|
+
readpreference: 'readPreference',
|
|
197
|
+
maxstalenessseconds: 'maxStalenessSeconds',
|
|
198
|
+
readpreferencetags: 'readPreferenceTags',
|
|
199
|
+
authsource: 'authSource',
|
|
200
|
+
authmechanism: 'authMechanism',
|
|
201
|
+
authmechanismproperties: 'authMechanismProperties',
|
|
202
|
+
gssapiservicename: 'gssapiServiceName',
|
|
203
|
+
localthresholdms: 'localThresholdMS',
|
|
204
|
+
serverselectiontimeoutms: 'serverSelectionTimeoutMS',
|
|
205
|
+
serverselectiontryonce: 'serverSelectionTryOnce',
|
|
206
|
+
heartbeatfrequencyms: 'heartbeatFrequencyMS',
|
|
207
|
+
retrywrites: 'retryWrites',
|
|
208
|
+
uuidrepresentation: 'uuidRepresentation',
|
|
209
|
+
zlibcompressionlevel: 'zlibCompressionLevel',
|
|
210
|
+
tlsallowinvalidcertificates: 'tlsAllowInvalidCertificates',
|
|
211
|
+
tlsallowinvalidhostnames: 'tlsAllowInvalidHostnames',
|
|
212
|
+
tlsinsecure: 'tlsInsecure',
|
|
213
|
+
tlscafile: 'tlsCAFile',
|
|
214
|
+
tlscertificatekeyfile: 'tlsCertificateKeyFile',
|
|
215
|
+
tlscertificatekeyfilepassword: 'tlsCertificateKeyFilePassword',
|
|
216
|
+
wtimeout: 'wTimeoutMS',
|
|
217
|
+
j: 'journal'
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Sets the value for `key`, allowing for any required translation
|
|
222
|
+
*
|
|
223
|
+
* @param {object} obj The object to set the key on
|
|
224
|
+
* @param {string} key The key to set the value for
|
|
225
|
+
* @param {*} value The value to set
|
|
226
|
+
* @param {object} options The options used for option parsing
|
|
227
|
+
*/
|
|
228
|
+
function applyConnectionStringOption(obj, key, value, options) {
|
|
229
|
+
// simple key translation
|
|
230
|
+
if (key === 'journal') {
|
|
231
|
+
key = 'j';
|
|
232
|
+
} else if (key === 'wtimeoutms') {
|
|
233
|
+
key = 'wtimeout';
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// more complicated translation
|
|
237
|
+
if (BOOLEAN_OPTIONS.has(key)) {
|
|
238
|
+
value = value === 'true' || value === true;
|
|
239
|
+
} else if (key === 'appname') {
|
|
240
|
+
value = decodeURIComponent(value);
|
|
241
|
+
} else if (key === 'readconcernlevel') {
|
|
242
|
+
obj['readConcernLevel'] = value;
|
|
243
|
+
key = 'readconcern';
|
|
244
|
+
value = { level: value };
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// simple validation
|
|
248
|
+
if (key === 'compressors') {
|
|
249
|
+
value = Array.isArray(value) ? value : [value];
|
|
250
|
+
|
|
251
|
+
if (!value.every(c => c === 'snappy' || c === 'zlib')) {
|
|
252
|
+
throw new MongoParseError(
|
|
253
|
+
'Value for `compressors` must be at least one of: `snappy`, `zlib`'
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (key === 'authmechanism' && !AUTH_MECHANISMS.has(value)) {
|
|
259
|
+
throw new MongoParseError(
|
|
260
|
+
'Value for `authMechanism` must be one of: `DEFAULT`, `GSSAPI`, `PLAIN`, `MONGODB-X509`, `SCRAM-SHA-1`, `SCRAM-SHA-256`'
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (key === 'readpreference' && !ReadPreference.isValid(value)) {
|
|
265
|
+
throw new MongoParseError(
|
|
266
|
+
'Value for `readPreference` must be one of: `primary`, `primaryPreferred`, `secondary`, `secondaryPreferred`, `nearest`'
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (key === 'zlibcompressionlevel' && (value < -1 || value > 9)) {
|
|
271
|
+
throw new MongoParseError('zlibCompressionLevel must be an integer between -1 and 9');
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// special cases
|
|
275
|
+
if (key === 'compressors' || key === 'zlibcompressionlevel') {
|
|
276
|
+
obj.compression = obj.compression || {};
|
|
277
|
+
obj = obj.compression;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (key === 'authmechanismproperties') {
|
|
281
|
+
if (typeof value.SERVICE_NAME === 'string') obj.gssapiServiceName = value.SERVICE_NAME;
|
|
282
|
+
if (typeof value.SERVICE_REALM === 'string') obj.gssapiServiceRealm = value.SERVICE_REALM;
|
|
283
|
+
if (typeof value.CANONICALIZE_HOST_NAME !== 'undefined') {
|
|
284
|
+
obj.gssapiCanonicalizeHostName = value.CANONICALIZE_HOST_NAME;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (key === 'readpreferencetags' && Array.isArray(value)) {
|
|
289
|
+
value = splitArrayOfMultipleReadPreferenceTags(value);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// set the actual value
|
|
293
|
+
if (options.caseTranslate && CASE_TRANSLATION[key]) {
|
|
294
|
+
obj[CASE_TRANSLATION[key]] = value;
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
obj[key] = value;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const USERNAME_REQUIRED_MECHANISMS = new Set([
|
|
302
|
+
'GSSAPI',
|
|
303
|
+
'MONGODB-CR',
|
|
304
|
+
'PLAIN',
|
|
305
|
+
'SCRAM-SHA-1',
|
|
306
|
+
'SCRAM-SHA-256'
|
|
307
|
+
]);
|
|
308
|
+
|
|
309
|
+
function splitArrayOfMultipleReadPreferenceTags(value) {
|
|
310
|
+
const parsedTags = [];
|
|
311
|
+
|
|
312
|
+
for (let i = 0; i < value.length; i++) {
|
|
313
|
+
parsedTags[i] = {};
|
|
314
|
+
value[i].split(',').forEach(individualTag => {
|
|
315
|
+
const splitTag = individualTag.split(':');
|
|
316
|
+
parsedTags[i][splitTag[0]] = splitTag[1];
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return parsedTags;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Modifies the parsed connection string object taking into account expectations we
|
|
325
|
+
* have for authentication-related options.
|
|
326
|
+
*
|
|
327
|
+
* @param {object} parsed The parsed connection string result
|
|
328
|
+
* @return The parsed connection string result possibly modified for auth expectations
|
|
329
|
+
*/
|
|
330
|
+
function applyAuthExpectations(parsed) {
|
|
331
|
+
if (parsed.options == null) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const options = parsed.options;
|
|
336
|
+
const authSource = options.authsource || options.authSource;
|
|
337
|
+
if (authSource != null) {
|
|
338
|
+
parsed.auth = Object.assign({}, parsed.auth, { db: authSource });
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const authMechanism = options.authmechanism || options.authMechanism;
|
|
342
|
+
if (authMechanism != null) {
|
|
343
|
+
if (
|
|
344
|
+
USERNAME_REQUIRED_MECHANISMS.has(authMechanism) &&
|
|
345
|
+
(!parsed.auth || parsed.auth.username == null)
|
|
346
|
+
) {
|
|
347
|
+
throw new MongoParseError(`Username required for mechanism \`${authMechanism}\``);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (authMechanism === 'GSSAPI') {
|
|
351
|
+
if (authSource != null && authSource !== '$external') {
|
|
352
|
+
throw new MongoParseError(
|
|
353
|
+
`Invalid source \`${authSource}\` for mechanism \`${authMechanism}\` specified.`
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (authMechanism === 'MONGODB-X509') {
|
|
361
|
+
if (parsed.auth && parsed.auth.password != null) {
|
|
362
|
+
throw new MongoParseError(`Password not allowed for mechanism \`${authMechanism}\``);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (authSource != null && authSource !== '$external') {
|
|
366
|
+
throw new MongoParseError(
|
|
367
|
+
`Invalid source \`${authSource}\` for mechanism \`${authMechanism}\` specified.`
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (authMechanism === 'PLAIN') {
|
|
375
|
+
if (parsed.auth && parsed.auth.db == null) {
|
|
376
|
+
parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// default to `admin` if nothing else was resolved
|
|
382
|
+
if (parsed.auth && parsed.auth.db == null) {
|
|
383
|
+
parsed.auth = Object.assign({}, parsed.auth, { db: 'admin' });
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
return parsed;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Parses a query string according the connection string spec.
|
|
391
|
+
*
|
|
392
|
+
* @param {String} query The query string to parse
|
|
393
|
+
* @param {object} [options] The options used for options parsing
|
|
394
|
+
* @return {Object|Error} The parsed query string as an object, or an error if one was encountered
|
|
395
|
+
*/
|
|
396
|
+
function parseQueryString(query, options) {
|
|
397
|
+
const result = {};
|
|
398
|
+
let parsedQueryString = qs.parse(query);
|
|
399
|
+
|
|
400
|
+
checkTLSOptions(parsedQueryString);
|
|
401
|
+
|
|
402
|
+
for (const key in parsedQueryString) {
|
|
403
|
+
const value = parsedQueryString[key];
|
|
404
|
+
if (value === '' || value == null) {
|
|
405
|
+
throw new MongoParseError('Incomplete key value pair for option');
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const normalizedKey = key.toLowerCase();
|
|
409
|
+
const parsedValue = parseQueryStringItemValue(normalizedKey, value);
|
|
410
|
+
applyConnectionStringOption(result, normalizedKey, parsedValue, options);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// special cases for known deprecated options
|
|
414
|
+
if (result.wtimeout && result.wtimeoutms) {
|
|
415
|
+
delete result.wtimeout;
|
|
416
|
+
console.warn('Unsupported option `wtimeout` specified');
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return Object.keys(result).length ? result : null;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Checks a query string for invalid tls options according to the URI options spec.
|
|
424
|
+
*
|
|
425
|
+
* @param {string} queryString The query string to check
|
|
426
|
+
* @throws {MongoParseError}
|
|
427
|
+
*/
|
|
428
|
+
function checkTLSOptions(queryString) {
|
|
429
|
+
const queryStringKeys = Object.keys(queryString);
|
|
430
|
+
if (
|
|
431
|
+
queryStringKeys.indexOf('tlsInsecure') !== -1 &&
|
|
432
|
+
(queryStringKeys.indexOf('tlsAllowInvalidCertificates') !== -1 ||
|
|
433
|
+
queryStringKeys.indexOf('tlsAllowInvalidHostnames') !== -1)
|
|
434
|
+
) {
|
|
435
|
+
throw new MongoParseError(
|
|
436
|
+
'The `tlsInsecure` option cannot be used with `tlsAllowInvalidCertificates` or `tlsAllowInvalidHostnames`.'
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const tlsValue = assertTlsOptionsAreEqual('tls', queryString, queryStringKeys);
|
|
441
|
+
const sslValue = assertTlsOptionsAreEqual('ssl', queryString, queryStringKeys);
|
|
442
|
+
|
|
443
|
+
if (tlsValue != null && sslValue != null) {
|
|
444
|
+
if (tlsValue !== sslValue) {
|
|
445
|
+
throw new MongoParseError('All values of `tls` and `ssl` must be the same.');
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Checks a query string to ensure all tls/ssl options are the same.
|
|
452
|
+
*
|
|
453
|
+
* @param {string} key The key (tls or ssl) to check
|
|
454
|
+
* @param {string} queryString The query string to check
|
|
455
|
+
* @throws {MongoParseError}
|
|
456
|
+
* @return The value of the tls/ssl option
|
|
457
|
+
*/
|
|
458
|
+
function assertTlsOptionsAreEqual(optionName, queryString, queryStringKeys) {
|
|
459
|
+
const queryStringHasTLSOption = queryStringKeys.indexOf(optionName) !== -1;
|
|
460
|
+
|
|
461
|
+
let optionValue;
|
|
462
|
+
if (Array.isArray(queryString[optionName])) {
|
|
463
|
+
optionValue = queryString[optionName][0];
|
|
464
|
+
} else {
|
|
465
|
+
optionValue = queryString[optionName];
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (queryStringHasTLSOption) {
|
|
469
|
+
if (Array.isArray(queryString[optionName])) {
|
|
470
|
+
const firstValue = queryString[optionName][0];
|
|
471
|
+
queryString[optionName].forEach(tlsValue => {
|
|
472
|
+
if (tlsValue !== firstValue) {
|
|
473
|
+
throw new MongoParseError('All values of ${optionName} must be the same.');
|
|
474
|
+
}
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
return optionValue;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const PROTOCOL_MONGODB = 'mongodb';
|
|
483
|
+
const PROTOCOL_MONGODB_SRV = 'mongodb+srv';
|
|
484
|
+
const SUPPORTED_PROTOCOLS = [PROTOCOL_MONGODB, PROTOCOL_MONGODB_SRV];
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* Parses a MongoDB connection string
|
|
488
|
+
*
|
|
489
|
+
* @param {*} uri the MongoDB connection string to parse
|
|
490
|
+
* @param {object} [options] Optional settings.
|
|
491
|
+
* @param {boolean} [options.caseTranslate] Whether the parser should translate options back into camelCase after normalization
|
|
492
|
+
* @param {parseCallback} callback
|
|
493
|
+
*/
|
|
494
|
+
function parseConnectionString(uri, options, callback) {
|
|
495
|
+
if (typeof options === 'function') (callback = options), (options = {});
|
|
496
|
+
options = Object.assign({}, { caseTranslate: true }, options);
|
|
497
|
+
|
|
498
|
+
// Check for bad uris before we parse
|
|
499
|
+
try {
|
|
500
|
+
URL.parse(uri);
|
|
501
|
+
} catch (e) {
|
|
502
|
+
return callback(new MongoParseError('URI malformed, cannot be parsed'));
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const cap = uri.match(HOSTS_RX);
|
|
506
|
+
if (!cap) {
|
|
507
|
+
return callback(new MongoParseError('Invalid connection string'));
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
const protocol = cap[1];
|
|
511
|
+
if (SUPPORTED_PROTOCOLS.indexOf(protocol) === -1) {
|
|
512
|
+
return callback(new MongoParseError('Invalid protocol provided'));
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (protocol === PROTOCOL_MONGODB_SRV) {
|
|
516
|
+
return parseSrvConnectionString(uri, options, callback);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const dbAndQuery = cap[4].split('?');
|
|
520
|
+
const db = dbAndQuery.length > 0 ? dbAndQuery[0] : null;
|
|
521
|
+
const query = dbAndQuery.length > 1 ? dbAndQuery[1] : null;
|
|
522
|
+
|
|
523
|
+
let parsedOptions;
|
|
524
|
+
try {
|
|
525
|
+
parsedOptions = parseQueryString(query, options);
|
|
526
|
+
} catch (parseError) {
|
|
527
|
+
return callback(parseError);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
parsedOptions = Object.assign({}, parsedOptions, options);
|
|
531
|
+
const auth = { username: null, password: null, db: db && db !== '' ? qs.unescape(db) : null };
|
|
532
|
+
if (parsedOptions.auth) {
|
|
533
|
+
// maintain support for legacy options passed into `MongoClient`
|
|
534
|
+
if (parsedOptions.auth.username) auth.username = parsedOptions.auth.username;
|
|
535
|
+
if (parsedOptions.auth.user) auth.username = parsedOptions.auth.user;
|
|
536
|
+
if (parsedOptions.auth.password) auth.password = parsedOptions.auth.password;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
if (cap[4].split('?')[0].indexOf('@') !== -1) {
|
|
540
|
+
return callback(new MongoParseError('Unescaped slash in userinfo section'));
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const authorityParts = cap[3].split('@');
|
|
544
|
+
if (authorityParts.length > 2) {
|
|
545
|
+
return callback(new MongoParseError('Unescaped at-sign in authority section'));
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
if (authorityParts.length > 1) {
|
|
549
|
+
const authParts = authorityParts.shift().split(':');
|
|
550
|
+
if (authParts.length > 2) {
|
|
551
|
+
return callback(new MongoParseError('Unescaped colon in authority section'));
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
auth.username = qs.unescape(authParts[0]);
|
|
555
|
+
auth.password = authParts[1] ? qs.unescape(authParts[1]) : null;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
let hostParsingError = null;
|
|
559
|
+
const hosts = authorityParts
|
|
560
|
+
.shift()
|
|
561
|
+
.split(',')
|
|
562
|
+
.map(host => {
|
|
563
|
+
let parsedHost = URL.parse(`mongodb://${host}`);
|
|
564
|
+
if (parsedHost.path === '/:') {
|
|
565
|
+
hostParsingError = new MongoParseError('Double colon in host identifier');
|
|
566
|
+
return null;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// heuristically determine if we're working with a domain socket
|
|
570
|
+
if (host.match(/\.sock/)) {
|
|
571
|
+
parsedHost.hostname = qs.unescape(host);
|
|
572
|
+
parsedHost.port = null;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
if (Number.isNaN(parsedHost.port)) {
|
|
576
|
+
hostParsingError = new MongoParseError('Invalid port (non-numeric string)');
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
const result = {
|
|
581
|
+
host: parsedHost.hostname,
|
|
582
|
+
port: parsedHost.port ? parseInt(parsedHost.port) : 27017
|
|
583
|
+
};
|
|
584
|
+
|
|
585
|
+
if (result.port === 0) {
|
|
586
|
+
hostParsingError = new MongoParseError('Invalid port (zero) with hostname');
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
if (result.port > 65535) {
|
|
591
|
+
hostParsingError = new MongoParseError('Invalid port (larger than 65535) with hostname');
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
if (result.port < 0) {
|
|
596
|
+
hostParsingError = new MongoParseError('Invalid port (negative number)');
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
return result;
|
|
601
|
+
})
|
|
602
|
+
.filter(host => !!host);
|
|
603
|
+
|
|
604
|
+
if (hostParsingError) {
|
|
605
|
+
return callback(hostParsingError);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
if (hosts.length === 0 || hosts[0].host === '' || hosts[0].host === null) {
|
|
609
|
+
return callback(new MongoParseError('No hostname or hostnames provided in connection string'));
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
const result = {
|
|
613
|
+
hosts: hosts,
|
|
614
|
+
auth: auth.db || auth.username ? auth : null,
|
|
615
|
+
options: Object.keys(parsedOptions).length ? parsedOptions : null
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
if (result.auth && result.auth.db) {
|
|
619
|
+
result.defaultDatabase = result.auth.db;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
try {
|
|
623
|
+
applyAuthExpectations(result);
|
|
624
|
+
} catch (authError) {
|
|
625
|
+
return callback(authError);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
callback(null, result);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
module.exports = parseConnectionString;
|