Skip to content

Commit 21dcac8

Browse files
author
Shiranuit
authored
Merge pull request #721 from kuzzleio/7.10.5-proposal
Release 7.10.5
2 parents 25e1cd5 + 6b1790a commit 21dcac8

File tree

6 files changed

+53
-11
lines changed

6 files changed

+53
-11
lines changed

index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { isBrowser } from './src/utils/browser';
2+
13
// defined by webpack plugin
24
declare var BUILT: any;
35

4-
if (typeof window !== 'undefined' && typeof BUILT === 'undefined') {
6+
if (isBrowser() && typeof BUILT === 'undefined') {
57
throw new Error('It looks like you are using the Nodejs version of Kuzzle SDK ' +
68
'in a browser. ' +
79
'It is strongly recommended to use the browser-specific build instead. ' +

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "7.10.4",
3+
"version": "7.10.5",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {

src/protocols/abstract/Realtime.ts

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

33
import { KuzzleAbstractProtocol } from "./Base";
44
import * as DisconnectionOrigin from "../DisconnectionOrigin";
5+
import { getBrowserWindow, isBrowser } from "../../utils/browser";
56

67
export abstract class BaseProtocolRealtime extends KuzzleAbstractProtocol {
78
protected _reconnectionDelay: number;
@@ -82,13 +83,13 @@ export abstract class BaseProtocolRealtime extends KuzzleAbstractProtocol {
8283
if (this.autoReconnect && !this.retrying && !this.stopRetryingToConnect) {
8384
this.retrying = true;
8485

86+
const window = getBrowserWindow();
8587
if (
86-
window !== null &&
87-
typeof window === "object" &&
88-
typeof window!.navigator === "object" &&
89-
window!.navigator.onLine === false
88+
isBrowser() &&
89+
typeof window.navigator === "object" &&
90+
window.navigator.onLine === false
9091
) {
91-
window!.addEventListener(
92+
window.addEventListener(
9293
"online",
9394
() => {
9495
this.retrying = false;

src/utils/browser.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export function getBrowserWindow(): Window | undefined {
2+
let windowObject: Window | undefined;
3+
4+
try {
5+
windowObject ||= globalThis.window;
6+
if (windowObject) {
7+
return windowObject;
8+
}
9+
} catch {
10+
// Check next variable
11+
}
12+
13+
try {
14+
windowObject ||= global.window;
15+
if (windowObject) {
16+
return windowObject;
17+
}
18+
} catch {
19+
// Check next variable
20+
}
21+
22+
try {
23+
windowObject ||= window;
24+
if (windowObject) {
25+
return windowObject;
26+
}
27+
} catch {
28+
// return undefined
29+
}
30+
}
31+
32+
export function isBrowser(): boolean {
33+
const window = getBrowserWindow();
34+
35+
return window !== undefined && window !== null && typeof window === "object";
36+
}

src/utils/debug.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
let NODE_DEBUG;
22

3+
const { isBrowser, getBrowserWindow } = require("./browser");
4+
35
/* eslint no-undef: 0 */
46

57
function shouldDebug() {
@@ -13,7 +15,7 @@ function shouldDebug() {
1315
* If something went wrong, be sure to return false to avoid any error.
1416
*/
1517
try {
16-
if (typeof window === "undefined") {
18+
if (!isBrowser()) {
1719
// Avoid multiple calls to process.env
1820
if (!NODE_DEBUG) {
1921
NODE_DEBUG = (process.env.DEBUG || "").includes("kuzzle-sdk");
@@ -22,6 +24,7 @@ function shouldDebug() {
2224
return NODE_DEBUG;
2325
}
2426

27+
const window = getBrowserWindow();
2528
return (
2629
window.debugKuzzleSdk ||
2730
(window.location &&
@@ -49,7 +52,7 @@ function debug(message, obj) {
4952

5053
if (obj) {
5154
// Browser console can print directly objects
52-
const toPrint = typeof window === "undefined" ? JSON.stringify(obj) : obj;
55+
const toPrint = !isBrowser() ? JSON.stringify(obj) : obj;
5356

5457
// eslint-disable-next-line no-console
5558
console.log(toPrint);

0 commit comments

Comments
 (0)