UNPKG

4.54 kBTypeScriptView Raw
1import type { Nullable } from '../utils';
2/**
3 * Parse a cookie date string into a {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date | Date}. Parses according to
4 * {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.1.1 | RFC6265 - Section 5.1.1}, not
5 * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse | Date.parse()}.
6 *
7 * @remarks
8 *
9 * ### RFC6265 - 5.1.1. Dates
10 *
11 * The user agent MUST use an algorithm equivalent to the following
12 * algorithm to parse a cookie-date. Note that the various boolean
13 * flags defined as a part of the algorithm (i.e., found-time, found-
14 * day-of-month, found-month, found-year) are initially "not set".
15 *
16 * 1. Using the grammar below, divide the cookie-date into date-tokens.
17 *
18 * ```
19 * cookie-date = *delimiter date-token-list *delimiter
20 * date-token-list = date-token *( 1*delimiter date-token )
21 * date-token = 1*non-delimiter
22 *
23 * delimiter = %x09 / %x20-2F / %x3B-40 / %x5B-60 / %x7B-7E
24 * non-delimiter = %x00-08 / %x0A-1F / DIGIT / ":" / ALPHA / %x7F-FF
25 * non-digit = %x00-2F / %x3A-FF
26 *
27 * day-of-month = 1*2DIGIT ( non-digit *OCTET )
28 * month = ( "jan" / "feb" / "mar" / "apr" /
29 * "may" / "jun" / "jul" / "aug" /
30 * "sep" / "oct" / "nov" / "dec" ) *OCTET
31 * year = 2*4DIGIT ( non-digit *OCTET )
32 * time = hms-time ( non-digit *OCTET )
33 * hms-time = time-field ":" time-field ":" time-field
34 * time-field = 1*2DIGIT
35 * ```
36 *
37 * 2. Process each date-token sequentially in the order the date-tokens
38 * appear in the cookie-date:
39 *
40 * 1. If the found-time flag is not set and the token matches the
41 * time production, set the found-time flag and set the hour-
42 * value, minute-value, and second-value to the numbers denoted
43 * by the digits in the date-token, respectively. Skip the
44 * remaining sub-steps and continue to the next date-token.
45 *
46 * 2. If the found-day-of-month flag is not set and the date-token
47 * matches the day-of-month production, set the found-day-of-
48 * month flag and set the day-of-month-value to the number
49 * denoted by the date-token. Skip the remaining sub-steps and
50 * continue to the next date-token.
51 *
52 * 3. If the found-month flag is not set and the date-token matches
53 * the month production, set the found-month flag and set the
54 * month-value to the month denoted by the date-token. Skip the
55 * remaining sub-steps and continue to the next date-token.
56 *
57 * 4. If the found-year flag is not set and the date-token matches
58 * the year production, set the found-year flag and set the
59 * year-value to the number denoted by the date-token. Skip the
60 * remaining sub-steps and continue to the next date-token.
61 *
62 * 3. If the year-value is greater than or equal to 70 and less than or
63 * equal to 99, increment the year-value by 1900.
64 *
65 * 4. If the year-value is greater than or equal to 0 and less than or
66 * equal to 69, increment the year-value by 2000.
67 *
68 * 1. NOTE: Some existing user agents interpret two-digit years differently.
69 *
70 * 5. Abort these steps and fail to parse the cookie-date if:
71 *
72 * - at least one of the found-day-of-month, found-month, found-
73 * year, or found-time flags is not set,
74 *
75 * - the day-of-month-value is less than 1 or greater than 31,
76 *
77 * - the year-value is less than 1601,
78 *
79 * - the hour-value is greater than 23,
80 *
81 * - the minute-value is greater than 59, or
82 *
83 * - the second-value is greater than 59.
84 *
85 * (Note that leap seconds cannot be represented in this syntax.)
86 *
87 * 6. Let the parsed-cookie-date be the date whose day-of-month, month,
88 * year, hour, minute, and second (in UTC) are the day-of-month-
89 * value, the month-value, the year-value, the hour-value, the
90 * minute-value, and the second-value, respectively. If no such
91 * date exists, abort these steps and fail to parse the cookie-date.
92 *
93 * 7. Return the parsed-cookie-date as the result of this algorithm.
94 *
95 * @example
96 * ```
97 * parseDate('Wed, 09 Jun 2021 10:18:14 GMT')
98 * ```
99 *
100 * @param cookieDate - the cookie date string
101 * @public
102 */
103export declare function parseDate(cookieDate: Nullable<string>): Date | undefined;