@@ -11,48 +11,78 @@ const debug = require('debug')
11
11
const log = debug ( 'ipfs-companion:precache' )
12
12
log . error = debug ( 'ipfs-companion:precache:error' )
13
13
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)
19
16
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
+ }
21
27
]
28
+ module . exports . PRECACHE_ARCHIVES = PRECACHE_ARCHIVES
22
29
23
30
/**
24
31
* Adds important assets such as Web UI to the local js-ipfs-repo.
25
32
* This ensures they load instantly, even in offline environments.
26
33
*/
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 )
40
54
continue
41
55
}
42
- log . error ( `error while processing ${ tarPath } ` , err )
43
56
}
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 )
46
60
}
47
61
}
48
62
}
49
63
50
64
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 )
54
85
}
55
- return false
56
86
}
57
87
58
88
async function importTar ( ipfs , tarReader , expectedCid ) {
@@ -84,7 +114,7 @@ async function importTar (ipfs, tarReader, expectedCid) {
84
114
85
115
const root = results . find ( e => e . cid . toString ( multibaseName ) === expectedCid )
86
116
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 } ` )
88
118
}
89
119
}
90
120
0 commit comments