Skip to content

Commit b48b4b4

Browse files
authored
fix: IS_BROWSER check is now safer and more agnostic about the bundler (#1571)
1 parent 3b2e1cb commit b48b4b4

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

lib/connect/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
const MqttClient = require('../client')
44
const Store = require('../store')
5+
const IS_BROWSER = require('../is-browser').IS_BROWSER
56
const url = require('url')
67
const xtend = require('xtend')
78
const debug = require('debug')('mqttjs')
89

910
const protocols = {}
1011

11-
// eslint-disable-next-line camelcase
12-
if ((typeof process !== 'undefined' && process.title !== 'browser') || typeof __webpack_require__ !== 'function') {
12+
if (!IS_BROWSER) {
1313
protocols.mqtt = require('./tcp')
1414
protocols.tcp = require('./tcp')
1515
protocols.ssl = require('./tls')

lib/connect/ws.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const WS = require('ws')
55
const debug = require('debug')('mqttjs:ws')
66
const duplexify = require('duplexify')
77
const Transform = require('readable-stream').Transform
8+
const IS_BROWSER = require('../is-browser').IS_BROWSER
89

910
const WSS_OPTIONS = [
1011
'rejectUnauthorized',
@@ -14,8 +15,7 @@ const WSS_OPTIONS = [
1415
'pfx',
1516
'passphrase'
1617
]
17-
// eslint-disable-next-line camelcase
18-
const IS_BROWSER = (typeof process !== 'undefined' && process.title === 'browser') || typeof __webpack_require__ === 'function'
18+
1919
function buildUrl (opts, client) {
2020
let url = opts.protocol + '://' + opts.hostname + ':' + opts.port + opts.path
2121
if (typeof (opts.transformWsUrl) === 'function') {

lib/is-browser.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const legacyIsBrowser =
2+
(typeof process !== 'undefined' && process.title === 'browser') ||
3+
// eslint-disable-next-line camelcase
4+
typeof __webpack_require__ === 'function'
5+
6+
const isBrowser =
7+
typeof window !== 'undefined' && typeof document !== 'undefined'
8+
9+
module.exports = {
10+
IS_BROWSER: isBrowser || legacyIsBrowser
11+
}

0 commit comments

Comments
 (0)