@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,244 @@
1
1
  "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.default = void 0;
5
-
6
- /**
7
- * WSClientPropTypes interface
8
- */
9
-
2
+ Object.defineProperty(exports, "__esModule", { value: true });
10
3
  /**
11
4
  * WSClient: manage socket connection
12
5
  * @param options
13
6
  * @constructor
14
7
  */
15
8
  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;
9
+ /**
10
+ * Constructor
11
+ * @param cfg
12
+ */
13
+ constructor(cfg) {
14
+ this._attempts = 1;
15
+ this._heartbeatInterval = null;
16
+ this._missedHeartbeats = 0;
17
+ if (!this.isValidOptions(cfg)) {
18
+ return;
19
+ }
20
+ this._cfg = Object.assign({}, { heartbeatMsg: null, debug: false, mustReconnect: true }, cfg);
21
+ this.connect();
22
+ }
23
+ /**
24
+ * Get instance
25
+ */
26
+ static getInstance(cfg) {
27
+ this._instance = this._instance || new WSClient(cfg);
28
+ return this._instance;
29
+ }
30
+ /**
31
+ * Connect
32
+ */
33
+ connect() {
34
+ try {
35
+ if (this._ws && (this.isConnecting() || this.isConnected())) {
36
+ // There is already a connection
37
+ this._cfg.debug && console.info('Websocket is connecting or already connected.');
38
+ return;
39
+ }
40
+ // Callback 'connecting' if exist
41
+ typeof this._cfg.connecting === 'function' && this._cfg.connecting();
42
+ this._cfg.debug && console.info(`Connecting to ${this._cfg.uri} ...`);
43
+ // Open the connection
44
+ this._ws = new WebSocket(this._cfg.uri, this._cfg.protocols);
45
+ this._ws.onopen = this.onOpen.bind(this);
46
+ this._ws.onmessage = this.onMessage.bind(this);
47
+ this._ws.onerror = this.onError.bind(this);
48
+ this._ws.onclose = this.onClose.bind(this);
49
+ this._timer = null;
50
+ }
51
+ catch (err) {
52
+ console.error(err);
53
+ this.tryToReconnect();
54
+ }
55
+ }
56
+ /**
57
+ * Validate options
58
+ * @param cfg
59
+ */
60
+ isValidOptions(cfg) {
61
+ let _error = false;
62
+ if (!cfg) {
63
+ console.error('Invalid WSClient options.');
64
+ return _error;
65
+ }
66
+ if (!cfg.uri) {
67
+ console.error('Invalid WSClient Uri options.');
68
+ _error = true;
69
+ }
70
+ if (cfg && cfg.connecting && !(typeof cfg.connecting === 'function')) {
71
+ console.error('Invalid WSClient connecting options.');
72
+ _error = true;
73
+ }
74
+ if (cfg && cfg.connected && !(typeof cfg.connected === 'function')) {
75
+ console.error('Invalid WSClient connected options.');
76
+ _error = true;
77
+ }
78
+ if (cfg && cfg.receiveMessage && !(typeof cfg.receiveMessage === 'function')) {
79
+ console.error('Invalid WSClient receiveMessage options.');
80
+ _error = true;
81
+ }
82
+ if (cfg && cfg.disconnected && !(typeof cfg.disconnected === 'function')) {
83
+ console.error('Invalid WSClient connecting options.');
84
+ _error = true;
85
+ }
86
+ if (cfg && cfg.heartbeatMsg && !(typeof cfg.heartbeatMsg === 'string')) {
87
+ console.error('Invalid WSClient heartbeatMsg options.');
88
+ _error = true;
89
+ }
90
+ if (cfg && cfg.debug && !(typeof cfg.debug === 'boolean')) {
91
+ console.error('Invalid WSClient debug options.');
92
+ _error = true;
93
+ }
94
+ return !_error;
95
+ }
96
+ /**
97
+ * Try to reconnect if previous connection failed
98
+ * Generate an interval, after that try to reconnect
99
+ */
100
+ tryToReconnect() {
101
+ if (this._cfg.mustReconnect && !this._timer) {
102
+ this._cfg.debug && console.info(`Reconnecting...`);
103
+ let interval = this.generateInteval(this._attempts);
104
+ this._timer = setTimeout(this.reconnect.bind(this), interval);
105
+ }
27
106
  }
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();
107
+ /**
108
+ * Reestablish the connection
109
+ * Increase the number of attempts
110
+ */
111
+ reconnect() {
112
+ this._attempts++;
113
+ this.connect();
71
114
  }
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;
115
+ /**
116
+ * Send heartbeat every 5 seconds
117
+ * If missing more than 3 heartbeats close connection
118
+ */
119
+ sendHeartbeat() {
120
+ try {
121
+ this._missedHeartbeats++;
122
+ if (this._missedHeartbeats > 3)
123
+ throw new Error('Too many missed heartbeats.');
124
+ this._ws.send(this._cfg.heartbeatMsg);
125
+ }
126
+ catch (e) {
127
+ clearInterval(this._heartbeatInterval);
128
+ this._heartbeatInterval = null;
129
+ this._cfg.debug && console.warn(`Closing connection. Reason: ${e.message}`);
130
+ if (!this.isClosing() && !this.isClosed()) {
131
+ this.close();
132
+ }
133
+ }
85
134
  }
