This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: pass libp2pOptions to the bundle function #2591
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -13,15 +13,20 @@ module.exports = function libp2p (self, config) { | |||||
const options = self._options || {} | ||||||
config = config || {} | ||||||
|
||||||
// Always create libp2p via a bundle function | ||||||
const createBundle = typeof options.libp2p === 'function' | ||||||
? options.libp2p | ||||||
: defaultBundle | ||||||
|
||||||
const { datastore } = self._repo | ||||||
const peerInfo = self._peerInfo | ||||||
const peerBook = self._peerInfoBook | ||||||
const libp2p = createBundle({ options, config, datastore, peerInfo, peerBook }) | ||||||
|
||||||
const libp2pOptions = getLibp2pOptions({ options, config, datastore, peerInfo, peerBook }) | ||||||
let libp2p | ||||||
|
||||||
if (typeof options.libp2p === 'function') { | ||||||
libp2p = options.libp2p({ libp2pOptions, options, config, datastore, peerInfo, peerBook }) | ||||||
} else { | ||||||
// Required inline to reduce startup time | ||||||
const Libp2p = require('libp2p') | ||||||
libp2p = new Libp2p(mergeOptions(libp2pOptions, get(options, 'libp2p', {}))) | ||||||
} | ||||||
|
||||||
libp2p.on('stop', () => { | ||||||
// Clear our addresses so we can start clean | ||||||
|
@@ -39,7 +44,7 @@ module.exports = function libp2p (self, config) { | |||||
return libp2p | ||||||
} | ||||||
|
||||||
function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) { | ||||||
function getLibp2pOptions ({ options, config, datastore, peerInfo, peerBook }) { | ||||||
// Set up Delegate Routing based on the presence of Delegates in the config | ||||||
let contentRouting | ||||||
let peerRouting | ||||||
|
@@ -80,7 +85,12 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) { | |||||
peerBook, | ||||||
modules: { | ||||||
contentRouting, | ||||||
peerRouting, | ||||||
peerRouting | ||||||
} | ||||||
} | ||||||
|
||||||
const libp2pOptions = { | ||||||
modules: { | ||||||
pubsub: getPubsubRouter() | ||||||
}, | ||||||
config: { | ||||||
|
@@ -133,9 +143,14 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) { | |||||
}) | ||||||
} | ||||||
|
||||||
const libp2pOptions = mergeOptions(libp2pDefaults, get(options, 'libp2p', {})) | ||||||
// Required inline to reduce startup time | ||||||
// Note: libp2p-nodejs gets replaced by libp2p-browser when webpacked/browserified | ||||||
const Node = require('../runtime/libp2p-nodejs') | ||||||
return new Node(libp2pOptions) | ||||||
const getEnvLibp2pOptions = require('../runtime/libp2p-nodejs') | ||||||
|
||||||
// Merge defaults with Node.js/browser/other environments options and configuration | ||||||
return mergeOptions( | ||||||
libp2pDefaults, | ||||||
getEnvLibp2pOptions({ options, config, datastore, peerInfo, peerBook }), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: easier to read if options-to-be-merged follow same naming convention (also,
Suggested change
|
||||||
libp2pOptions | ||||||
) | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, options is getting passed into
getLibp2pOptions
, but then it is still passed into libp2p creation. ShouldgetLibp2pOptions
just handle merging all of that so options doesn't need to be passed again?Along those lines, why is
options
being passed here? It feels like the result ofgetLibp2pOptions
should just be able to get passed directly to bothnew Libp2p
andoptions.libp2p
. No other params should be needed right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There may be some other IPFS option you want to use in your custom bundle function. It's "passed again" because that's what currently happens, and I'm trying to avoid a breaking change.
options
is IPFS options, passed to the IPFS constructor. It's passed here because that's currently what happens when you provide a custom bundle function. If we removed it, we'd have a breaking change.getLibp2pOptions
is passed IPFSoptions
and returns libp2p options (I would call them defaults, but they're not really because they're based off the IPFSoptions
and the IPFS config from the repo).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, so
options
andconfig
are for IPFS? If so, would it be easier to understand if in the scope of the custom bundleroptions
were the libp2p options and the IPFS ones were prefixed? If those are going to be exposed I think it might be helpful to make that distinction.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm, that would be a breaking change. I think that would be clearer, but since the goal is to avoid the breaking change I think this is fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW I agree!