Skip to content

Commit

Permalink
chore: improve getFieldValue (#2785)
Browse files Browse the repository at this point in the history
* chore: improve getFieldValue

* remove

* Update benchmarks/cacheGetFieldValues.mjs
  • Loading branch information
Uzlopak authored Feb 20, 2024
1 parent fd66cf0 commit 02107fe
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
23 changes: 23 additions & 0 deletions benchmarks/cacheGetFieldValues.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { bench, group, run } from 'mitata'
import { getFieldValues } from '../lib/cache/util.js'

const values = [
'',
'foo',
'invälid',
'foo, ',
'foo, bar',
'foo, bar, baz',
'foo, bar, baz, ',
'foo, bar, baz, , '
]

group('getFieldValues', () => {
bench('getFieldValues', () => {
for (let i = 0; i < values.length; ++i) {
getFieldValues(values[i])
}
})
})

await run()
2 changes: 1 addition & 1 deletion lib/cache/cache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { kConstruct } = require('./symbols')
const { urlEquals, fieldValues: getFieldValues } = require('./util')
const { urlEquals, getFieldValues } = require('./util')
const { kEnumerableProperty, isDisturbed } = require('../core/util')
const { webidl } = require('../fetch/webidl')
const { Response, cloneResponse, fromInnerResponse } = require('../fetch/response')
Expand Down
12 changes: 4 additions & 8 deletions lib/cache/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,23 @@ function urlEquals (A, B, excludeFragment = false) {
* @see https://github.com/chromium/chromium/blob/694d20d134cb553d8d89e5500b9148012b1ba299/content/browser/cache_storage/cache_storage_cache.cc#L260-L262
* @param {string} header
*/
function fieldValues (header) {
function getFieldValues (header) {
assert(header !== null)

const values = []

for (let value of header.split(',')) {
value = value.trim()

if (!value.length) {
continue
} else if (!isValidHeaderName(value)) {
continue
if (isValidHeaderName(value)) {
values.push(value)
}

values.push(value)
}

return values
}

module.exports = {
urlEquals,
fieldValues
getFieldValues
}
4 changes: 1 addition & 3 deletions lib/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ function isValidReasonPhrase (statusText) {
* @see https://fetch.spec.whatwg.org/#header-name
* @param {string} potentialValue
*/
function isValidHeaderName (potentialValue) {
return isValidHTTPToken(potentialValue)
}
const isValidHeaderName = isValidHTTPToken

/**
* @see https://fetch.spec.whatwg.org/#header-value
Expand Down
19 changes: 19 additions & 0 deletions test/cache/get-field-values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

const { deepStrictEqual, throws } = require('node:assert')
const { test } = require('node:test')
const { getFieldValues } = require('../../lib/cache/util')

test('getFieldValues', () => {
throws(() => getFieldValues(null), {
name: 'AssertionError',
message: 'The expression evaluated to a falsy value:\n\n assert(header !== null)\n'
})
deepStrictEqual(getFieldValues(''), [])
deepStrictEqual(getFieldValues('foo'), ['foo'])
deepStrictEqual(getFieldValues('invälid'), [])
deepStrictEqual(getFieldValues('foo, bar'), ['foo', 'bar'])
deepStrictEqual(getFieldValues('foo, bar, baz'), ['foo', 'bar', 'baz'])
deepStrictEqual(getFieldValues('foo, bar, baz, '), ['foo', 'bar', 'baz'])
deepStrictEqual(getFieldValues('foo, bar, baz, , '), ['foo', 'bar', 'baz'])
})

0 comments on commit 02107fe

Please sign in to comment.