Skip to content

Commit a6e74ad

Browse files
alex-controlxAlex Sharikov
andauthored
fix: isWebWorkerEnv check for Deno environment (#2023)
# Deno Worker Fix Explanation ## The Problem The issue was that MQTT.js was incorrectly identifying Deno workers as browser environments, which caused problems when trying to use TCP connections. Here's what was happening: 1. **Original `isWebWorkerEnv()` logic**: The function was checking if the environment was a Web Worker by looking for `self?.constructor?.name?.includes('WorkerGlobalScope')` 2. **Deno Worker confusion**: In Deno workers, this condition was returning `true` because Deno workers do have a `WorkerGlobalScope`-like environment (`DedicatedWorkerGlobalScope` to be exact) 3. **Incorrect browser detection**: Since `isWebWorkerEnv()` returned `true`, the overall `isBrowser` check also returned `true` (line 37: `isBrowser = isStandardBrowserEnv() || isWebWorkerEnv() || isReactNativeEnv()`) 4. **TCP connection blocked**: When `isBrowser` was `true`, the MQTT connection logic would force WebSocket connections (`ws://` or `wss://`) and didn't allow TCP protocols (`mqtt://` or `mqtts://`) ## The Solution By adding `typeof Deno === "undefined"` to the `isWebWorkerEnv()` function, you're ensuring that: - **Deno workers are NOT considered web workers**: The function now returns `false` for Deno workers, even though they have a `WorkerGlobalScope`-like environment - **Real web workers still work**: Browser-based web workers don't have `Deno` defined, so they still get detected correctly - **TCP connections work in Deno**: Since `isBrowser` now returns `false` for Deno workers, the connection logic allows TCP connections (`mqtt://`, `mqtts://`) instead of forcing WebSocket connections ## Why This Matters Deno workers need to be able to use TCP connections for MQTT because: - They run in a server-like environment (not a browser) - TCP connections are more efficient for MQTT than WebSocket connections - The original browser detection was preventing legitimate TCP usage in Deno The fix ensures that Deno workers are treated as server environments (like Node.js) rather than browser environments, allowing them to use the appropriate connection method for MQTT protocols. --------- Co-authored-by: Alex Sharikov <alex@controlx.au>
1 parent 7e2bfe4 commit a6e74ad

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ junit.xml
2323

2424
/build/
2525

26+
# macOS stuff
27+
.DS_Store
28+
.DS_Store?

src/lib/is-browser.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// Global type declaration for Deno
2+
declare global {
3+
const Deno: any
4+
}
5+
16
const isStandardBrowserEnv = () => {
27
// window is only defined when it is a browser
38
if (typeof window !== 'undefined') {
@@ -28,7 +33,8 @@ const isStandardBrowserEnv = () => {
2833
const isWebWorkerEnv = () =>
2934
Boolean(
3035
typeof self === 'object' &&
31-
self?.constructor?.name?.includes('WorkerGlobalScope'),
36+
self?.constructor?.name?.includes('WorkerGlobalScope') &&
37+
typeof Deno === 'undefined',
3238
)
3339

3440
const isReactNativeEnv = () =>

0 commit comments

Comments
 (0)