-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
cookie.ts
129 lines (122 loc) · 3.35 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.
import { toIMF } from "./vendor/https/deno.land/std/datetime/mod.ts";
export interface SetCookieOpts {
expires?: Date;
maxAge?: number;
domain?: string;
path?: string;
secure?: boolean;
httpOnly?: boolean;
sameSite?: "Strict" | "Lax" | "None";
}
export interface Cookie extends SetCookieOpts {
name: string;
value: string;
}
export function parseCookie(header: string): Map<string, string> {
const query = decodeURIComponent(header)
.split(";")
.map((v) => v.trim())
.join("&");
return new Map(new URLSearchParams(query).entries());
}
export function parseSetCookie(header: string): Cookie {
const m = header.match(/^(.+?)=(.+?);(.+?)$/);
if (!m) {
throw new Error("invalid cookie header");
}
const [_, name, value, optStr] = m;
const optMap = new Headers(
optStr.split(";").map((i) => {
const [k, v] = i.trim().split("=");
return [k, v] as [string, string];
}),
);
const domain = optMap.get("Domain") || undefined;
const path = optMap.get("Path") || undefined;
const secure = optMap.has("Secure") || undefined;
const httpOnly = optMap.has("HttpOnly") || undefined;
const sameSite = optMap.get("SameSite") || undefined;
let expires: Date | undefined;
if (optMap.has("Expires")) {
const e = optMap.get("Expires")!;
expires = new Date(e);
}
let maxAge: number | undefined;
if (optMap.has("Max-Age")) {
const m = optMap.get("Max-Age")!;
maxAge = parseInt(m);
}
if (
typeof sameSite === "string" &&
sameSite !== "Lax" &&
sameSite !== "Strict"
) {
throw new Error("sameSite is invalid");
}
return {
name,
value,
expires,
maxAge,
domain,
path,
secure,
httpOnly,
sameSite,
};
}
export function cookieToString(
name: string,
value: string,
opts: SetCookieOpts = {},
) {
const out: string[] = [];
out.push(`${encodeURIComponent(name)}=${encodeURIComponent(value)}`);
if (opts.expires != null) {
out.push("Expires=" + toIMF(opts.expires));
}
if (opts.maxAge != null) {
if (!Number.isInteger(opts.maxAge) || opts.maxAge <= 0) {
throw new TypeError("maxAge must be integer and > 0");
}
out.push("Max-Age=" + opts.maxAge);
}
if (opts.domain != null) {
out.push("Domain=" + opts.domain);
}
if (opts.path != null) {
out.push("Path=" + opts.path);
}
if (opts.secure != null) {
out.push("Secure");
}
if (opts.httpOnly != null) {
out.push("HttpOnly");
}
if (opts.sameSite != null) {
out.push("SameSite=" + opts.sameSite);
}
return out.join("; ");
}
export interface CookieSetter {
setCookie(name: string, value: string, opts?: SetCookieOpts): void;
clearCookie(name: string, opts?: { path?: string }): void;
}
export function cookieSetter(responseHeaders: Headers): CookieSetter {
function clearCookie(name: string, opts: { path?: string } = {}) {
const out: string[] = [];
out.push(`${name}=`);
if (opts.path) {
out.push("Path=" + opts.path);
}
// Set past date
out.push("Expires=" + toIMF(new Date(0)));
const v = out.join("; ");
responseHeaders.append("Set-Cookie", v);
}
function setCookie(name: string, value: string, opts: SetCookieOpts = {}) {
responseHeaders.append("Set-Cookie", cookieToString(name, value, opts));
}
return { setCookie, clearCookie };
}