Allow markbind serve to specify custom host #2382#2395
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2395 +/- ##
=======================================
Coverage 48.87% 48.87%
=======================================
Files 124 124
Lines 5238 5238
Branches 1109 1109
=======================================
Hits 2560 2560
Misses 2371 2371
Partials 307 307 ☔ View full report in Codecov by Sentry. |
yucheng11122017
left a comment
There was a problem hiding this comment.
Hi @LamJiuFong thanks for the work!
One thing is I think it might be better to keep 127.0.0.1 as the default if the customised port isn't valid. What do you think?
| } else if (e.code === 'EADDRNOTAVAIL') { | ||
| console.log('address(host) %s is not available. Trying another address'.yellow, host); | ||
| setTimeout(function() { | ||
| server.listen(port, '0.0.0.0'); |
There was a problem hiding this comment.
I'll prefer to make 127.0.0.1 as the default address if the host is invalid. Because 0.0.0.0 has some special meaning and might accidentally expose it to the rest of the interner (see link).
| .option('-p, --port <port>', 'port for server to listen on (Default is 8080)') | ||
| .option('-s, --site-config <file>', 'specify the site config file (default: site.json)') | ||
| .option('-d, --dev', 'development mode, enabling live & hot reload for frontend source files.') | ||
| .option('-a, --address <address>', 'specify the server address/host (Default is localhost') |
There was a problem hiding this comment.
Forgot a closing bracket here 😔
lhw-1
left a comment
There was a problem hiding this comment.
The functional code should be good to go once you've addressed the comments - remember to update the relevant documentation and tests afterwards.
| .option('-p, --port <port>', 'port for server to listen on (Default is 8080)') | ||
| .option('-s, --site-config <file>', 'specify the site config file (default: site.json)') | ||
| .option('-d, --dev', 'development mode, enabling live & hot reload for frontend source files.') | ||
| .option('-a, --address <address>', 'specify the server address/host (Default is localhost') |
There was a problem hiding this comment.
Instead of "localhost", let's specify the address as 127.0.0.1.
| }, 1000); | ||
| } else { | ||
| } else if (e.code === 'EADDRNOTAVAIL') { | ||
| console.log('address(host) %s is not available. Trying another address'.yellow, host); |
There was a problem hiding this comment.
| console.log('address(host) %s is not available. Trying another address'.yellow, host); | |
| console.log('%s is not available. Trying another address.'.yellow, host); |
|
Hi @yucheng11122017 @lhw-1, thank you for the suggestions. Initially I was thinking of setting 0.0.0.0 as the default host so that the users can run the server once there is any available host. However, I did overlook the safety issues of allowing this to happen. I think it's good to set '127.0.0.1' as the default host. I will work on the descriptions too. |
|
Do you think it is useful to add a warning for |
Yeah this sounds good! |
yucheng11122017
left a comment
There was a problem hiding this comment.
Hi @LamJiuFong thanks for the changes! Last thing is to add a warning as suggested by @itsyme when 0.0.0.0 is used. :)
| LiveServer.start = function(options) { | ||
| options = options || {}; | ||
| var host = options.host || '0.0.0.0'; | ||
| var host = options.host !== undefined ? options.host : '127.0.0.1'; |
There was a problem hiding this comment.
| var host = options.host !== undefined ? options.host : '127.0.0.1'; | |
| var host = options.host || '127.0.0.1'; |
|
Would like to seek advice on the latest commit, I display a warning and prompt the user to input "y" in the CLI to proceed with host 0.0.0.0 at their own risk. Is this a good idea to warn the users? |
| const address = server.address(); | ||
| const serveHost = address.address === '0.0.0.0' ? '127.0.0.1' : address.address; | ||
| const serveURL = `http://${serveHost}:${address.port}`; | ||
| const serveHost = address.address; |
There was a problem hiding this comment.
What do you think about adding some validation to the user input host address?
I believe that this could be better for UX as
- it checks the host address before building the pages (which could take some time)
- it better handles the errors that are being thrown. Currently users will be exposed to errors like
Error: getaddrinfo ENOTFOUND 127.0.300.1
I was thinking some sort of simple validation like checking if the input is an actual IP address and if any number in the address exceeds 255 could improve the user experience
There was a problem hiding this comment.
Hi, good idea! I was thinking if we want to do this validation, we can actually just do the validations (including check if port is unavailable, check if host is being used etc) all at once before building the pages. Currently, all these validations are done after building the pages.
Since this PR is focusing on adding another small feature, I suggest we create a separate issue and pull request to specifically address this validation issue. What do you think?
There was a problem hiding this comment.
Yep I think a separate issue is fine as well! I believe there is some level of error handling for when the host is being used. From my experience if I specified a used host, the nearest unused hostname will be used, which allows the user to still serve the site. I think we can discuss about what needs to be validated once the issue is opened!
| options = options || {}; | ||
| var host = options.host || '0.0.0.0'; | ||
| var host = options.host !== undefined ? options.host : '127.0.0.1'; | ||
| var port = options.port !== undefined ? options.port : 8080; // 0 means random |
There was a problem hiding this comment.
| var port = options.port !== undefined ? options.port : 8080; // 0 means random | |
| var port = options.port ?? 8080; // 0 means random |
There was a problem hiding this comment.
Thanks for the suggestion, I wasn't aware of the difference between null and undefined in JS. Thank you for reminding.
I will also set line 161 to be
var host = options.host ?? '127.0.0.1';
|
@yucheng11122017 @itsyme I was thinking if we should use the |
|
Sounds good to me :) do make the change, I think this PR is almost there! |
I agree! My opinion is that we should use the nullish coalescing operator |
Sounds good! Thanks for the research. Remember to write it in your knowledge base to get some credit HAHA |
yucheng11122017
left a comment
There was a problem hiding this comment.
LGTM! Thanks for the work @LamJiuFong
itsyme
left a comment
There was a problem hiding this comment.
LGTM! Good work @LamJiuFong!
markbind serve to specify custom host #2382markbind serve to specify custom host #2382
|
@all-contributors please add @LamJiuFong for code |
|
I couldn't determine any contributions to add, did you specify any contributions? |
|
@all-contributors please add @LamJiuFong for code |
|
I've put up a pull request to add @LamJiuFong! 🎉 |
What is the purpose of this pull request?
Overview of changes:
Resolves #2382
Added
-aoption to theservecommandAnything you'd like to highlight/discuss:
Default host/address is 127.0.0.1. If the custom host specified by users is not working, run server on the default host
Testing instructions:
Run
markbind serve -a <address>, eg.markbind serve -a 127.0.0.1Proposed commit message: (wrap lines at 72 characters)
Allow markbind serve to specify custom host
Checklist: ☑️
Reviewer checklist:
Indicate the SEMVER impact of the PR:
At the end of the review, please label the PR with the appropriate label:
r.Major,r.Minor,r.Patch.Breaking change release note preparation (if applicable):