@@ -2,33 +2,31 @@ import { v4 as uuidv4 } from 'uuid';
2
2
import * as vscode from 'vscode' ;
3
3
import Connection = require( 'mongodb-connection-model/lib/model' ) ;
4
4
import DataService = require( 'mongodb-data-service' ) ;
5
- import * as keytarType from 'keytar' ;
5
+
6
6
import { ConnectionModelType } from './connectionModelType' ;
7
7
import { DataServiceType } from './dataServiceType' ;
8
8
import { createLogger } from './logging' ;
9
9
import { StatusView } from './views' ;
10
10
import { EventEmitter } from 'events' ;
11
11
import { StorageController , StorageVariables } from './storage' ;
12
12
import { SavedConnection , StorageScope } from './storage/storageController' ;
13
- import { getNodeModule } from './utils/getNodeModule' ;
14
13
import TelemetryController from './telemetry/telemetryController' ;
14
+ import { ext } from './extensionConstants' ;
15
15
16
16
const { name, version } = require ( '../package.json' ) ;
17
17
const log = createLogger ( 'connection controller' ) ;
18
18
const MAX_CONNECTION_NAME_LENGTH = 512 ;
19
19
20
- type KeyTar = typeof keytarType ;
21
-
22
20
export enum DataServiceEventTypes {
23
21
CONNECTIONS_DID_CHANGE = 'CONNECTIONS_DID_CHANGE' ,
24
22
ACTIVE_CONNECTION_CHANGED = 'ACTIVE_CONNECTION_CHANGED' ,
25
- ACTIVE_CONNECTION_CHANGING = 'ACTIVE_CONNECTION_CHANGING'
23
+ ACTIVE_CONNECTION_CHANGING = 'ACTIVE_CONNECTION_CHANGING' ,
26
24
}
27
25
28
26
export enum ConnectionTypes {
29
27
CONNECTION_FORM = 'CONNECTION_FORM' ,
30
28
CONNECTION_STRING = 'CONNECTION_STRING' ,
31
- CONNECTION_ID = 'CONNECTION_ID'
29
+ CONNECTION_ID = 'CONNECTION_ID' ,
32
30
}
33
31
34
32
export type SavedConnectionInformation = {
@@ -48,8 +46,6 @@ export default class ConnectionController {
48
46
} = { } ;
49
47
50
48
private readonly _serviceName = 'mdb.vscode.savedConnections' ;
51
- private _keytar : KeyTar | undefined ;
52
-
53
49
_activeDataService : null | DataServiceType = null ;
54
50
_activeConnectionModel : null | ConnectionModelType = null ;
55
51
private _currentConnectionId : null | string = null ;
@@ -73,33 +69,20 @@ export default class ConnectionController {
73
69
this . _statusView = _statusView ;
74
70
this . _storageController = storageController ;
75
71
this . _telemetryController = telemetryController ;
76
-
77
- try {
78
- // We load keytar in two different ways. This is because when the
79
- // extension is webpacked it requires the vscode external keytar dependency
80
- // differently then our testing development environment.
81
- this . _keytar = require ( 'keytar' ) ;
82
-
83
- if ( ! this . _keytar ) {
84
- this . _keytar = getNodeModule < typeof keytarType > ( 'keytar' ) ;
85
- }
86
- } catch ( err ) {
87
- // Couldn't load keytar, proceed without storing & loading connections.
88
- }
89
72
}
90
73
91
74
_loadSavedConnection = async (
92
75
connectionId : string ,
93
76
savedConnection : SavedConnection
94
77
) : Promise < void > => {
95
- if ( ! this . _keytar ) {
78
+ if ( ! ext . keytarModule ) {
96
79
return ;
97
80
}
98
81
99
82
let loadedSavedConnection : LoadedConnection ;
100
83
101
84
try {
102
- const unparsedConnectionInformation = await this . _keytar . getPassword (
85
+ const unparsedConnectionInformation = await ext . keytarModule . getPassword (
103
86
this . _serviceName ,
104
87
connectionId
105
88
) ;
@@ -138,7 +121,7 @@ export default class ConnectionController {
138
121
} ;
139
122
140
123
loadSavedConnections = async ( ) : Promise < void > => {
141
- if ( ! this . _keytar ) {
124
+ if ( ! ext . keytarModule ) {
142
125
return ;
143
126
}
144
127
@@ -297,10 +280,10 @@ export default class ConnectionController {
297
280
298
281
this . _connections [ connectionId ] = newLoadedConnection ;
299
282
300
- if ( this . _keytar ) {
283
+ if ( ext . keytarModule ) {
301
284
const connectionInfoAsString = JSON . stringify ( connectionInformation ) ;
302
285
303
- await this . _keytar . setPassword (
286
+ await ext . keytarModule . setPassword (
304
287
this . _serviceName ,
305
288
connectionId ,
306
289
connectionInfoAsString
@@ -496,8 +479,8 @@ export default class ConnectionController {
496
479
) : Promise < void > => {
497
480
delete this . _connections [ connectionId ] ;
498
481
499
- if ( this . _keytar ) {
500
- await this . _keytar . deletePassword ( this . _serviceName , connectionId ) ;
482
+ if ( ext . keytarModule ) {
483
+ await ext . keytarModule . deletePassword ( this . _serviceName , connectionId ) ;
501
484
// We only remove the connection from the saved connections if we
502
485
// have deleted the connection information with keytar.
503
486
this . _storageController . removeConnection ( connectionId ) ;
@@ -595,13 +578,13 @@ export default class ConnectionController {
595
578
const connectionNameToRemove :
596
579
| string
597
580
| undefined = await vscode . window . showQuickPick (
598
- connectionIds . map (
599
- ( id , index ) => `${ index + 1 } : ${ this . _connections [ id ] . name } `
600
- ) ,
601
- {
602
- placeHolder : 'Choose a connection to remove...'
603
- }
604
- ) ;
581
+ connectionIds . map (
582
+ ( id , index ) => `${ index + 1 } : ${ this . _connections [ id ] . name } `
583
+ ) ,
584
+ {
585
+ placeHolder : 'Choose a connection to remove...'
586
+ }
587
+ ) ;
605
588
606
589
if ( ! connectionNameToRemove ) {
607
590
return Promise . resolve ( false ) ;
0 commit comments