-
Notifications
You must be signed in to change notification settings - Fork 324
/
ipfs-companion.js
809 lines (739 loc) · 31.1 KB
/
ipfs-companion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
'use strict'
/* eslint-env browser, webextensions */
const debug = require('debug')
const log = debug('ipfs-companion:main')
log.error = debug('ipfs-companion:main:error')
const browser = require('webextension-polyfill')
const toMultiaddr = require('uri-to-multiaddr')
const pMemoize = require('p-memoize')
const LRU = require('lru-cache')
const all = require('it-all')
const { optionDefaults, storeMissingOptions, migrateOptions, guiURLString, safeURL } = require('./options')
const { initState, offlinePeerCount } = require('./state')
const { createIpfsPathValidator, sameGateway, safeHostname } = require('./ipfs-path')
const createDnslinkResolver = require('./dnslink')
const { createRequestModifier } = require('./ipfs-request')
const { initIpfsClient, destroyIpfsClient } = require('./ipfs-client')
const { braveNodeType, useBraveEndpoint, releaseBraveEndpoint } = require('./ipfs-client/brave')
const { createIpfsImportHandler, formatImportDirectory, browserActionFilesCpImportCurrentTab } = require('./ipfs-import')
const createNotifier = require('./notifier')
const createCopier = require('./copier')
const createInspector = require('./inspector')
const { createRuntimeChecks } = require('./runtime-checks')
const { createContextMenus, findValueForContext, contextMenuCopyAddressAtPublicGw, contextMenuCopyRawCid, contextMenuCopyCanonicalAddress, contextMenuViewOnGateway, contextMenuCopyPermalink, contextMenuCopyCidAddress } = require('./context-menus')
const createIpfsProxy = require('./ipfs-proxy')
const { registerSubdomainProxy } = require('./http-proxy')
const { runPendingOnInstallTasks } = require('./on-installed')
let browserActionPort // reuse instance for status updates between on/off toggles
// init happens on addon load in background/background.js
module.exports = async function init () {
// INIT
// ===================================================================
let ipfs // ipfs-api instance
let state // avoid redundant API reads by utilizing local cache of various states
let dnslinkResolver
let ipfsPathValidator
let modifyRequest
let notify
let copier
let inspector
let runtime
let contextMenus
let apiStatusUpdateInterval
let ipfsProxy
// TODO: window.ipfs var ipfsProxyContentScript
let ipfsImportHandler
const idleInSecs = 5 * 60
const browserActionPortName = 'browser-action-port'
try {
log('init')
await storeMissingOptions(await browser.storage.local.get(), optionDefaults, browser.storage.local)
await migrateOptions(browser.storage.local, debug)
const options = await browser.storage.local.get(optionDefaults)
runtime = await createRuntimeChecks(browser)
state = initState(options)
notify = createNotifier(getState)
if (state.active) {
// It's ok for this to fail, node might be unavailable or mis-configured
try {
ipfs = await initIpfsClient(browser, state)
} catch (err) {
console.error('[ipfs-companion] Failed to init IPFS client', err)
notify(
'notify_startIpfsNodeErrorTitle',
err.name === 'ValidationError' ? err.details[0].message : err.message
)
}
}
dnslinkResolver = createDnslinkResolver(getState)
ipfsPathValidator = createIpfsPathValidator(getState, getIpfs, dnslinkResolver)
copier = createCopier(notify, ipfsPathValidator)
ipfsImportHandler = createIpfsImportHandler(getState, getIpfs, ipfsPathValidator, runtime, copier)
inspector = createInspector(notify, ipfsPathValidator, getState)
contextMenus = createContextMenus(getState, runtime, ipfsPathValidator, {
onAddFromContext,
onCopyCanonicalAddress: copier.copyCanonicalAddress,
onCopyRawCid: copier.copyRawCid,
onCopyAddressAtPublicGw: copier.copyAddressAtPublicGw
})
modifyRequest = createRequestModifier(getState, dnslinkResolver, ipfsPathValidator, runtime)
ipfsProxy = createIpfsProxy(getIpfs, getState)
// TODO(window.ipfs) ipfsProxyContentScript = await registerIpfsProxyContentScript()
log('register all listeners')
registerListeners()
await registerSubdomainProxy(getState, runtime, notify)
log('init done')
setApiStatusUpdateInterval(options.ipfsApiPollMs)
await runPendingOnInstallTasks()
} catch (error) {
log.error('Unable to initialize addon due to error', error)
if (notify) notify('notify_addonIssueTitle', 'notify_addonIssueMsg')
throw error
}
function getState () {
return state
}
function getIpfs () {
if (state.active && ipfs) return ipfs
log.error('refused access to IPFS API client, check if extension is enabled')
throw new Error('IPFS Companion: API client is disabled')
}
function registerListeners () {
const onBeforeSendInfoSpec = ['blocking', 'requestHeaders']
if (browser.webRequest.OnBeforeSendHeadersOptions && 'EXTRA_HEADERS' in browser.webRequest.OnBeforeSendHeadersOptions) {
// Chrome 72+ requires 'extraHeaders' for accessing all headers
// Note: we need this for code ensuring ipfs-http-client can talk to API without setting CORS
onBeforeSendInfoSpec.push('extraHeaders')
}
browser.webRequest.onBeforeSendHeaders.addListener(onBeforeSendHeaders, { urls: ['<all_urls>'] }, onBeforeSendInfoSpec)
browser.webRequest.onBeforeRequest.addListener(onBeforeRequest, { urls: ['<all_urls>'] }, ['blocking'])
browser.webRequest.onHeadersReceived.addListener(onHeadersReceived, { urls: ['<all_urls>'] }, ['blocking', 'responseHeaders'])
browser.webRequest.onErrorOccurred.addListener(onErrorOccurred, { urls: ['<all_urls>'], types: ['main_frame'] })
browser.webRequest.onCompleted.addListener(onCompleted, { urls: ['<all_urls>'], types: ['main_frame'] })
browser.storage.onChanged.addListener(onStorageChange)
browser.webNavigation.onCommitted.addListener(onNavigationCommitted)
browser.webNavigation.onDOMContentLoaded.addListener(onDOMContentLoaded)
browser.tabs.onActivated.addListener(onActivatedTab)
if (browser.windows) {
browser.windows.onFocusChanged.addListener(onWindowFocusChanged)
}
browser.runtime.onMessage.addListener(onRuntimeMessage)
browser.runtime.onConnect.addListener(onRuntimeConnect)
if (runtime.hasNativeProtocolHandler) {
log('native protocol handler support detected, but IPFS handler is not implemented yet :-(')
} else {
log('no browser.protocol API, native protocol will not be registered')
}
}
// Register Content Script responsible for loading window.ipfs (ipfsProxy)
//
// The key difference between tabs.executeScript and contentScripts API
// is the latter provides guarantee to execute before anything else.
// https://github.com/ipfs-shipyard/ipfs-companion/issues/451#issuecomment-382669093
/* TODO(window.ipfs)
async function registerIpfsProxyContentScript (previousHandle) {
previousHandle = previousHandle || ipfsProxyContentScript
if (previousHandle) {
previousHandle.unregister()
}
// TODO:
// No window.ipfs for now.
// We will restore when Migration to JS API with Async Await and Async Iterables
// is done:
// https://github.com/ipfs-shipyard/ipfs-companion/pull/777
// https://github.com/ipfs-shipyard/ipfs-companion/issues/843
// https://github.com/ipfs-shipyard/ipfs-companion/issues/852#issuecomment-594510819
const forceOff = true
if (forceOff || !state.active || !state.ipfsProxy || !browser.contentScripts) {
// no-op if global toggle is off, window.ipfs is disabled in Preferences
// or if runtime has no contentScript API
// (Chrome loads content script via manifest)
return
}
const newHandle = await browser.contentScripts.register({
matches: ['<all_urls>'],
js: [
{ file: '/dist/bundles/ipfsProxyContentScript.bundle.js' }
],
allFrames: true,
runAt: 'document_start'
})
return newHandle
}
*/
// HTTP Request Hooks
// ===================================================================
function onBeforeSendHeaders (request) {
return modifyRequest.onBeforeSendHeaders(request)
}
function onBeforeRequest (request) {
return modifyRequest.onBeforeRequest(request)
}
function onHeadersReceived (request) {
return modifyRequest.onHeadersReceived(request)
}
function onErrorOccurred (request) {
return modifyRequest.onErrorOccurred(request)
}
function onCompleted (request) {
return modifyRequest.onCompleted(request)
}
// RUNTIME MESSAGES (one-off messaging)
// ===================================================================
// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/sendMessage
function onRuntimeMessage (request, sender) {
// console.log((sender.tab ? 'Message from a content script:' + sender.tab.url : 'Message from the extension'), request)
if (request.pubGwUrlForIpfsOrIpnsPath) {
const path = request.pubGwUrlForIpfsOrIpnsPath
const { validIpfsOrIpns, resolveToPublicUrl } = ipfsPathValidator
const result = validIpfsOrIpns(path) ? resolveToPublicUrl(path) : null
return Promise.resolve({ pubGwUrlForIpfsOrIpnsPath: result })
}
}
// PORTS (connection-based messaging)
// ===================================================================
// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/connect
// Make a connection between different contexts inside the add-on,
// e.g. signalling between browser action popup and background page that works
// in everywhere, even in private contexts (https://github.com/ipfs/ipfs-companion/issues/243)
// Cache for async URL2CID resolution used by browser action
// (resolution happens off-band so UI render is not blocked with sometimes expensive DHT traversal)
const resolveCache = new LRU({ max: 10, maxAge: 1000 * 30 })
function onRuntimeConnect (port) {
// console.log('onConnect', port)
if (port.name === browserActionPortName) {
browserActionPort = port
browserActionPort.onMessage.addListener(handleMessageFromBrowserAction)
browserActionPort.onDisconnect.addListener(() => { browserActionPort = null })
sendStatusUpdateToBrowserAction()
}
}
const BrowserActionMessageHandlers = {
notification: (message) => notify(message.title, message.message),
[browserActionFilesCpImportCurrentTab]: () => ipfsImportHandler.filesCpImportCurrentTab(browser),
[contextMenuViewOnGateway]: inspector.viewOnGateway,
[contextMenuCopyCanonicalAddress]: copier.copyCanonicalAddress,
[contextMenuCopyCidAddress]: copier.copyCidAddress,
[contextMenuCopyRawCid]: copier.copyRawCid,
[contextMenuCopyAddressAtPublicGw]: copier.copyAddressAtPublicGw,
[contextMenuCopyPermalink]: copier.copyPermalink
}
function handleMessageFromBrowserAction (message) {
const handler = BrowserActionMessageHandlers[message && message.event]
if (!handler) return console.warn('Unknown browser action message event', message)
handler(message)
}
async function sendStatusUpdateToBrowserAction () {
if (!browserActionPort) return
const dropSlash = url => url.replace(/\/$/, '')
const currentTab = await browser.tabs.query({ active: true, currentWindow: true }).then(tabs => tabs[0])
const { version } = browser.runtime.getManifest()
const info = {
active: state.active,
ipfsNodeType: state.ipfsNodeType,
peerCount: state.peerCount,
gwURLString: dropSlash(state.gwURLString),
pubGwURLString: dropSlash(state.pubGwURLString),
webuiRootUrl: dropSlash(state.webuiRootUrl), // TODO: fix js-ipfs - it fails with trailing slash
importDir: state.importDir,
openViaWebUI: state.openViaWebUI,
apiURLString: dropSlash(state.apiURLString),
redirect: state.redirect,
enabledOn: state.enabledOn,
disabledOn: state.disabledOn,
newVersion: state.dismissedUpdate !== version ? version : null,
currentTab
}
try {
const v = await ipfs.version()
if (v) {
info.gatewayVersion = v.commit ? v.version + '/' + v.commit : v.version
}
} catch (error) {
info.gatewayVersion = null
}
if (state.active && info.currentTab) {
const url = info.currentTab.url
info.isIpfsContext = ipfsPathValidator.isIpfsPageActionsContext(url)
if (info.isIpfsContext) {
info.currentTabPublicUrl = ipfsPathValidator.resolveToPublicUrl(url)
info.currentTabContentPath = ipfsPathValidator.resolveToIpfsPath(url)
if (resolveCache.has(url)) {
const [immutableIpfsPath, permalink, cid] = resolveCache.get(url)
info.currentTabImmutablePath = immutableIpfsPath
info.currentTabPermalink = permalink
info.currentTabCid = cid
} else {
// run async resolution in the next event loop so it does not block the UI
setTimeout(async () => {
resolveCache.set(url, [
await ipfsPathValidator.resolveToImmutableIpfsPath(url),
await ipfsPathValidator.resolveToPermalink(url),
await ipfsPathValidator.resolveToCid(url)
])
await sendStatusUpdateToBrowserAction()
}, 0)
}
}
info.currentDnslinkFqdn = dnslinkResolver.findDNSLinkHostname(url)
info.currentFqdn = info.currentDnslinkFqdn || safeHostname(url)
info.currentTabIntegrationsOptOut = !state.activeIntegrations(info.currentFqdn)
info.isRedirectContext = info.currentFqdn && ipfsPathValidator.isRedirectPageActionsContext(url)
}
// Still here?
if (browserActionPort) {
browserActionPort.postMessage({ statusUpdate: info })
}
}
// Context Menu Importer
// -------------------------------------------------------------------
async function onAddFromContext (context, contextType, options) {
const {
copyImportResultsToFiles,
copyShareLink,
preloadFilesAtPublicGateway,
openFilesAtGateway,
openFilesAtWebUI
} = ipfsImportHandler
const importDir = formatImportDirectory(state.importDir)
let data
let results
try {
const dataSrc = await findValueForContext(context, contextType)
if (contextType === 'selection') {
// TODO: persist full pageUrl somewhere (eg. append at the end of the content but add toggle to disable it)
data = {
path: `${new URL(context.pageUrl).hostname}.txt`,
content: dataSrc
}
} else {
// Enchanced addFromURL
// --------------------
// Initially, this was a workaround due to https://github.com/ipfs/ipfs-companion/issues/227
// but now we have additional rules about keeping file name, so we can't use valilla ipfs.addFromURL
const fetchOptions = {
cache: 'force-cache',
referrer: context.pageUrl
}
// console.log('onAddFromContext.context', context)
// console.log('onAddFromContext.fetchOptions', fetchOptions)
const response = await fetch(dataSrc, fetchOptions)
const blob = await response.blob()
const url = new URL(response.url)
// https://github.com/ipfs-shipyard/ipfs-companion/issues/599
const filename = url.pathname === '/'
? url.hostname
: url.pathname.replace(/[\\/]+$/, '').split('/').pop()
data = {
path: decodeURIComponent(filename),
content: blob
}
}
results = await all(ipfs.addAll([data], options))
copyShareLink(results)
preloadFilesAtPublicGateway(results)
await copyImportResultsToFiles(results, importDir)
if (!state.openViaWebUI || state.ipfsNodeType.startsWith('embedded')) {
await openFilesAtGateway(importDir)
} else {
await openFilesAtWebUI(importDir)
}
} catch (error) {
console.error('Error in import to IPFS context menu', error)
if (error.message === 'NetworkError when attempting to fetch resource.') {
notify('notify_importErrorTitle', 'notify_importTrackingProtectionErrorMsg')
console.warn('IPFS import often fails because remote file can not be downloaded due to Tracking Protection. See details at: https://github.com/ipfs/ipfs-companion/issues/227')
browser.tabs.create({
url: 'https://github.com/ipfs/ipfs-companion/issues/227'
})
} else {
notify('notify_importErrorTitle', 'notify_inlineErrorMsg', `${error.message}`)
}
}
}
// Page-specific Actions
// -------------------------------------------------------------------
async function onWindowFocusChanged (windowId) {
// Note: On some Linux window managers, WINDOW_ID_NONE will always be sent
// immediately preceding a switch from one browser window to another.
if (windowId !== browser.windows.WINDOW_ID_NONE) {
const currentTab = await browser.tabs.query({ active: true, windowId }).then(tabs => tabs[0])
await contextMenus.update(currentTab.id)
}
}
async function onActivatedTab (activeInfo) {
await contextMenus.update(activeInfo.tabId)
}
async function onNavigationCommitted (details) {
await contextMenus.update(details.tabId)
await updatePageActionIndicator(details.tabId, details.url)
}
async function updatePageActionIndicator (tabId, url) {
// Chrome does not permit for both pageAction and browserAction to be enabled at the same time
// https://github.com/ipfs-shipyard/ipfs-companion/issues/398
if (runtime.isFirefox && ipfsPathValidator.isIpfsPageActionsContext(url)) {
if (sameGateway(url, state.gwURL) || sameGateway(url, state.apiURL)) {
await browser.pageAction.setIcon({ tabId: tabId, path: '/icons/ipfs-logo-on.svg' })
await browser.pageAction.setTitle({ tabId: tabId, title: browser.i18n.getMessage('pageAction_titleIpfsAtCustomGateway') })
} else {
await browser.pageAction.setIcon({ tabId: tabId, path: '/icons/ipfs-logo-off.svg' })
await browser.pageAction.setTitle({ tabId: tabId, title: browser.i18n.getMessage('pageAction_titleIpfsAtPublicGateway') })
}
await browser.pageAction.show(tabId)
}
}
async function onDOMContentLoaded (details) {
if (!state.active) return // skip content script injection when off
if (!details.url || !details.url.startsWith('http')) return // skip empty and special pages
if (!state.activeIntegrations(details.url)) return // skip if opt-out exists
// console.info(`[ipfs-companion] onDOMContentLoaded`, details)
if (state.linkify) {
log(`running linkfy experiment on ${details.url}`)
try {
await browser.tabs.executeScript(details.tabId, {
file: '/dist/bundles/linkifyContentScript.bundle.js',
matchAboutBlank: false,
allFrames: true,
runAt: 'document_idle'
})
} catch (error) {
log.error(`Unable to linkify DOM at '${details.url}' due to`, error)
}
}
// Ensure embedded js-ipfs in Brave uses correct API
if (details.url.startsWith(state.webuiRootUrl)) {
const apiMultiaddr = toMultiaddr(state.apiURLString)
await browser.tabs.executeScript(details.tabId, {
runAt: 'document_start',
code: `if (!localStorage.getItem('ipfsApi')) {
console.log('[ipfs-companion] Setting API to ${apiMultiaddr}');
localStorage.setItem('ipfsApi', '${apiMultiaddr}');
window.location.reload();
}`
})
}
}
// API STATUS UPDATES
// -------------------------------------------------------------------
// API is polled for peer count every ipfsApiPollMs
async function setApiStatusUpdateInterval (ipfsApiPollMs) {
if (apiStatusUpdateInterval) {
clearInterval(apiStatusUpdateInterval)
}
apiStatusUpdateInterval = setInterval(() => runIfNotIdle(apiStatusUpdate), ipfsApiPollMs)
await apiStatusUpdate()
}
async function apiStatusUpdate () {
// update peer count
const oldPeerCount = state.peerCount
state.peerCount = await getSwarmPeerCount()
await Promise.all([
updateAutomaticModeRedirectState(oldPeerCount, state.peerCount),
updateBrowserActionBadge(),
contextMenus.update(),
sendStatusUpdateToBrowserAction()
])
}
async function getSwarmPeerCount () {
if (!ipfs) return offlinePeerCount
try {
const peerInfos = await ipfs.swarm.peers({ timeout: 2500 })
return peerInfos.length
} catch (error) {
console.error(`Error while ipfs.swarm.peers: ${error}`)
return offlinePeerCount
}
}
async function runIfNotIdle (action) {
try {
const state = await browser.idle.queryState(idleInSecs)
if (state === 'active') {
return action()
}
} catch (error) {
console.error('Unable to read idle state, executing action without idle check', error)
return action()
}
}
// browserAction
// -------------------------------------------------------------------
async function updateBrowserActionBadge () {
if (typeof browser.browserAction.setBadgeBackgroundColor === 'undefined') {
// Firefox for Android does not have this UI, so we just skip it
return
}
let badgeText, badgeColor, badgeIcon
badgeText = state.peerCount.toString()
if (state.peerCount > 0) {
// All is good (online with peers)
badgeColor = '#418B8E'
badgeIcon = '/icons/ipfs-logo-on.svg'
} else if (state.peerCount === 0) {
// API is online but no peers
badgeColor = 'red'
badgeIcon = '/icons/ipfs-logo-on.svg'
} else {
// API is offline
badgeText = ''
badgeColor = '#8C8C8C'
badgeIcon = '/icons/ipfs-logo-off.svg'
}
try {
const oldColor = colorArraytoHex(await browser.browserAction.getBadgeBackgroundColor({}))
if (badgeColor !== oldColor) {
await browser.browserAction.setBadgeBackgroundColor({ color: badgeColor })
await setBrowserActionIcon(badgeIcon)
}
const oldText = await browser.browserAction.getBadgeText({})
if (oldText !== badgeText) await browser.browserAction.setBadgeText({ text: badgeText })
} catch (error) {
console.error('Unable to update browserAction badge due to error', error)
}
}
// ColorArray [0,0,0,0] → Hex #000000
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/browserAction/ColorArray
function colorArraytoHex (colorArray) {
if (!colorArray) return ''
const colorAt = i => colorArray[i].toString(16).padStart(2, '0')
const r = colorAt(0)
const g = colorAt(1)
const b = colorAt(2)
return ('#' + r + g + b).toUpperCase()
}
/* Alternative: raster images generated on the fly
const rasterIconDefinition = pMemoize((svgPath) => {
const rasterData = (size) => {
const icon = new Image()
icon.src = svgPath
const canvas = document.createElement('canvas')
const context = canvas.getContext('2d')
context.clearRect(0, 0, size, size)
context.drawImage(icon, 0, 0, size, size)
return context.getImageData(0, 0, size, size)
}
// icon sizes to cover ranges from:
// - https://bugs.chromium.org/p/chromium/issues/detail?id=647182
// - https://developer.chrome.com/extensions/manifest/icons
const r19 = rasterData(19)
const r38 = rasterData(38)
const r128 = rasterData(128)
// return computed values to be cached by p-memoize
return { imageData: { 19: r19, 38: r38, 128: r128 } }
})
*/
async function setBrowserActionIcon (iconPath) {
return browser.browserAction.setIcon(rasterIconDefinition(iconPath))
/* Below fallback does not work since Chromium 80
* (it fails in a way that does not produce error we can catch)
const iconDefinition = { path: iconPath }
try {
// Try SVG first -- Firefox supports it natively
await browser.browserAction.setIcon(iconDefinition)
} catch (error) {
// Fallback!
// Chromium does not support SVG [ticket below is 8 years old, I can't even..]
// https://bugs.chromium.org/p/chromium/issues/detail?id=29683
// Still, we want icon, so we precompute rasters of popular sizes and use them instead
await browser.browserAction.setIcon(rasterIconDefinition(iconPath))
}
*/
}
// OPTIONS
// ===================================================================
async function updateAutomaticModeRedirectState (oldPeerCount, newPeerCount) {
// enable/disable gw redirect based on API going online or offline
if (state.automaticMode && state.localGwAvailable) {
if (oldPeerCount === offlinePeerCount && newPeerCount > offlinePeerCount && !state.redirect) {
await browser.storage.local.set({ useCustomGateway: true })
await notify('notify_apiOnlineTitle', 'notify_apiOnlineAutomaticModeMsg')
} else if (newPeerCount === offlinePeerCount && state.redirect) {
await browser.storage.local.set({ useCustomGateway: false })
await notify('notify_apiOfflineTitle', 'notify_apiOfflineAutomaticModeMsg')
}
}
}
async function onStorageChange (changes, area) {
let shouldReloadExtension = false
let shouldRestartIpfsClient = false
let shouldStopIpfsClient = false
for (const key in changes) {
const change = changes[key]
if (change.oldValue === change.newValue) continue
// debug info
log(`storage key "${key}" changed: "${change.oldValue}" → "${change.newValue}"`)
switch (key) {
case 'active':
state[key] = change.newValue
// TODO(window.ipfs) ipfsProxyContentScript = await registerIpfsProxyContentScript()
await registerSubdomainProxy(getState, runtime)
shouldRestartIpfsClient = true
shouldStopIpfsClient = !state.active
break
case 'ipfsNodeType':
if (change.oldValue !== braveNodeType && change.newValue === braveNodeType) {
useBraveEndpoint(browser)
} else if (change.oldValue === braveNodeType && change.newValue !== braveNodeType) {
releaseBraveEndpoint(browser)
}
shouldRestartIpfsClient = true
state[key] = change.newValue
break
case 'ipfsNodeConfig':
shouldRestartIpfsClient = true
state[key] = change.newValue
break
case 'ipfsApiUrl':
state.apiURL = safeURL(change.newValue, { useLocalhostName: false }) // go-ipfs returns 403 if IP is beautified to 'localhost'
state.apiURLString = state.apiURL.toString()
shouldRestartIpfsClient = true
break
case 'ipfsApiPollMs':
setApiStatusUpdateInterval(change.newValue)
break
case 'customGatewayUrl':
state.gwURL = safeURL(change.newValue, { useLocalhostName: state.useSubdomains })
state.gwURLString = state.gwURL.toString()
break
case 'publicGatewayUrl':
state.pubGwURL = new URL(change.newValue)
state.pubGwURLString = state.pubGwURL.toString()
break
case 'publicSubdomainGatewayUrl':
state.pubSubdomainGwURL = new URL(change.newValue)
state.pubSubdomainGwURLString = state.pubSubdomainGwURL.toString()
break
case 'useCustomGateway':
state.redirect = change.newValue
break
case 'useSubdomains':
state[key] = change.newValue
await browser.storage.local.set({
// We need to update the hostname in customGatewayUrl because:
// 127.0.0.1 - path gateway
// localhost - subdomain gateway
// and we need to use the latter
customGatewayUrl: guiURLString(state.gwURLString, {
useLocalhostName: state.useSubdomains
})
})
// Finally, update proxy settings based on the state
await registerSubdomainProxy(getState, runtime)
break
/* TODO(window.ipfs)
case 'ipfsProxy':
state[key] = change.newValue
// This is window.ipfs proxy, requires update of the content script:
ipfsProxyContentScript = await registerIpfsProxyContentScript()
break */
case 'dnslinkPolicy':
state.dnslinkPolicy = String(change.newValue) === 'false' ? false : change.newValue
if (state.dnslinkPolicy === 'best-effort' && !state.detectIpfsPathHeader) {
await browser.storage.local.set({ detectIpfsPathHeader: true })
}
break
case 'logNamespaces':
shouldReloadExtension = true
state[key] = localStorage.debug = change.newValue
break
default:
state[key] = change.newValue
break
}
}
if ((state.active && shouldRestartIpfsClient) || shouldStopIpfsClient) {
try {
log('stoping ipfs client due to config changes', changes)
await destroyIpfsClient(browser)
} catch (err) {
console.error('[ipfs-companion] Failed to destroy IPFS client', err)
notify('notify_stopIpfsNodeErrorTitle', err.message)
} finally {
ipfs = null
}
if (!state.active) return
try {
log('starting ipfs client with the new config')
ipfs = await initIpfsClient(browser, state)
} catch (err) {
console.error('[ipfs-companion] Failed to init IPFS client', err)
notify(
'notify_startIpfsNodeErrorTitle',
err.name === 'ValidationError' ? err.details[0].message : err.message
)
}
apiStatusUpdate()
}
if (shouldReloadExtension) {
log('reloading extension due to config change')
browser.tabs.reload() // async reload of options page to keep it alive
await browser.runtime.reload()
}
// Post update to Browser Action (if exists) -- this gives UX a snappy feel
await sendStatusUpdateToBrowserAction()
}
// Public API
// (typically attached to a window variable to interact with this companion instance)
const api = {
get ipfs () {
return ipfs
},
get state () {
return state
},
get runtime () {
return runtime
},
get dnslinkResolver () {
return dnslinkResolver
},
get ipfsPathValidator () {
return ipfsPathValidator
},
get notify () {
return notify
},
get ipfsImportHandler () {
return ipfsImportHandler
},
async destroy () {
if (state) {
state.active = false
state.peerCount = -1 // api down
}
if (apiStatusUpdateInterval) {
clearInterval(apiStatusUpdateInterval)
apiStatusUpdateInterval = null
}
/* TODO(window.ipfs)
if (ipfsProxyContentScript) {
ipfsProxyContentScript.unregister()
ipfsProxyContentScript = null
}
*/
if (ipfsProxy) {
await ipfsProxy.destroy()
ipfsProxy = null
}
if (ipfs) {
await destroyIpfsClient(browser)
ipfs = null
}
}
}
return api
}
const rasterIconDefinition = pMemoize((svgPath) => {
const pngPath = (size) => {
// point at precomputed PNG file
const baseName = /\/icons\/(.+)\.svg/.exec(svgPath)[1]
return `/icons/png/${baseName}_${size}.png`
}
// icon sizes to cover ranges from:
// - https://bugs.chromium.org/p/chromium/issues/detail?id=647182
// - https://developer.chrome.com/extensions/manifest/icons
const r19 = pngPath(19)
const r38 = pngPath(38)
const r128 = pngPath(128)
// return computed values to be cached by p-memoize
return { path: { 19: r19, 38: r38, 128: r128 } }
})