diff --git a/index.ts b/index.ts index 12c30507a..8cd3c1731 100644 --- a/index.ts +++ b/index.ts @@ -1,7 +1,9 @@ +import { isBrowser } from './src/utils/browser'; + // defined by webpack plugin declare var BUILT: any; -if (typeof window !== 'undefined' && typeof BUILT === 'undefined') { +if (isBrowser() && typeof BUILT === 'undefined') { throw new Error('It looks like you are using the Nodejs version of Kuzzle SDK ' + 'in a browser. ' + 'It is strongly recommended to use the browser-specific build instead. ' + diff --git a/package-lock.json b/package-lock.json index c482b89d7..29fd61138 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "kuzzle-sdk", - "version": "7.10.4", + "version": "7.10.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "kuzzle-sdk", - "version": "7.10.3", + "version": "7.10.5", "license": "Apache-2.0", "dependencies": { "min-req-promise": "^1.0.1", diff --git a/package.json b/package.json index 3629d1d55..8e65472dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kuzzle-sdk", - "version": "7.10.4", + "version": "7.10.5", "description": "Official Javascript SDK for Kuzzle", "author": "The Kuzzle Team ", "repository": { diff --git a/src/protocols/abstract/Realtime.ts b/src/protocols/abstract/Realtime.ts index 929976568..9911f516a 100644 --- a/src/protocols/abstract/Realtime.ts +++ b/src/protocols/abstract/Realtime.ts @@ -2,6 +2,7 @@ import { KuzzleAbstractProtocol } from "./Base"; import * as DisconnectionOrigin from "../DisconnectionOrigin"; +import { getBrowserWindow, isBrowser } from "../../utils/browser"; export abstract class BaseProtocolRealtime extends KuzzleAbstractProtocol { protected _reconnectionDelay: number; @@ -82,13 +83,13 @@ export abstract class BaseProtocolRealtime extends KuzzleAbstractProtocol { if (this.autoReconnect && !this.retrying && !this.stopRetryingToConnect) { this.retrying = true; + const window = getBrowserWindow(); if ( - window !== null && - typeof window === "object" && - typeof window!.navigator === "object" && - window!.navigator.onLine === false + isBrowser() && + typeof window.navigator === "object" && + window.navigator.onLine === false ) { - window!.addEventListener( + window.addEventListener( "online", () => { this.retrying = false; diff --git a/src/utils/browser.ts b/src/utils/browser.ts new file mode 100644 index 000000000..db8695f60 --- /dev/null +++ b/src/utils/browser.ts @@ -0,0 +1,36 @@ +export function getBrowserWindow(): Window | undefined { + let windowObject: Window | undefined; + + try { + windowObject ||= globalThis.window; + if (windowObject) { + return windowObject; + } + } catch { + // Check next variable + } + + try { + windowObject ||= global.window; + if (windowObject) { + return windowObject; + } + } catch { + // Check next variable + } + + try { + windowObject ||= window; + if (windowObject) { + return windowObject; + } + } catch { + // return undefined + } +} + +export function isBrowser(): boolean { + const window = getBrowserWindow(); + + return window !== undefined && window !== null && typeof window === "object"; +} diff --git a/src/utils/debug.js b/src/utils/debug.js index cb504a59a..7e1116634 100644 --- a/src/utils/debug.js +++ b/src/utils/debug.js @@ -1,5 +1,7 @@ let NODE_DEBUG; +const { isBrowser, getBrowserWindow } = require("./browser"); + /* eslint no-undef: 0 */ function shouldDebug() { @@ -13,7 +15,7 @@ function shouldDebug() { * If something went wrong, be sure to return false to avoid any error. */ try { - if (typeof window === "undefined") { + if (!isBrowser()) { // Avoid multiple calls to process.env if (!NODE_DEBUG) { NODE_DEBUG = (process.env.DEBUG || "").includes("kuzzle-sdk"); @@ -22,6 +24,7 @@ function shouldDebug() { return NODE_DEBUG; } + const window = getBrowserWindow(); return ( window.debugKuzzleSdk || (window.location && @@ -49,7 +52,7 @@ function debug(message, obj) { if (obj) { // Browser console can print directly objects - const toPrint = typeof window === "undefined" ? JSON.stringify(obj) : obj; + const toPrint = !isBrowser() ? JSON.stringify(obj) : obj; // eslint-disable-next-line no-console console.log(toPrint);