0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-21 10:08:58 +01:00

perf(helper/cookie): fast-path for name specified (#3608)

* perf(helper/cookie): fast-path for a specific key

* perf(helper/cookie): fast-path for a specific key not found

* perf(helper/cookie): added test for missing case

* perf(helper/cookie): fix tests

* perf(helper/cookie): fix tests

* perf(helper/cookie): cleanup tests
This commit is contained in:
TATSUNO “Taz” Yasuhiro 2024-11-03 07:58:31 +09:00 committed by GitHub
parent e9830c6ba1
commit e43f203a2a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 6 deletions

View File

@ -30,6 +30,14 @@ describe('Parse cookie', () => {
expect(cookie['tasty_cookie']).toBeUndefined()
})
it('Should parse one cookie specified by name even if it is not found', () => {
const cookieString = 'yummy_cookie=choco; tasty_cookie = strawberry '
const cookie: Cookie = parse(cookieString, 'no_such_cookie')
expect(cookie['yummy_cookie']).toBeUndefined()
expect(cookie['tasty_cookie']).toBeUndefined()
expect(cookie['no_such_cookie']).toBeUndefined()
})
it('Should parse cookies with no value', () => {
const cookieString = 'yummy_cookie=; tasty_cookie = ; best_cookie= ; last_cookie=""'
const cookie: Cookie = parse(cookieString)

View File

@ -77,17 +77,22 @@ const validCookieNameRegEx = /^[\w!#$%&'*.^`|~+-]+$/
const validCookieValueRegEx = /^[ !#-:<-[\]-~]*$/
export const parse = (cookie: string, name?: string): Cookie => {
if (name && cookie.indexOf(name) === -1) {
// Fast-path: return immediately if the demanded-key is not in the cookie string
return {}
}
const pairs = cookie.trim().split(';')
return pairs.reduce((parsedCookie, pairStr) => {
const parsedCookie: Cookie = {}
for (let pairStr of pairs) {
pairStr = pairStr.trim()
const valueStartPos = pairStr.indexOf('=')
if (valueStartPos === -1) {
return parsedCookie
continue
}
const cookieName = pairStr.substring(0, valueStartPos).trim()
if ((name && name !== cookieName) || !validCookieNameRegEx.test(cookieName)) {
return parsedCookie
continue
}
let cookieValue = pairStr.substring(valueStartPos + 1).trim()
@ -96,10 +101,13 @@ export const parse = (cookie: string, name?: string): Cookie => {
}
if (validCookieValueRegEx.test(cookieValue)) {
parsedCookie[cookieName] = decodeURIComponent_(cookieValue)
if (name) {
// Fast-path: return only the demanded-key immediately. Other keys are not needed.
break
}
}
return parsedCookie
}, {} as Cookie)
}
return parsedCookie
}
export const parseSigned = async (