86
-
87
- if (!cfg.uri) {
88
- console.error('Invalid WSClient Uri options.');
89
- _error = true;
135
+ /**
136
+ * Established the new connection
137
+ * Reset this._attempts counter
138
+ */
139
+ onOpen() {
140
+ this._cfg.debug && console.info('Connected!');
141
+ this._attempts = 1;
142
+ if (this._cfg.heartbeatMsg && this._heartbeatInterval === null) {
143
+ this._missedHeartbeats = 0;
144
+ this._heartbeatInterval = setInterval(this.sendHeartbeat.bind(this), 5000);
145
+ }
146
+ typeof this._cfg.connected === 'function' && this._cfg.connected();
90
147
  }
91
-
92
- if (cfg && cfg.connecting && !(typeof cfg.connecting === 'function')) {
93
- console.error('Invalid WSClient connecting options.');
94
- _error = true;
148
+ /**
149
+ * Connection closed. Try to reconnect.
150
+ * @param evt
151
+ */
152
+ onClose(evt) {
153
+ this._cfg.debug && console.info('Connection closed!');
154
+ typeof this._cfg.disconnected === 'function' && this._cfg.disconnected(evt);
155
+ this.tryToReconnect();
95
156
  }
96
-
97
- if (cfg && cfg.connected && !(typeof cfg.connected === 'function')) {
98
- console.error('Invalid WSClient connected options.');
99
- _error = true;
157
+ /**
158
+ * An error occured
159
+ * @param evt
160
+ */
161
+ onError(evt) {
162
+ this._cfg.debug && console.error('Websocket connection is broken!');
163
+ this._cfg.debug && console.error(evt);
100
164
  }
101
-
102
- if (cfg && cfg.receiveMessage && !(typeof cfg.receiveMessage === 'function')) {
103
- console.error('Invalid WSClient receiveMessage options.');
104
- _error = true;
165
+ /**
166
+ * A message has arrived.
167
+ * If it is the heartbeat -> reset this._missedHeartbeats
168
+ * If it is data pass data to the callback
169
+ * @param evt
170
+ */
171
+ onMessage(evt) {
172
+ if (this._cfg.heartbeatMsg && evt.data === this._cfg.heartbeatMsg) {
173
+ // reset the counter for missed heartbeats
174
+ this._missedHeartbeats = 0;
175
+ }
176
+ else if (typeof this._cfg.receiveMessage === 'function') {
177
+ return this._cfg.receiveMessage(evt.data);
178
+ }
105
179
  }
106
-
107
- if (cfg && cfg.disconnected && !(typeof cfg.disconnected === 'function')) {
108
- console.error('Invalid WSClient connecting options.');
109
- _error = true;
180
+ /**
181
+ * Generate an interval that is randomly between 0 and 2^k - 1, where k is
182
+ * the number of connection attmpts, with a maximum interval of 30 seconds,
183
+ * so it starts at 0 - 1 seconds and maxes out at 0 - 30 seconds
184
+ */
185
+ generateInteval(k) {
186
+ let maxInterval = (Math.pow(2, k) - 1) * 1000;
187
+ // If the generated interval is more than 30 seconds, truncate it down to 30 seconds.
188
+ if (maxInterval > 30 * 1000) {
189
+ maxInterval = 30 * 1000;
190
+ }
191
+ // generate the interval to a random number between 0 and the maxInterval determined from above
192
+ return Math.random() * maxInterval;
110
193
  }
111
-
112
- if (cfg && cfg.heartbeatMsg && !(typeof cfg.heartbeatMsg === 'string')) {
113
- console.error('Invalid WSClient heartbeatMsg options.');
114
- _error = true;
194
+ /**
195
+ * Send message
196
+ * @param message
197
+ */
198
+ sendMessage(message) {
199
+ this._ws && this._ws.send(message);
115
200
  }
116
-
117
- if (cfg && cfg.debug && !(typeof cfg.debug === 'boolean')) {
118
- console.error('Invalid WSClient debug options.');
119
- _error = true;
201
+ /**
202
+ * Get the ws state
203
+ */
204
+ getState() {
205
+ return this._ws && this._ws.readyState;
120
206
  }
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);
207
+ /**
208
+ * Check if ws is in connecting state
209
+ */
210
+ isConnecting() {
211
+ return this._ws && this._ws.readyState === 0;
135
212
  }
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
- }
213
+ /**
214
+ * Check if ws is connected
215
+ */
216
+ isConnected() {
217
+ return this._ws && this._ws.readyState === 1;
167
218
  }
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);
219
+ /**
220
+ * Check if ws is in closing state
221
+ */
222
+ isClosing() {
223
+ return this._ws && this._ws.readyState === 2;
182
224
  }
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);
225
+ /**
226
+ * Check if ws is closed
227
+ */
228
+ isClosed() {
229
+ return this._ws && this._ws.readyState === 3;
221
230
  }
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.');
231
+ /**
232
+ * Close the connection
233
+ */
234
+ close() {
235
+ clearInterval(this._heartbeatInterval);
236
+ this._cfg.mustReconnect = false;
237
+ if (!this.isClosing() || !this.isClosed()) {
238
+ this._ws.close();
239
+ this._cfg.debug && console.error('Websocket closed.');
240
+ }
302
241
  }
303
- }
304
-
305
242
  }
306
-
307
- exports.default = WSClient;
243
+ exports.default = WSClient;
244
+ //#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,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAC;SACvD;IACH,CAAC;CACF;AA/PD,2BA+PC"}
@@ -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,43 @@
1
1
  "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.getHighestSafeWindowContext = getHighestSafeWindowContext;
5
- exports.getWindowHeight = getWindowHeight;
6
- exports.getWindowWidth = getWindowWidth;
7
-
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isClientSideRendering = exports.getHighestSafeWindowContext = exports.getWindowHeight = exports.getWindowWidth = void 0;
8
4
  function getWindowWidth() {
9
- return typeof global.window !== 'undefined' ? global.window.innerWidth : 0;
5
+ return typeof global.window !== 'undefined' ? global.window.innerWidth : 0;
10
6
  }
11
-
7
+ exports.getWindowWidth = getWindowWidth;
12
8
  function getWindowHeight() {
13
- return typeof global.window !== 'undefined' ? global.window.innerHeight : 0;
9
+ return typeof global.window !== 'undefined' ? global.window.innerHeight : 0;
14
10
  }
15
-
11
+ exports.getWindowHeight = getWindowHeight;
16
12
  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
13
+ try {
14
+ return global.window.location.hostname !== global.window.parent.location.hostname;
15
+ }
16
+ catch (e) {
17
+ return true;
18
+ }
19
+ };
20
+ // Get the highest window context that isn't cross-origin
23
21
  // (When in an iframe)
