-
Notifications
You must be signed in to change notification settings - Fork 1
/
cookie_map.ts
833 lines (765 loc) · 25 KB
/
cookie_map.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
// Copyright 2018-2024 the oak authors. All rights reserved. MIT license.
/**
* Provides a iterable map interfaces for managing cookies server side.
*
* @example
* To access the keys in a request and have any set keys available for creating
* a response:
*
* ```ts
* import { CookieMap, mergeHeaders } from "jsr:@oak/commons/cookie_map";
*
* const request = new Request("https://localhost/", {
* headers: { "cookie": "foo=bar; bar=baz;"}
* });
*
* const cookies = new CookieMap(request, { secure: true });
* console.log(cookies.get("foo")); // logs "bar"
* cookies.set("session", "1234567", { secure: true });
*
* const response = new Response("hello", {
* headers: mergeHeaders({
* "content-type": "text/plain",
* }, cookies),
* });
* ```
*
* @example
* To have automatic management of cryptographically signed cookies, you can use
* the {@linkcode SecureCookieMap} instead of {@linkcode CookieMap}. The biggest
* difference is that the methods operate async in order to be able to support
* async signing and validation of cookies:
*
* ```ts
* import {
* SecureCookieMap,
* mergeHeaders,
* type KeyRing,
* } from "jsr:@oak/commons/cookie_map";
*
* const request = new Request("https://localhost/", {
* headers: { "cookie": "foo=bar; bar=baz;"}
* });
*
* // The keys must implement the `KeyRing` interface.
* declare const keys: KeyRing;
*
* const cookies = new SecureCookieMap(request, { keys, secure: true });
* console.log(await cookies.get("foo")); // logs "bar"
* // the cookie will be automatically signed using the supplied key ring.
* await cookies.set("session", "1234567");
*
* const response = new Response("hello", {
* headers: mergeHeaders({
* "content-type": "text/plain",
* }, cookies),
* });
* ```
*
* In addition, if you have a {@linkcode Response} or {@linkcode Headers} for a
* response at construction of the cookies object, they can be passed and any
* set cookies will be added directly to those headers:
*
* ```ts
* import { CookieMap } from "jsr:@oak/commons/cookie_map";
*
* const request = new Request("https://localhost/", {
* headers: { "cookie": "foo=bar; bar=baz;"}
* });
*
* const response = new Response("hello", {
* headers: { "content-type": "text/plain" },
* });
*
* const cookies = new CookieMap(request, { response });
* console.log(cookies.get("foo")); // logs "bar"
* cookies.set("session", "1234567");
* ```
*
* @module
*/
export interface CookieMapOptions {
/** The {@linkcode Response} or the headers that will be used with the
* response. When provided, `Set-Cookie` headers will be set in the headers
* when cookies are set or deleted in the map.
*
* An alternative way to extract the headers is to pass the cookie map to the
* {@linkcode mergeHeaders} function to merge various sources of the
* headers to be provided when creating or updating a response.
*/
response?: Headered | Headers;
/** A flag that indicates if the request and response are being handled over
* a secure (e.g. HTTPS/TLS) connection.
*
* @default {false}
*/
secure?: boolean;
}
export interface CookieMapSetDeleteOptions {
/** The domain to scope the cookie for. */
domain?: string;
/** When the cookie expires. */
expires?: Date;
/** Number of seconds until the cookie expires */
maxAge?: number;
/** A flag that indicates if the cookie is valid over HTTP only. */
httpOnly?: boolean;
/** Do not error when signing and validating cookies over an insecure
* connection. */
ignoreInsecure?: boolean;
/** Overwrite an existing value. */
overwrite?: boolean;
/** The path the cookie is valid for. */
path?: string;
/** Override the flag that was set when the instance was created. */
secure?: boolean;
/** Set the same-site indicator for a cookie. */
sameSite?: "strict" | "lax" | "none" | boolean;
}
/**
* An object which contains a `headers` property which has a value of an
* instance of {@linkcode Headers}, like {@linkcode Request} and
* {@linkcode Response}.
*/
export interface Headered {
headers: Headers;
}
/**
* An object which contains a symbol which indicates that it can be merged with
* other headers.
*/
export interface Mergeable {
[cookieMapHeadersInitSymbol](): [string, string][];
}
/** Options which can be set when initializing a {@linkcode SecureCookieMap}. */
export interface SecureCookieMapOptions {
/** Keys which will be used to validate and sign cookies. The key ring should
* implement the {@linkcode KeyRing} interface. */
keys?: KeyRing;
/** The {@linkcode Response} or the headers that will be used with the
* response. When provided, `Set-Cookie` headers will be set in the headers
* when cookies are set or deleted in the map.
*
* An alternative way to extract the headers is to pass the cookie map to the
* {@linkcode mergeHeaders} function to merge various sources of the
* headers to be provided when creating or updating a response.
*/
response?: Headered | Headers;
/** A flag that indicates if the request and response are being handled over
* a secure (e.g. HTTPS/TLS) connection.
*
* @default {false}
*/
secure?: boolean;
}
/**
* Options which can be set when calling the `.get()` method on a
* {@linkcode SecureCookieMap}.
*/
export interface SecureCookieMapGetOptions {
/** Overrides the flag that was set when the instance was created. */
signed?: boolean;
}
/**
* Options which can be set when calling the `.set()` or `.delete()` method on a
* {@linkcode SecureCookieMap}.
*/
export interface SecureCookieMapSetDeleteOptions {
/** The domain to scope the cookie for. */
domain?: string;
/** When the cookie expires. */
expires?: Date;
/** Number of seconds until the cookie expires */
maxAge?: number;
/** A flag that indicates if the cookie is valid over HTTP only. */
httpOnly?: boolean;
/** Do not error when signing and validating cookies over an insecure
* connection. */
ignoreInsecure?: boolean;
/** Overwrite an existing value. */
overwrite?: boolean;
/** The path the cookie is valid for. */
path?: string;
/** Override the flag that was set when the instance was created. */
secure?: boolean;
/** Set the same-site indicator for a cookie. */
sameSite?: "strict" | "lax" | "none" | boolean;
/** Override the default behavior of signing the cookie. */
signed?: boolean;
}
type CookieAttributes = SecureCookieMapSetDeleteOptions;
// deno-lint-ignore no-control-regex
const FIELD_CONTENT_REGEXP = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
const KEY_REGEXP = /(?:^|;) *([^=]*)=[^;]*/g;
const SAME_SITE_REGEXP = /^(?:lax|none|strict)$/i;
const matchCache: Record<string, RegExp> = {};
function getPattern(name: string): RegExp {
if (name in matchCache) {
return matchCache[name];
}
return matchCache[name] = new RegExp(
`(?:^|;) *${name.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")}=([^;]*)`,
);
}
function pushCookie(values: string[], cookie: Cookie) {
if (cookie.overwrite) {
for (let i = values.length - 1; i >= 0; i--) {
if (values[i].indexOf(`${cookie.name}=`) === 0) {
values.splice(i, 1);
}
}
}
values.push(cookie.toHeaderValue());
}
function validateCookieProperty(
key: string,
value: string | undefined | null,
) {
if (value && !FIELD_CONTENT_REGEXP.test(value)) {
throw new TypeError(`The "${key}" of the cookie (${value}) is invalid.`);
}
}
/** An internal abstraction to manage cookies. */
class Cookie implements CookieAttributes {
domain?: string;
expires?: Date;
httpOnly = true;
maxAge?: number;
name: string;
overwrite = false;
path = "/";
sameSite: "strict" | "lax" | "none" | boolean = false;
secure = false;
signed?: boolean;
value: string;
constructor(
name: string,
value: string | null,
attributes: CookieAttributes,
) {
validateCookieProperty("name", name);
this.name = name;
validateCookieProperty("value", value);
this.value = value ?? "";
Object.assign(this, attributes);
if (!this.value) {
this.expires = new Date(0);
this.maxAge = undefined;
}
validateCookieProperty("path", this.path);
validateCookieProperty("domain", this.domain);
if (
this.sameSite && typeof this.sameSite === "string" &&
!SAME_SITE_REGEXP.test(this.sameSite)
) {
throw new TypeError(
`The "sameSite" of the cookie ("${this.sameSite}") is invalid.`,
);
}
}
toHeaderValue(): string {
let value = this.toString();
if (this.maxAge) {
this.expires = new Date(Date.now() + (this.maxAge * 1000));
}
if (this.path) {
value += `; path=${this.path}`;
}
if (this.expires) {
value += `; expires=${this.expires.toUTCString()}`;
}
if (this.domain) {
value += `; domain=${this.domain}`;
}
if (this.sameSite) {
value += `; samesite=${
this.sameSite === true ? "strict" : this.sameSite.toLowerCase()
}`;
}
if (this.secure) {
value += "; secure";
}
if (this.httpOnly) {
value += "; httponly";
}
return value;
}
toString(): string {
return `${this.name}=${this.value}`;
}
}
/**
* Symbol which is used in {@link mergeHeaders} to extract a
* `[string | string][]` from an instance to generate the final set of
* headers.
*/
export const cookieMapHeadersInitSymbol: unique symbol = Symbol.for(
"oak.commons.cookieMap.headersInit",
);
function isMergeable(value: unknown): value is Mergeable {
return value !== null && value !== undefined && typeof value === "object" &&
cookieMapHeadersInitSymbol in value;
}
/**
* Allows merging of various sources of headers into a final set of headers
* which can be used in a {@linkcode Response}.
*
* Note, that unlike when passing a `Response` or {@linkcode Headers} used in a
* response to {@linkcode CookieMap} or {@linkcode SecureCookieMap}, merging
* will not ensure that there are no other `Set-Cookie` headers from other
* sources, it will simply append the various headers together.
*/
export function mergeHeaders(
...sources: (Headered | HeadersInit | Mergeable)[]
): Headers {
const headers = new Headers();
for (const source of sources) {
let entries: Iterable<[string, string]>;
if (source instanceof Headers) {
entries = source;
} else if ("headers" in source && source.headers instanceof Headers) {
entries = source.headers;
} else if (isMergeable(source)) {
entries = source[cookieMapHeadersInitSymbol]();
} else if (Array.isArray(source)) {
entries = source as [string, string][];
} else {
entries = Object.entries(source);
}
for (const [key, value] of entries) {
headers.append(key, value);
}
}
return headers;
}
const keys = Symbol("#keys");
const requestHeaders = Symbol("#requestHeaders");
const responseHeaders = Symbol("#responseHeaders");
const isSecure = Symbol("#secure");
const requestKeys: unique symbol = Symbol("#requestKeys");
/** An internal abstract class which provides common functionality for
* {@link CookieMap} and {@link SecureCookieMap}.
*/
abstract class CookieMapBase implements Mergeable {
[keys]?: string[];
[requestHeaders]: Headers;
[responseHeaders]: Headers;
[isSecure]: boolean;
[requestKeys](): string[] {
if (this[keys]) {
return this[keys];
}
const result = this[keys] = [] as string[];
const header = this[requestHeaders].get("cookie");
if (!header) {
return result;
}
let matches: RegExpExecArray | null;
while ((matches = KEY_REGEXP.exec(header))) {
const [, key] = matches;
result.push(key);
}
return result;
}
constructor(request: Headers | Headered, options: CookieMapOptions) {
this[requestHeaders] = "headers" in request ? request.headers : request;
const { secure = false, response = new Headers() } = options;
this[responseHeaders] = "headers" in response ? response.headers : response;
this[isSecure] = secure;
}
/** A method used by {@linkcode mergeHeaders} to be able to merge
* headers from various sources when forming a {@linkcode Response}. */
[cookieMapHeadersInitSymbol](): [string, string][] {
const init: [string, string][] = [];
for (const [key, value] of this[responseHeaders]) {
if (key === "set-cookie") {
init.push([key, value]);
}
}
return init;
}
[Symbol.for("Deno.customInspect")](): string {
return `${this.constructor.name} []`;
}
[Symbol.for("nodejs.util.inspect.custom")](
depth: number,
// deno-lint-ignore no-explicit-any
options: any,
inspect: (value: unknown, options?: unknown) => string,
): string {
if (depth < 0) {
return options.stylize(`[${this.constructor.name}]`, "special");
}
const newOptions = Object.assign({}, options, {
depth: options.depth === null ? null : options.depth - 1,
});
return `${options.stylize(this.constructor.name, "special")} ${
inspect([], newOptions)
}`;
}
}
/**
* Provides a way to manage cookies in a request and response on the server
* as a single iterable collection.
*
* The methods and properties align to {@linkcode Map}. When constructing a
* {@linkcode Request} or {@linkcode Headers} from the request need to be
* provided, as well as optionally the {@linkcode Response} or `Headers` for the
* response can be provided. Alternatively the {@linkcode mergeHeaders}
* function can be used to generate a final set of headers for sending in the
* response.
*/
export class CookieMap extends CookieMapBase {
/** Contains the number of valid cookies in the request headers. */
get size(): number {
return [...this].length;
}
constructor(request: Headers | Headered, options: CookieMapOptions = {}) {
super(request, options);
}
/** Deletes all the cookies from the {@linkcode Request} in the response. */
clear(options: CookieMapSetDeleteOptions = {}) {
for (const key of this.keys()) {
this.set(key, null, options);
}
}
/** Set a cookie to be deleted in the response.
*
* This is a convenience function for `set(key, null, options?)`.
*/
delete(key: string, options: CookieMapSetDeleteOptions = {}): boolean {
this.set(key, null, options);
return true;
}
/** Return the value of a matching key present in the {@linkcode Request}. If
* the key is not present `undefined` is returned. */
get(key: string): string | undefined {
const headerValue = this[requestHeaders].get("cookie");
if (!headerValue) {
return undefined;
}
const match = headerValue.match(getPattern(key));
if (!match) {
return undefined;
}
const [, value] = match;
return value;
}
/** Returns `true` if the matching key is present in the {@linkcode Request},
* otherwise `false`. */
has(key: string): boolean {
const headerValue = this[requestHeaders].get("cookie");
if (!headerValue) {
return false;
}
return getPattern(key).test(headerValue);
}
/** Set a named cookie in the response. The optional
* {@linkcode CookieMapSetDeleteOptions} are applied to the cookie being set.
*/
set(
key: string,
value: string | null,
options: CookieMapSetDeleteOptions = {},
): this {
const resHeaders = this[responseHeaders];
const values: string[] = [];
for (const [key, value] of resHeaders) {
if (key === "set-cookie") {
values.push(value);
}
}
const secure = this[isSecure];
if (!secure && options.secure && !options.ignoreInsecure) {
throw new TypeError(
"Cannot send secure cookie over unencrypted connection.",
);
}
const cookie = new Cookie(key, value, options);
cookie.secure = options.secure ?? secure;
pushCookie(values, cookie);
resHeaders.delete("set-cookie");
for (const value of values) {
resHeaders.append("set-cookie", value);
}
return this;
}
/** Iterate over the cookie keys and values that are present in the
* {@linkcode Request}. This is an alias of the `[Symbol.iterator]` method
* present on the class. */
entries(): IterableIterator<[string, string]> {
return this[Symbol.iterator]();
}
/** Iterate over the cookie keys that are present in the
* {@linkcode Request}. */
*keys(): IterableIterator<string> {
for (const [key] of this) {
yield key;
}
}
/** Iterate over the cookie values that are present in the
* {@linkcode Request}. */
*values(): IterableIterator<string> {
for (const [, value] of this) {
yield value;
}
}
/** Iterate over the cookie keys and values that are present in the
* {@linkcode Request}. */
*[Symbol.iterator](): IterableIterator<[string, string]> {
const keys = this[requestKeys]();
for (const key of keys) {
const value = this.get(key);
if (value) {
yield [key, value];
}
}
}
}
/**
* Types of data that can be signed cryptographically.
*/
export type Data = string | number[] | ArrayBuffer | Uint8Array;
/**
* An interface which describes the methods that {@linkcode SecureCookieMap}
* uses to sign and verify cookies.
*/
export interface KeyRing {
/** Given a set of data and a digest, return the key index of the key used
* to sign the data. The index is 0 based. A non-negative number indices the
* digest is valid and a key was found. */
indexOf(data: Data, digest: string): Promise<number> | number;
/** Sign the data, returning a string based digest of the data. */
sign(data: Data): Promise<string> | string;
/** Verifies the digest matches the provided data, indicating the data was
* signed by the keyring and has not been tampered with. */
verify(data: Data, digest: string): Promise<boolean> | boolean;
}
/**
* Provides an way to manage cookies in a request and response on the server
* as a single iterable collection, as well as the ability to sign and verify
* cookies to prevent tampering.
*
* The methods and properties align to {@linkcode Map}, but due to the need to
* support asynchronous cryptographic keys, all the APIs operate async. When
* constructing a {@linkcode Request} or {@linkcode Headers} from the request
* need to be provided, as well as optionally the {@linkcode Response} or
* `Headers` for the response can be provided. Alternatively the
* {@linkcode mergeHeaders} function can be used to generate a final set
* of headers for sending in the response.
*
* On construction, the optional set of keys implementing the
* {@linkcode KeyRing} interface. While it is optional, if you don't plan to use
* keys, you might want to consider using just the {@linkcode CookieMap}.
*/
export class SecureCookieMap extends CookieMapBase {
#keyRing?: KeyRing;
/** Is set to a promise which resolves with the number of cookies in the
* {@linkcode Request}. */
get size(): Promise<number> {
return (async () => {
let size = 0;
for await (const _ of this) {
size++;
}
return size;
})();
}
constructor(
request: Headers | Headered,
options: SecureCookieMapOptions = {},
) {
super(request, options);
const { keys } = options;
this.#keyRing = keys;
}
/** Sets all cookies in the {@linkcode Request} to be deleted in the
* response. */
async clear(options: SecureCookieMapSetDeleteOptions) {
const promises = [];
for await (const key of this.keys()) {
promises.push(this.set(key, null, options));
}
await Promise.all(promises);
}
/** Set a cookie to be deleted in the response.
*
* This is a convenience function for `set(key, null, options?)`. */
async delete(
key: string,
options: SecureCookieMapSetDeleteOptions = {},
): Promise<boolean> {
await this.set(key, null, options);
return true;
}
/** Get the value of a cookie from the {@linkcode Request}.
*
* If the cookie is signed, and the signature is invalid, `undefined` will be
* returned and the cookie will be set to be deleted in the response. If the
* cookie is using an "old" key from the keyring, the cookie will be re-signed
* with the current key and be added to the response to be updated. */
async get(
key: string,
options: SecureCookieMapGetOptions = {},
): Promise<string | undefined> {
const signed = options.signed ?? !!this.#keyRing;
const nameSig = `${key}.sig`;
const header = this[requestHeaders].get("cookie");
if (!header) {
return;
}
const match = header.match(getPattern(key));
if (!match) {
return;
}
const [, value] = match;
if (!signed) {
return value;
}
const digest = await this.get(nameSig, { signed: false });
if (!digest) {
return;
}
const data = `${key}=${value}`;
if (!this.#keyRing) {
throw new TypeError("key ring required for signed cookies");
}
const index = await this.#keyRing.indexOf(data, digest);
if (index < 0) {
await this.delete(nameSig, { path: "/", signed: false });
} else {
if (index) {
await this.set(nameSig, await this.#keyRing.sign(data), {
signed: false,
});
}
return value;
}
}
/** Returns `true` if the key is in the {@linkcode Request}.
*
* If the cookie is signed, and the signature is invalid, `false` will be
* returned and the cookie will be set to be deleted in the response. If the
* cookie is using an "old" key from the keyring, the cookie will be re-signed
* with the current key and be added to the response to be updated. */
async has(
key: string,
options: SecureCookieMapGetOptions = {},
): Promise<boolean> {
const signed = options.signed ?? !!this.#keyRing;
const nameSig = `${key}.sig`;
const header = this[requestHeaders].get("cookie");
if (!header) {
return false;
}
const match = header.match(getPattern(key));
if (!match) {
return false;
}
if (!signed) {
return true;
}
const digest = await this.get(nameSig, { signed: false });
if (!digest) {
return false;
}
const [, value] = match;
const data = `${key}=${value}`;
if (!this.#keyRing) {
throw new TypeError("key ring required for signed cookies");
}
const index = await this.#keyRing.indexOf(data, digest);
if (index < 0) {
await this.delete(nameSig, { path: "/", signed: false });
return false;
} else {
if (index) {
await this.set(nameSig, await this.#keyRing.sign(data), {
signed: false,
});
}
return true;
}
}
/** Set a cookie in the response headers.
*
* If there was a keyring set, cookies will be automatically signed, unless
* overridden by the passed options. Cookies can be deleted by setting the
* value to `null`. */
async set(
key: string,
value: string | null,
options: SecureCookieMapSetDeleteOptions = {},
): Promise<this> {
const resHeaders = this[responseHeaders];
const headers: string[] = [];
for (const [key, value] of resHeaders.entries()) {
if (key === "set-cookie") {
headers.push(value);
}
}
const secure = this[isSecure];
const signed = options.signed ?? !!this.#keyRing;
if (!secure && options.secure && !options.ignoreInsecure) {
throw new TypeError(
"Cannot send secure cookie over unencrypted connection.",
);
}
const cookie = new Cookie(key, value, options);
cookie.secure = options.secure ?? secure;
pushCookie(headers, cookie);
if (signed) {
if (!this.#keyRing) {
throw new TypeError("keys required for signed cookies.");
}
cookie.value = await this.#keyRing.sign(cookie.toString());
cookie.name += ".sig";
pushCookie(headers, cookie);
}
resHeaders.delete("set-cookie");
for (const header of headers) {
resHeaders.append("set-cookie", header);
}
return this;
}
/** Iterate over the {@linkcode Request} cookies, yielding up a tuple
* containing the key and value of each cookie.
*
* If a key ring was provided, only properly signed cookie keys and values are
* returned. */
entries(): AsyncIterableIterator<[string, string]> {
return this[Symbol.asyncIterator]();
}
/** Iterate over the request's cookies, yielding up the key of each cookie.
*
* If a keyring was provided, only properly signed cookie keys are
* returned. */
async *keys(): AsyncIterableIterator<string> {
for await (const [key] of this) {
yield key;
}
}
/** Iterate over the request's cookies, yielding up the value of each cookie.
*
* If a keyring was provided, only properly signed cookie values are
* returned. */
async *values(): AsyncIterableIterator<string> {
for await (const [, value] of this) {
yield value;
}
}
/** Iterate over the {@linkcode Request} cookies, yielding up a tuple
* containing the key and value of each cookie.
*
* If a key ring was provided, only properly signed cookie keys and values are
* returned. */
async *[Symbol.asyncIterator](): AsyncIterableIterator<[string, string]> {
const keys = this[requestKeys]();
for (const key of keys) {
const value = await this.get(key);
if (value) {
yield [key, value];
}
}
}
}