From 47c7fd42c76a78c23be79dbc9bf34a498cad22d6 Mon Sep 17 00:00:00 2001 From: Rizwan Mustafa Date: Fri, 25 Mar 2022 19:36:04 +0500 Subject: [PATCH] Improve webpack - Reduced filesize by 18% by converting from node-object-hash to object-hash (#2768) rizwanmustafa * Refreshed the PR * not sending stringified result anymore * removed length check Co-authored-by: Miodec --- backend/api/controllers/result.ts | 11 +++------- backend/package-lock.json | 19 ++--------------- backend/package.json | 2 +- frontend/package-lock.json | 28 ++++++++++++------------- frontend/package.json | 2 +- frontend/src/scripts/test/test-logic.ts | 16 +++----------- frontend/tsconfig.json | 3 ++- frontend/webpack/config.base.js | 13 +----------- package-lock.json | 27 ++++++++++++++++++++++++ package.json | 4 ++++ 10 files changed, 58 insertions(+), 67 deletions(-) diff --git a/backend/api/controllers/result.ts b/backend/api/controllers/result.ts index 936abc362..27e31e063 100644 --- a/backend/api/controllers/result.ts +++ b/backend/api/controllers/result.ts @@ -3,7 +3,7 @@ import UserDAO from "../../dao/user"; import PublicStatsDAO from "../../dao/public-stats"; import BotDAO from "../../dao/bot"; import { roundTo2, stdDev } from "../../utils/misc"; -import node_object_hash from "node-object-hash"; +import objectHash from "object-hash"; import Logger from "../../utils/logger"; import "dotenv/config"; import { MonkeyResponse } from "../../utils/monkey-response"; @@ -18,8 +18,6 @@ import MonkeyStatusCodes from "../../constants/monkey-status-codes"; import { incrementResult } from "../../utils/prometheus"; import George from "../../tasks/george"; -const objecthash = node_object_hash().hash; - try { if (anticheatImplemented() === false) throw new Error("undefined"); console.log("Anticheat module loaded"); @@ -80,12 +78,9 @@ class ResultController { const resulthash = result.hash; delete result.hash; delete result.stringified; - if ( - req.ctx.configuration.resultObjectHashCheck.enabled && - resulthash.length === 64 - ) { + if (req.ctx.configuration.resultObjectHashCheck.enabled) { //if its not 64 that means client is still using old hashing package - const serverhash = objecthash(result); + const serverhash = objectHash(result); if (serverhash !== resulthash) { Logger.log( "incorrect_result_hash", diff --git a/backend/package-lock.json b/backend/package-lock.json index 9ada3a62e..de56aeb3f 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -23,8 +23,8 @@ "lodash": "4.17.21", "mongodb": "4.4.0", "node-fetch": "2.6.7", - "node-object-hash": "2.3.10", "nodemon": "2.0.7", + "object-hash": "^3.0.0", "path": "0.12.7", "prom-client": "14.0.1", "simple-git": "2.45.1", @@ -3574,14 +3574,6 @@ "node-gyp-build-test": "build-test.js" } }, - "node_modules/node-object-hash": { - "version": "2.3.10", - "resolved": "https://registry.npmjs.org/node-object-hash/-/node-object-hash-2.3.10.tgz", - "integrity": "sha512-jY5dPJzw6NHd/KPSfPKJ+IHoFS81/tJ43r34ZeNMXGzCOM8jwQDCD12HYayKIB6MuznrnqIYy2e891NA2g0ibA==", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/nodemon": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.7.tgz", @@ -3683,7 +3675,6 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz", "integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==", - "optional": true, "engines": { "node": ">= 6" } @@ -8005,11 +7996,6 @@ "integrity": "sha512-iWjXZvmboq0ja1pUGULQBexmxq8CV4xBhX7VDOTbL7ZR4FOowwY/VOtRxBN/yKxmdGoIp4j5ysNT4u3S2pDQ3Q==", "optional": true }, - "node-object-hash": { - "version": "2.3.10", - "resolved": "https://registry.npmjs.org/node-object-hash/-/node-object-hash-2.3.10.tgz", - "integrity": "sha512-jY5dPJzw6NHd/KPSfPKJ+IHoFS81/tJ43r34ZeNMXGzCOM8jwQDCD12HYayKIB6MuznrnqIYy2e891NA2g0ibA==" - }, "nodemon": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.7.tgz", @@ -8082,8 +8068,7 @@ "object-hash": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz", - "integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==", - "optional": true + "integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==" }, "object-inspect": { "version": "1.12.0", diff --git a/backend/package.json b/backend/package.json index 4d8f62e7b..7a92a5d06 100644 --- a/backend/package.json +++ b/backend/package.json @@ -29,8 +29,8 @@ "lodash": "4.17.21", "mongodb": "4.4.0", "node-fetch": "2.6.7", - "node-object-hash": "2.3.10", "nodemon": "2.0.7", + "object-hash": "^3.0.0", "path": "0.12.7", "prom-client": "14.0.1", "simple-git": "2.45.1", diff --git a/frontend/package-lock.json b/frontend/package-lock.json index f10b04169..5fa0f599f 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -20,7 +20,7 @@ "firebase": "9.6.0", "howler": "^2.2.1", "html2canvas": "1.4.1", - "node-object-hash": "2.3.10", + "object-hash": "^3.0.0", "stemmer": "2.0.0" }, "devDependencies": { @@ -8601,14 +8601,6 @@ "node": ">= 6.13.0" } }, - "node_modules/node-object-hash": { - "version": "2.3.10", - "resolved": "https://registry.npmjs.org/node-object-hash/-/node-object-hash-2.3.10.tgz", - "integrity": "sha512-jY5dPJzw6NHd/KPSfPKJ+IHoFS81/tJ43r34ZeNMXGzCOM8jwQDCD12HYayKIB6MuznrnqIYy2e891NA2g0ibA==", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/node-releases": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.2.tgz", @@ -8799,6 +8791,14 @@ "node": ">=0.10.0" } }, + "node_modules/object-hash": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz", + "integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==", + "engines": { + "node": ">= 6" + } + }, "node_modules/object-is": { "version": "1.1.5", "resolved": "https://registry.npmjs.org/object-is/-/object-is-1.1.5.tgz", @@ -20387,11 +20387,6 @@ "integrity": "sha512-08ARB91bUi6zNKzVmaj3QO7cr397uiDT2nJ63cHjyNtCTWIgvS47j3eT0WfzUwS9+6Z5YshRaoasFkXCKrIYbA==", "dev": true }, - "node-object-hash": { - "version": "2.3.10", - "resolved": "https://registry.npmjs.org/node-object-hash/-/node-object-hash-2.3.10.tgz", - "integrity": "sha512-jY5dPJzw6NHd/KPSfPKJ+IHoFS81/tJ43r34ZeNMXGzCOM8jwQDCD12HYayKIB6MuznrnqIYy2e891NA2g0ibA==" - }, "node-releases": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.2.tgz", @@ -20540,6 +20535,11 @@ } } }, + "object-hash": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz", + "integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==" + }, "object-is": { "version": "1.1.5", "resolved": "https://registry.npmjs.org/object-is/-/object-is-1.1.5.tgz", diff --git a/frontend/package.json b/frontend/package.json index bb5e73d71..2c9ce3da0 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -60,7 +60,7 @@ "firebase": "9.6.0", "howler": "^2.2.1", "html2canvas": "1.4.1", - "node-object-hash": "2.3.10", + "object-hash": "^3.0.0", "stemmer": "2.0.0" } } diff --git a/frontend/src/scripts/test/test-logic.ts b/frontend/src/scripts/test/test-logic.ts index a413a5e5b..a9b685322 100644 --- a/frontend/src/scripts/test/test-logic.ts +++ b/frontend/src/scripts/test/test-logic.ts @@ -49,12 +49,10 @@ import * as ConfigEvent from "../observables/config-event"; import * as TimerEvent from "../observables/timer-event"; import * as Last10Average from "../elements/last-10-average"; import * as Monkey from "./monkey"; -import NodeObjectHash from "node-object-hash"; +import objectHash from "object-hash"; import * as AnalyticsController from "../controllers/analytics-controller"; import { Auth } from "../firebase"; -const objecthash = NodeObjectHash().hash; - let failReason = ""; export let notSignedInLastResult: MonkeyTypes.Result | null = @@ -68,7 +66,7 @@ export function setNotSignedInUid(uid: string): void { if (notSignedInLastResult === null) return; notSignedInLastResult.uid = uid; delete notSignedInLastResult.hash; - notSignedInLastResult.hash = objecthash(notSignedInLastResult); + notSignedInLastResult.hash = objectHash(notSignedInLastResult); } let spanishSentenceTracker = ""; @@ -1488,15 +1486,7 @@ export async function finish(difficultyFailed = false): Promise { completedEvent.challenge = ChallengeContoller.verify(completedEvent); if (completedEvent.challenge === null) delete completedEvent?.challenge; - completedEvent.hash = objecthash(completedEvent); - - if ( - (completedEvent.mode === "words" && completedEvent.mode2 == 10) || - completedEvent.mode === "custom" - ) { - //@ts-ignore - completedEvent.stringified = JSON.stringify(completedEvent); - } + completedEvent.hash = objectHash(completedEvent); const response = await Ape.results.save(completedEvent); diff --git a/frontend/tsconfig.json b/frontend/tsconfig.json index f4e79dd98..3034dd549 100644 --- a/frontend/tsconfig.json +++ b/frontend/tsconfig.json @@ -10,7 +10,8 @@ "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */, "types": [ "jquery", - "select2" + "select2", + "node" ] /* Specify type package names to be included without being referenced in a source file. */, "allowUmdGlobalAccess": true /* Allow accessing UMD globals from modules. */, "resolveJsonModule": true /* Enable importing .json files */, diff --git a/frontend/webpack/config.base.js b/frontend/webpack/config.base.js index 71999f830..0d0bfce9c 100644 --- a/frontend/webpack/config.base.js +++ b/frontend/webpack/config.base.js @@ -11,18 +11,7 @@ const BASE_CONFIG = { entry: { monkeytype: resolve(__dirname, "../src/scripts/index.ts"), }, - resolve: { - fallback: { - crypto: require.resolve("crypto-browserify"), - stream: require.resolve("stream-browserify"), - buffer: require.resolve("buffer"), - "bn.js": require.resolve("bn.js"), - }, - alias: { - "bn.js": resolve(__dirname, "node_modules/bn.js/lib/bn.js"), - }, - extensions: [".ts", ".js"], - }, + resolve: { extensions: [".ts", ".js"] }, output: { filename: "./js/[name].[chunkhash:8].js", path: resolve(__dirname, "../public/"), diff --git a/package-lock.json b/package-lock.json index f6aee5bf6..b87c9b24d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,10 @@ "name": "monkeytype", "version": "1.5.6", "license": "GPL-3.0", + "dependencies": { + "@types/object-hash": "^2.2.1", + "object-hash": "^3.0.0" + }, "devDependencies": { "@typescript-eslint/eslint-plugin": "^5.11.0", "@typescript-eslint/parser": "^5.11.0", @@ -218,6 +222,11 @@ "integrity": "sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==", "dev": true }, + "node_modules/@types/object-hash": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/@types/object-hash/-/object-hash-2.2.1.tgz", + "integrity": "sha512-i/rtaJFCsPljrZvP/akBqEwUP2y5cZLOmvO+JaYnz01aPknrQ+hB5MRcO7iqCUsFaYfTG8kGfKUyboA07xeDHQ==" + }, "node_modules/@types/parse-json": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.0.tgz", @@ -1878,6 +1887,14 @@ "node": ">=8" } }, + "node_modules/object-hash": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz", + "integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==", + "engines": { + "node": ">= 6" + } + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -3054,6 +3071,11 @@ "integrity": "sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==", "dev": true }, + "@types/object-hash": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/@types/object-hash/-/object-hash-2.2.1.tgz", + "integrity": "sha512-i/rtaJFCsPljrZvP/akBqEwUP2y5cZLOmvO+JaYnz01aPknrQ+hB5MRcO7iqCUsFaYfTG8kGfKUyboA07xeDHQ==" + }, "@types/parse-json": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.0.tgz", @@ -4298,6 +4320,11 @@ "path-key": "^3.0.0" } }, + "object-hash": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz", + "integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==" + }, "once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", diff --git a/package.json b/package.json index b8e3c58b2..7ff064dd2 100644 --- a/package.json +++ b/package.json @@ -41,5 +41,9 @@ "hooks": { "pre-commit": "pretty-quick --staged && npm run lint" } + }, + "dependencies": { + "@types/object-hash": "^2.2.1", + "object-hash": "^3.0.0" } }