Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix restify (#88) by only using req.path if a string #89

Merged
merged 4 commits into from
Feb 23, 2022

Conversation

rektide
Copy link
Contributor

@rektide rektide commented Feb 22, 2022

only use req.path if it is a string. this is for restify compatibility; fixes #88.

in #29 we preferred req.path, because req.url would throw in hapi if the path was invalid.
in restify though, req.path is a function (which returns a parsed url object). so after #29, restify stopped logging url.

in this pr, we check req.path to see if it is a string before using it. else we go back to using req.url. alternatively, we could have checked if path was a function, then checked if the object returned had a path element, but simply being more specific about what req.path's we accept was preferred as it reduced the amount of branches the code would take.

i have also added a check for req.url being undefined, as i nearly hadn't considered that case & almost left the code in a broken/error-throwing state then.

req.url.path

side discussion- we could possibly simplify more, see rektide#2 which i am happy to do.

right now, this pr still leaves in an attempt to use req.url.path, which was introduced to support hapi a number of years ago. since we now use req.path to better/more-safely support hapi (as of #29), we could consider removing req.url.path checking: these old checks are redunant. hapi has supported req.path since before 2013's v1.0.0 so no risk there.

yet there may be people in the world other than hapi who have grown to expect req.url.path. i tend to doubt that, and it's tempting to accept that risk & simplify/drop the req.url.path check, but i have left it intact for now. happy to do whatever folks want.

in pinojs#29 we preferred req.path, because req.url would throw in hapi if the path was invalid. in restify though, req.path is a function, and this change made the url no longer log. check req.path to see if it is a function, and invoke it if it is.
@rektide rektide marked this pull request as draft February 22, 2022 20:12
@rektide
Copy link
Contributor Author

rektide commented Feb 22, 2022

Crud, issue is slightly hairier. Converted to draft temporarily.

In restify, path()/getUrl() (path() is an alias) return a parsed url object. Handling this.

restify has a req.path but it's a function that returns an object. rather than try to determine how to handle path if we have it, only use path if it's a simple string. otherwise, keep using req.url, like we used to pre pinojs#29.
it's tempting to simply stop looking at req.url.path altogether, since that was introduced for hapi 17 in pinojs@83d58a7#diff-2c4782cb20f7f0d7ca90c7bc04939f4420c4bdb42fdce618984da393ba9202f7R60 .

but since pinojs#29 we look at req.path first, which is safer in hapi, so hapi doesn't care about req.url.path anymore.

but perhaps someone else does depend on req.url.path . so for now, continuing to look at req.url.path , which means a bit more guarding in the req serializer.
@rektide rektide changed the title Call req.path() if a function. Fixes #88 / restify. Fix restify (#88) by only using req.path if a string Feb 23, 2022
@rektide rektide marked this pull request as ready for review February 23, 2022 00:32
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit 2b9d5f5 into pinojs:master Feb 23, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Restify req.url broken by #29
2 participants