Skip to content

Commit 140a0c5

Browse files
committed
Return { available: false } instead of null on no updates
1 parent bdb4e43 commit 140a0c5

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

packages/compass/src/main/auto-update-manager.spec.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe('CompassAutoUpdateManager', function () {
109109
const stub = sandbox
110110
.stub(CompassAutoUpdateManager, 'checkForUpdate')
111111
.callsFake(() => {
112-
return Promise.resolve(null);
112+
return Promise.resolve({ available: false });
113113
});
114114

115115
expect(
@@ -128,7 +128,12 @@ describe('CompassAutoUpdateManager', function () {
128128
const stub = sandbox
129129
.stub(CompassAutoUpdateManager, 'checkForUpdate')
130130
.callsFake(() => {
131-
return Promise.resolve({ from: '0.0.0', to: '1.0.0', name: '1.0.0' });
131+
return Promise.resolve({
132+
available: true,
133+
from: '0.0.0',
134+
to: '1.0.0',
135+
name: '1.0.0',
136+
});
132137
});
133138

134139
expect(
@@ -147,7 +152,12 @@ describe('CompassAutoUpdateManager', function () {
147152
const stub = sandbox
148153
.stub(CompassAutoUpdateManager, 'checkForUpdate')
149154
.callsFake(() => {
150-
return Promise.resolve({ from: '0.0.0', to: '1.0.0', name: '1.0.0' });
155+
return Promise.resolve({
156+
available: true,
157+
from: '0.0.0',
158+
to: '1.0.0',
159+
name: '1.0.0',
160+
});
151161
});
152162

153163
expect(
@@ -165,7 +175,12 @@ describe('CompassAutoUpdateManager', function () {
165175
const stub = sandbox
166176
.stub(CompassAutoUpdateManager, 'checkForUpdate')
167177
.callsFake(() => {
168-
return wait(100, { from: '0.0.0', to: '1.0.0', name: '1.0.0' });
178+
return wait(100, {
179+
available: true,
180+
from: '0.0.0',
181+
to: '1.0.0',
182+
name: '1.0.0',
183+
});
169184
});
170185

171186
CompassAutoUpdateManager.setState(

packages/compass/src/main/auto-update-manager.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from 'assert/strict';
12
import { EventEmitter } from 'events';
23
import os from 'os';
34
import { 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+
578591
const emitter = new EventEmitter();
579592

580593
class 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

Comments
 (0)