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

doc: explain edge case when assigning port to url #19645

Closed
wants to merge 9 commits into from

Conversation

nodeav
Copy link
Contributor

@nodeav nodeav commented Mar 27, 2018

numbers which are coerced to scientific notation via .toString(),
will behave unexpectedly when assigned to a url's port.

Fixes: #19595

Checklist

numbers which are coerced to scientific notation via .toString(),
will behave unexpectedly when assigned to a url's port.

Fixes: nodejs#19595
@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to the documentations. url Issues and PRs related to the legacy built-in url module. labels Mar 27, 2018
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
Copy link
Member

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.

Copy link
Contributor Author

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
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 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.

Copy link
Contributor Author

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.

@Trott
Copy link
Member

Trott commented Mar 28, 2018

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;
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor

@vsemozhetbyt vsemozhetbyt left a 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,
Copy link
Contributor

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
Copy link
Contributor

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)?

Copy link
Contributor Author

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

Copy link
Contributor

@vsemozhetbyt vsemozhetbyt Apr 3, 2018

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.

I mean these hard wraps:
hb
breaks:

Copy link
Contributor

@vsemozhetbyt vsemozhetbyt Apr 3, 2018

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.

Copy link
Contributor Author

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

Copy link
Contributor

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)

Copy link
Contributor Author

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

Copy link
Contributor Author

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.

Copy link
Contributor

@vsemozhetbyt vsemozhetbyt Apr 3, 2018

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

Copy link
Contributor Author

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.
Copy link
Contributor

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")
Copy link
Contributor

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

Copy link
Contributor

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")

Copy link
Contributor

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.

@vsemozhetbyt
Copy link
Contributor

CI-lite: https://ci.nodejs.org/job/node-test-pull-request-lite/401/

Copy link
Contributor

@vsemozhetbyt vsemozhetbyt left a 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.
Copy link
Member

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.

Copy link
Contributor Author

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.

@nodeav
Copy link
Contributor Author

nodeav commented Apr 8, 2018

@Trott @TimothyGu
I've commented on the issue (#19595) as I believe this should be fixed in the code instead; Otherwise, please let me know if you'd like any more changes to this PR.

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,
Copy link
Member

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
Copy link
Member

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:
Copy link
Member

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.

@Trott
Copy link
Member

Trott commented Apr 8, 2018

@Trott @TimothyGu
I've commented on the issue (#19595) as I believe this should > be fixed in the code instead; Otherwise, please let me know if you'd like any more changes to this PR.

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

@TimothyGu
Copy link
Member

Looks great, thanks!

@nodeav
Copy link
Contributor Author

nodeav commented Apr 8, 2018

@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.

@BridgeAR
Copy link
Member

BridgeAR commented Apr 9, 2018

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Apr 9, 2018
trivikr pushed a commit that referenced this pull request Apr 9, 2018
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]>
@trivikr
Copy link
Member

trivikr commented Apr 9, 2018

Landed in 51c2c51

Congrats @nodeav for your first commit in Node.js core! 🎉🎉🎉

@trivikr trivikr closed this Apr 9, 2018
targos pushed a commit that referenced this pull request Apr 12, 2018
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. doc Issues and PRs related to the documentations. url Issues and PRs related to the legacy built-in url module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

url library behaves unexpectedly when attempting to set a url's port to a large number
9 participants