From 0585512a2b9c1ea1eb55c8f1361d5b56c59ab87e Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Tue, 3 Jan 2023 18:06:22 +0000 Subject: [PATCH 1/2] Also check isElectronNodeRenderer --- packages/replay/src/integration.ts | 6 +++--- packages/replay/src/util/isBrowser.ts | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/replay/src/integration.ts b/packages/replay/src/integration.ts index b68ddd7f11fa..c71b4041bb26 100644 --- a/packages/replay/src/integration.ts +++ b/packages/replay/src/integration.ts @@ -5,7 +5,7 @@ import { Integration } from '@sentry/types'; import { DEFAULT_ERROR_SAMPLE_RATE, DEFAULT_SESSION_SAMPLE_RATE, MASK_ALL_TEXT_SELECTOR } from './constants'; import { ReplayContainer } from './replay'; import type { RecordingOptions, ReplayConfiguration, ReplayPluginOptions } from './types'; -import { isBrowser } from './util/isBrowser'; +import { isBrowser, isElectronNodeRenderer } from './util/isBrowser'; const MEDIA_SELECTORS = 'img,image,svg,path,rect,area,video,object,picture,embed,map,audio'; @@ -120,7 +120,7 @@ Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`, : `${this.recordingOptions.blockSelector},${MEDIA_SELECTORS}`; } - if (isBrowser() && this._isInitialized) { + if (this._isInitialized && (isBrowser() || isElectronNodeRenderer())) { throw new Error('Multiple Sentry Session Replay instances are not supported'); } @@ -138,7 +138,7 @@ Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`, * here to avoid any future issues. */ setupOnce(): void { - if (!isBrowser()) { + if (!isBrowser() && !isElectronNodeRenderer()) { return; } diff --git a/packages/replay/src/util/isBrowser.ts b/packages/replay/src/util/isBrowser.ts index fb87bc3c7e55..cb35f75eb2ef 100644 --- a/packages/replay/src/util/isBrowser.ts +++ b/packages/replay/src/util/isBrowser.ts @@ -4,3 +4,9 @@ export function isBrowser(): boolean { // eslint-disable-next-line no-restricted-globals return typeof window !== 'undefined' && !isNodeEnv(); } + +type ElectronProcess = { type?: string }; + +export function isElectronNodeRenderer(): boolean { + return typeof process !== 'undefined' && (process as ElectronProcess).type === 'renderer'; +} From 0ecefa38984a720474fb771944f6238bc61170fd Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 4 Jan 2023 14:46:12 +0000 Subject: [PATCH 2/2] Review --- packages/replay/src/integration.ts | 6 +++--- packages/replay/src/util/isBrowser.ts | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/replay/src/integration.ts b/packages/replay/src/integration.ts index c71b4041bb26..449c19ec9f00 100644 --- a/packages/replay/src/integration.ts +++ b/packages/replay/src/integration.ts @@ -5,7 +5,7 @@ import { Integration } from '@sentry/types'; import { DEFAULT_ERROR_SAMPLE_RATE, DEFAULT_SESSION_SAMPLE_RATE, MASK_ALL_TEXT_SELECTOR } from './constants'; import { ReplayContainer } from './replay'; import type { RecordingOptions, ReplayConfiguration, ReplayPluginOptions } from './types'; -import { isBrowser, isElectronNodeRenderer } from './util/isBrowser'; +import { isBrowser } from './util/isBrowser'; const MEDIA_SELECTORS = 'img,image,svg,path,rect,area,video,object,picture,embed,map,audio'; @@ -120,7 +120,7 @@ Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`, : `${this.recordingOptions.blockSelector},${MEDIA_SELECTORS}`; } - if (this._isInitialized && (isBrowser() || isElectronNodeRenderer())) { + if (this._isInitialized && isBrowser()) { throw new Error('Multiple Sentry Session Replay instances are not supported'); } @@ -138,7 +138,7 @@ Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`, * here to avoid any future issues. */ setupOnce(): void { - if (!isBrowser() && !isElectronNodeRenderer()) { + if (!isBrowser()) { return; } diff --git a/packages/replay/src/util/isBrowser.ts b/packages/replay/src/util/isBrowser.ts index cb35f75eb2ef..3ad78dce93a5 100644 --- a/packages/replay/src/util/isBrowser.ts +++ b/packages/replay/src/util/isBrowser.ts @@ -2,11 +2,12 @@ import { isNodeEnv } from '@sentry/utils'; export function isBrowser(): boolean { // eslint-disable-next-line no-restricted-globals - return typeof window !== 'undefined' && !isNodeEnv(); + return typeof window !== 'undefined' && (!isNodeEnv() || isElectronNodeRenderer()); } type ElectronProcess = { type?: string }; -export function isElectronNodeRenderer(): boolean { +// Electron renderers with nodeIntegration enabled are detected as Node.js so we specifically test for them +function isElectronNodeRenderer(): boolean { return typeof process !== 'undefined' && (process as ElectronProcess).type === 'renderer'; }