Skip to content

Commit

Permalink
refactor: replace setHeaders function with optimized inline header se…
Browse files Browse the repository at this point in the history
…tting
  • Loading branch information
Phillip9587 committed Nov 25, 2024
1 parent 401ec84 commit 733b318
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 21 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==================

* refactor: replace setHeaders function with optimized inline header setting
* remove unnecessary devDependency `safe-buffer`
* remove `unpipe` package and use native `unpipe()` method
* remove unnecessary devDependency `readable-stream`
Expand Down
24 changes: 3 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ function send (req, res, status, headers, message) {
res.removeHeader('Content-Range')

// response headers
setHeaders(res, headers)
for (const [key, value] of Object.entries(headers ?? {})) {
res.setHeader(key, value)
}

// security headers
res.setHeader('Content-Security-Policy', "default-src 'none'")
Expand Down Expand Up @@ -318,23 +320,3 @@ function send (req, res, status, headers, message) {
onFinished(req, write)
req.resume()
}

/**
* Set response headers from an object.
*
* @param {OutgoingMessage} res
* @param {object} headers
* @private
*/

function setHeaders (res, headers) {
if (!headers) {
return
}

var keys = Object.keys(headers)
for (var i = 0; i < keys.length; i++) {
var key = keys[i]
res.setHeader(key, headers[key])
}
}

0 comments on commit 733b318

Please sign in to comment.