From bc76d19c6f5687dd18c96db249e0abe7ad79a673 Mon Sep 17 00:00:00 2001 From: Vijay A Date: Sun, 15 Aug 2021 17:15:09 +0530 Subject: chore: typescript conversion of some minor utilities Also removed 'targz' unused package from runtime. --- src/helpers/array-helpers.js | 4 --- src/helpers/array-helpers.ts | 4 +++ src/helpers/asar-helpers.js | 3 -- src/helpers/asar-helpers.ts | 3 ++ src/helpers/async-helpers.js | 5 --- src/helpers/async-helpers.ts | 5 +++ src/helpers/i18n-helpers.js | 72 ---------------------------------------- src/helpers/i18n-helpers.ts | 72 ++++++++++++++++++++++++++++++++++++++++ src/helpers/password-helpers.js | 36 -------------------- src/helpers/password-helpers.ts | 36 ++++++++++++++++++++ src/helpers/recipe-helpers.js | 34 ------------------- src/helpers/recipe-helpers.ts | 34 +++++++++++++++++++ src/helpers/routing-helpers.js | 4 --- src/helpers/routing-helpers.ts | 3 ++ src/helpers/schedule-helpers.js | 70 -------------------------------------- src/helpers/schedule-helpers.ts | 70 ++++++++++++++++++++++++++++++++++++++ src/helpers/userAgent-helpers.js | 41 ----------------------- src/helpers/userAgent-helpers.ts | 41 +++++++++++++++++++++++ 18 files changed, 268 insertions(+), 269 deletions(-) delete mode 100644 src/helpers/array-helpers.js create mode 100644 src/helpers/array-helpers.ts delete mode 100644 src/helpers/asar-helpers.js create mode 100644 src/helpers/asar-helpers.ts delete mode 100644 src/helpers/async-helpers.js create mode 100644 src/helpers/async-helpers.ts delete mode 100644 src/helpers/i18n-helpers.js create mode 100644 src/helpers/i18n-helpers.ts delete mode 100644 src/helpers/password-helpers.js create mode 100644 src/helpers/password-helpers.ts delete mode 100644 src/helpers/recipe-helpers.js create mode 100644 src/helpers/recipe-helpers.ts delete mode 100644 src/helpers/routing-helpers.js create mode 100644 src/helpers/routing-helpers.ts delete mode 100644 src/helpers/schedule-helpers.js create mode 100644 src/helpers/schedule-helpers.ts delete mode 100644 src/helpers/userAgent-helpers.js create mode 100644 src/helpers/userAgent-helpers.ts (limited to 'src/helpers') diff --git a/src/helpers/array-helpers.js b/src/helpers/array-helpers.js deleted file mode 100644 index 5e592b7f7..000000000 --- a/src/helpers/array-helpers.js +++ /dev/null @@ -1,4 +0,0 @@ -export const shuffleArray = (arr) => arr - .map((a) => [Math.random(), a]) - .sort((a, b) => a[0] - b[0]) - .map((a) => a[1]); diff --git a/src/helpers/array-helpers.ts b/src/helpers/array-helpers.ts new file mode 100644 index 000000000..ae5d8d99f --- /dev/null +++ b/src/helpers/array-helpers.ts @@ -0,0 +1,4 @@ +export const shuffleArray = (arr: any[]) => arr + .map((a) => [Math.random(), a]) + .sort((a, b) => a[0] - b[0]) + .map((a) => a[1]); diff --git a/src/helpers/asar-helpers.js b/src/helpers/asar-helpers.js deleted file mode 100644 index 9e4380c06..000000000 --- a/src/helpers/asar-helpers.js +++ /dev/null @@ -1,3 +0,0 @@ -export function asarPath(dir = '') { - return dir.replace('app.asar', 'app.asar.unpacked'); -} diff --git a/src/helpers/asar-helpers.ts b/src/helpers/asar-helpers.ts new file mode 100644 index 000000000..3d9f0d941 --- /dev/null +++ b/src/helpers/asar-helpers.ts @@ -0,0 +1,3 @@ +export function asarPath(dir: string = '') { + return dir.replace('app.asar', 'app.asar.unpacked'); +} diff --git a/src/helpers/async-helpers.js b/src/helpers/async-helpers.js deleted file mode 100644 index c6c57e28e..000000000 --- a/src/helpers/async-helpers.js +++ /dev/null @@ -1,5 +0,0 @@ -/* eslint-disable import/prefer-default-export */ - -export function sleep(ms = 0) { - return new Promise((r) => setTimeout(r, ms)); -} diff --git a/src/helpers/async-helpers.ts b/src/helpers/async-helpers.ts new file mode 100644 index 000000000..aae3c3928 --- /dev/null +++ b/src/helpers/async-helpers.ts @@ -0,0 +1,5 @@ +/* eslint-disable import/prefer-default-export */ + +export function sleep(ms: number = 0) { + return new Promise((r) => setTimeout(r, ms)); +} diff --git a/src/helpers/i18n-helpers.js b/src/helpers/i18n-helpers.js deleted file mode 100644 index 807b9066e..000000000 --- a/src/helpers/i18n-helpers.js +++ /dev/null @@ -1,72 +0,0 @@ -export function getLocale({ - locale, locales, defaultLocale, fallbackLocale, -}) { - let localeStr = locale; - if (locales[locale] === undefined) { - let localeFuzzy; - Object.keys(locales).forEach((localStr) => { - if (locales && Object.hasOwnProperty.call(locales, localStr)) { - if (locale.substring(0, 2) === localStr.substring(0, 2)) { - localeFuzzy = localStr; - } - } - }); - - if (localeFuzzy !== undefined) { - localeStr = localeFuzzy; - } - } - - if (locales[localeStr] === undefined) { - localeStr = defaultLocale; - } - - if (!localeStr) { - localeStr = fallbackLocale; - } - - return localeStr; -} - -export function getSelectOptions({ - locales, resetToDefaultText = '', automaticDetectionText = '', sort = true, -}) { - const options = []; - - if (resetToDefaultText) { - options.push( - { - value: '', - label: resetToDefaultText, - }, - ); - } - - if (automaticDetectionText) { - options.push( - { - value: 'automatic', - label: automaticDetectionText, - }, - ); - } - - options.push({ - value: '───', - label: '───', - disabled: true, - }); - - let keys = Object.keys(locales); - if (sort) { - keys = keys.sort(Intl.Collator().compare); - } - keys.forEach((key) => { - options.push({ - value: key, - label: locales[key], - }); - }); - - return options; -} diff --git a/src/helpers/i18n-helpers.ts b/src/helpers/i18n-helpers.ts new file mode 100644 index 000000000..c1f18f446 --- /dev/null +++ b/src/helpers/i18n-helpers.ts @@ -0,0 +1,72 @@ +export function getLocale({ + locale, locales, defaultLocale, fallbackLocale, +}) { + let localeStr = locale; + if (locales[locale] === undefined) { + let localeFuzzy: string | undefined; + Object.keys(locales).forEach((localStr) => { + if (locales && Object.hasOwnProperty.call(locales, localStr)) { + if (locale.substring(0, 2) === localStr.substring(0, 2)) { + localeFuzzy = localStr; + } + } + }); + + if (localeFuzzy !== undefined) { + localeStr = localeFuzzy; + } + } + + if (locales[localeStr] === undefined) { + localeStr = defaultLocale; + } + + if (!localeStr) { + localeStr = fallbackLocale; + } + + return localeStr; +} + +export function getSelectOptions({ + locales, resetToDefaultText = '', automaticDetectionText = '', sort = true, +}) { + const options: object[] = []; + + if (resetToDefaultText) { + options.push( + { + value: '', + label: resetToDefaultText, + }, + ); + } + + if (automaticDetectionText) { + options.push( + { + value: 'automatic', + label: automaticDetectionText, + }, + ); + } + + options.push({ + value: '───', + label: '───', + disabled: true, + }); + + let keys = Object.keys(locales); + if (sort) { + keys = keys.sort(Intl.Collator().compare); + } + keys.forEach((key) => { + options.push({ + value: key, + label: locales[key], + }); + }); + + return options; +} diff --git a/src/helpers/password-helpers.js b/src/helpers/password-helpers.js deleted file mode 100644 index cf461e4f7..000000000 --- a/src/helpers/password-helpers.js +++ /dev/null @@ -1,36 +0,0 @@ -import crypto from 'crypto'; - -export function hash(password) { - return crypto.createHash('sha256').update(password).digest('base64'); -} - -export function scorePassword(password) { - let score = 0; - if (!password) { - return score; - } - - // award every unique letter until 5 repetitions - const letters = {}; - for (let i = 0; i < password.length; i += 1) { - letters[password[i]] = (letters[password[i]] || 0) + 1; - score += 5.0 / letters[password[i]]; - } - - // bonus points for mixing it up - const variations = { - digits: /\d/.test(password), - lower: /[a-z]/.test(password), - upper: /[A-Z]/.test(password), - nonWords: /\W/.test(password), - }; - - let variationCount = 0; - Object.keys(variations).forEach((key) => { - variationCount += (variations[key] === true) ? 1 : 0; - }); - - score += (variationCount - 1) * 10; - - return parseInt(score, 10); -} diff --git a/src/helpers/password-helpers.ts b/src/helpers/password-helpers.ts new file mode 100644 index 000000000..89c75c752 --- /dev/null +++ b/src/helpers/password-helpers.ts @@ -0,0 +1,36 @@ +import crypto from 'crypto'; + +export function hash(password: crypto.BinaryLike) { + return crypto.createHash('sha256').update(password).digest('base64'); +} + +export function scorePassword(password: string) { + let score = 0; + if (!password) { + return score; + } + + // award every unique letter until 5 repetitions + const letters = {}; + for (let i = 0; i < password.length; i += 1) { + letters[password[i]] = (letters[password[i]] || 0) + 1; + score += 5.0 / letters[password[i]]; + } + + // bonus points for mixing it up + const variations = { + digits: /\d/.test(password), + lower: /[a-z]/.test(password), + upper: /[A-Z]/.test(password), + nonWords: /\W/.test(password), + }; + + let variationCount = 0; + Object.keys(variations).forEach((key) => { + variationCount += (variations[key] === true) ? 1 : 0; + }); + + score += (variationCount - 1) * 10; + + return parseInt(score.toString(), 10); +} diff --git a/src/helpers/recipe-helpers.js b/src/helpers/recipe-helpers.js deleted file mode 100644 index 7e4bfa85a..000000000 --- a/src/helpers/recipe-helpers.js +++ /dev/null @@ -1,34 +0,0 @@ -import { parse } from 'path'; -import { userDataRecipesPath } from '../environment'; - -export function getRecipeDirectory(id = '') { - return userDataRecipesPath(id); -} - -export function getDevRecipeDirectory(id = '') { - return userDataRecipesPath('dev', id); -} - -export function loadRecipeConfig(recipeId) { - try { - const configPath = `${recipeId}/package.json`; - // Delete module from cache - delete require.cache[require.resolve(configPath)]; - - // eslint-disable-next-line - let config = require(configPath); - - const moduleConfigPath = require.resolve(configPath); - config.path = parse(moduleConfigPath).dir; - - return config; - } catch (e) { - console.error(e); - return null; - } -} - -module.paths.unshift( - getDevRecipeDirectory(), - getRecipeDirectory(), -); diff --git a/src/helpers/recipe-helpers.ts b/src/helpers/recipe-helpers.ts new file mode 100644 index 000000000..965429210 --- /dev/null +++ b/src/helpers/recipe-helpers.ts @@ -0,0 +1,34 @@ +import { parse } from 'path'; +import { userDataRecipesPath } from '../environment'; + +export function getRecipeDirectory(id: string = ''): string { + return userDataRecipesPath(id); +} + +export function getDevRecipeDirectory(id: string = ''): string { + return userDataRecipesPath('dev', id); +} + +export function loadRecipeConfig(recipeId: string) { + try { + const configPath = `${recipeId}/package.json`; + // Delete module from cache + delete require.cache[require.resolve(configPath)]; + + // eslint-disable-next-line + let config = require(configPath); + + const moduleConfigPath = require.resolve(configPath); + config.path = parse(moduleConfigPath).dir; + + return config; + } catch (e) { + console.error(e); + return null; + } +} + +module.paths.unshift( + getDevRecipeDirectory(), + getRecipeDirectory(), +); 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 @@ -import RouteParser from 'route-parser'; - -// eslint-disable-next-line -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 @@ +import RouteParser from 'route-parser'; + +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.js deleted file mode 100644 index a3020cad6..000000000 --- a/src/helpers/schedule-helpers.js +++ /dev/null @@ -1,70 +0,0 @@ -/* eslint-disable import/prefer-default-export */ - -export function isInTimeframe(start, end) { - const [ - startHourStr, - startMinuteStr, - ] = start.split(':'); - const startHour = parseInt(startHourStr, 10); - const startMinute = parseInt(startMinuteStr, 10); - - const [ - endHourStr, - endMinuteStr, - ] = end.split(':'); - const endHour = parseInt(endHourStr, 10); - const endMinute = parseInt(endMinuteStr, 10); - - const currentHour = new Date().getHours(); - const currentMinute = new Date().getMinutes(); - - // Check if the end time is before the start time (scheduled overnight) - // as we need to change our checks based on this - const endBeforeStart = (startHour > endHour || (startHour === endHour && startMinute > endMinute)); - - if ( - // End is after start (e.g. 09:00-17:00) - !endBeforeStart - // Check if past start - && ((currentHour > startHour - || ( - currentHour === startHour - && currentMinute >= startMinute - ) - ) - // Check that not past end - && (currentHour < endHour - || ( - currentHour === endHour - && currentMinute < endMinute - ) - )) - ) { - // We are in scheduled timeframe - return true; - } - if ( - // End is before start (e.g. 17:00-09:00) - endBeforeStart - // Check if past start - && ((currentHour > startHour - || ( - currentHour === startHour - && currentMinute >= startMinute - ) - ) - // Check that we are not past end - || (currentHour < endHour - || ( - currentHour === endHour - && currentMinute < endMinute - ) - )) - ) { - // We are also in scheduled timeframe - return true; - } - - // We are not in scheduled timeframe - return false; -} diff --git a/src/helpers/schedule-helpers.ts b/src/helpers/schedule-helpers.ts new file mode 100644 index 000000000..754fd5556 --- /dev/null +++ b/src/helpers/schedule-helpers.ts @@ -0,0 +1,70 @@ +/* eslint-disable import/prefer-default-export */ + +export function isInTimeframe(start: string, end: string) { + const [ + startHourStr, + startMinuteStr, + ] = start.split(':'); + const startHour = parseInt(startHourStr, 10); + const startMinute = parseInt(startMinuteStr, 10); + + const [ + endHourStr, + endMinuteStr, + ] = end.split(':'); + const endHour = parseInt(endHourStr, 10); + const endMinute = parseInt(endMinuteStr, 10); + + const currentHour = new Date().getHours(); + const currentMinute = new Date().getMinutes(); + + // Check if the end time is before the start time (scheduled overnight) + // as we need to change our checks based on this + const endBeforeStart = (startHour > endHour || (startHour === endHour && startMinute > endMinute)); + + if ( + // End is after start (e.g. 09:00-17:00) + !endBeforeStart + // Check if past start + && ((currentHour > startHour + || ( + currentHour === startHour + && currentMinute >= startMinute + ) + ) + // Check that not past end + && (currentHour < endHour + || ( + currentHour === endHour + && currentMinute < endMinute + ) + )) + ) { + // We are in scheduled timeframe + return true; + } + if ( + // End is before start (e.g. 17:00-09:00) + endBeforeStart + // Check if past start + && ((currentHour > startHour + || ( + currentHour === startHour + && currentMinute >= startMinute + ) + ) + // Check that we are not past end + || (currentHour < endHour + || ( + currentHour === endHour + && currentMinute < endMinute + ) + )) + ) { + // We are also in scheduled timeframe + return true; + } + + // We are not in scheduled timeframe + return false; +} diff --git a/src/helpers/userAgent-helpers.js b/src/helpers/userAgent-helpers.js deleted file mode 100644 index ede5e6dc4..000000000 --- a/src/helpers/userAgent-helpers.js +++ /dev/null @@ -1,41 +0,0 @@ -import os from 'os'; -import macosVersion from 'macos-version'; -import { chrome } from 'useragent-generator'; -import { - chromeVersion, isMac, isWindows, is64Bit, osArch, osRelease, -} from '../environment'; - -function macOS() { - const version = macosVersion(); - let cpuName = os.cpus()[0].model.split(' ')[0]; - if (cpuName && cpuName.match(/\(/)) { - cpuName = cpuName.split('(')[0]; - } - return `Macintosh; ${cpuName} Mac OS X ${version.replace(/\./g, '_')}`; -} - -function windows() { - const version = osRelease; - const [majorVersion, minorVersion] = version.split('.'); - const archString = is64Bit ? 'Win64' : 'Win32'; - return `Windows NT ${majorVersion}.${minorVersion}; ${archString}; ${osArch}`; -} - -function linux() { - const archString = is64Bit ? 'x86_64' : osArch; - return `X11; Ubuntu; Linux ${archString}`; -} - -export default function userAgent() { - let platformString = ''; - - if (isMac) { - platformString = macOS(); - } else if (isWindows) { - platformString = windows(); - } else { - platformString = linux(); - } - - return chrome({ os: platformString, version: chromeVersion }); -} diff --git a/src/helpers/userAgent-helpers.ts b/src/helpers/userAgent-helpers.ts new file mode 100644 index 000000000..73c8bfd03 --- /dev/null +++ b/src/helpers/userAgent-helpers.ts @@ -0,0 +1,41 @@ +import os from 'os'; +import macosVersion from 'macos-version'; +import { chrome } from 'useragent-generator'; +import { + chromeVersion, isMac, isWindows, is64Bit, osArch, osRelease, +} from '../environment'; + +function macOS() { + const version = macosVersion() || ''; + let cpuName = os.cpus()[0].model.split(' ')[0]; + if (cpuName && cpuName.match(/\(/)) { + cpuName = cpuName.split('(')[0]; + } + return `Macintosh; ${cpuName} Mac OS X ${version.replace(/\./g, '_')}`; +} + +function windows() { + const version = osRelease; + const [majorVersion, minorVersion] = version.split('.'); + const archString = is64Bit ? 'Win64' : 'Win32'; + return `Windows NT ${majorVersion}.${minorVersion}; ${archString}; ${osArch}`; +} + +function linux() { + const archString = is64Bit ? 'x86_64' : osArch; + return `X11; Ubuntu; Linux ${archString}`; +} + +export default function userAgent() { + let platformString = ''; + + if (isMac) { + platformString = macOS(); + } else if (isWindows) { + platformString = windows(); + } else { + platformString = linux(); + } + + return chrome({ os: platformString, version: chromeVersion }); +} -- cgit v1.2.3-54-g00ecf