Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2236ecf

Browse files
committedSep 29, 2020
refactor: remove webui TAR from Chromium bundle
We were bundling TAR inside of Chromium package to make it work instantly when opened in Brave. Unfortunately it added 10MB to the package, which meant one-click install was slow in Brave, but also on every other Chromium browser. This replaces bundled TAR with prefetching it from a public gateway on the first run. The idea is that if user had access to Chrome Web Store to install extension, they have access to the gateway, so no need for bundling. We also precache regular webui in Chromium and Firefox via ipfs.refs
1 parent 7ae89e6 commit 2236ecf

File tree

4 files changed

+72
-44
lines changed

4 files changed

+72
-44
lines changed
 

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async function initIpfsClient (opts) {
3434

3535
const instance = await client.init(opts)
3636
easeApiChanges(instance)
37-
_reloadIpfsClientDependents(instance) // async (API is present)
37+
_reloadIpfsClientDependents(instance, opts) // async (API is present)
3838
return instance
3939
}
4040

@@ -71,9 +71,9 @@ async function _reloadIpfsClientDependents (instance, opts) {
7171
}
7272
}
7373
// online only
74-
if (client && instance) {
74+
if (client && instance && opts) {
7575
// add important data to local ipfs repo for instant load
76-
setTimeout(() => precache(instance), 10000)
76+
setTimeout(() => precache(instance, opts), 5000)
7777
}
7878
}
7979

‎add-on/src/lib/precache.js

+57-27
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,78 @@ const debug = require('debug')
1111
const log = debug('ipfs-companion:precache')
1212
log.error = debug('ipfs-companion:precache:error')
1313

14-
// Web UI release that should be precached
15-
// WARNING: do not remove this constant, as its used in package.json
16-
const webuiCid = 'bafybeigkbbjnltbd4ewfj7elajsbnjwinyk6tiilczkqsibf3o7dcr6nn4' // v2.9.0
17-
module.exports.precachedWebuiCid = webuiCid
18-
14+
// TODO: make below smarter by detecting CID behind :5001/webui
15+
// (or webui.ipfs.io if bledding edge is enabled)
1916
const PRECACHE_ARCHIVES = [
20-
{ tarPath: '/dist/precache/webui.tar', cid: webuiCid }
17+
{
18+
nodeType: 'external',
19+
name: 'webui v2.11.2',
20+
cid: 'bafybeifekmcbbi4nwyj4aasti6x3nuhyli464wfjjfdjg4xnz53lhyiedq'
21+
},
22+
{
23+
nodeType: 'embedded:chromesockets',
24+
name: 'webui v2.9.0 for js-ipfs in Brave',
25+
cid: 'bafybeigkbbjnltbd4ewfj7elajsbnjwinyk6tiilczkqsibf3o7dcr6nn4'
26+
}
2127
]
28+
module.exports.PRECACHE_ARCHIVES = PRECACHE_ARCHIVES
2229

