@selfcommunity/utils 0.1.7 → 0.1.8-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/lib/{types → cjs}/index.d.ts +2 -2
  2. package/lib/cjs/index.d.ts.map +1 -0
  3. package/lib/cjs/index.js +35 -50
  4. package/lib/cjs/index.js.map +1 -0
  5. package/lib/{types → cjs}/utils/browser.d.ts +0 -0
  6. package/lib/{types → cjs}/utils/browser.d.ts.map +0 -0
  7. package/lib/cjs/utils/browser.js +62 -78
  8. package/lib/cjs/utils/browser.js.map +1 -0
  9. package/lib/{types → cjs}/utils/logger.d.ts +0 -0
  10. package/lib/{types → cjs}/utils/logger.d.ts.map +0 -0
  11. package/lib/cjs/utils/logger.js +18 -25
  12. package/lib/cjs/utils/logger.js.map +1 -0
  13. package/lib/{types → cjs}/utils/object.d.ts +0 -0
  14. package/lib/{types → cjs}/utils/object.d.ts.map +0 -0
  15. package/lib/cjs/utils/object.js +23 -29
  16. package/lib/cjs/utils/object.js.map +1 -0
  17. package/lib/{types → cjs}/utils/string.d.ts +0 -0
  18. package/lib/{types → cjs}/utils/string.d.ts.map +0 -0
  19. package/lib/cjs/utils/string.js +56 -65
  20. package/lib/cjs/utils/string.js.map +1 -0
  21. package/lib/{types → cjs}/utils/url.d.ts +0 -0
  22. package/lib/{types → cjs}/utils/url.d.ts.map +0 -0
  23. package/lib/cjs/utils/url.js +52 -77
  24. package/lib/cjs/utils/url.js.map +1 -0
  25. package/lib/{types → cjs}/utils/websocket.d.ts +0 -0
  26. package/lib/{types → cjs}/utils/websocket.d.ts.map +0 -0
  27. package/lib/cjs/utils/websocket.js +221 -284
  28. package/lib/cjs/utils/websocket.js.map +1 -0
  29. package/lib/cjs/utils/window.d.ts +5 -0
  30. package/lib/cjs/utils/window.d.ts.map +1 -0
  31. package/lib/cjs/utils/window.js +34 -32
  32. package/lib/cjs/utils/window.js.map +1 -0
  33. package/lib/esm/index.d.ts +12 -0
  34. package/lib/esm/index.d.ts.map +1 -0
  35. package/lib/esm/index.js +12 -51
  36. package/lib/esm/index.js.map +1 -0
  37. package/lib/esm/utils/browser.d.ts +9 -0
  38. package/lib/esm/utils/browser.d.ts.map +1 -0
  39. package/lib/esm/utils/browser.js +61 -81
  40. package/lib/esm/utils/browser.js.map +1 -0
  41. package/lib/esm/utils/logger.d.ts +11 -0
  42. package/lib/esm/utils/logger.d.ts.map +1 -0
  43. package/lib/esm/utils/logger.js +17 -28
  44. package/lib/esm/utils/logger.js.map +1 -0
  45. package/lib/esm/utils/object.d.ts +13 -0
  46. package/lib/esm/utils/object.d.ts.map +1 -0
  47. package/lib/esm/utils/object.js +21 -32
  48. package/lib/esm/utils/object.js.map +1 -0
  49. package/lib/esm/utils/string.d.ts +43 -0
  50. package/lib/esm/utils/string.d.ts.map +1 -0
  51. package/lib/esm/utils/string.js +54 -73
  52. package/lib/esm/utils/string.js.map +1 -0
  53. package/lib/esm/utils/url.d.ts +33 -0
  54. package/lib/esm/utils/url.d.ts.map +1 -0
  55. package/lib/esm/utils/url.js +46 -80
  56. package/lib/esm/utils/url.js.map +1 -0
  57. package/lib/esm/utils/websocket.d.ts +177 -0
  58. package/lib/esm/utils/websocket.d.ts.map +1 -0
  59. package/lib/esm/utils/websocket.js +220 -286
  60. package/lib/esm/utils/websocket.js.map +1 -0
  61. package/lib/esm/utils/window.d.ts +5 -0
  62. package/lib/esm/utils/window.d.ts.map +1 -0
  63. package/lib/esm/utils/window.js +31 -36
  64. package/lib/esm/utils/window.js.map +1 -0
  65. package/lib/umd/utils.js +1 -1
  66. package/lib/umd/utils.js.map +1 -1
  67. package/package.json +5 -7
  68. package/lib/cjs/index.d.js +0 -2
  69. package/lib/esm/index.d.js +0 -2
  70. package/lib/types/index.d.ts.map +0 -1
  71. package/lib/types/tsconfig.build.tsbuildinfo +0 -1
