Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

chore: update types #87

Merged
merged 4 commits into from
Apr 7, 2021
Merged

chore: update types #87

merged 4 commits into from
Apr 7, 2021

Conversation

vasco-santos
Copy link
Member

@vasco-santos vasco-santos commented Mar 24, 2021

This PR is a follow up PR for types support. It includes:

  • aegir update to latest, with new types problems fixed and ts files renamed to d.ts
  • pubsub with options types available
  • typedefs for remaining interfaces: discovery and routing

@vasco-santos vasco-santos force-pushed the chore/update-types branch 6 times, most recently from 3d88eae to 17f4031 Compare March 24, 2021 17:28
@vasco-santos vasco-santos force-pushed the chore/update-types branch 8 times, most recently from fc35ea6 to 7a861c4 Compare March 25, 2021 14:01
@@ -62,6 +62,9 @@ class Topology {
return Boolean(other && other[topologySymbol])
}

/**
* @param {any} registrar
Copy link
Member Author

Choose a reason for hiding this comment

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

We would need to create a circular dependency to have registrar types in the interface. We can change this interaction, but I feel we should not spend a lof of time reinventing this interaction at the moment.

@vasco-santos vasco-santos force-pushed the chore/update-types branch 3 times, most recently from 09f867b to ba4b7a3 Compare March 26, 2021 21:31
@vasco-santos vasco-santos marked this pull request as ready for review March 29, 2021 13:28
import PeerId from 'peer-id'
import Multiaddr from 'multiaddr'

declare class PeerRouting {
Copy link
Member

Choose a reason for hiding this comment

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

its not exported ?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was doing a named import on libp2p, but it makes sense to export this by default. I changed it here and in the other relevant types files

@vasco-santos vasco-santos force-pushed the chore/update-types branch 2 times, most recently from 376b50c to a974f8f Compare April 6, 2021 20:25
@vasco-santos vasco-santos merged commit 64a478d into master Apr 7, 2021
@vasco-santos vasco-santos deleted the chore/update-types branch April 7, 2021 07:39
@@ -47,39 +47,43 @@
},
"homepage": "https://github.com/libp2p/js-interfaces#readme",
"dependencies": {
"@types/bl": "^2.1.0",
"@types/bl": "4.1.0",
Copy link

Choose a reason for hiding this comment

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

has this lost the ^ for a reason?

Copy link
Member Author

Choose a reason for hiding this comment

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

good catch, mistyped! I will PR this shortly

@@ -62,7 +63,7 @@ export type MultiaddrConnectionTimeline = {

export type MultiaddrConnection = {
sink: Sink;
source: () => AsyncIterable<Uint8Array>;
source: AsyncIterable<Uint8Array | BufferList>;
Copy link

Choose a reason for hiding this comment

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

What are the use-cases for returning a plain BufferList, are we doing this as an optimisation to avoid buffer concatenation? Do our code paths actually take advantage of this or do we end up concatenating anyway?

Copy link
Member Author

Choose a reason for hiding this comment

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

We use BufferList because some modules we use (example: https://github.com/bustle/streaming-iterables) also support them and it is valid from users stand point, when they create libp2p protocols. Anyway, we don't use this internally anyway for optimisation and we end up concatenating as you mention.
This is something that we can look into

@@ -1,3 +1,4 @@
import BufferList from 'bl'
Copy link

Choose a reason for hiding this comment

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

So when you import 'bl' but have @types/bl then it automagically pulls those definitions in rather than looking for the real bl (which isn't a dependency)?

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, if you are in typescript it will look into @types namespace. You can also do this in jsdoc for specifying typedefs with import:

@typedef {import('bl')} BufferList

},
"devDependencies": {
"aegir": "^29.2.0",
"@types/debug": "^4.1.5",
Copy link

Choose a reason for hiding this comment

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

I'm assuming this is a devdep because it's an internal concern and we only want it to typecheck internal consistency and don't expose debug to users in any way form here. Is that correct?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, that is correct!

declare class ContentRouting {
constructor (options: Object);
provide (cid: CID): Promise<void>;
findProviders (cid: CID, options: Object): AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>;
Copy link

Choose a reason for hiding this comment

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

maybe better to define a Provider here rather than an anonymous type for {id, multiaddrs}?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is more a representation of a peer rather then a Provider. As you pointed below, findPeer also has the same. We have been discussing about potentially change the API return type of this in #85

We will probably look back into this


declare class PeerRouting {
constructor (options?: Object);
findPeer (peerId: PeerId, options?: Object): Promise<{ id: PeerId, multiaddrs: Multiaddr[] }>;
Copy link

Choose a reason for hiding this comment

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

the {id,multiaddrs} appears twice here again, further suggests it should be it's own Provider type

@@ -589,7 +595,7 @@ class PubsubBaseProtocol extends EventEmitter {
for (const topic of message.topicIDs) {
const validatorFn = this.topicValidators.get(topic)
if (!validatorFn) {
continue
continue // eslint-disable-line
Copy link

Choose a reason for hiding this comment

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

you could just flip the logic here to if (validatorFn) { await validatorFn(..) } to avoid the disable

Copy link
Member Author

Choose a reason for hiding this comment

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

I will also update this in the follow up PR

exports.RPC = RPC
exports.Message = RPC.Message
exports.SubOpts = RPC.SubOpts
module.exports = {
Copy link

Choose a reason for hiding this comment

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

out of interest: was this change forced by the type checking?

Copy link
Member Author

Choose a reason for hiding this comment

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

By the linter, but I think this can cause problems with inferred types and some bundlers. @hugomrdias has the bigger picture here on why we should avoid this

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants