Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: working necromancy tests #1101

Merged
merged 14 commits into from
Nov 21, 2022
10 changes: 5 additions & 5 deletions add-on/src/background/background.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict'
/* eslint-env browser, webextensions */

const browser = require('webextension-polyfill')
const { onInstalled } = require('../lib/on-installed')
const { getUninstallURL } = require('../lib/on-uninstalled')
const { optionDefaults } = require('../lib/options')
import browser from 'webextension-polyfill'
import { onInstalled } from '../lib/on-installed.js'
import { getUninstallURL } from '../lib/on-uninstalled.js'
import { optionDefaults } from '../lib/options.js'
import createIpfsCompanion from '../lib/ipfs-companion.js'

// register lifecycle hooks early, otherwise we miss first install event
browser.runtime.onInstalled.addListener(onInstalled)
Expand All @@ -15,6 +16,5 @@ document.addEventListener('DOMContentLoaded', async () => {
// setting debug level early
localStorage.debug = (await browser.storage.local.get({ logNamespaces: optionDefaults.logNamespaces })).logNamespaces
// init inlined to read updated localStorage.debug
const createIpfsCompanion = require('../lib/ipfs-companion')
window.ipfsCompanion = await createIpfsCompanion()
})
4 changes: 2 additions & 2 deletions add-on/src/contentScripts/linkifyDOM.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'
/* eslint-env browser, webextensions */

const browser = require('webextension-polyfill')
const { default: PQueue } = require('p-queue')
import browser from 'webextension-polyfill'
import PQueue from 'p-queue'

/*
* This content script is responsible for performing the logic of replacing
Expand Down
10 changes: 5 additions & 5 deletions add-on/src/landing-pages/welcome/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'
/* eslint-env browser, webextensions */

require('./welcome.css')
import './welcome.css'

const browser = require('webextension-polyfill')
const choo = require('choo')
const createWelcomePageStore = require('./store')
const createWelcomePage = require('./page')
import browser from 'webextension-polyfill'
import choo from 'choo'
import createWelcomePageStore from './store.js'
import createWelcomePage from './page.js'

const app = choo()

Expand Down
16 changes: 7 additions & 9 deletions add-on/src/landing-pages/welcome/page.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict'

const browser = require('webextension-polyfill')
const html = require('choo/html')
const logo = require('../../popup/logo')
const { renderTranslatedLinks, renderTranslatedSpans } = require('../../utils/i18n')
import browser from 'webextension-polyfill'
import html from 'choo/html/index.js'
import logo from '../../popup/logo.js'
import { renderTranslatedLinks, renderTranslatedSpans } from '../../utils/i18n.js'

// Brave detection
const { brave } = require('../../../src/lib/ipfs-client/brave')
const { optionsPage } = require('../../../src/lib/constants')
import { brave } from '../../../src/lib/ipfs-client/brave.js'
import { optionsPage } from '../../../src/lib/constants.js'

// Assets
const libp2pLogo = '../../../images/libp2p.svg'
Expand All @@ -19,7 +19,7 @@ const colorIpfsLogo = '#57cbd0'
const colorWhite = '#ffffff'
const colorYellow = '#f39021'

