1+ import assert from 'assert/strict' ;
12import { EventEmitter } from 'events' ;
23import os from 'os' ;
34import { createLogger } from '@mongodb-js/compass-logging' ;
@@ -178,7 +179,7 @@ const checkForUpdates: StateEnterAction = async function checkForUpdates(
178179
179180 this . maybeInterrupt ( ) ;
180181
181- if ( updateInfo ) {
182+ if ( updateInfo . available ) {
182183 updateManager . setState ( AutoUpdateManagerState . UpdateAvailable , updateInfo ) ;
183184 } else {
184185 if ( fromState === AutoUpdateManagerState . UserPromptedManualCheck ) {
@@ -575,6 +576,18 @@ export type AutoUpdateManagerOptions = {
575576 initialUpdateDelay : number ;
576577} ;
577578
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+
578591const emitter = new EventEmitter ( ) ;
579592
580593class CompassAutoUpdateManager {
@@ -618,26 +631,37 @@ class CompassAutoUpdateManager {
618631 return url ;
619632 }
620633
621- static async checkForUpdate ( ) : Promise < {
622- name : string ;
623- from : string ;
624- to : string ;
625- } | null > {
634+ static async checkForUpdate ( ) : Promise < AutoUpdateResponse > {
626635 try {
627636 const response = await this . fetch ( ( await this . getUpdateCheckURL ( ) ) . href ) ;
628- if ( response . status !== 200 ) {
629- return null ;
630- }
637+
638+
639+ assert ( response . ok ) ;
640+
631641 try {
632- return ( await response . json ( ) ) as any ;
642+ const json = await response . json ( ) ;
643+ assert (
644+ typeof json === 'object' && json !== null ,
645+ 'Expected response to be an object'
646+ ) ;
647+ assert ( 'name' in json , 'Expected "name" in response' ) ;
648+ assert ( 'to' in json , 'Expected "to" in response' ) ;
649+ assert ( 'from' in json , 'Expected "from" in response' ) ;
650+
651+ const { name, from, to } = json ;
652+ assert ( typeof name === 'string' , 'Expected "name" to be a string' ) ;
653+ assert ( typeof from === 'string' , 'Expected "from" to be a string' ) ;
654+ assert ( typeof to === 'string' , 'Expected "to" to be a string' ) ;
655+
656+ return { available : true , name, from, to } ;
633657 } catch ( err ) {
634658 log . warn (
635659 mongoLogId ( 1_001_000_163 ) ,
636660 'AutoUpdateManager' ,
637661 'Failed to parse update info' ,
638662 { error : ( err as Error ) . message }
639663 ) ;
640- return null ;
664+ return { available : false } ;
641665 }
642666 } catch ( err ) {
643667 log . warn (
@@ -646,7 +670,7 @@ class CompassAutoUpdateManager {
646670 'Failed to check for update' ,
647671 { error : ( err as Error ) . message }
648672 ) ;
649- return null ;
673+ return { available : false } ;
650674 }
651675 }
652676
0 commit comments