Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 94a8055

Browse files
committed
feat: use new bundle chaining ecosystem
1 parent a333b96 commit 94a8055

File tree

2 files changed

+49
-30
lines changed

2 files changed

+49
-30
lines changed

src/core/components/libp2p.js

+47-26
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,63 @@ const multiaddr = require('multiaddr')
88
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
99
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
1010
const PubsubRouters = require('../runtime/libp2p-pubsub-routers-nodejs')
11+
const Libp2p = require('libp2p')
1112

1213
module.exports = function libp2p (self, config) {
1314
const options = self._options || {}
1415
config = config || {}
1516

1617
// 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'
1822
? options.libp2p
19-
: defaultBundle
23+
: defaultBundle */
2024

2125
const { datastore } = self._repo
2226
const peerInfo = self._peerInfo
2327
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+
}
2568

2669
libp2p.on('stop', () => {
2770
// Clear our addresses so we can start clean
@@ -133,27 +176,5 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) {
133176
})
134177
}
135178

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
159180
}

test/core/libp2p.spec.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ describe('libp2p customization', function () {
108108
})
109109
})
110110

111-
it('should allow for custom overrideFunction', (done) => {
111+
it('should allow for custom bundle-chain', (done) => {
112112
const ipfs = {
113113
_repo: {
114114
datastore
@@ -118,9 +118,7 @@ describe('libp2p customization', function () {
118118
// eslint-disable-next-line no-console
119119
_print: console.log,
120120
_options: {
121-
libp2p: {
122-
overrideFunction: require('stardust4ipfs')
123-
}
121+
libp2p: [require('stardust4ipfs')]
124122
}
125123
}
126124

0 commit comments

Comments
 (0)