-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Typescript support #150
Conversation
Two questions for the reviewer:
Lines 497 to 500 in 42efbb5
Please suggest how to handle these two cases. |
Thanks for reaching out @nazarhussain
yes, it should be removed. We should have something similar to https://github.com/multiformats/js-multiaddr/blob/master/src/index.js#L550 where we also had before class-is before moving with the types work
yes, sadly it can also return undefined. The story here is that not all types of key types allows us to compute the public key with the peerId. And you can create a PeerId just with the id. You mention this will break the manually implemented types in https://github.com/libp2p/js-peer-id/blob/master/src/index.d.ts#L118 right? The answer here is that we need to make breaking changes as the types do not represent the truth and undefined can happen |
@vasco-santos PR is ready for review. Feel free to share your feedback. Just to be sure this PR contains breaking changes in terms of types not behaviour. |
@nazarhussain thanks, sorry for taking long to get here. I will review this tomorrow |
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.
Thanks for submitting these @nazarhussain . I left some feedback, but I think we are close here
src/index.js
Outdated
@@ -143,7 +223,8 @@ class PeerId { | |||
enumerable: false | |||
}) | |||
} | |||
return this._idCIDString | |||
|
|||
return this._idCIDString ? this._idCIDString : '' |
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 will always exist a this._idCIDString
at this point, right?
If this is a typescript complaint, probably better to add a @ts-ignore and also point to the issue in typescript: microsoft/TypeScript#28694 rather than having this "impossible" possibility of returning an invalid peerId string (empty string). What do you think?
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.
It was the issue of Non-nullable types was not familiar its different than typescript.
src/index.js
Outdated
if (val) { | ||
return uint8ArrayToString(val, 'base64pad') | ||
} | ||
|
||
return '' |
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.
I don't like returning things that are not valid. I think we just do in toJson
:
const privKey = this.marshalPrivKey()
const pubKey = this.marshalPubKey()
return {
id: this.toB58String(),
privKey: privKey && toB64Opt(privKey),
pubKey: pubKey && toB64Opt(pubKey)
}
This way, this function could not accept undefined and this would return what is suppoed or throw
@vasco-santos Thanks for such a detail feedback. My intention was to make minimum change possible to existing code which does not involve making changes to the tests. Hopefully it looks better now. Currently let with these linting warning/errors.
I am not sure how to disable warnings for such TODO comments, please suggest. For the error case it comes because of this line in constructor.
Linting is expecting to assign some value there, we can disable this linting error. Else ee can assign an empty string and later we can check for empty string in https://github.com/nazarhussain/js-peer-id/blob/nh-ts-support/test/peer-id.spec.js#L312 |
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.
Some other bits of feedback.
FYI, we discussed yesterday about creating a PeerId interface, among other interfaces, so that they are implementation agnostic allowing us upgrade implementation without coordinating these changes across the board and avoid breaking changes like this will be.
With this in mind, I think we will wait for the PeerId interface and this would become an implementation of it. The current content of the PR is still needed, but we will need to add @implements PeerIdInterface
@vasco-santos Thanks for the information. Let me know if I can be helpful in creating that interface. |
Triage note:
|
@lidel @vasco-santos If you guys allow I can create the interface for PeerID in interfaces repo. What do you think? |
@nazarhussain yes i believe https://github.com/libp2p/js-libp2p-interfaces is the place. |
@nazarhussain : just checking in - are you planing to add in the interface? Thanks. |
@BigLep I was not sure based on discussion, that should I contribute to the interfaces repo or not. But now I will create PR in few coming days. |
@nazarhussain: just checking in to see if you're going to make this PR or not. Thanks! |
Hi @BigLep sorry to get late. I got engaged with some other issues. I actually worked on it and created the interfaces. Was writing the test helpers where I left it. Will complete and open the PR this weekend. Then we can discuss on that PR to improve any thing further. |
@BigLep I just opened a PR libp2p/js-libp2p-interfaces#107. I will work on its feedback. As soon it's merged will refactor this PR according to it. |
Creates `PeerId` interface and its compliance tests. The discussion was initiated from libp2p/js-libp2p#955 and libp2p/js-peer-id#150 (review) Co-authored-by: achingbrain <alex@achingbrain.net>
@nazarhussain: given libp2p/js-libp2p-interfaces#107 got merged, are you ok to refactor this PR? |
@BigLep I was waiting for the newer release of |
The changes related |
Closing because this is being handled by the TypeScript conversion push (being tracked in libp2p/js-libp2p#1021 ). |
With reference to the issue libp2p/js-libp2p#659 this PR will add typescript support to
peer-id