-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
171 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { HttpRequest } from './model.js'; | ||
|
||
export type ParsedCookies = Record<string, string>; | ||
|
||
export function createLazyCookiesAccessor(requestHeaders: HttpRequest['headers']): ParsedCookies { | ||
const cookieHeaderContents = requestHeaders['cookie']; | ||
if (cookieHeaderContents === undefined) { | ||
return {}; | ||
} | ||
|
||
const parsedCookies: ParsedCookies = {}; | ||
|
||
let wasParsingPerformed = false; | ||
function parseCookies() { | ||
Object.assign(parsedCookies, parseCookiesFromCookieHeader(cookieHeaderContents as string)) | ||
wasParsingPerformed = true; | ||
} | ||
|
||
return new Proxy(parsedCookies, { | ||
get(target, prop, receiver) { | ||
if (!wasParsingPerformed) { parseCookies(); } | ||
return Reflect.get(target, prop, receiver); | ||
}, | ||
|
||
has(target, prop) { | ||
if (!wasParsingPerformed) { parseCookies(); } | ||
return Reflect.has(target, prop); | ||
} | ||
}); | ||
} | ||
|
||
|
||
const validCookieNameRegExp = /^[\w!#$%&'*.^`|~+-]+$/; | ||
const validCookieValueRegExp = /^[ !#-:<-[\]-~]*$/; | ||
|
||
export function parseCookiesFromCookieHeader(cookieHeaderValue: string): ParsedCookies { | ||
const pairs = cookieHeaderValue.trim().split(';'); | ||
|
||
return pairs.reduce<ParsedCookies>((parsedCookie, pairStr) => { | ||
pairStr = pairStr.trim(); | ||
|
||
const valueStartPos = pairStr.indexOf('='); | ||
if (valueStartPos === -1) { | ||
return parsedCookie; | ||
} | ||
|
||
const cookieName = pairStr.substring(0, valueStartPos).trim(); | ||
if (!validCookieNameRegExp.test(cookieName)) { | ||
return parsedCookie; | ||
} | ||
|
||
let cookieValue = pairStr.substring(valueStartPos + 1).trim(); | ||
if (cookieValue.startsWith('"') && cookieValue.endsWith('"')) { | ||
cookieValue = cookieValue.slice(1, -1); | ||
} | ||
if (validCookieValueRegExp.test(cookieValue)) { | ||
parsedCookie[cookieName] = decodeURIComponent(cookieValue); | ||
} | ||
|
||
return parsedCookie; | ||
}, {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters