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

fix(server): use regex instead of isAbsoluteUrl #2202

Merged
merged 11 commits into from
Aug 22, 2019

Conversation

EslamHiko
Copy link
Member

  • This is a bugfix
  • This is a feature
  • This is a code refactor
  • This is a test update
  • This is a docs update
  • This is a metadata update

For Bugs and Features; did you add new tests?

Yes

Motivation / Use-Case

fixes: #2190

Breaking Changes

no

Additional Info

If we want to still isAbsoluteUrl use can add an extra check for the second character in the contentBase and replace all isAbsoluteUrl(String(contetBase)) with isAbsoluteUrl(String(contetBase)) && String(contetBase)[1] !== ':' assuming that always local paths will have : as a second character ex:c: , e: , ... but I think reverting the changes to the original regex will be better, what do you think ?

Copy link
Member

@hiroppy hiroppy left a comment

Choose a reason for hiding this comment

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

Need to uninstall is-absolute-url. And I think it is better to create util for this change.

@codecov
Copy link

codecov bot commented Aug 16, 2019

Codecov Report

Merging #2202 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2202   +/-   ##
=======================================
  Coverage   93.89%   93.89%           
=======================================
  Files          34       34           
  Lines        1278     1278           
  Branches      366      366           
=======================================
  Hits         1200     1200           
  Misses         71       71           
  Partials        7        7
Impacted Files Coverage Δ
lib/Server.js 97.33% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update eab6acd...b415ede. Read the comment docs.

@alexander-akait
Copy link
Member

I think we need ti wait answer sindresorhus/is-absolute-url#7 before doing something here

@EslamHiko
Copy link
Member Author

@hiroppy done
@evilebottnawi sorry I didn't see the comment, we have now a util file we can use any regex we want.we can modify is-absolute-url regex and use it in checkUrl util. what do you think?

@alexander-akait
Copy link
Member

/cc @EslamHiko looks it is solved on dep side, but will be great keep test to avoid future regression, can you rebase?

@EslamHiko EslamHiko force-pushed the use-regex-instead-of-is-absolute-url branch from 3a3fb3d to 8a827e8 Compare August 21, 2019 12:15
@EslamHiko
Copy link
Member Author

@evilebottnawi yes, I kept only the tests.

@@ -14,7 +14,7 @@ function runOpen(uri, options, log) {
}

const pageUrl =
options.openPage && isAbsoluteUrl(options.openPage)
options.openPage && isAbsoluteUrl(String(options.openPage))
Copy link
Member

Choose a reason for hiding this comment

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

No need this here, if you have problem place let's solve this in other PR

