1
+ import assert from 'assert/strict' ;
1
2
import { EventEmitter } from 'events' ;
2
3
import os from 'os' ;
3
4
import { createLogger } from '@mongodb-js/compass-logging' ;
@@ -178,7 +179,7 @@ const checkForUpdates: StateEnterAction = async function checkForUpdates(
178
179
179
180
this . maybeInterrupt ( ) ;
180
181
181
- if ( updateInfo ) {
182
+ if ( updateInfo . available ) {
182
183
updateManager . setState ( AutoUpdateManagerState . UpdateAvailable , updateInfo ) ;
183
184
} else {
184
185
if ( fromState === AutoUpdateManagerState . UserPromptedManualCheck ) {
@@ -575,6 +576,18 @@ export type AutoUpdateManagerOptions = {
575
576
initialUpdateDelay : number ;
576
577
} ;
577
578
579
+ type AutoUpdateResponse =
580
+ | {
581
+ available : true ;
582
+ name : string ;
583
+ from : string ;
584
+ to : string ;
585
+ }
586
+ | {
587
+ available : false ;
588
+ reason ?: never ;
589
+ } ;
590
+
578
591
const emitter = new EventEmitter ( ) ;
579
592
580
593
class CompassAutoUpdateManager {
@@ -618,26 +631,38 @@ class CompassAutoUpdateManager {
618
631
return url ;
619
632
}
620
633
621
- static async checkForUpdate ( ) : Promise < {
622
- name : string ;
623
- from : string ;
624
- to : string ;
625
- } | null > {
634
+ static async checkForUpdate ( ) : Promise < AutoUpdateResponse > {
626
635
try {
627
636
const response = await this . fetch ( ( await this . getUpdateCheckURL ( ) ) . href ) ;
637
+
628
638
if ( response . status !== 200 ) {
629
- return null ;
639
+ return { available : false } ;
630
640
}
641
+
631
642
try {
632
- return ( await response . json ( ) ) as any ;
643
+ const json = await response . json ( ) ;
644
+ assert (
645
+ typeof json === 'object' && json !== null ,
646
+ 'Expected response to be an object'
647
+ ) ;
648
+ assert ( 'name' in json , 'Expected "name" in response' ) ;
649
+ assert ( 'to' in json , 'Expected "to" in response' ) ;
650
+ assert ( 'from' in json , 'Expected "from" in response' ) ;
651
+
652
+ const { name, from, to } = json ;
653
+ assert ( typeof name === 'string' , 'Expected "name" to be a string' ) ;
654
+ assert ( typeof from === 'string' , 'Expected "from" to be a string' ) ;
655
+ assert ( typeof to === 'string' , 'Expected "to" to be a string' ) ;
656
+
657
+ return { available : true , name, from, to } ;
633
658
} catch ( err ) {
634
659
log . warn (
635
660
mongoLogId ( 1_001_000_163 ) ,
636
661
'AutoUpdateManager' ,
637
662
'Failed to parse update info' ,
638
663
{ error : ( err as Error ) . message }
639
664
) ;
640
- return null ;
665
+ return { available : false } ;
641
666
}
642
667
} catch ( err ) {
643
668
log . warn (
@@ -646,7 +671,7 @@ class CompassAutoUpdateManager {
646
671
'Failed to check for update' ,
647
672
{ error : ( err as Error ) . message }
648
673
) ;
649
- return null ;
674
+ return { available : false } ;
650
675
}
651
676
}
652
677
0 commit comments