@@ -8,20 +8,63 @@ const multiaddr = require('multiaddr')
8
8
const DelegatedPeerRouter = require ( 'libp2p-delegated-peer-routing' )
9
9
const DelegatedContentRouter = require ( 'libp2p-delegated-content-routing' )
10
10
const PubsubRouters = require ( '../runtime/libp2p-pubsub-routers-nodejs' )
11
+ const Libp2p = require ( 'libp2p' )
11
12
12
13
module . exports = function libp2p ( self , config ) {
13
14
const options = self . _options || { }
14
15
config = config || { }
15
16
16
17
// Always create libp2p via a bundle function
17
- const createBundle = typeof options . libp2p === 'function'
18
+ const createBundle = typeof options . libp2p !== 'undefined' ? ( Array . isArray ( options . libp2p ) ? options . libp2p : [ options . libp2p ] ) : [ ]
19
+ createBundle . unshift ( defaultBundle )
20
+
21
+ /* typeof options.libp2p === 'function'
18
22
? options.libp2p
19
- : defaultBundle
23
+ : defaultBundle */
20
24
21
25
const { datastore } = self . _repo
22
26
const peerInfo = self . _peerInfo
23
27
const peerBook = self . _peerInfoBook
24
- const libp2p = createBundle ( { options, config, datastore, peerInfo, peerBook } )
28
+
29
+ const end = createBundle . length - 1
30
+ let libp2p
31
+ let libp2pOpts
32
+
33
+ options . libp2p = libp2pOpts = null
34
+
35
+ createBundle . forEach ( ( fncOrObj , i ) => {
36
+ if ( typeof fncOrObj === 'function' ) {
37
+ const r = fncOrObj ( { options, config, datastore, peerInfo, peerBook } )
38
+ if ( r instanceof Libp2p ) {
39
+ if ( i === end ) {
40
+ libp2p = r
41
+ } else {
42
+ throw new Error ( 'Using chained, but non-last function returned instance' )
43
+ }
44
+ } else if ( typeof r === 'object' ) {
45
+ if ( r . extend ) {
46
+ libp2pOpts = options . libp2p = mergeOptions . call ( { concatArrays : true } , libp2pOpts , r ) // extend
47
+ } else {
48
+ libp2pOpts = options . libp2p = r // override
49
+ }
50
+ } else if ( typeof r === 'undefined' ) {
51
+ // ignore, go on
52
+ } else {
53
+ // maybe print a warning?
54
+ }
55
+ } else if ( typeof fncOrObj === 'object' ) {
56
+ libp2pOpts = options . libp2p = mergeOptions . call ( { concatArrays : fncOrObj . extend } , libp2pOpts , fncOrObj )
57
+ } else {
58
+ throw new TypeError ( 'Option .libp2p has invalid type ' + typeof fncOrObj )
59
+ }
60
+ } )
61
+
62
+ if ( ! libp2p ) {
63
+ // Required inline to reduce startup time
64
+ // Note: libp2p-nodejs gets replaced by libp2p-browser when webpacked/browserified
65
+ const Node = require ( '../runtime/libp2p-nodejs' )
66
+ libp2p = new Node ( libp2pOpts )
67
+ }
25
68
26
69
libp2p . on ( 'stop' , ( ) => {
27
70
// Clear our addresses so we can start clean
@@ -133,27 +176,5 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) {
133
176
} )
134
177
}
135
178
136
- const libp2pUserOptions = get ( options , 'libp2p' , { } )
137
- let libp2pExtend = get ( options , 'libp2p.extend' , false )
138
-
139
- let libp2pOptions = libp2pDefaults
140
-
141
- // allow user to specify overrideFunction, while at the same time allowing static arguments
142
- if ( libp2pUserOptions . overrideFunction && typeof libp2pUserOptions . overrideFunction === 'function' ) {
143
- const override = libp2pUserOptions . overrideFunction ( { datastore, peerInfo, peerBook, options, config } )
144
-
145
- if ( override . extend != null ) {
146
- libp2pExtend = override . extend
147
- }
148
-
149
- libp2pOptions = mergeOptions . call ( { concatArrays : libp2pExtend } , libp2pOptions , override )
150
- delete libp2pUserOptions . overrideFunction
151
- }
152
-
153
- libp2pOptions = mergeOptions . call ( { concatArrays : libp2pExtend } , libp2pOptions , libp2pUserOptions )
154
-
155
- // Required inline to reduce startup time
156
- // Note: libp2p-nodejs gets replaced by libp2p-browser when webpacked/browserified
157
- const Node = require ( '../runtime/libp2p-nodejs' )
158
- return new Node ( libp2pOptions )
179
+ return libp2pDefaults
159
180
}
0 commit comments