Skip to content

Commit

Permalink
fix: IS_BROWSER check is now safer and more agnostic about the bundler (
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioimpe committed Jun 23, 2023
1 parent 3b2e1cb commit b48b4b4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/connect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

const MqttClient = require('../client')
const Store = require('../store')
const IS_BROWSER = require('../is-browser').IS_BROWSER
const url = require('url')
const xtend = require('xtend')
const debug = require('debug')('mqttjs')

const protocols = {}

// eslint-disable-next-line camelcase
if ((typeof process !== 'undefined' && process.title !== 'browser') || typeof __webpack_require__ !== 'function') {
if (!IS_BROWSER) {
protocols.mqtt = require('./tcp')
protocols.tcp = require('./tcp')
protocols.ssl = require('./tls')
Expand Down
4 changes: 2 additions & 2 deletions lib/connect/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const WS = require('ws')
const debug = require('debug')('mqttjs:ws')
const duplexify = require('duplexify')
const Transform = require('readable-stream').Transform
const IS_BROWSER = require('../is-browser').IS_BROWSER

const WSS_OPTIONS = [
'rejectUnauthorized',
Expand All @@ -14,8 +15,7 @@ const WSS_OPTIONS = [
'pfx',
'passphrase'
]
// eslint-disable-next-line camelcase
const IS_BROWSER = (typeof process !== 'undefined' && process.title === 'browser') || typeof __webpack_require__ === 'function'

function buildUrl (opts, client) {
let url = opts.protocol + '://' + opts.hostname + ':' + opts.port + opts.path
if (typeof (opts.transformWsUrl) === 'function') {
Expand Down
11 changes: 11 additions & 0 deletions lib/is-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const legacyIsBrowser =
(typeof process !== 'undefined' && process.title === 'browser') ||
// eslint-disable-next-line camelcase
typeof __webpack_require__ === 'function'

const isBrowser =
typeof window !== 'undefined' && typeof document !== 'undefined'

module.exports = {
IS_BROWSER: isBrowser || legacyIsBrowser
}

0 comments on commit b48b4b4

Please sign in to comment.