@@ -14,50 +14,41 @@ export const ipfsIsWorking = async ipfs => {
14
14
}
15
15
} ;
16
16
17
- const getIpfs = ( permissions = DEFAULT_PERMISSIONS ) => {
18
- return new Promise ( async ( resolve , reject ) => {
19
- // if already instantiated
20
- if ( ipfsInstance ) {
21
- return resolve ( ipfsInstance ) ;
22
- }
23
-
24
- if ( window . ipfs ) {
25
- let ipfs = window . ipfs ;
26
- if ( window . ipfs . enable ) {
27
- console . log ( "window.ipfs.enable is available!" ) ;
28
- // if user set permissions, make sure id and version are both added
29
- if ( permissions . indexOf ( "id" ) < 0 ) {
30
- permissions . push ( "id" ) ;
31
- }
32
- if ( permissions . indexOf ( "version" ) < 0 ) {
33
- permissions . push ( "version" ) ;
34
- }
35
- ipfs = await window . ipfs . enable ( {
36
- commands : permissions
37
- } ) ;
38
- } else {
39
- console . log ( "legacy window.ipfs is available!" ) ;
17
+ export const loadWindowIpfs = async permissions => {
18
+ if ( window . ipfs ) {
19
+ let ipfs = window . ipfs ;
20
+ if ( window . ipfs . enable ) {
21
+ // if user set permissions, make sure id and version are both added
22
+ if ( permissions . indexOf ( "id" ) < 0 ) {
23
+ permissions . push ( "id" ) ;
40
24
}
41
- const isWorking = await ipfsIsWorking ( ipfs ) ;
42
- if ( isWorking ) {
43
- ipfsInstance = ipfs ;
44
- resolve ( ipfsInstance ) ;
45
- } else {
46
- console . log (
47
- "cannot access window.ipfs, may not be connected to a working gateway"
48
- ) ;
25
+ if ( permissions . indexOf ( "version" ) < 0 ) {
26
+ permissions . push ( "version" ) ;
49
27
}
28
+ ipfs = await window . ipfs . enable ( {
29
+ commands : permissions
30
+ } ) ;
31
+ } else {
50
32
}
33
+ const isWorking = await ipfsIsWorking ( ipfs ) ;
34
+ if ( isWorking ) {
35
+ return ipfs ;
36
+ } else {
37
+ return null ;
38
+ }
39
+ }
40
+ return null ;
41
+ } ;
51
42
52
- console . log ( "window.ipfs is not available, downloading from CDN..." ) ;
43
+ export const loadJsIpfs = async ( ) => {
44
+ return new Promise ( ( resolve , reject ) => {
53
45
const script = document . createElement ( "script" ) ;
54
46
script . src = "https://unpkg.com/ipfs/dist/index.min.js" ;
55
47
script . onload = async ( ) => {
56
48
const ipfs = await window . Ipfs . create ( ) ;
57
49
const isWorking = await ipfsIsWorking ( ipfs ) ;
58
50
if ( isWorking ) {
59
- ipfsInstance = ipfs ;
60
- resolve ( ipfsInstance ) ;
51
+ resolve ( ipfs ) ;
61
52
} else {
62
53
reject ( new Error ( "js-ipfs is not able to load" ) ) ;
63
54
}
@@ -67,4 +58,21 @@ const getIpfs = (permissions = DEFAULT_PERMISSIONS) => {
67
58
} ) ;
68
59
} ;
69
60
61
+ const getIpfs = ( permissions = DEFAULT_PERMISSIONS ) => {
62
+ // if already instantiated
63
+ if ( ipfsInstance ) {
64
+ return ipfsInstance ;
65
+ }
66
+
67
+ ipfsInstance = await loadWindowIpfs ( permissions )
68
+ if ( ipfsInstance ) {
69
+ console . log ( "window.ipfs is available!" )
70
+ return ipfsInstance
71
+ }
72
+
73
+ console . log ( "window.ipfs is not available, downloading from CDN..." ) ;
74
+ ipfsInstance = await loadJsIpfs ( ) ;
75
+ return ipfsInstance
76
+ } ;
77
+
70
78
export default getIpfs ;
0 commit comments