-
Notifications
You must be signed in to change notification settings - Fork 64
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
Conversation
src/index.js
Outdated
return { | ||
family: (codes[0] === 41) ? 'IPv6' : 'IPv4', | ||
family: family, |
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.
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.
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 didn't know that. I will modify it, setting IPv6
for ip6, as well as dns6.
test/index.spec.js
Outdated
@@ -443,11 +443,11 @@ describe('helpers', () => { | |||
}) | |||
|
|||
describe('.nodeAddress', () => { | |||
it('throws on non thinWaistAddress', () => { | |||
it('throws on non invalid node address', () => { |
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.
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.
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.
yes, my bad!
test/index.spec.js
Outdated
|
||
it('returns a node friendly address with dns', () => { | ||
expect( | ||
multiaddr('dns4/wss0.bootstrap.libp2p.io/tcp/443').nodeAddress() |
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.
This is not a valid multiaddr, missing the initial /
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.
Not sure how this test can be passing currently? Seems some checks are missing, at least this test should be failing.
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 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 😕
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.
Let's add a test and checks that it starts with a /
as well then
ddd3e71
to
65fc669
Compare
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.') |
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.
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.
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.
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', |
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.
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.
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 is fixed now
test/index.spec.js
Outdated
family: 'IPv4', | ||
port: '443' | ||
}) | ||
}) | ||
}) | ||
|
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.
Please also add a test case for checking if the multiaddr is missing the prefix with /
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.
Test was added :)
65fc669
to
568f018
Compare
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') |
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.
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.
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.
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.') |
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.
Same goes here, should give the user an idea of what's not, not just that it's invalid.
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.
Agreed and fixed
568f018
to
53af336
Compare
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 "/"`) |
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.
Last mile! Typo multiAddr
should be multiaddr
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.
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}".') |
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.
Last mile! Typo Multiaddr
should be multiaddr
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.
Oh, and valid format for a multiaddr is to start with /
;)
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.
Fixed :)
53af336
to
f193d46
Compare
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.
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.
Aiming to support the
js-ipfs-api
to connect with a node using aDNS
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