-
Notifications
You must be signed in to change notification settings - Fork 132
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
Allow markbind serve
to specify custom host #2382
#2395
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. |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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).
packages/cli/index.js
Outdated
@@ -56,6 +56,7 @@ program | |||
.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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgot a closing bracket here 😔
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.
The functional code should be good to go once you've addressed the comments - remember to update the relevant documentation and tests afterwards.
packages/cli/index.js
Outdated
@@ -56,6 +56,7 @@ program | |||
.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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of "localhost", let's specify the address as 127.0.0.1
.
@@ -281,7 +281,13 @@ LiveServer.start = function(options) { | |||
setTimeout(function() { | |||
server.listen(0, host); | |||
}, 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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! |
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.
Hi @LamJiuFong thanks for the changes! Last thing is to add a warning as suggested by @itsyme when 0.0.0.0 is used. :)
@@ -158,7 +158,7 @@ function entryPoint(staticHandler, file) { | |||
*/ | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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? |
@@ -111,8 +137,9 @@ function serve(userSpecifiedRoot, options) { | |||
const server = liveServer.start(serverConfig); | |||
server.addListener('listening', () => { | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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!
@@ -158,7 +158,7 @@ function entryPoint(staticHandler, file) { | |||
*/ | |||
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'; | |||
var port = options.port !== undefined ? options.port : 8080; // 0 means random |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
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.
LGTM! Thanks for the work @LamJiuFong
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.
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
-a
option to theserve
commandAnything 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.1
Proposed 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):