This repository was archived by the owner on May 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (42 loc) · 1.32 KB
/
index.js
File metadata and controls
52 lines (42 loc) · 1.32 KB
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
module.exports = function getIpfs (opts) {
opts = opts || {}
return new Promise(function (resolve, reject) {
if (window.ipfs) {
// forward-compatible migration
// https://github.com/ipfs-shipyard/ipfs-companion/issues/589
if (typeof window.ipfs.enable === 'function') {
return resolve(window.ipfs.enable(opts.permissions))
}
// backward-compatible
return resolve(window.ipfs)
}
var onLoad = function () {
var Ipfs = getConstructor(opts.api)
var ipfs = new Ipfs(opts.ipfs)
if (opts.api) return resolve(ipfs)
var onReady = function () {
ipfs.removeListener('error', onError)
resolve(ipfs)
}
var onError = function (err) {
ipfs.removeListener('ready', onReady)
reject(err)
}
ipfs.once('ready', onReady).once('error', onError)
}
if (getConstructor(opts.api)) return onLoad()
var script = document.createElement('script')
script.src = opts.cdn || getCdnUrl(opts.api)
script.onload = onLoad
script.onerror = reject
document.body.appendChild(script)
})
}
function getConstructor (api) {
return api ? window.IpfsApi : window.Ipfs
}
function getCdnUrl (api) {
return api
? 'https://unpkg.com/ipfs-api/dist/index.min.js'
: 'https://unpkg.com/ipfs/dist/index.min.js'
}