Skip to content
Merged
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
4 changes: 3 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -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. ' +
Expand Down
11 changes: 6 additions & 5 deletions src/protocols/abstract/Realtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions src/utils/browser.ts
Original file line number Diff line number Diff line change
@@ -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";
}
7 changes: 5 additions & 2 deletions src/utils/debug.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
let NODE_DEBUG;

const { isBrowser, getBrowserWindow } = require("./browser");

/* eslint no-undef: 0 */

function shouldDebug() {
Expand All @@ -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");
Expand All @@ -22,6 +24,7 @@ function shouldDebug() {
return NODE_DEBUG;
}

const window = getBrowserWindow();
return (
window.debugKuzzleSdk ||
(window.location &&
Expand Down Expand Up @@ -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);
Expand Down