function createWelcomePage (i18n) {
export default function createWelcomePage (i18n) {
return function welcomePage (state, emit) {
const { isIpfsOnline, peerCount } = state
const openWebUi = (page) => () => emit('openWebUi', page)
Expand Down Expand Up @@ -227,5 +227,3 @@ const renderProjects = (i18n) => {
</div>
`
}

module.exports = createWelcomePage
6 changes: 2 additions & 4 deletions add-on/src/landing-pages/welcome/store.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'
/* eslint-env browser, webextensions */
const browser = require('webextension-polyfill')
import browser from 'webextension-polyfill'

function createWelcomePageStore (i18n, runtime) {
export default function createWelcomePageStore (i18n, runtime) {
return function welcomePageStore (state, emitter) {
state.isIpfsOnline = null
state.peerCount = null
Expand Down Expand Up @@ -36,5 +36,3 @@ function createWelcomePageStore (i18n, runtime) {
})
}
}

module.exports = createWelcomePageStore
6 changes: 3 additions & 3 deletions add-on/src/lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'
/* eslint-env browser, webextensions */

exports.welcomePage = '/dist/landing-pages/welcome/index.html'
exports.optionsPage = '/dist/options/options.html'
exports.tickMs = 250 // no CPU spike, but still responsive enough
export const welcomePage = '/dist/landing-pages/welcome/index.html'
export const optionsPage = '/dist/options/options.html'
export const tickMs = 250 // no CPU spike, but still responsive enough
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love a JSdoc comment on this to explain its intention. I can guess, but it's better if we don't force others to.

31 changes: 11 additions & 20 deletions add-on/src/lib/context-menus.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const browser = require('webextension-polyfill')
import browser from 'webextension-polyfill'

const debug = require('debug')
import debug from 'debug'
const log = debug('ipfs-companion:context-menus')
log.error = debug('ipfs-companion:context-menus:error')

Expand All @@ -17,7 +17,7 @@ const contextSources = {
page: 'pageUrl'
}

async function findValueForContext (context, contextType) {
export async function findValueForContext (context, contextType) {
if (context) {
if (contextType) {
const field = contextSources[contextType]
Expand All @@ -41,8 +41,6 @@ async function findValueForContext (context, contextType) {
return currentTab.url
}

module.exports.findValueForContext = findValueForContext

// Context Roots
const menuParentImage = 'contextMenu_parentImage'
const menuParentVideo = 'contextMenu_parentVideo'
Expand All @@ -55,18 +53,12 @@ const contextMenuImportToIpfs = 'contextMenu_importToIpfs'
// Add X to IPFS
const contextMenuImportToIpfsSelection = 'contextMenu_importToIpfsSelection'
// Copy X
const contextMenuCopyCidAddress = 'panelCopy_currentIpfsAddress'
const contextMenuCopyCanonicalAddress = 'panelCopy_currentIpnsAddress'
const contextMenuCopyRawCid = 'panelCopy_copyRawCid'
const contextMenuCopyAddressAtPublicGw = 'panel_copyCurrentPublicGwUrl'
const contextMenuViewOnGateway = 'panel_contextMenuViewOnGateway'
const contextMenuCopyPermalink = 'panel_copyCurrentPermalink'
module.exports.contextMenuCopyCidAddress = contextMenuCopyCidAddress
module.exports.contextMenuCopyCanonicalAddress = contextMenuCopyCanonicalAddress
module.exports.contextMenuCopyRawCid = contextMenuCopyRawCid
module.exports.contextMenuCopyAddressAtPublicGw = contextMenuCopyAddressAtPublicGw
module.exports.contextMenuViewOnGateway = contextMenuViewOnGateway
module.exports.contextMenuCopyPermalink = contextMenuCopyPermalink
export const contextMenuCopyCidAddress = 'panelCopy_currentIpfsAddress'
export const contextMenuCopyCanonicalAddress = 'panelCopy_currentIpnsAddress'
export const contextMenuCopyRawCid = 'panelCopy_copyRawCid'
export const contextMenuCopyAddressAtPublicGw = 'panel_copyCurrentPublicGwUrl'
export const contextMenuViewOnGateway = 'panel_contextMenuViewOnGateway'
export const contextMenuCopyPermalink = 'panel_copyCurrentPermalink'

// menu item ids for things that are enabled only when API is online (static)
const apiMenuItemIds = new Set([contextMenuCopyRawCid, contextMenuCopyCanonicalAddress, contextMenuImportToIpfs])
Expand All @@ -75,7 +67,8 @@ const apiMenuItems = new Set()
// menu items enabled only in IPFS context (dynamic)
const ipfsContextItems = new Set()

function createContextMenus (getState, runtime, ipfsPathValidator, { onAddFromContext, onCopyCanonicalAddress, onCopyRawCid, onCopyAddressAtPublicGw }) {
export function createContextMenus (
getState, _runtime, ipfsPathValidator, { onAddFromContext, onCopyRawCid, onCopyAddressAtPublicGw }) {
try {
const createSubmenu = (id, contextType, menuBuilder) => {
browser.contextMenus.create({
Expand Down Expand Up @@ -192,5 +185,3 @@ function createContextMenus (getState, runtime, ipfsPathValidator, { onAddFromCo
// TODO: destroy?
}
}

module.exports.createContextMenus = createContextMenus
6 changes: 2 additions & 4 deletions add-on/src/lib/copier.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { findValueForContext } = require('./context-menus')
import { findValueForContext } from './context-menus.js'

async function copyTextToClipboard (text, notify) {
try {
Expand Down Expand Up @@ -31,7 +31,7 @@ async function copyTextToClipboard (text, notify) {
}
}

function createCopier (notify, ipfsPathValidator) {
export default function createCopier (notify, ipfsPathValidator) {
return {
async copyTextToClipboard (text) {
await copyTextToClipboard(text, notify)
Expand Down Expand Up @@ -83,5 +83,3 @@ function createCopier (notify, ipfsPathValidator) {
}
}
}

module.exports = createCopier
21 changes: 11 additions & 10 deletions add-on/src/lib/dnslink.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
'use strict'
/* eslint-env browser */

const debug = require('debug')
import Pqueue from 'p-queue'

import debug from 'debug'
import IsIpfs from 'is-ipfs'
import LRU from 'lru-cache'
import { offlinePeerCount } from './state.js'
import { ipfsContentPath, sameGateway, pathAtHttpGateway } from './ipfs-path.js'
Comment on lines +4 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: spacing - I personally prefer organizing imports in four (potentially 5 groups depending on how large your 1st party import sections are):

  1. stdlib imports;
  2. 3p imports;
  3. 2p imports (internal to company but not to team)
  4. 1p imports (internal to team)
  5. local/relative imports (internal to project)

eslint-plugin-imports allows you to automate this


const log = debug('ipfs-companion:dnslink')
log.error = debug('ipfs-companion:dnslink:error')

const IsIpfs = require('is-ipfs')
const LRU = require('lru-cache')
const { default: PQueue } = require('p-queue')
const { offlinePeerCount } = require('./state')
const { ipfsContentPath, sameGateway, pathAtHttpGateway } = require('./ipfs-path')

module.exports = function createDnslinkResolver (getState) {
export default function createDnslinkResolver (getState) {
// DNSLink lookup result cache
const cacheOptions = { max: 1000, maxAge: 1000 * 60 * 60 * 12 }
const cache = new LRU(cacheOptions)
// upper bound for concurrent background lookups done by resolve(url)
const lookupQueue = new PQueue({ concurrency: 4 })
const lookupQueue = new Pqueue({ concurrency: 4 })
// preload of DNSLink data
const preloadUrlCache = new LRU(cacheOptions)
const preloadQueue = new PQueue({ concurrency: 4 })
const preloadQueue = new Pqueue({ concurrency: 4 })

const dnslinkResolver = {

Expand Down
10 changes: 4 additions & 6 deletions add-on/src/lib/http-proxy.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'
/* eslint-env browser, webextensions */

const browser = require('webextension-polyfill')
const { safeURL } = require('./options')
import browser from 'webextension-polyfill'
import { safeURL } from './options.js'

const debug = require('debug')
import debug from 'debug'
const log = debug('ipfs-companion:http-proxy')
log.error = debug('ipfs-companion:http-proxy:error')

Expand All @@ -30,7 +30,7 @@ log.error = debug('ipfs-companion:http-proxy:error')
// - Firefox requires proxy to avoid DNS lookup, but there is an open issue
// that will remove that need at some point:
// https://bugzilla.mozilla.org/show_bug.cgi?id=1220810
async function registerSubdomainProxy (getState, runtime, notify) {
export async function registerSubdomainProxy (getState, runtime, notify) {
// At the moment only firefox requires proxy registration
if (!runtime.isFirefox) return

Expand Down Expand Up @@ -151,5 +151,3 @@ async function registerSubdomainProxyChromium (enable, hostname, port) {
}
}
*/

module.exports.registerSubdomainProxy = registerSubdomainProxy
10 changes: 4 additions & 6 deletions add-on/src/lib/inspector.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

const browser = require('webextension-polyfill')
const { findValueForContext } = require('./context-menus')
const { pathAtHttpGateway } = require('./ipfs-path')
import browser from 'webextension-polyfill'
import { findValueForContext } from './context-menus.js'
import { pathAtHttpGateway } from './ipfs-path.js'

function createInspector (notify, ipfsPathValidator, getState) {
export default function createInspector (notify, ipfsPathValidator, getState) {
return {
async viewOnGateway (context, contextType) {
const url = await findValueForContext(context, contextType)
Expand All @@ -17,5 +17,3 @@ function createInspector (notify, ipfsPathValidator, getState) {
// TODO: view in WebUI's IPLD Explorer
}
}

module.exports = createInspector
Loading