@@ -2,33 +2,31 @@ import { v4 as uuidv4 } from 'uuid';
22import * as vscode from 'vscode' ;
33import Connection = require( 'mongodb-connection-model/lib/model' ) ;
44import DataService = require( 'mongodb-data-service' ) ;
5- import * as keytarType from 'keytar' ;
5+
66import { ConnectionModelType } from './connectionModelType' ;
77import { DataServiceType } from './dataServiceType' ;
88import { createLogger } from './logging' ;
99import { StatusView } from './views' ;
1010import { EventEmitter } from 'events' ;
1111import { StorageController , StorageVariables } from './storage' ;
1212import { SavedConnection , StorageScope } from './storage/storageController' ;
13- import { getNodeModule } from './utils/getNodeModule' ;
1413import TelemetryController from './telemetry/telemetryController' ;
14+ import { ext } from './extensionConstants' ;
1515
1616const { name, version } = require ( '../package.json' ) ;
1717const log = createLogger ( 'connection controller' ) ;
1818const MAX_CONNECTION_NAME_LENGTH = 512 ;
1919
20- type KeyTar = typeof keytarType ;
21-
2220export enum DataServiceEventTypes {
2321 CONNECTIONS_DID_CHANGE = 'CONNECTIONS_DID_CHANGE' ,
2422 ACTIVE_CONNECTION_CHANGED = 'ACTIVE_CONNECTION_CHANGED' ,
25- ACTIVE_CONNECTION_CHANGING = 'ACTIVE_CONNECTION_CHANGING'
23+ ACTIVE_CONNECTION_CHANGING = 'ACTIVE_CONNECTION_CHANGING' ,
2624}
2725
2826export enum ConnectionTypes {
2927 CONNECTION_FORM = 'CONNECTION_FORM' ,
3028 CONNECTION_STRING = 'CONNECTION_STRING' ,
31- CONNECTION_ID = 'CONNECTION_ID'
29+ CONNECTION_ID = 'CONNECTION_ID' ,
3230}
3331
3432export type SavedConnectionInformation = {
@@ -48,8 +46,6 @@ export default class ConnectionController {
4846 } = { } ;
4947
5048 private readonly _serviceName = 'mdb.vscode.savedConnections' ;
51- private _keytar : KeyTar | undefined ;
52-
5349 _activeDataService : null | DataServiceType = null ;
5450 _activeConnectionModel : null | ConnectionModelType = null ;
5551 private _currentConnectionId : null | string = null ;
@@ -73,33 +69,20 @@ export default class ConnectionController {
7369 this . _statusView = _statusView ;
7470 this . _storageController = storageController ;
7571 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- }
8972 }
9073
9174 _loadSavedConnection = async (
9275 connectionId : string ,
9376 savedConnection : SavedConnection
9477 ) : Promise < void > => {
95- if ( ! this . _keytar ) {
78+ if ( ! ext . keytarModule ) {
9679 return ;
9780 }
9881
9982 let loadedSavedConnection : LoadedConnection ;
10083
10184 try {
102- const unparsedConnectionInformation = await this . _keytar . getPassword (
85+ const unparsedConnectionInformation = await ext . keytarModule . getPassword (
10386 this . _serviceName ,
10487 connectionId
10588 ) ;
@@ -138,7 +121,7 @@ export default class ConnectionController {
138121 } ;
139122
140123 loadSavedConnections = async ( ) : Promise < void > => {
141- if ( ! this . _keytar ) {
124+ if ( ! ext . keytarModule ) {
142125 return ;
143126 }
144127
@@ -297,10 +280,10 @@ export default class ConnectionController {
297280
298281 this . _connections [ connectionId ] = newLoadedConnection ;
299282
300- if ( this . _keytar ) {
283+ if ( ext . keytarModule ) {
301284 const connectionInfoAsString = JSON . stringify ( connectionInformation ) ;
302285
303- await this . _keytar . setPassword (
286+ await ext . keytarModule . setPassword (
304287 this . _serviceName ,
305288 connectionId ,
306289 connectionInfoAsString
@@ -496,8 +479,8 @@ export default class ConnectionController {
496479 ) : Promise < void > => {
497480 delete this . _connections [ connectionId ] ;
498481
499- if ( this . _keytar ) {
500- await this . _keytar . deletePassword ( this . _serviceName , connectionId ) ;
482+ if ( ext . keytarModule ) {
483+ await ext . keytarModule . deletePassword ( this . _serviceName , connectionId ) ;
501484 // We only remove the connection from the saved connections if we
502485 // have deleted the connection information with keytar.
503486 this . _storageController . removeConnection ( connectionId ) ;
@@ -595,13 +578,13 @@ export default class ConnectionController {
595578 const connectionNameToRemove :
596579 | string
597580 | 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+ ) ;
605588
606589 if ( ! connectionNameToRemove ) {
607590 return Promise . resolve ( false ) ;
0 commit comments