cookie-es 0.0.0 → 0.5.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 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
+ [![bundle size](https://flat.badgen.net/bundlephobia/minzip/cookie-es)](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,109 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const decode = decodeURIComponent;
6
+ const encode = encodeURIComponent;
7
+ const pairSplitRegExp = /; */;
8
+ const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
9
+ function parse(str, options) {
10
+ if (typeof str !== "string") {
11
+ throw new TypeError("argument str must be a string");
12
+ }
13
+ let obj = {};
14
+ let opt = options || {};
15
+ let pairs = str.split(pairSplitRegExp);
16
+ let dec = opt.decode || decode;
17
+ for (let i = 0; i < pairs.length; i++) {
18
+ let pair = pairs[i];
19
+ let eq_idx = pair.indexOf("=");
20
+ if (eq_idx < 0) {
21
+ continue;
22
+ }
23
+ let key = pair.substr(0, eq_idx).trim();
24
+ let val = pair.substr(++eq_idx, pair.length).trim();
25
+ if (val[0] == '"') {
26
+ val = val.slice(1, -1);
27
+ }
28
+ if (obj[key] == void 0) {
29
+ obj[key] = tryDecode(val, dec);
30
+ }
31
+ }
32
+ return obj;
33
+ }
34
+ function serialize(name, value, options) {
35
+ let opt = options || {};
36
+ let 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
+ let 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 (opt.maxAge != null) {
49
+ let maxAge = opt.maxAge - 0;
50
+ if (isNaN(maxAge) || !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 (typeof opt.expires.toUTCString !== "function") {
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.sameSite) {
80
+ let sameSite = typeof opt.sameSite === "string" ? opt.sameSite.toLowerCase() : opt.sameSite;
81
+ switch (sameSite) {
82
+ case true:
83
+ str += "; SameSite=Strict";
84
+ break;
85
+ case "lax":
86
+ str += "; SameSite=Lax";
87
+ break;
88
+ case "strict":
89
+ str += "; SameSite=Strict";
90
+ break;
91
+ case "none":
92
+ str += "; SameSite=None";
93
+ break;
94
+ default:
95
+ throw new TypeError("option sameSite is invalid");
96
+ }
97
+ }
98
+ return str;
99
+ }
100
+ function tryDecode(str, decode2) {
101
+ try {
102
+ return decode2(str);
103
+ } catch (e) {
104
+ return str;
105
+ }
106
+ }
107
+
108
+ exports.parse = parse;
109
+ exports.serialize = serialize;
@@ -0,0 +1,126 @@
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 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}.
63
+ *
64
+ * - `true` will set the `SameSite` attribute to `Strict` for strict same
65
+ * site enforcement.
66
+ * - `false` will not set the `SameSite` attribute.
67
+ * - `'lax'` will set the `SameSite` attribute to Lax for lax same site
68
+ * enforcement.
69
+ * - `'strict'` will set the `SameSite` attribute to Strict for strict same
70
+ * site enforcement.
71
+ * - `'none'` will set the SameSite attribute to None for an explicit
72
+ * cross-site cookie.
73
+ *
74
+ * 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}.
75
+ *
76
+ * *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.
77
+ */
78
+ sameSite?: true | false | 'lax' | 'strict' | 'none' | undefined;
79
+ /**
80
+ * Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.5|`Secure` `Set-Cookie` attribute}. When truthy, the
81
+ * `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set.
82
+ *
83
+ * *Note* be careful when setting this to `true`, as compliant clients will
84
+ * not send the cookie back to the server in the future if the browser does
85
+ * not have an HTTPS connection.
86
+ */
87
+ secure?: boolean | undefined;
88
+ }
89
+ /**
90
+ * Additional parsing options
91
+ */
92
+ interface CookieParseOptions {
93
+ /**
94
+ * Specifies a function that will be used to decode a cookie's value. Since
95
+ * the value of a cookie has a limited character set (and must be a simple
96
+ * string), this function can be used to decode a previously-encoded cookie
97
+ * value into a JavaScript string or other object.
98
+ *
99
+ * The default function is the global `decodeURIComponent`, which will decode
100
+ * any URL-encoded sequences into their byte representations.
101
+ *
102
+ * *Note* if an error is thrown from this function, the original, non-decoded
103
+ * cookie value will be returned as the cookie's value.
104
+ */
105
+ decode?(value: string): string;
106
+ }
107
+
108
+ /**
109
+ * Parse an HTTP Cookie header string and returning an object of all cookie
110
+ * name-value pairs.
111
+ *
112
+ * @param str the string representing a `Cookie` header value
113
+ * @param [options] object containing parsing options
114
+ */
115
+ declare function parse(str: string, options?: CookieParseOptions): {};
116
+ /**
117
+ * Serialize a cookie name-value pair into a `Set-Cookie` header string.
118
+ *
119
+ * @param name the name for the cookie
120
+ * @param value value to set the cookie to
121
+ * @param [options] object containing serialization options
122
+ * @throws {TypeError} when `maxAge` options is invalid
123
+ */
124
+ declare function serialize(name: string, value: string, options?: CookieSerializeOptions): string;
125
+
126
+ export { CookieParseOptions, CookieSerializeOptions, parse, serialize };
package/dist/index.mjs ADDED
@@ -0,0 +1,104 @@
1
+ const decode = decodeURIComponent;
2
+ const encode = encodeURIComponent;
3
+ const pairSplitRegExp = /; */;
4
+ const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
5
+ function parse(str, options) {
6
+ if (typeof str !== "string") {
7
+ throw new TypeError("argument str must be a string");
8
+ }
9
+ let obj = {};
10
+ let opt = options || {};
11
+ let pairs = str.split(pairSplitRegExp);
12
+ let dec = opt.decode || decode;
13
+ for (let i = 0; i < pairs.length; i++) {
14
+ let pair = pairs[i];
15
+ let eq_idx = pair.indexOf("=");
16
+ if (eq_idx < 0) {
17
+ continue;
18
+ }
19
+ let key = pair.substr(0, eq_idx).trim();
20
+ let val = pair.substr(++eq_idx, pair.length).trim();
21
+ if (val[0] == '"') {
22
+ val = val.slice(1, -1);
23
+ }
24
+ if (obj[key] == void 0) {
25
+ obj[key] = tryDecode(val, dec);
26
+ }
27
+ }
28
+ return obj;
29
+ }
30
+ function serialize(name, value, options) {
31
+ let opt = options || {};
32
+ let enc = opt.encode || encode;
33
+ if (typeof enc !== "function") {
34
+ throw new TypeError("option encode is invalid");
35
+ }
36
+ if (!fieldContentRegExp.test(name)) {
37
+ throw new TypeError("argument name is invalid");
38
+ }
39
+ let encodedValue = enc(value);
40
+ if (encodedValue && !fieldContentRegExp.test(encodedValue)) {
41
+ throw new TypeError("argument val is invalid");
42
+ }
43
+ let str = name + "=" + encodedValue;
44
+ if (opt.maxAge != null) {
45
+ let maxAge = opt.maxAge - 0;
46
+ if (isNaN(maxAge) || !isFinite(maxAge)) {
47
+ throw new TypeError("option maxAge is invalid");
48
+ }
49
+ str += "; Max-Age=" + Math.floor(maxAge);
50
+ }
51
+ if (opt.domain) {
52
+ if (!fieldContentRegExp.test(opt.domain)) {
53
+ throw new TypeError("option domain is invalid");
54
+ }
55
+ str += "; Domain=" + opt.domain;
56
+ }
57
+ if (opt.path) {
58
+ if (!fieldContentRegExp.test(opt.path)) {
59
+ throw new TypeError("option path is invalid");
60
+ }
61
+ str += "; Path=" + opt.path;
62
+ }
63
+ if (opt.expires) {
64
+ if (typeof opt.expires.toUTCString !== "function") {
65
+ throw new TypeError("option expires is invalid");
66
+ }
67
+ str += "; Expires=" + opt.expires.toUTCString();
68
+ }
69
+ if (opt.httpOnly) {
70
+ str += "; HttpOnly";
71
+ }
72
+ if (opt.secure) {
73
+ str += "; Secure";
74
+ }
75
+ if (opt.sameSite) {
76
+ let sameSite = typeof opt.sameSite === "string" ? opt.sameSite.toLowerCase() : opt.sameSite;
77
+ switch (sameSite) {
78
+ case true:
79
+ str += "; SameSite=Strict";
80
+ break;
81
+ case "lax":
82
+ str += "; SameSite=Lax";
83
+ break;
84
+ case "strict":
85
+ str += "; SameSite=Strict";
86
+ break;
87
+ case "none":
88
+ str += "; SameSite=None";
89
+ break;
90
+ default:
91
+ throw new TypeError("option sameSite is invalid");
92
+ }
93
+ }
94
+ return str;
95
+ }
96
+ function tryDecode(str, decode2) {
97
+ try {
98
+ return decode2(str);
99
+ } catch (e) {
100
+ return str;
101
+ }
102
+ }
103
+
104
+ export { parse, serialize };
package/package.json CHANGED
@@ -1,5 +1,27 @@
1
1
  {
2
- "name": "cookie-es",
3
- "version": "0.0.0",
4
- "license": "MIT"
2
+ "name": "cookie-es",
3
+ "version": "0.5.0",
4
+ "repository": "unjs/cookie-es",
5
+ "license": "MIT",
6
+ "sideEffects": false,
7
+ "type": "module",
8
+ "exports": {
9
+ "require": "./dist/index.cjs",
10
+ "import": "./dist/index.mjs"
11
+ },
12
+ "main": "./dist/index.cjs",
13
+ "module": "./dist/index.mjs",
14
+ "types": "./dist/index.d.ts",
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "unbuild",
20
+ "release": "yarn build && standard-version && git push --follow-tags && npm publish",
21
+ "test": "node test/index.mjs"
22
+ },
23
+ "devDependencies": {
24
+ "standard-version": "latest",
25
+ "unbuild": "latest"
26
+ }
5
27
  }