// eslint-disable-next-line no-unused-vars
server = new Promise((resolve, reject) => {
testServer.start(config, {
contentBase:
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 two tests here c:\absolute\path\to\content-base and C:\absolute\path\to\content-base

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Typos

// eslint-disable-next-line no-unused-vars
server = new Promise((resolve, reject) => {
testServer.start(config, {
contentBase: 'c:absolutepath\tocontent-base',
Copy link
Member

Choose a reason for hiding this comment

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

Typo 😄

@EslamHiko EslamHiko closed this Aug 21, 2019
@EslamHiko EslamHiko reopened this Aug 21, 2019
contentBasePublic.charAt(0).toLowerCase() +
contentBasePublic.substring(1),
watchContentBase: true,
port: 2222,
Copy link
Member

Choose a reason for hiding this comment

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

Don't use the port number directly.

Copy link
Member Author

Choose a reason for hiding this comment

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

if I use port it'll cause listen EADDRINUSE: address already in use 127.0.0.1:8107 error and cause the tests to hang, can I use port+1,port+2 or add new values in ports-map ?

Copy link
Member

@hiroppy hiroppy Aug 21, 2019

Choose a reason for hiding this comment

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

Don't add the port number.

diff --git a/test/server/contentBase-option.test.js b/test/server/contentBase-option.test.js
index fe120e5..bcde1e6 100644
--- a/test/server/contentBase-option.test.js
+++ b/test/server/contentBase-option.test.js
@@ -269,10 +269,10 @@ describe('contentBase option', () => {
 
   describe('testing single & multiple external paths', () => {
     afterEach((done) => {
-      testServer.close(() => {
-        done();
-      });
+      testServer.close(done);
     });
+
     it('Should throw exception (string)', (done) => {
       try {
         // eslint-disable-next-line no-unused-vars
@@ -302,71 +302,40 @@ describe('contentBase option', () => {
       }
     });
     it('Should not throw exception (local path with lower case first character)', (done) => {
-      try {
-        // eslint-disable-next-line no-unused-vars
-        server = new Promise((resolve, reject) => {
-          testServer.start(config, {
-            contentBase:
-              contentBasePublic.charAt(0).toLowerCase() +
-              contentBasePublic.substring(1),
-            watchContentBase: true,
-            port: 2222,
-          });
-          resolve(testServer);
-        });
-
-        server.then((testServer) => {
-          testServer.close(() => {
-            done();
-          });
-        });
-      } catch (e) {
-        expect(true).toBe(false);
-      }
+      testServer.start(
+        config,
+        {
+          contentBase:
+            contentBasePublic.charAt(0).toLowerCase() +
+            contentBasePublic.substring(1),
+          watchContentBase: true,
+          port,
+        },
+        done
+      );
     });
     it("Should not throw exception (local path with lower case first character & has '-')", (done) => {
-      try {
-        // eslint-disable-next-line no-unused-vars
-        server = new Promise((resolve, reject) => {
-          testServer.start(config, {
-            contentBase: 'c:\\absolute\\path\\to\\content-base',
-            watchContentBase: true,
-            port: 2223,
-          });
-          resolve(testServer);
-        });
-
-        server.then((testServer) => {
-          testServer.close(() => {
-            done();
-          });
-        });
-      } catch (e) {
-        expect(true).toBe(false);
-      }
+      testServer.start(
+        config,
+        {
+          contentBase: 'c:\\absolute\\path\\to\\content-base',
+          watchContentBase: true,
+          port,
+        },
+        done
+      );
     });
     it("Should not throw exception (local path with upper case first character & has '-')", (done) => {
-      try {
-        // eslint-disable-next-line no-unused-vars
-        server = new Promise((resolve, reject) => {
-          testServer.start(config, {
-            contentBase: 'C:\\absolute\\path\\to\\content-base',
-            watchContentBase: true,
-            port: 2224,
-          });
-          resolve(testServer);
-        });
-
-        server.then((testServer) => {
-          testServer.close(() => {
-            done();
-          });
-        });
-      } catch (e) {
-        expect(true).toBe(false);
-      }
+      testServer.start(
+        config,
+        {
+          contentBase: 'C:\\absolute\\path\\to\\content-base',
+          watchContentBase: true,
+          port,
+        },
+        done
+      );
     });
-
     it('Should throw exception (array)', (done) => {
       try {
         // eslint-disable-next-line no-unused-vars

You can patch this code.

@EslamHiko
Copy link
Member Author

/cc @evilebottnawi @hiroppy
CI is green 😄

@@ -405,7 +405,7 @@ class Server {
throw new Error('Watching remote files is not supported.');
} else if (Array.isArray(contentBase)) {
contentBase.forEach((item) => {
if (isAbsoluteUrl(String(item))) {
if (isAbsoluteUrl(String(item)) || typeof item === 'number') {
Copy link
Member

Choose a reason for hiding this comment

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

Why we add typeof item === 'number'?

Copy link
Member Author

Choose a reason for hiding this comment

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

@evilebottnawi it's already blocking number values in options.js accepting only strings, in case in future refactors if we change the type of array items to accept any type we still need it to throw an exception if the array item is number type.

Copy link
Member Author

@EslamHiko EslamHiko Aug 22, 2019

Choose a reason for hiding this comment

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

@evilebottnawi we can remove number type from options.json

  "contentBase": {
      "anyOf": [
        {
          "enum": [false]
        },
        {
          "type": "number"
        },
        {
          "type": "string"
        },
        {
          "type": "array",
          "items": {
            "type": "string"
          },
          "minItems": 1
        }
      ]
    },

and remove || typeof item === 'number' checks and update tests to check for the exception "should be {String|Array} (https://webpack.js.org/configuration/dev-server/#devservercontentbase)" in number type cases
what do you think ?

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 do it next version

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Good job, one question

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

/cc @hiroppy

@hiroppy hiroppy merged commit 68ecf78 into webpack:master Aug 22, 2019
@EslamHiko EslamHiko deleted the use-regex-instead-of-is-absolute-url branch August 22, 2019 22:05
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.

"Error: Watching remote files is not supported" on windows with latest
3 participants