cookie-es 0.0.0 → 1.0.0
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/LICENSE +24 -0
- package/README.md +32 -0
- package/dist/index.cjs +136 -0
- package/dist/index.d.ts +140 -0
- package/dist/index.mjs +133 -0
- package/package.json +39 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
(The MIT License)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2012-2014 Roman Shtylman <shtylman@gmail.com>
|
|
4
|
+
Copyright (c) 2015 Douglas Christopher Wilson <doug@somethingdoug.com>
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
7
|
+
a copy of this software and associated documentation files (the
|
|
8
|
+
'Software'), to deal in the Software without restriction, including
|
|
9
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
10
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
11
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
12
|
+
the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be
|
|
15
|
+
included in all copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
19
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
20
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
21
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
22
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
23
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
24
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
# cookie-es
|
|
3
|
+
|
|
4
|
+
[](https://bundlephobia.com/package/cookie-es)
|
|
5
|
+
|
|
6
|
+
ESM build of [cookie](https://www.npmjs.com/package/cookie) with bundled types.
|
|
7
|
+
|
|
8
|
+
## Usage
|
|
9
|
+
|
|
10
|
+
Install:
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
# npm
|
|
14
|
+
npm i cookie-es
|
|
15
|
+
|
|
16
|
+
# yarn
|
|
17
|
+
yarn add cookie-es
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Import:
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
// ESM
|
|
24
|
+
import { parse, serialize } from 'cookie-es'
|
|
25
|
+
|
|
26
|
+
// CommonJS
|
|
27
|
+
const { parse, serialize } = require('cookie-es')
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## License
|
|
31
|
+
|
|
32
|
+
[MIT](./LICENSE)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fieldContentRegExp = /^[\u0009\u0020-\u007E\u0080-\u00FF]+$/;
|
|
4
|
+
function parse(str, options) {
|
|
5
|
+
if (typeof str !== "string") {
|
|
6
|
+
throw new TypeError("argument str must be a string");
|
|
7
|
+
}
|
|
8
|
+
const obj = {};
|
|
9
|
+
const opt = options || {};
|
|
10
|
+
const dec = opt.decode || decode;
|
|
11
|
+
let index = 0;
|
|
12
|
+
while (index < str.length) {
|
|
13
|
+
const eqIdx = str.indexOf("=", index);
|
|
14
|
+
if (eqIdx === -1) {
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
let endIdx = str.indexOf(";", index);
|
|
18
|
+
if (endIdx === -1) {
|
|
19
|
+
endIdx = str.length;
|
|
20
|
+
} else if (endIdx < eqIdx) {
|
|
21
|
+
index = str.lastIndexOf(";", eqIdx - 1) + 1;
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const key = str.slice(index, eqIdx).trim();
|
|
25
|
+
if (void 0 === obj[key]) {
|
|
26
|
+
let val = str.slice(eqIdx + 1, endIdx).trim();
|
|
27
|
+
if (val.codePointAt(0) === 34) {
|
|
28
|
+
val = val.slice(1, -1);
|
|
29
|
+
}
|
|
30
|
+
obj[key] = tryDecode(val, dec);
|
|
31
|
+
}
|
|
32
|
+
index = endIdx + 1;
|
|
33
|
+
}
|
|
34
|
+
return obj;
|
|
35
|
+
}
|
|
36
|
+
function serialize(name, value, options) {
|
|
37
|
+
const opt = options || {};
|
|
38
|
+
const enc = opt.encode || encode;
|
|
39
|
+
if (typeof enc !== "function") {
|
|
40
|
+
throw new TypeError("option encode is invalid");
|
|
41
|
+
}
|
|
42
|
+
if (!fieldContentRegExp.test(name)) {
|
|
43
|
+
throw new TypeError("argument name is invalid");
|
|
44
|
+
}
|
|
45
|
+
const encodedValue = enc(value);
|
|
46
|
+
if (encodedValue && !fieldContentRegExp.test(encodedValue)) {
|
|
47
|
+
throw new TypeError("argument val is invalid");
|
|
48
|
+
}
|
|
49
|
+
let str = name + "=" + encodedValue;
|
|
50
|
+
if (void 0 !== opt.maxAge && opt.maxAge !== null) {
|
|
51
|
+
const maxAge = opt.maxAge - 0;
|
|
52
|
+
if (Number.isNaN(maxAge) || !Number.isFinite(maxAge)) {
|
|
53
|
+
throw new TypeError("option maxAge is invalid");
|
|
54
|
+
}
|
|
55
|
+
str += "; Max-Age=" + Math.floor(maxAge);
|
|
56
|
+
}
|
|
57
|
+
if (opt.domain) {
|
|
58
|
+
if (!fieldContentRegExp.test(opt.domain)) {
|
|
59
|
+
throw new TypeError("option domain is invalid");
|
|
60
|
+
}
|
|
61
|
+
str += "; Domain=" + opt.domain;
|
|
62
|
+
}
|
|
63
|
+
if (opt.path) {
|
|
64
|
+
if (!fieldContentRegExp.test(opt.path)) {
|
|
65
|
+
throw new TypeError("option path is invalid");
|
|
66
|
+
}
|
|
67
|
+
str += "; Path=" + opt.path;
|
|
68
|
+
}
|
|
69
|
+
if (opt.expires) {
|
|
70
|
+
if (!isDate(opt.expires) || Number.isNaN(opt.expires.valueOf())) {
|
|
71
|
+
throw new TypeError("option expires is invalid");
|
|
72
|
+
}
|
|
73
|
+
str += "; Expires=" + opt.expires.toUTCString();
|
|
74
|
+
}
|
|
75
|
+
if (opt.httpOnly) {
|
|
76
|
+
str += "; HttpOnly";
|
|
77
|
+
}
|
|
78
|
+
if (opt.secure) {
|
|
79
|
+
str += "; Secure";
|
|
80
|
+
}
|
|
81
|
+
if (opt.priority) {
|
|
82
|
+
const priority = typeof opt.priority === "string" ? opt.priority.toLowerCase() : opt.priority;
|
|
83
|
+
switch (priority) {
|
|
84
|
+
case "low":
|
|
85
|
+
str += "; Priority=Low";
|
|
86
|
+
break;
|
|
87
|
+
case "medium":
|
|
88
|
+
str += "; Priority=Medium";
|
|
89
|
+
break;
|
|
90
|
+
case "high":
|
|
91
|
+
str += "; Priority=High";
|
|
92
|
+
break;
|
|
93
|
+
default:
|
|
94
|
+
throw new TypeError("option priority is invalid");
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (opt.sameSite) {
|
|
98
|
+
const sameSite = typeof opt.sameSite === "string" ? opt.sameSite.toLowerCase() : opt.sameSite;
|
|
99
|
+
switch (sameSite) {
|
|
100
|
+
case true:
|
|
101
|
+
str += "; SameSite=Strict";
|
|
102
|
+
break;
|
|
103
|
+
case "lax":
|
|
104
|
+
str += "; SameSite=Lax";
|
|
105
|
+
break;
|
|
106
|
+
case "strict":
|
|
107
|
+
str += "; SameSite=Strict";
|
|
108
|
+
break;
|
|
109
|
+
case "none":
|
|
110
|
+
str += "; SameSite=None";
|
|
111
|
+
break;
|
|
112
|
+
default:
|
|
113
|
+
throw new TypeError("option sameSite is invalid");
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return str;
|
|
117
|
+
}
|
|
118
|
+
function isDate(val) {
|
|
119
|
+
return Object.prototype.toString.call(val) === "[object Date]" || val instanceof Date;
|
|
120
|
+
}
|
|
121
|
+
function tryDecode(str, decode2) {
|
|
122
|
+
try {
|
|
123
|
+
return decode2(str);
|
|
124
|
+
} catch {
|
|
125
|
+
return str;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function decode(str) {
|
|
129
|
+
return str.includes("%") ? decodeURIComponent(str) : str;
|
|
130
|
+
}
|
|
131
|
+
function encode(val) {
|
|
132
|
+
return encodeURIComponent(val);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
exports.parse = parse;
|
|
136
|
+
exports.serialize = serialize;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Basic HTTP cookie parser and serializer for HTTP servers.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Additional serialization options
|
|
6
|
+
*/
|
|
7
|
+
interface CookieSerializeOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.3|Domain Set-Cookie attribute}. By default, no
|
|
10
|
+
* domain is set, and most clients will consider the cookie to apply to only
|
|
11
|
+
* the current domain.
|
|
12
|
+
*/
|
|
13
|
+
domain?: string | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Specifies a function that will be used to encode a cookie's value. Since
|
|
16
|
+
* value of a cookie has a limited character set (and must be a simple
|
|
17
|
+
* string), this function can be used to encode a value into a string suited
|
|
18
|
+
* for a cookie's value.
|
|
19
|
+
*
|
|
20
|
+
* The default function is the global `encodeURIComponent`, which will
|
|
21
|
+
* encode a JavaScript string into UTF-8 byte sequences and then URL-encode
|
|
22
|
+
* any that fall outside of the cookie range.
|
|
23
|
+
*/
|
|
24
|
+
encode?(value: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* Specifies the `Date` object to be the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.1|`Expires` `Set-Cookie` attribute}. By default,
|
|
27
|
+
* no expiration is set, and most clients will consider this a "non-persistent cookie" and will delete
|
|
28
|
+
* it on a condition like exiting a web browser application.
|
|
29
|
+
*
|
|
30
|
+
* *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
|
|
31
|
+
* states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
|
|
32
|
+
* possible not all clients by obey this, so if both are set, they should
|
|
33
|
+
* point to the same date and time.
|
|
34
|
+
*/
|
|
35
|
+
expires?: Date | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.6|`HttpOnly` `Set-Cookie` attribute}.
|
|
38
|
+
* When truthy, the `HttpOnly` attribute is set, otherwise it is not. By
|
|
39
|
+
* default, the `HttpOnly` attribute is not set.
|
|
40
|
+
*
|
|
41
|
+
* *Note* be careful when setting this to true, as compliant clients will
|
|
42
|
+
* not allow client-side JavaScript to see the cookie in `document.cookie`.
|
|
43
|
+
*/
|
|
44
|
+
httpOnly?: boolean | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Specifies the number (in seconds) to be the value for the `Max-Age`
|
|
47
|
+
* `Set-Cookie` attribute. The given number will be converted to an integer
|
|
48
|
+
* by rounding down. By default, no maximum age is set.
|
|
49
|
+
*
|
|
50
|
+
* *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
|
|
51
|
+
* states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
|
|
52
|
+
* possible not all clients by obey this, so if both are set, they should
|
|
53
|
+
* point to the same date and time.
|
|
54
|
+
*/
|
|
55
|
+
maxAge?: number | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.4|`Path` `Set-Cookie` attribute}.
|
|
58
|
+
* By default, the path is considered the "default path".
|
|
59
|
+
*/
|
|
60
|
+
path?: string | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Specifies the `string` to be the value for the [`Priority` `Set-Cookie` attribute][rfc-west-cookie-priority-00-4.1].
|
|
63
|
+
*
|
|
64
|
+
* - `'low'` will set the `Priority` attribute to `Low`.
|
|
65
|
+
* - `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
|
|
66
|
+
* - `'high'` will set the `Priority` attribute to `High`.
|
|
67
|
+
*
|
|
68
|
+
* More information about the different priority levels can be found in
|
|
69
|
+
* [the specification][rfc-west-cookie-priority-00-4.1].
|
|
70
|
+
*
|
|
71
|
+
* **note** This is an attribute that has not yet been fully standardized, and may change in the future.
|
|
72
|
+
* This also means many clients may ignore this attribute until they understand it.
|
|
73
|
+
*/
|
|
74
|
+
priority?: "low" | "medium" | "high" | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Specifies the boolean or string to be the value for the {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|`SameSite` `Set-Cookie` attribute}.
|
|
77
|
+
*
|
|
78
|
+
* - `true` will set the `SameSite` attribute to `Strict` for strict same
|
|
79
|
+
* site enforcement.
|
|
80
|
+
* - `false` will not set the `SameSite` attribute.
|
|
81
|
+
* - `'lax'` will set the `SameSite` attribute to Lax for lax same site
|
|
82
|
+
* enforcement.
|
|
83
|
+
* - `'strict'` will set the `SameSite` attribute to Strict for strict same
|
|
84
|
+
* site enforcement.
|
|
85
|
+
* - `'none'` will set the SameSite attribute to None for an explicit
|
|
86
|
+
* cross-site cookie.
|
|
87
|
+
*
|
|
88
|
+
* More information about the different enforcement levels can be found in {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|the specification}.
|
|
89
|
+
*
|
|
90
|
+
* *note* This is an attribute that has not yet been fully standardized, and may change in the future. This also means many clients may ignore this attribute until they understand it.
|
|
91
|
+
*/
|
|
92
|
+
sameSite?: true | false | "lax" | "strict" | "none" | undefined;
|
|
93
|
+
/**
|
|
94
|
+
* Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.5|`Secure` `Set-Cookie` attribute}. When truthy, the
|
|
95
|
+
* `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set.
|
|
96
|
+
*
|
|
97
|
+
* *Note* be careful when setting this to `true`, as compliant clients will
|
|
98
|
+
* not send the cookie back to the server in the future if the browser does
|
|
99
|
+
* not have an HTTPS connection.
|
|
100
|
+
*/
|
|
101
|
+
secure?: boolean | undefined;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Additional parsing options
|
|
105
|
+
*/
|
|
106
|
+
interface CookieParseOptions {
|
|
107
|
+
/**
|
|
108
|
+
* Specifies a function that will be used to decode a cookie's value. Since
|
|
109
|
+
* the value of a cookie has a limited character set (and must be a simple
|
|
110
|
+
* string), this function can be used to decode a previously-encoded cookie
|
|
111
|
+
* value into a JavaScript string or other object.
|
|
112
|
+
*
|
|
113
|
+
* The default function is the global `decodeURIComponent`, which will decode
|
|
114
|
+
* any URL-encoded sequences into their byte representations.
|
|
115
|
+
*
|
|
116
|
+
* *Note* if an error is thrown from this function, the original, non-decoded
|
|
117
|
+
* cookie value will be returned as the cookie's value.
|
|
118
|
+
*/
|
|
119
|
+
decode?(value: string): string;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Parse an HTTP Cookie header string and returning an object of all cookie
|
|
124
|
+
* name-value pairs.
|
|
125
|
+
*
|
|
126
|
+
* @param str the string representing a `Cookie` header value
|
|
127
|
+
* @param [options] object containing parsing options
|
|
128
|
+
*/
|
|
129
|
+
declare function parse(str: string, options?: CookieParseOptions): Record<string, string>;
|
|
130
|
+
/**
|
|
131
|
+
* Serialize a cookie name-value pair into a `Set-Cookie` header string.
|
|
132
|
+
*
|
|
133
|
+
* @param name the name for the cookie
|
|
134
|
+
* @param value value to set the cookie to
|
|
135
|
+
* @param [options] object containing serialization options
|
|
136
|
+
* @throws {TypeError} when `maxAge` options is invalid
|
|
137
|
+
*/
|
|
138
|
+
declare function serialize(name: string, value: string, options?: CookieSerializeOptions): string;
|
|
139
|
+
|
|
140
|
+
export { CookieParseOptions, CookieSerializeOptions, parse, serialize };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
const fieldContentRegExp = /^[\u0009\u0020-\u007E\u0080-\u00FF]+$/;
|
|
2
|
+
function parse(str, options) {
|
|
3
|
+
if (typeof str !== "string") {
|
|
4
|
+
throw new TypeError("argument str must be a string");
|
|
5
|
+
}
|
|
6
|
+
const obj = {};
|
|
7
|
+
const opt = options || {};
|
|
8
|
+
const dec = opt.decode || decode;
|
|
9
|
+
let index = 0;
|
|
10
|
+
while (index < str.length) {
|
|
11
|
+
const eqIdx = str.indexOf("=", index);
|
|
12
|
+
if (eqIdx === -1) {
|
|
13
|
+
break;
|
|
14
|
+
}
|
|
15
|
+
let endIdx = str.indexOf(";", index);
|
|
16
|
+
if (endIdx === -1) {
|
|
17
|
+
endIdx = str.length;
|
|
18
|
+
} else if (endIdx < eqIdx) {
|
|
19
|
+
index = str.lastIndexOf(";", eqIdx - 1) + 1;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
const key = str.slice(index, eqIdx).trim();
|
|
23
|
+
if (void 0 === obj[key]) {
|
|
24
|
+
let val = str.slice(eqIdx + 1, endIdx).trim();
|
|
25
|
+
if (val.codePointAt(0) === 34) {
|
|
26
|
+
val = val.slice(1, -1);
|
|
27
|
+
}
|
|
28
|
+
obj[key] = tryDecode(val, dec);
|
|
29
|
+
}
|
|
30
|
+
index = endIdx + 1;
|
|
31
|
+
}
|
|
32
|
+
return obj;
|
|
33
|
+
}
|
|
34
|
+
function serialize(name, value, options) {
|
|
35
|
+
const opt = options || {};
|
|
36
|
+
const enc = opt.encode || encode;
|
|
37
|
+
if (typeof enc !== "function") {
|
|
38
|
+
throw new TypeError("option encode is invalid");
|
|
39
|
+
}
|
|
40
|
+
if (!fieldContentRegExp.test(name)) {
|
|
41
|
+
throw new TypeError("argument name is invalid");
|
|
42
|
+
}
|
|
43
|
+
const encodedValue = enc(value);
|
|
44
|
+
if (encodedValue && !fieldContentRegExp.test(encodedValue)) {
|
|
45
|
+
throw new TypeError("argument val is invalid");
|
|
46
|
+
}
|
|
47
|
+
let str = name + "=" + encodedValue;
|
|
48
|
+
if (void 0 !== opt.maxAge && opt.maxAge !== null) {
|
|
49
|
+
const maxAge = opt.maxAge - 0;
|
|
50
|
+
if (Number.isNaN(maxAge) || !Number.isFinite(maxAge)) {
|
|
51
|
+
throw new TypeError("option maxAge is invalid");
|
|
52
|
+
}
|
|
53
|
+
str += "; Max-Age=" + Math.floor(maxAge);
|
|
54
|
+
}
|
|
55
|
+
if (opt.domain) {
|
|
56
|
+
if (!fieldContentRegExp.test(opt.domain)) {
|
|
57
|
+
throw new TypeError("option domain is invalid");
|
|
58
|
+
}
|
|
59
|
+
str += "; Domain=" + opt.domain;
|
|
60
|
+
}
|
|
61
|
+
if (opt.path) {
|
|
62
|
+
if (!fieldContentRegExp.test(opt.path)) {
|
|
63
|
+
throw new TypeError("option path is invalid");
|
|
64
|
+
}
|
|
65
|
+
str += "; Path=" + opt.path;
|
|
66
|
+
}
|
|
67
|
+
if (opt.expires) {
|
|
68
|
+
if (!isDate(opt.expires) || Number.isNaN(opt.expires.valueOf())) {
|
|
69
|
+
throw new TypeError("option expires is invalid");
|
|
70
|
+
}
|
|
71
|
+
str += "; Expires=" + opt.expires.toUTCString();
|
|
72
|
+
}
|
|
73
|
+
if (opt.httpOnly) {
|
|
74
|
+
str += "; HttpOnly";
|
|
75
|
+
}
|
|
76
|
+
if (opt.secure) {
|
|
77
|
+
str += "; Secure";
|
|
78
|
+
}
|
|
79
|
+
if (opt.priority) {
|
|
80
|
+
const priority = typeof opt.priority === "string" ? opt.priority.toLowerCase() : opt.priority;
|
|
81
|
+
switch (priority) {
|
|
82
|
+
case "low":
|
|
83
|
+
str += "; Priority=Low";
|
|
84
|
+
break;
|
|
85
|
+
case "medium":
|
|
86
|
+
str += "; Priority=Medium";
|
|
87
|
+
break;
|
|
88
|
+
case "high":
|
|
89
|
+
str += "; Priority=High";
|
|
90
|
+
break;
|
|
91
|
+
default:
|
|
92
|
+
throw new TypeError("option priority is invalid");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (opt.sameSite) {
|
|
96
|
+
const sameSite = typeof opt.sameSite === "string" ? opt.sameSite.toLowerCase() : opt.sameSite;
|
|
97
|
+
switch (sameSite) {
|
|
98
|
+
case true:
|
|
99
|
+
str += "; SameSite=Strict";
|
|
100
|
+
break;
|
|
101
|
+
case "lax":
|
|
102
|
+
str += "; SameSite=Lax";
|
|
103
|
+
break;
|
|
104
|
+
case "strict":
|
|
105
|
+
str += "; SameSite=Strict";
|
|
106
|
+
break;
|
|
107
|
+
case "none":
|
|
108
|
+
str += "; SameSite=None";
|
|
109
|
+
break;
|
|
110
|
+
default:
|
|
111
|
+
throw new TypeError("option sameSite is invalid");
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return str;
|
|
115
|
+
}
|
|
116
|
+
function isDate(val) {
|
|
117
|
+
return Object.prototype.toString.call(val) === "[object Date]" || val instanceof Date;
|
|
118
|
+
}
|
|
119
|
+
function tryDecode(str, decode2) {
|
|
120
|
+
try {
|
|
121
|
+
return decode2(str);
|
|
122
|
+
} catch {
|
|
123
|
+
return str;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function decode(str) {
|
|
127
|
+
return str.includes("%") ? decodeURIComponent(str) : str;
|
|
128
|
+
}
|
|
129
|
+
function encode(val) {
|
|
130
|
+
return encodeURIComponent(val);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export { parse, serialize };
|
package/package.json
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
"name": "cookie-es",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"repository": "unjs/cookie-es",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"require": "./dist/index.cjs",
|
|
12
|
+
"import": "./dist/index.mjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"main": "./dist/index.cjs",
|
|
16
|
+
"module": "./dist/index.mjs",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "unbuild",
|
|
23
|
+
"dev": "vitest",
|
|
24
|
+
"lint": "eslint --cache --ext .ts,.js,.mjs,.cjs . && prettier -c src test",
|
|
25
|
+
"lint:fix": "eslint --cache --ext .ts,.js,.mjs,.cjs . --fix && prettier -c src test -w",
|
|
26
|
+
"release": "pnpm test && pnpm build && changelogen --release --push && npm publish",
|
|
27
|
+
"test": "pnpm lint && vitest run --coverage"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@vitest/coverage-c8": "^0.31.0",
|
|
31
|
+
"changelogen": "^0.5.3",
|
|
32
|
+
"eslint": "^8.39.0",
|
|
33
|
+
"eslint-config-unjs": "^0.1.0",
|
|
34
|
+
"prettier": "^2.8.8",
|
|
35
|
+
"typescript": "^5.0.4",
|
|
36
|
+
"unbuild": "^1.2.1",
|
|
37
|
+
"vitest": "^0.31.0"
|
|
38
|
+
},
|
|
39
|
+
"packageManager": "pnpm@8.4.0"
|
|
40
|
+
}
|