Skip to content

Commit

Permalink
code review pt2
Browse files Browse the repository at this point in the history
Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
  • Loading branch information
flakey5 committed Oct 14, 2024
1 parent baaa914 commit 8bf6b42
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/cache/memory-cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,15 +366,15 @@ class MemoryStoreWritableStream extends Writable {
super()
this.#value = value
this.#value.readLock = true
this.#maxEntrySize = maxEntrySize
this.#maxEntrySize = maxEntrySize ?? Infinity
}

get rawTrailers () {
return this.#value.opts.rawTrailers
}

/**
* @param {Buffer[] | undefined} trailers
* @param {string[] | undefined} trailers
*/
set rawTrailers (trailers) {
this.#value.opts.rawTrailers = trailers
Expand Down
8 changes: 5 additions & 3 deletions lib/core/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,14 @@ Object.setPrototypeOf(headerNameLowerCasedRecord, null)
*/
const wellknownHeaderNameBuffers = {}

// Note: object prototypes should not be able to be referenced. e.g. `Object#hasOwnProperty`.
Object.setPrototypeOf(wellknownHeaderNameBuffers, null)

/**
* @param {string} header Lowercased header
* @returns {Buffer}
*/
function getWellknownHeaderNameBuffer (header) {
function getHeaderNameAsBuffer (header) {
let buffer = wellknownHeaderNameBuffers[header]

if (buffer === undefined) {
Expand All @@ -136,6 +139,5 @@ for (let i = 0; i < wellknownHeaderNames.length; ++i) {
module.exports = {
wellknownHeaderNames,
headerNameLowerCasedRecord,
wellknownHeaderNameBuffers,
getWellknownHeaderNameBuffer
getHeaderNameAsBuffer
}
4 changes: 2 additions & 2 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const nodeUtil = require('node:util')
const { stringify } = require('node:querystring')
const { EventEmitter: EE } = require('node:events')
const { InvalidArgumentError } = require('./errors')
const { headerNameLowerCasedRecord, getWellknownHeaderNameBuffer } = require('./constants')
const { headerNameLowerCasedRecord, getHeaderNameAsBuffer } = require('./constants')
const { tree } = require('./tree')

const [nodeMajor, nodeMinor] = process.versions.node.split('.').map(v => Number(v))
Expand Down Expand Up @@ -462,7 +462,7 @@ function encodeHeaders (headers) {
rawValue = Buffer.from(value)
}

const headerBuffer = getWellknownHeaderNameBuffer(header)
const headerBuffer = getHeaderNameAsBuffer(header)

rawHeaders[rawHeadersIndex] = headerBuffer
rawHeadersIndex++
Expand Down
9 changes: 4 additions & 5 deletions lib/handler/cache-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,19 @@ class CacheHandler extends DecoratorHandler {
* @param {Buffer[]} rawHeaders
* @param {() => void} resume
* @param {string} statusMessage
* @param {Record<string, string | string[]> | undefined} headers
* @returns {boolean}
*/
onHeaders (
statusCode,
rawHeaders,
resume,
statusMessage,
headers = util.parseHeaders(rawHeaders)
statusMessage
) {
const downstreamOnHeaders = () => this.#handler.onHeaders(
statusCode,
rawHeaders,
resume,
statusMessage,
headers
statusMessage
)

if (
Expand All @@ -101,6 +98,8 @@ class CacheHandler extends DecoratorHandler {
return downstreamOnHeaders()
}

const headers = util.parseHeaders(rawHeaders)

const cacheControlHeader = headers['cache-control']
const contentLengthHeader = headers['content-length']

Expand Down
7 changes: 2 additions & 5 deletions lib/handler/cache-revalidation-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,13 @@ class CacheRevalidationHandler extends DecoratorHandler {
* @param {Buffer[]} rawHeaders
* @param {() => void} resume
* @param {string} statusMessage
* @param {string[] | undefined} headers
* @returns {boolean}
*/
onHeaders (
statusCode,
rawHeaders,
resume,
statusMessage,
headers = undefined
statusMessage
) {
// https://www.rfc-editor.org/rfc/rfc9111.html#name-handling-a-validation-respo
if (statusCode === 304) {
Expand All @@ -71,8 +69,7 @@ class CacheRevalidationHandler extends DecoratorHandler {
statusCode,
rawHeaders,
resume,
statusMessage,
headers
statusMessage
)
}
}
Expand Down

0 comments on commit 8bf6b42

Please sign in to comment.