frontend linting

This commit is contained in:
realaravinth
2021-10-08 15:24:29 +05:30
parent f7afc72d81
commit 53720ff740
91 changed files with 2158 additions and 1677 deletions

View File

@@ -14,7 +14,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import './main.scss';
import "./main.scss";
//import prove from './runner/prove';
//import fetchPoWConfig from './runner/fetchPoWConfig';
//import sendWork from './runner/sendWork';

View File

@@ -8,19 +8,19 @@
* this program. If not, see <https://spdx.org/licenses/MIT.html> for
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
*/
import LazyElement from '../../utils/lazyElement';
import LazyElement from "../../utils/lazyElement";
/** mcaptcha checkbox ID **/
export const btnId = 'widget__verification-checkbox';
export const btnId = "widget__verification-checkbox";
/** get sitekey */
export const sitekey = () => {
export const sitekey = (): string => {
let sitekey;
return (() => {
if (sitekey === null || sitekey === undefined) {
sitekey = new URL(window.location.href).searchParams.get('sitekey');
sitekey = new URL(window.location.href).searchParams.get("sitekey");
if (sitekey === null || sitekey === undefined) {
throw new Error(`Define sitekey in query parameter`);
throw new Error("Define sitekey in query parameter");
}
}
return sitekey;
@@ -29,8 +29,8 @@ export const sitekey = () => {
/** mCaptcha API routes */
export const ROUTES = (() => {
const getConfig = '/api/v1/pow/config';
const verififyPoW = '/api/v1/pow/verify';
const getConfig = "/api/v1/pow/config";
const verififyPoW = "/api/v1/pow/verify";
return {
/** get URL to fetch PoW configuration */
@@ -41,24 +41,31 @@ export const ROUTES = (() => {
})();
/** get mCaptcha verifify checkbox button */
export const btn = () => {
export const btn = (): HTMLInputElement => {
let btn;
return (() => {
if (btn === null || btn === undefined) {
btn = <HTMLInputElement>document.getElementById(btnId);
if (btn === null || btn === undefined) {
throw new Error(`mCaptcha button not found)`);
throw new Error("mCaptcha button not found)");
}
}
return btn;
})();
};
export const messageText = () => {
const beforeID = 'widget__verification-text--before';
const duringID = 'widget__verification-text--during';
const errorID = 'widget__verification-text--error';
const afterID = 'widget__verification-text--after';
type messageTextReturn = {
before: () => void;
after: () => void;
during: () => void;
error: () => void;
};
export const messageText = (): messageTextReturn => {
const beforeID = "widget__verification-text--before";
const duringID = "widget__verification-text--during";
const errorID = "widget__verification-text--error";
const afterID = "widget__verification-text--after";
const before = new LazyElement(beforeID);
const after = new LazyElement(afterID);
@@ -70,9 +77,9 @@ export const messageText = () => {
// let error: HTMLElement;
/** runner fn to display HTMLElement **/
const showMsg = (e: HTMLElement) => (e.style.display = 'block');
const showMsg = (e: HTMLElement) => (e.style.display = "block");
/** runner fn to hide HTMLElement **/
const hideMsg = (e: HTMLElement) => (e.style.display = 'none');
const hideMsg = (e: HTMLElement) => (e.style.display = "none");
return {
/** display "before" message **/
@@ -109,4 +116,4 @@ export const messageText = () => {
};
};
export const inputId = 'mcaptcha-response';
export const inputId = "mcaptcha-response";

View File

@@ -9,8 +9,8 @@
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
*/
import genJsonPayload from './utils/genJsonPayload';
import * as CONST from './const';
import genJsonPayload from "../../utils/genJsonPayload";
import * as CONST from "./const";
type GetConfigPayload = {
key: string;
@@ -26,22 +26,18 @@ export type PoWConfig = {
* fetch proof-of-work configuration
* @returns {PoWConfig} pow config
* */
export const fetchPoWConfig = async () => {
try {
const payload: GetConfigPayload = {
key: CONST.sitekey(),
};
export const fetchPoWConfig = async (): Promise<PoWConfig> => {
const payload: GetConfigPayload = {
key: CONST.sitekey(),
};
const res = await fetch(CONST.ROUTES.getConfig, genJsonPayload(payload));
if (res.ok) {
const config: PoWConfig = await res.json();
return config;
} else {
const err = await res.json();
throw new Error(err);
}
} catch (err) {
throw err;
const res = await fetch(CONST.ROUTES.getConfig, genJsonPayload(payload));
if (res.ok) {
const config: PoWConfig = await res.json();
return config;
} else {
const err = await res.json();
throw new Error(err);
}
};

View File

@@ -9,26 +9,26 @@
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
*/
import prove from './prove';
import fetchPoWConfig from './fetchPoWConfig';
import sendWork from './sendWork';
import sendToParent from './sendToParent';
import * as CONST from './const';
import prove from "./prove";
import fetchPoWConfig from "./fetchPoWConfig";
import sendWork from "./sendWork";
import sendToParent from "./sendToParent";
import * as CONST from "./const";
import '../main.scss';
import "../main.scss";
let LOCK = false;
/** add mcaptcha widget element to DOM */
export const registerVerificationEventHandler = () => {
export const registerVerificationEventHandler = (): void => {
const verificationContainer = <HTMLElement>(
document.querySelector('.widget__verification-container')
document.querySelector(".widget__verification-container")
);
verificationContainer.style.display = 'flex';
CONST.btn().addEventListener('click', e => solveCaptchaRunner(e));
verificationContainer.style.display = "flex";
CONST.btn().addEventListener("click", (e) => solveCaptchaRunner(e));
};
export const solveCaptchaRunner = async (e: Event) => {
export const solveCaptchaRunner = async (e: Event): Promise<void> => {
if (LOCK) {
e.preventDefault();
return;

View File

@@ -9,9 +9,9 @@
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
*/
import {gen_pow} from 'mcaptcha-browser';
import {PoWConfig} from './fetchPoWConfig';
import * as CONST from './const';
import { gen_pow } from "mcaptcha-browser";
import { PoWConfig } from "./fetchPoWConfig";
import * as CONST from "./const";
export type Work = {
result: string;
@@ -30,26 +30,22 @@ type WasmWork = {
* @param {PoWConfig} config - the proof-of-work configuration using which
* work needs to be computed
* */
const prove = async (config: PoWConfig) => {
try {
const proofString = gen_pow(
config.salt,
config.string,
config.difficulty_factor,
);
const proof: WasmWork = JSON.parse(proofString);
const prove = async (config: PoWConfig): Promise<Work> => {
const proofString = gen_pow(
config.salt,
config.string,
config.difficulty_factor
);
const proof: WasmWork = JSON.parse(proofString);
const res: Work = {
key: CONST.sitekey(),
string: config.string,
nonce: proof.nonce,
result: proof.result,
};
const res: Work = {
key: CONST.sitekey(),
string: config.string,
nonce: proof.nonce,
result: proof.result,
};
return res;
} catch (err) {
throw err;
}
return res;
};
export default prove;

View File

@@ -8,15 +8,15 @@
* this program. If not, see <https://spdx.org/licenses/MIT.html> for
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
*/
import {Token} from './sendWork';
import {Token} from "./sendWork";
/**
* send pow validation token as message to parant of the iframe
* @param {Token} token: token received from mCaptcha service
* upon successful PoW validation
* */
export const sendToParent = (token: Token) => {
window.parent.postMessage(token, '*');
export const sendToParent = (token: Token): void => {
window.parent.postMessage(token, "*");
// TODO set origin. Make parent send origin as query parameter
// or as a message to iframe
};

View File

@@ -9,19 +9,19 @@
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
*/
import genJsonPayload from './utils/genJsonPayload';
import * as CONST from './const';
import {Work} from './prove';
import genJsonPayload from "../../utils/genJsonPayload";
import * as CONST from "./const";
import {Work} from "./prove";
export type Token = {
token: string;
};
export const sendWork = async (payload: Work) => {
export const sendWork = async (payload: Work): Promise<Token> => {
try {
const res = await fetch(CONST.ROUTES.verififyPoW, genJsonPayload(payload));
if (res.ok) {
console.debug('work verified');
console.debug("work verified");
const token: Token = await res.json();
console.debug(`token ${token.token}`);
return token;

View File

@@ -8,13 +8,13 @@
* this program. If not, see <https://spdx.org/licenses/MIT.html> for
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
*/
import * as CONST from '../const';
import * as CONST from "../const";
import {getBaseHtml, sitekey, checkbox} from './setupTests';
import * as TESTElements from './setupTests';
import {getBaseHtml, sitekey, checkbox} from "./setupTests";
import * as TESTElements from "./setupTests";
it('const works', () => {
const body = document.querySelector('body');
it("const works", () => {
const body = document.querySelector("body");
const container = getBaseHtml();
body.appendChild(container);
expect(CONST.sitekey()).toBe(sitekey);
@@ -22,29 +22,29 @@ it('const works', () => {
// display after
CONST.messageText().after();
expect(TESTElements.afterMsg.style.display).toBe('block');
expect(TESTElements.beforeMsg.style.display).toBe('none');
expect(TESTElements.duringMsg.style.display).toBe('none');
expect(TESTElements.errorMsg.style.display).toBe('none');
expect(TESTElements.afterMsg.style.display).toBe("block");
expect(TESTElements.beforeMsg.style.display).toBe("none");
expect(TESTElements.duringMsg.style.display).toBe("none");
expect(TESTElements.errorMsg.style.display).toBe("none");
// display before
CONST.messageText().before();
expect(TESTElements.afterMsg.style.display).toBe('none');
expect(TESTElements.beforeMsg.style.display).toBe('block');
expect(TESTElements.duringMsg.style.display).toBe('none');
expect(TESTElements.errorMsg.style.display).toBe('none');
expect(TESTElements.afterMsg.style.display).toBe("none");
expect(TESTElements.beforeMsg.style.display).toBe("block");
expect(TESTElements.duringMsg.style.display).toBe("none");
expect(TESTElements.errorMsg.style.display).toBe("none");
// display during
CONST.messageText().during();
expect(TESTElements.afterMsg.style.display).toBe('none');
expect(TESTElements.beforeMsg.style.display).toBe('none');
expect(TESTElements.duringMsg.style.display).toBe('block');
expect(TESTElements.errorMsg.style.display).toBe('none');
expect(TESTElements.afterMsg.style.display).toBe("none");
expect(TESTElements.beforeMsg.style.display).toBe("none");
expect(TESTElements.duringMsg.style.display).toBe("block");
expect(TESTElements.errorMsg.style.display).toBe("none");
// display error
CONST.messageText().error();
expect(TESTElements.afterMsg.style.display).toBe('none');
expect(TESTElements.beforeMsg.style.display).toBe('none');
expect(TESTElements.duringMsg.style.display).toBe('none');
expect(TESTElements.errorMsg.style.display).toBe('block');
expect(TESTElements.afterMsg.style.display).toBe("none");
expect(TESTElements.beforeMsg.style.display).toBe("none");
expect(TESTElements.duringMsg.style.display).toBe("none");
expect(TESTElements.errorMsg.style.display).toBe("block");
});

View File

@@ -8,28 +8,28 @@
* this program. If not, see <https://spdx.org/licenses/MIT.html> for
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
*/
import * as CONST from '../const';
import * as CONST from "../const";
export const sitekey = 'imbatman';
export const sitekey = "imbatman";
export const checkbox = <HTMLInputElement>document.createElement('input');
checkbox.type = 'checkbox';
export const checkbox = <HTMLInputElement>document.createElement("input");
checkbox.type = "checkbox";
checkbox.id = CONST.btnId;
const getMessages = (state: string) => {
const msg = <HTMLElement>document.createElement('span');
const msg = <HTMLElement>document.createElement("span");
msg.id = `widget__verification-text--${state}`;
return msg;
};
export const beforeMsg = getMessages('before');
export const afterMsg = getMessages('after');
export const duringMsg = getMessages('during');
export const errorMsg = getMessages('error');
export const beforeMsg = getMessages("before");
export const afterMsg = getMessages("after");
export const duringMsg = getMessages("during");
export const errorMsg = getMessages("error");
/** get base HTML with empty mCaptcha container */
export const getBaseHtml = () => {
const form = <HTMLFormElement>document.createElement('form');
export const getBaseHtml = (): HTMLFormElement => {
const form = <HTMLFormElement>document.createElement("form");
form.appendChild(checkbox);
form.appendChild(beforeMsg);
form.appendChild(duringMsg);

View File

@@ -1,30 +0,0 @@
/*
* mCaptcha is a PoW based DoS protection software.
* This is the frontend web component of the mCaptcha system
* Copyright © 2021 Aravinth Manivnanan <realaravinth@batsense.net>.
*
* Use of this source code is governed by Apache 2.0 or MIT license.
* You shoud have received a copy of MIT and Apache 2.0 along with
* this program. If not, see <https://spdx.org/licenses/MIT.html> for
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
*/
import genJsonPayload from './genJsonPayload';
'use strict';
const payload = {
username: 'Jhon',
};
const value = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
};
it('getFromUrl workds', () => {
expect(genJsonPayload(payload)).toEqual(value);
});

View File

@@ -1,23 +0,0 @@
/*
* mCaptcha is a PoW based DoS protection software.
* This is the frontend web component of the mCaptcha system
* Copyright © 2021 Aravinth Manivnanan <realaravinth@batsense.net>.
*
* Use of this source code is governed by Apache 2.0 or MIT license.
* You shoud have received a copy of MIT and Apache 2.0 along with
* this program. If not, see <https://spdx.org/licenses/MIT.html> for
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
*/
const genJsonPayload = (payload: any) => {
const value = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
};
return value;
};
export default genJsonPayload;