@selfcommunity/utils 0.1.4 → 0.1.5

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.
@@ -1 +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,EAAC,MAAM,gBAAgB,CAAC;AAC1E,OAAO,EAAC,UAAU,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAC,MAAM,aAAa,CAAC;AAE5E;;GAEG;AACH,OAAO,EAAC,UAAU,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAC,CAAC"}
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,EAAC,MAAM,gBAAgB,CAAC;AAC5F,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,MAAM,EACN,SAAS,EACT,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,iBAAiB,EACjB,kBAAkB,EACnB,CAAC"}
package/lib/esm/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,9 @@
1
+ /**
2
+ * Get browser version to track on backend
3
+ * @return {{name: string, version: null}|{name: string, version: string}|{name: *, version: *}|{name: string, version}}
4
+ */
5
+ export declare const loadVersionBrowser: () => {
6
+ name: any;
7
+ version: any;
8
+ };
9
+ //# sourceMappingURL=browser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../../../src/utils/browser.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,kBAAkB;;;CAgE9B,CAAC"}
@@ -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,11 @@
1
+ /**
2
+ * Emit styled message
3
+ */
4
+ export declare class Logger {
5
+ static info(scope: any, message: any): void;
6
+ static warn(scope: any, message: any): void;
7
+ static error(scope: any, message: any): void;
8
+ static log(scope: any, message: any): void;
9
+ static debug(scope: any, message: any): void;
10
+ }
11
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../src/utils/logger.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,MAAM;IACjB,MAAM,CAAC,IAAI,CAAC,KAAK,KAAA,EAAE,OAAO,KAAA;IAI1B,MAAM,CAAC,IAAI,CAAC,KAAK,KAAA,EAAE,OAAO,KAAA;IAI1B,MAAM,CAAC,KAAK,CAAC,KAAK,KAAA,EAAE,OAAO,KAAA;IAI3B,MAAM,CAAC,GAAG,CAAC,KAAK,KAAA,EAAE,OAAO,KAAA;IAIzB,MAAM,CAAC,KAAK,CAAC,KAAK,KAAA,EAAE,OAAO,KAAA;CAG5B"}
@@ -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,13 @@
1
+ /**
2
+ * Check if v is an object
3
+ * @param v
4
+ */
5
+ export declare function isObject(v: any): boolean;
6
+ /**
7
+ * Perfrom deep merge of two objects (not a shallow merge)
8
+ * @param target
9
+ * @param source
10
+ * @return {*}
11
+ */
12
+ export declare function mergeDeep(target: object, source: object): object;
13
+ //# sourceMappingURL=object.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../../../src/utils/object.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,CAAC,KAAA,WAEzB;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAgBhE"}
@@ -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
+ }
@@ -18,4 +18,26 @@ export declare function camelCase(str: string): string;
18
18
  * @param str
19
19
  */
20
20
  export declare function stripHtml(str: string): string;
21
+ export declare function random(): string;
22
+ /**
23
+ * Fallback if navigator.clipboard doensn't exist
24
+ * @param text
25
+ * @returns {Promise<void>}
26
+ */
27
+ export declare function fallbackCopyTextToClipboard(text: any): Promise<void>;
28
+ /**
29
+ * Copy text to clipboard
30
+ * @param text
31
+ * @returns {Promise<void>}
32
+ *
33
+ * Ex.
34
+ * copyTextToClipboard(text).then(
35
+ * function () {
36
+ * console.log('Async: Copying to clipboard was successful!');
37
+ * },
38
+ * function (err) {
39
+ * console.error('Async: Could not copy text: ', err);
40
+ * });
41
+ */
42
+ export declare function copyTextToClipboard(text: any): Promise<void>;
21
43
  //# sourceMappingURL=string.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../../src/utils/string.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,CAAC,KAAA,WAEzB;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAO9C;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAiB7C;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE7C"}
1
+ {"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../../src/utils/string.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,CAAC,KAAA,WAEzB;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAO9C;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAiB7C;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED,wBAAgB,MAAM,WAErB;AAED;;;;GAIG;AACH,wBAAgB,2BAA2B,CAAC,IAAI,KAAA,iBAqB/C;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,KAAA,iBAKvC"}
@@ -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
  }
@@ -19,4 +19,15 @@ export declare const isValidUrl: (url: string) => boolean;
19
19
  * @param delimiter
20
20
  */
21
21
  export declare const isValidUrls: (value: string, delimiter: string) => boolean;