24
-
25
-
26
22
  function getHighestSafeWindowContext(self = global.window.self) {
27
- // If we reached the top level, return self
28
- if (self === global.window.top) {
23
+ // If we reached the top level, return self
24
+ if (self === global.window.top) {
25
+ return self;
26
+ }
27
+ // If parent is the same origin, we can move up one context
28
+ // Reference: https://stackoverflow.com/a/21965342/1601953
29
+ if (!isCrossOriginFrame()) {
30
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
31
+ // @ts-ignore
32
+ return getHighestSafeWindowContext(self.parent);
33
+ }
34
+ // If a different origin, we consider the current level
35
+ // as the top reachable one
29
36
  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
- }
37
+ }
38
+ exports.getHighestSafeWindowContext = getHighestSafeWindowContext;
39
+ function isClientSideRendering() {
40
+ return typeof window !== 'undefined';
41
+ }
42
+ exports.isClientSideRendering = isClientSideRendering;
43
+ //# sourceMappingURL=window.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"window.js","sourceRoot":"","sources":["../../../src/utils/window.ts"],"names":[],"mappings":";;;AAAA,SAAgB,cAAc;IAC5B,OAAO,OAAO,MAAM,CAAC,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;AAC7E,CAAC;AAFD,wCAEC;AAED,SAAgB,eAAe;IAC7B,OAAO,OAAO,MAAM,CAAC,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;AAC9E,CAAC;AAFD,0CAEC;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,SAAgB,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;AAjBD,kEAiBC;AAED,SAAgB,qBAAqB;IACnC,OAAO,OAAO,MAAM,KAAK,WAAW,CAAC;AACvC,CAAC;AAFD,sDAEC"}
@@ -0,0 +1,12 @@
1
+ import { capitalize, isString, stripHtml, camelCase, copyTextToClipboard, fallbackCopyTextToClipboard, random } from './utils/string';
2
+ import { isValidUrl, isValidUrls, urlReplacer, getDomain, appendURLSearchParams, urlB64ToUint8Array } from './utils/url';
3
+ import { getHighestSafeWindowContext, getWindowWidth, getWindowHeight, isClientSideRendering } from './utils/window';
4
+ import { mergeDeep, isObject } from './utils/object';
5
+ import { loadVersionBrowser } from './utils/browser';
6
+ import { Logger } from './utils/logger';
7
+ import WSClient, { WSClientType, WSClientPropTypes } from './utils/websocket';
8
+ /**
9
+ * Export all utilities
10
+ */
11
+ export { capitalize, isString, stripHtml, camelCase, copyTextToClipboard, fallbackCopyTextToClipboard, random, isValidUrl, isValidUrls, urlReplacer, getDomain, appendURLSearchParams, urlB64ToUint8Array, getHighestSafeWindowContext, getWindowWidth, getWindowHeight, isClientSideRendering, Logger, mergeDeep, isObject, WSClient, WSClientType, WSClientPropTypes, loadVersionBrowser };
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,mBAAmB,EAAE,2BAA2B,EAAE,MAAM,EAAC,MAAM,gBAAgB,CAAC;AACpI,OAAO,EAAC,UAAU,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,qBAAqB,EAAE,kBAAkB,EAAC,MAAM,aAAa,CAAC;AACvH,OAAO,EAAC,2BAA2B,EAAE,cAAc,EAAE,eAAe,EAAE,qBAAqB,EAAC,MAAM,gBAAgB,CAAC;AACnH,OAAO,EAAC,SAAS,EAAE,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAC,kBAAkB,EAAC,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAC,MAAM,EAAC,MAAM,gBAAgB,CAAC;AACtC,OAAO,QAAQ,EAAE,EAAC,YAAY,EAAE,iBAAiB,EAAC,MAAM,mBAAmB,CAAC;AAE5E;;GAEG;AACH,OAAO,EACL,UAAU,EACV,QAAQ,EACR,SAAS,EACT,SAAS,EACT,mBAAmB,EACnB,2BAA2B,EAC3B,MAAM,EACN,UAAU,EACV,WAAW,EACX,WAAW,EACX,SAAS,EACT,qBAAqB,EACrB,kBAAkB,EAClB,2BAA2B,EAC3B,cAAc,EACd,eAAe,EACf,qBAAqB,EACrB,MAAM,EACN,SAAS,EACT,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,iBAAiB,EACjB,kBAAkB,EACnB,CAAC"}