From b4c95c6fb447b2d2de81be54344a9b96af8198b9 Mon Sep 17 00:00:00 2001 From: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> Date: Sun, 10 Sep 2023 18:08:45 -0600 Subject: [PATCH] feat(mv3): :heavy_plus_sign: Adding hash function to generate predictable ids. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> --- add-on/src/lib/redirect-handler/blockOrObserve.ts | 7 +++++-- package-lock.json | 13 +++++++++++++ package.json | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/add-on/src/lib/redirect-handler/blockOrObserve.ts b/add-on/src/lib/redirect-handler/blockOrObserve.ts index e73289c87..93fbd9954 100644 --- a/add-on/src/lib/redirect-handler/blockOrObserve.ts +++ b/add-on/src/lib/redirect-handler/blockOrObserve.ts @@ -1,4 +1,5 @@ import debug from 'debug' +import { fastHashCode } from 'fast-hash-code' import browser from 'webextension-polyfill' import { CompanionState } from '../../types/companion.js' import { IFilter, IRegexFilter, RegexFilter } from './baseRegexFilter.js' @@ -281,8 +282,10 @@ function saveAndGenerateRule ( regexSubstitution: string, excludedInitiatorDomains: string[] = [] ): browser.DeclarativeNetRequest.Rule { - // We need to generate a random ID for the rule. - const id = Math.floor(Math.random() * 29999) + // We need to generate a positive number as an id. + const id = fastHashCode(`${regexFilter}:${regexSubstitution}:${excludedInitiatorDomains.join(':')}`, { + forcePositive: true + }) // We need to save the regex filter and ID to check if the rule already exists later. savedRegexFilters.set(regexFilter, { id, regexSubstitution }) return generateAddRule(id, regexFilter, regexSubstitution, excludedInitiatorDomains) diff --git a/package-lock.json b/package-lock.json index b147e047e..abecff204 100644 --- a/package-lock.json +++ b/package-lock.json @@ -58,6 +58,7 @@ "css-loader": "6.7.2", "download-cli": "1.1.1", "exponential-backoff": "^3.1.0", + "fast-hash-code": "^2.1.0", "file-loader": "6.2.0", "fs-promise": "2.0.3", "get-firefox": "5.0.0", @@ -8869,6 +8870,12 @@ "node": ">=8.6.0" } }, + "node_modules/fast-hash-code": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/fast-hash-code/-/fast-hash-code-2.1.0.tgz", + "integrity": "sha512-hJ/kSGwlKevFdK7ff1wv6H5m5zINYnScp2/uX4G24OCvGvVM/3/UmIpdNkMW3x6T6CMSN1TG94nMSjiqHHVg8A==", + "dev": true + }, "node_modules/fast-json-patch": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/fast-json-patch/-/fast-json-patch-2.2.1.tgz", @@ -28064,6 +28071,12 @@ "micromatch": "^4.0.4" } }, + "fast-hash-code": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/fast-hash-code/-/fast-hash-code-2.1.0.tgz", + "integrity": "sha512-hJ/kSGwlKevFdK7ff1wv6H5m5zINYnScp2/uX4G24OCvGvVM/3/UmIpdNkMW3x6T6CMSN1TG94nMSjiqHHVg8A==", + "dev": true + }, "fast-json-patch": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/fast-json-patch/-/fast-json-patch-2.2.1.tgz", diff --git a/package.json b/package.json index 79fa3129e..8f0cad3bd 100644 --- a/package.json +++ b/package.json @@ -111,6 +111,7 @@ "css-loader": "6.7.2", "download-cli": "1.1.1", "exponential-backoff": "^3.1.0", + "fast-hash-code": "^2.1.0", "file-loader": "6.2.0", "fs-promise": "2.0.3", "get-firefox": "5.0.0",