Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve getFieldValue #2785

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions benchmarks/cacheGetFieldValues.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { bench, group, run } from 'mitata'
import { getFieldValues } from '../lib/cache/util.js'

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

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
18 changes: 10 additions & 8 deletions lib/cache/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,33 @@ function urlEquals (A, B, excludeFragment = false) {
return serializedA === serializedB
}

const emptyArray = Object.freeze([])

/**
* @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)

if (header === '') {
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
return emptyArray
}

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
20 changes: 20 additions & 0 deletions test/cache/get-field-values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const { deepStrictEqual, throws, strictEqual } = 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(''), [])
strictEqual(Object.isFrozen(getFieldValues('')), true)
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'])
})
Loading