Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: electron main and renderer process detection #1879

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 14 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
} from './shared'
import TopicAliasSend from './topic-alias-send'
import { TypedEventEmitter } from './TypedEmitter'
import KeepaliveManager from './KeepaliveManager'

Check failure on line 42 in src/lib/client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Dependency cycle via ./get-timer:2=>./is-browser:1
import isBrowser, { isWebWorker } from './is-browser'

const setImmediate =
Expand Down Expand Up @@ -405,6 +405,7 @@
*/
export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbacks> {
public static VERSION = MQTTJS_VERSION
public static NEED_CHECK_BROWSER_ENVIRONMENT = true

Check failure on line 408 in src/lib/client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected blank line between class members

/** Public fields */

Expand Down Expand Up @@ -486,6 +487,10 @@
return `mqttjs_${Math.random().toString(16).substr(2, 8)}`
}

public get needCheckBrowserEnvironment() {
return MqttClient.NEED_CHECK_BROWSER_ENVIRONMENT
}

constructor(streamBuilder: StreamBuilder, options: IClientOptions) {
super()

Expand Down
29 changes: 4 additions & 25 deletions src/lib/is-browser.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,7 @@
import MqttClient from './client'

Check failure on line 1 in src/lib/is-browser.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Dependency cycle detected

const isStandardBrowserEnv = () => {
// window is only defined when it is a browser
if (typeof window !== 'undefined') {
// Is the process an electron application
// check if we are in electron `renderer`
const electronRenderCheck =
typeof navigator !== 'undefined' &&
navigator.userAgent?.toLowerCase().indexOf(' electron/') > -1
if (electronRenderCheck && process?.versions) {
const electronMainCheck = Object.prototype.hasOwnProperty.call(
process.versions,
'electron',
)
// Both electron checks are only true if the following webPreferences are set in the main electron BrowserWindow()
// webPreferences: {
// sandbox: false,
// nodeIntegration: true
// contextIsolation: false
// }
return !electronMainCheck
}
return typeof window.document !== 'undefined'
}
// return false if nothing is detected
return false
return typeof window !== 'undefined' && typeof window.document !== 'undefined'

Check failure on line 4 in src/lib/is-browser.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Replace `typeof·window·!==·'undefined'·&&·typeof·window.document·!==·'undefined'` with `(⏎↹↹typeof·window·!==·'undefined'·&&·typeof·window.document·!==·'undefined'⏎↹)`
}

const isWebWorkerEnv = () =>
Expand All @@ -37,7 +16,7 @@
typeof navigator !== 'undefined' && navigator.product === 'ReactNative'

const isBrowser =
isStandardBrowserEnv() || isWebWorkerEnv() || isReactNativeEnv()
MqttClient.NEED_CHECK_BROWSER_ENVIRONMENT !== false && (isStandardBrowserEnv() || isWebWorkerEnv() || isReactNativeEnv())

Check failure on line 19 in src/lib/is-browser.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Replace `·` with `⏎↹`

export const isWebWorker = isWebWorkerEnv()

Expand Down
Loading