diff options
author | Vijay A <avijayr@protonmail.com> | 2021-08-24 10:22:22 +0530 |
---|---|---|
committer | Vijay A <avijayr@protonmail.com> | 2021-08-24 10:22:22 +0530 |
commit | 2dc7b2fd3976853f3dc2f182f12cfe11c3b83688 (patch) | |
tree | 22fc7a9e4d3ddd2be16821c871259fb067c7df30 /src/helpers | |
parent | docs: upgraded version to 5.6.1-beta.1 (diff) | |
parent | 5.6.1-nightly.37 [skip ci] (diff) | |
download | ferdium-app-2dc7b2fd3976853f3dc2f182f12cfe11c3b83688.tar.gz ferdium-app-2dc7b2fd3976853f3dc2f182f12cfe11c3b83688.tar.zst ferdium-app-2dc7b2fd3976853f3dc2f182f12cfe11c3b83688.zip |
Merge remote-tracking branch 'origin/develop' into release
Diffstat (limited to 'src/helpers')
-rw-r--r-- | src/helpers/array-helpers.ts (renamed from src/helpers/array-helpers.js) | 2 | ||||
-rw-r--r-- | src/helpers/asar-helpers.ts (renamed from src/helpers/asar-helpers.js) | 2 | ||||
-rw-r--r-- | src/helpers/async-helpers.ts (renamed from src/helpers/async-helpers.js) | 2 | ||||
-rw-r--r-- | src/helpers/i18n-helpers.ts (renamed from src/helpers/i18n-helpers.js) | 4 | ||||
-rw-r--r-- | src/helpers/password-helpers.ts (renamed from src/helpers/password-helpers.js) | 6 | ||||
-rw-r--r-- | src/helpers/recipe-helpers.ts (renamed from src/helpers/recipe-helpers.js) | 6 | ||||
-rw-r--r-- | src/helpers/routing-helpers.js | 4 | ||||
-rw-r--r-- | src/helpers/routing-helpers.ts | 3 | ||||
-rw-r--r-- | src/helpers/schedule-helpers.ts (renamed from src/helpers/schedule-helpers.js) | 2 | ||||
-rw-r--r-- | src/helpers/url-helpers.js | 20 | ||||
-rw-r--r-- | src/helpers/url-helpers.ts | 37 | ||||
-rw-r--r-- | src/helpers/userAgent-helpers.ts (renamed from src/helpers/userAgent-helpers.js) | 2 | ||||
-rw-r--r-- | src/helpers/visibility-helper.js | 27 |
13 files changed, 53 insertions, 64 deletions
diff --git a/src/helpers/array-helpers.js b/src/helpers/array-helpers.ts index 5e592b7f7..ae5d8d99f 100644 --- a/src/helpers/array-helpers.js +++ b/src/helpers/array-helpers.ts | |||
@@ -1,4 +1,4 @@ | |||
1 | export const shuffleArray = (arr) => arr | 1 | export const shuffleArray = (arr: any[]) => arr |
2 | .map((a) => [Math.random(), a]) | 2 | .map((a) => [Math.random(), a]) |
3 | .sort((a, b) => a[0] - b[0]) | 3 | .sort((a, b) => a[0] - b[0]) |
4 | .map((a) => a[1]); | 4 | .map((a) => a[1]); |
diff --git a/src/helpers/asar-helpers.js b/src/helpers/asar-helpers.ts index 9e4380c06..3d9f0d941 100644 --- a/src/helpers/asar-helpers.js +++ b/src/helpers/asar-helpers.ts | |||
@@ -1,3 +1,3 @@ | |||
1 | export function asarPath(dir = '') { | 1 | export function asarPath(dir: string = '') { |
2 | return dir.replace('app.asar', 'app.asar.unpacked'); | 2 | return dir.replace('app.asar', 'app.asar.unpacked'); |
3 | } | 3 | } |
diff --git a/src/helpers/async-helpers.js b/src/helpers/async-helpers.ts index c6c57e28e..aae3c3928 100644 --- a/src/helpers/async-helpers.js +++ b/src/helpers/async-helpers.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | /* eslint-disable import/prefer-default-export */ | 1 | /* eslint-disable import/prefer-default-export */ |
2 | 2 | ||
3 | export function sleep(ms = 0) { | 3 | export function sleep(ms: number = 0) { |
4 | return new Promise((r) => setTimeout(r, ms)); | 4 | return new Promise((r) => setTimeout(r, ms)); |
5 | } | 5 | } |
diff --git a/src/helpers/i18n-helpers.js b/src/helpers/i18n-helpers.ts index 807b9066e..c1f18f446 100644 --- a/src/helpers/i18n-helpers.js +++ b/src/helpers/i18n-helpers.ts | |||
@@ -3,7 +3,7 @@ export function getLocale({ | |||
3 | }) { | 3 | }) { |
4 | let localeStr = locale; | 4 | let localeStr = locale; |
5 | if (locales[locale] === undefined) { | 5 | if (locales[locale] === undefined) { |
6 | let localeFuzzy; | 6 | let localeFuzzy: string | undefined; |
7 | Object.keys(locales).forEach((localStr) => { | 7 | Object.keys(locales).forEach((localStr) => { |
8 | if (locales && Object.hasOwnProperty.call(locales, localStr)) { | 8 | if (locales && Object.hasOwnProperty.call(locales, localStr)) { |
9 | if (locale.substring(0, 2) === localStr.substring(0, 2)) { | 9 | if (locale.substring(0, 2) === localStr.substring(0, 2)) { |
@@ -31,7 +31,7 @@ export function getLocale({ | |||
31 | export function getSelectOptions({ | 31 | export function getSelectOptions({ |
32 | locales, resetToDefaultText = '', automaticDetectionText = '', sort = true, | 32 | locales, resetToDefaultText = '', automaticDetectionText = '', sort = true, |
33 | }) { | 33 | }) { |
34 | const options = []; | 34 | const options: object[] = []; |
35 | 35 | ||
36 | if (resetToDefaultText) { | 36 | if (resetToDefaultText) { |
37 | options.push( | 37 | options.push( |
diff --git a/src/helpers/password-helpers.js b/src/helpers/password-helpers.ts index cf461e4f7..89c75c752 100644 --- a/src/helpers/password-helpers.js +++ b/src/helpers/password-helpers.ts | |||
@@ -1,10 +1,10 @@ | |||
1 | import crypto from 'crypto'; | 1 | import crypto from 'crypto'; |
2 | 2 | ||
3 | export function hash(password) { | 3 | export function hash(password: crypto.BinaryLike) { |
4 | return crypto.createHash('sha256').update(password).digest('base64'); | 4 | return crypto.createHash('sha256').update(password).digest('base64'); |
5 | } | 5 | } |
6 | 6 | ||
7 | export function scorePassword(password) { | 7 | export function scorePassword(password: string) { |
8 | let score = 0; | 8 | let score = 0; |
9 | if (!password) { | 9 | if (!password) { |
10 | return score; | 10 | return score; |
@@ -32,5 +32,5 @@ export function scorePassword(password) { | |||
32 | 32 | ||
33 | score += (variationCount - 1) * 10; | 33 | score += (variationCount - 1) * 10; |
34 | 34 | ||
35 | return parseInt(score, 10); | 35 | return parseInt(score.toString(), 10); |
36 | } | 36 | } |
diff --git a/src/helpers/recipe-helpers.js b/src/helpers/recipe-helpers.ts index 7e4bfa85a..965429210 100644 --- a/src/helpers/recipe-helpers.js +++ b/src/helpers/recipe-helpers.ts | |||
@@ -1,15 +1,15 @@ | |||
1 | import { parse } from 'path'; | 1 | import { parse } from 'path'; |
2 | import { userDataRecipesPath } from '../environment'; | 2 | import { userDataRecipesPath } from '../environment'; |
3 | 3 | ||
4 | export function getRecipeDirectory(id = '') { | 4 | export function getRecipeDirectory(id: string = ''): string { |
5 | return userDataRecipesPath(id); | 5 | return userDataRecipesPath(id); |
6 | } | 6 | } |
7 | 7 | ||
8 | export function getDevRecipeDirectory(id = '') { | 8 | export function getDevRecipeDirectory(id: string = ''): string { |
9 | return userDataRecipesPath('dev', id); | 9 | return userDataRecipesPath('dev', id); |
10 | } | 10 | } |
11 | 11 | ||
12 | export function loadRecipeConfig(recipeId) { | 12 | export function loadRecipeConfig(recipeId: string) { |
13 | try { | 13 | try { |
14 | const configPath = `${recipeId}/package.json`; | 14 | const configPath = `${recipeId}/package.json`; |
15 | // Delete module from cache | 15 | // Delete module from cache |
diff --git a/src/helpers/routing-helpers.js b/src/helpers/routing-helpers.js deleted file mode 100644 index 14922ebf3..000000000 --- a/src/helpers/routing-helpers.js +++ /dev/null | |||
@@ -1,4 +0,0 @@ | |||
1 | import RouteParser from 'route-parser'; | ||
2 | |||
3 | // eslint-disable-next-line | ||
4 | export const matchRoute = (pattern, path) => new RouteParser(pattern).match(path); | ||
diff --git a/src/helpers/routing-helpers.ts b/src/helpers/routing-helpers.ts new file mode 100644 index 000000000..18169f01b --- /dev/null +++ b/src/helpers/routing-helpers.ts | |||
@@ -0,0 +1,3 @@ | |||
1 | import RouteParser from 'route-parser'; | ||
2 | |||
3 | export const matchRoute = (pattern: string, path: string) => new RouteParser(pattern).match(path); | ||
diff --git a/src/helpers/schedule-helpers.js b/src/helpers/schedule-helpers.ts index a3020cad6..754fd5556 100644 --- a/src/helpers/schedule-helpers.js +++ b/src/helpers/schedule-helpers.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | /* eslint-disable import/prefer-default-export */ | 1 | /* eslint-disable import/prefer-default-export */ |
2 | 2 | ||
3 | export function isInTimeframe(start, end) { | 3 | export function isInTimeframe(start: string, end: string) { |
4 | const [ | 4 | const [ |
5 | startHourStr, | 5 | startHourStr, |
6 | startMinuteStr, | 6 | startMinuteStr, |
diff --git a/src/helpers/url-helpers.js b/src/helpers/url-helpers.js deleted file mode 100644 index 972f9b79a..000000000 --- a/src/helpers/url-helpers.js +++ /dev/null | |||
@@ -1,20 +0,0 @@ | |||
1 | import { URL } from 'url'; | ||
2 | |||
3 | import { ALLOWED_PROTOCOLS } from '../config'; | ||
4 | |||
5 | const debug = require('debug')('Ferdi:Helpers:url'); | ||
6 | |||
7 | export function isValidExternalURL(url) { | ||
8 | let parsedUrl; | ||
9 | try { | ||
10 | parsedUrl = new URL(url); | ||
11 | } catch (_) { | ||
12 | return false; | ||
13 | } | ||
14 | |||
15 | const isAllowed = ALLOWED_PROTOCOLS.includes(parsedUrl.protocol); | ||
16 | |||
17 | debug('protocol check is', isAllowed, 'for:', url); | ||
18 | |||
19 | return isAllowed; | ||
20 | } | ||
diff --git a/src/helpers/url-helpers.ts b/src/helpers/url-helpers.ts new file mode 100644 index 000000000..3657ae693 --- /dev/null +++ b/src/helpers/url-helpers.ts | |||
@@ -0,0 +1,37 @@ | |||
1 | // This is taken from: https://benjamin-altpeter.de/shell-openexternal-dangers/ | ||
2 | |||
3 | import { URL } from 'url'; | ||
4 | import { ensureDirSync } from 'fs-extra'; | ||
5 | import { shell } from 'electron'; | ||
6 | |||
7 | import { ALLOWED_PROTOCOLS } from '../config'; | ||
8 | |||
9 | const debug = require('debug')('Ferdi:Helpers:url'); | ||
10 | |||
11 | export function isValidExternalURL(url: string | URL) { | ||
12 | let parsedUrl: URL; | ||
13 | try { | ||
14 | parsedUrl = new URL(url.toString()); | ||
15 | } catch (_) { | ||
16 | return false; | ||
17 | } | ||
18 | |||
19 | const isAllowed = ALLOWED_PROTOCOLS.includes(parsedUrl.protocol); | ||
20 | |||
21 | debug('protocol check is', isAllowed, 'for:', url); | ||
22 | |||
23 | return isAllowed; | ||
24 | } | ||
25 | |||
26 | export async function openPath(folderName: string) { | ||
27 | ensureDirSync(folderName); | ||
28 | shell.openPath(folderName); | ||
29 | } | ||
30 | |||
31 | // TODO: Need to verify and fix/remove the skipping logic. Ideally, we should never skip this check | ||
32 | export function openExternalUrl(url: string | URL, skipValidityCheck: boolean = false) { | ||
33 | debug('Open url:', url, 'with skipValidityCheck:', skipValidityCheck); | ||
34 | if (skipValidityCheck || isValidExternalURL(url)) { | ||
35 | shell.openExternal(url.toString()); | ||
36 | } | ||
37 | } | ||
diff --git a/src/helpers/userAgent-helpers.js b/src/helpers/userAgent-helpers.ts index ede5e6dc4..73c8bfd03 100644 --- a/src/helpers/userAgent-helpers.js +++ b/src/helpers/userAgent-helpers.ts | |||
@@ -6,7 +6,7 @@ import { | |||
6 | } from '../environment'; | 6 | } from '../environment'; |
7 | 7 | ||
8 | function macOS() { | 8 | function macOS() { |
9 | const version = macosVersion(); | 9 | const version = macosVersion() || ''; |
10 | let cpuName = os.cpus()[0].model.split(' ')[0]; | 10 | let cpuName = os.cpus()[0].model.split(' ')[0]; |
11 | if (cpuName && cpuName.match(/\(/)) { | 11 | if (cpuName && cpuName.match(/\(/)) { |
12 | cpuName = cpuName.split('(')[0]; | 12 | cpuName = cpuName.split('(')[0]; |
diff --git a/src/helpers/visibility-helper.js b/src/helpers/visibility-helper.js deleted file mode 100644 index 8456c4ecf..000000000 --- a/src/helpers/visibility-helper.js +++ /dev/null | |||
@@ -1,27 +0,0 @@ | |||
1 | export function onVisibilityChange(cb) { | ||
2 | let isVisible = true; | ||
3 | |||
4 | if (!cb) { | ||
5 | throw new Error('no callback given'); | ||
6 | } | ||
7 | |||
8 | function focused() { | ||
9 | if (!isVisible) { | ||
10 | cb(isVisible = true); | ||
11 | } | ||
12 | } | ||
13 | |||
14 | function unfocused() { | ||
15 | if (isVisible) { | ||
16 | cb(isVisible = false); | ||
17 | } | ||
18 | } | ||
19 | |||
20 | document.addEventListener('visibilitychange', () => { (document.hidden ? unfocused : focused)(); }); | ||
21 | |||
22 | window.onpageshow = focused; | ||
23 | window.onfocus = focused; | ||
24 | |||
25 | window.onpagehid = unfocused; | ||
26 | window.onblur = unfocused; | ||
27 | } | ||