You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.1 KiB
50 lines
1.1 KiB
import {Request} from "../lifecycle/Request";
|
|
import {infer} from "@extollo/util";
|
|
|
|
/**
|
|
* Base type representing a parsed cookie.
|
|
*/
|
|
export interface HTTPCookie {
|
|
key: string,
|
|
originalValue: string,
|
|
value: any,
|
|
exists: boolean,
|
|
}
|
|
|
|
export type MaybeHTTPCookie = HTTPCookie | undefined;
|
|
|
|
export class HTTPCookieJar {
|
|
protected parsed: {[key: string]: HTTPCookie} = {}
|
|
|
|
constructor(
|
|
protected request: Request,
|
|
) {
|
|
this.parseCookies()
|
|
}
|
|
|
|
get(name: string): MaybeHTTPCookie {
|
|
if ( name in this.parsed ) {
|
|
return this.parsed[name]
|
|
}
|
|
}
|
|
|
|
private parseCookies() {
|
|
const cookies = String(this.request.getHeader('cookie'))
|
|
cookies.split(';').forEach(cookie => {
|
|
const parts = cookie.split('=')
|
|
|
|
const key = parts.shift()?.trim()
|
|
if ( !key ) return;
|
|
|
|
const value = decodeURI(parts.join('='))
|
|
|
|
this.parsed[key] = {
|
|
key,
|
|
originalValue: value,
|
|
value: infer(value),
|
|
exists: true,
|
|
}
|
|
})
|
|
}
|
|
}
|