This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
207 lines (175 loc) · 5.67 KB
/
index.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
'use strict'
/* eslint-env browser, webextensions */
const root = require('window-or-global')
const httpClient = require('ipfs-http-client')
const tryCompanion = require('./companion')
const tryWindow = require('./window.ipfs')
const tryApi = require('./js-ipfs-api')
const tryJsIpfs = require('./js-ipfs')
const { isURL, isMultiaddress } = require('./utils')
const defaultOptions = {
tryWindow: true,
tryCompanion: true,
tryApi: true,
tryJsIpfs: false,
defaultApiAddress: '/ip4/127.0.0.1/tcp/5001',
ipfsConnectionTest: (ipfs) => {
// ipfs connection is working if can we fetch the empty directtory.
return ipfs.get('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
}
}
module.exports = (opts) => {
opts = Object.assign({}, defaultOptions, opts)
const defaultState = {
defaultApiAddress: opts.defaultApiAddress,
apiAddress: getUserProvidedIpfsApi(),
provider: null, // 'window.ipfs' || 'ipfs-companion' || 'js-ipfs-api' || 'js-ipfs'
failed: false,
ready: false,
invalidAddress: false
}
// Throws a warning if the user wants to use js-ipfs but didn't pass an instance.
if (opts.tryJsIpfs && !opts.getJsIpfs) {
console.warn('When enabling tryJsIpfs, you must provide a js-ipfs instance as opts.getJsIpfs. It will be disabled for now.')
opts.tryJsIpfs = false
}
let ipfs = null
return {
name: 'ipfs',
reducer (state, { type, payload }) {
state = state || defaultState
if (type === 'IPFS_INIT_STARTED') {
return Object.assign({}, state, { ready: false })
}
if (type === 'IPFS_INIT_FINISHED') {
// dont store ipfs in redux store!
ipfs = payload.ipfs
const opts = {
ready: true,
failed: false,
provider: payload.provider
}
if (payload.apiAddress) {
opts.apiAddress = payload.apiAddress
}
return Object.assign({}, state, opts)
}
if (type === 'IPFS_STOPPED') {
return Object.assign({}, state, { ready: false, failed: false })
}
if (type === 'IPFS_INIT_FAILED') {
return Object.assign({}, state, { ready: false, failed: true })
}
if (type === 'IPFS_API_ADDRESS_UPDATED') {
return Object.assign({}, state, { apiAddress: payload, invalidAddress: false })
}
if (type === 'IPFS_API_ADDRESS_INVALID') {
return Object.assign({}, state, { invalidAddress: true })
}
if (type === 'IPFS_API_ADDRESS_INVALID_DISMISS') {
return Object.assign({}, state, { invalidAddress: false })
}
return state
},
getExtraArgs () {
return { getIpfs: () => ipfs }
},
selectIpfsReady: state => state.ipfs.ready,
selectIpfsProvider: state => state.ipfs.provider,
selectIpfsApiAddress: state => state.ipfs.apiAddress,
selectIpfsInvalidAddress: state => state.ipfs.invalidAddress,
selectIpfsInitFailed: state => state.ipfs.failed,
doInitIpfs: () => async (store) => {
await getIpfs(opts, store)
},
doStopIpfs: () => async ({ dispatch }) => {
ipfs.stop(() => {
dispatch({ type: 'IPFS_STOPPED' })
})
},
doUpdateIpfsApiAddress: (addr) => (store) => {
if (!isMultiaddress(addr) && !isURL(addr)) {
store.dispatch({ type: 'IPFS_API_ADDRESS_INVALID' })
return
}
saveUserOpts('ipfsApi', addr)
store.dispatch({ type: 'IPFS_API_ADDRESS_UPDATED', payload: addr })
getIpfs(Object.assign({}, opts, {
tryWindow: false,
tryJsIpfs: false
}), store)
},
doDismissIpfsInvalidAddress: () => ({ dispatch }) => {
dispatch({ type: 'IPFS_API_ADDRESS_INVALID_DISMISS' })
}
}
}
async function getIpfs (opts, { store, getState, dispatch }) {
dispatch({ type: 'IPFS_INIT_STARTED' })
const { ipfsConnectionTest } = opts
if (opts.tryCompanion) {
const res = await tryCompanion({ root, ipfsConnectionTest })
if (res) {
return dispatch({ type: 'IPFS_INIT_FINISHED', payload: res })
}
}
if (opts.tryWindow) {
const res = await tryWindow({ root, ipfsConnectionTest })
if (res) {
return dispatch({ type: 'IPFS_INIT_FINISHED', payload: res })
}
}
if (opts.tryApi) {
const { apiAddress, defaultApiAddress } = getState().ipfs
const { location } = root
const res = await tryApi({ apiAddress, defaultApiAddress, location, httpClient, ipfsConnectionTest })
if (res) {
return dispatch({ type: 'IPFS_INIT_FINISHED', payload: res })
}
}
if (opts.tryJsIpfs) {
const jsIpfsOpts = getUserOpts('ipfsOpts') || {}
const { getJsIpfs } = opts
const res = await tryJsIpfs({ jsIpfsOpts, getJsIpfs, ipfsConnectionTest })
if (res) {
return dispatch({ type: 'IPFS_INIT_FINISHED', payload: res })
}
}
dispatch({ type: 'IPFS_INIT_FAILED' })
}
function getUserOpts (key) {
let userOpts = null
if (root.localStorage) {
try {
userOpts = root.localStorage.getItem(key)
} catch (error) {
console.log(`Error reading '${key}' value from localStorage`, error)
}
try {
return JSON.parse(userOpts)
} catch (_) {
// res was probably a string, so pass it on.
return userOpts
}
}
}
function saveUserOpts (key, val) {
if (root.localStorage) {
try {
if (typeof val !== 'string') {
val = JSON.stringify(val)
}
root.localStorage.setItem(key, val)
} catch (error) {
console.log(`Error writing '${key}' value to localStorage`, error)
}
}
}
function getUserProvidedIpfsApi () {
const ipfsApi = getUserOpts('ipfsApi')
if (ipfsApi && !isMultiaddress(ipfsApi) && !isURL(ipfsApi)) {
console.warn(`The ipfsApi address ${ipfsApi} is invalid.`)
return null
}
return ipfsApi
}