Skip to content

Commit a81f545

Browse files
whizzzkidSgtPookilidel
authored
fix(recovery): Recovery Rules should reset (#1266)
* feat(mv3): ✨ Patching countly-sdk-web Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * feat(mv3): ✨ Implementing Custom Async Store. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * chore(mv3): 🩹 Hooking everything up together. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(mv3): Countly Patching + ignite-metrics@2.0.0 Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(mv3): 🩹 Patching the Patch Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix: tests Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix: lint Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(mv3): ♻️ Refactoring `supportsBlock` Checks. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(mv3): Regex Bug Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * feat: Migrating blocking redirection test to observing redirection test Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(mv3): 🔧 Fixing the mocha-setup. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(mv3): ♻️ Moving Setup Files. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(mv3): gateway-redirect tests now fixed. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix: 🩹 Patching error messages Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(patch): countly-web-sdk * fix(patch): :pin: Pinning countly-web-sdk to 23.2.2 * fix(mv3): 💄 Fixing Lint Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * feat: protocol-handler-redirection-tests Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * feat: more tests fixed Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix: More tests Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix: lint fix Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * test: merge mocha-setup files (#1246) * test: merge mocha-setup files * test(helper): add mv3-test-enabled helper * test(setup): remove duplicate setup for mv3/mv2 * test(mv3): merge mv3 tests into mv2 test files (#1247) * test(mv3): merge mv3 tests into mv2 test files * chore(lint): fix lint failures * test(supportsBlock): cleanup test * test: migrating some tests * test(redirect): mv3 tests use same code as mv2 * test(redirect): mv3 test cleanup * test(protohandler): mv3 tests use same code as mv2 * test(helper): make isMv3 flag a boolean * test: fix after merge * test: fix after merge * fix: typerrors for localstorage Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix: Updating test:functional_MV3 command. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix: setup Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(test): Fixing tests Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(lint): Becuase Ofcourse * feat(test): scaffolding mv3 + mv2 calls in a single check. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * feat(test): unskipping and upgrading dnslink tests to mv3 Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * feat(test): Upgrading workaround tests to MV3 Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(test): removing all skips with better checks. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(test): ♻️ Refactoring tests and removing redundant calls. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(test): More Dryer Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(test): one more Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(test): ✏️ Renaming isMv3TestingEnabled -> isManifestV3 Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(test): refactor expectNoRedirect Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(test): ♻️ Refactoring more. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix: replacing checks to undefined Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix: renaming expectNoRedirect -> ensureNoRedirect Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(test): Adding missing JSDoc Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(test): 🤷 how did this get removed. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(test): 🗑️ removed. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(test): Suggestion * fix(test): 🩹 Bad Merge Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(test): sequential expectNoRedirect * Update add-on/src/lib/redirect-handler/blockOrObserve.ts Co-authored-by: Marcin Rataj <lidel@lidel.org> * fix(mv3): 🔧 Modifying the default local redirect behaviour. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(mv3): 🔧 Modifying the default local redirect behaviour. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(mv3): 🐛 Making rules less greedy Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(mv3): ✨ Dynamic Rules for subdomain gateways. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(types): Adding ambient types for is-ipfs. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(test): Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(test): helper Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * feat(mv3): less greedy rules Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * feat: Adding simpler regex for redirects from similar namespaces. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(lint): 🚨 Warnings Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * feat(mv3): Better Default Rules (#1260) * refactor(mv3): blockOrRequest code Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * refactor(mv3): Port Logic for Default Rules is more robust. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * feat(test): Adding tests for default rule logic. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> --------- Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * Update add-on/src/lib/redirect-handler/blockOrObserve.ts * fix(docs): ✏️ Adding comments Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * refactor(regexFilters): ✨ Adding a base class for regexFilters. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * refactor(regexFilters): ♻️ Moving subdomain filter to a subclass Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * refactor(regexFilters): ♻️ Moving namespace filter to a subclass Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * refactor(regexFilters): ♻️ Moving common filter to a subclass Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * feat(regexFilters): ✨ Hooking Up All together Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(lint): ✏️ Lint Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(regexFilters): ✏️ Updating message. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(rename): ✏️ CommonPatterRedirectRegexFilter -> CommonPatternRedirectRegexFilter Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(regexFilters): ♻️ Refactor to remove call to super Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix: make _canHandle private Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix: ⚡ Fix math.min on every loop. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(recovery): reset rules Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(recovery): ♻️ Refactor messaging logic Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(recovery): 🐛 Cleanup Logic Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(mv3): 🐛 fix toggle site integration. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(mv3): 🩹 Hard coding wait as the browser is not cooperative. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> --------- Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> Co-authored-by: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Co-authored-by: Marcin Rataj <lidel@lidel.org>
1 parent 7be2c23 commit a81f545

File tree

6 files changed

+55
-52
lines changed

6 files changed

+55
-52
lines changed

add-on/src/lib/ipfs-companion.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { createRequestModifier } from './ipfs-request.js'
2222
import createNotifier from './notifier.js'
2323
import { runPendingOnInstallTasks } from './on-installed.js'
2424
import { guiURLString, migrateOptions, optionDefaults, safeURL, storeMissingOptions } from './options.js'
25-
import { getExtraInfoSpec } from './redirect-handler/blockOrObserve.js'
25+
import { cleanupRules, getExtraInfoSpec } from './redirect-handler/blockOrObserve.js'
2626
import createRuntimeChecks from './runtime-checks.js'
2727
import { initState, offlinePeerCount } from './state.js'
2828

@@ -521,6 +521,7 @@ export default async function init (inQuickImport = false) {
521521
try {
522522
const oldColor = colorArraytoHex(await browser.action.getBadgeBackgroundColor({}))
523523
if (badgeColor !== oldColor) {
524+
await cleanupRules(true)
524525
await browser.action.setBadgeBackgroundColor({ color: badgeColor })
525526
await setBrowserActionIcon(badgeIcon)
526527
}
@@ -577,7 +578,7 @@ export default async function init (inQuickImport = false) {
577578
}
578579
}
579580

580-
async function onStorageChange (changes, area) {
581+
async function onStorageChange (changes) {
581582
let shouldReloadExtension = false
582583
let shouldRestartIpfsClient = false
583584
let shouldStopIpfsClient = false
@@ -696,6 +697,8 @@ export default async function init (inQuickImport = false) {
696697
browser.tabs.reload() // async reload of options page to keep it alive
697698
await browser.runtime.reload()
698699
}
700+
log('storage change processed')
701+
699702
// Post update to Browser Action (if exists) -- this gives UX a snappy feel
700703
await sendStatusUpdateToBrowserAction()
701704
}

add-on/src/lib/redirect-handler/blockOrObserve.ts

+38-38
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ const log = debug('ipfs-companion:redirect-handler:blockOrObserve')
1212
log.error = debug('ipfs-companion:redirect-handler:blockOrObserve:error')
1313

1414
export const DEFAULT_NAMESPACES = new Set(['ipfs', 'ipns'])
15-
16-
export const GLOBAL_STATE_CHANGE = 'GLOBAL_STATE_CHANGE'
1715
export const GLOBAL_STATE_OPTION_CHANGE = 'GLOBAL_STATE_OPTION_CHANGE'
1816
export const DELETE_RULE_REQUEST = 'DELETE_RULE_REQUEST'
1917
export const DELETE_RULE_REQUEST_SUCCESS = 'DELETE_RULE_REQUEST_SUCCESS'
@@ -32,7 +30,7 @@ interface redirectHandlerInput {
3230
getPort: (state: CompanionState) => string
3331
}
3432

35-
type messageToSelfType = typeof GLOBAL_STATE_CHANGE | typeof GLOBAL_STATE_OPTION_CHANGE | typeof DELETE_RULE_REQUEST
33+
type messageToSelfType = typeof GLOBAL_STATE_OPTION_CHANGE | typeof DELETE_RULE_REQUEST
3634
interface messageToSelf {
3735
type: messageToSelfType
3836
value?: string | Record<string, unknown>
@@ -50,36 +48,38 @@ export const defaultNSRegexStr = `(${[...DEFAULT_NAMESPACES].join('|')})`
5048
export const supportsBlock = (): boolean => !(browser.declarativeNetRequest?.MAX_NUMBER_OF_DYNAMIC_AND_SESSION_RULES > 0)
5149

5250
/**
53-
* Notify self about state change.
54-
* @returns void
51+
* Sends message to self to notify about change.
52+
*
53+
* @param msg
5554
*/
56-
export async function notifyStateChange (): Promise<void> {
57-
return await sendMessageToSelf(GLOBAL_STATE_CHANGE)
55+
async function sendMessageToSelf (msg: messageToSelfType, value?: any): Promise<void> {
56+
// this check ensures we don't send messages to ourselves if blocking mode is enabled.
57+
if (!supportsBlock()) {
58+
const message: messageToSelf = { type: msg, value }
59+
// on FF, this call waits for the response from the listener.
60+
// on Chrome, this needs a callback.
61+
await browser.runtime.sendMessage(message)
62+
}
5863
}
5964

6065
/**
6166
* Notify self about option change.
67+
*
6268
* @returns void
6369
*/
6470
export async function notifyOptionChange (): Promise<void> {
71+
log('notifyOptionChange')
6572
return await sendMessageToSelf(GLOBAL_STATE_OPTION_CHANGE)
6673
}
6774

68-
export async function notifyDeleteRule (id: number): Promise<void> {
69-
return await sendMessageToSelf(DELETE_RULE_REQUEST, id)
70-
}
71-
7275
/**
73-
* Sends message to self to notify about change.
76+
* Notify self about rule deletion.
7477
*
75-
* @param msg
78+
* @param id number
79+
* @returns void
7680
*/
77-
async function sendMessageToSelf (msg: messageToSelfType, value?: any): Promise<void> {
78-
// this check ensures we don't send messages to ourselves if blocking mode is enabled.
79-
if (!supportsBlock()) {
80-
const message: messageToSelf = { type: msg }
81-
await browser.runtime.sendMessage(message)
82-
}
81+
export async function notifyDeleteRule (id: number): Promise<void> {
82+
return await sendMessageToSelf(DELETE_RULE_REQUEST, id)
8383
}
8484

8585
const savedRegexFilters: Map<string, regexFilterMap> = new Map()
@@ -205,7 +205,8 @@ async function cleanupRuleById (id: number): Promise<void> {
205205
* @param {function} handlerFn
206206
*/
207207
function setupListeners (handlers: Record<messageToSelfType, (value: any) => Promise<void>>): void {
208-
browser.runtime.onMessage.addListener(async ({ message: { type, value } }: { message: messageToSelf }): Promise<void> => {
208+
browser.runtime.onMessage.addListener(async (message: messageToSelf): Promise<void> => {
209+
const { type, value } = message
209210
if (type in handlers) {
210211
await handlers[type](value)
211212
}
@@ -340,6 +341,23 @@ export function generateAddRule (
340341
*/
341342
export function addRuleToDynamicRuleSetGenerator (
342343
getState: () => CompanionState): (input: redirectHandlerInput) => Promise<void> {
344+
// setup listeners for the extension.
345+
setupListeners({
346+
[GLOBAL_STATE_OPTION_CHANGE]: async (): Promise<void> => {
347+
log('GLOBAL_STATE_OPTION_CHANGE')
348+
await cleanupRules(true)
349+
await reconcileRulesAndRemoveOld(getState())
350+
},
351+
[DELETE_RULE_REQUEST]: async (value: number): Promise<void> => {
352+
if (value != null) {
353+
await cleanupRuleById(value)
354+
await browser.runtime.sendMessage({ type: DELETE_RULE_REQUEST_SUCCESS })
355+
} else {
356+
await cleanupRules(true)
357+
}
358+
}
359+
})
360+
343361
// returning a closure to avoid passing `getState` as an argument to `addRuleToDynamicRuleSet`.
344362
return async function ({ originUrl, redirectUrl }: redirectHandlerInput): Promise<void> {
345363
// update the rules so that the next request is handled correctly.
@@ -378,24 +396,6 @@ export function addRuleToDynamicRuleSetGenerator (
378396
}
379397
)
380398
}
381-
382-
setupListeners({
383-
[GLOBAL_STATE_CHANGE]: async (): Promise<void> => {
384-
await reconcileRulesAndRemoveOld(getState())
385-
},
386-
[GLOBAL_STATE_OPTION_CHANGE]: async (): Promise<void> => {
387-
await cleanupRules(true)
388-
await reconcileRulesAndRemoveOld(getState())
389-
},
390-
[DELETE_RULE_REQUEST]: async (value: number): Promise<void> => {
391-
if (value != null) {
392-
await cleanupRuleById(value)
393-
await browser.runtime.sendMessage({ type: DELETE_RULE_REQUEST_SUCCESS })
394-
} else {
395-
await cleanupRules(true)
396-
}
397-
}
398-
})
399399
// call to reconcile rules and remove old ones.
400400
await reconcileRulesAndRemoveOld(state)
401401
}

add-on/src/lib/redirect-handler/subdomainRedirectRegexFilter.ts

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export class SubdomainRedirectRegexFilter extends RegexFilter {
3131
const subdomainPart = urlParts.shift() as string
3232
// this needs to be computed for every iteration as the staticUrlParts changes
3333
const commonStaticUrlEnd = `\\.${escapeURLRegex(staticUrlParts.join('.'))}\\/${RULE_REGEX_ENDING}`
34-
3534
// this does not work for subdomains where namespace is not provided.
3635
// e.g. https://helia-identify.on.fleek.co/
3736
// e.g. https://bafybeib3bzis4mejzsnzsb65od3rnv5ffit7vsllratddjkgfgq4wiamqu.on.fleek.co/

add-on/src/options/page.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ export default function optionsPage (state, emit) {
4343
// when global toggle is in "suspended" state
4444
return html`
4545
<div class="sans-serif">
46-
${globalToggleForm({
47-
active: state.options.active,
48-
onOptionChange
49-
})}
46+
${globalToggleForm({
47+
active: state.options.active,
48+
onOptionChange
49+
})}
5050
</div>
5151
`
5252
}

add-on/src/options/store.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import browser from 'webextension-polyfill'
55
import { optionDefaults } from '../lib/options.js'
6-
import { DELETE_RULE_REQUEST_SUCCESS, RULE_REGEX_ENDING, notifyDeleteRule, notifyOptionChange, notifyStateChange } from '../lib/redirect-handler/blockOrObserve.js'
6+
import { DELETE_RULE_REQUEST_SUCCESS, RULE_REGEX_ENDING, notifyDeleteRule, notifyOptionChange } from '../lib/redirect-handler/blockOrObserve.js'
77
import createRuntimeChecks from '../lib/runtime-checks.js'
88

99
// The store contains and mutates the state for the app
@@ -49,9 +49,6 @@ export default function optionStore (state, emitter) {
4949

5050
emitter.on('optionChange', async ({ key, value }) => {
5151
browser.storage.local.set({ [key]: value })
52-
if (key === 'active') {
53-
await notifyStateChange()
54-
}
5552
await notifyOptionChange()
5653
})
5754

add-on/src/popup/browser-action/store.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { optionsPage, welcomePage } from '../../lib/constants.js'
77
import { contextMenuCopyAddressAtPublicGw, contextMenuCopyCanonicalAddress, contextMenuCopyCidAddress, contextMenuCopyPermalink, contextMenuCopyRawCid, contextMenuViewOnGateway } from '../../lib/context-menus.js'
88
import { browserActionFilesCpImportCurrentTab } from '../../lib/ipfs-import.js'
99
import { ipfsContentPath } from '../../lib/ipfs-path.js'
10-
import { notifyStateChange } from '../../lib/redirect-handler/blockOrObserve.js'
10+
import { notifyOptionChange } from '../../lib/redirect-handler/blockOrObserve.js'
1111
import { POSSIBLE_NODE_TYPES } from '../../lib/state.js'
1212

1313
// The store contains and mutates the state for the app
@@ -179,7 +179,11 @@ export default (state, emitter) => {
179179
}
180180
// console.dir('toggleSiteIntegrations', state)
181181
await browser.storage.local.set({ disabledOn, enabledOn })
182-
await notifyStateChange()
182+
await notifyOptionChange()
183+
// notifyOptionsChange call is async, sends a message to background and
184+
// waits for it to resolve. However, that doesnt work the
185+
// same way in Chrome and FF. So we need to wait a bit before reloading.
186+
await new Promise(resolve => setTimeout(resolve, 200))
183187

184188
const path = ipfsContentPath(currentTab.url, { keepURIParams: true })
185189
// Reload the current tab to apply updated redirect preference
@@ -216,7 +220,7 @@ export default (state, emitter) => {
216220
state.swarmPeers = null
217221
state.isIpfsOnline = false
218222
}
219-
await notifyStateChange()
223+
await notifyOptionChange()
220224
await browser.storage.local.set({ active: state.active })
221225
} catch (error) {
222226
console.error(`Unable to update global Active flag due to ${error}`)

0 commit comments

Comments
 (0)