From 31734621b09afcb3c653f9fb1f23ceff5b39bd51 Mon Sep 17 00:00:00 2001 From: Nitin Date: Tue, 22 Jul 2025 15:13:59 +0530 Subject: [PATCH] refactor(res.send): break into helper functions for clarity and maintainability Signed-off-by: Nitin --- lib/response.js | 145 +++++++++++++++++++++++------------------------- 1 file changed, 68 insertions(+), 77 deletions(-) diff --git a/lib/response.js b/lib/response.js index 7a2f0ecce56..23c3ba0af66 100644 --- a/lib/response.js +++ b/lib/response.js @@ -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; + } 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; + + 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. *