22
+ /**
23
+ * Append params
24
+ * @param baseUrl
25
+ * @param queryParams
26
+ */
27
+ export declare function appendURLSearchParams(baseUrl: string, queryParams: Record<string, string | number>[]): string;
28
+ /**
29
+ * Take the application server's public key, which is Base64 URL-safe encoded,
30
+ * and convert it to a UInt8Array, because this is the expected input of the subscribe()
31
+ */
32
+ export declare const urlB64ToUint8Array: (base64String: any) => Uint8Array;
22
33
  //# sourceMappingURL=url.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"url.d.ts","sourceRoot":"","sources":["../../../src/utils/url.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,WAAW,SAAU,MAAM,cAWtB,MAAM,QACvB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,SAAS,QAAS,MAAM,KAAG,MAOvC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,UAAU,QAAS,MAAM,KAAG,OAGxC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,aAAa,MAAM,KAAG,OAG9D,CAAC"}
1
+ {"version":3,"file":"url.d.ts","sourceRoot":"","sources":["../../../src/utils/url.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,WAAW,SAAU,MAAM,cAWtB,MAAM,QACvB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,SAAS,QAAS,MAAM,KAAG,MAOvC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,UAAU,QAAS,MAAM,KAAG,OAGxC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,aAAa,MAAM,KAAG,OAG9D,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,EAAE,UAWpG;AAED;;;GAGG;AACH,eAAO,MAAM,kBAAkB,mCAW9B,CAAC"}
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports.urlReplacer = exports.isValidUrls = exports.isValidUrl = exports.getDomain = void 0;
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.isValidUrls = isValidUrls;
112
+ exports.urlB64ToUint8Array = urlB64ToUint8Array;
@@ -0,0 +1,177 @@
1
+ /**
2
+ * WSClientPropTypes interface
3
+ */
4
+ export interface WSClientPropTypes {
5
+ /**
6
+ * The Websocket URI
7
+ */
8
+ uri: string;
9
+ /**
10
+ * The Websocket Protocols
11
+ */
12
+ protocols: string | string[];
13
+ /**
14
+ * String to identify the heartbeat message.
15
+ */
16
+ heartbeatMsg?: string;
17
+ /**
18
+ * Reconnect the websocket if close
19
+ */
20
+ mustReconnect?: boolean;
21
+ /**
22
+ * Callback called after the websocket is connected.
23
+ */
24
+ connected?: () => any;
25
+ /**
26
+ * Callback called when the websocket is connecting.
27
+ */
28
+ connecting?: () => any;
29
+ /**
30
+ * Callback called after the websocket is disconnected.
31
+ */
32
+ disconnected?: (evt: any) => any;
33
+ /**
34
+ * Callback called when a message is received from the websocket.
35
+ */
36
+ receiveMessage?: (data: any) => any;
37
+ /**
38
+ * Enable/disable debug
39
+ */
40
+ debug?: boolean;
41
+ }
42
+ export interface WSClientType {
43
+ /**
44
+ * Send message
45
+ * @param message
46
+ */
47
+ sendMessage: (message: any) => void;
48
+ /**
49
+ * Get current state
50
+ */
51
+ getState: () => string;
52
+ /**
53
+ * Get current connection state
54
+ */
55
+ isConnecting: () => boolean;
56
+ /**
57
+ * Check if ws is connected
58
+ */
59
+ isConnected: () => boolean;
60
+ /**
61
+ * Check if ws is closing connection
62
+ */
63
+ isClosing: () => boolean;
64
+ /**
65
+ * Return if ws is closed
66
+ */
67
+ isClosed: () => boolean;
68
+ /**
69
+ * Close the connection
70
+ */
71
+ close: () => void;
72
+ }
73
+ /**
74
+ * WSClient: manage socket connection
75
+ * @param options
76
+ * @constructor
77
+ */
78
+ export default class WSClient implements WSClientType {
79
+ private static _instance;
80
+ private _cfg;
81
+ private _ws;
82
+ private _timer;
83
+ private _attempts;
84
+ private _heartbeatInterval;
85
+ private _missedHeartbeats;
86
+ /**
87
+ * Constructor
88
+ * @param cfg
89
+ */
90
+ constructor(cfg: WSClientPropTypes);
91
+ /**
92
+ * Get instance
93
+ */
94
+ static getInstance(cfg: any): WSClient;
95
+ /**
96
+ * Connect
97
+ */
98
+ connect(): void;
99
+ /**
100
+ * Validate options
101
+ * @param cfg
102
+ */
103
+ isValidOptions(cfg: WSClientPropTypes): boolean;
104
+ /**
105
+ * Try to reconnect if previous connection failed
106
+ * Generate an interval, after that try to reconnect
107
+ */
108
+ tryToReconnect(): void;
109
+ /**
110
+ * Reestablish the connection
111
+ * Increase the number of attempts
112
+ */
113
+ reconnect(): void;
114
+ /**
115
+ * Send heartbeat every 5 seconds
116
+ * If missing more than 3 heartbeats close connection
117
+ */
118
+ sendHeartbeat(): void;
119
+ /**
120
+ * Established the new connection
121
+ * Reset this._attempts counter
122
+ */
123
+ onOpen(): void;
124
+ /**
125
+ * Connection closed. Try to reconnect.
126
+ * @param evt
127
+ */
128
+ onClose(evt: any): void;
129
+ /**
130
+ * An error occured
131
+ * @param evt
132
+ */
133
+ onError(evt: any): void;
134
+ /**
135
+ * A message has arrived.
136
+ * If it is the heartbeat -> reset this._missedHeartbeats
137
+ * If it is data pass data to the callback
138
+ * @param evt
139
+ */
140
+ onMessage(evt: any): any;
141
+ /**
142
+ * Generate an interval that is randomly between 0 and 2^k - 1, where k is
143
+ * the number of connection attmpts, with a maximum interval of 30 seconds,
144
+ * so it starts at 0 - 1 seconds and maxes out at 0 - 30 seconds
145
+ */
146
+ generateInteval(k: any): number;
147
+ /**
148
+ * Send message
149
+ * @param message
150
+ */
151
+ sendMessage(message: any): void;
152
+ /**
153
+ * Get the ws state
154
+ */
155
+ getState(): any;
156
+ /**
157
+ * Check if ws is in connecting state
158
+ */
159
+ isConnecting(): boolean;
160
+ /**
161
+ * Check if ws is connected
162
+ */
163
+ isConnected(): boolean;
164
+ /**
165
+ * Check if ws is in closing state
166
+ */
167
+ isClosing(): boolean;
168
+ /**
169
+ * Check if ws is closed
170
+ */
171
+ isClosed(): boolean;
172
+ /**
173
+ * Close the connection
174
+ */
175
+ close(): void;
176
+ }
177
+ //# sourceMappingURL=websocket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket.d.ts","sourceRoot":"","sources":["../../../src/utils/websocket.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IAEZ;;OAEG;IACH,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAE7B;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IAExB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,GAAG,CAAC;IAEtB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,GAAG,CAAC;IAEvB;;OAEG;IACH,YAAY,CAAC,EAAE,CAAC,GAAG,KAAA,KAAK,GAAG,CAAC;IAE5B;;OAEG;IACH,cAAc,CAAC,EAAE,CAAC,IAAI,KAAA,KAAK,GAAG,CAAC;IAE/B;;OAEG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,YAAY;IAC3B;;;OAGG;IACH,WAAW,EAAE,CAAC,OAAO,KAAA,KAAK,IAAI,CAAC;IAE/B;;OAEG;IACH,QAAQ,EAAE,MAAM,MAAM,CAAC;IAEvB;;OAEG;IACH,YAAY,EAAE,MAAM,OAAO,CAAC;IAE5B;;OAEG;IACH,WAAW,EAAE,MAAM,OAAO,CAAC;IAE3B;;OAEG;IACH,SAAS,EAAE,MAAM,OAAO,CAAC;IAEzB;;OAEG;IACH,QAAQ,EAAE,MAAM,OAAO,CAAC;IAExB;;OAEG;IACH,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB;AAED;;;;GAIG;AACH,MAAM,CAAC,OAAO,OAAO,QAAS,YAAW,YAAY;IACnD,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC;IACzB,OAAO,CAAC,IAAI,CAAC;IACb,OAAO,CAAC,GAAG,CAAC;IACZ,OAAO,CAAC,MAAM,CAAC;IACf,OAAO,CAAC,SAAS,CAAK;IACtB,OAAO,CAAC,kBAAkB,CAAQ;IAClC,OAAO,CAAC,iBAAiB,CAAK;IAE9B;;;OAGG;gBACS,GAAG,EAAE,iBAAiB;IAQlC;;OAEG;WACW,WAAW,CAAC,GAAG,KAAA,GAAG,QAAQ;IAKxC;;OAEG;IACH,OAAO;IAyBP;;;OAGG;IACH,cAAc,CAAC,GAAG,EAAE,iBAAiB;IAqCrC;;;OAGG;IACH,cAAc;IAQd;;;OAGG;IACH,SAAS;IAKT;;;OAGG;IACH,aAAa;IAeb;;;OAGG;IACH,MAAM;IAUN;;;OAGG;IACH,OAAO,CAAC,GAAG,KAAA;IAMX;;;OAGG;IACH,OAAO,CAAC,GAAG,KAAA;IAKX;;;;;OAKG;IACH,SAAS,CAAC,GAAG,KAAA;IASb;;;;OAIG;IACH,eAAe,CAAC,CAAC,KAAA;IAUjB;;;OAGG;IACI,WAAW,CAAC,OAAO,KAAA;IAI1B;;OAEG;IACI,QAAQ;IAIf;;OAEG;IACI,YAAY;IAInB;;OAEG;IACI,WAAW;IAIlB;;OAEG;IACI,SAAS;IAIhB;;OAEG;IACI,QAAQ;IAIf;;OAEG;IACI,KAAK;CAQb"}