Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* 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

authored by TATSUNO “Taz” Yasuhiro · committed by GitHub · parent e9830c6 · Browse files · 2 files+22 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+22 added−6 removed

  • tests:+8 added−0 removed
  • other:+14 added−6 removed

2 files+22 added−6 removed

modified:src/utils/cookie.test.ts+8 added
28 unmodified lines
29 expect(cookie['yummy_cookie']).toBe('choco')30 expect(cookie['tasty_cookie']).toBeUndefined()31 })3233 it('Should parse one cookie specified by name even if it is not found', () => {34 const cookieString = 'yummy_cookie=choco; tasty_cookie = strawberry '35 const cookie: Cookie = parse(cookieString, 'no_such_cookie')36 expect(cookie['yummy_cookie']).toBeUndefined()37 expect(cookie['tasty_cookie']).toBeUndefined()38 expect(cookie['no_such_cookie']).toBeUndefined()39 })4041 it('Should parse cookies with no value', () => {42 const cookieString = 'yummy_cookie=; tasty_cookie = ; best_cookie= ; last_cookie=""'43 const cookie: Cookie = parse(cookieString)44 expect(cookie['yummy_cookie']).toBe('')
219 unmodified lines
modified:src/utils/cookie.ts+14 added-6 removed
75 unmodified lines
76// (see: https://github.com/golang/go/issues/7243)77const validCookieValueRegEx = /^[ !#-:<-[\]-~]*$/7879export const parse = (cookie: string, name?: string): Cookie => {80 if (name && cookie.indexOf(name) === -1) {81 // Fast-path: return immediately if the demanded-key is not in the cookie string82 return {}83 }84 const pairs = cookie.trim().split(';')81 return pairs.reduce((parsedCookie, pairStr) => {85 const parsedCookie: Cookie = {}86 for (let pairStr of pairs) {87 pairStr = pairStr.trim()88 const valueStartPos = pairStr.indexOf('=')89 if (valueStartPos === -1) {85 return parsedCookie90 continue91 }9293 const cookieName = pairStr.substring(0, valueStartPos).trim()94 if ((name && name !== cookieName) || !validCookieNameRegEx.test(cookieName)) {90 return parsedCookie95 continue96 }9798 let cookieValue = pairStr.substring(valueStartPos + 1).trim()99 if (cookieValue.startsWith('"') && cookieValue.endsWith('"')) {100 cookieValue = cookieValue.slice(1, -1)101 }102 if (validCookieValueRegEx.test(cookieValue)) {103 parsedCookie[cookieName] = decodeURIComponent_(cookieValue)104 if (name) {105 // Fast-path: return only the demanded-key immediately. Other keys are not needed.106 break107 }108 }100101 return parsedCookie102 }, {} as Cookie)109 }110 return parsedCookie111}112113export const parseSigned = async (114 cookie: string,
124 unmodified lines