2330
/**
2431
* Adds important assets such as Web UI to the local js-ipfs-repo.
2532
* This ensures they load instantly, even in offline environments.
2633
*/
27-
module.exports.precache = async (ipfs) => {
28-
for (const { cid, tarPath } of PRECACHE_ARCHIVES) {
29-
if (!await inRepo(ipfs, cid)) {
30-
try {
31-
const { body } = await fetch(tarPath)
32-
log(`importing ${tarPath} to js-ipfs-repo`)
33-
await importTar(ipfs, body.getReader(), cid)
34-
log(`${tarPath} successfully cached under CID ${cid}`)
35-
} catch (err) {
36-
if (err.message === 'Error in body stream') {
37-
// This error means .tar is missing from the extension bundle:
38-
// It is the case in Firefox, due to https://github.com/ipfs-shipyard/ipfs-webui/issues/959
39-
log(`unable to find/read ${tarPath}, skipping`)
34+
module.exports.precache = async (ipfs, state) => {
35+
for (const { name, cid, nodeType } of PRECACHE_ARCHIVES) {
36+
if (state.ipfsNodeType !== nodeType) continue
37+
if (await inRepo(ipfs, cid)) {
38+
log(`${name} (${cid}) already in local repo, skipping import`)
39+
continue
40+
}
41+
log(`importing ${name} (${cid}) to js-ipfs-repo`)
42+
43+
// prefetch over HTTP when in Brave
44+
if (state.ipfsNodeType === 'embedded:chromesockets') {
45+
await preloadOverHTTP(ipfs, state, cid)
46+
continue
47+
}
48+
49+
// prefetch over IPFS
50+
try {
51+
for await (const ref of ipfs.refs(cid, { recursive: true })) {
52+
if (ref.err) {
53+
log.error(`error while preloading ${name} (${cid})`, ref.err)
4054
continue
4155
}
42-
log.error(`error while processing ${tarPath}`, err)
4356
}
44-
} else {
45-
log(`${cid} already in local repo, skipping import`)
57+
log(`${name} successfully cached under CID ${cid}`)
58+
} catch (err) {
59+
log.error(`error while processing ${name}`, err)
4660
}
4761
}
4862
}
4963

5064
async function inRepo (ipfs, cid) {
51-
for await (const ref of ipfs.refs.local()) {
52-
if (ref.err) return false
53-
if (ref.ref === cid) return true
65+
// dag.get in offline mode will throw block is not present in local repo
66+
// (we also have timeout as a failsafe)
67+
try {
68+
await ipfs.dag.get(cid, { offline: true, timeout: 5000 })
69+
return true
70+
} catch (_) {
71+
return false
72+
}
73+
}
74+
75+
// Downloads CID from a public gateway
76+
// (alternative to ipfs.refs -r)
77+
async function preloadOverHTTP (ipfs, state, cid) {
78+
try {
79+
const url = `${state.pubGwURLString}api/v0/get?arg=${cid}&archive=true`
80+
const { body } = await fetch(url)
81+
await importTar(ipfs, body.getReader(), cid)
82+
log(`successfully cached under CID ${cid}`)
83+
} catch (err) {
84+
log.error(`error while processing ${cid}`, err)
5485
}
55-
return false
5686
}
5787

5888
async function importTar (ipfs, tarReader, expectedCid) {
@@ -84,7 +114,7 @@ async function importTar (ipfs, tarReader, expectedCid) {
84114

85115
const root = results.find(e => e.cid.toString(multibaseName) === expectedCid)
86116
if (!root) {
87-
throw new Error('imported CID does not match expected one')
117+
throw new Error(`imported CID (${root}) does not match expected one: ${expectedCid}`)
88118
}
89119
}
90120

‎add-on/src/lib/state.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint-env browser, webextensions */
33

44
const { safeURL } = require('./options')
5-
const { precachedWebuiCid } = require('./precache')
5+
const { PRECACHE_ARCHIVES } = require('./precache')
66
const offlinePeerCount = -1
77

88
function initState (options, overrides) {
@@ -48,12 +48,12 @@ function initState (options, overrides) {
4848
// Did user opt-in for rolling release published on DNSLink?
4949
if (state.useLatestWebUI) return `${state.gwURLString}ipns/webui.ipfs.io/`
5050

51-
// Below is needed to make webui work for embedded js-ipfs
52-
// TODO: revisit if below is still needed after upgrading to js-ipfs >= 44
53-
const webuiUrl = state.ipfsNodeType === 'embedded:chromesockets'
54-
? `${state.gwURLString}ipfs/${precachedWebuiCid}/`
55-
: `${state.apiURLString}webui`
56-
return webuiUrl
51+
// Below is needed to make webui work for embedded js-ipfs in Brave
52+
if (state.ipfsNodeType === 'embedded:chromesockets') {
53+
const precached = PRECACHE_ARCHIVES.find(a => a.nodeType === 'embedded:chromesockets')
54+
if (precached) return `${state.gwURLString}ipfs/${precached.cid}/`
55+
}
56+
return `${state.apiURLString}webui`
5757
}
5858
})
5959
// apply optional overrides

‎package.json

+5-7
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,20 @@
2323
"build:bundle-all": "cross-env RELEASE_CHANNEL=${RELEASE_CHANNEL:=dev} run-s bundle:chromium bundle:brave:$RELEASE_CHANNEL bundle:firefox:$RELEASE_CHANNEL",
2424
"build:rename-artifacts": "./scripts/rename-artifacts.js",
2525
"precache:clean": "shx rm -rf add-on/dist/precache",
26-
"precache:webui:cid": "shx grep 'const webuiCid' add-on/src/lib/precache.js | shx sed \"s/^const webuiCid = '//\" | shx sed \"s/'.*$//\"",
27-
"precache:webui": "shx mkdir -p add-on/dist/precache && ipfs-or-gateway -c $(npm run -s precache:webui:cid) -p add-on/dist/precache/webui.tar --archive",
2826
"bundle": "run-s bundle:*",
29-
"bundle:chromium": "run-s precache:webui && shx cat add-on/manifest.common.json add-on/manifest.chromium.json | json --deep-merge > add-on/manifest.json && web-ext build -a build/chromium && run-s build:rename-artifacts",
30-
"bundle:firefox": "run-s precache:clean && shx cat add-on/manifest.common.json add-on/manifest.firefox.json | json --deep-merge > add-on/manifest.json && web-ext build -a build/firefox/ && run-s build:rename-artifacts",
27+
"bundle:chromium": "shx cat add-on/manifest.common.json add-on/manifest.chromium.json | json --deep-merge > add-on/manifest.json && web-ext build -a build/chromium && run-s build:rename-artifacts",
28+
"bundle:firefox": "shx cat add-on/manifest.common.json add-on/manifest.firefox.json | json --deep-merge > add-on/manifest.json && web-ext build -a build/firefox/ && run-s build:rename-artifacts",
3129
"bundle:firefox:dev": "npm run bundle:firefox",
3230
"bundle:firefox:stable": "npm run bundle:firefox",
33-
"bundle:firefox:beta": "run-s precache:clean && shx cat add-on/manifest.common.json add-on/manifest.firefox.json add-on/manifest.firefox-beta.json | json --deep-merge > add-on/manifest.json && web-ext build -a build/firefox/ && run-s build:rename-artifacts",
31+
"bundle:firefox:beta": "shx cat add-on/manifest.common.json add-on/manifest.firefox.json add-on/manifest.firefox-beta.json | json --deep-merge > add-on/manifest.json && web-ext build -a build/firefox/ && run-s build:rename-artifacts",
3432
"bundle:fennec": "npm run bundle:firefox",
3533
"bundle:fennec:dev": "npm run bundle:firefox:dev",
3634
"bundle:fennec:stable": "npm run bundle:firefox:stable",
3735
"bundle:fennec:beta": "npm run bundle:firefox:beta",
38-
"bundle:brave": "run-s precache:webui && shx cat add-on/manifest.common.json add-on/manifest.chromium.json add-on/manifest.brave.json | json --deep-merge > add-on/manifest.json && web-ext build -a build/brave/ && run-s build:rename-artifacts",
36+
"bundle:brave": "shx cat add-on/manifest.common.json add-on/manifest.chromium.json add-on/manifest.brave.json | json --deep-merge > add-on/manifest.json && web-ext build -a build/brave/ && run-s build:rename-artifacts",
3937
"bundle:brave:dev": "npm run bundle:brave",
4038
"bundle:brave:stable": "npm run bundle:brave",
41-
"bundle:brave:beta": "run-s precache:webui && shx cat add-on/manifest.common.json add-on/manifest.chromium.json add-on/manifest.brave.json add-on/manifest.brave-beta.json | json --deep-merge > add-on/manifest.json && web-ext build -a build/brave/ && run-s build:rename-artifacts",
39+
"bundle:brave:beta": "shx cat add-on/manifest.common.json add-on/manifest.chromium.json add-on/manifest.brave.json add-on/manifest.brave-beta.json | json --deep-merge > add-on/manifest.json && web-ext build -a build/brave/ && run-s build:rename-artifacts",
4240
"watch": "npm-run-all build:copy --parallel watch:*",
4341
"watch:js": "run-p watch:js:*",
4442
"watch:js:webpack": "webpack --watch --progress -d --devtool inline-source-map --config ./webpack.config.js",

0 commit comments

Comments
 (0)
Please sign in to comment.