@@ -1,307 +1,241 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.default = void 0;
5
-
6
- /**
7
- * WSClientPropTypes interface
8
- */
9
-
10
1
  /**
11
2
  * WSClient: manage socket connection
12
3
  * @param options
13
4
  * @constructor
14
5
  */
15
- class WSClient {
16
- /**
17
- * Constructor
18
- * @param cfg
19
- */
20
- constructor(cfg) {
21
- this._attempts = 1;
22
- this._heartbeatInterval = null;
23
- this._missedHeartbeats = 0;
24
-
25
- if (!this.isValidOptions(cfg)) {
26
- return;
6
+ export default class WSClient {
7
+ /**
8
+ * Constructor
9
+ * @param cfg
10
+ */
11
+ constructor(cfg) {
12
+ this._attempts = 1;
13
+ this._heartbeatInterval = null;
14
+ this._missedHeartbeats = 0;
15
+ if (!this.isValidOptions(cfg)) {
16
+ return;
17
+ }
18
+ this._cfg = Object.assign({}, { heartbeatMsg: null, debug: false, mustReconnect: true }, cfg);
19
+ this.connect();
20
+ }
21
+ /**
22
+ * Get instance
23
+ */
24
+ static getInstance(cfg) {
25
+ this._instance = this._instance || new WSClient(cfg);
26
+ return this._instance;
27
+ }
28
+ /**
29
+ * Connect
30
+ */
31
+ connect() {
32
+ try {
33
+ if (this._ws && (this.isConnecting() || this.isConnected())) {
34
+ // There is already a connection
35
+ this._cfg.debug && console.info('Websocket is connecting or already connected.');
36
+ return;
37
+ }
38
+ // Callback 'connecting' if exist
39
+ typeof this._cfg.connecting === 'function' && this._cfg.connecting();
40
+ this._cfg.debug && console.info(`Connecting to ${this._cfg.uri} ...`);
41
+ // Open the connection
42
+ this._ws = new WebSocket(this._cfg.uri, this._cfg.protocols);
43
+ this._ws.onopen = this.onOpen.bind(this);
44
+ this._ws.onmessage = this.onMessage.bind(this);
45
+ this._ws.onerror = this.onError.bind(this);
46
+ this._ws.onclose = this.onClose.bind(this);
47
+ this._timer = null;
48
+ }
49
+ catch (err) {
50
+ console.error(err);
51
+ this.tryToReconnect();
52
+ }
53
+ }
54
+ /**
55
+ * Validate options
56
+ * @param cfg
57
+ */
58
+ isValidOptions(cfg) {
59
+ let _error = false;
60
+ if (!cfg) {
61
+ console.error('Invalid WSClient options.');
62
+ return _error;
63
+ }
64
+ if (!cfg.uri) {
65
+ console.error('Invalid WSClient Uri options.');
66
+ _error = true;
67
+ }
68
+ if (cfg && cfg.connecting && !(typeof cfg.connecting === 'function')) {
69
+ console.error('Invalid WSClient connecting options.');
70
+ _error = true;
71
+ }
72
+ if (cfg && cfg.connected && !(typeof cfg.connected === 'function')) {
73
+ console.error('Invalid WSClient connected options.');
74
+ _error = true;
75
+ }
76
+ if (cfg && cfg.receiveMessage && !(typeof cfg.receiveMessage === 'function')) {
77
+ console.error('Invalid WSClient receiveMessage options.');
78
+ _error = true;
79
+ }
80
+ if (cfg && cfg.disconnected && !(typeof cfg.disconnected === 'function')) {
81
+ console.error('Invalid WSClient connecting options.');
82
+ _error = true;
83
+ }
84
+ if (cfg && cfg.heartbeatMsg && !(typeof cfg.heartbeatMsg === 'string')) {
85
+ console.error('Invalid WSClient heartbeatMsg options.');
86
+ _error = true;
87
+ }
88
+ if (cfg && cfg.debug && !(typeof cfg.debug === 'boolean')) {
89
+ console.error('Invalid WSClient debug options.');
90
+ _error = true;
91
+ }
92
+ return !_error;
93
+ }
94
+ /**
95
+ * Try to reconnect if previous connection failed
96
+ * Generate an interval, after that try to reconnect
97
+ */
98
+ tryToReconnect() {
99
+ if (this._cfg.mustReconnect && !this._timer) {
100
+ this._cfg.debug && console.info(`Reconnecting...`);
101
+ let interval = this.generateInteval(this._attempts);
102
+ this._timer = setTimeout(this.reconnect.bind(this), interval);
103
+ }
27
104
  }
28
-
29
- this._cfg = Object.assign({}, {
30
- heartbeatMsg: null,
31
- debug: false,
32
- mustReconnect: true
33
- }, cfg);
34
- this.connect();
35
- }
36
- /**
37
- * Get instance
38
- */
39
-
40
-
41
- static getInstance(cfg) {
42
- this._instance = this._instance || new WSClient(cfg);
43
- return this._instance;
44
- }
45
- /**
46
- * Connect
47
- */
48
-
49
-
50
- connect() {
51
- try {
52
- if (this._ws && (this.isConnecting() || this.isConnected())) {
53
- // There is already a connection
54
- this._cfg.debug && console.info('Websocket is connecting or already connected.');
55
- return;
56
- } // Callback 'connecting' if exist
57
-
58
-
59
- typeof this._cfg.connecting === 'function' && this._cfg.connecting();
60
- this._cfg.debug && console.info(`Connecting to ${this._cfg.uri} ...`); // Open the connection
61
-
62
- this._ws = new WebSocket(this._cfg.uri, this._cfg.protocols);
63
- this._ws.onopen = this.onOpen.bind(this);
64
- this._ws.onmessage = this.onMessage.bind(this);
65
- this._ws.onerror = this.onError.bind(this);
66
- this._ws.onclose = this.onClose.bind(this);
67
- this._timer = null;
68
- } catch (err) {
69
- console.error(err);
70
- this.tryToReconnect();
105
+ /**
106
+ * Reestablish the connection
107
+ * Increase the number of attempts
108
+ */
109
+ reconnect() {
110
+ this._attempts++;
111
+ this.connect();
71
112
  }
72
- }
73
- /**
74
- * Validate options
75
- * @param cfg
76
- */
77
-
78
-
79
- isValidOptions(cfg) {
80
- let _error = false;
81
-
82
- if (!cfg) {
83
- console.error('Invalid WSClient options.');
84
- return _error;
113
+ /**
114
+ * Send heartbeat every 5 seconds
115
+ * If missing more than 3 heartbeats close connection
116
+ */
117
+ sendHeartbeat() {
118
+ try {
119
+ this._missedHeartbeats++;
120
+ if (this._missedHeartbeats > 3)
121
+ throw new Error('Too many missed heartbeats.');
122
+ this._ws.send(this._cfg.heartbeatMsg);
123
+ }
124
+ catch (e) {
125
+ clearInterval(this._heartbeatInterval);
126
+ this._heartbeatInterval = null;
127
+ this._cfg.debug && console.warn(`Closing connection. Reason: ${e.message}`);
128
+ if (!this.isClosing() && !this.isClosed()) {
129
+ this.close();
130
+ }
131
+ }
85
132
  }
86
-
87
- if (!cfg.uri) {
88
- console.error('Invalid WSClient Uri options.');
89
- _error = true;
133
+ /**
134
+ * Established the new connection
135
+ * Reset this._attempts counter
136
+ */
137
+ onOpen() {
138
+ this._cfg.debug && console.info('Connected!');
139
+ this._attempts = 1;
140
+ if (this._cfg.heartbeatMsg && this._heartbeatInterval === null) {
141
+ this._missedHeartbeats = 0;
142
+ this._heartbeatInterval = setInterval(this.sendHeartbeat.bind(this), 5000);
143
+ }
144
+ typeof this._cfg.connected === 'function' && this._cfg.connected();
90
145
  }
91
-
92
- if (cfg && cfg.connecting && !(typeof cfg.connecting === 'function')) {
93
- console.error('Invalid WSClient connecting options.');
94
- _error = true;
146
+ /**
147
+ * Connection closed. Try to reconnect.
148
+ * @param evt
149
+ */
150
+ onClose(evt) {
151
+ this._cfg.debug && console.info('Connection closed!');
152
+ typeof this._cfg.disconnected === 'function' && this._cfg.disconnected(evt);
153
+ this.tryToReconnect();
95
154
  }
96
-
97
- if (cfg && cfg.connected && !(typeof cfg.connected === 'function')) {
98
- console.error('Invalid WSClient connected options.');
99
- _error = true;
155
+ /**
156
+ * An error occured
157
+ * @param evt
158
+ */
159
+ onError(evt) {
160
+ this._cfg.debug && console.error('Websocket connection is broken!');
161
+ this._cfg.debug && console.error(evt);
100
162
  }
101
-
102
- if (cfg && cfg.receiveMessage && !(typeof cfg.receiveMessage === 'function')) {
103
- console.error('Invalid WSClient receiveMessage options.');
104
- _error = true;
163
+ /**
164
+ * A message has arrived.
165
+ * If it is the heartbeat -> reset this._missedHeartbeats
166
+ * If it is data pass data to the callback
167
+ * @param evt
168
+ */
169
+ onMessage(evt) {
170
+ if (this._cfg.heartbeatMsg && evt.data === this._cfg.heartbeatMsg) {
171
+ // reset the counter for missed heartbeats
172
+ this._missedHeartbeats = 0;
173
+ }
174
+ else if (typeof this._cfg.receiveMessage === 'function') {
175
+ return this._cfg.receiveMessage(evt.data);
176
+ }
105
177
  }
106
-
107
- if (cfg && cfg.disconnected && !(typeof cfg.disconnected === 'function')) {
108
- console.error('Invalid WSClient connecting options.');
109
- _error = true;
178
+ /**
179
+ * Generate an interval that is randomly between 0 and 2^k - 1, where k is
180
+ * the number of connection attmpts, with a maximum interval of 30 seconds,
181
+ * so it starts at 0 - 1 seconds and maxes out at 0 - 30 seconds
182
+ */
183
+ generateInteval(k) {
184
+ let maxInterval = (Math.pow(2, k) - 1) * 1000;
185
+ // If the generated interval is more than 30 seconds, truncate it down to 30 seconds.
186
+ if (maxInterval > 30 * 1000) {
187
+ maxInterval = 30 * 1000;
188
+ }
189
+ // generate the interval to a random number between 0 and the maxInterval determined from above
190
+ return Math.random() * maxInterval;
110
191
  }
111
-
112
- if (cfg && cfg.heartbeatMsg && !(typeof cfg.heartbeatMsg === 'string')) {
113
- console.error('Invalid WSClient heartbeatMsg options.');
114
- _error = true;
192
+ /**
193
+ * Send message
194
+ * @param message
195
+ */
196
+ sendMessage(message) {
197
+ this._ws && this._ws.send(message);
115
198
  }
116
-
117
- if (cfg && cfg.debug && !(typeof cfg.debug === 'boolean')) {
118
- console.error('Invalid WSClient debug options.');
119
- _error = true;
199
+ /**
200
+ * Get the ws state
201
+ */
202
+ getState() {
203
+ return this._ws && this._ws.readyState;
120
204
  }
121
-
122
- return !_error;
123
- }
124
- /**
125
- * Try to reconnect if previous connection failed
126
- * Generate an interval, after that try to reconnect
127
- */
128
-
129
-
130
- tryToReconnect() {
131
- if (this._cfg.mustReconnect && !this._timer) {
132
- this._cfg.debug && console.info(`Reconnecting...`);
133
- let interval = this.generateInteval(this._attempts);
134
- this._timer = setTimeout(this.reconnect.bind(this), interval);
205
+ /**
206
+ * Check if ws is in connecting state
207
+ */
208
+ isConnecting() {
209
+ return this._ws && this._ws.readyState === 0;
135
210
  }
136
- }
137
- /**
138
- * Reestablish the connection
139
- * Increase the number of attempts
140
- */
141
-
142
-
143
- reconnect() {
144
- this._attempts++;
145
- this.connect();
146
- }
147
- /**
148
- * Send heartbeat every 5 seconds
149
- * If missing more than 3 heartbeats close connection
150
- */
151
-
152
-
153
- sendHeartbeat() {
154
- try {
155
- this._missedHeartbeats++;
156
- if (this._missedHeartbeats > 3) throw new Error('Too many missed heartbeats.');
157
-
158
- this._ws.send(this._cfg.heartbeatMsg);
159
- } catch (e) {
160
- clearInterval(this._heartbeatInterval);
161
- this._heartbeatInterval = null;
162
- this._cfg.debug && console.warn(`Closing connection. Reason: ${e.message}`);
163
-
164
- if (!this.isClosing() && !this.isClosed()) {
165
- this.close();
166
- }
211
+ /**
212
+ * Check if ws is connected
213
+ */
214
+ isConnected() {
215
+ return this._ws && this._ws.readyState === 1;
167
216
  }
168
- }
169
- /**
170
- * Established the new connection
171
- * Reset this._attempts counter
172
- */
173
-
174
-
175
- onOpen() {
176
- this._cfg.debug && console.info('Connected!');
177
- this._attempts = 1;
178
-
179
- if (this._cfg.heartbeatMsg && this._heartbeatInterval === null) {
180
- this._missedHeartbeats = 0;
181
- this._heartbeatInterval = setInterval(this.sendHeartbeat.bind(this), 5000);
217
+ /**
218
+ * Check if ws is in closing state
219
+ */
220
+ isClosing() {
221
+ return this._ws && this._ws.readyState === 2;
182
222
  }
183
-
184
- typeof this._cfg.connected === 'function' && this._cfg.connected();
185
- }
186
- /**
187
- * Connection closed. Try to reconnect.
188
- * @param evt
189
- */
190
-
191
-
192
- onClose(evt) {
193
- this._cfg.debug && console.info('Connection closed!');
194
- typeof this._cfg.disconnected === 'function' && this._cfg.disconnected(evt);
195
- this.tryToReconnect();
196
- }
197
- /**
198
- * An error occured
199
- * @param evt
200
- */
201
-
202
-
203
- onError(evt) {
204
- this._cfg.debug && console.error('Websocket connection is broken!');
205
- this._cfg.debug && console.error(evt);
206
- }
207
- /**
208
- * A message has arrived.
209
- * If it is the heartbeat -> reset this._missedHeartbeats
210
- * If it is data pass data to the callback
211
- * @param evt
212
- */
213
-
214
-
215
- onMessage(evt) {
216
- if (this._cfg.heartbeatMsg && evt.data === this._cfg.heartbeatMsg) {
217
- // reset the counter for missed heartbeats
218
- this._missedHeartbeats = 0;
219
- } else if (typeof this._cfg.receiveMessage === 'function') {
220
- return this._cfg.receiveMessage(evt.data);
223
+ /**
224
+ * Check if ws is closed
225
+ */
226
+ isClosed() {
227
+ return this._ws && this._ws.readyState === 3;
221
228
  }
222
- }
223
- /**
224
- * Generate an interval that is randomly between 0 and 2^k - 1, where k is
225
- * the number of connection attmpts, with a maximum interval of 30 seconds,
226
- * so it starts at 0 - 1 seconds and maxes out at 0 - 30 seconds
227
- */
228
-
229
-
230
- generateInteval(k) {
231
- let maxInterval = (Math.pow(2, k) - 1) * 1000; // If the generated interval is more than 30 seconds, truncate it down to 30 seconds.
232
-
233
- if (maxInterval > 30 * 1000) {
234
- maxInterval = 30 * 1000;
235
- } // generate the interval to a random number between 0 and the maxInterval determined from above
236
-
237
-
238
- return Math.random() * maxInterval;
239
- }
240
- /**
241
- * Send message
242
- * @param message
243
- */
244
-
245
-
246
- sendMessage(message) {
247
- this._ws && this._ws.send(message);
248
- }
249
- /**
250
- * Get the ws state
251
- */
252
-
253
-
254
- getState() {
255
- return this._ws && this._ws.readyState;
256
- }
257
- /**
258
- * Check if ws is in connecting state
259
- */
260
-
261
-
262
- isConnecting() {
263
- return this._ws && this._ws.readyState === 0;
264
- }
265
- /**
266
- * Check if ws is connected
267
- */
268
-
269
-
270
- isConnected() {
271
- return this._ws && this._ws.readyState === 1;
272
- }
273
- /**
274
- * Check if ws is in closing state
275
- */
276
-
277
-
278
- isClosing() {
279
- return this._ws && this._ws.readyState === 2;
280
- }
281
- /**
282
- * Check if ws is closed
283
- */
284
-
285
-
286
- isClosed() {
287
- return this._ws && this._ws.readyState === 3;
288
- }
289
- /**
290
- * Close the connection
291
- */
292
-
293
-
294
- close() {
295
- clearInterval(this._heartbeatInterval);
296
- this._cfg.mustReconnect = false;
297
-
298
- if (!this.isClosing() || !this.isClosed()) {
299
- this._ws.close();
300
-
301
- this._cfg.debug && console.error('Websocket closed.');
229
+ /**
230
+ * Close the connection
231
+ */
232
+ close() {
233
+ clearInterval(this._heartbeatInterval);
234
+ this._cfg.mustReconnect = false;
235
+ if (!this.isClosing() || !this.isClosed()) {
236
+ this._ws.close();
237
+ this._cfg.debug && console.error('Websocket closed.');
238
+ }
302
239
  }
303
- }
304
-
305
240
  }
306
-
307
- exports.default = WSClient;
241
+ //# sourceMappingURL=websocket.js.map
@@ -0,0 +1 @@
1
+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B,CAAC,CAAC;SACvD;IACH,CAAC;CACF"}
@@ -0,0 +1,5 @@
1
+ export declare function getWindowWidth(): number;
2
+ export declare function getWindowHeight(): number;
3
+ export declare function getHighestSafeWindowContext(self?: Window & typeof globalThis): any;
4
+ export declare function isClientSideRendering(): boolean;
5
+ //# sourceMappingURL=window.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"window.d.ts","sourceRoot":"","sources":["../../../src/utils/window.ts"],"names":[],"mappings":"AAAA,wBAAgB,cAAc,WAE7B;AAED,wBAAgB,eAAe,WAE9B;AAYD,wBAAgB,2BAA2B,CAAC,IAAI,6BAAqB,OAiBpE;AAED,wBAAgB,qBAAqB,YAEpC"}
@@ -1,41 +1,36 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.getHighestSafeWindowContext = getHighestSafeWindowContext;
5
- exports.getWindowHeight = getWindowHeight;
6
- exports.getWindowWidth = getWindowWidth;
7
-
8
- function getWindowWidth() {
9
- return typeof global.window !== 'undefined' ? global.window.innerWidth : 0;
1
+ export function getWindowWidth() {
2
+ return typeof global.window !== 'undefined' ? global.window.innerWidth : 0;
10
3
  }
11
-
12
- function getWindowHeight() {
13
- return typeof global.window !== 'undefined' ? global.window.innerHeight : 0;
4
+ export function getWindowHeight() {
5
+ return typeof global.window !== 'undefined' ? global.window.innerHeight : 0;
14
6
  }
15
-
16
7
  const isCrossOriginFrame = () => {
17
- try {
18
- return global.window.location.hostname !== global.window.parent.location.hostname;
19
- } catch (e) {
20
- return true;
21
- }
22
- }; // Get the highest window context that isn't cross-origin
8
+ try {
9
+ return global.window.location.hostname !== global.window.parent.location.hostname;
10
+ }
11
+ catch (e) {
12
+ return true;
13
+ }
14
+ };
15
+ // Get the highest window context that isn't cross-origin
23
16
  // (When in an iframe)
24
-
25
-
26
- function getHighestSafeWindowContext(self = global.window.self) {
27
- // If we reached the top level, return self
28
- if (self === global.window.top) {
17
+ export function getHighestSafeWindowContext(self = global.window.self) {
18
+ // If we reached the top level, return self
19
+ if (self === global.window.top) {
20
+ return self;
21
+ }
22
+ // If parent is the same origin, we can move up one context
23
+ // Reference: https://stackoverflow.com/a/21965342/1601953
24
+ if (!isCrossOriginFrame()) {
25
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
26
+ // @ts-ignore
27
+ return getHighestSafeWindowContext(self.parent);
28
+ }
29
+ // If a different origin, we consider the current level
30
+ // as the top reachable one
29
31
  return self;
30
- } // If parent is the same origin, we can move up one context
31
- // Reference: https://stackoverflow.com/a/21965342/1601953
32
-
33
-
34
- if (!isCrossOriginFrame()) {
35
- return getHighestSafeWindowContext(self.parent);
36
- } // If a different origin, we consider the current level
37
- // as the top reachable one
38
-
39
-
40
- return self;
41
- }
32
+ }
33
+ export function isClientSideRendering() {
34
+ return typeof window !== 'undefined';
35
+ }
36
+ //# sourceMappingURL=window.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"window.js","sourceRoot":"","sources":["../../../src/utils/window.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,cAAc;IAC5B,OAAO,OAAO,MAAM,CAAC,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;AAC7E,CAAC;AAED,MAAM,UAAU,eAAe;IAC7B,OAAO,OAAO,MAAM,CAAC,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;AAC9E,CAAC;AAED,MAAM,kBAAkB,GAAG,GAAG,EAAE;IAC9B,IAAI;QACF,OAAO,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,KAAK,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC;KACnF;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,IAAI,CAAC;KACb;AACH,CAAC,CAAC;AAEF,yDAAyD;AACzD,sBAAsB;AACtB,MAAM,UAAU,2BAA2B,CAAC,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI;IACnE,2CAA2C;IAC3C,IAAI,IAAI,KAAK,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE;QAC9B,OAAO,IAAI,CAAC;KACb;IAED,2DAA2D;IAC3D,0DAA0D;IAC1D,IAAI,CAAC,kBAAkB,EAAE,EAAE;QACzB,4DAA4D;QAC5D,aAAa;QACb,OAAO,2BAA2B,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;KACjD;IAED,uDAAuD;IACvD,2BAA2B;IAC3B,OAAO,IAAI,CAAC;AACd,CAAC;AAED,MAAM,UAAU,qBAAqB;IACnC,OAAO,OAAO,MAAM,KAAK,WAAW,CAAC;AACvC,CAAC"}