From d719151112c117813f5b3a04fe5fbfe80763d98d Mon Sep 17 00:00:00 2001 From: nodeav <30617226+nodeav@users.noreply.github.com> Date: Wed, 28 Mar 2018 01:41:15 +0300 Subject: [PATCH 1/9] doc: explain edge case when assigning port to url numbers which are coerced to scientific notation via .toString(), will behave unexpectedly when assigned to a url's port. Fixes: https://github.com/nodejs/node/issues/19595 --- doc/api/url.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/doc/api/url.md b/doc/api/url.md index 2c12e9ac29352f..d4725c15233d16 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -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 +// 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 +myURL.port = 4.567e21; +console.log(myURL.port); +// Prints 4 (because the coefficient is 4.567) ``` The port value may be set as either a number or as a String containing a number From 2cc61c121ee1b7cd8225e80062d1fa680a14daa0 Mon Sep 17 00:00:00 2001 From: LosBunkos Date: Wed, 28 Mar 2018 08:53:33 +0300 Subject: [PATCH 2/9] clarify some details --- doc/api/url.md | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/doc/api/url.md b/doc/api/url.md index d4725c15233d16..ee5c4378b02629 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -313,19 +313,21 @@ myURL.port = 1234.5678; console.log(myURL.port); // Prints 1234 -// Out-of-range numbers are ignored -myURL.port = 1e10; -console.log(myURL.port); -// Prints 1234 +// Numbers which are represented in scientific notation in a String, +// or as very large / small numbers in a Number, +// will be assigned the first digit of the coefficient, +// assuming the number is normalized (for example, 0.9e30 => 9e29). -// Out-of-range numbers, which are converted to exponential -// 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 myURL.port = 4.567e21; console.log(myURL.port); // 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; +console.log(myURL.port); +// Prints 1234 + ``` The port value may be set as either a number or as a String containing a number From 257eae02fedcecb91667559510a5c1679e295022 Mon Sep 17 00:00:00 2001 From: LosBunkos Date: Tue, 3 Apr 2018 01:09:37 +0300 Subject: [PATCH 3/9] improve docs again --- doc/api/url.md | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/doc/api/url.md b/doc/api/url.md index ee5c4378b02629..588f27208a3ee1 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -313,18 +313,9 @@ myURL.port = 1234.5678; console.log(myURL.port); // Prints 1234 -// Numbers which are represented in scientific notation in a String, -// or as very large / small numbers in a Number, -// will be assigned the first digit of the coefficient, -// assuming the number is normalized (for example, 0.9e30 => 9e29). - -myURL.port = 4.567e21; -console.log(myURL.port); -// 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; +myURL.port = 1e10; // 10000000000, will be range-checked console.log(myURL.port); // Prints 1234 @@ -335,9 +326,26 @@ in the range `0` to `65535` (inclusive). Setting the value to the default port of the `URL` objects given `protocol` will result in the `port` value becoming the empty string (`''`). -If an invalid string is assigned to the `port` property, but it begins with a -number, the leading number is assigned to `port`. Otherwise, or if the number -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 +number, the leading number is assigned to `port`. +Otherwise, or if the number +lies outside the range denoted above, it is ignored. + +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. + +For example: + +```js +myURL.port = 4.567e21; +console.log(myURL.port); +// Prints 4 (because it is the leading number in the string "4.567e21") + +``` + #### url.protocol From e402ff4b54dd1eeb0cd0e7ce346751e97769c880 Mon Sep 17 00:00:00 2001 From: LosBunkos Date: Tue, 3 Apr 2018 01:10:19 +0300 Subject: [PATCH 4/9] add another small note --- doc/api/url.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/url.md b/doc/api/url.md index 588f27208a3ee1..3686933c355e05 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -315,7 +315,7 @@ console.log(myURL.port); // Out-of-range numbers, which are not represented in scientific noation, // will be ignored. -myURL.port = 1e10; // 10000000000, will be range-checked +myURL.port = 1e10; // 10000000000, will be range-checked as described below console.log(myURL.port); // Prints 1234 From 64863ee2ceacae86fdd07c9f3f2335e1c96a6885 Mon Sep 17 00:00:00 2001 From: LosBunkos Date: Tue, 3 Apr 2018 08:21:52 +0300 Subject: [PATCH 5/9] fix typo --- doc/api/url.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/url.md b/doc/api/url.md index 3686933c355e05..3592cec3b75205 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -313,7 +313,7 @@ myURL.port = 1234.5678; console.log(myURL.port); // Prints 1234 -// Out-of-range numbers, which are not represented in scientific noation, +// Out-of-range numbers, which are not represented in scientific notation, // will be ignored. myURL.port = 1e10; // 10000000000, will be range-checked as described below console.log(myURL.port); From aedadce6a2e3ecea078afec82cd19dd37133ded5 Mon Sep 17 00:00:00 2001 From: LosBunkos Date: Tue, 3 Apr 2018 08:27:55 +0300 Subject: [PATCH 6/9] remove redundant newlines and small styling fix --- doc/api/url.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/doc/api/url.md b/doc/api/url.md index 3592cec3b75205..a235c970b4e8ba 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -318,7 +318,6 @@ console.log(myURL.port); myURL.port = 1e10; // 10000000000, will be range-checked as described below console.log(myURL.port); // Prints 1234 - ``` The port value may be set as either a number or as a String containing a number @@ -335,18 +334,16 @@ lies outside the range denoted above, it is ignored. 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. +Leading numbers up to the decimal point will be set as the URL's port, assuming they are valid. For example: ```js myURL.port = 4.567e21; console.log(myURL.port); -// Prints 4 (because it is the leading number in the string "4.567e21") - +// Prints 4 (because it is the leading number in the string '4.567e21') ``` - #### url.protocol * {string} From a55b432a083befb322809495096342e0b0b9e03a Mon Sep 17 00:00:00 2001 From: Nadav Eidelstein Date: Tue, 3 Apr 2018 12:01:21 +0300 Subject: [PATCH 7/9] remove trailing spaces and limit line length to 80 --- doc/api/url.md | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/doc/api/url.md b/doc/api/url.md index a235c970b4e8ba..e6ca096efb7811 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -325,16 +325,19 @@ in the range `0` to `65535` (inclusive). Setting the value to the default port of the `URL` objects given `protocol` will result in the `port` value becoming the empty string (`''`). -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 assigned to `port`. -Otherwise, or if the number -lies outside the range denoted above, it is ignored. - -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. +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 +assigned to `port`. +Otherwise, or if the number lies outside the range denoted above, +it is ignored. + +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. For example: From 0d39ca694801d3e7ecc71118da50d34ff3141d51 Mon Sep 17 00:00:00 2001 From: Nadav Eidelstein Date: Tue, 3 Apr 2018 12:04:21 +0300 Subject: [PATCH 8/9] remove more trailing spaces --- doc/api/url.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/url.md b/doc/api/url.md index e6ca096efb7811..76b3edb916929c 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -331,13 +331,13 @@ string using `.toString()`. If that string is invalid, but it begins with a number, the leading number is assigned to `port`. Otherwise, or if the number lies outside the range denoted above, -it is ignored. +it is ignored. 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. +assuming they are valid. For example: From 92919bca05ea0288aafec2b5822ad8d66080b9c8 Mon Sep 17 00:00:00 2001 From: Nadav Eidelstein Date: Sun, 8 Apr 2018 18:52:57 +0300 Subject: [PATCH 9/9] fix some nits --- doc/api/url.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/doc/api/url.md b/doc/api/url.md index 76b3edb916929c..26ee1b1e727d5b 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -313,7 +313,7 @@ myURL.port = 1234.5678; console.log(myURL.port); // Prints 1234 -// Out-of-range numbers, which are not represented in scientific notation, +// Out-of-range numbers which are not represented in scientific notation // will be ignored. myURL.port = 1e10; // 10000000000, will be range-checked as described below console.log(myURL.port); @@ -328,7 +328,7 @@ the empty string (`''`). 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 +If that string is invalid but it begins with a number, the leading number is assigned to `port`. Otherwise, or if the number lies outside the range denoted above, it is ignored. @@ -337,9 +337,7 @@ 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. - -For example: +assuming they are valid: ```js myURL.port = 4.567e21;