-
-
Notifications
You must be signed in to change notification settings - Fork 21.2k
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
Open
nitin-is-me
wants to merge
1
commit into
expressjs:master
Choose a base branch
from
nitin-is-me:refactor/res-send-helpers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,107 +123,98 @@ res.links = function(links) { | |
*/ | ||
|
||
res.send = function send(body) { | ||
var chunk = body; | ||
var encoding; | ||
var req = this.req; | ||
var type; | ||
const req = this.req; | ||
const app = this.app; | ||
|
||
// settings | ||
var app = this.app; | ||
let chunk = body; | ||
let encoding; | ||
let type; | ||
|
||
switch (typeof chunk) { | ||
// string defaulting to html | ||
case 'string': | ||
if (!this.get('Content-Type')) { | ||
this.type('html'); | ||
} | ||
break; | ||
case 'boolean': | ||
case 'number': | ||
case 'object': | ||
if (chunk === null) { | ||
chunk = ''; | ||
} else if (ArrayBuffer.isView(chunk)) { | ||
if (!this.get('Content-Type')) { | ||
this.type('bin'); | ||
} | ||
} else { | ||
return this.json(chunk); | ||
} | ||
break; | ||
if (typeof chunk === 'string') { | ||
if (!this.get('Content-Type')) this.type('html'); | ||
encoding = 'utf8'; | ||
} else if (typeof chunk === 'boolean' || typeof chunk === 'number' || chunk === null) { | ||
chunk = chunk === null ? '' : chunk; | ||
Comment on lines
+136
to
+137
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would make sense to split the |
||
} else if (typeof chunk === 'object') { | ||
if (ArrayBuffer.isView(chunk)) { | ||
if (!this.get('Content-Type')) this.type('bin'); | ||
} else { | ||
return this.json(chunk); | ||
} | ||
} | ||
|
||
// write strings in utf-8 | ||
if (typeof chunk === 'string') { | ||
encoding = 'utf8'; | ||
type = this.get('Content-Type'); | ||
|
||
// reflect this in content-type | ||
if (typeof type === 'string') { | ||
this.set('Content-Type', setCharset(type, 'utf-8')); | ||
} | ||
} | ||
|
||
// determine if ETag should be generated | ||
var etagFn = app.get('etag fn') | ||
var generateETag = !this.get('ETag') && typeof etagFn === 'function' | ||
|
||
// populate Content-Length | ||
var len | ||
if (chunk !== undefined) { | ||
if (Buffer.isBuffer(chunk)) { | ||
// get length of Buffer | ||
len = chunk.length | ||
} else if (!generateETag && chunk.length < 1000) { | ||
// just calculate length when no ETag + small chunk | ||
len = Buffer.byteLength(chunk, encoding) | ||
} else { | ||
// convert chunk to Buffer and calculate | ||
chunk = Buffer.from(chunk, encoding) | ||
encoding = undefined; | ||
len = chunk.length | ||
} | ||
|
||
this.set('Content-Length', len); | ||
} | ||
chunk = prepareChunk(this, chunk, encoding); | ||
|
||
// populate ETag | ||
var etag; | ||
if (generateETag && len !== undefined) { | ||
if ((etag = etagFn(chunk, encoding))) { | ||
this.set('ETag', etag); | ||
} | ||
} | ||
setETagHeader(this, chunk, encoding); | ||
|
||
// freshness | ||
if (req.fresh) this.status(304); | ||
|
||
// strip irrelevant headers | ||
if (204 === this.statusCode || 304 === this.statusCode) { | ||
this.removeHeader('Content-Type'); | ||
this.removeHeader('Content-Length'); | ||
this.removeHeader('Transfer-Encoding'); | ||
chunk = ''; | ||
} | ||
|
||
// alter headers for 205 | ||
if (this.statusCode === 205) { | ||
this.set('Content-Length', '0') | ||
this.removeHeader('Transfer-Encoding') | ||
chunk = '' | ||
} | ||
stripBodyIfNotAllowed(this); | ||
|
||
if (req.method === 'HEAD') { | ||
// skip body for HEAD | ||
this.end(); | ||
} else { | ||
// respond | ||
this.end(chunk, encoding); | ||
} | ||
|
||
return this; | ||
}; | ||
|
||
function prepareChunk(res, chunk, encoding) { | ||
const app = res.app; | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. We could even return earlier. |
||
|
||
let len; | ||
|
||
if (Buffer.isBuffer(chunk)) { | ||
len = chunk.length; | ||
} else if (!shouldGenerateETag && chunk.length < 1000) { | ||
len = Buffer.byteLength(chunk, encoding); | ||
} else { | ||
chunk = Buffer.from(chunk, encoding); | ||
encoding = undefined; | ||
len = chunk.length; | ||
} | ||
|
||
res.set('Content-Length', len); | ||
return chunk; | ||
} | ||
|
||
function setETagHeader(res, chunk, encoding) { | ||
const etagFn = res.app.get('etag fn'); | ||
if (!res.get('ETag') && typeof etagFn === 'function' && chunk != null) { | ||
const etag = etagFn(chunk, encoding); | ||
if (etag) res.set('ETag', etag); | ||
} | ||
} | ||
|
||
function stripBodyIfNotAllowed(res) { | ||
const status = res.statusCode; | ||
|
||
if (status === 204 || status === 304) { | ||
res.removeHeader('Content-Type'); | ||
res.removeHeader('Content-Length'); | ||
res.removeHeader('Transfer-Encoding'); | ||
return res.end(''); | ||
} | ||
|
||
if (status === 205) { | ||
res.set('Content-Length', '0'); | ||
res.removeHeader('Transfer-Encoding'); | ||
return res.end(''); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Send JSON response. | ||
* | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.