-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathoptions.js
135 lines (124 loc) · 4.27 KB
/
options.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
'use strict'
const isFQDN = require('is-fqdn')
const { hasChromeSocketsForTcp } = require('./runtime-checks')
// TODO: enable by default when embedded node is performant enough
const DEFAULT_TO_EMBEDDED_GATEWAY = false && hasChromeSocketsForTcp()
exports.optionDefaults = Object.freeze({
active: true, // global ON/OFF switch, overrides everything else
ipfsNodeType: buildDefaultIpfsNodeType(),
ipfsNodeConfig: buildDefaultIpfsNodeConfig(),
publicGatewayUrl: 'https://ipfs.io',
publicSubdomainGatewayUrl: 'https://dweb.link',
useCustomGateway: true,
noRedirectHostnames: [],
automaticMode: true,
linkify: false,
dnslinkPolicy: 'best-effort',
dnslinkDataPreload: true,
dnslinkRedirect: true,
recoverFailedHttpRequests: true,
detectIpfsPathHeader: true,
preloadAtPublicGateway: true,
catchUnhandledProtocols: true,
displayNotifications: true,
customGatewayUrl: buildCustomGatewayUrl(),
ipfsApiUrl: buildIpfsApiUrl(),
ipfsApiPollMs: 3000,
ipfsProxy: true, // window.ipfs
logNamespaces: 'jsipfs*,ipfs*,libp2p:mdns*,libp2p-delegated*,-*:ipns*,-ipfs:preload*,-ipfs-http-client:request*,-ipfs:http-api*',
importDir: '/ipfs-companion-imports/%Y-%M-%D_%h%m%s/',
openViaWebUI: true
})
function buildCustomGatewayUrl () {
// TODO: make more robust (sync with buildDefaultIpfsNodeConfig)
const port = DEFAULT_TO_EMBEDDED_GATEWAY ? 9091 : 8080
return `http://127.0.0.1:${port}`
}
function buildIpfsApiUrl () {
// TODO: make more robust (sync with buildDefaultIpfsNodeConfig)
const port = DEFAULT_TO_EMBEDDED_GATEWAY ? 5003 : 5001
return `http://127.0.0.1:${port}`
}
function buildDefaultIpfsNodeType () {
// Right now Brave is the only vendor giving us access to chrome.sockets
return DEFAULT_TO_EMBEDDED_GATEWAY ? 'embedded:chromesockets' : 'external'
}
function buildDefaultIpfsNodeConfig () {
return JSON.stringify({
config: {
Addresses: {
Swarm: []
}
}
}, null, 2)
}
// `storage` should be a browser.storage.local or similar
exports.storeMissingOptions = async (read, defaults, storage) => {
const requiredKeys = Object.keys(defaults)
const changes = {}
const has = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key)
for (const key of requiredKeys) {
// limit work to defaults and missing values, skip values other than defaults
if (!has(read, key) || read[key] === defaults[key]) {
const data = await storage.get(key)
if (!has(data, key)) { // detect and fix key without value in storage
changes[key] = defaults[key]
}
}
}
// save all in bulk
await storage.set(changes)
return changes
}
function normalizeGatewayURL (url) {
if (typeof url === 'string') {
url = new URL(url)
}
// https://github.com/ipfs/ipfs-companion/issues/328
if (url.hostname.toLowerCase() === 'localhost') {
url.hostname = '127.0.0.1'
}
// Return string without trailing slash
return url.toString().replace(/\/$/, '')
}
exports.normalizeGatewayURL = normalizeGatewayURL
exports.safeURL = (url) => new URL(normalizeGatewayURL(url))
// convert JS array to multiline textarea
function hostArrayCleanup (array) {
array = array.map(host => host.trim().toLowerCase())
array = [...new Set(array)] // dedup
array = array.filter(Boolean).filter(isFQDN)
array.sort()
return array
}
function hostArrayToText (array) {
return hostArrayCleanup(array).join('\n')
}
function hostTextToArray (text) {
return hostArrayCleanup(text.split('\n'))
}
exports.hostArrayToText = hostArrayToText
exports.hostTextToArray = hostTextToArray
exports.migrateOptions = async (storage) => {
// <= v2.4.4
// DNSLINK: convert old on/off 'dnslink' flag to text-based 'dnslinkPolicy'
const { dnslink } = await storage.get('dnslink')
if (dnslink) {
// migrating old dnslink policy to 'best-effort'
await storage.set({
dnslinkPolicy: 'best-effort',
detectIpfsPathHeader: true
})
await storage.remove('dnslink')
}
// ~ v2.8.x + Brave
// Upgrade js-ipfs to js-ipfs + chrome.sockets
const { ipfsNodeType } = await storage.get('ipfsNodeType')
if (ipfsNodeType === 'embedded' && hasChromeSocketsForTcp()) {
// migrating ipfsNodeType to embedded:chromesockets
await storage.set({
ipfsNodeType: 'embedded:chromesockets',
ipfsNodeConfig: buildDefaultIpfsNodeConfig()
})
}
}