@@ -30,6 +30,7 @@ import LINKS from './utils/links';
30
30
import { isAtlasStream } from 'mongodb-build-info' ;
31
31
import type { ConnectionTreeItem } from './explorer' ;
32
32
import { PresetConnectionEditedTelemetryEvent } from './telemetry' ;
33
+ import getBuildInfo from 'mongodb-build-info' ;
33
34
34
35
// eslint-disable-next-line @typescript-eslint/no-var-requires
35
36
const packageJSON = require ( '../package.json' ) ;
@@ -304,13 +305,6 @@ export default class ConnectionController {
304
305
305
306
const connectionStringData = new ConnectionString ( connectionString ) ;
306
307
307
- // TODO: Allow overriding appname + use driverInfo instead
308
- // (https://jira.mongodb.org/browse/MONGOSH-1015)
309
- connectionStringData . searchParams . set (
310
- 'appname' ,
311
- `${ packageJSON . name } ${ packageJSON . version } `
312
- ) ;
313
-
314
308
try {
315
309
const connectResult = await this . saveNewConnectionAndConnect ( {
316
310
connectionId : uuidv4 ( ) ,
@@ -362,6 +356,33 @@ export default class ConnectionController {
362
356
return this . _connect ( connection . id , connectionType ) ;
363
357
}
364
358
359
+ /**
360
+ * In older versions, we'd manually store a connectionString with an appended
361
+ * appName with the VSCode extension name and version. This overrides the
362
+ * connection string if needed and returns true if it does so, false otherwise.
363
+ */
364
+ private _overrideLegacyConnectionStringAppName (
365
+ connectionId : string
366
+ ) : boolean {
367
+ const connectionString = new ConnectionString (
368
+ this . _connections [ connectionId ] . connectionOptions . connectionString
369
+ ) ;
370
+
371
+ if (
372
+ connectionString . searchParams
373
+ . get ( 'appname' )
374
+ ?. match ( / m o n g o d b - v s c o d e \d \. \d \. \d .* / )
375
+ ) {
376
+ connectionString . searchParams . delete ( 'appname' ) ;
377
+
378
+ this . _connections [ connectionId ] . connectionOptions . connectionString =
379
+ connectionString . toString ( ) ;
380
+
381
+ return true ;
382
+ }
383
+ return false ;
384
+ }
385
+
365
386
// eslint-disable-next-line complexity
366
387
async _connect (
367
388
connectionId : string ,
@@ -427,10 +448,17 @@ export default class ConnectionController {
427
448
428
449
const connectionOptions = adjustConnectionOptionsBeforeConnect ( {
429
450
connectionOptions : connectionInfo . connectionOptions ,
430
- defaultAppName : packageJSON . name ,
451
+ connectionInfo : {
452
+ id : connectionId ,
453
+ isAtlas : getBuildInfo . isAtlas (
454
+ connectionInfo . connectionOptions . connectionString
455
+ ) ,
456
+ } ,
457
+ defaultAppName : `${ packageJSON . name } ${ packageJSON . version } ` ,
431
458
notifyDeviceFlow,
432
459
preferences : {
433
460
forceConnectionOptions : [ ] ,
461
+ telemetryAnonymousId : this . _connectionStorage . getUserAnonymousId ( ) ,
434
462
browserCommandForOIDCAuth : undefined , // We overwrite this below.
435
463
} ,
436
464
} ) ;
@@ -620,12 +648,23 @@ export default class ConnectionController {
620
648
}
621
649
622
650
try {
623
- await this . _connect ( connectionId , ConnectionTypes . CONNECTION_ID ) ;
651
+ const wasOverridden =
652
+ this . _overrideLegacyConnectionStringAppName ( connectionId ) ;
624
653
625
- return {
626
- successfullyConnected : true ,
627
- connectionErrorMessage : '' ,
628
- } ;
654
+ const result = await this . _connect (
655
+ connectionId ,
656
+ ConnectionTypes . CONNECTION_ID
657
+ ) ;
658
+
659
+ /** After successfully connecting with an overridden connection
660
+ * string, save it to storage for the future. */
661
+ if ( result . successfullyConnected && wasOverridden ) {
662
+ await this . _connectionStorage . saveConnection (
663
+ this . _connections [ connectionId ]
664
+ ) ;
665
+ }
666
+
667
+ return result ;
629
668
} catch ( error ) {
630
669
log . error ( 'Failed to connect by a connection id' , error ) ;
631
670
const printableError = formatError ( error ) ;
0 commit comments