Skip to content

Commit

Permalink
fix(recovery): Recovery Rules should reset (#1266)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Sep 7, 2023
1 parent 7be2c23 commit a81f545
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 52 deletions.
7 changes: 5 additions & 2 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { createRequestModifier } from './ipfs-request.js'
import createNotifier from './notifier.js'
import { runPendingOnInstallTasks } from './on-installed.js'
import { guiURLString, migrateOptions, optionDefaults, safeURL, storeMissingOptions } from './options.js'
import { getExtraInfoSpec } from './redirect-handler/blockOrObserve.js'
import { cleanupRules, getExtraInfoSpec } from './redirect-handler/blockOrObserve.js'
import createRuntimeChecks from './runtime-checks.js'
import { initState, offlinePeerCount } from './state.js'

Expand Down Expand Up @@ -521,6 +521,7 @@ export default async function init (inQuickImport = false) {
try {
const oldColor = colorArraytoHex(await browser.action.getBadgeBackgroundColor({}))
if (badgeColor !== oldColor) {
await cleanupRules(true)
await browser.action.setBadgeBackgroundColor({ color: badgeColor })
await setBrowserActionIcon(badgeIcon)
}
Expand Down Expand Up @@ -577,7 +578,7 @@ export default async function init (inQuickImport = false) {
}
}

async function onStorageChange (changes, area) {
async function onStorageChange (changes) {
let shouldReloadExtension = false
let shouldRestartIpfsClient = false
let shouldStopIpfsClient = false
Expand Down Expand Up @@ -696,6 +697,8 @@ export default async function init (inQuickImport = false) {
browser.tabs.reload() // async reload of options page to keep it alive
await browser.runtime.reload()
}
log('storage change processed')

// Post update to Browser Action (if exists) -- this gives UX a snappy feel
await sendStatusUpdateToBrowserAction()
}
Expand Down
76 changes: 38 additions & 38 deletions add-on/src/lib/redirect-handler/blockOrObserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const log = debug('ipfs-companion:redirect-handler:blockOrObserve')
log.error = debug('ipfs-companion:redirect-handler:blockOrObserve:error')

export const DEFAULT_NAMESPACES = new Set(['ipfs', 'ipns'])

export const GLOBAL_STATE_CHANGE = 'GLOBAL_STATE_CHANGE'
export const GLOBAL_STATE_OPTION_CHANGE = 'GLOBAL_STATE_OPTION_CHANGE'
export const DELETE_RULE_REQUEST = 'DELETE_RULE_REQUEST'
export const DELETE_RULE_REQUEST_SUCCESS = 'DELETE_RULE_REQUEST_SUCCESS'
Expand All @@ -32,7 +30,7 @@ interface redirectHandlerInput {
getPort: (state: CompanionState) => string
}

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

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

/**
* Notify self about option change.
*
* @returns void
*/
export async function notifyOptionChange (): Promise<void> {
log('notifyOptionChange')
return await sendMessageToSelf(GLOBAL_STATE_OPTION_CHANGE)
}

export async function notifyDeleteRule (id: number): Promise<void> {
return await sendMessageToSelf(DELETE_RULE_REQUEST, id)
}

/**
* Sends message to self to notify about change.
* Notify self about rule deletion.
*
* @param msg
* @param id number
* @returns void
*/
async function sendMessageToSelf (msg: messageToSelfType, value?: any): Promise<void> {
// this check ensures we don't send messages to ourselves if blocking mode is enabled.
if (!supportsBlock()) {
const message: messageToSelf = { type: msg }
await browser.runtime.sendMessage(message)
}
export async function notifyDeleteRule (id: number): Promise<void> {
return await sendMessageToSelf(DELETE_RULE_REQUEST, id)
}

const savedRegexFilters: Map<string, regexFilterMap> = new Map()
Expand Down Expand Up @@ -205,7 +205,8 @@ async function cleanupRuleById (id: number): Promise<void> {
* @param {function} handlerFn
*/
function setupListeners (handlers: Record<messageToSelfType, (value: any) => Promise<void>>): void {
browser.runtime.onMessage.addListener(async ({ message: { type, value } }: { message: messageToSelf }): Promise<void> => {
browser.runtime.onMessage.addListener(async (message: messageToSelf): Promise<void> => {
const { type, value } = message
if (type in handlers) {
await handlers[type](value)
}
Expand Down Expand Up @@ -340,6 +341,23 @@ export function generateAddRule (
*/
export function addRuleToDynamicRuleSetGenerator (
getState: () => CompanionState): (input: redirectHandlerInput) => Promise<void> {
// setup listeners for the extension.
setupListeners({
[GLOBAL_STATE_OPTION_CHANGE]: async (): Promise<void> => {
log('GLOBAL_STATE_OPTION_CHANGE')
await cleanupRules(true)
await reconcileRulesAndRemoveOld(getState())
},
[DELETE_RULE_REQUEST]: async (value: number): Promise<void> => {
if (value != null) {
await cleanupRuleById(value)
await browser.runtime.sendMessage({ type: DELETE_RULE_REQUEST_SUCCESS })
} else {
await cleanupRules(true)
}
}
})

// returning a closure to avoid passing `getState` as an argument to `addRuleToDynamicRuleSet`.
return async function ({ originUrl, redirectUrl }: redirectHandlerInput): Promise<void> {
// update the rules so that the next request is handled correctly.
Expand Down Expand Up @@ -378,24 +396,6 @@ export function addRuleToDynamicRuleSetGenerator (
}
)
}

setupListeners({
[GLOBAL_STATE_CHANGE]: async (): Promise<void> => {
await reconcileRulesAndRemoveOld(getState())
},
[GLOBAL_STATE_OPTION_CHANGE]: async (): Promise<void> => {
await cleanupRules(true)
await reconcileRulesAndRemoveOld(getState())
},
[DELETE_RULE_REQUEST]: async (value: number): Promise<void> => {
if (value != null) {
await cleanupRuleById(value)
await browser.runtime.sendMessage({ type: DELETE_RULE_REQUEST_SUCCESS })
} else {
await cleanupRules(true)
}
}
})
// call to reconcile rules and remove old ones.
await reconcileRulesAndRemoveOld(state)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export class SubdomainRedirectRegexFilter extends RegexFilter {
const subdomainPart = urlParts.shift() as string
// this needs to be computed for every iteration as the staticUrlParts changes
const commonStaticUrlEnd = `\\.${escapeURLRegex(staticUrlParts.join('.'))}\\/${RULE_REGEX_ENDING}`

// this does not work for subdomains where namespace is not provided.
// e.g. https://helia-identify.on.fleek.co/
// e.g. https://bafybeib3bzis4mejzsnzsb65od3rnv5ffit7vsllratddjkgfgq4wiamqu.on.fleek.co/
Expand Down
8 changes: 4 additions & 4 deletions add-on/src/options/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ export default function optionsPage (state, emit) {
// when global toggle is in "suspended" state
return html`
<div class="sans-serif">
${globalToggleForm({
active: state.options.active,
onOptionChange
})}
${globalToggleForm({
active: state.options.active,
onOptionChange
})}
</div>
`
}
Expand Down
5 changes: 1 addition & 4 deletions add-on/src/options/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

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

emitter.on('optionChange', async ({ key, value }) => {
browser.storage.local.set({ [key]: value })
if (key === 'active') {
await notifyStateChange()
}
await notifyOptionChange()
})

Expand Down
10 changes: 7 additions & 3 deletions add-on/src/popup/browser-action/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { optionsPage, welcomePage } from '../../lib/constants.js'
import { contextMenuCopyAddressAtPublicGw, contextMenuCopyCanonicalAddress, contextMenuCopyCidAddress, contextMenuCopyPermalink, contextMenuCopyRawCid, contextMenuViewOnGateway } from '../../lib/context-menus.js'
import { browserActionFilesCpImportCurrentTab } from '../../lib/ipfs-import.js'
import { ipfsContentPath } from '../../lib/ipfs-path.js'
import { notifyStateChange } from '../../lib/redirect-handler/blockOrObserve.js'
import { notifyOptionChange } from '../../lib/redirect-handler/blockOrObserve.js'
import { POSSIBLE_NODE_TYPES } from '../../lib/state.js'

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

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

0 comments on commit a81f545

Please sign in to comment.