From 7154a309be6710eb66e1fc5e399b5c68daebd445 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Wed, 1 Dec 2021 17:23:47 +0530 Subject: [PATCH] mv widget js from out of subdir --- templates/widget/{js => }/const.ts | 6 +- templates/widget/{js => }/fetchPoWConfig.ts | 2 +- templates/widget/index.ts | 117 ++++++++++-------- templates/widget/js/index.ts | 68 ---------- templates/widget/{js => }/prove.ts | 0 templates/widget/{js => }/sendToParent.ts | 0 templates/widget/{js => }/sendWork.ts | 2 +- templates/widget/{js => }/tests/const.test.ts | 0 templates/widget/{js => }/tests/setupTests.ts | 0 webpack.config.js | 32 ++--- 10 files changed, 84 insertions(+), 143 deletions(-) rename templates/widget/{js => }/const.ts (94%) rename templates/widget/{js => }/fetchPoWConfig.ts (95%) delete mode 100644 templates/widget/js/index.ts rename templates/widget/{js => }/prove.ts (100%) rename templates/widget/{js => }/sendToParent.ts (100%) rename templates/widget/{js => }/sendWork.ts (95%) rename templates/widget/{js => }/tests/const.test.ts (100%) rename templates/widget/{js => }/tests/setupTests.ts (100%) diff --git a/templates/widget/js/const.ts b/templates/widget/const.ts similarity index 94% rename from templates/widget/js/const.ts rename to templates/widget/const.ts index 6e18be20..6edf003e 100644 --- a/templates/widget/js/const.ts +++ b/templates/widget/const.ts @@ -8,7 +8,7 @@ * this program. If not, see for * MIT or for Apache. */ -import LazyElement from "../../utils/lazyElement"; +import LazyElement from "../utils/lazyElement"; /** mcaptcha checkbox ID **/ export const btnId = "widget__verification-checkbox"; @@ -71,10 +71,6 @@ export const messageText = (): messageTextReturn => { const after = new LazyElement(afterID); const during = new LazyElement(duringID); const error = new LazyElement(errorID); - // let before: HTMLElement; - // let after: HTMLElement; - // let during: HTMLElement; - // let error: HTMLElement; /** runner fn to display HTMLElement **/ const showMsg = (e: HTMLElement) => (e.style.display = "block"); diff --git a/templates/widget/js/fetchPoWConfig.ts b/templates/widget/fetchPoWConfig.ts similarity index 95% rename from templates/widget/js/fetchPoWConfig.ts rename to templates/widget/fetchPoWConfig.ts index 06365060..f45da555 100644 --- a/templates/widget/js/fetchPoWConfig.ts +++ b/templates/widget/fetchPoWConfig.ts @@ -9,7 +9,7 @@ * MIT or for Apache. */ -import genJsonPayload from "../../utils/genJsonPayload"; +import genJsonPayload from "../utils/genJsonPayload"; import * as CONST from "./const"; type GetConfigPayload = { diff --git a/templates/widget/index.ts b/templates/widget/index.ts index 6f551672..7cf0e2d2 100644 --- a/templates/widget/index.ts +++ b/templates/widget/index.ts @@ -1,55 +1,68 @@ /* - * Copyright (C) 2021 Aravinth Manivannan + * mCaptcha is a PoW based DoS protection software. + * This is the frontend web component of the mCaptcha system + * Copyright © 2021 Aravinth Manivnanan . * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . + * 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 for + * MIT or for Apache. */ -import "./main.scss"; -//import prove from './runner/prove'; -//import fetchPoWConfig from './runner/fetchPoWConfig'; -//import sendWork from './runner/sendWork'; -//import sendToParent from './runner/sendToParent'; -//import * as CONST from './runner/const'; -// -///** add mcaptcha widget element to DOM */ -//export const register = () => { -// const verificationContainer = ( -// document.querySelector('.widget__verification-container') -// ); -// verificationContainer.style.display = 'flex'; -// -// CONST.btn().addEventListener('click', e => solveCaptchaRunner(e)); -//}; -// -//const solveCaptchaRunner = async (e: Event) => { -// e.preventDefault(); -// // steps: -// -// // 1. hide --before message -// CONST.messageText().before().style.display = 'none'; -// -// // 1. show --during -// CONST.messageText().during().style.display = 'block'; -// // 1. get config -// const config = await fetchPoWConfig(); -// // 2. prove work -// const proof = await prove(config); -// // 3. submit work -// const token = await sendWork(proof); -// // 4. send token -// sendToParent(token); -// // 5. mark checkbox checked -// CONST.btn().checked = true; -//}; -// -//register(); + +import prove from "./prove"; +import fetchPoWConfig from "./fetchPoWConfig"; +import sendWork from "./sendWork"; +import sendToParent from "./sendToParent"; +import * as CONST from "./const"; + +import "../main.scss"; + +let LOCK = false; + +/** add mcaptcha widget element to DOM */ +export const registerVerificationEventHandler = (): void => { + const verificationContainer = ( + document.querySelector(".widget__verification-container") + ); + verificationContainer.style.display = "flex"; + CONST.btn().addEventListener("click", (e) => solveCaptchaRunner(e)); +}; + +export const solveCaptchaRunner = async (e: Event): Promise => { + if (LOCK) { + e.preventDefault(); + return; + } + + try { + LOCK = true; + if (CONST.btn().checked == false) { + CONST.messageText().before(); + LOCK = false; + return; + } + e.preventDefault(); + // steps: + + // 1. show during + CONST.messageText().during(); + // 1. get config + const config = await fetchPoWConfig(); + // 2. prove work + const proof = await prove(config); + // 3. submit work + const token = await sendWork(proof); + // 4. send token + sendToParent(token); + // 5. mark checkbox checked + CONST.btn().checked = true; + CONST.messageText().after(); + LOCK = false; + } catch (e) { + CONST.messageText().error(); + console.error(e); + LOCK = false; + } +}; + +registerVerificationEventHandler(); diff --git a/templates/widget/js/index.ts b/templates/widget/js/index.ts deleted file mode 100644 index 7cf0e2d2..00000000 --- a/templates/widget/js/index.ts +++ /dev/null @@ -1,68 +0,0 @@ -/* - * mCaptcha is a PoW based DoS protection software. - * This is the frontend web component of the mCaptcha system - * Copyright © 2021 Aravinth Manivnanan . - * - * 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 for - * MIT or for Apache. - */ - -import prove from "./prove"; -import fetchPoWConfig from "./fetchPoWConfig"; -import sendWork from "./sendWork"; -import sendToParent from "./sendToParent"; -import * as CONST from "./const"; - -import "../main.scss"; - -let LOCK = false; - -/** add mcaptcha widget element to DOM */ -export const registerVerificationEventHandler = (): void => { - const verificationContainer = ( - document.querySelector(".widget__verification-container") - ); - verificationContainer.style.display = "flex"; - CONST.btn().addEventListener("click", (e) => solveCaptchaRunner(e)); -}; - -export const solveCaptchaRunner = async (e: Event): Promise => { - if (LOCK) { - e.preventDefault(); - return; - } - - try { - LOCK = true; - if (CONST.btn().checked == false) { - CONST.messageText().before(); - LOCK = false; - return; - } - e.preventDefault(); - // steps: - - // 1. show during - CONST.messageText().during(); - // 1. get config - const config = await fetchPoWConfig(); - // 2. prove work - const proof = await prove(config); - // 3. submit work - const token = await sendWork(proof); - // 4. send token - sendToParent(token); - // 5. mark checkbox checked - CONST.btn().checked = true; - CONST.messageText().after(); - LOCK = false; - } catch (e) { - CONST.messageText().error(); - console.error(e); - LOCK = false; - } -}; - -registerVerificationEventHandler(); diff --git a/templates/widget/js/prove.ts b/templates/widget/prove.ts similarity index 100% rename from templates/widget/js/prove.ts rename to templates/widget/prove.ts diff --git a/templates/widget/js/sendToParent.ts b/templates/widget/sendToParent.ts similarity index 100% rename from templates/widget/js/sendToParent.ts rename to templates/widget/sendToParent.ts diff --git a/templates/widget/js/sendWork.ts b/templates/widget/sendWork.ts similarity index 95% rename from templates/widget/js/sendWork.ts rename to templates/widget/sendWork.ts index 70c3d36e..248f5c44 100644 --- a/templates/widget/js/sendWork.ts +++ b/templates/widget/sendWork.ts @@ -9,7 +9,7 @@ * MIT or for Apache. */ -import genJsonPayload from "../../utils/genJsonPayload"; +import genJsonPayload from "../utils/genJsonPayload"; import * as CONST from "./const"; import {Work} from "./prove"; diff --git a/templates/widget/js/tests/const.test.ts b/templates/widget/tests/const.test.ts similarity index 100% rename from templates/widget/js/tests/const.test.ts rename to templates/widget/tests/const.test.ts diff --git a/templates/widget/js/tests/setupTests.ts b/templates/widget/tests/setupTests.ts similarity index 100% rename from templates/widget/js/tests/setupTests.ts rename to templates/widget/tests/setupTests.ts diff --git a/webpack.config.js b/webpack.config.js index 671d8892..525efda5 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,38 +1,38 @@ -'use strict'; -const path = require('path'); -const MiniCssExtractPlugin = require('mini-css-extract-plugin'); -const CssMinimizerPlugin = require('css-minimizer-webpack-plugin'); +"use strict"; +const path = require("path"); +const MiniCssExtractPlugin = require("mini-css-extract-plugin"); +const CssMinimizerPlugin = require("css-minimizer-webpack-plugin"); //const WasmPackPlugin = require('@wasm-tool/wasm-pack-plugin'); module.exports = { //devtool: 'inline-source-map', //mode: 'development', - mode: 'production', + mode: "production", entry: { - bundle: './templates/index.ts', - mobile: './templates/mobile.ts', - verificationWidget: './templates/widget/js/index.ts', + bundle: "./templates/index.ts", + mobile: "./templates/mobile.ts", + verificationWidget: "./templates/widget/index.ts", }, output: { - filename: '[name].js', - path: path.resolve(__dirname, './static/cache/bundle/'), + filename: "[name].js", + path: path.resolve(__dirname, "./static/cache/bundle/"), }, module: { rules: [ { test: /\.tsx?$/, - loader: 'ts-loader', + loader: "ts-loader", }, { test: /\.s[ac]ss$/i, use: [ MiniCssExtractPlugin.loader, - 'css-loader', + "css-loader", { - loader: 'sass-loader', + loader: "sass-loader", options: { - implementation: require('dart-sass'), + implementation: require("dart-sass"), }, }, ], @@ -40,7 +40,7 @@ module.exports = { ], }, resolve: { - extensions: ['.ts', '.tsx', '.js'], + extensions: [".ts", ".tsx", ".js"], }, plugins: [ @@ -53,7 +53,7 @@ module.exports = { optimization: { minimizer: [ // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line - `...`, + "...", new CssMinimizerPlugin(), ], },