1
+ const { EventEmitter } = require ( 'events' ) ;
1
2
const fs = require ( 'fs' ) ;
2
3
const async = require ( 'async' ) ;
3
- const includes = require ( 'lodash.includes' ) ;
4
- const clone = require ( 'lodash.clone' ) ;
5
- const assign = require ( 'lodash.assign' ) ;
6
- const isString = require ( 'lodash.isstring' ) ;
7
- const isFunction = require ( 'lodash.isfunction' ) ;
8
- const omit = require ( 'lodash.omit' ) ;
9
- const MongoClient = require ( 'mongodb' ) . MongoClient ;
10
- const parseConnectionString = require ( 'mongodb-core' ) . parseConnectionString ;
4
+ const {
5
+ includes,
6
+ clone,
7
+ assign,
8
+ isString,
9
+ isFunction,
10
+ omit
11
+ } = require ( 'lodash' ) ;
12
+ const { MongoClient } = require ( 'mongodb' ) ;
13
+ const { parseConnectionString } = require ( 'mongodb/lib/core' ) ;
11
14
const Connection = require ( './extended-model' ) ;
12
15
const createSSHTunnel = require ( './ssh-tunnel' ) ;
13
- const EventEmitter = require ( 'events' ) . EventEmitter ;
16
+
14
17
const debug = require ( 'debug' ) ( 'mongodb-connection-model:connect' ) ;
15
18
16
- const needToLoadSSLFiles = ( model ) => ! includes (
17
- [ 'NONE' , 'UNVALIDATED' ] ,
18
- model . sslType
19
- ) ;
19
+ const needToLoadSSLFiles = model =>
20
+ ! includes ( [ 'NONE' , 'UNVALIDATED' ] , model . sslType ) ;
20
21
21
22
const loadOptions = ( model , done ) => {
22
23
if ( ! needToLoadSSLFiles ( model ) ) {
@@ -28,18 +29,19 @@ const loadOptions = (model, done) => {
28
29
const tasks = { } ;
29
30
const opts = clone ( model . driverOptions , true ) ;
30
31
31
- Object . keys ( opts ) . map ( ( key ) => {
32
+ Object . keys ( opts ) . map ( key => {
32
33
if ( key . indexOf ( 'ssl' ) === - 1 ) {
33
34
return ;
34
35
}
35
36
36
37
if ( Array . isArray ( opts [ key ] ) ) {
37
- opts [ key ] . forEach ( ( value ) => {
38
+ opts [ key ] . forEach ( value => {
38
39
if ( typeof value === 'string' ) {
39
- tasks [ key ] = ( cb ) => async . parallel (
40
- opts [ key ] . map ( ( k ) => fs . readFile . bind ( null , k ) ) ,
41
- cb
42
- ) ;
40
+ tasks [ key ] = cb =>
41
+ async . parallel (
42
+ opts [ key ] . map ( k => fs . readFile . bind ( null , k ) ) ,
43
+ cb
44
+ ) ;
43
45
}
44
46
} ) ;
45
47
}
@@ -60,7 +62,7 @@ const loadOptions = (model, done) => {
60
62
return done ( err ) ;
61
63
}
62
64
63
- Object . keys ( res ) . map ( ( key ) => {
65
+ Object . keys ( res ) . map ( key => {
64
66
opts [ key ] = res [ key ] ;
65
67
} ) ;
66
68
@@ -90,7 +92,7 @@ const validateURL = (model, done) => {
90
92
} ) ;
91
93
} ;
92
94
93
- const getStatusStateString = ( evt ) => {
95
+ const getStatusStateString = evt => {
94
96
if ( ! evt ) {
95
97
return 'UNKNOWN' ;
96
98
}
@@ -145,7 +147,7 @@ const getTasks = (model, setupListeners) => {
145
147
}
146
148
} ;
147
149
148
- ctx . skip = ( reason ) => {
150
+ ctx . skip = reason => {
149
151
state . emit ( 'status' , { message, skipped : true , reason } ) ;
150
152
151
153
if ( cb ) {
@@ -167,19 +169,21 @@ const getTasks = (model, setupListeners) => {
167
169
* TODO (imlucas) dns.lookup() model.hostname and model.sshTunnelHostname to check for typos
168
170
*/
169
171
assign ( tasks , {
170
- 'Load SSL files' : ( cb ) => {
172
+ 'Load SSL files' : cb => {
171
173
const ctx = status ( 'Load SSL files' , cb ) ;
172
174
173
175
if ( ! needToLoadSSLFiles ( model ) ) {
174
- return ctx . skip ( 'The selected SSL mode does not need to load any files.' ) ;
176
+ return ctx . skip (
177
+ 'The selected SSL mode does not need to load any files.'
178
+ ) ;
175
179
}
176
180
177
181
loadOptions ( model , ctx ) ;
178
182
}
179
183
} ) ;
180
184
181
185
assign ( tasks , {
182
- 'Create SSH Tunnel' : ( cb ) => {
186
+ 'Create SSH Tunnel' : cb => {
183
187
const ctx = status ( 'Create SSH Tunnel' , cb ) ;
184
188
185
189
if ( model . sshTunnel === 'NONE' ) {
@@ -191,7 +195,7 @@ const getTasks = (model, setupListeners) => {
191
195
} ) ;
192
196
193
197
assign ( tasks , {
194
- 'Connect to MongoDB' : ( cb ) => {
198
+ 'Connect to MongoDB' : cb => {
195
199
const ctx = status ( 'Connect to MongoDB' ) ;
196
200
197
201
// @note : Durran:
@@ -281,23 +285,25 @@ const connect = (model, setupListeners, done) => {
281
285
}
282
286
283
287
if ( ! isFunction ( done ) ) {
284
- done = ( err ) => {
288
+ done = err => {
285
289
if ( err ) {
286
290
throw err ;
287
291
}
288
292
} ;
289
293
}
290
294
291
295
const tasks = getTasks ( model , setupListeners ) ;
292
- const logTaskStatus = require ( 'debug' ) ( 'mongodb-connection-model:connect:status' ) ;
296
+ const logTaskStatus = require ( 'debug' ) (
297
+ 'mongodb-connection-model:connect:status'
298
+ ) ;
293
299
294
- tasks . state . on ( 'status' , ( evt ) => {
300
+ tasks . state . on ( 'status' , evt => {
295
301
logTaskStatus ( '%s [%s]' , evt . message , getStatusStateString ( evt ) ) ;
296
302
} ) ;
297
303
298
304
logTaskStatus ( 'Connecting...' ) ;
299
305
300
- async . series ( tasks , ( err ) => {
306
+ async . series ( tasks , err => {
301
307
if ( err ) {
302
308
logTaskStatus ( 'Error connecting:' , err ) ;
303
309
0 commit comments