@@ -34,66 +34,85 @@ function Factory () {
34
34
. substring ( 2 , 8 )
35
35
}
36
36
37
- if ( ! config ) {
38
- config = JSON . parse ( JSON . stringify ( defaultConfig ) )
39
- const pId = PeerId . create ( { bits : 512 } ) . toJSON ( )
40
- config . Identity . PeerID = pId . id
41
- config . Identity . PrivKey = pId . privKey
42
- }
43
-
44
- // set up the repo
45
- let store
46
- let teardown
47
-
48
- if ( isNode ) {
49
- store = require ( 'fs-pull-blob-store' )
50
- teardown = ( done ) => {
51
- cleanRepo ( repoPath )
52
- done ( )
53
- }
54
- } else {
55
- const idb = window . indexedDB ||
56
- window . mozIndexedDB ||
57
- window . webkitIndexedDB ||
58
- window . msIndexedDB
59
- store = require ( 'idb-pull-blob-store' )
60
- teardown = ( done ) => {
61
- idb . deleteDatabase ( repoPath )
62
- idb . deleteDatabase ( repoPath + '/blocks' )
63
- done ( )
37
+ createConfig ( config , ( err , conf ) => {
38
+ if ( err ) {
39
+ return callback ( err )
64
40
}
65
- }
66
41
67
- const repo = new IPFSRepo ( repoPath , { stores : store } )
68
- repo . teardown = teardown
42
+ config = conf
69
43
70
- // create the IPFS node
71
- const ipfs = new IPFS ( repo )
72
- ipfs . init ( { emptyRepo : true , bits : 512 } , ( err ) => {
73
- if ( err ) {
74
- return callback ( err )
44
+ // set up the repo
45
+ let store
46
+ let teardown
47
+
48
+ if ( isNode ) {
49
+ store = require ( 'fs-pull-blob-store' )
50
+ teardown = ( done ) => {
51
+ cleanRepo ( repoPath )
52
+ done ( )
53
+ }
54
+ } else {
55
+ const idb = window . indexedDB ||
56
+ window . mozIndexedDB ||
57
+ window . webkitIndexedDB ||
58
+ window . msIndexedDB
59
+ store = require ( 'idb-pull-blob-store' )
60
+ teardown = ( done ) => {
61
+ idb . deleteDatabase ( repoPath )
62
+ idb . deleteDatabase ( repoPath + '/blocks' )
63
+ done ( )
64
+ }
75
65
}
76
- repo . config . set ( config , launchNode )
77
- } )
78
66
79
- function launchNode ( ) {
80
- ipfs . load ( ( err ) => {
67
+ const repo = new IPFSRepo ( repoPath , { stores : store } )
68
+ repo . teardown = teardown
69
+
70
+ // create the IPFS node
71
+ const ipfs = new IPFS ( repo )
72
+ ipfs . init ( { emptyRepo : true , bits : 512 } , ( err ) => {
81
73
if ( err ) {
82
74
return callback ( err )
83
75
}
76
+ repo . config . set ( config , launchNode )
77
+ } )
84
78
85
- ipfs . goOnline ( ( err ) => {
79
+ function launchNode ( ) {
80
+ ipfs . load ( ( err ) => {
86
81
if ( err ) {
87
82
return callback ( err )
88
83
}
89
84
90
- nodes . push ( {
91
- repo : repo ,
92
- ipfs : ipfs
93
- } )
85
+ ipfs . goOnline ( ( err ) => {
86
+ if ( err ) {
87
+ return callback ( err )
88
+ }
89
+
90
+ nodes . push ( {
91
+ repo : repo ,
92
+ ipfs : ipfs
93
+ } )
94
94
95
- callback ( null , ipfs )
95
+ callback ( null , ipfs )
96
+ } )
96
97
} )
98
+ }
99
+ } )
100
+
101
+ function createConfig ( config , cb ) {
102
+ if ( config ) {
103
+ return cb ( null , config )
104
+ }
105
+ const conf = JSON . parse ( JSON . stringify ( defaultConfig ) )
106
+
107
+ PeerId . create ( { bits : 512 } , ( err , id ) => {
108
+ if ( err ) {
109
+ return cb ( err )
110
+ }
111
+
112
+ const pId = id . toJSON ( )
113
+ conf . Identity . PeerID = pId . id
114
+ conf . Identity . PrivKey = pId . privKey
115
+ cb ( null , conf )
97
116
} )
98
117
}
99
118
}
0 commit comments