diff options
author | Vijay A <vraravam@users.noreply.github.com> | 2024-01-26 08:24:20 +0530 |
---|---|---|
committer | Vijay Aravamudhan <vraravam@users.noreply.github.com> | 2024-01-28 03:31:35 +0000 |
commit | 6c67387b2978f1a02244a1350725238da35b341b (patch) | |
tree | c357ab6826cce8272642c01b0d80120bd5baaa86 | |
parent | 6.7.1-nightly.11 [skip ci] (diff) | |
download | ferdium-app-6c67387b2978f1a02244a1350725238da35b341b.tar.gz ferdium-app-6c67387b2978f1a02244a1350725238da35b341b.tar.zst ferdium-app-6c67387b2978f1a02244a1350725238da35b341b.zip |
chore: minor refactoring to reduce lines and add tests
-rw-r--r-- | src/helpers/certs-helpers.ts | 9 | ||||
-rw-r--r-- | src/index.ts | 19 | ||||
-rw-r--r-- | src/jsUtils.ts | 3 | ||||
-rw-r--r-- | test/jsUtils.test.ts | 16 |
4 files changed, 24 insertions, 23 deletions
diff --git a/src/helpers/certs-helpers.ts b/src/helpers/certs-helpers.ts index a81e7e365..a6c83f5c4 100644 --- a/src/helpers/certs-helpers.ts +++ b/src/helpers/certs-helpers.ts | |||
@@ -1,12 +1,9 @@ | |||
1 | import { readdirSync, readFileSync, ensureDirSync } from 'fs-extra'; | 1 | import { readdirSync, readFileSync, ensureDirSync } from 'fs-extra'; |
2 | import { join } from 'node:path'; | 2 | import { join } from 'node:path'; |
3 | import { userDataCertsPath } from '../environment-remote'; | 3 | import { userDataCertsPath } from '../environment-remote'; |
4 | import { removeNewLines } from '../jsUtils'; | ||
4 | 5 | ||
5 | export function removeNewLines(string: string) { | 6 | export function checkIfCertIsPresent(certData: string): boolean { |
6 | return string.replaceAll(/\r?\n|\r/g, ''); | ||
7 | } | ||
8 | |||
9 | export function readCerts() { | ||
10 | const certsFolder = userDataCertsPath(); | 7 | const certsFolder = userDataCertsPath(); |
11 | 8 | ||
12 | ensureDirSync(certsFolder); | 9 | ensureDirSync(certsFolder); |
@@ -21,5 +18,5 @@ export function readCerts() { | |||
21 | certs.push(removeNewLines(cert)); | 18 | certs.push(removeNewLines(cert)); |
22 | } | 19 | } |
23 | 20 | ||
24 | return certs; | 21 | return certs.length > 0 && certs.includes(removeNewLines(certData)); |
25 | } | 22 | } |
diff --git a/src/index.ts b/src/index.ts index 8419d5cf9..fe254b4f6 100644 --- a/src/index.ts +++ b/src/index.ts | |||
@@ -50,7 +50,7 @@ import { openExternalUrl } from './helpers/url-helpers'; | |||
50 | import userAgent from './helpers/userAgent-helpers'; | 50 | import userAgent from './helpers/userAgent-helpers'; |
51 | import { translateTo } from './helpers/translation-helpers'; | 51 | import { translateTo } from './helpers/translation-helpers'; |
52 | import { darkThemeGrayDarkest } from './themes/legacy'; | 52 | import { darkThemeGrayDarkest } from './themes/legacy'; |
53 | import { readCerts, removeNewLines } from './helpers/certs-helpers'; | 53 | import { checkIfCertIsPresent } from './helpers/certs-helpers'; |
54 | 54 | ||
55 | const debug = require('./preload-safe-debug')('Ferdium:App'); | 55 | const debug = require('./preload-safe-debug')('Ferdium:App'); |
56 | 56 | ||
@@ -773,21 +773,6 @@ app.on( | |||
773 | return; | 773 | return; |
774 | } | 774 | } |
775 | 775 | ||
776 | const trustedCerts = readCerts(); | 776 | callback(checkIfCertIsPresent(certificate.data)); |
777 | if (!trustedCerts) { | ||
778 | callback(false); | ||
779 | return; | ||
780 | } | ||
781 | |||
782 | const isTrustedCert = trustedCerts.includes( | ||
783 | removeNewLines(certificate.data), | ||
784 | ); | ||
785 | |||
786 | if (isTrustedCert) { | ||
787 | callback(true); | ||
788 | return; | ||
789 | } | ||
790 | |||
791 | callback(false); | ||
792 | }, | 777 | }, |
793 | ); | 778 | ); |
diff --git a/src/jsUtils.ts b/src/jsUtils.ts index 0095028ef..31d6a2121 100644 --- a/src/jsUtils.ts +++ b/src/jsUtils.ts | |||
@@ -30,3 +30,6 @@ export const acceleratorString = ( | |||
30 | prefix: string = '(', | 30 | prefix: string = '(', |
31 | suffix: string = ')', | 31 | suffix: string = ')', |
32 | ) => (index <= 10 ? `${prefix}${keyCombo}+${index % 10}${suffix}` : ''); | 32 | ) => (index <= 10 ? `${prefix}${keyCombo}+${index % 10}${suffix}` : ''); |
33 | |||
34 | export const removeNewLines = (input: string): string => | ||
35 | input.replaceAll(/\r?\n|\r/g, ''); | ||
diff --git a/test/jsUtils.test.ts b/test/jsUtils.test.ts index 138158611..0662c3c48 100644 --- a/test/jsUtils.test.ts +++ b/test/jsUtils.test.ts | |||
@@ -151,4 +151,20 @@ describe('jsUtils', () => { | |||
151 | expect(jsUtils.acceleratorString(11, 'abc')).toEqual(''); | 151 | expect(jsUtils.acceleratorString(11, 'abc')).toEqual(''); |
152 | }); | 152 | }); |
153 | }); | 153 | }); |
154 | |||
155 | describe('removeNewLines', () => { | ||
156 | it('handles unix style new lines', () => { | ||
157 | expect(jsUtils.removeNewLines('abc def\nghi')).toEqual('abc defghi'); | ||
158 | }); | ||
159 | |||
160 | it('handles windows style new lines', () => { | ||
161 | expect(jsUtils.removeNewLines('abc def\r\nghi')).toEqual('abc defghi'); | ||
162 | }); | ||
163 | |||
164 | it('handles new lines in the beginning, middle and ending of the string', () => { | ||
165 | expect(jsUtils.removeNewLines('\nabc def\r\nghi\n')).toEqual( | ||
166 | 'abc defghi', | ||
167 | ); | ||
168 | }); | ||
169 | }); | ||
154 | }); | 170 | }); |