-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathserialize.ts
195 lines (168 loc) · 5.78 KB
/
serialize.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import type { RequestCookie, ResponseCookie } from './types'
export function stringifyCookie(c: ResponseCookie | RequestCookie): string {
const attrs = [
'path' in c && c.path && `Path=${c.path}`,
'expires' in c &&
(c.expires || c.expires === 0) &&
`Expires=${(typeof c.expires === 'number'
? new Date(c.expires)
: c.expires
).toUTCString()}`,
'maxAge' in c && typeof c.maxAge === 'number' && `Max-Age=${c.maxAge}`,
'domain' in c && c.domain && `Domain=${c.domain}`,
'secure' in c && c.secure && 'Secure',
'httpOnly' in c && c.httpOnly && 'HttpOnly',
'sameSite' in c && c.sameSite && `SameSite=${c.sameSite}`,
'partitioned' in c && c.partitioned && 'Partitioned',
'priority' in c && c.priority && `Priority=${c.priority}`,
].filter(Boolean)
const stringified = `${c.name}=${encodeURIComponent(c.value ?? '')}`
return attrs.length === 0 ? stringified : `${stringified}; ${attrs.join('; ')}`
}
/** Parse a `Cookie` header value */
export function parseCookie(cookie: string) {
const map = new Map<string, string>()
for (const pair of cookie.split(/; */)) {
if (!pair) continue
const splitAt = pair.indexOf('=')
// If the attribute doesn't have a value, set it to 'true'.
if (splitAt === -1) {
map.set(pair, 'true')
continue
}
// Otherwise split it into key and value and trim the whitespace on the
// value.
const [key, value] = [pair.slice(0, splitAt), pair.slice(splitAt + 1)]
try {
map.set(key, decodeURIComponent(value ?? 'true'))
} catch {
// ignore invalid encoded values
}
}
return map
}
/** Parse a `Set-Cookie` header value */
export function parseSetCookie(setCookie: string): undefined | ResponseCookie {
if (!setCookie) {
return undefined
}
const [[name, value], ...attributes] = parseCookie(setCookie)
const {
domain,
expires,
httponly,
maxage,
path,
samesite,
secure,
partitioned,
priority,
} = Object.fromEntries(
attributes.map(([key, value]) => [key.toLowerCase(), value]),
)
const cookie: ResponseCookie = {
name,
value: decodeURIComponent(value),
domain,
...(expires && { expires: new Date(expires) }),
...(httponly && { httpOnly: true }),
...(typeof maxage === 'string' && { maxAge: Number(maxage) }),
path,
...(samesite && { sameSite: parseSameSite(samesite) }),
...(secure && { secure: true }),
...(priority && { priority: parsePriority(priority) }),
...(partitioned && { partitioned: true }),
}
return compact(cookie)
}
function compact<T>(t: T): T {
const newT = {} as Partial<T>
for (const key in t) {
if (t[key]) {
newT[key] = t[key]
}
}
return newT as T
}
const SAME_SITE: ResponseCookie['sameSite'][] = ['strict', 'lax', 'none']
function parseSameSite(string: string): ResponseCookie['sameSite'] {
string = string.toLowerCase()
return SAME_SITE.includes(string as any)
? (string as ResponseCookie['sameSite'])
: undefined
}
const PRIORITY: ResponseCookie['priority'][] = ['low', 'medium', 'high']
function parsePriority(string: string): ResponseCookie['priority'] {
string = string.toLowerCase()
return PRIORITY.includes(string as any)
? (string as ResponseCookie['priority'])
: undefined
}
/**
* @source https://github.com/nfriedly/set-cookie-parser/blob/master/lib/set-cookie.js
*
* Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas
* that are within a single set-cookie field-value, such as in the Expires portion.
* This is uncommon, but explicitly allowed - see https://tools.ietf.org/html/rfc2616#section-4.2
* Node.js does this for every header *except* set-cookie - see https://github.com/nodejs/node/blob/d5e363b77ebaf1caf67cd7528224b651c86815c1/lib/_http_incoming.js#L128
* React Native's fetch does this for *every* header, including set-cookie.
*
* Based on: https://github.com/google/j2objc/commit/16820fdbc8f76ca0c33472810ce0cb03d20efe25
* Credits to: https://github.com/tomball for original and https://github.com/chrusart for JavaScript implementation
*/
export function splitCookiesString(cookiesString: string) {
if (!cookiesString) return []
var cookiesStrings = []
var pos = 0
var start
var ch
var lastComma
var nextStart
var cookiesSeparatorFound
function skipWhitespace() {
while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) {
pos += 1
}
return pos < cookiesString.length
}
function notSpecialChar() {
ch = cookiesString.charAt(pos)
return ch !== '=' && ch !== ';' && ch !== ','
}
while (pos < cookiesString.length) {
start = pos
cookiesSeparatorFound = false
while (skipWhitespace()) {
ch = cookiesString.charAt(pos)
if (ch === ',') {
// ',' is a cookie separator if we have later first '=', not ';' or ','
lastComma = pos
pos += 1
skipWhitespace()
nextStart = pos
while (pos < cookiesString.length && notSpecialChar()) {
pos += 1
}
// currently special character
if (pos < cookiesString.length && cookiesString.charAt(pos) === '=') {
// we found cookies separator
cookiesSeparatorFound = true
// pos is inside the next cookie, so back up and return it.
pos = nextStart
cookiesStrings.push(cookiesString.substring(start, lastComma))
start = pos
} else {
// in param ',' or param separator ';',
// we continue from that comma
pos = lastComma + 1
}
} else {
pos += 1
}
}
if (!cookiesSeparatorFound || pos >= cookiesString.length) {
cookiesStrings.push(cookiesString.substring(start, cookiesString.length))
}
}
return cookiesStrings
}