-
Notifications
You must be signed in to change notification settings - Fork 490
/
config.js
69 lines (59 loc) · 1.87 KB
/
config.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
import toUri from 'multiaddr-to-uri'
import { createAsyncResourceBundle, createSelector } from 'redux-bundler'
const bundle = createAsyncResourceBundle({
name: 'config',
getPromise: async ({ getIpfs }) => {
// SEE: https://github.com/ipfs/js-ipfs-api/issues/822
const rawConf = await getIpfs().config.get()
let conf
if (Buffer.isBuffer(rawConf)) {
conf = rawConf.toString()
} else {
conf = JSON.stringify(rawConf, null, '\t')
}
// stringy json for quick compares
return conf
},
staleAfter: 60000,
persist: false,
checkIfOnline: false
})
// derive the object from the stringy json
bundle.selectConfigObject = createSelector(
`selectConfig`,
(configStr) => JSON.parse(configStr)
)
bundle.selectApiUrl = createSelector(
`selectConfigObject`,
(config) => getURLFromAddress('API', config) || 'https://ipfs.io'
)
bundle.selectGatewayUrl = createSelector(
`selectConfigObject`,
(config) => getURLFromAddress('Gateway', config) || 'https://ipfs.io'
)
// TODO: this is a work-around for IPFS companion blocking the config API
// see: https://github.com/ipfs-shipyard/ipfs-companion/issues/454
bundle.selectIsConfigBlocked = createSelector(
`selectConfigRaw`,
({ errorType }) => errorType === 'Access to config.get API is globally blocked for window.ipfs'
)
// Fetch the config if we don't have it or it's more than `staleAfter` ms old
bundle.reactConfigFetch = createSelector(
'selectConfigShouldUpdate',
'selectIpfsReady',
(shouldUpdate, ipfsReady) => {
if (shouldUpdate && ipfsReady) {
return { actionCreator: 'doFetchConfig' }
}
}
)
function getURLFromAddress (name, config) {
try {
const address = config.Addresses[name]
return toUri(address).replace('tcp://', 'http://')
} catch (error) {
console.log(`Failed to get url from Addresses.${name}`, error)
return null
}
}
export default bundle