Skip to content

Commit 4dda1b1

Browse files
committed
wip: js-ipfs in Brave
- the latest js-ipfs builds and runs in the browser - 4x delegates and preloads enabled - 2x webrtc star provides signaling Known Issues: - the node starts, but struggles to find content, and there is a separate issue of infinite restart when user enables and disables it too fast - import via quick import page fails due to API discrepancy
1 parent 2f3f532 commit 4dda1b1

File tree

7 files changed

+122
-63
lines changed

7 files changed

+122
-63
lines changed

add-on/src/lib/ipfs-client/embedded-chromesockets/config.js

+59-34
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ const chromeDefaultOpts = {
3030
but Web UI needs API (can't use window.ipfs due to sandboxing)
3131
*/
3232
Swarm: [
33-
// optional ws-star signaling provides a backup for non-LAN peer discovery
33+
// additional signaling service provides a backup for non-LAN peer discovery
3434
// (this will be removed when autorelay and DHT are stable in js-ipfs)
35-
'/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star'
35+
'/dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star',
36+
'/dns4/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star'
3637
],
3738
// Delegated Content and Peer Routing: https://github.com/ipfs/js-ipfs/pull/2195
3839
Delegates: [
40+
'/dns4/node0.delegate.ipfs.io/tcp/443/https',
3941
'/dns4/node1.delegate.ipfs.io/tcp/443/https',
40-
'/dns4/node0.delegate.ipfs.io/tcp/443/https'
42+
'/dns4/node2.delegate.ipfs.io/tcp/443/https',
43+
'/dns4/node3.delegate.ipfs.io/tcp/443/https'
4144
]
4245
},
4346
Discovery: {
@@ -49,47 +52,51 @@ const chromeDefaultOpts = {
4952
Swarm: {
5053
ConnMgr: {
5154
LowWater: 50,
52-
HighWater: 150
55+
HighWater: 200
5356
}
5457
},
5558
Bootstrap: [
56-
// Prioritize TCP Bootstrappers from https://github.com/ipfs/js-ipfs/blob/v0.37.1/src/core/runtime/config-nodejs.js#L22
57-
'/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
58-
'/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
59-
'/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
60-
'/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
61-
'/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
62-
'/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
63-
'/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
64-
'/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
65-
'/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
66-
'/ip6/2604:a880:1:20::1f9:9001/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
67-
'/ip6/2604:a880:1:20::203:d001/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
68-
'/ip6/2604:a880:0:1010::23:d001/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
69-
'/ip6/2400:6180:0:d0::151:6001/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
70-
'/ip6/2604:a880:800:10::4a:5001/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
71-
'/ip6/2a03:b0c0:0:1010::23:1001/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
72-
'/ip6/2a03:b0c0:1:d0::e7:1/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
73-
'/ip6/2604:a880:1:20::1d9:6001/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
59+
// Prioritize TCP Bootstrappers from https://github.com/ipfs/js-ipfs/blob/ipfs%400.49.0/packages/ipfs/src/core/runtime/config-nodejs.js
60+
'/ip4/104.236.176.52/tcp/4001/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
61+
'/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
62+
'/ip4/104.236.179.241/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
63+
'/ip4/162.243.248.213/tcp/4001/p2p/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
64+
'/ip4/128.199.219.111/tcp/4001/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
65+
'/ip4/104.236.76.40/tcp/4001/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
66+
'/ip4/178.62.158.247/tcp/4001/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
67+
'/ip4/178.62.61.185/tcp/4001/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
68+
'/ip4/104.236.151.122/tcp/4001/p2p/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
69+
'/ip6/2604:a880:1:20::1f9:9001/tcp/4001/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
70+
'/ip6/2604:a880:1:20::203:d001/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
71+
'/ip6/2604:a880:0:1010::23:d001/tcp/4001/p2p/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
72+
'/ip6/2400:6180:0:d0::151:6001/tcp/4001/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
73+
'/ip6/2604:a880:800:10::4a:5001/tcp/4001/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
74+
'/ip6/2a03:b0c0:0:1010::23:1001/tcp/4001/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
75+
'/ip6/2a03:b0c0:1:d0::e7:1/tcp/4001/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
76+
'/ip6/2604:a880:1:20::1d9:6001/tcp/4001/p2p/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
7477
// Twist: connect to preload nodes, but over tcp :)
75-
'/dns4/node0.preload.ipfs.io/tcp/4001/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
76-
'/dns4/node1.preload.ipfs.io/tcp/4001/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6',
77-
// WebSockets versions from https://github.com/ipfs/js-ipfs/blob/v0.37.1/src/core/runtime/config-browser.js#L20
78+
'/dns4/node0.preload.ipfs.io/tcp/443/wss/p2p/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
79+
'/dns4/node1.preload.ipfs.io/tcp/443/wss/p2p/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6',
80+
'/dns4/node2.preload.ipfs.io/tcp/443/wss/p2p/QmV7gnbW5VTcJ3oyM2Xk1rdFBJ3kTkvxc87UFGsun29STS',
81+
'/dns4/node3.preload.ipfs.io/tcp/443/wss/p2p/QmY7JB6MQXhxHvq7dBDh4HpbH29v4yE9JRadAVpndvzySN',
82+
// WebSockets versions from https://github.com/ipfs/js-ipfs/blob/ipfs%400.49.0/packages/ipfs/src/core/runtime/config-browser.js
7883
'/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
7984
'/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
8085
'/dns4/sfo-3.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
8186
'/dns4/sgp-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
8287
'/dns4/nyc-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
8388
'/dns4/nyc-2.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
84-
'/dns4/node0.preload.ipfs.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
85-
'/dns4/node1.preload.ipfs.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6'
89+
'/dns4/node0.preload.ipfs.io/tcp/443/wss/p2p/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
90+
'/dns4/node1.preload.ipfs.io/tcp/443/wss/p2p/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6',
91+
'/dns4/node2.preload.ipfs.io/tcp/443/wss/p2p/QmV7gnbW5VTcJ3oyM2Xk1rdFBJ3kTkvxc87UFGsun29STS',
92+
'/dns4/node3.preload.ipfs.io/tcp/443/wss/p2p/QmY7JB6MQXhxHvq7dBDh4HpbH29v4yE9JRadAVpndvzySN'
8693
]
8794
},
88-
// preload: { enabled: false, addresses: [] }
89-
/* TODO: when we have p2p transport, are preloads still needed? should Brave have own nodes? */
9095
preload: {
9196
enabled: true,
9297
addresses: [
98+
'/dns4/node3.preload.ipfs.io/tcp/443/https',
99+
'/dns4/node2.preload.ipfs.io/tcp/443/https',
93100
'/dns4/node1.preload.ipfs.io/tcp/443/https',
94101
'/dns4/node0.preload.ipfs.io/tcp/443/https'
95102
]
@@ -140,11 +147,29 @@ async function buildConfig (opts, log) {
140147
},
141148
webRTCStar: {
142149
enabled: true
150+
},
151+
dht: {
152+
kBucketSize: 20,
153+
enabled: false,
154+
clientMode: false, /* TODO experiment with true */
155+
randomWalk: {
156+
enabled: false
157+
}
158+
},
159+
pubsub: {
160+
enabled: true,
161+
emitSelf: true
143162
}
144163
}
145164
})
146165

