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

fix: update types after feedback from ceramic #3657

Merged
merged 3 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/custom-ipfs-repo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"datastore-fs": "4.0.0",
"ipfs": "^0.54.4",
"ipfs-repo": "^9.1.1",
"ipfs-repo": "^9.1.3",
"it-all": "^1.0.4"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"ipfs-core-utils": "^0.7.2",
"ipfs-daemon": "^0.5.4",
"ipfs-http-client": "^49.0.4",
"ipfs-repo": "^9.1.1",
"ipfs-repo": "^9.1.3",
"ipfs-utils": "^6.0.4",
"ipld-dag-cbor": "^1.0.0",
"ipld-dag-pb": "^0.22.1",
Expand Down
4 changes: 3 additions & 1 deletion packages/ipfs-core-types/src/config/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export interface AddressConfig {
RPC?: string
Delegates?: string[]
Gateway?: string
Swarm?: string[]
Swarm?: string[],
Announce?: string[],
NoAnnounce?: string[]
}

export interface APIConfig {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"ipfs-block-service": "^0.19.0",
"ipfs-core-types": "^0.3.1",
"ipfs-core-utils": "^0.7.2",
"ipfs-repo": "^9.1.1",
"ipfs-repo": "^9.1.3",
"ipfs-unixfs": "^4.0.3",
"ipfs-unixfs-exporter": "^5.0.3",
"ipfs-unixfs-importer": "^7.0.3",
Expand Down
45 changes: 16 additions & 29 deletions packages/ipfs-core/src/components/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const PubsubRouters = require('../runtime/libp2p-pubsub-routers-nodejs')
* @typedef {import('peer-id')} PeerId
* @typedef {import('../types').Options} IPFSOptions
* @typedef {import('libp2p')} LibP2P
* @typedef {import('libp2p').Libp2pOptions & import('libp2p').constructorOptions} Options
* @typedef {import('libp2p').Libp2pOptions & import('libp2p').CreateOptions} Libp2pOptions
* @typedef {import('ipfs-core-types/src/config').Config} IPFSConfig
* @typedef {import('multiaddr').Multiaddr} Multiaddr
*/
Expand Down Expand Up @@ -66,6 +66,7 @@ module.exports = ({
* @param {KeychainConfig} input.keychainConfig
* @param {PeerId} input.peerId
* @param {Multiaddr[]} input.multiaddrs
* @returns {Libp2pOptions}
*/
function getLibp2pOptions ({ options, config, datastore, keys, keychainConfig, peerId, multiaddrs }) {
const getPubsubRouter = () => {
Expand Down Expand Up @@ -93,25 +94,20 @@ function getLibp2pOptions ({ options, config, datastore, keys, keychainConfig, p
config: {
peerDiscovery: {
mdns: {
enabled: get(options, 'config.Discovery.MDNS.Enabled',
get(config, 'Discovery.MDNS.Enabled', true))
enabled: get(options, 'config.Discovery.MDNS.Enabled', get(config, 'Discovery.MDNS.Enabled', true))
},
webRTCStar: {
enabled: get(options, 'config.Discovery.webRTCStar.Enabled',
get(config, 'Discovery.webRTCStar.Enabled', true))
enabled: get(options, 'config.Discovery.webRTCStar.Enabled', get(config, 'Discovery.webRTCStar.Enabled', true))
},
bootstrap: {
list: get(options, 'config.Bootstrap', get(config, 'Bootstrap', []))
}
},
relay: {
enabled: get(options, 'relay.enabled',
get(config, 'relay.enabled', true)),
enabled: get(options, 'relay.enabled', get(config, 'relay.enabled', true)),
hop: {
enabled: get(options, 'relay.hop.enabled',
get(config, 'relay.hop.enabled', false)),
active: get(options, 'relay.hop.active',
get(config, 'relay.hop.active', false))
enabled: get(options, 'relay.hop.enabled', get(config, 'relay.hop.enabled', false)),
active: get(options, 'relay.hop.active', get(config, 'relay.hop.active', false))
}
},
dht: {
Expand All @@ -120,30 +116,20 @@ function getLibp2pOptions ({ options, config, datastore, keys, keychainConfig, p
kBucketSize: get(options, 'dht.kBucketSize', 20)
},
pubsub: {
enabled: get(options, 'config.Pubsub.Enabled',
get(config, 'Pubsub.Enabled', true))
enabled: get(options, 'config.Pubsub.Enabled', get(config, 'Pubsub.Enabled', true))
},
nat: {
enabled: get(options, 'libp2p.config.nat.enabled', !get(config, 'Swarm.DisableNatPortMap', false)),
ttl: get(options, 'libp2p.config.nat.ttl', 7200),
keepAlive: get(options, 'libp2p.config.nat.keepAlive', true),
gateway: get(options, 'libp2p.config.nat.gateway'),
externalIp: get(options, 'libp2p.config.nat.externalIp'),
pmp: {
enabled: get(options, 'libp2p.config.nat.pmp.enabled', false)
}
enabled: !get(config, 'Swarm.DisableNatPortMap', false)
}
},
addresses: {
listen: multiaddrs.map(ma => ma.toString()),
announce: get(options, 'addresses.announce',
get(config, 'Addresses.Announce', []))
announce: get(options, 'addresses.announce', get(config, 'Addresses.Announce', [])),
noAnnounce: get(options, 'addresses.noAnnounce', get(config, 'Addresses.NoAnnounce', []))
},
connectionManager: get(options, 'connectionManager', {
maxConnections: get(options, 'config.Swarm.ConnMgr.HighWater',
get(config, 'Swarm.ConnMgr.HighWater')),
minConnections: get(options, 'config.Swarm.ConnMgr.LowWater',
get(config, 'Swarm.ConnMgr.LowWater'))
maxConnections: get(options, 'config.Swarm.ConnMgr.HighWater', get(config, 'Swarm.ConnMgr.HighWater')),
minConnections: get(options, 'config.Swarm.ConnMgr.LowWater', get(config, 'Swarm.ConnMgr.LowWater'))
}),
keychain: {
datastore: keys,
Expand All @@ -155,10 +141,11 @@ function getLibp2pOptions ({ options, config, datastore, keys, keychainConfig, p
// Note: libp2p-nodejs gets replaced by libp2p-browser when webpacked/browserified
const getEnvLibp2pOptions = require('../runtime/libp2p-nodejs')

let constructorOptions = get(options, 'libp2p', {})
/** @type {import('libp2p').Libp2pOptions | undefined} */
let constructorOptions = get(options, 'libp2p', undefined)

if (typeof constructorOptions === 'function') {
constructorOptions = {}
constructorOptions = undefined
}

// Merge defaults with Node.js/browser/other environments options and configuration
Expand Down
5 changes: 1 addition & 4 deletions packages/ipfs-core/src/components/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ class Network {
await repo.open()
}

/** @type {IPFSConfig} */
const config = await repo.config.getAll()

const libp2p = await createLibP2P({
options,
repo,
peerId,
// @ts-ignore - TODO move config types into ipfs-repo
multiaddrs: readAddrs(peerId, config),
// @ts-ignore - TODO move config types into ipfs-repo
config,
keychainConfig: undefined
})
Expand Down Expand Up @@ -90,10 +89,8 @@ class Network {
module.exports = Network

/**
*
* @param {PeerId} peerId
* @param {IPFSConfig} config
* @returns {Multiaddr[]}
*/
const readAddrs = (peerId, config) => {
const peerIdStr = peerId.toB58String()
Expand Down
1 change: 1 addition & 0 deletions packages/ipfs-core/src/runtime/config-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = () => ({
Swarm: [
],
Announce: [],
NoAnnounce: [],
API: '',
Gateway: '',
RPC: '',
Expand Down
1 change: 1 addition & 0 deletions packages/ipfs-core/src/runtime/config-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = () => ({
'/ip4/127.0.0.1/tcp/4003/ws'
],
Announce: [],
NoAnnounce: [],
API: '/ip4/127.0.0.1/tcp/5002',
Gateway: '/ip4/127.0.0.1/tcp/9090',
RPC: '/ip4/127.0.0.1/tcp/5003',
Expand Down
4 changes: 2 additions & 2 deletions packages/ipfs-core/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ProfileNames } from 'ipfs-core-types/src/config/profiles'
import type IPLD from 'ipld'
import type { Options as IPLDOptions } from 'ipld'
import type Libp2p from 'libp2p'
import type { Libp2pConfig as Libp2pOptions } from 'libp2p'
import type { Libp2pOptions } from 'libp2p'
import type IPFSRepo from 'ipfs-repo'
import type { ProgressCallback as MigrationProgressCallback } from 'ipfs-repo-migrations'
import type { Datastore } from 'interface-datastore'
Expand Down Expand Up @@ -117,7 +117,7 @@ export interface Options {
* in Node.js, [`libp2p-browser.js`](../src/core/runtime/libp2p-browser.js) in
* browsers.
*/
libp2p?: Libp2pOptions | Libp2pFactoryFn
libp2p?: Partial<Libp2pOptions> | Libp2pFactoryFn
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL about Partial<>.
What's the reason for doing this here, is Libp2pOptions too constrained, or is this what getLibp2pOptions() is doing, filling out defaults for the required?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The libp2p option type has a bunch of required fields, so make it a partial so we can accept an incomplete config object from the user. Otherwise we’d have to duplicate the type to mark the fields optional.

It’s not perfect as it’s non-recursive so if that’s an issue maybe we can relax the required fields in libp2p itself in the future.


silent?: boolean
}
Expand Down