File tree Expand file tree Collapse file tree 4 files changed +50
-8
lines changed
Expand file tree Collapse file tree 4 files changed +50
-8
lines changed Original file line number Diff line number Diff line change 1+ import { isBrowser } from './src/utils/browser' ;
2+
13// defined by webpack plugin
24declare 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. ' +
Original file line number Diff line number Diff line change 22
33import { KuzzleAbstractProtocol } from "./Base" ;
44import * as DisconnectionOrigin from "../DisconnectionOrigin" ;
5+ import { getBrowserWindow , isBrowser } from "../../utils/browser" ;
56
67export 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 ;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11let NODE_DEBUG ;
22
3+ const { isBrowser, getBrowserWindow } = require ( "./browser" ) ;
4+
35/* eslint no-undef: 0 */
46
57function 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 ) ;
You can’t perform that action at this time.
0 commit comments