Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions packages/compass/src/main/auto-update-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe('CompassAutoUpdateManager', function () {
const stub = sandbox
.stub(CompassAutoUpdateManager, 'checkForUpdate')
.callsFake(() => {
return Promise.resolve(null);
return Promise.resolve({ available: false });
});

expect(
Expand All @@ -128,7 +128,12 @@ describe('CompassAutoUpdateManager', function () {
const stub = sandbox
.stub(CompassAutoUpdateManager, 'checkForUpdate')
.callsFake(() => {
return Promise.resolve({ from: '0.0.0', to: '1.0.0', name: '1.0.0' });
return Promise.resolve({
available: true,
from: '0.0.0',
to: '1.0.0',
name: '1.0.0',
});
});

expect(
Expand All @@ -147,7 +152,12 @@ describe('CompassAutoUpdateManager', function () {
const stub = sandbox
.stub(CompassAutoUpdateManager, 'checkForUpdate')
.callsFake(() => {
return Promise.resolve({ from: '0.0.0', to: '1.0.0', name: '1.0.0' });
return Promise.resolve({
available: true,
from: '0.0.0',
to: '1.0.0',
name: '1.0.0',
});
});

expect(
Expand All @@ -165,7 +175,12 @@ describe('CompassAutoUpdateManager', function () {
const stub = sandbox
.stub(CompassAutoUpdateManager, 'checkForUpdate')
.callsFake(() => {
return wait(100, { from: '0.0.0', to: '1.0.0', name: '1.0.0' });
return wait(100, {
available: true,
from: '0.0.0',
to: '1.0.0',
name: '1.0.0',
});
});

CompassAutoUpdateManager.setState(
Expand All @@ -191,7 +206,7 @@ describe('CompassAutoUpdateManager', function () {
sandbox.stub(autoUpdater);
});

describe('when electron does not support plaform updates', function () {
describe('when electron does not support platform updates', function () {
before(function () {
if (supportsAutoupdates) {
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -233,7 +248,7 @@ describe('CompassAutoUpdateManager', function () {
});
});

describe('when electron supports plaform updates', function () {
describe('when electron supports platform updates', function () {
before(function () {
if (!supportsAutoupdates) {
// eslint-disable-next-line no-console
Expand Down
45 changes: 35 additions & 10 deletions packages/compass/src/main/auto-update-manager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'assert/strict';
import { EventEmitter } from 'events';
import os from 'os';
import { createLogger } from '@mongodb-js/compass-logging';
Expand Down Expand Up @@ -178,7 +179,7 @@ const checkForUpdates: StateEnterAction = async function checkForUpdates(

this.maybeInterrupt();

if (updateInfo) {
if (updateInfo.available) {
updateManager.setState(AutoUpdateManagerState.UpdateAvailable, updateInfo);
} else {
if (fromState === AutoUpdateManagerState.UserPromptedManualCheck) {
Expand Down Expand Up @@ -575,6 +576,18 @@ export type AutoUpdateManagerOptions = {
initialUpdateDelay: number;
};

type AutoUpdateResponse =
| {
available: true;
name: string;
from: string;
to: string;
}
| {
available: false;
reason?: never;
};

const emitter = new EventEmitter();

class CompassAutoUpdateManager {
Expand Down Expand Up @@ -618,26 +631,38 @@ class CompassAutoUpdateManager {
return url;
}

static async checkForUpdate(): Promise<{
name: string;
from: string;
to: string;
} | null> {
static async checkForUpdate(): Promise<AutoUpdateResponse> {
try {
const response = await this.fetch((await this.getUpdateCheckURL()).href);

if (response.status !== 200) {
return null;
return { available: false };
}

try {
return (await response.json()) as any;
const json = await response.json();
assert(
typeof json === 'object' && json !== null,
'Expected response to be an object'
);
assert('name' in json, 'Expected "name" in response');
assert('to' in json, 'Expected "to" in response');
assert('from' in json, 'Expected "from" in response');

const { name, from, to } = json;
assert(typeof name === 'string', 'Expected "name" to be a string');
assert(typeof from === 'string', 'Expected "from" to be a string');
assert(typeof to === 'string', 'Expected "to" to be a string');

return { available: true, name, from, to };
Comment on lines +643 to +657
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm including this more defensive parsing of the response from the server.

} catch (err) {
log.warn(
mongoLogId(1_001_000_163),
'AutoUpdateManager',
'Failed to parse update info',
{ error: (err as Error).message }
);
return null;
return { available: false };
}
} catch (err) {
log.warn(
Expand All @@ -646,7 +671,7 @@ class CompassAutoUpdateManager {
'Failed to check for update',
{ error: (err as Error).message }
);
return null;
return { available: false };
}
}

Expand Down