Skip to content

Commit

Permalink
fix: Do not assume navigator is defined (#1977)
Browse files Browse the repository at this point in the history
Instead get navigator from shared/src/navigator.ts which annotates the
type as `Navigator | undefined` which forces us to check it before using
it.
  • Loading branch information
arv authored Jun 5, 2024
1 parent 6ec3986 commit ff18b04
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 deletions.
7 changes: 4 additions & 3 deletions packages/reflect-client/src/client/connect-checks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {LogContext} from '@rocicorp/logger';
import {resolver} from '@rocicorp/resolver';
import {navigator} from 'shared/src/navigator.js';
import {sleep} from 'shared/src/sleep.js';
import {nanoid} from '../util/nanoid.js';
import {
Expand All @@ -24,7 +25,7 @@ export async function checkConnectivity(
const id = nanoid();
lc = lc.withContext('connectCheckID', id).withContext('checkReason', reason);
lc.info?.('Starting connectivity checks', {
navigatorOnline: navigator.onLine,
navigatorOnline: navigator?.onLine,
});
const socketOrigin = toWSString(server);
const cfChecks: Checks = {
Expand Down Expand Up @@ -80,7 +81,7 @@ export async function checkConnectivity(
(checkName, i) => `${checkName}=${results[i].success}\n`,
),
{
navigatorOnline: navigator.onLine,
navigatorOnline: navigator?.onLine,
},
);
lc.info?.(
Expand All @@ -89,7 +90,7 @@ export async function checkConnectivity(
(checkName, i) => `${checkName}=${results[i].detail}\n`,
),
{
navigatorOnline: navigator.onLine,
navigatorOnline: navigator?.onLine,
},
);
}
Expand Down
9 changes: 5 additions & 4 deletions packages/reflect-client/src/client/reflect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {assert} from 'shared/src/asserts.js';
import {getDocumentVisibilityWatcher} from 'shared/src/document-visible.js';
import {getDocument} from 'shared/src/get-document.js';
import {getWindow} from 'shared/src/get-window.js';
import {navigator} from 'shared/src/navigator.js';
import {sleep, sleepWithAbort} from 'shared/src/sleep.js';
import * as valita from 'shared/src/valita.js';
import {nanoid} from '../util/nanoid.js';
Expand Down Expand Up @@ -637,7 +638,7 @@ export class Reflect<MD extends MutatorDefs> {
const now = Date.now();
const timeToOpenMs = now - this.#connectStart;
l.info?.('Got socket open event', {
navigatorOnline: navigator.onLine,
navigatorOnline: navigator?.onLine,
timeToOpenMs,
});
}
Expand Down Expand Up @@ -729,7 +730,7 @@ export class Reflect<MD extends MutatorDefs> {
this.#metrics.setConnected(timeToConnectMs ?? 0, totalTimeToConnectMs ?? 0);

lc.info?.('Connected', {
navigatorOnline: navigator.onLine,
navigatorOnline: navigator?.onLine,
timeToConnectMs,
totalTimeToConnectMs,
connectMsgLatencyMs,
Expand Down Expand Up @@ -767,7 +768,7 @@ export class Reflect<MD extends MutatorDefs> {

const wsid = nanoid();
l = addWebSocketIDToLogContext(wsid, l);
l.info?.('Connecting...', {navigatorOnline: navigator.onLine});
l.info?.('Connecting...', {navigatorOnline: navigator?.onLine});

this.#setConnectionState(ConnectionState.Connecting);

Expand Down Expand Up @@ -837,7 +838,7 @@ export class Reflect<MD extends MutatorDefs> {
this.#connectErrorCount++;
}
l.info?.('disconnecting', {
navigatorOnline: navigator.onLine,
navigatorOnline: navigator?.onLine,
reason,
connectStart: this.#connectStart,
totalToConnectStart: this.#totalToConnectStart,
Expand Down
3 changes: 2 additions & 1 deletion packages/replicache/src/kv/idb-store-with-mem-fallback.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {LogContext} from '@rocicorp/logger';
import {navigator} from 'shared/src/navigator.js';
import {promiseVoid} from 'shared/src/resolved-promises.js';
import {IDBStore} from './idb-store.js';
import {MemStore, dropMemStore} from './mem-store.js';
Expand Down Expand Up @@ -76,7 +77,7 @@ function isFirefoxPrivateBrowsingError(e: unknown): e is DOMException {
}

function isFirefox(): boolean {
return navigator.userAgent.includes('Firefox');
return navigator?.userAgent.includes('Firefox') ?? false;
}

export function newIDBStoreWithMemFallback(
Expand Down
10 changes: 10 additions & 0 deletions packages/shared/src/navigator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type Navigator = {
onLine: boolean;
userAgent: string;
// add more as needed
};

const localNavigator: Navigator | undefined =
typeof navigator !== 'undefined' ? navigator : undefined;

export {localNavigator as navigator};
7 changes: 4 additions & 3 deletions packages/zero-client/src/client/connect-checks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {LogContext} from '@rocicorp/logger';
import {resolver} from '@rocicorp/resolver';
import {navigator} from 'shared/src/navigator.js';
import {sleep} from 'shared/src/sleep.js';
import {nanoid} from '../util/nanoid.js';
import {
Expand All @@ -24,7 +25,7 @@ export async function checkConnectivity(
const id = nanoid();
lc = lc.withContext('connectCheckID', id).withContext('checkReason', reason);
lc.info?.('Starting connectivity checks', {
navigatorOnline: navigator.onLine,
navigatorOnline: navigator?.onLine,
});
const socketOrigin = toWSString(server);
const cfChecks: Checks = {
Expand Down Expand Up @@ -80,7 +81,7 @@ export async function checkConnectivity(
(checkName, i) => `${checkName}=${results[i].success}\n`,
),
{
navigatorOnline: navigator.onLine,
navigatorOnline: navigator?.onLine,
},
);
lc.info?.(
Expand All @@ -89,7 +90,7 @@ export async function checkConnectivity(
(checkName, i) => `${checkName}=${results[i].detail}\n`,
),
{
navigatorOnline: navigator.onLine,
navigatorOnline: navigator?.onLine,
},
);
}
Expand Down
9 changes: 5 additions & 4 deletions packages/zero-client/src/client/zero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {assert} from 'shared/src/asserts.js';
import {getDocumentVisibilityWatcher} from 'shared/src/document-visible.js';
import {getDocument} from 'shared/src/get-document.js';
import {must} from 'shared/src/must.js';
import {navigator} from 'shared/src/navigator.js';
import {sleep, sleepWithAbort} from 'shared/src/sleep.js';
import type {MaybePromise} from 'shared/src/types.js';
import * as valita from 'shared/src/valita.js';
Expand Down Expand Up @@ -658,7 +659,7 @@ export class Zero<QD extends QueryDefs> {
const now = Date.now();
const timeToOpenMs = now - this.#connectStart;
l.info?.('Got socket open event', {
navigatorOnline: navigator.onLine,
navigatorOnline: navigator?.onLine,
timeToOpenMs,
});
}
Expand Down Expand Up @@ -753,7 +754,7 @@ export class Zero<QD extends QueryDefs> {
this.#metrics.setConnected(timeToConnectMs ?? 0, totalTimeToConnectMs ?? 0);

lc.info?.('Connected', {
navigatorOnline: navigator.onLine,
navigatorOnline: navigator?.onLine,
timeToConnectMs,
totalTimeToConnectMs,
connectMsgLatencyMs,
Expand Down Expand Up @@ -801,7 +802,7 @@ export class Zero<QD extends QueryDefs> {

const wsid = nanoid();
l = addWebSocketIDToLogContext(wsid, l);
l.info?.('Connecting...', {navigatorOnline: navigator.onLine});
l.info?.('Connecting...', {navigatorOnline: navigator?.onLine});

this.#setConnectionState(ConnectionState.Connecting);

Expand Down Expand Up @@ -879,7 +880,7 @@ export class Zero<QD extends QueryDefs> {
this.#connectErrorCount++;
}
l.info?.('disconnecting', {
navigatorOnline: navigator.onLine,
navigatorOnline: navigator?.onLine,
reason,
connectStart: this.#connectStart,
totalToConnectStart: this.#totalToConnectStart,
Expand Down

0 comments on commit ff18b04

Please sign in to comment.