-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
doc: explain edge case when assigning port to url #19645
Conversation
numbers which are coerced to scientific notation via .toString(), will behave unexpectedly when assigned to a url's port. Fixes: nodejs#19595
doc/api/url.md
Outdated
// notation via .toString(), will behave as strings with leading zeroes. | ||
// This means that the port will be assigned the integer part of the coefficient, | ||
// assuming the number is normalized (for example, 0.9e10 => 9e9). | ||
// See https://www.ecma-international.org/ecma-262/6.0/#sec-tostring-applied-to-the-number-type for more information |
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'd omit the spec link. I also think "will behave as strings with leading zeroes" might be more confusing than clarifying.
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, and made the entire thing arguably clearer
doc/api/url.md
Outdated
@@ -317,6 +317,15 @@ console.log(myURL.port); | |||
myURL.port = 1e10; | |||
console.log(myURL.port); | |||
// Prints 1234 | |||
|
|||
// Out-of-range numbers, which are converted to exponential |
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 especially confusing given the previous example which says unconditionally that out-of-range numbers are ignored. Are we sure this behavior isn't simply a bug that needs fixing, rather than a defined behavior that needs documenting? It's hard to imagine anyone actually intentionally using this behavior.
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 agree with both points. I didn't have time to go through the actual spec, however I was told it is indeed the case in:
#19595
whatwg/url#377
Obviously adding a range-check and some tests is a very straightforward solution for the unexpected behavior, but I'm not at all sure this is not a spec bug.
In the meantime though, the documentation should perhaps be changed as it does not reflect the actual behavior expressed in node.
Welcome @nodeav, and thanks for the pull request! Documentation changes tend to attract a lot of discussion and comments, so please be patient with us! Don't get discouraged! |
doc/api/url.md
Outdated
// Prints 4 (because the coefficient is 4.567) | ||
|
||
// Out-of-range numbers, which are not represented in scientific noation, | ||
// will be ignored. | ||
myURL.port = 1e10; |
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 guess it isn't super clear that (1e10).toString()
is '10000000000'
-- might be good to clarify that.
It got me thinking that it might be good to just describe the actual algorithm for assigning a number to port
in the prose (ll. 322-329): the number is first stringified using String(num)
, and then the leading characters of the resulting string that are digits are then converted back to a number. (Note: in cases where the default representation of the number is an exponential, the parts after the decimal point or the e
character are discarded.) Only then is the range checked.
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've rephrased the whole thing, adopting most of your advice, and it is much clearer now in my opinion.
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.
Sorry for many nits)
doc/api/url.md
Outdated
@@ -313,20 +313,39 @@ myURL.port = 1234.5678; | |||
console.log(myURL.port); | |||
// Prints 1234 | |||
|
|||
// Out-of-range numbers are ignored | |||
myURL.port = 1e10; | |||
// Out-of-range numbers, which are not represented in scientific noation, |
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.
Nit: noation -> notation
doc/api/url.md
Outdated
lies outside the range denoted above, it is ignored. | ||
Upon assigning a value to the port, the value will first be converted to a string using `.toString()`. | ||
|
||
If that string is invalid, but it begins 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.
Not sure why, but the wrapping of these two paragraphs creates hard wraps in the rendered text: https://github.com/nodeav/node/blob/e402ff4b54dd1eeb0cd0e7ce346751e97769c880/doc/api/url.md#urlport
Maybe we can rewrap them with more long lines (but within 80 characters)?
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'm not sure I understand, in which line does the hard wrap occur?
And are we really limited to 80 characters? Existing lines in this doc are considerably longer
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, existing lines more than 80 chars is a temporal legacy, but now we have a rule in the STYLE_GUIDE.md and doc linting should throw on limit breaking.
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, I see You have trailing spaces after some added lines, and markdown parses such spaces as hard breaks. It is usually useful to trim trailing spaces in the editor before saving the doc.
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, the trailing spaces were actually intentional, but I don't mind changing that.
and good to know about the line widths
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.
Trailing spaces are not usually safe to rely upon in shared sources: some collaborator may edit any other line and have their editor set for trailing spaces auto-trimming in the whole doc, so these spaces may be deleted silently in any other PR)
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.
Is there an actual designated docs linter? How do I run it?
(I made the changes manually for now)
by the way, automatically removing trailing spaces for markdown documents is a bug IMO, as it's ignoring a part of the markdown language
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 made sure the regex \ $
doesn't find any result in the document.
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.
If I am not mistaken. this is how to lint docs locally:
make lint-md-build
make test-doc
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! The first one says there's nothing to be done; the second one passes.
doc/api/url.md
Outdated
|
||
Note that numbers which contain a decimal point, | ||
such as floating-point numbers or numbers in scientific notation, are not an exception to this rule. | ||
Leading numbers up to the decimal point will be set as the url's port, assuming they are valid. |
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.
Nit: url's
-> URL's
?
doc/api/url.md
Outdated
```js | ||
myURL.port = 4.567e21; | ||
console.log(myURL.port); | ||
// Prints 4 (because it is the leading number in the string "4.567e21") |
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.
Nit: "4.567e21"
-> '4.567e21'
?
We usually use single quotes in code-related things.
doc/api/url.md
Outdated
console.log(myURL.port); | ||
// Prints 1234 | ||
|
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.
Nit: this empty line seems redundant.
doc/api/url.md
Outdated
myURL.port = 4.567e21; | ||
console.log(myURL.port); | ||
// Prints 4 (because it is the leading number in the string "4.567e21") | ||
|
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.
Nit: this empty line seems redundant.
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.
Doc form LGTM.
Thank you!
doc/api/url.md
Outdated
such as floating-point numbers or numbers in scientific notation, | ||
are not an exception to this rule. | ||
Leading numbers up to the decimal point will be set as the URL's port, | ||
assuming they are valid. |
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.
nit: The line breaks in this are rather inconsistent.
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.
Indeed they are. fortunately markdown doesn't care about my linebreaks :) I wanted to keep the width under 80 characters.
@Trott @TimothyGu Thanks. |
doc/api/url.md
Outdated
@@ -313,8 +313,9 @@ myURL.port = 1234.5678; | |||
console.log(myURL.port); | |||
// Prints 1234 | |||
|
|||
// Out-of-range numbers are ignored | |||
myURL.port = 1e10; | |||
// Out-of-range numbers, which are not represented in scientific notation, |
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.
Nit: commas should be removed here.
doc/api/url.md
Outdated
Upon assigning a value to the port, the value will first be converted to a | ||
string using `.toString()`. | ||
|
||
If that string is invalid, but it begins with a number, the leading number is |
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.
Nit: First comma should be removed.
doc/api/url.md
Outdated
Leading numbers up to the decimal point will be set as the URL's port, | ||
assuming they are valid. | ||
|
||
For example: |
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.
Nit: Remove this line and put a colon instead of a period after valid
on the line of text above this one.
I have some minor formatting nits, but the wording and text seems fine to me. I think this is good to land, unless code changes that make it invalid are imminent. @TimothyGu @jasnell |
Looks great, thanks! |
@Trott I agree with the formatting nits you have mentioned and have fixed them. Thanks for the review! As for the code changes - for now I haven't gotten any further comments on the issue; If, however, the decision will be made to change the code, I'll be sure to change the docs as well. |
numbers which are coerced to scientific notation via .toString(), will behave unexpectedly when assigned to a url's port. Fixes: #19595 PR-URL: #19645 Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
numbers which are coerced to scientific notation via .toString(), will behave unexpectedly when assigned to a url's port. Fixes: #19595 PR-URL: #19645 Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
numbers which are coerced to scientific notation via .toString(),
will behave unexpectedly when assigned to a url's port.
Fixes: #19595
Checklist