Patch out unsafe usage of navigator and window in node envs

This commit is contained in:
Daniel Imms
2024-07-10 06:22:16 -07:00
parent b401865957
commit 27195b003a
5 changed files with 10 additions and 11 deletions
+1 -5
View File
@@ -173,11 +173,7 @@ if (config.addon) {
outConfig = {
...outConfig,
entryPoints: [
`src/browser/public/Terminal.ts`,
`src/headless/public/Terminal.ts`,
`src/browser/**/*.test.ts`,
`src/common/**/*.test.ts`,
`src/headless/**/*.test.ts`
`src/**/*.ts`
],
outdir: 'out-esbuild/'
};
+1
View File
@@ -29,6 +29,7 @@ describe('Terminal', () => {
term = new TestTerminal(termOptions);
term.refresh = () => { };
(term as any).renderer = new MockRenderer();
(term as any).viewport = new MockViewport();
(term as any)._compositionHelper = new MockCompositionHelper();
(term as any).element = {
classList: {
+1 -1
View File
@@ -98,7 +98,7 @@ export function isFullscreen(targetWindow: Window): boolean {
}
export const onDidChangeFullscreen = WindowManager.INSTANCE.onDidChangeFullscreen;
const userAgent = navigator.userAgent;
const userAgent = typeof navigator === 'object' ? navigator.userAgent : '';
export const isFirefox = (userAgent.indexOf('Firefox') >= 0);
export const isWebKit = (userAgent.indexOf('AppleWebKit') >= 0);
+6 -4
View File
@@ -13,6 +13,8 @@ export const enum KeyboardSupport {
None
}
const safeNavigator = typeof navigator === 'object' ? navigator : {} as { [key: string]: any };
/**
* Browser feature we can support in current platform, browser and environment.
*/
@@ -21,11 +23,11 @@ export const BrowserFeatures = {
writeText: (
platform.isNative
|| (document.queryCommandSupported && document.queryCommandSupported('copy'))
|| !!(navigator && navigator.clipboard && navigator.clipboard.writeText)
|| !!(safeNavigator && safeNavigator.clipboard && safeNavigator.clipboard.writeText)
),
readText: (
platform.isNative
|| !!(navigator && navigator.clipboard && navigator.clipboard.readText)
|| !!(safeNavigator && safeNavigator.clipboard && safeNavigator.clipboard.readText)
)
},
keyboard: (() => {
@@ -33,7 +35,7 @@ export const BrowserFeatures = {
return KeyboardSupport.Always;
}
if ((<any>navigator).keyboard || browser.isSafari) {
if ((<any>safeNavigator).keyboard || browser.isSafari) {
return KeyboardSupport.FullScreen;
}
@@ -42,6 +44,6 @@ export const BrowserFeatures = {
// 'ontouchstart' in window always evaluates to true with typescript's modern typings. This causes `window` to be
// `never` later in `window.navigator`. That's why we need the explicit `window as Window` cast
touch: 'ontouchstart' in mainWindow || navigator.maxTouchPoints > 0,
touch: 'ontouchstart' in mainWindow || safeNavigator.maxTouchPoints > 0,
pointerEvents: mainWindow.PointerEvent && ('ontouchstart' in mainWindow || navigator.maxTouchPoints > 0)
};
+1 -1
View File
@@ -11,4 +11,4 @@ export function ensureCodeWindow(targetWindow: Window, fallbackWindowId: number)
}
// eslint-disable-next-line no-restricted-globals
export const mainWindow = window as CodeWindow;
export const mainWindow = (typeof window === 'object' ? window : globalThis) as CodeWindow;