@ruiapp/rapid-core 0.5.11 → 0.5.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -7
- package/dist/facilities/cache/CacheFacilityTypes.d.ts +4 -2
- package/dist/facilities/cache/MemoryCache.d.ts +3 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +131 -72
- package/dist/utilities/entityUtility.d.ts +1 -0
- package/dist/utilities/passwordUtility.d.ts +14 -0
- package/package.json +2 -2
- package/rollup.config.js +16 -16
- package/src/bootstrapApplicationConfig.ts +638 -638
- package/src/core/actionHandler.ts +22 -22
- package/src/core/eventManager.ts +20 -20
- package/src/core/facility.ts +7 -7
- package/src/core/http/formDataParser.ts +89 -89
- package/src/core/http-types.ts +4 -4
- package/src/core/pluginManager.ts +175 -175
- package/src/core/providers/runtimeProvider.ts +5 -5
- package/src/core/request.ts +95 -95
- package/src/core/response.ts +79 -79
- package/src/core/routeContext.ts +100 -100
- package/src/core/routesBuilder.ts +88 -88
- package/src/core/server.ts +145 -145
- package/src/dataAccess/columnTypeMapper.ts +22 -22
- package/src/dataAccess/dataAccessTypes.ts +163 -163
- package/src/dataAccess/dataAccessor.ts +135 -135
- package/src/dataAccess/entityManager.ts +1910 -1910
- package/src/dataAccess/entityMapper.ts +100 -100
- package/src/dataAccess/propertyMapper.ts +28 -28
- package/src/deno-std/assert/assert.ts +9 -9
- package/src/deno-std/assert/assertion_error.ts +7 -7
- package/src/deno-std/datetime/to_imf.ts +32 -32
- package/src/deno-std/encoding/base64.ts +141 -141
- package/src/deno-std/http/cookie.ts +372 -372
- package/src/facilities/cache/CacheFacilityTypes.ts +29 -27
- package/src/facilities/cache/CacheFactory.ts +31 -31
- package/src/facilities/cache/MemoryCache.ts +58 -42
- package/src/facilities/cache/MemoryCacheProvider.ts +15 -15
- package/src/facilities/log/LogFacility.ts +35 -35
- package/src/helpers/entityHelpers.ts +76 -76
- package/src/helpers/filterHelper.ts +148 -148
- package/src/helpers/inputHelper.ts +11 -11
- package/src/helpers/metaHelper.ts +104 -104
- package/src/helpers/runCollectionEntityActionHandler.ts +57 -57
- package/src/index.ts +67 -63
- package/src/plugins/auth/AuthPlugin.ts +93 -93
- package/src/plugins/auth/actionHandlers/changePassword.ts +60 -61
- package/src/plugins/auth/actionHandlers/createSession.ts +68 -68
- package/src/plugins/auth/actionHandlers/deleteSession.ts +18 -18
- package/src/plugins/auth/actionHandlers/getMyProfile.ts +35 -35
- package/src/plugins/auth/actionHandlers/index.ts +8 -8
- package/src/plugins/auth/actionHandlers/resetPassword.ts +44 -45
- package/src/plugins/auth/models/AccessToken.ts +56 -56
- package/src/plugins/auth/models/index.ts +3 -3
- package/src/plugins/auth/routes/changePassword.ts +15 -15
- package/src/plugins/auth/routes/getMyProfile.ts +15 -15
- package/src/plugins/auth/routes/index.ts +7 -7
- package/src/plugins/auth/routes/resetPassword.ts +15 -15
- package/src/plugins/auth/routes/signin.ts +15 -15
- package/src/plugins/auth/routes/signout.ts +15 -15
- package/src/plugins/auth/services/AuthService.ts +39 -39
- package/src/plugins/cronJob/CronJobPlugin.ts +112 -112
- package/src/plugins/cronJob/CronJobPluginTypes.ts +49 -49
- package/src/plugins/cronJob/actionHandlers/index.ts +4 -4
- package/src/plugins/cronJob/actionHandlers/runCronJob.ts +29 -29
- package/src/plugins/cronJob/routes/index.ts +3 -3
- package/src/plugins/cronJob/routes/runCronJob.ts +15 -15
- package/src/plugins/dataManage/DataManagePlugin.ts +163 -163
- package/src/plugins/dataManage/actionHandlers/addEntityRelations.ts +15 -15
- package/src/plugins/dataManage/actionHandlers/countCollectionEntities.ts +17 -17
- package/src/plugins/dataManage/actionHandlers/createCollectionEntitiesBatch.ts +81 -81
- package/src/plugins/dataManage/actionHandlers/createCollectionEntity.ts +20 -20
- package/src/plugins/dataManage/actionHandlers/deleteCollectionEntities.ts +45 -45
- package/src/plugins/dataManage/actionHandlers/deleteCollectionEntityById.ts +20 -20
- package/src/plugins/dataManage/actionHandlers/findCollectionEntities.ts +27 -27
- package/src/plugins/dataManage/actionHandlers/findCollectionEntityById.ts +30 -30
- package/src/plugins/dataManage/actionHandlers/queryDatabase.ts +22 -22
- package/src/plugins/dataManage/actionHandlers/removeEntityRelations.ts +15 -15
- package/src/plugins/dataManage/actionHandlers/updateCollectionEntityById.ts +38 -38
- package/src/plugins/entityAccessControl/EntityAccessControlPlugin.ts +146 -146
- package/src/plugins/fileManage/FileManagePlugin.ts +52 -52
- package/src/plugins/fileManage/actionHandlers/downloadDocument.ts +65 -65
- package/src/plugins/fileManage/actionHandlers/downloadFile.ts +44 -44
- package/src/plugins/fileManage/actionHandlers/uploadFile.ts +33 -33
- package/src/plugins/fileManage/routes/downloadDocument.ts +15 -15
- package/src/plugins/fileManage/routes/downloadFile.ts +15 -15
- package/src/plugins/fileManage/routes/index.ts +5 -5
- package/src/plugins/fileManage/routes/uploadFile.ts +15 -15
- package/src/plugins/license/LicensePlugin.ts +79 -79
- package/src/plugins/license/LicensePluginTypes.ts +95 -95
- package/src/plugins/license/LicenseService.ts +118 -118
- package/src/plugins/license/actionHandlers/getLicense.ts +18 -18
- package/src/plugins/license/actionHandlers/index.ts +4 -4
- package/src/plugins/license/helpers/certHelper.ts +21 -21
- package/src/plugins/license/helpers/cryptoHelper.ts +47 -47
- package/src/plugins/license/models/index.ts +1 -1
- package/src/plugins/license/routes/getLicense.ts +15 -15
- package/src/plugins/license/routes/index.ts +3 -3
- package/src/plugins/mail/MailPlugin.ts +74 -74
- package/src/plugins/mail/MailPluginTypes.ts +27 -27
- package/src/plugins/mail/MailService.ts +38 -38
- package/src/plugins/mail/actionHandlers/index.ts +3 -3
- package/src/plugins/mail/models/index.ts +1 -1
- package/src/plugins/mail/routes/index.ts +1 -1
- package/src/plugins/metaManage/MetaManagePlugin.ts +530 -530
- package/src/plugins/metaManage/actionHandlers/getMetaModelDetail.ts +10 -10
- package/src/plugins/metaManage/actionHandlers/listMetaModels.ts +9 -9
- package/src/plugins/metaManage/actionHandlers/listMetaRoutes.ts +9 -9
- package/src/plugins/notification/NotificationPlugin.ts +68 -68
- package/src/plugins/notification/NotificationPluginTypes.ts +13 -13
- package/src/plugins/notification/NotificationService.ts +25 -25
- package/src/plugins/notification/actionHandlers/index.ts +3 -3
- package/src/plugins/notification/models/Notification.ts +60 -60
- package/src/plugins/notification/models/index.ts +3 -3
- package/src/plugins/notification/routes/index.ts +1 -1
- package/src/plugins/routeManage/RouteManagePlugin.ts +62 -62
- package/src/plugins/routeManage/actionHandlers/httpProxy.ts +13 -13
- package/src/plugins/sequence/SequencePlugin.ts +146 -146
- package/src/plugins/sequence/SequencePluginTypes.ts +69 -69
- package/src/plugins/sequence/SequenceService.ts +92 -92
- package/src/plugins/sequence/actionHandlers/generateSn.ts +32 -32
- package/src/plugins/sequence/actionHandlers/index.ts +4 -4
- package/src/plugins/sequence/models/SequenceAutoIncrementRecord.ts +49 -49
- package/src/plugins/sequence/models/SequenceRule.ts +42 -42
- package/src/plugins/sequence/models/index.ts +4 -4
- package/src/plugins/sequence/routes/generateSn.ts +15 -15
- package/src/plugins/sequence/routes/index.ts +3 -3
- package/src/plugins/sequence/segment-utility.ts +11 -11
- package/src/plugins/sequence/segments/autoIncrement.ts +90 -90
- package/src/plugins/sequence/segments/dayOfMonth.ts +19 -19
- package/src/plugins/sequence/segments/index.ts +9 -9
- package/src/plugins/sequence/segments/literal.ts +16 -16
- package/src/plugins/sequence/segments/month.ts +19 -19
- package/src/plugins/sequence/segments/parameter.ts +20 -20
- package/src/plugins/sequence/segments/year.ts +19 -19
- package/src/plugins/serverOperation/ServerOperationPlugin.ts +91 -91
- package/src/plugins/serverOperation/ServerOperationPluginTypes.ts +15 -15
- package/src/plugins/serverOperation/actionHandlers/index.ts +4 -4
- package/src/plugins/serverOperation/actionHandlers/runServerOperation.ts +15 -15
- package/src/plugins/setting/SettingPlugin.ts +68 -68
- package/src/plugins/setting/SettingPluginTypes.ts +37 -37
- package/src/plugins/setting/SettingService.ts +213 -213
- package/src/plugins/setting/actionHandlers/getSystemSettingValues.ts +30 -30
- package/src/plugins/setting/actionHandlers/getUserSettingValues.ts +38 -38
- package/src/plugins/setting/actionHandlers/index.ts +6 -6
- package/src/plugins/setting/actionHandlers/setSystemSettingValues.ts +30 -30
- package/src/plugins/setting/models/SystemSettingGroupSetting.ts +57 -57
- package/src/plugins/setting/models/SystemSettingItem.ts +48 -48
- package/src/plugins/setting/models/SystemSettingItemSetting.ts +73 -73
- package/src/plugins/setting/models/UserSettingGroupSetting.ts +57 -57
- package/src/plugins/setting/models/UserSettingItem.ts +55 -55
- package/src/plugins/setting/models/UserSettingItemSetting.ts +73 -73
- package/src/plugins/setting/models/index.ts +8 -8
- package/src/plugins/setting/routes/getSystemSettingValues.ts +15 -15
- package/src/plugins/setting/routes/getUserSettingValues.ts +15 -15
- package/src/plugins/setting/routes/index.ts +5 -5
- package/src/plugins/setting/routes/setSystemSettingValues.ts +15 -15
- package/src/plugins/stateMachine/StateMachinePlugin.ts +196 -196
- package/src/plugins/stateMachine/StateMachinePluginTypes.ts +48 -48
- package/src/plugins/stateMachine/actionHandlers/index.ts +4 -4
- package/src/plugins/stateMachine/actionHandlers/sendStateMachineEvent.ts +54 -54
- package/src/plugins/stateMachine/models/StateMachine.ts +42 -42
- package/src/plugins/stateMachine/models/index.ts +3 -3
- package/src/plugins/stateMachine/routes/index.ts +3 -3
- package/src/plugins/stateMachine/routes/sendStateMachineEvent.ts +15 -15
- package/src/plugins/stateMachine/stateMachineHelper.ts +36 -36
- package/src/plugins/webhooks/WebhooksPlugin.ts +148 -148
- package/src/plugins/webhooks/pluginConfig.ts +75 -75
- package/src/polyfill.ts +5 -5
- package/src/proxy/mod.ts +38 -38
- package/src/proxy/types.ts +21 -21
- package/src/queryBuilder/index.ts +1 -1
- package/src/queryBuilder/queryBuilder.ts +668 -668
- package/src/server.ts +480 -480
- package/src/types.ts +722 -722
- package/src/utilities/accessControlUtility.ts +33 -33
- package/src/utilities/entityUtility.ts +18 -0
- package/src/utilities/errorUtility.ts +15 -15
- package/src/utilities/fsUtility.ts +61 -61
- package/src/utilities/httpUtility.ts +19 -19
- package/src/utilities/jwtUtility.ts +26 -26
- package/src/utilities/passwordUtility.ts +26 -0
- package/src/utilities/pathUtility.ts +14 -14
- package/src/utilities/timeUtility.ts +9 -9
- package/src/utilities/typeUtility.ts +15 -15
- package/tsconfig.json +19 -19
|
@@ -1,372 +1,372 @@
|
|
|
1
|
-
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
2
|
-
// Structured similarly to Go's cookie.go
|
|
3
|
-
// https://github.com/golang/go/blob/master/src/net/http/cookie.go
|
|
4
|
-
// This module is browser compatible.
|
|
5
|
-
|
|
6
|
-
import { assert } from "../assert/assert";
|
|
7
|
-
import { toIMF } from "../datetime/to_imf";
|
|
8
|
-
|
|
9
|
-
export interface Cookie {
|
|
10
|
-
/** Name of the cookie. */
|
|
11
|
-
name: string;
|
|
12
|
-
/** Value of the cookie. */
|
|
13
|
-
value: string;
|
|
14
|
-
/** The cookie's `Expires` attribute, either as an explicit date or UTC milliseconds.
|
|
15
|
-
* @example <caption>Explicit date:</caption>
|
|
16
|
-
*
|
|
17
|
-
* ```ts
|
|
18
|
-
* import { Cookie } from "https://deno.land/std@$STD_VERSION/http/cookie.ts";
|
|
19
|
-
* const cookie: Cookie = {
|
|
20
|
-
* name: 'name',
|
|
21
|
-
* value: 'value',
|
|
22
|
-
* // expires on Fri Dec 30 2022
|
|
23
|
-
* expires: new Date('2022-12-31')
|
|
24
|
-
* }
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* @example <caption>UTC milliseconds</caption>
|
|
28
|
-
*
|
|
29
|
-
* ```ts
|
|
30
|
-
* import { Cookie } from "https://deno.land/std@$STD_VERSION/http/cookie.ts";
|
|
31
|
-
* const cookie: Cookie = {
|
|
32
|
-
* name: 'name',
|
|
33
|
-
* value: 'value',
|
|
34
|
-
* // expires 10 seconds from now
|
|
35
|
-
* expires: Date.now() + 10000
|
|
36
|
-
* }
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
expires?: Date | number;
|
|
40
|
-
/** The cookie's `Max-Age` attribute, in seconds. Must be a non-negative integer. A cookie with a `maxAge` of `0` expires immediately. */
|
|
41
|
-
maxAge?: number;
|
|
42
|
-
/** The cookie's `Domain` attribute. Specifies those hosts to which the cookie will be sent. */
|
|
43
|
-
domain?: string;
|
|
44
|
-
/** The cookie's `Path` attribute. A cookie with a path will only be included in the `Cookie` request header if the requested URL matches that path. */
|
|
45
|
-
path?: string;
|
|
46
|
-
/** The cookie's `Secure` attribute. If `true`, the cookie will only be included in the `Cookie` request header if the connection uses SSL and HTTPS. */
|
|
47
|
-
secure?: boolean;
|
|
48
|
-
/** The cookie's `HTTPOnly` attribute. If `true`, the cookie cannot be accessed via JavaScript. */
|
|
49
|
-
httpOnly?: boolean;
|
|
50
|
-
/**
|
|
51
|
-
* Allows servers to assert that a cookie ought not to
|
|
52
|
-
* be sent along with cross-site requests.
|
|
53
|
-
*/
|
|
54
|
-
sameSite?: "Strict" | "Lax" | "None";
|
|
55
|
-
/** Additional key value pairs with the form "key=value" */
|
|
56
|
-
unparsed?: string[];
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const FIELD_CONTENT_REGEXP = /^(?=[\x20-\x7E]*$)[^()@<>,;:\\"\[\]?={}\s]+$/;
|
|
60
|
-
|
|
61
|
-
function toString(cookie: Cookie): string {
|
|
62
|
-
if (!cookie.name) {
|
|
63
|
-
return "";
|
|
64
|
-
}
|
|
65
|
-
const out: string[] = [];
|
|
66
|
-
validateName(cookie.name);
|
|
67
|
-
validateValue(cookie.name, cookie.value);
|
|
68
|
-
out.push(`${cookie.name}=${cookie.value}`);
|
|
69
|
-
|
|
70
|
-
// Fallback for invalid Set-Cookie
|
|
71
|
-
// ref: https://tools.ietf.org/html/draft-ietf-httpbis-cookie-prefixes-00#section-3.1
|
|
72
|
-
if (cookie.name.startsWith("__Secure")) {
|
|
73
|
-
cookie.secure = true;
|
|
74
|
-
}
|
|
75
|
-
if (cookie.name.startsWith("__Host")) {
|
|
76
|
-
cookie.path = "/";
|
|
77
|
-
cookie.secure = true;
|
|
78
|
-
delete cookie.domain;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (cookie.secure) {
|
|
82
|
-
out.push("Secure");
|
|
83
|
-
}
|
|
84
|
-
if (cookie.httpOnly) {
|
|
85
|
-
out.push("HttpOnly");
|
|
86
|
-
}
|
|
87
|
-
if (typeof cookie.maxAge === "number" && Number.isInteger(cookie.maxAge)) {
|
|
88
|
-
assert(cookie.maxAge >= 0, "Max-Age must be an integer superior or equal to 0");
|
|
89
|
-
out.push(`Max-Age=${cookie.maxAge}`);
|
|
90
|
-
}
|
|
91
|
-
if (cookie.domain) {
|
|
92
|
-
validateDomain(cookie.domain);
|
|
93
|
-
out.push(`Domain=${cookie.domain}`);
|
|
94
|
-
}
|
|
95
|
-
if (cookie.sameSite) {
|
|
96
|
-
out.push(`SameSite=${cookie.sameSite}`);
|
|
97
|
-
}
|
|
98
|
-
if (cookie.path) {
|
|
99
|
-
validatePath(cookie.path);
|
|
100
|
-
out.push(`Path=${cookie.path}`);
|
|
101
|
-
}
|
|
102
|
-
if (cookie.expires) {
|
|
103
|
-
const { expires } = cookie;
|
|
104
|
-
const dateString = toIMF(typeof expires === "number" ? new Date(expires) : expires);
|
|
105
|
-
out.push(`Expires=${dateString}`);
|
|
106
|
-
}
|
|
107
|
-
if (cookie.unparsed) {
|
|
108
|
-
out.push(cookie.unparsed.join("; "));
|
|
109
|
-
}
|
|
110
|
-
return out.join("; ");
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Validate Cookie Name.
|
|
115
|
-
* @param name Cookie name.
|
|
116
|
-
*/
|
|
117
|
-
function validateName(name: string | undefined | null) {
|
|
118
|
-
if (name && !FIELD_CONTENT_REGEXP.test(name)) {
|
|
119
|
-
throw new TypeError(`Invalid cookie name: "${name}".`);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Validate Path Value.
|
|
125
|
-
* See {@link https://tools.ietf.org/html/rfc6265#section-4.1.2.4}.
|
|
126
|
-
* @param path Path value.
|
|
127
|
-
*/
|
|
128
|
-
function validatePath(path: string | null) {
|
|
129
|
-
if (path == null) {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
for (let i = 0; i < path.length; i++) {
|
|
133
|
-
const c = path.charAt(i);
|
|
134
|
-
if (c < String.fromCharCode(0x20) || c > String.fromCharCode(0x7e) || c == ";") {
|
|
135
|
-
throw new Error(path + ": Invalid cookie path char '" + c + "'");
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Validate Cookie Value.
|
|
142
|
-
* See {@link https://tools.ietf.org/html/rfc6265#section-4.1}.
|
|
143
|
-
* @param value Cookie value.
|
|
144
|
-
*/
|
|
145
|
-
function validateValue(name: string, value: string | null) {
|
|
146
|
-
if (value == null || name == null) return;
|
|
147
|
-
for (let i = 0; i < value.length; i++) {
|
|
148
|
-
const c = value.charAt(i);
|
|
149
|
-
if (
|
|
150
|
-
c < String.fromCharCode(0x21) ||
|
|
151
|
-
c == String.fromCharCode(0x22) ||
|
|
152
|
-
c == String.fromCharCode(0x2c) ||
|
|
153
|
-
c == String.fromCharCode(0x3b) ||
|
|
154
|
-
c == String.fromCharCode(0x5c) ||
|
|
155
|
-
c == String.fromCharCode(0x7f)
|
|
156
|
-
) {
|
|
157
|
-
throw new Error("RFC2616 cookie '" + name + "' cannot contain character '" + c + "'");
|
|
158
|
-
}
|
|
159
|
-
if (c > String.fromCharCode(0x80)) {
|
|
160
|
-
throw new Error("RFC2616 cookie '" + name + "' can only have US-ASCII chars as value" + c.charCodeAt(0).toString(16));
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Validate Cookie Domain.
|
|
167
|
-
* See {@link https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.2.3}.
|
|
168
|
-
* @param domain Cookie domain.
|
|
169
|
-
*/
|
|
170
|
-
function validateDomain(domain: string) {
|
|
171
|
-
if (domain == null) {
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
const char1 = domain.charAt(0);
|
|
175
|
-
const charN = domain.charAt(domain.length - 1);
|
|
176
|
-
if (char1 == "-" || charN == "." || charN == "-") {
|
|
177
|
-
throw new Error("Invalid first/last char in cookie domain: " + domain);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Parse cookies of a header
|
|
183
|
-
*
|
|
184
|
-
* @example
|
|
185
|
-
* ```ts
|
|
186
|
-
* import { getCookies } from "https://deno.land/std@$STD_VERSION/http/cookie.ts";
|
|
187
|
-
*
|
|
188
|
-
* const headers = new Headers();
|
|
189
|
-
* headers.set("Cookie", "full=of; tasty=chocolate");
|
|
190
|
-
*
|
|
191
|
-
* const cookies = getCookies(headers);
|
|
192
|
-
* console.log(cookies); // { full: "of", tasty: "chocolate" }
|
|
193
|
-
* ```
|
|
194
|
-
*
|
|
195
|
-
* @param headers The headers instance to get cookies from
|
|
196
|
-
* @return Object with cookie names as keys
|
|
197
|
-
*/
|
|
198
|
-
export function getCookies(headers: Headers): Record<string, string> {
|
|
199
|
-
const cookie = headers.get("Cookie");
|
|
200
|
-
if (cookie != null) {
|
|
201
|
-
const out: Record<string, string> = {};
|
|
202
|
-
const c = cookie.split(";");
|
|
203
|
-
for (const kv of c) {
|
|
204
|
-
const [cookieKey, ...cookieVal] = kv.split("=");
|
|
205
|
-
assert(cookieKey != null);
|
|
206
|
-
const key = cookieKey.trim();
|
|
207
|
-
out[key] = cookieVal.join("=");
|
|
208
|
-
}
|
|
209
|
-
return out;
|
|
210
|
-
}
|
|
211
|
-
return {};
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* Set the cookie header properly in the headers
|
|
216
|
-
*
|
|
217
|
-
* @example
|
|
218
|
-
* ```ts
|
|
219
|
-
* import {
|
|
220
|
-
* Cookie,
|
|
221
|
-
* setCookie,
|
|
222
|
-
* } from "https://deno.land/std@$STD_VERSION/http/cookie.ts";
|
|
223
|
-
*
|
|
224
|
-
* const headers = new Headers();
|
|
225
|
-
* const cookie: Cookie = { name: "Space", value: "Cat" };
|
|
226
|
-
* setCookie(headers, cookie);
|
|
227
|
-
*
|
|
228
|
-
* const cookieHeader = headers.get("set-cookie");
|
|
229
|
-
* console.log(cookieHeader); // Space=Cat
|
|
230
|
-
* ```
|
|
231
|
-
*
|
|
232
|
-
* @param headers The headers instance to set the cookie to
|
|
233
|
-
* @param cookie Cookie to set
|
|
234
|
-
*/
|
|
235
|
-
export function setCookie(headers: Headers, cookie: Cookie) {
|
|
236
|
-
// Parsing cookie headers to make consistent set-cookie header
|
|
237
|
-
// ref: https://tools.ietf.org/html/rfc6265#section-4.1.1
|
|
238
|
-
const v = toString(cookie);
|
|
239
|
-
if (v) {
|
|
240
|
-
headers.append("Set-Cookie", v);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Set the cookie header with empty value in the headers to delete it
|
|
246
|
-
*
|
|
247
|
-
* > Note: Deleting a `Cookie` will set its expiration date before now. Forcing
|
|
248
|
-
* > the browser to delete it.
|
|
249
|
-
*
|
|
250
|
-
* @example
|
|
251
|
-
* ```ts
|
|
252
|
-
* import { deleteCookie } from "https://deno.land/std@$STD_VERSION/http/cookie.ts";
|
|
253
|
-
*
|
|
254
|
-
* const headers = new Headers();
|
|
255
|
-
* deleteCookie(headers, "deno");
|
|
256
|
-
*
|
|
257
|
-
* const cookieHeader = headers.get("set-cookie");
|
|
258
|
-
* console.log(cookieHeader); // deno=; Expires=Thus, 01 Jan 1970 00:00:00 GMT
|
|
259
|
-
* ```
|
|
260
|
-
*
|
|
261
|
-
* @param headers The headers instance to delete the cookie from
|
|
262
|
-
* @param name Name of cookie
|
|
263
|
-
* @param attributes Additional cookie attributes
|
|
264
|
-
*/
|
|
265
|
-
export function deleteCookie(headers: Headers, name: string, attributes?: { path?: string; domain?: string }) {
|
|
266
|
-
setCookie(headers, {
|
|
267
|
-
name: name,
|
|
268
|
-
value: "",
|
|
269
|
-
expires: new Date(0),
|
|
270
|
-
...attributes,
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
function parseSetCookie(value: string): Cookie | null {
|
|
275
|
-
const attrs = value.split(";").map((attr) => {
|
|
276
|
-
const [key, ...values] = attr.trim().split("=");
|
|
277
|
-
return [key, values.join("=")];
|
|
278
|
-
});
|
|
279
|
-
const cookie: Cookie = {
|
|
280
|
-
name: attrs[0][0],
|
|
281
|
-
value: attrs[0][1],
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
for (const [key, value] of attrs.slice(1)) {
|
|
285
|
-
switch (key.toLocaleLowerCase()) {
|
|
286
|
-
case "expires":
|
|
287
|
-
cookie.expires = new Date(value);
|
|
288
|
-
break;
|
|
289
|
-
case "max-age":
|
|
290
|
-
cookie.maxAge = Number(value);
|
|
291
|
-
if (cookie.maxAge < 0) {
|
|
292
|
-
console.warn("Max-Age must be an integer superior or equal to 0. Cookie ignored.");
|
|
293
|
-
return null;
|
|
294
|
-
}
|
|
295
|
-
break;
|
|
296
|
-
case "domain":
|
|
297
|
-
cookie.domain = value;
|
|
298
|
-
break;
|
|
299
|
-
case "path":
|
|
300
|
-
cookie.path = value;
|
|
301
|
-
break;
|
|
302
|
-
case "secure":
|
|
303
|
-
cookie.secure = true;
|
|
304
|
-
break;
|
|
305
|
-
case "httponly":
|
|
306
|
-
cookie.httpOnly = true;
|
|
307
|
-
break;
|
|
308
|
-
case "samesite":
|
|
309
|
-
cookie.sameSite = value as Cookie["sameSite"];
|
|
310
|
-
break;
|
|
311
|
-
default:
|
|
312
|
-
if (!Array.isArray(cookie.unparsed)) {
|
|
313
|
-
cookie.unparsed = [];
|
|
314
|
-
}
|
|
315
|
-
cookie.unparsed.push([key, value].join("="));
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
if (cookie.name.startsWith("__Secure-")) {
|
|
319
|
-
/** This requirement is mentioned in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie but not the RFC. */
|
|
320
|
-
if (!cookie.secure) {
|
|
321
|
-
console.warn("Cookies with names starting with `__Secure-` must be set with the secure flag. Cookie ignored.");
|
|
322
|
-
return null;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
if (cookie.name.startsWith("__Host-")) {
|
|
326
|
-
if (!cookie.secure) {
|
|
327
|
-
console.warn("Cookies with names starting with `__Host-` must be set with the secure flag. Cookie ignored.");
|
|
328
|
-
return null;
|
|
329
|
-
}
|
|
330
|
-
if (cookie.domain !== undefined) {
|
|
331
|
-
console.warn("Cookies with names starting with `__Host-` must not have a domain specified. Cookie ignored.");
|
|
332
|
-
return null;
|
|
333
|
-
}
|
|
334
|
-
if (cookie.path !== "/") {
|
|
335
|
-
console.warn("Cookies with names starting with `__Host-` must have path be `/`. Cookie has been ignored.");
|
|
336
|
-
return null;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
return cookie;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* Parse set-cookies of a header
|
|
344
|
-
*
|
|
345
|
-
* @example
|
|
346
|
-
* ```ts
|
|
347
|
-
* import { getSetCookies } from "https://deno.land/std@$STD_VERSION/http/cookie.ts";
|
|
348
|
-
*
|
|
349
|
-
* const headers = new Headers([
|
|
350
|
-
* ["Set-Cookie", "lulu=meow; Secure; Max-Age=3600"],
|
|
351
|
-
* ["Set-Cookie", "booya=kasha; HttpOnly; Path=/"],
|
|
352
|
-
* ]);
|
|
353
|
-
*
|
|
354
|
-
* const cookies = getSetCookies(headers);
|
|
355
|
-
* console.log(cookies); // [{ name: "lulu", value: "meow", secure: true, maxAge: 3600 }, { name: "booya", value: "kahsa", httpOnly: true, path: "/ }]
|
|
356
|
-
* ```
|
|
357
|
-
*
|
|
358
|
-
* @param headers The headers instance to get set-cookies from
|
|
359
|
-
* @return List of cookies
|
|
360
|
-
*/
|
|
361
|
-
export function getSetCookies(headers: Headers): Cookie[] {
|
|
362
|
-
// TODO(lino-levan): remove this ts-ignore when Typescript 5.2 lands in Deno
|
|
363
|
-
// @ts-ignore Typescript's TS Dom types will be out of date until 5.2
|
|
364
|
-
return (
|
|
365
|
-
(headers as any)
|
|
366
|
-
.getSetCookie()
|
|
367
|
-
/** Parse each `set-cookie` header separately */
|
|
368
|
-
.map(parseSetCookie)
|
|
369
|
-
/** Skip empty cookies */
|
|
370
|
-
.filter(Boolean) as Cookie[]
|
|
371
|
-
);
|
|
372
|
-
}
|
|
1
|
+
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
2
|
+
// Structured similarly to Go's cookie.go
|
|
3
|
+
// https://github.com/golang/go/blob/master/src/net/http/cookie.go
|
|
4
|
+
// This module is browser compatible.
|
|
5
|
+
|
|
6
|
+
import { assert } from "../assert/assert";
|
|
7
|
+
import { toIMF } from "../datetime/to_imf";
|
|
8
|
+
|
|
9
|
+
export interface Cookie {
|
|
10
|
+
/** Name of the cookie. */
|
|
11
|
+
name: string;
|
|
12
|
+
/** Value of the cookie. */
|
|
13
|
+
value: string;
|
|
14
|
+
/** The cookie's `Expires` attribute, either as an explicit date or UTC milliseconds.
|
|
15
|
+
* @example <caption>Explicit date:</caption>
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { Cookie } from "https://deno.land/std@$STD_VERSION/http/cookie.ts";
|
|
19
|
+
* const cookie: Cookie = {
|
|
20
|
+
* name: 'name',
|
|
21
|
+
* value: 'value',
|
|
22
|
+
* // expires on Fri Dec 30 2022
|
|
23
|
+
* expires: new Date('2022-12-31')
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @example <caption>UTC milliseconds</caption>
|
|
28
|
+
*
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { Cookie } from "https://deno.land/std@$STD_VERSION/http/cookie.ts";
|
|
31
|
+
* const cookie: Cookie = {
|
|
32
|
+
* name: 'name',
|
|
33
|
+
* value: 'value',
|
|
34
|
+
* // expires 10 seconds from now
|
|
35
|
+
* expires: Date.now() + 10000
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
expires?: Date | number;
|
|
40
|
+
/** The cookie's `Max-Age` attribute, in seconds. Must be a non-negative integer. A cookie with a `maxAge` of `0` expires immediately. */
|
|
41
|
+
maxAge?: number;
|
|
42
|
+
/** The cookie's `Domain` attribute. Specifies those hosts to which the cookie will be sent. */
|
|
43
|
+
domain?: string;
|
|
44
|
+
/** The cookie's `Path` attribute. A cookie with a path will only be included in the `Cookie` request header if the requested URL matches that path. */
|
|
45
|
+
path?: string;
|
|
46
|
+
/** The cookie's `Secure` attribute. If `true`, the cookie will only be included in the `Cookie` request header if the connection uses SSL and HTTPS. */
|
|
47
|
+
secure?: boolean;
|
|
48
|
+
/** The cookie's `HTTPOnly` attribute. If `true`, the cookie cannot be accessed via JavaScript. */
|
|
49
|
+
httpOnly?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Allows servers to assert that a cookie ought not to
|
|
52
|
+
* be sent along with cross-site requests.
|
|
53
|
+
*/
|
|
54
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
55
|
+
/** Additional key value pairs with the form "key=value" */
|
|
56
|
+
unparsed?: string[];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const FIELD_CONTENT_REGEXP = /^(?=[\x20-\x7E]*$)[^()@<>,;:\\"\[\]?={}\s]+$/;
|
|
60
|
+
|
|
61
|
+
function toString(cookie: Cookie): string {
|
|
62
|
+
if (!cookie.name) {
|
|
63
|
+
return "";
|
|
64
|
+
}
|
|
65
|
+
const out: string[] = [];
|
|
66
|
+
validateName(cookie.name);
|
|
67
|
+
validateValue(cookie.name, cookie.value);
|
|
68
|
+
out.push(`${cookie.name}=${cookie.value}`);
|
|
69
|
+
|
|
70
|
+
// Fallback for invalid Set-Cookie
|
|
71
|
+
// ref: https://tools.ietf.org/html/draft-ietf-httpbis-cookie-prefixes-00#section-3.1
|
|
72
|
+
if (cookie.name.startsWith("__Secure")) {
|
|
73
|
+
cookie.secure = true;
|
|
74
|
+
}
|
|
75
|
+
if (cookie.name.startsWith("__Host")) {
|
|
76
|
+
cookie.path = "/";
|
|
77
|
+
cookie.secure = true;
|
|
78
|
+
delete cookie.domain;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (cookie.secure) {
|
|
82
|
+
out.push("Secure");
|
|
83
|
+
}
|
|
84
|
+
if (cookie.httpOnly) {
|
|
85
|
+
out.push("HttpOnly");
|
|
86
|
+
}
|
|
87
|
+
if (typeof cookie.maxAge === "number" && Number.isInteger(cookie.maxAge)) {
|
|
88
|
+
assert(cookie.maxAge >= 0, "Max-Age must be an integer superior or equal to 0");
|
|
89
|
+
out.push(`Max-Age=${cookie.maxAge}`);
|
|
90
|
+
}
|
|
91
|
+
if (cookie.domain) {
|
|
92
|
+
validateDomain(cookie.domain);
|
|
93
|
+
out.push(`Domain=${cookie.domain}`);
|
|
94
|
+
}
|
|
95
|
+
if (cookie.sameSite) {
|
|
96
|
+
out.push(`SameSite=${cookie.sameSite}`);
|
|
97
|
+
}
|
|
98
|
+
if (cookie.path) {
|
|
99
|
+
validatePath(cookie.path);
|
|
100
|
+
out.push(`Path=${cookie.path}`);
|
|
101
|
+
}
|
|
102
|
+
if (cookie.expires) {
|
|
103
|
+
const { expires } = cookie;
|
|
104
|
+
const dateString = toIMF(typeof expires === "number" ? new Date(expires) : expires);
|
|
105
|
+
out.push(`Expires=${dateString}`);
|
|
106
|
+
}
|
|
107
|
+
if (cookie.unparsed) {
|
|
108
|
+
out.push(cookie.unparsed.join("; "));
|
|
109
|
+
}
|
|
110
|
+
return out.join("; ");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Validate Cookie Name.
|
|
115
|
+
* @param name Cookie name.
|
|
116
|
+
*/
|
|
117
|
+
function validateName(name: string | undefined | null) {
|
|
118
|
+
if (name && !FIELD_CONTENT_REGEXP.test(name)) {
|
|
119
|
+
throw new TypeError(`Invalid cookie name: "${name}".`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Validate Path Value.
|
|
125
|
+
* See {@link https://tools.ietf.org/html/rfc6265#section-4.1.2.4}.
|
|
126
|
+
* @param path Path value.
|
|
127
|
+
*/
|
|
128
|
+
function validatePath(path: string | null) {
|
|
129
|
+
if (path == null) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
for (let i = 0; i < path.length; i++) {
|
|
133
|
+
const c = path.charAt(i);
|
|
134
|
+
if (c < String.fromCharCode(0x20) || c > String.fromCharCode(0x7e) || c == ";") {
|
|
135
|
+
throw new Error(path + ": Invalid cookie path char '" + c + "'");
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Validate Cookie Value.
|
|
142
|
+
* See {@link https://tools.ietf.org/html/rfc6265#section-4.1}.
|
|
143
|
+
* @param value Cookie value.
|
|
144
|
+
*/
|
|
145
|
+
function validateValue(name: string, value: string | null) {
|
|
146
|
+
if (value == null || name == null) return;
|
|
147
|
+
for (let i = 0; i < value.length; i++) {
|
|
148
|
+
const c = value.charAt(i);
|
|
149
|
+
if (
|
|
150
|
+
c < String.fromCharCode(0x21) ||
|
|
151
|
+
c == String.fromCharCode(0x22) ||
|
|
152
|
+
c == String.fromCharCode(0x2c) ||
|
|
153
|
+
c == String.fromCharCode(0x3b) ||
|
|
154
|
+
c == String.fromCharCode(0x5c) ||
|
|
155
|
+
c == String.fromCharCode(0x7f)
|
|
156
|
+
) {
|
|
157
|
+
throw new Error("RFC2616 cookie '" + name + "' cannot contain character '" + c + "'");
|
|
158
|
+
}
|
|
159
|
+
if (c > String.fromCharCode(0x80)) {
|
|
160
|
+
throw new Error("RFC2616 cookie '" + name + "' can only have US-ASCII chars as value" + c.charCodeAt(0).toString(16));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Validate Cookie Domain.
|
|
167
|
+
* See {@link https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.2.3}.
|
|
168
|
+
* @param domain Cookie domain.
|
|
169
|
+
*/
|
|
170
|
+
function validateDomain(domain: string) {
|
|
171
|
+
if (domain == null) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const char1 = domain.charAt(0);
|
|
175
|
+
const charN = domain.charAt(domain.length - 1);
|
|
176
|
+
if (char1 == "-" || charN == "." || charN == "-") {
|
|
177
|
+
throw new Error("Invalid first/last char in cookie domain: " + domain);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Parse cookies of a header
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```ts
|
|
186
|
+
* import { getCookies } from "https://deno.land/std@$STD_VERSION/http/cookie.ts";
|
|
187
|
+
*
|
|
188
|
+
* const headers = new Headers();
|
|
189
|
+
* headers.set("Cookie", "full=of; tasty=chocolate");
|
|
190
|
+
*
|
|
191
|
+
* const cookies = getCookies(headers);
|
|
192
|
+
* console.log(cookies); // { full: "of", tasty: "chocolate" }
|
|
193
|
+
* ```
|
|
194
|
+
*
|
|
195
|
+
* @param headers The headers instance to get cookies from
|
|
196
|
+
* @return Object with cookie names as keys
|
|
197
|
+
*/
|
|
198
|
+
export function getCookies(headers: Headers): Record<string, string> {
|
|
199
|
+
const cookie = headers.get("Cookie");
|
|
200
|
+
if (cookie != null) {
|
|
201
|
+
const out: Record<string, string> = {};
|
|
202
|
+
const c = cookie.split(";");
|
|
203
|
+
for (const kv of c) {
|
|
204
|
+
const [cookieKey, ...cookieVal] = kv.split("=");
|
|
205
|
+
assert(cookieKey != null);
|
|
206
|
+
const key = cookieKey.trim();
|
|
207
|
+
out[key] = cookieVal.join("=");
|
|
208
|
+
}
|
|
209
|
+
return out;
|
|
210
|
+
}
|
|
211
|
+
return {};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Set the cookie header properly in the headers
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* import {
|
|
220
|
+
* Cookie,
|
|
221
|
+
* setCookie,
|
|
222
|
+
* } from "https://deno.land/std@$STD_VERSION/http/cookie.ts";
|
|
223
|
+
*
|
|
224
|
+
* const headers = new Headers();
|
|
225
|
+
* const cookie: Cookie = { name: "Space", value: "Cat" };
|
|
226
|
+
* setCookie(headers, cookie);
|
|
227
|
+
*
|
|
228
|
+
* const cookieHeader = headers.get("set-cookie");
|
|
229
|
+
* console.log(cookieHeader); // Space=Cat
|
|
230
|
+
* ```
|
|
231
|
+
*
|
|
232
|
+
* @param headers The headers instance to set the cookie to
|
|
233
|
+
* @param cookie Cookie to set
|
|
234
|
+
*/
|
|
235
|
+
export function setCookie(headers: Headers, cookie: Cookie) {
|
|
236
|
+
// Parsing cookie headers to make consistent set-cookie header
|
|
237
|
+
// ref: https://tools.ietf.org/html/rfc6265#section-4.1.1
|
|
238
|
+
const v = toString(cookie);
|
|
239
|
+
if (v) {
|
|
240
|
+
headers.append("Set-Cookie", v);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Set the cookie header with empty value in the headers to delete it
|
|
246
|
+
*
|
|
247
|
+
* > Note: Deleting a `Cookie` will set its expiration date before now. Forcing
|
|
248
|
+
* > the browser to delete it.
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```ts
|
|
252
|
+
* import { deleteCookie } from "https://deno.land/std@$STD_VERSION/http/cookie.ts";
|
|
253
|
+
*
|
|
254
|
+
* const headers = new Headers();
|
|
255
|
+
* deleteCookie(headers, "deno");
|
|
256
|
+
*
|
|
257
|
+
* const cookieHeader = headers.get("set-cookie");
|
|
258
|
+
* console.log(cookieHeader); // deno=; Expires=Thus, 01 Jan 1970 00:00:00 GMT
|
|
259
|
+
* ```
|
|
260
|
+
*
|
|
261
|
+
* @param headers The headers instance to delete the cookie from
|
|
262
|
+
* @param name Name of cookie
|
|
263
|
+
* @param attributes Additional cookie attributes
|
|
264
|
+
*/
|
|
265
|
+
export function deleteCookie(headers: Headers, name: string, attributes?: { path?: string; domain?: string }) {
|
|
266
|
+
setCookie(headers, {
|
|
267
|
+
name: name,
|
|
268
|
+
value: "",
|
|
269
|
+
expires: new Date(0),
|
|
270
|
+
...attributes,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function parseSetCookie(value: string): Cookie | null {
|
|
275
|
+
const attrs = value.split(";").map((attr) => {
|
|
276
|
+
const [key, ...values] = attr.trim().split("=");
|
|
277
|
+
return [key, values.join("=")];
|
|
278
|
+
});
|
|
279
|
+
const cookie: Cookie = {
|
|
280
|
+
name: attrs[0][0],
|
|
281
|
+
value: attrs[0][1],
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
for (const [key, value] of attrs.slice(1)) {
|
|
285
|
+
switch (key.toLocaleLowerCase()) {
|
|
286
|
+
case "expires":
|
|
287
|
+
cookie.expires = new Date(value);
|
|
288
|
+
break;
|
|
289
|
+
case "max-age":
|
|
290
|
+
cookie.maxAge = Number(value);
|
|
291
|
+
if (cookie.maxAge < 0) {
|
|
292
|
+
console.warn("Max-Age must be an integer superior or equal to 0. Cookie ignored.");
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
break;
|
|
296
|
+
case "domain":
|
|
297
|
+
cookie.domain = value;
|
|
298
|
+
break;
|
|
299
|
+
case "path":
|
|
300
|
+
cookie.path = value;
|
|
301
|
+
break;
|
|
302
|
+
case "secure":
|
|
303
|
+
cookie.secure = true;
|
|
304
|
+
break;
|
|
305
|
+
case "httponly":
|
|
306
|
+
cookie.httpOnly = true;
|
|
307
|
+
break;
|
|
308
|
+
case "samesite":
|
|
309
|
+
cookie.sameSite = value as Cookie["sameSite"];
|
|
310
|
+
break;
|
|
311
|
+
default:
|
|
312
|
+
if (!Array.isArray(cookie.unparsed)) {
|
|
313
|
+
cookie.unparsed = [];
|
|
314
|
+
}
|
|
315
|
+
cookie.unparsed.push([key, value].join("="));
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
if (cookie.name.startsWith("__Secure-")) {
|
|
319
|
+
/** This requirement is mentioned in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie but not the RFC. */
|
|
320
|
+
if (!cookie.secure) {
|
|
321
|
+
console.warn("Cookies with names starting with `__Secure-` must be set with the secure flag. Cookie ignored.");
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
if (cookie.name.startsWith("__Host-")) {
|
|
326
|
+
if (!cookie.secure) {
|
|
327
|
+
console.warn("Cookies with names starting with `__Host-` must be set with the secure flag. Cookie ignored.");
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
if (cookie.domain !== undefined) {
|
|
331
|
+
console.warn("Cookies with names starting with `__Host-` must not have a domain specified. Cookie ignored.");
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
if (cookie.path !== "/") {
|
|
335
|
+
console.warn("Cookies with names starting with `__Host-` must have path be `/`. Cookie has been ignored.");
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return cookie;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Parse set-cookies of a header
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```ts
|
|
347
|
+
* import { getSetCookies } from "https://deno.land/std@$STD_VERSION/http/cookie.ts";
|
|
348
|
+
*
|
|
349
|
+
* const headers = new Headers([
|
|
350
|
+
* ["Set-Cookie", "lulu=meow; Secure; Max-Age=3600"],
|
|
351
|
+
* ["Set-Cookie", "booya=kasha; HttpOnly; Path=/"],
|
|
352
|
+
* ]);
|
|
353
|
+
*
|
|
354
|
+
* const cookies = getSetCookies(headers);
|
|
355
|
+
* console.log(cookies); // [{ name: "lulu", value: "meow", secure: true, maxAge: 3600 }, { name: "booya", value: "kahsa", httpOnly: true, path: "/ }]
|
|
356
|
+
* ```
|
|
357
|
+
*
|
|
358
|
+
* @param headers The headers instance to get set-cookies from
|
|
359
|
+
* @return List of cookies
|
|
360
|
+
*/
|
|
361
|
+
export function getSetCookies(headers: Headers): Cookie[] {
|
|
362
|
+
// TODO(lino-levan): remove this ts-ignore when Typescript 5.2 lands in Deno
|
|
363
|
+
// @ts-ignore Typescript's TS Dom types will be out of date until 5.2
|
|
364
|
+
return (
|
|
365
|
+
(headers as any)
|
|
366
|
+
.getSetCookie()
|
|
367
|
+
/** Parse each `set-cookie` header separately */
|
|
368
|
+
.map(parseSetCookie)
|
|
369
|
+
/** Skip empty cookies */
|
|
370
|
+
.filter(Boolean) as Cookie[]
|
|
371
|
+
);
|
|
372
|
+
}
|