@selfcommunity/utils 0.1.4-alpha.2 → 0.1.5-alpha.1
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.
- package/lib/cjs/index.js +35 -1
- package/lib/cjs/utils/browser.js +90 -0
- package/lib/cjs/utils/logger.js +32 -0
- package/lib/cjs/utils/object.js +44 -0
- package/lib/cjs/utils/string.js +57 -0
- package/lib/cjs/utils/url.js +44 -2
- package/lib/cjs/utils/websocket.js +307 -0
- package/lib/cjs/utils/window.js +41 -0
- package/lib/esm/index.d.ts +8 -3
- package/lib/esm/index.d.ts.map +1 -1
- package/lib/esm/index.js +35 -1
- package/lib/esm/utils/browser.d.ts +9 -0
- package/lib/esm/utils/browser.d.ts.map +1 -0
- package/lib/esm/utils/browser.js +90 -0
- package/lib/esm/utils/logger.d.ts +11 -0
- package/lib/esm/utils/logger.d.ts.map +1 -0
- package/lib/esm/utils/logger.js +32 -0
- package/lib/esm/utils/object.d.ts +13 -0
- package/lib/esm/utils/object.d.ts.map +1 -0
- package/lib/esm/utils/object.js +44 -0
- package/lib/esm/utils/string.d.ts +22 -0
- package/lib/esm/utils/string.d.ts.map +1 -1
- package/lib/esm/utils/string.js +57 -0
- package/lib/esm/utils/url.d.ts +11 -0
- package/lib/esm/utils/url.d.ts.map +1 -1
- package/lib/esm/utils/url.js +44 -2
- package/lib/esm/utils/websocket.d.ts +177 -0
- package/lib/esm/utils/websocket.d.ts.map +1 -0
- package/lib/esm/utils/websocket.js +307 -0
- package/lib/esm/utils/window.js +41 -0
- package/lib/umd/utils.js +1 -1
- package/lib/umd/utils.js.map +1 -1
- package/package.json +2 -2
package/lib/cjs/index.js
CHANGED
|
@@ -8,10 +8,44 @@ exports.capitalize = _string.capitalize;
|
|
|
8
8
|
exports.isString = _string.isString;
|
|
9
9
|
exports.stripHtml = _string.stripHtml;
|
|
10
10
|
exports.camelCase = _string.camelCase;
|
|
11
|
+
exports.copyTextToClipboard = _string.copyTextToClipboard;
|
|
12
|
+
exports.fallbackCopyTextToClipboard = _string.fallbackCopyTextToClipboard;
|
|
13
|
+
exports.random = _string.random;
|
|
11
14
|
|
|
12
15
|
var _url = require("./utils/url");
|
|
13
16
|
|
|
14
17
|
exports.isValidUrl = _url.isValidUrl;
|
|
15
18
|
exports.isValidUrls = _url.isValidUrls;
|
|
16
19
|
exports.urlReplacer = _url.urlReplacer;
|
|
17
|
-
exports.getDomain = _url.getDomain;
|
|
20
|
+
exports.getDomain = _url.getDomain;
|
|
21
|
+
exports.appendURLSearchParams = _url.appendURLSearchParams;
|
|
22
|
+
exports.urlB64ToUint8Array = _url.urlB64ToUint8Array;
|
|
23
|
+
|
|
24
|
+
var _window = require("./utils/window");
|
|
25
|
+
|
|
26
|
+
exports.getHighestSafeWindowContext = _window.getHighestSafeWindowContext;
|
|
27
|
+
exports.getWindowWidth = _window.getWindowWidth;
|
|
28
|
+
exports.getWindowHeight = _window.getWindowHeight;
|
|
29
|
+
|
|
30
|
+
var _object = require("./utils/object");
|
|
31
|
+
|
|
32
|
+
exports.mergeDeep = _object.mergeDeep;
|
|
33
|
+
exports.isObject = _object.isObject;
|
|
34
|
+
|
|
35
|
+
var _browser = require("./utils/browser");
|
|
36
|
+
|
|
37
|
+
exports.loadVersionBrowser = _browser.loadVersionBrowser;
|
|
38
|
+
|
|
39
|
+
var _logger = require("./utils/logger");
|
|
40
|
+
|
|
41
|
+
exports.Logger = _logger.Logger;
|
|
42
|
+
|
|
43
|
+
var _websocket = _interopRequireWildcard(require("./utils/websocket"));
|
|
44
|
+
|
|
45
|
+
exports.WSClient = _websocket.default;
|
|
46
|
+
exports.WSClientType = _websocket.WSClientType;
|
|
47
|
+
exports.WSClientPropTypes = _websocket.WSClientPropTypes;
|
|
48
|
+
|
|
49
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
50
|
+
|
|
51
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.loadVersionBrowser = void 0;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Get browser version to track on backend
|
|
8
|
+
* @return {{name: string, version: null}|{name: string, version: string}|{name: *, version: *}|{name: string, version}}
|
|
9
|
+
*/
|
|
10
|
+
const loadVersionBrowser = () => {
|
|
11
|
+
if ('userAgentData' in navigator) {
|
|
12
|
+
// navigator.userAgentData is not available in
|
|
13
|
+
// Firefox and Safari
|
|
14
|
+
const uaData = navigator['userAgentData']; // Outputs of navigator.userAgentData.brands[n].brand are e.g.
|
|
15
|
+
// Chrome: 'Google Chrome'
|
|
16
|
+
// Edge: 'Microsoft Edge'
|
|
17
|
+
// Opera: 'Opera'
|
|
18
|
+
|
|
19
|
+
let browsername;
|
|
20
|
+
let browserversion;
|
|
21
|
+
let chromeVersion = null;
|
|
22
|
+
|
|
23
|
+
for (let i = 0; i < uaData['brands'].length; i++) {
|
|
24
|
+
const brand = uaData['brands'][i].brand;
|
|
25
|
+
browserversion = uaData['brands'][i].version;
|
|
26
|
+
|
|
27
|
+
if (brand.match(/opera|chrome|edge|safari|firefox|msie|trident/i) !== null) {
|
|
28
|
+
// If we have a chrome match, save the match, but try to find another match
|
|
29
|
+
// E.g. Edge can also produce a false Chrome match.
|
|
30
|
+
if (brand.match(/chrome/i) !== null) {
|
|
31
|
+
chromeVersion = browserversion;
|
|
32
|
+
} // If this is not a chrome match return immediately
|
|
33
|
+
else {
|
|
34
|
+
browsername = brand.substr(brand.indexOf(' ') + 1);
|
|
35
|
+
return {
|
|
36
|
+
name: browsername,
|
|
37
|
+
version: browserversion
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
} // No non-Chrome match was found. If we have a chrome match, return it.
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
if (chromeVersion !== null) {
|
|
45
|
+
return {
|
|
46
|
+
name: 'chrome',
|
|
47
|
+
version: chromeVersion
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
} // If no userAgentData is not present, or if no match via userAgentData was found,
|
|
51
|
+
// try to extract the browser name and version from userAgent
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
const userAgent = navigator.userAgent;
|
|
55
|
+
let ua = userAgent;
|
|
56
|
+
let tem;
|
|
57
|
+
let M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
|
|
58
|
+
|
|
59
|
+
if (/trident/i.test(M[1])) {
|
|
60
|
+
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
|
|
61
|
+
return {
|
|
62
|
+
name: 'IE',
|
|
63
|
+
version: tem[1] || ''
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (M[1] === 'Chrome') {
|
|
68
|
+
tem = ua.match(/\bOPR\/(\d+)/);
|
|
69
|
+
|
|
70
|
+
if (tem != null) {
|
|
71
|
+
return {
|
|
72
|
+
name: 'Opera',
|
|
73
|
+
version: tem[1]
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?']; // eslint-disable-next-line @typescript-eslint/prefer-regexp-exec
|
|
79
|
+
|
|
80
|
+
if ((tem = ua.match(/version\/(\d+)/i)) != null) {
|
|
81
|
+
M.splice(1, 1, tem[1]);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
name: M[0],
|
|
86
|
+
version: M[1]
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
exports.loadVersionBrowser = loadVersionBrowser;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.Logger = void 0;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Emit styled message
|
|
8
|
+
*/
|
|
9
|
+
class Logger {
|
|
10
|
+
static info(scope, message) {
|
|
11
|
+
console.info(`%c[${scope}]`, 'color:#008080', ` ${message}`);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static warn(scope, message) {
|
|
15
|
+
console.warn(`%c[${scope}]`, 'color:#008080', ` ${message}`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static error(scope, message) {
|
|
19
|
+
console.error(`%c[${scope}]`, 'color:#008080', ` ${message}`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static log(scope, message) {
|
|
23
|
+
console.log(`%c[${scope}]`, 'color:#008080', ` ${message}`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static debug(scope, message) {
|
|
27
|
+
console.debug(`%c[${scope}]`, 'color:#008080', ` ${message}`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
exports.Logger = Logger;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.isObject = isObject;
|
|
5
|
+
exports.mergeDeep = mergeDeep;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Check if v is an object
|
|
9
|
+
* @param v
|
|
10
|
+
*/
|
|
11
|
+
function isObject(v) {
|
|
12
|
+
return typeof v === 'object' && !Array.isArray(v) && v !== null;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Perfrom deep merge of two objects (not a shallow merge)
|
|
16
|
+
* @param target
|
|
17
|
+
* @param source
|
|
18
|
+
* @return {*}
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
function mergeDeep(target, source) {
|
|
23
|
+
let output = Object.assign({}, target);
|
|
24
|
+
|
|
25
|
+
if (isObject(target) && isObject(source)) {
|
|
26
|
+
Object.keys(source).forEach(key => {
|
|
27
|
+
if (isObject(source[key])) {
|
|
28
|
+
if (!(key in target)) {
|
|
29
|
+
Object.assign(output, {
|
|
30
|
+
[key]: source[key]
|
|
31
|
+
});
|
|
32
|
+
} else {
|
|
33
|
+
output[key] = mergeDeep(target[key], source[key]);
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
Object.assign(output, {
|
|
37
|
+
[key]: source[key]
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return output;
|
|
44
|
+
}
|
package/lib/cjs/utils/string.js
CHANGED
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.camelCase = camelCase;
|
|
5
5
|
exports.capitalize = capitalize;
|
|
6
|
+
exports.copyTextToClipboard = copyTextToClipboard;
|
|
7
|
+
exports.fallbackCopyTextToClipboard = fallbackCopyTextToClipboard;
|
|
6
8
|
exports.isString = isString;
|
|
9
|
+
exports.random = random;
|
|
7
10
|
exports.stripHtml = stripHtml;
|
|
8
11
|
|
|
9
12
|
/**
|
|
@@ -54,4 +57,58 @@ function camelCase(str) {
|
|
|
54
57
|
|
|
55
58
|
function stripHtml(str) {
|
|
56
59
|
return str.replace(/<[^>]*>?/gm, '').trim();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function random() {
|
|
63
|
+
return (Math.random() + 1).toString(36).substring(7);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Fallback if navigator.clipboard doensn't exist
|
|
67
|
+
* @param text
|
|
68
|
+
* @returns {Promise<void>}
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
function fallbackCopyTextToClipboard(text) {
|
|
73
|
+
let textArea = document.createElement('textarea');
|
|
74
|
+
textArea.value = text; // Avoid scrolling to bottom
|
|
75
|
+
|
|
76
|
+
textArea.style.top = '0';
|
|
77
|
+
textArea.style.left = '0';
|
|
78
|
+
textArea.style.position = 'fixed';
|
|
79
|
+
document.body.appendChild(textArea);
|
|
80
|
+
textArea.focus();
|
|
81
|
+
textArea.select();
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
document.execCommand('copy');
|
|
85
|
+
document.body.removeChild(textArea);
|
|
86
|
+
return Promise.resolve();
|
|
87
|
+
} catch (err) {
|
|
88
|
+
document.body.removeChild(textArea);
|
|
89
|
+
return Promise.reject(err);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Copy text to clipboard
|
|
94
|
+
* @param text
|
|
95
|
+
* @returns {Promise<void>}
|
|
96
|
+
*
|
|
97
|
+
* Ex.
|
|
98
|
+
* copyTextToClipboard(text).then(
|
|
99
|
+
* function () {
|
|
100
|
+
* console.log('Async: Copying to clipboard was successful!');
|
|
101
|
+
* },
|
|
102
|
+
* function (err) {
|
|
103
|
+
* console.error('Async: Could not copy text: ', err);
|
|
104
|
+
* });
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
function copyTextToClipboard(text) {
|
|
109
|
+
if (!navigator.clipboard) {
|
|
110
|
+
return fallbackCopyTextToClipboard(text);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return navigator.clipboard.writeText(text);
|
|
57
114
|
}
|
package/lib/cjs/utils/url.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
|
-
exports.
|
|
4
|
+
exports.appendURLSearchParams = appendURLSearchParams;
|
|
5
|
+
exports.urlReplacer = exports.urlB64ToUint8Array = exports.isValidUrls = exports.isValidUrl = exports.getDomain = void 0;
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Utility Url Replacer
|
|
@@ -66,5 +67,46 @@ const isValidUrls = (value, delimiter) => {
|
|
|
66
67
|
const urls = value.trim().split(delimiter);
|
|
67
68
|
return urls.every(isValidUrl);
|
|
68
69
|
};
|
|
70
|
+
/**
|
|
71
|
+
* Append params
|
|
72
|
+
* @param baseUrl
|
|
73
|
+
* @param queryParams
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
exports.isValidUrls = isValidUrls;
|
|
78
|
+
|
|
79
|
+
function appendURLSearchParams(baseUrl, queryParams) {
|
|
80
|
+
let _url = baseUrl;
|
|
81
|
+
|
|
82
|
+
if (queryParams.length && _url) {
|
|
83
|
+
const key = Object.keys(queryParams[0])[0];
|
|
84
|
+
_url += (_url.split('?')[1] ? '&' : '?') + `${key}=${queryParams[0][key]}`;
|
|
85
|
+
queryParams.slice(1).map(p => {
|
|
86
|
+
const key = Object.keys(p)[0];
|
|
87
|
+
_url += `&${key}=${p[key]}`;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return _url;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Take the application server's public key, which is Base64 URL-safe encoded,
|
|
95
|
+
* and convert it to a UInt8Array, because this is the expected input of the subscribe()
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
const urlB64ToUint8Array = base64String => {
|
|
100
|
+
const padding = '='.repeat((4 - base64String.length % 4) % 4);
|
|
101
|
+
const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/');
|
|
102
|
+
const rawData = window.atob(base64);
|
|
103
|
+
const outputArray = new Uint8Array(rawData.length);
|
|
104
|
+
|
|
105
|
+
for (let i = 0; i < rawData.length; ++i) {
|
|
106
|
+
outputArray[i] = rawData.charCodeAt(i);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return outputArray;
|
|
110
|
+
};
|
|
69
111
|
|
|
70
|
-
exports.
|
|
112
|
+
exports.urlB64ToUint8Array = urlB64ToUint8Array;
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* WSClientPropTypes interface
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* WSClient: manage socket connection
|
|
12
|
+
* @param options
|
|
13
|
+
* @constructor
|
|
14
|
+
*/
|
|
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;
|
|
27
|
+
}
|
|
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();
|
|
71
|
+
}
|
|
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;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (!cfg.uri) {
|
|
88
|
+
console.error('Invalid WSClient Uri options.');
|
|
89
|
+
_error = true;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (cfg && cfg.connecting && !(typeof cfg.connecting === 'function')) {
|
|
93
|
+
console.error('Invalid WSClient connecting options.');
|
|
94
|
+
_error = true;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (cfg && cfg.connected && !(typeof cfg.connected === 'function')) {
|
|
98
|
+
console.error('Invalid WSClient connected options.');
|
|
99
|
+
_error = true;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (cfg && cfg.receiveMessage && !(typeof cfg.receiveMessage === 'function')) {
|
|
103
|
+
console.error('Invalid WSClient receiveMessage options.');
|
|
104
|
+
_error = true;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (cfg && cfg.disconnected && !(typeof cfg.disconnected === 'function')) {
|
|
108
|
+
console.error('Invalid WSClient connecting options.');
|
|
109
|
+
_error = true;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (cfg && cfg.heartbeatMsg && !(typeof cfg.heartbeatMsg === 'string')) {
|
|
113
|
+
console.error('Invalid WSClient heartbeatMsg options.');
|
|
114
|
+
_error = true;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (cfg && cfg.debug && !(typeof cfg.debug === 'boolean')) {
|
|
118
|
+
console.error('Invalid WSClient debug options.');
|
|
119
|
+
_error = true;
|
|
120
|
+
}
|
|
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);
|
|
135
|
+
}
|
|
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
|
+
}
|
|
167
|
+
}
|
|
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);
|
|
182
|
+
}
|
|
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);
|
|
221
|
+
}
|
|
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.');
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
exports.default = WSClient;
|
|
@@ -0,0 +1,41 @@
|
|
|
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;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function getWindowHeight() {
|
|
13
|
+
return typeof global.window !== 'undefined' ? global.window.innerHeight : 0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
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
|
|
23
|
+
// (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) {
|
|
29
|
+
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
|
+
}
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import { capitalize, isString, stripHtml, camelCase } from './utils/string';
|
|
2
|
-
import { isValidUrl, isValidUrls, urlReplacer, getDomain } from './utils/url';
|
|
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 } 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';
|
|
3
8
|
/**
|
|
4
9
|
* Export all utilities
|
|
5
10
|
*/
|
|
6
|
-
export { capitalize, isString, stripHtml, camelCase, isValidUrl, isValidUrls, urlReplacer, getDomain };
|
|
11
|
+
export { capitalize, isString, stripHtml, camelCase, copyTextToClipboard, fallbackCopyTextToClipboard, random, isValidUrl, isValidUrls, urlReplacer, getDomain, appendURLSearchParams, urlB64ToUint8Array, getHighestSafeWindowContext, getWindowWidth, getWindowHeight, Logger, mergeDeep, isObject, WSClient, WSClientType, WSClientPropTypes, loadVersionBrowser };
|
|
7
12
|
//# sourceMappingURL=index.d.ts.map
|