-
-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Refactor res.send() into helper functions #6654
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
base: master
Are you sure you want to change the base?
Refactor res.send() into helper functions #6654
Conversation
…ainability Signed-off-by: Nitin <[email protected]>
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.
✅
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.
Thank you for your contribution. At the moment, I’m a bit unsure if this would be a good idea. I don’t think it would improve the code’s readability or its maintenance much. I’m going to block it until more people on the team can review it
if (!this.get('Content-Type')) this.type('html'); | ||
encoding = 'utf8'; |
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.
Using curly braces would make it more consistent and readable.
} else if (typeof chunk === 'boolean' || typeof chunk === 'number' || chunk === null) { | ||
chunk = chunk === null ? '' : chunk; |
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.
It would make sense to split the null
case from the boolean | number
case to avoid using a ternary.
const etagFn = app.get('etag fn'); | ||
const shouldGenerateETag = !res.get('ETag') && typeof etagFn === 'function'; | ||
|
||
if (chunk === undefined) return chunk; |
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.
We could even return earlier.
Summary
Refactored
res.send()
inresponse.js
to improve readability and maintainability. Broke up the large method into smaller helper functions:prepareChunk
setETagHeader
stripBodyIfNotAllowed
Behavior is preserved 100%. This helps future contributors understand and modify
res.send()
more easily.