Skip to content

Commit

Permalink
refactor: update Reply module (#4072)
Browse files Browse the repository at this point in the history
* refactor: update Reply module

* Use node.js 14 syntax
* Improve code readability
* Reuse Reply's methods where possible

* refactor: partial revert

* refactor: revert for in loop

* refactor: if condition

* refactor: partial revert
  • Loading branch information
denshakhov committed Jul 1, 2022
1 parent 438369f commit c525e67
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions lib/reply.js
Expand Up @@ -203,10 +203,8 @@ Reply.prototype.getHeaders = function () {

Reply.prototype.hasHeader = function (key) {
key = key.toLowerCase()
if (this[kReplyHeaders][key] !== undefined) {
return true
}
return this.raw.hasHeader(key)

return this[kReplyHeaders][key] !== undefined || this.raw.hasHeader(key)
}

Reply.prototype.removeHeader = function (key) {
Expand All @@ -216,25 +214,24 @@ Reply.prototype.removeHeader = function (key) {
return this
}

Reply.prototype.header = function (key, value) {
const _key = key.toLowerCase()

// default the value to ''
value = value === undefined ? '' : value
Reply.prototype.header = function (key, value = '') {
key = key.toLowerCase()

if (this[kReplyHeaders][_key] && _key === 'set-cookie') {
if (this[kReplyHeaders][key] && key === 'set-cookie') {
// https://tools.ietf.org/html/rfc7230#section-3.2.2
if (typeof this[kReplyHeaders][_key] === 'string') {
this[kReplyHeaders][_key] = [this[kReplyHeaders][_key]]
if (typeof this[kReplyHeaders][key] === 'string') {
this[kReplyHeaders][key] = [this[kReplyHeaders][key]]
}

if (Array.isArray(value)) {
Array.prototype.push.apply(this[kReplyHeaders][_key], value)
this[kReplyHeaders][key].push(...value)
} else {
this[kReplyHeaders][_key].push(value)
this[kReplyHeaders][key].push(value)
}
} else {
this[kReplyHeaders][_key] = value
this[kReplyHeaders][key] = value
}

return this
}

Expand All @@ -245,6 +242,7 @@ Reply.prototype.headers = function (headers) {
const key = keys[i]
this.header(key, headers[key])
}

return this
}

Expand Down Expand Up @@ -279,8 +277,7 @@ Reply.prototype.trailer = function (key, fn) {
}

Reply.prototype.hasTrailer = function (key) {
if (this[kReplyTrailers] === null) return false
return this[kReplyTrailers][key.toLowerCase()] !== undefined
return this[kReplyTrailers]?.[key.toLowerCase()] !== undefined
}

Reply.prototype.removeTrailer = function (key) {
Expand Down Expand Up @@ -330,8 +327,7 @@ Reply.prototype.redirect = function (code, url) {
code = this[kReplyHasStatusCode] ? this.raw.statusCode : 302
}

this.header('location', url).code(code).send()
return this
return this.header('location', url).code(code).send()
}

Reply.prototype.callNotFound = function () {
Expand Down Expand Up @@ -485,9 +481,12 @@ function onSendEnd (reply, payload) {
}

if (reply[kReplyTrailers] === null) {
if (!reply[kReplyHeaders]['content-length']) {
reply[kReplyHeaders]['content-length'] = '' + Buffer.byteLength(payload)
} else if (req.raw.method !== 'HEAD' && reply[kReplyHeaders]['content-length'] !== Buffer.byteLength(payload)) {
const contentLength = reply[kReplyHeaders]['content-length']
if (!contentLength ||
(req.raw.method !== 'HEAD' &&
parseInt(contentLength, 10) !== Buffer.byteLength(payload)
)
) {
reply[kReplyHeaders]['content-length'] = '' + Buffer.byteLength(payload)
}
}
Expand Down

0 comments on commit c525e67

Please sign in to comment.