-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.ts
113 lines (99 loc) · 3.38 KB
/
cookie.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2023-2024 the Sequoia authors. All rights reserved. MIT license.
export type CookieValue = string | number | boolean | null
export interface CookieOptions {
domain?: string
path?: string
expires?: Date
httpOnly?: boolean
maxAge?: number
secure?: boolean
sameSite?: 'strict' | 'lax' | 'none' | boolean
signed?: boolean
overwrite?: boolean
}
export function recordToStorage(
src: Record<string, string>,
options: CookieOptions = {},
) {
return Object.entries(src).map((entry) => [
entry[0],
new Cookie(entry[0], entry[1], options),
]) as [string, Cookie][]
}
export function parseCookies(input: string): Record<string, string> {
return input.length
? Object.fromEntries(
input.split('; ').map((v) => v.split(/=(.*)/s).map(decodeURIComponent)),
)
: {}
}
export class Cookie {
readonly name: string
value: string | null
overwrite: boolean
path?: string
httpOnly?: boolean
domain?: string
expires?: Date
maxAge?: number
secure?: boolean
sameSite?: 'strict' | 'lax' | 'none' | boolean
signed?: boolean
constructor(name: string, value?: CookieValue, options?: CookieOptions) {
this.name = name
this.value = value ? encodeURIComponent(value) : null
this.httpOnly = options?.httpOnly
this.path = options?.path
this.domain = options?.domain
this.expires = value ? options?.expires : new Date(1970, 0, 1)
this.maxAge = options?.maxAge
this.secure = options?.secure
this.sameSite = options?.sameSite
this.signed = options?.signed
this.overwrite = options && typeof options.overwrite === 'boolean'
? options.overwrite
: true
}
toString(): string {
if (this.value) {
return `${this.name}=${this.value}${
this.expires ? `; Expires=${this.expires.toUTCString()}` : ''
}${this.httpOnly ? `; HttpOnly` : ''}${this.secure ? `; Secure` : ''}${
this.domain ? `; Domain=${this.domain}` : ''
}${this.path ? `; Path=${this.path}` : ''}${
this.sameSite
? `; SameSite=${this.sameSite}${this.maxAge ? `; Max-Age=${this.maxAge}` : ''}`
: ''
}`
} else {
return `${this.name}=; Expires=${new Date(1970, 0, 1)}; Path=${this.path || '/'}`
}
}
}
export class CookieStorage {
readonly #storage: Map<string, Cookie>
constructor(
src?: Record<string, string> | undefined,
options?: CookieOptions,
) {
this.#storage = src
? new Map<string, Cookie>(
recordToStorage(src, options ?? { overwrite: false }),
)
: new Map()
}
set = (name: string, value: CookieValue, options?: CookieOptions): void => {
this.#storage.set(name, new Cookie(name, value, options))
}
delete = (name: string): void => {
if (this.#storage.has(name)) {
const cookie = this.#storage.get(name) as Cookie
this.#storage.set(name, new Cookie(name, null, { path: cookie.path }))
}
}
get = (name: string): Cookie | null => {
return this.#storage.get(name) || null
}
entries = (): [string, Cookie][] => Array.from(this.#storage.entries())
size = (): number => this.#storage.size
}