147-
libp2pOptions.metrics = { enabled: false }
166+
/* TODO: find out the best valus for chrome apps context
167+
libp2pOptions.dialer = {
168+
// https://github.com/ipfs/js-ipfs/blob/ipfs%400.49.0/packages/ipfs/src/core/runtime/libp2p-browser.js#L14
169+
maxParallelDials: 150, // 150 total parallel multiaddr dials
170+
maxDialsPerPeer: 4, // Allow 4 multiaddrs to be dialed per peer in parallel
171+
dialTimeout: 10e3 // 10 second dial timeout per peer dial
172+
}*/
148173

149174
log('initializing libp2p with libp2pOptions', libp2pOptions)
150175
return new Libp2p(libp2pOptions)
@@ -170,11 +195,11 @@ async function buildConfig (opts, log) {
170195
return ipfsNodeConfig
171196
}
172197

173-
async function syncConfig (ipfs, opts, log) {
198+
async function syncConfig (liveConfig, log) {
174199
const storedConfig = await browser.storage.local.get('ipfsNodeConfig')
175200
if (storedConfig && storedConfig.ipfsNodeConfig) {
176-
const maGw = await ipfs.config.get('Addresses.Gateway')
177-
const maApi = await ipfs.config.get('Addresses.API')
201+
const maGw = liveConfig.config.Addresses.Gateway
202+
const maApi = liveConfig.config.Addresses.API
178203
const httpGw = multiaddr2httpUrl(maGw)
179204
const httpApi = multiaddr2httpUrl(maApi)
180205
// update config in browser.storage to ports from js-ipfs instance
@@ -191,7 +216,7 @@ async function syncConfig (ipfs, opts, log) {
191216
changes.ipfsNodeConfig = JSON.stringify(cfg, null, 2)
192217
}
193218
// update runtime config in place
194-
Object.assign(opts, changes)
219+
Object.assign(liveConfig, changes)
195220
// save config to browser.storage (triggers async client restart if ports changed)
196221
log(`synchronizing ipfsNodeConfig with customGatewayUrl (${changes.customGatewayUrl}) and ipfsApiUrl (${changes.ipfsApiUrl})`)
197222
await browser.storage.local.set(changes)

add-on/src/lib/ipfs-client/embedded-chromesockets/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ exports.init = async function init (opts) {
3030

3131
log('creating js-ipfs with opts: ', ipfsOpts)
3232
node = await Ipfs.create(ipfsOpts)
33+
await node.start()
3334

3435
log('starting HTTP servers with opts: ', ipfsOpts)
3536
const httpServers = new HttpApi(node, ipfsOpts)
3637
nodeHttpApi = await httpServers.start()
37-
await syncConfig(node, opts, log)
38+
39+
await syncConfig(ipfsOpts, log)
40+
3841
return node
3942
}
4043

add-on/src/lib/ipfs-client/index.js

+39-24
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,59 @@ const log = debug('ipfs-companion:client')
77
log.error = debug('ipfs-companion:client:error')
88

99
const browser = require('webextension-polyfill')
10+
const mortice = require('mortice')
1011
const external = require('./external')
1112
const embedded = require('./embedded')
1213
const embeddedWithChromeSockets = require('./embedded-chromesockets')
1314
const { precache } = require('../precache')
1415

16+
// ensure single client at all times, and no overlap between init and destroy
17+
// TODO: replace mortice with state machine?
18+
const mutex = mortice('ipfs-client-lock')
1519
let client
1620

1721
async function initIpfsClient (opts) {
18-
log('init ipfs client')
1922
await destroyIpfsClient()
20-
switch (opts.ipfsNodeType) {
21-
case 'embedded':
22-
client = embedded
23-
break
24-
case 'embedded:chromesockets':
25-
client = embeddedWithChromeSockets
26-
break
27-
case 'external':
28-
client = external
29-
break
30-
default:
31-
throw new Error(`Unsupported ipfsNodeType: ${opts.ipfsNodeType}`)
32-
}
23+
const release = await mutex.writeLock()
24+
try {
25+
log('init ipfs client')
26+
switch (opts.ipfsNodeType) {
27+
case 'embedded':
28+
client = embedded
29+
break
30+
case 'embedded:chromesockets':
31+
client = embeddedWithChromeSockets
32+
break
33+
case 'external':
34+
client = external
35+
break
36+
default:
37+
throw new Error(`Unsupported ipfsNodeType: ${opts.ipfsNodeType}`)
38+
}
3339

34-
const instance = await client.init(opts)
35-
easeApiChanges(instance)
36-
_reloadIpfsClientDependents(instance) // async (API is present)
37-
return instance
40+
const instance = await client.init(opts)
41+
easeApiChanges(instance)
42+
_reloadIpfsClientDependents(instance) // async (API is present)
43+
return instance
44+
} finally {
45+
release()
46+
}
3847
}
3948

4049
async function destroyIpfsClient () {
41-
if (client && client.destroy) {
42-
try {
43-
await client.destroy()
44-
} finally {
45-
client = null
46-
await _reloadIpfsClientDependents() // sync (API stopped working)
50+
const release = await mutex.writeLock()
51+
log('destroy ipfs client')
52+
try {
53+
if (client && client.destroy) {
54+
try {
55+
await client.destroy()
56+
} finally {
57+
client = null
58+
await _reloadIpfsClientDependents() // sync (API stopped working)
59+
}
4760
}
61+
} finally {
62+
release()
4863
}
4964
}
5065

add-on/src/popup/browser-action/ipfs-version.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function statusEntry ({ label, labelLegend, title, value, check, valueClass = ''
1111
value = value || value === 0 ? value : offline
1212
return html`
1313
<div title="${labelLegend}" class="ma0 pa0" style="line-height: 0.25">
14-
<span class="f7 tr monospace force-select-all ${valueClass}" title="${title}">${value}</span>
14+
<span class="f7 tr monospace force-select-all ${valueClass}" title="${title}">${value.substring(0, 13)}</span>
1515
</div>
1616
`
1717
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"iso-stream-http": "0.1.2",
7676
"stream-http": "npm:[email protected]",
7777
"stream": "npm:[email protected]",
78-
"prom-client": "npm:[email protected]",
78+
"prom-client": "file:./scripts/prom-client-stub",
7979
"prometheus-gc-stats": "npm:[email protected]",
8080
"pull-to-stream": "0.1.1"
8181
},
@@ -147,6 +147,7 @@
147147
"it-tar": "1.2.2",
148148
"lru-cache": "5.1.1",
149149
"merge-options": "2.0.0",
150+
"mortice": "2.0.0",
150151
"multiaddr": "7.4.3",
151152
"multiaddr-to-uri": "5.0.0",
152153
"p-memoize": "4.0.0",

scripts/prom-client-stub/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
// This is a basic stub to build node-like js-ipfs
4+
// for use in Brave, but without prometheus libs which are not compatible
5+
// with browser context
6+
function Gauge () {}
7+
module.exports = {
8+
Gauge,
9+
register: {
10+
clear () { }
11+
}
12+
}

yarn.lock

+5-2
Original file line numberDiff line numberDiff line change
@@ -10069,7 +10069,7 @@ moment@^2.10.6:
1006910069
resolved "https://registry.yarnpkg.com/moment/-/moment-2.27.0.tgz#8bff4e3e26a236220dfe3e36de756b6ebaa0105d"
1007010070
integrity sha512-al0MUK7cpIcglMv3YF13qSgdAIqxHTO7brRtaz3DlSULbqfazqkc5kEjNrLDOM7fsjshoFIihnU8snrP7zUvhQ==
1007110071

10072-
mortice@^2.0.0:
10072+
mortice@2.0.0, mortice@^2.0.0:
1007310073
version "2.0.0"
1007410074
resolved "https://registry.yarnpkg.com/mortice/-/mortice-2.0.0.tgz#7be171409c2115561ba3fc035e4527f9082eefde"
1007510075
integrity sha512-rXcjRgv2MRhpwGHErxKcDcp5IoA9CPvPFLXmmseQYIuQ2fSVu8tsMKi/eYUXzp/HH1s6y3IID/GwRqlSglDdRA==
@@ -11930,7 +11930,10 @@ progress@^2.0.0, progress@^2.0.1:
1193011930
resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8"
1193111931
integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==
1193211932

11933-
prom-client@^11.5.3, prom-client@^12.0.0, "prom-client@npm:[email protected]", prometheus-gc-stats@^0.6.0, "prometheus-gc-stats@npm:[email protected]", "v8@npm:[email protected]":
11933+
prom-client@^11.5.3, prom-client@^12.0.0, "prom-client@file:./scripts/prom-client-stub":
11934+
version "0.0.0"
11935+
11936+
prometheus-gc-stats@^0.6.0, "prometheus-gc-stats@npm:[email protected]", "v8@npm:[email protected]":
1193411937
version "0.0.2"
1193511938
resolved "https://registry.yarnpkg.com/empty-module/-/empty-module-0.0.2.tgz#13b4dd8d4afeddd35e50c18dcd788c51087f1545"
1193611939
integrity sha1-E7TdjUr+3dNeUMGNzXiMUQh/FUU=

0 commit comments

Comments
 (0)