Skip to content
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

feat: Add support for multiaddr node with dns #62

Merged
merged 1 commit into from
Apr 24, 2018

Conversation

vasco-santos
Copy link
Member

Aiming to support the js-ipfs-api to connect with a node using a DNS multiaddr, I created this PR. Basically, as the API uses the nodeAddress function, it must support all types of address, instead of supporting only thin waist addresses.

Closes ipfs-inactive/js-ipfs-http-client#712

src/index.js Outdated
return {
family: (codes[0] === 41) ? 'IPv6' : 'IPv4',
family: family,
Copy link
Member

Choose a reason for hiding this comment

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

This should only be set to either the number 4 or 6. See https://nodejs.org/api/http.html#http_http_request_options_callback

Also, adding a test case with using whatever nodeAddress returns can be passed to http.request would be great, as that would have caught this.

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 didn't know that. I will modify it, setting IPv6 for ip6, as well as dns6.

@@ -443,11 +443,11 @@ describe('helpers', () => {
})

describe('.nodeAddress', () => {
it('throws on non thinWaistAddress', () => {
it('throws on non invalid node address', () => {
Copy link
Member

Choose a reason for hiding this comment

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

This is a double-negative, could be just throws on valid node address but the test title doesn't make sense. Either it's valid and doesn't throw, or it's invalid and throws.

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, my bad!


it('returns a node friendly address with dns', () => {
expect(
multiaddr('dns4/wss0.bootstrap.libp2p.io/tcp/443').nodeAddress()
Copy link
Member

Choose a reason for hiding this comment

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

This is not a valid multiaddr, missing the initial /

Copy link
Member

Choose a reason for hiding this comment

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

Not sure how this test can be passing currently? Seems some checks are missing, at least this test should be failing.

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 add the /.

Anyway. it is possible to do the following:

const ipfs = ipfsAPI('ip4/127.0.0.1/tcp/5002')

Even, with the code in master 😕

Copy link
Member

Choose a reason for hiding this comment

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

Let's add a test and checks that it starts with a / as well then

@vasco-santos vasco-santos force-pushed the feat_add-support-for-multiaddr-with-dns branch from ddd3e71 to 65fc669 Compare April 17, 2018 15:33
src/index.js Outdated
const codes = this.protoCodes()
const parts = this.toString().split('/').slice(1)

if (parts.length < 4) {
throw new Error('Multiaddr must be a valid address for nodeAddress.')
Copy link
Member

Choose a reason for hiding this comment

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

This validation is not strict enough. For us to be able to generate a nodeAddress (which is address + port + family) we need to have at least /ip4 or /ip6 or /dns4 or /dns6 with /tcp or /udp. Simply checking if it has 4 parts is not enough.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed! Added those validations now

src/index.js Outdated
return {
family: (codes[0] === 41) ? 'IPv6' : 'IPv4',
family: (codes[0] === 41 || codes[0] === 55) ? 'IPv6' : 'IPv4',
Copy link
Member

Choose a reason for hiding this comment

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

This seems still incorrect (and was before your change as well) as https://nodejs.org/api/http.html#http_http_request_options_callback says it has to be a number, not a string.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is fixed now

family: 'IPv4',
port: '443'
})
})
})

Copy link
Member

Choose a reason for hiding this comment

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

Please also add a test case for checking if the multiaddr is missing the prefix with /

Copy link
Member Author

Choose a reason for hiding this comment

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

Test was added :)

@vasco-santos vasco-santos force-pushed the feat_add-support-for-multiaddr-with-dns branch from 65fc669 to 568f018 Compare April 18, 2018 09:18
src/index.js Outdated
@@ -37,6 +37,9 @@ const Multiaddr = withIs.proto(function (addr) {
*/
this.buffer = codec.fromBuffer(addr)
} else if (typeof addr === 'string' || addr instanceof String) {
if (addr.length > 0 && addr.charAt(0) !== '/') {
throw new Error('addr string must have the correct format')
Copy link
Member

Choose a reason for hiding this comment

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

Better error message would be: multiaddr "ip4/127.0.0.1/tcp/4001" must start with a "/"

We might as well tell the user what's wrong. Worth keeping in mind for future errors as well, if we can tell the user what's wrong exactly, we should.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed and fixed

src/index.js Outdated
if (parts.length < 4 ||
(codes[0] !== 4 && codes[0] !== 41 && codes[0] !== 54 && codes[0] !== 55) ||
(parts[2] !== 'tcp' && parts[2] !== 'udp')) {
throw new Error('Multiaddr must be a valid address for nodeAddress.')
Copy link
Member

Choose a reason for hiding this comment

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

Same goes here, should give the user an idea of what's not, not just that it's invalid.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed and fixed

@vasco-santos vasco-santos force-pushed the feat_add-support-for-multiaddr-with-dns branch from 568f018 to 53af336 Compare April 18, 2018 11:12
src/index.js Outdated
@@ -37,6 +37,9 @@ const Multiaddr = withIs.proto(function (addr) {
*/
this.buffer = codec.fromBuffer(addr)
} else if (typeof addr === 'string' || addr instanceof String) {
if (addr.length > 0 && addr.charAt(0) !== '/') {
throw new Error(`multiAddr "${addr}" must start with a "/"`)
Copy link
Member

Choose a reason for hiding this comment

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

Last mile! Typo multiAddr should be multiaddr

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed :)

src/index.js Outdated
const parts = this.toString().split('/').slice(1)

if (parts.length < 4) {
throw new Error('Multiaddr must have a valid format: "{ip4, ip6, dns4, dns6}/{address}/{tcp, udp}/{port}".')
Copy link
Member

Choose a reason for hiding this comment

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

Last mile! Typo Multiaddr should be multiaddr

Copy link
Member

Choose a reason for hiding this comment

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

Oh, and valid format for a multiaddr is to start with / ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed :)

@vasco-santos vasco-santos force-pushed the feat_add-support-for-multiaddr-with-dns branch from 53af336 to f193d46 Compare April 19, 2018 08:54
Copy link
Member

@daviddias daviddias left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for making this @vasco-santos and thanks for reviewing it @victorbjelkholm :)

This is a breaking change (e.g IPv4 -> 4) and so it means that we will have to update pretty much all the modules 😢 I'll start with this one.

@daviddias daviddias merged commit 5d6b93a into master Apr 24, 2018
@daviddias daviddias deleted the feat_add-support-for-multiaddr-with-dns branch April 24, 2018 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants