Skip to content

Commit

Permalink
Use Set instead of Map in constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Murderlon committed Sep 16, 2023
1 parent 75f62e7 commit 9a0ceb8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 33 deletions.
51 changes: 21 additions & 30 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,45 +49,36 @@ export const securityHeaders = {
* remote server.
* Only those present and true are forwarded.
*
* @type {Readonly<Map<string, boolean>>}
* @type {Readonly<Set<string>>}
*/
export const defaultRequestHeaders = new Map([
['Accept', true],
['Accept-Charset', true],
// No `Accept-Encoding` because images (other than from XML or SVG), don’t
// typically benefit from compression:
['Accept-Encoding', false],
['Accept-Language', true],
['Cache-Control', true],
['If-None-Match', true],
['If-Modified-Since', true],
// `Range` is used by Safari for byte range requests on video:
['Range', true],
// No `X-Forwarded-For` because ???
['X-Forwarded-For', false]
export const defaultRequestHeaders = new Set([
'Accept',
'Accept-Charset',
'Accept-Language',
'Cache-Control',
'If-None-Match',
'If-Modified-Since',
'Range' // used by Safari for byte range requests on video
])

/**
* HTTP response headers that are acceptable to pass from the remote server to
* the client.
* Only those present and true are forwarded.
*
* @type {Readonly<Map<string, boolean>>}
* @type {Readonly<Set<string>>}
*/
export const defaultResponseHeaders = new Map([
// `Accept-Ranges` is used by Safari for byte range requests on video:
['Accept-Ranges', true],
['Cache-Control', true],
['Content-Length', true],
['Content-Encoding', true],
['Content-Range', true],
['Content-Type', true],
['ETag', true],
['Expires', true],
['Last-Modified', true],
// `Server` is disallowed because ???
['Server', false],
['Transfer-Encoding', true]
export const defaultResponseHeaders = new Set([
'Accept-Ranges', // used by Safari for byte range requests on video
'Cache-Control',
'Content-Length',
'Content-Encoding',
'Content-Range',
'Content-Type',
'ETag',
'Expires',
'Last-Modified',
'Transfer-Encoding'
])

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ import {
* Filter incoming headers based on allowed headers.
* We always want to send Pascal-Case headers so we use the key from constants
* with the value from the incoming header.
* @param {Map<string, boolean>} allowedHeaders
* @param {Set<string>} allowedHeaders
* @returns {(incomingHeaders: Headers) => Record<string, string>}
* */
function filterHeaders(allowedHeaders) {
return function (incomingHeaders) {
/** @type {Record<string, string>} */
const filteredHeaders = {}

for (const [key, bool] of allowedHeaders.entries()) {
for (const key of allowedHeaders.keys()) {
const value = incomingHeaders.get(key.toLowerCase())

if (value !== null && value !== undefined && bool) {
if (value !== null && value !== undefined) {
filteredHeaders[key] = value
}
}
Expand Down

0 comments on commit 9a0ceb8

Please sign in to comment.