Skip to content

refactor(serve): add custom parser function for --port option#44

Merged
yusukebe merged 6 commits intohonojs:mainfrom
ysknsid25:refactor/add-custom-parser-function-for-port-option
Nov 30, 2025
Merged

refactor(serve): add custom parser function for --port option#44
yusukebe merged 6 commits intohonojs:mainfrom
ysknsid25:refactor/add-custom-parser-function-for-port-option

Conversation

@ysknsid25
Copy link
Copy Markdown
Contributor

Continued from #41

Signed-off-by: ysknsid25 <kengo071225@gmail.com>
Signed-off-by: ysknsid25 <kengo071225@gmail.com>
@usualoma
Copy link
Copy Markdown
Member

usualoma commented Nov 8, 2025

Hi @ysknsid25, thank you for creating the pull request.

(For instance, when using it as a simple server within Docker) Provided the user possesses the necessary permissions, I see no particular reason why they should not be able to specify ports below 1024.

Signed-off-by: ysknsid25 <kengo071225@gmail.com>
@ysknsid25
Copy link
Copy Markdown
Contributor Author

Thank you for reviewing, @usualoma

For instance, when using it as a simple server within Docker

Yes, that's right. I fixed port range 👍

@usualoma
Copy link
Copy Markdown
Member

usualoma commented Nov 8, 2025

@ysknsid25
Thank you for changing the port range! I've confirmed it.

when the port specification is incorrect

I'd also like to hear @yusukebe's opinion, but I think that if the port specification is incorrect, it's better to terminate abnormally rather than fall back to the default (7070) port.

--port 99999

or

--port oops

zero

It's difficult to say whether there's a specific use case, but since ‘specifying 0 for the port number causes the OS to automatically assign an available ephemeral port’ is a common behaviour supported by many operating systems, it seems acceptable for hono cli to accept this (as it does currently).

% npx hono serve --port 0
Listening on http://localhost:58650

values other than numerical ones

This is also the case on the current main branch, and it's not an issue specific to this pull request, but if we decide to implement validation, I believe we'll need to determine how to handle values such as the following.

--port 2048GB # Is this interpreted as 2048? Or is it an invalid value?

@yusukebe
Copy link
Copy Markdown
Member

yusukebe commented Nov 9, 2025

Hi @ysknsid25 thank you for the PR!

when the port specification is incorrect
zero

I totally agree with the two things @usualoma said.

values other than numerical ones

I think treating 2048GB as an invalid value is better.

Signed-off-by: ysknsid25 <kengo071225@gmail.com>

await program.parseAsync(['node', 'test', 'serve', '-p', '0'])

expect(mockServe).toHaveBeenCalledWith(
Copy link
Copy Markdown
Contributor Author

@ysknsid25 ysknsid25 Nov 11, 2025

Choose a reason for hiding this comment

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

Should I implement mockserve to be more like serve??🤔
For example,

  • When mock receives 0, mock returns 12345.
  • When mock receives others port number, mock returns itself.

@ysknsid25
Copy link
Copy Markdown
Contributor Author

Thanks for reviews, @usualoma @yusukebe 🙌

  • Added verification for cases such as 2048GB
  • Fixed to pass 0 instead of 7070 when 0 is specified.

e.g.)

% node ./dist/cli.js serve -p 0
Listening on http://localhost:65107

@usualoma
Copy link
Copy Markdown
Member

Hi @ysknsid25, thank you for update!

In the comments below, there is an opinion that if an invalid value is passed to --port, the program should terminate abnormally instead of falling back to 7070. What is your opinion on this?

@ysknsid25
Copy link
Copy Markdown
Contributor Author

ysknsid25 commented Nov 12, 2025

@usualoma

it's better to terminate

Sorry, I was misunderstanding!!😭 I'll fix it🙇🙇🙇

if (!/^\d+$/.test(value)) {
throw new Error(`Includes non-digit characters in ${value}\n`)
}
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@usualoma

We also believe that the limit option of search should also be treated 2048GM as an invalid value, so we have provided consistent validation.

However, I have new worries... please give me opinion.

About also -1, hoge, We would like to treate as invalid value?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see.

I think "limit for search" and "port for serve" are slightly different in nature.

--limit for search

  • Falling back to a value different from the specified one still returns meaningful results (at least more meaningful than an abnormal termination)
  • Falling back to a value different from the specified one does not cause security issues

--port for serve

  • Falling back to a different value may cause the server to listen on an unintended port, potentially unnoticed by the user (though sometimes meaningful, it causes confusion)
  • Falling back to a different value may cause a development server to be exposed externally on an unintended port. While extremely rare, this could potentially lead to security issues

That's how I see it, so I don't think we need to force them together. But if -1 or hoge is specified for limit, I think it should still be an abnormal termination.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Well, if it doesn't confuse users or pose any security issues, I don't think it's necessary to spend that much time addressing it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Regarding --port 99999, I believe it should return an error rather than fall back to 7070, as it causes user confusion for the reasons stated above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your advice. Please re-review 🙏

Signed-off-by: ysknsid25 <kengo071225@gmail.com>
@ysknsid25 ysknsid25 force-pushed the refactor/add-custom-parser-function-for-port-option branch from 2bd60b1 to 613e3a0 Compare November 14, 2025 09:51
@ysknsid25 ysknsid25 requested a review from usualoma November 14, 2025 09:56
@usualoma
Copy link
Copy Markdown
Member

@ysknsid25 Thank you for putting in so much effort. It looks great to me!

Copy link
Copy Markdown
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

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

LGTM!

@yusukebe
Copy link
Copy Markdown
Member

@ysknsid25

Sorry to be late!

Looks good. This seems to be a "refactor", but the behavior is slightly changed. So I'll release a new version with this change. Thanks!

@yusukebe yusukebe merged commit 017df42 into honojs:main Nov 30, 2025
3 checks passed
@yusukebe
Copy link
Copy Markdown
Member

@ysknsid25

The test fails: https://github.com/honojs/cli/actions/runs/19797511278/job/56720018892

Can you work on fixing it? I think you should fix the mocking.

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.

3 participants