@pi-r/mongodb 0.3.0 → 0.5.0
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/client/index.js +64 -195
- package/package.json +5 -5
package/client/index.js
CHANGED
|
@@ -8,12 +8,6 @@ const util_1 = require("@e-mc/db/util");
|
|
|
8
8
|
const Db = require("@e-mc/db");
|
|
9
9
|
const Request = require("@e-mc/request");
|
|
10
10
|
const DbPool = require('@e-mc/db/pool');
|
|
11
|
-
const MONGODB_V3 = Db.checkSemVer("mongodb" /* STRINGS.MODULE_NAME */, 1, 4);
|
|
12
|
-
const MONGODB_V330 = Db.checkSemVer("mongodb" /* STRINGS.MODULE_NAME */, '3.3.0');
|
|
13
|
-
const MONGODB_V333 = Db.checkSemVer("mongodb" /* STRINGS.MODULE_NAME */, '3.3.0', '3.3.3');
|
|
14
|
-
const MONGODB_V360 = Db.checkSemVer("mongodb" /* STRINGS.MODULE_NAME */, '3.6.0');
|
|
15
|
-
const MONGODB_TOPOLOGY = MONGODB_V3 && MONGODB_V330;
|
|
16
|
-
const MONGODB_NOPOOL = MONGODB_V3 && !MONGODB_V330;
|
|
17
11
|
class MongoDBPool extends DbPool {
|
|
18
12
|
getConnection() {
|
|
19
13
|
return this.client.connect();
|
|
@@ -31,51 +25,36 @@ class MongoDBPool extends DbPool {
|
|
|
31
25
|
}
|
|
32
26
|
const topology = this.client.topology;
|
|
33
27
|
if (topology) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (name) {
|
|
37
|
-
return (0, util_1.checkEmpty)(topology[name]);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return (0, util_1.checkEmpty)(topology.s?.sessions);
|
|
28
|
+
const name = Object.getOwnPropertySymbols(topology).find(sym => sym.toString() === 'Symbol(waitQueue)');
|
|
29
|
+
return (0, util_1.checkEmpty)(name ? topology[name] : topology.s.servers);
|
|
41
30
|
}
|
|
42
31
|
return false;
|
|
43
32
|
}
|
|
44
33
|
get closeable() {
|
|
45
34
|
const topology = this.client.topology;
|
|
46
|
-
return topology ? super.closeable ||
|
|
35
|
+
return topology ? super.closeable || topology.s.state === 'closing' : true;
|
|
47
36
|
}
|
|
48
37
|
get closed() {
|
|
49
38
|
const topology = this.client.topology;
|
|
50
39
|
if (topology && (this.success > 0 || this.failed > 0)) {
|
|
51
|
-
|
|
52
|
-
return topology.s.servers.size === 0;
|
|
53
|
-
}
|
|
54
|
-
if (typeof topology.isDestroyed === 'function') {
|
|
55
|
-
return topology.isDestroyed();
|
|
56
|
-
}
|
|
40
|
+
return topology.isDestroyed();
|
|
57
41
|
}
|
|
58
42
|
return this.client.s?.hasBeenClosed === true;
|
|
59
43
|
}
|
|
60
44
|
}
|
|
61
45
|
const POOL_STATE = {};
|
|
62
|
-
const POOL_UNUSED = ['maxPoolSize', 'minPoolSize', 'maxConnecting', 'maxIdleTimeMS', 'waitQueueTimeoutMS', 'serverSelectionTimeoutMS', 'heartbeatFrequencyMS', 'minHeartbeatFrequencyMS', '
|
|
46
|
+
const POOL_UNUSED = ['maxPoolSize', 'minPoolSize', 'connectTimeoutMS', 'socketTimeoutMS', 'maxConnecting', 'maxIdleTimeMS', 'waitQueueTimeoutMS', 'serverSelectionTimeoutMS', 'heartbeatFrequencyMS', 'minHeartbeatFrequencyMS', 'compressors', 'zlibCompressionLevel', 'srvMaxHosts'];
|
|
63
47
|
function removePoolProperties(credential) {
|
|
64
48
|
const options = credential.options;
|
|
65
49
|
if (options && !credential.uuidKey) {
|
|
66
50
|
let result;
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
delete result[attr];
|
|
73
|
-
}
|
|
51
|
+
for (let i = 0, length = POOL_UNUSED.length; i < length; ++i) {
|
|
52
|
+
const attr = POOL_UNUSED[i];
|
|
53
|
+
if (attr in credential) {
|
|
54
|
+
result || (result = { ...options });
|
|
55
|
+
delete result[attr];
|
|
74
56
|
}
|
|
75
57
|
}
|
|
76
|
-
else if ('poolSize' in options || 'minSize' in options) {
|
|
77
|
-
result = { ...options, poolSize: undefined, minSize: undefined };
|
|
78
|
-
}
|
|
79
58
|
if (result) {
|
|
80
59
|
return { uri: credential.uri, options: result };
|
|
81
60
|
}
|
|
@@ -91,43 +70,13 @@ function getPoolKey(credential) {
|
|
|
91
70
|
if (options && credential.uri) {
|
|
92
71
|
const auth = options.auth;
|
|
93
72
|
let result = credential.uri + (auth ? '_' + (auth.username || '') + '@' + (auth.password || '') : '') + '_' + (options.authMechanism || '') + Db.asString(options.authMechanismProperties);
|
|
94
|
-
if (options.tls) {
|
|
95
|
-
const { tlsCertificateKeyFilePassword = '', tlsAllowInvalidHostnames, tlsAllowInvalidCertificates, tlsInsecure } = options;
|
|
96
|
-
|
|
97
|
-
if (MONGODB_V3) {
|
|
98
|
-
tlsCertificateFile = '';
|
|
99
|
-
tlsCertificateKeyFile = checkBuffer(tlsCertificateKeyFile);
|
|
100
|
-
tlsCAFile = checkBuffer(tlsCAFile);
|
|
101
|
-
}
|
|
102
|
-
result += tlsCertificateFile + tlsCertificateKeyFile + tlsCAFile + tlsCertificateKeyFilePassword + (tlsAllowInvalidHostnames ? '1' : '0') + (tlsAllowInvalidCertificates ? '1' : '0') + (tlsInsecure ? '1' : '0');
|
|
103
|
-
}
|
|
104
|
-
else if (options.ssl) {
|
|
105
|
-
let { sslCert = '', sslKey = '', sslCA = '' } = options;
|
|
106
|
-
if (MONGODB_V3) {
|
|
107
|
-
sslCert = checkBuffer(sslCert);
|
|
108
|
-
sslKey = checkBuffer(sslKey);
|
|
109
|
-
sslCA = checkBuffer(sslCA);
|
|
110
|
-
}
|
|
111
|
-
result += sslCert + sslKey + sslCA + (options.sslPass || '') + (options.sslValidate !== false ? '1' : '0');
|
|
73
|
+
if (options.tls || options.ssl) {
|
|
74
|
+
const { tlsCertificateKeyFile = '', tlsCAFile = '', tlsCRLFile = '', tlsCertificateKeyFilePassword = '', tlsAllowInvalidHostnames, tlsAllowInvalidCertificates, tlsInsecure } = options;
|
|
75
|
+
result += tlsCertificateKeyFile + tlsCAFile + tlsCRLFile + tlsCertificateKeyFilePassword + (tlsAllowInvalidHostnames ? '1' : '0') + (tlsAllowInvalidCertificates ? '1' : '0') + (tlsInsecure ? '1' : '0');
|
|
112
76
|
}
|
|
113
77
|
return result;
|
|
114
78
|
}
|
|
115
79
|
}
|
|
116
|
-
function checkBuffer(value) {
|
|
117
|
-
if (typeof value === 'string') {
|
|
118
|
-
return value;
|
|
119
|
-
}
|
|
120
|
-
if (Buffer.isBuffer(value)) {
|
|
121
|
-
return value.toString('utf-8');
|
|
122
|
-
}
|
|
123
|
-
if (Array.isArray(value)) {
|
|
124
|
-
const items = value.map(item => checkBuffer(item)).filter(item => item);
|
|
125
|
-
if (items.length) {
|
|
126
|
-
return items.join('');
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
return '';
|
|
130
|
-
}
|
|
131
80
|
function setCredential(item) {
|
|
132
81
|
const credential = this.getCredential(item);
|
|
133
82
|
const options = item.options || (item.options = {});
|
|
@@ -141,16 +90,10 @@ function setCredential(item) {
|
|
|
141
90
|
}
|
|
142
91
|
}
|
|
143
92
|
else if (auth?.username) {
|
|
144
|
-
|
|
145
|
-
options.auth = auth;
|
|
146
|
-
}
|
|
147
|
-
else {
|
|
148
|
-
username = auth.username;
|
|
149
|
-
password = auth.password;
|
|
150
|
-
}
|
|
93
|
+
options.auth = auth;
|
|
151
94
|
}
|
|
152
95
|
if (!uri || !Db.isURL(uri)) {
|
|
153
|
-
if (
|
|
96
|
+
if (!options.auth) {
|
|
154
97
|
authentication = (0, util_1.getBasicAuth)(username, password);
|
|
155
98
|
}
|
|
156
99
|
if (!server && hostname) {
|
|
@@ -161,16 +104,9 @@ function setCredential(item) {
|
|
|
161
104
|
}
|
|
162
105
|
uri = (protocol || 'mongodb:') + '//' + authentication + server;
|
|
163
106
|
}
|
|
164
|
-
else
|
|
107
|
+
else {
|
|
165
108
|
options.auth || (options.auth = { username, password });
|
|
166
109
|
}
|
|
167
|
-
else if (!(0, util_1.hasBasicAuth)(uri) && (authentication || (authentication = (0, util_1.getBasicAuth)(username, password)))) {
|
|
168
|
-
const url = uri.split('://');
|
|
169
|
-
uri = url[0] + '://' + authentication + url[1];
|
|
170
|
-
}
|
|
171
|
-
if (MONGODB_V3 && 'auth' in options) {
|
|
172
|
-
delete options.auth;
|
|
173
|
-
}
|
|
174
110
|
if (uri.indexOf('?') === -1) {
|
|
175
111
|
if (uri[uri.length - 1] !== '/') {
|
|
176
112
|
uri += '/';
|
|
@@ -181,110 +117,54 @@ function setCredential(item) {
|
|
|
181
117
|
switch (authMechanism) {
|
|
182
118
|
case 'MONGODB-X509': {
|
|
183
119
|
const checkFile = (value) => {
|
|
184
|
-
if ((0, types_1.isString)(value)) {
|
|
185
|
-
|
|
186
|
-
return MONGODB_V3 ? value : '';
|
|
187
|
-
}
|
|
188
|
-
if (Db.isPath(value = path.resolve(value)) && this.canRead(value, { ownPermissionOnly: true })) {
|
|
189
|
-
return MONGODB_V3 ? Db.readText(value, true) : value;
|
|
190
|
-
}
|
|
120
|
+
if ((0, types_1.isString)(value) && !Request.isCert(value = value.trim()) && Db.isPath(value = path.resolve(value)) && this.canRead(value, { ownPermissionOnly: true })) {
|
|
121
|
+
return value;
|
|
191
122
|
}
|
|
192
123
|
return '';
|
|
193
124
|
};
|
|
194
|
-
let {
|
|
195
|
-
if (
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
125
|
+
let { tlsCertificateKeyFile, tlsCAFile, tlsCRLFile } = credential;
|
|
126
|
+
if (tlsCertificateKeyFile && (tlsCertificateKeyFile = checkFile(tlsCertificateKeyFile))) {
|
|
127
|
+
const { tlsCertificateKeyFilePassword, tlsAllowInvalidHostnames, tlsAllowInvalidCertificates, tlsInsecure } = credential;
|
|
128
|
+
options.tlsCertificateKeyFile = tlsCertificateKeyFile;
|
|
129
|
+
if (tlsCAFile = checkFile(tlsCAFile)) {
|
|
130
|
+
options.tlsCAFile = tlsCAFile;
|
|
199
131
|
}
|
|
200
|
-
if (
|
|
201
|
-
options.
|
|
202
|
-
valid = true;
|
|
132
|
+
if (tlsCRLFile = checkFile(tlsCRLFile)) {
|
|
133
|
+
options.tlsCRLFile = tlsCRLFile;
|
|
203
134
|
}
|
|
204
|
-
if (
|
|
205
|
-
|
|
206
|
-
if (tlsCAFile = checkFile(tlsCAFile)) {
|
|
207
|
-
options.tlsCAFile = tlsCAFile;
|
|
208
|
-
}
|
|
209
|
-
if (tlsCertificateKeyFilePassword) {
|
|
210
|
-
options.tlsCertificateKeyFilePassword = tlsCertificateKeyFilePassword;
|
|
211
|
-
}
|
|
212
|
-
if (typeof tlsAllowInvalidHostnames === 'boolean') {
|
|
213
|
-
options.tlsAllowInvalidHostnames = tlsAllowInvalidHostnames;
|
|
214
|
-
}
|
|
215
|
-
if (typeof tlsAllowInvalidCertificates === 'boolean') {
|
|
216
|
-
options.tlsAllowInvalidCertificates = tlsAllowInvalidCertificates;
|
|
217
|
-
}
|
|
218
|
-
if (typeof tlsInsecure === 'boolean') {
|
|
219
|
-
options.tlsInsecure = tlsInsecure;
|
|
220
|
-
}
|
|
221
|
-
options.tls = true;
|
|
222
|
-
uri += '&tls=true';
|
|
135
|
+
if (tlsCertificateKeyFilePassword) {
|
|
136
|
+
options.tlsCertificateKeyFilePassword = tlsCertificateKeyFilePassword;
|
|
223
137
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
if (
|
|
228
|
-
options.
|
|
229
|
-
if (sslKey = checkFile(sslKey)) {
|
|
230
|
-
options.sslKey = sslKey;
|
|
231
|
-
}
|
|
232
|
-
if (MONGODB_V3) {
|
|
233
|
-
if (sslCA) {
|
|
234
|
-
const ca = (!Array.isArray(sslCA) ? [sslCA] : sslCA).map(value => this.readFile(value, { ownPermissionOnly: true, encoding: 'utf-8' })).filter(value => value);
|
|
235
|
-
if (ca.length) {
|
|
236
|
-
options.sslCA = ca;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
else if (sslCA = checkFile(sslCA)) {
|
|
241
|
-
options.sslCA = sslCA;
|
|
242
|
-
}
|
|
243
|
-
const { sslPass, sslValidate } = credential;
|
|
244
|
-
if (sslPass) {
|
|
245
|
-
options.sslPass = sslPass;
|
|
246
|
-
}
|
|
247
|
-
if (typeof sslValidate === 'boolean') {
|
|
248
|
-
options.sslValidate = sslValidate;
|
|
249
|
-
}
|
|
250
|
-
options.ssl = true;
|
|
251
|
-
uri += '&ssl=true';
|
|
252
|
-
valid = true;
|
|
138
|
+
if (typeof tlsAllowInvalidHostnames === 'boolean') {
|
|
139
|
+
options.tlsAllowInvalidHostnames = tlsAllowInvalidHostnames;
|
|
140
|
+
}
|
|
141
|
+
if (typeof tlsAllowInvalidCertificates === 'boolean') {
|
|
142
|
+
options.tlsAllowInvalidCertificates = tlsAllowInvalidCertificates;
|
|
253
143
|
}
|
|
144
|
+
if (typeof tlsInsecure === 'boolean') {
|
|
145
|
+
options.tlsInsecure = tlsInsecure;
|
|
146
|
+
}
|
|
147
|
+
options.tls = true;
|
|
148
|
+
uri += '&tls=true';
|
|
254
149
|
}
|
|
255
|
-
|
|
150
|
+
else {
|
|
256
151
|
throw (0, types_1.errorMessage)("mongodb" /* STRINGS.MODULE_NAME */, 'Missing TLS/SSL credentials');
|
|
257
152
|
}
|
|
258
153
|
authSource || (authSource = '$external');
|
|
259
154
|
break;
|
|
260
155
|
}
|
|
261
|
-
case 'GSSAPI':
|
|
262
|
-
|
|
263
|
-
let serviceName;
|
|
156
|
+
case 'GSSAPI':
|
|
157
|
+
case 'MONGODB-AWS': {
|
|
264
158
|
if ((0, types_1.isObject)(authMechanismProperties)) {
|
|
265
|
-
|
|
266
|
-
options.authMechanismProperties = authMechanismProperties;
|
|
267
|
-
break;
|
|
268
|
-
}
|
|
269
|
-
serviceName = authMechanismProperties.SERVICE_NAME;
|
|
270
|
-
}
|
|
271
|
-
else if ((0, types_1.isString)(authMechanismProperties)) {
|
|
272
|
-
serviceName = authMechanismProperties;
|
|
159
|
+
options.authMechanismProperties = authMechanismProperties;
|
|
273
160
|
}
|
|
274
|
-
|
|
275
|
-
uri += !MONGODB_V3 ? '&authMechanismProperties=' + encodeURIComponent('SERVICE_NAME:' + serviceName) : '&gssapiServiceName=' + encodeURIComponent(serviceName);
|
|
161
|
+
authSource || (authSource = '$external');
|
|
276
162
|
break;
|
|
277
163
|
}
|
|
278
164
|
case 'PLAIN':
|
|
279
165
|
uri += '&maxPoolSize=1';
|
|
280
166
|
authSource || (authSource = '$external');
|
|
281
167
|
break;
|
|
282
|
-
case 'MONGODB-AWS':
|
|
283
|
-
if ((0, types_1.isObject)(authMechanismProperties)) {
|
|
284
|
-
options.authMechanismProperties = authMechanismProperties;
|
|
285
|
-
}
|
|
286
|
-
authSource || (authSource = '$external');
|
|
287
|
-
break;
|
|
288
168
|
}
|
|
289
169
|
if (authSource) {
|
|
290
170
|
uri += '&authSource=' + encodeURIComponent(authSource);
|
|
@@ -294,17 +174,10 @@ function setCredential(item) {
|
|
|
294
174
|
else if (!(0, types_1.isString)(uri)) {
|
|
295
175
|
throw (0, types_1.errorMessage)("mongodb" /* STRINGS.MODULE_NAME */, 'Not defined - credentials');
|
|
296
176
|
}
|
|
297
|
-
if (MONGODB_TOPOLOGY) {
|
|
298
|
-
options.useUnifiedTopology = true;
|
|
299
|
-
}
|
|
300
177
|
const usePool = item.usePool;
|
|
301
178
|
if (usePool) {
|
|
302
|
-
if (MONGODB_NOPOOL) {
|
|
303
|
-
item.usePool = false;
|
|
304
|
-
return;
|
|
305
|
-
}
|
|
306
179
|
let username, password, pool;
|
|
307
|
-
if (typeof usePool === 'string' && (username =
|
|
180
|
+
if (typeof usePool === 'string' && (username = options.auth?.username || item.uri && (0, util_1.parseConnectionString)(item.uri)?.username)) {
|
|
308
181
|
[password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
|
|
309
182
|
if (pool) {
|
|
310
183
|
pool.add(item, password);
|
|
@@ -316,31 +189,27 @@ function setCredential(item) {
|
|
|
316
189
|
if (!(pool = POOL_STATE[poolKey]) || pool.closed) {
|
|
317
190
|
const config = this.getPoolConfig("mongodb" /* STRINGS.MODULE_NAME */, password);
|
|
318
191
|
if (config) {
|
|
319
|
-
const { min, max, idle, queue_max, queue_idle } = config;
|
|
320
|
-
if (
|
|
321
|
-
|
|
322
|
-
options.minPoolSize ?? (options.minPoolSize = min);
|
|
323
|
-
}
|
|
324
|
-
if (max > 0) {
|
|
325
|
-
options.maxPoolSize ?? (options.maxPoolSize = max);
|
|
326
|
-
}
|
|
327
|
-
if (idle >= 0) {
|
|
328
|
-
options.maxIdleTimeMS ?? (options.maxIdleTimeMS = idle);
|
|
329
|
-
}
|
|
330
|
-
if (queue_max > 0 && !MONGODB_V3) {
|
|
331
|
-
options.maxConnecting ?? (options.maxConnecting = queue_max);
|
|
332
|
-
}
|
|
333
|
-
if (queue_idle >= 0) {
|
|
334
|
-
options.waitQueueTimeoutMS ?? (options.waitQueueTimeoutMS = queue_idle);
|
|
335
|
-
}
|
|
192
|
+
const { min, max, idle, queue_max, queue_idle, timeout, socket_timeout } = config;
|
|
193
|
+
if (min >= 0) {
|
|
194
|
+
options.minPoolSize ?? (options.minPoolSize = min);
|
|
336
195
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
196
|
+
if (max > 0) {
|
|
197
|
+
options.maxPoolSize ?? (options.maxPoolSize = max);
|
|
198
|
+
}
|
|
199
|
+
if (idle >= 0) {
|
|
200
|
+
options.maxIdleTimeMS ?? (options.maxIdleTimeMS = idle);
|
|
201
|
+
}
|
|
202
|
+
if (queue_max > 0) {
|
|
203
|
+
options.maxConnecting ?? (options.maxConnecting = queue_max);
|
|
204
|
+
}
|
|
205
|
+
if (queue_idle >= 0) {
|
|
206
|
+
options.waitQueueTimeoutMS ?? (options.waitQueueTimeoutMS = queue_idle);
|
|
207
|
+
}
|
|
208
|
+
if (timeout > 0) {
|
|
209
|
+
options.connectTimeoutMS ?? (options.connectTimeoutMS = timeout);
|
|
210
|
+
}
|
|
211
|
+
if (socket_timeout > 0) {
|
|
212
|
+
options.socketTimeoutMS ?? (options.socketTimeoutMS = socket_timeout);
|
|
344
213
|
}
|
|
345
214
|
}
|
|
346
215
|
new MongoDBPool(new mongodb.MongoClient(item.uri, options), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/mongodb",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "MongoDB client driver for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
6
|
"types": "client/index.d.ts",
|
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@e-mc/db": "^0.
|
|
25
|
-
"@e-mc/request": "^0.
|
|
26
|
-
"@e-mc/types": "^0.
|
|
27
|
-
"mongodb": "^
|
|
24
|
+
"@e-mc/db": "^0.7.0",
|
|
25
|
+
"@e-mc/request": "^0.7.0",
|
|
26
|
+
"@e-mc/types": "^0.7.0",
|
|
27
|
+
"mongodb": "^6.3.0"
|
|
28
28
|
}
|
|
29
29
|
}
|