@stryke/cookie 0.1.34 → 0.1.36
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 +14 -0
- package/dist/index.cjs +8 -1
- package/dist/index.mjs +5 -1
- package/dist/parse.cjs +97 -1
- package/dist/parse.mjs +96 -1
- package/dist/parse.mjs.map +1 -1
- package/dist/split.cjs +47 -1
- package/dist/split.mjs +47 -1
- package/dist/split.mjs.map +1 -1
- package/dist/stringify.cjs +83 -1
- package/dist/stringify.mjs +83 -1
- package/dist/stringify.mjs.map +1 -1
- package/dist/type-checks/src/get-object-tag.cjs +15 -1
- package/dist/type-checks/src/get-object-tag.mjs +14 -1
- package/dist/type-checks/src/get-object-tag.mjs.map +1 -1
- package/dist/type-checks/src/index.cjs +12 -1
- package/dist/type-checks/src/index.mjs +14 -1
- package/dist/type-checks/src/is-buffer.cjs +12 -1
- package/dist/type-checks/src/is-buffer.mjs +11 -1
- package/dist/type-checks/src/is-buffer.mjs.map +1 -1
- package/dist/type-checks/src/is-collection.cjs +1 -1
- package/dist/type-checks/src/is-collection.mjs +3 -1
- package/dist/type-checks/src/is-date.cjs +23 -1
- package/dist/type-checks/src/is-date.mjs +23 -1
- package/dist/type-checks/src/is-date.mjs.map +1 -1
- package/dist/type-checks/src/is-empty.cjs +20 -1
- package/dist/type-checks/src/is-empty.mjs +20 -1
- package/dist/type-checks/src/is-empty.mjs.map +1 -1
- package/dist/type-checks/src/is-function.cjs +27 -1
- package/dist/type-checks/src/is-function.mjs +25 -1
- package/dist/type-checks/src/is-function.mjs.map +1 -1
- package/dist/type-checks/src/is-null.cjs +12 -1
- package/dist/type-checks/src/is-null.mjs +11 -1
- package/dist/type-checks/src/is-null.mjs.map +1 -1
- package/dist/type-checks/src/is-plain-object.cjs +30 -1
- package/dist/type-checks/src/is-plain-object.mjs +29 -1
- package/dist/type-checks/src/is-plain-object.mjs.map +1 -1
- package/dist/type-checks/src/is-set.cjs +19 -1
- package/dist/type-checks/src/is-set.mjs +19 -1
- package/dist/type-checks/src/is-set.mjs.map +1 -1
- package/dist/type-checks/src/is-string.cjs +12 -1
- package/dist/type-checks/src/is-string.mjs +11 -1
- package/dist/type-checks/src/is-string.mjs.map +1 -1
- package/dist/type-checks/src/is-undefined.cjs +8 -1
- package/dist/type-checks/src/is-undefined.mjs +7 -1
- package/dist/type-checks/src/is-undefined.mjs.map +1 -1
- package/dist/type-checks/src/type-detect.cjs +15 -1
- package/dist/type-checks/src/type-detect.mjs +16 -1
- package/dist/type-checks/src/type-detect.mjs.map +1 -1
- package/dist/types.mjs +1 -1
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
# Changelog for Stryke - Cookie
|
|
4
4
|
|
|
5
|
+
## [0.1.35](https://github.com/storm-software/stryke/releases/tag/cookie%400.1.35) (01/16/2026)
|
|
6
|
+
|
|
7
|
+
### Updated Dependencies
|
|
8
|
+
|
|
9
|
+
- Updated **type-checks** to **v0.5.19**
|
|
10
|
+
- Updated **convert** to **v0.6.34**
|
|
11
|
+
|
|
12
|
+
## [0.1.34](https://github.com/storm-software/stryke/releases/tag/cookie%400.1.34) (01/16/2026)
|
|
13
|
+
|
|
14
|
+
### Updated Dependencies
|
|
15
|
+
|
|
16
|
+
- Updated **type-checks** to **v0.5.18**
|
|
17
|
+
- Updated **convert** to **v0.6.33**
|
|
18
|
+
|
|
5
19
|
## [0.1.33](https://github.com/storm-software/stryke/releases/tag/cookie%400.1.33) (01/15/2026)
|
|
6
20
|
|
|
7
21
|
### Updated Dependencies
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_parse = require('./parse.cjs');
|
|
2
|
+
const require_split = require('./split.cjs');
|
|
3
|
+
const require_stringify = require('./stringify.cjs');
|
|
4
|
+
|
|
5
|
+
exports.parseCookie = require_parse.parseCookie;
|
|
6
|
+
exports.parseSetCookie = require_parse.parseSetCookie;
|
|
7
|
+
exports.splitSetCookieString = require_split.splitSetCookieString;
|
|
8
|
+
exports.stringifyCookie = require_stringify.stringifyCookie;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
import{parseCookie
|
|
1
|
+
import { parseCookie, parseSetCookie } from "./parse.mjs";
|
|
2
|
+
import { splitSetCookieString } from "./split.mjs";
|
|
3
|
+
import { stringifyCookie } from "./stringify.mjs";
|
|
4
|
+
|
|
5
|
+
export { parseCookie, parseSetCookie, splitSetCookieString, stringifyCookie };
|
package/dist/parse.cjs
CHANGED
|
@@ -1 +1,97 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_is_string = require('./type-checks/src/is-string.cjs');
|
|
2
|
+
require('./type-checks/src/index.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/parse.ts
|
|
5
|
+
/**
|
|
6
|
+
* Parse an HTTP Cookie header string and returning an object of all cookie name-value pairs.
|
|
7
|
+
*
|
|
8
|
+
* @param cookie - the string representing a `Cookie` header value
|
|
9
|
+
* @param options - object containing parsing options
|
|
10
|
+
* @returns an object with the parsed cookies
|
|
11
|
+
*/
|
|
12
|
+
function parseCookie(cookie, options) {
|
|
13
|
+
if (!require_is_string.isString(cookie)) throw new Error("argument str must be a string");
|
|
14
|
+
const obj = {};
|
|
15
|
+
const opt = options ?? {};
|
|
16
|
+
const dec = opt.decode ?? ((str) => str.includes("%") ? decodeURIComponent(str) : str);
|
|
17
|
+
let index = 0;
|
|
18
|
+
while (index < cookie.length) {
|
|
19
|
+
const eqIdx = cookie.indexOf("=", index);
|
|
20
|
+
if (eqIdx === -1) break;
|
|
21
|
+
let endIdx = cookie.indexOf(";", index);
|
|
22
|
+
if (endIdx === -1) endIdx = cookie.length;
|
|
23
|
+
else if (endIdx < eqIdx) {
|
|
24
|
+
index = cookie.lastIndexOf(";", eqIdx - 1) + 1;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const key = cookie.slice(index, eqIdx).trim();
|
|
28
|
+
if (opt?.filter && !opt?.filter(key)) {
|
|
29
|
+
index = endIdx + 1;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (void 0 === obj[key]) {
|
|
33
|
+
let val = cookie.slice(eqIdx + 1, endIdx).trim();
|
|
34
|
+
if (val.codePointAt(0) === 34) val = val.slice(1, -1);
|
|
35
|
+
try {
|
|
36
|
+
obj[key] = dec(val);
|
|
37
|
+
} catch {
|
|
38
|
+
obj[key] = val;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
index = endIdx + 1;
|
|
42
|
+
}
|
|
43
|
+
return obj;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Parse a [Set-Cookie](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) header string into an object.
|
|
47
|
+
*
|
|
48
|
+
* @param setCookieValue - the string representing a `Set-Cookie` header value
|
|
49
|
+
* @param options - object containing parsing options
|
|
50
|
+
* @returns an object with the parsed cookie
|
|
51
|
+
*/
|
|
52
|
+
function parseSetCookie(setCookieValue, options) {
|
|
53
|
+
const parts = (setCookieValue || "").split(";").filter((str) => require_is_string.isString(str) && Boolean(str.trim()));
|
|
54
|
+
const nameValuePairStr = parts.shift() || "";
|
|
55
|
+
let name = "";
|
|
56
|
+
let value = "";
|
|
57
|
+
const nameValueArr = nameValuePairStr.split("=");
|
|
58
|
+
if (nameValueArr.length > 1) {
|
|
59
|
+
name = nameValueArr.shift();
|
|
60
|
+
value = nameValueArr.join("=");
|
|
61
|
+
} else value = nameValuePairStr;
|
|
62
|
+
try {
|
|
63
|
+
value = options?.decode === false ? value : (options?.decode ?? decodeURIComponent)(value);
|
|
64
|
+
} catch {}
|
|
65
|
+
const cookie = {
|
|
66
|
+
name,
|
|
67
|
+
value
|
|
68
|
+
};
|
|
69
|
+
for (const part of parts) {
|
|
70
|
+
const sides = part.split("=");
|
|
71
|
+
const partKey = (sides.shift() || "").trimStart().toLowerCase();
|
|
72
|
+
const partValue = sides.join("=");
|
|
73
|
+
switch (partKey) {
|
|
74
|
+
case "expires":
|
|
75
|
+
cookie.expires = new Date(partValue);
|
|
76
|
+
break;
|
|
77
|
+
case "max-age":
|
|
78
|
+
cookie.maxAge = Number.parseInt(partValue, 10);
|
|
79
|
+
break;
|
|
80
|
+
case "secure":
|
|
81
|
+
cookie.secure = true;
|
|
82
|
+
break;
|
|
83
|
+
case "httponly":
|
|
84
|
+
cookie.httpOnly = true;
|
|
85
|
+
break;
|
|
86
|
+
case "samesite":
|
|
87
|
+
cookie.sameSite = partValue;
|
|
88
|
+
break;
|
|
89
|
+
default: cookie[partKey] = partValue;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return cookie;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
//#endregion
|
|
96
|
+
exports.parseCookie = parseCookie;
|
|
97
|
+
exports.parseSetCookie = parseSetCookie;
|
package/dist/parse.mjs
CHANGED
|
@@ -1,2 +1,97 @@
|
|
|
1
|
-
import{isString
|
|
1
|
+
import { isString } from "./type-checks/src/is-string.mjs";
|
|
2
|
+
import "./type-checks/src/index.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/parse.ts
|
|
5
|
+
/**
|
|
6
|
+
* Parse an HTTP Cookie header string and returning an object of all cookie name-value pairs.
|
|
7
|
+
*
|
|
8
|
+
* @param cookie - the string representing a `Cookie` header value
|
|
9
|
+
* @param options - object containing parsing options
|
|
10
|
+
* @returns an object with the parsed cookies
|
|
11
|
+
*/
|
|
12
|
+
function parseCookie(cookie, options) {
|
|
13
|
+
if (!isString(cookie)) throw new Error("argument str must be a string");
|
|
14
|
+
const obj = {};
|
|
15
|
+
const opt = options ?? {};
|
|
16
|
+
const dec = opt.decode ?? ((str) => str.includes("%") ? decodeURIComponent(str) : str);
|
|
17
|
+
let index = 0;
|
|
18
|
+
while (index < cookie.length) {
|
|
19
|
+
const eqIdx = cookie.indexOf("=", index);
|
|
20
|
+
if (eqIdx === -1) break;
|
|
21
|
+
let endIdx = cookie.indexOf(";", index);
|
|
22
|
+
if (endIdx === -1) endIdx = cookie.length;
|
|
23
|
+
else if (endIdx < eqIdx) {
|
|
24
|
+
index = cookie.lastIndexOf(";", eqIdx - 1) + 1;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const key = cookie.slice(index, eqIdx).trim();
|
|
28
|
+
if (opt?.filter && !opt?.filter(key)) {
|
|
29
|
+
index = endIdx + 1;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (void 0 === obj[key]) {
|
|
33
|
+
let val = cookie.slice(eqIdx + 1, endIdx).trim();
|
|
34
|
+
if (val.codePointAt(0) === 34) val = val.slice(1, -1);
|
|
35
|
+
try {
|
|
36
|
+
obj[key] = dec(val);
|
|
37
|
+
} catch {
|
|
38
|
+
obj[key] = val;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
index = endIdx + 1;
|
|
42
|
+
}
|
|
43
|
+
return obj;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Parse a [Set-Cookie](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) header string into an object.
|
|
47
|
+
*
|
|
48
|
+
* @param setCookieValue - the string representing a `Set-Cookie` header value
|
|
49
|
+
* @param options - object containing parsing options
|
|
50
|
+
* @returns an object with the parsed cookie
|
|
51
|
+
*/
|
|
52
|
+
function parseSetCookie(setCookieValue, options) {
|
|
53
|
+
const parts = (setCookieValue || "").split(";").filter((str) => isString(str) && Boolean(str.trim()));
|
|
54
|
+
const nameValuePairStr = parts.shift() || "";
|
|
55
|
+
let name = "";
|
|
56
|
+
let value = "";
|
|
57
|
+
const nameValueArr = nameValuePairStr.split("=");
|
|
58
|
+
if (nameValueArr.length > 1) {
|
|
59
|
+
name = nameValueArr.shift();
|
|
60
|
+
value = nameValueArr.join("=");
|
|
61
|
+
} else value = nameValuePairStr;
|
|
62
|
+
try {
|
|
63
|
+
value = options?.decode === false ? value : (options?.decode ?? decodeURIComponent)(value);
|
|
64
|
+
} catch {}
|
|
65
|
+
const cookie = {
|
|
66
|
+
name,
|
|
67
|
+
value
|
|
68
|
+
};
|
|
69
|
+
for (const part of parts) {
|
|
70
|
+
const sides = part.split("=");
|
|
71
|
+
const partKey = (sides.shift() || "").trimStart().toLowerCase();
|
|
72
|
+
const partValue = sides.join("=");
|
|
73
|
+
switch (partKey) {
|
|
74
|
+
case "expires":
|
|
75
|
+
cookie.expires = new Date(partValue);
|
|
76
|
+
break;
|
|
77
|
+
case "max-age":
|
|
78
|
+
cookie.maxAge = Number.parseInt(partValue, 10);
|
|
79
|
+
break;
|
|
80
|
+
case "secure":
|
|
81
|
+
cookie.secure = true;
|
|
82
|
+
break;
|
|
83
|
+
case "httponly":
|
|
84
|
+
cookie.httpOnly = true;
|
|
85
|
+
break;
|
|
86
|
+
case "samesite":
|
|
87
|
+
cookie.sameSite = partValue;
|
|
88
|
+
break;
|
|
89
|
+
default: cookie[partKey] = partValue;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return cookie;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
//#endregion
|
|
96
|
+
export { parseCookie, parseSetCookie };
|
|
2
97
|
//# sourceMappingURL=parse.mjs.map
|
package/dist/parse.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.mjs","names":["cookie: SetCookie"],"sources":["../src/parse.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isString } from \"@stryke/type-checks\";\nimport type {\n CookieParseOptions,\n SetCookie,\n SetCookieParseOptions\n} from \"./types\";\n\n/**\n * Parse an HTTP Cookie header string and returning an object of all cookie name-value pairs.\n *\n * @param cookie - the string representing a `Cookie` header value\n * @param options - object containing parsing options\n * @returns an object with the parsed cookies\n */\nexport function parseCookie(\n cookie: string,\n options?: CookieParseOptions\n): Record<string, string> {\n if (!isString(cookie)) {\n throw new Error(\"argument str must be a string\");\n }\n\n const obj = {};\n const opt = options ?? {};\n const dec =\n opt.decode ??\n ((str: string) => (str.includes(\"%\") ? decodeURIComponent(str) : str));\n\n let index = 0;\n while (index < cookie.length) {\n const eqIdx = cookie.indexOf(\"=\", index);\n\n // no more cookie pairs\n if (eqIdx === -1) {\n break;\n }\n\n let endIdx = cookie.indexOf(\";\", index);\n\n if (endIdx === -1) {\n endIdx = cookie.length;\n } else if (endIdx < eqIdx) {\n // backtrack on prior semicolon\n index = cookie.lastIndexOf(\";\", eqIdx - 1) + 1;\n\n continue;\n }\n\n const key = cookie.slice(index, eqIdx).trim();\n if (opt?.filter && !opt?.filter(key)) {\n index = endIdx + 1;\n\n continue;\n }\n\n // only assign once\n if (undefined === obj[key as keyof typeof obj]) {\n let val = cookie.slice(eqIdx + 1, endIdx).trim();\n\n // quoted values\n if (val.codePointAt(0) === 0x22) {\n val = val.slice(1, -1);\n }\n\n try {\n (obj as any)[key] = dec(val);\n } catch {\n (obj as any)[key] = val;\n }\n }\n\n index = endIdx + 1;\n }\n\n return obj;\n}\n\n/**\n * Parse a [Set-Cookie](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) header string into an object.\n *\n * @param setCookieValue - the string representing a `Set-Cookie` header value\n * @param options - object containing parsing options\n * @returns an object with the parsed cookie\n */\nexport function parseSetCookie(\n setCookieValue: string,\n options?: SetCookieParseOptions\n): SetCookie {\n const parts = (setCookieValue || \"\")\n .split(\";\")\n .filter(str => isString(str) && Boolean(str.trim()));\n\n const nameValuePairStr = parts.shift() || \"\";\n\n let name = \"\";\n let value = \"\";\n const nameValueArr = nameValuePairStr.split(\"=\");\n if (nameValueArr.length > 1) {\n name = nameValueArr.shift()!;\n // Everything after the first =, joined by a \"=\" if there was more than one part\n value = nameValueArr.join(\"=\");\n } else {\n value = nameValuePairStr;\n }\n\n try {\n value =\n options?.decode === false\n ? value\n : (options?.decode ?? decodeURIComponent)(value);\n } catch {\n // Do nothing\n }\n\n const cookie: SetCookie = {\n name,\n value\n };\n\n for (const part of parts) {\n const sides = part.split(\"=\");\n const partKey = (sides.shift() || \"\").trimStart().toLowerCase();\n const partValue = sides.join(\"=\");\n switch (partKey) {\n case \"expires\": {\n cookie.expires = new Date(partValue);\n break;\n }\n case \"max-age\": {\n cookie.maxAge = Number.parseInt(partValue, 10);\n break;\n }\n case \"secure\": {\n cookie.secure = true;\n break;\n }\n case \"httponly\": {\n cookie.httpOnly = true;\n break;\n }\n case \"samesite\": {\n cookie.sameSite = partValue as SetCookie[\"sameSite\"];\n break;\n }\n default: {\n cookie[partKey] = partValue;\n }\n }\n }\n\n return cookie;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"parse.mjs","names":["cookie: SetCookie"],"sources":["../src/parse.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isString } from \"@stryke/type-checks\";\nimport type {\n CookieParseOptions,\n SetCookie,\n SetCookieParseOptions\n} from \"./types\";\n\n/**\n * Parse an HTTP Cookie header string and returning an object of all cookie name-value pairs.\n *\n * @param cookie - the string representing a `Cookie` header value\n * @param options - object containing parsing options\n * @returns an object with the parsed cookies\n */\nexport function parseCookie(\n cookie: string,\n options?: CookieParseOptions\n): Record<string, string> {\n if (!isString(cookie)) {\n throw new Error(\"argument str must be a string\");\n }\n\n const obj = {};\n const opt = options ?? {};\n const dec =\n opt.decode ??\n ((str: string) => (str.includes(\"%\") ? decodeURIComponent(str) : str));\n\n let index = 0;\n while (index < cookie.length) {\n const eqIdx = cookie.indexOf(\"=\", index);\n\n // no more cookie pairs\n if (eqIdx === -1) {\n break;\n }\n\n let endIdx = cookie.indexOf(\";\", index);\n\n if (endIdx === -1) {\n endIdx = cookie.length;\n } else if (endIdx < eqIdx) {\n // backtrack on prior semicolon\n index = cookie.lastIndexOf(\";\", eqIdx - 1) + 1;\n\n continue;\n }\n\n const key = cookie.slice(index, eqIdx).trim();\n if (opt?.filter && !opt?.filter(key)) {\n index = endIdx + 1;\n\n continue;\n }\n\n // only assign once\n if (undefined === obj[key as keyof typeof obj]) {\n let val = cookie.slice(eqIdx + 1, endIdx).trim();\n\n // quoted values\n if (val.codePointAt(0) === 0x22) {\n val = val.slice(1, -1);\n }\n\n try {\n (obj as any)[key] = dec(val);\n } catch {\n (obj as any)[key] = val;\n }\n }\n\n index = endIdx + 1;\n }\n\n return obj;\n}\n\n/**\n * Parse a [Set-Cookie](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) header string into an object.\n *\n * @param setCookieValue - the string representing a `Set-Cookie` header value\n * @param options - object containing parsing options\n * @returns an object with the parsed cookie\n */\nexport function parseSetCookie(\n setCookieValue: string,\n options?: SetCookieParseOptions\n): SetCookie {\n const parts = (setCookieValue || \"\")\n .split(\";\")\n .filter(str => isString(str) && Boolean(str.trim()));\n\n const nameValuePairStr = parts.shift() || \"\";\n\n let name = \"\";\n let value = \"\";\n const nameValueArr = nameValuePairStr.split(\"=\");\n if (nameValueArr.length > 1) {\n name = nameValueArr.shift()!;\n // Everything after the first =, joined by a \"=\" if there was more than one part\n value = nameValueArr.join(\"=\");\n } else {\n value = nameValuePairStr;\n }\n\n try {\n value =\n options?.decode === false\n ? value\n : (options?.decode ?? decodeURIComponent)(value);\n } catch {\n // Do nothing\n }\n\n const cookie: SetCookie = {\n name,\n value\n };\n\n for (const part of parts) {\n const sides = part.split(\"=\");\n const partKey = (sides.shift() || \"\").trimStart().toLowerCase();\n const partValue = sides.join(\"=\");\n switch (partKey) {\n case \"expires\": {\n cookie.expires = new Date(partValue);\n break;\n }\n case \"max-age\": {\n cookie.maxAge = Number.parseInt(partValue, 10);\n break;\n }\n case \"secure\": {\n cookie.secure = true;\n break;\n }\n case \"httponly\": {\n cookie.httpOnly = true;\n break;\n }\n case \"samesite\": {\n cookie.sameSite = partValue as SetCookie[\"sameSite\"];\n break;\n }\n default: {\n cookie[partKey] = partValue;\n }\n }\n }\n\n return cookie;\n}\n"],"mappings":";;;;;;;;;;;AAgCA,SAAgB,YACd,QACA,SACwB;AACxB,KAAI,CAAC,SAAS,OAAO,CACnB,OAAM,IAAI,MAAM,gCAAgC;CAGlD,MAAM,MAAM,EAAE;CACd,MAAM,MAAM,WAAW,EAAE;CACzB,MAAM,MACJ,IAAI,YACF,QAAiB,IAAI,SAAS,IAAI,GAAG,mBAAmB,IAAI,GAAG;CAEnE,IAAI,QAAQ;AACZ,QAAO,QAAQ,OAAO,QAAQ;EAC5B,MAAM,QAAQ,OAAO,QAAQ,KAAK,MAAM;AAGxC,MAAI,UAAU,GACZ;EAGF,IAAI,SAAS,OAAO,QAAQ,KAAK,MAAM;AAEvC,MAAI,WAAW,GACb,UAAS,OAAO;WACP,SAAS,OAAO;AAEzB,WAAQ,OAAO,YAAY,KAAK,QAAQ,EAAE,GAAG;AAE7C;;EAGF,MAAM,MAAM,OAAO,MAAM,OAAO,MAAM,CAAC,MAAM;AAC7C,MAAI,KAAK,UAAU,CAAC,KAAK,OAAO,IAAI,EAAE;AACpC,WAAQ,SAAS;AAEjB;;AAIF,MAAI,WAAc,IAAI,MAA0B;GAC9C,IAAI,MAAM,OAAO,MAAM,QAAQ,GAAG,OAAO,CAAC,MAAM;AAGhD,OAAI,IAAI,YAAY,EAAE,KAAK,GACzB,OAAM,IAAI,MAAM,GAAG,GAAG;AAGxB,OAAI;AACF,IAAC,IAAY,OAAO,IAAI,IAAI;WACtB;AACN,IAAC,IAAY,OAAO;;;AAIxB,UAAQ,SAAS;;AAGnB,QAAO;;;;;;;;;AAUT,SAAgB,eACd,gBACA,SACW;CACX,MAAM,SAAS,kBAAkB,IAC9B,MAAM,IAAI,CACV,QAAO,QAAO,SAAS,IAAI,IAAI,QAAQ,IAAI,MAAM,CAAC,CAAC;CAEtD,MAAM,mBAAmB,MAAM,OAAO,IAAI;CAE1C,IAAI,OAAO;CACX,IAAI,QAAQ;CACZ,MAAM,eAAe,iBAAiB,MAAM,IAAI;AAChD,KAAI,aAAa,SAAS,GAAG;AAC3B,SAAO,aAAa,OAAO;AAE3B,UAAQ,aAAa,KAAK,IAAI;OAE9B,SAAQ;AAGV,KAAI;AACF,UACE,SAAS,WAAW,QAChB,SACC,SAAS,UAAU,oBAAoB,MAAM;SAC9C;CAIR,MAAMA,SAAoB;EACxB;EACA;EACD;AAED,MAAK,MAAM,QAAQ,OAAO;EACxB,MAAM,QAAQ,KAAK,MAAM,IAAI;EAC7B,MAAM,WAAW,MAAM,OAAO,IAAI,IAAI,WAAW,CAAC,aAAa;EAC/D,MAAM,YAAY,MAAM,KAAK,IAAI;AACjC,UAAQ,SAAR;GACE,KAAK;AACH,WAAO,UAAU,IAAI,KAAK,UAAU;AACpC;GAEF,KAAK;AACH,WAAO,SAAS,OAAO,SAAS,WAAW,GAAG;AAC9C;GAEF,KAAK;AACH,WAAO,SAAS;AAChB;GAEF,KAAK;AACH,WAAO,WAAW;AAClB;GAEF,KAAK;AACH,WAAO,WAAW;AAClB;GAEF,QACE,QAAO,WAAW;;;AAKxB,QAAO"}
|
package/dist/split.cjs
CHANGED
|
@@ -1 +1,47 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_is_string = require('./type-checks/src/is-string.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/split.ts
|
|
4
|
+
function splitSetCookieString(strCookie) {
|
|
5
|
+
if (Array.isArray(strCookie)) return strCookie.flatMap((c) => splitSetCookieString(c));
|
|
6
|
+
if (!require_is_string.isString(strCookie)) return [];
|
|
7
|
+
const cookiesStrings = [];
|
|
8
|
+
let pos = 0;
|
|
9
|
+
let start;
|
|
10
|
+
let ch;
|
|
11
|
+
let lastComma;
|
|
12
|
+
let nextStart;
|
|
13
|
+
let cookiesSeparatorFound;
|
|
14
|
+
const skipWhitespace = () => {
|
|
15
|
+
while (pos < strCookie.length && /\s/.test(strCookie.charAt(pos))) pos += 1;
|
|
16
|
+
return pos < strCookie.length;
|
|
17
|
+
};
|
|
18
|
+
const notSpecialChar = () => {
|
|
19
|
+
ch = strCookie.charAt(pos);
|
|
20
|
+
return ch !== "=" && ch !== ";" && ch !== ",";
|
|
21
|
+
};
|
|
22
|
+
while (pos < strCookie.length) {
|
|
23
|
+
start = pos;
|
|
24
|
+
cookiesSeparatorFound = false;
|
|
25
|
+
while (skipWhitespace()) {
|
|
26
|
+
ch = strCookie.charAt(pos);
|
|
27
|
+
if (ch === ",") {
|
|
28
|
+
lastComma = pos;
|
|
29
|
+
pos += 1;
|
|
30
|
+
skipWhitespace();
|
|
31
|
+
nextStart = pos;
|
|
32
|
+
while (pos < strCookie.length && notSpecialChar()) pos += 1;
|
|
33
|
+
if (pos < strCookie.length && strCookie.charAt(pos) === "=") {
|
|
34
|
+
cookiesSeparatorFound = true;
|
|
35
|
+
pos = nextStart;
|
|
36
|
+
cookiesStrings.push(strCookie.slice(start, lastComma));
|
|
37
|
+
start = pos;
|
|
38
|
+
} else pos = lastComma + 1;
|
|
39
|
+
} else pos += 1;
|
|
40
|
+
}
|
|
41
|
+
if (!cookiesSeparatorFound || pos >= strCookie.length) cookiesStrings.push(strCookie.slice(start));
|
|
42
|
+
}
|
|
43
|
+
return cookiesStrings;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
//#endregion
|
|
47
|
+
exports.splitSetCookieString = splitSetCookieString;
|
package/dist/split.mjs
CHANGED
|
@@ -1,2 +1,48 @@
|
|
|
1
|
-
import{isString
|
|
1
|
+
import { isString } from "./type-checks/src/is-string.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/split.ts
|
|
4
|
+
function splitSetCookieString(strCookie) {
|
|
5
|
+
if (Array.isArray(strCookie)) return strCookie.flatMap((c) => splitSetCookieString(c));
|
|
6
|
+
if (!isString(strCookie)) return [];
|
|
7
|
+
const cookiesStrings = [];
|
|
8
|
+
let pos = 0;
|
|
9
|
+
let start;
|
|
10
|
+
let ch;
|
|
11
|
+
let lastComma;
|
|
12
|
+
let nextStart;
|
|
13
|
+
let cookiesSeparatorFound;
|
|
14
|
+
const skipWhitespace = () => {
|
|
15
|
+
while (pos < strCookie.length && /\s/.test(strCookie.charAt(pos))) pos += 1;
|
|
16
|
+
return pos < strCookie.length;
|
|
17
|
+
};
|
|
18
|
+
const notSpecialChar = () => {
|
|
19
|
+
ch = strCookie.charAt(pos);
|
|
20
|
+
return ch !== "=" && ch !== ";" && ch !== ",";
|
|
21
|
+
};
|
|
22
|
+
while (pos < strCookie.length) {
|
|
23
|
+
start = pos;
|
|
24
|
+
cookiesSeparatorFound = false;
|
|
25
|
+
while (skipWhitespace()) {
|
|
26
|
+
ch = strCookie.charAt(pos);
|
|
27
|
+
if (ch === ",") {
|
|
28
|
+
lastComma = pos;
|
|
29
|
+
pos += 1;
|
|
30
|
+
skipWhitespace();
|
|
31
|
+
nextStart = pos;
|
|
32
|
+
while (pos < strCookie.length && notSpecialChar()) pos += 1;
|
|
33
|
+
if (pos < strCookie.length && strCookie.charAt(pos) === "=") {
|
|
34
|
+
cookiesSeparatorFound = true;
|
|
35
|
+
pos = nextStart;
|
|
36
|
+
cookiesStrings.push(strCookie.slice(start, lastComma));
|
|
37
|
+
start = pos;
|
|
38
|
+
} else pos = lastComma + 1;
|
|
39
|
+
} else pos += 1;
|
|
40
|
+
}
|
|
41
|
+
if (!cookiesSeparatorFound || pos >= strCookie.length) cookiesStrings.push(strCookie.slice(start));
|
|
42
|
+
}
|
|
43
|
+
return cookiesStrings;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
//#endregion
|
|
47
|
+
export { splitSetCookieString };
|
|
2
48
|
//# sourceMappingURL=split.mjs.map
|
package/dist/split.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"split.mjs","names":["cookiesStrings: string[]","pos: number","start: number","ch: string","lastComma: number","nextStart: number","cookiesSeparatorFound: boolean"],"sources":["../src/split.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isString } from \"@stryke/type-checks/is-string\";\n\n/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nexport function splitSetCookieString(strCookie: string | string[]): string[] {\n if (Array.isArray(strCookie)) {\n return strCookie.flatMap(c => splitSetCookieString(c));\n }\n\n if (!isString(strCookie)) {\n return [];\n }\n\n const cookiesStrings: string[] = [];\n let pos: number = 0;\n let start: number;\n let ch: string;\n let lastComma: number;\n let nextStart: number;\n let cookiesSeparatorFound: boolean;\n\n const skipWhitespace = () => {\n while (pos < strCookie.length && /\\s/.test(strCookie.charAt(pos))) {\n pos += 1;\n }\n return pos < strCookie.length;\n };\n\n const notSpecialChar = () => {\n ch = strCookie.charAt(pos);\n return ch !== \"=\" && ch !== \";\" && ch !== \",\";\n };\n\n while (pos < strCookie.length) {\n start = pos;\n cookiesSeparatorFound = false;\n\n while (skipWhitespace()) {\n ch = strCookie.charAt(pos);\n if (ch === \",\") {\n // ',' is a cookie separator if we have later first '=', not ';' or ','\n lastComma = pos;\n pos += 1;\n\n skipWhitespace();\n nextStart = pos;\n\n while (pos < strCookie.length && notSpecialChar()) {\n pos += 1;\n }\n\n // currently special character\n if (pos < strCookie.length && strCookie.charAt(pos) === \"=\") {\n // we found cookies separator\n cookiesSeparatorFound = true;\n // pos is inside the next cookie, so back up and return it.\n pos = nextStart;\n cookiesStrings.push(strCookie.slice(start, lastComma));\n start = pos;\n } else {\n // in param ',' or param separator ';',\n // we continue from that comma\n pos = lastComma + 1;\n }\n } else {\n pos += 1;\n }\n }\n\n if (!cookiesSeparatorFound || pos >= strCookie.length) {\n cookiesStrings.push(strCookie.slice(start));\n }\n }\n\n return cookiesStrings;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"split.mjs","names":["cookiesStrings: string[]","pos: number","start: number","ch: string","lastComma: number","nextStart: number","cookiesSeparatorFound: boolean"],"sources":["../src/split.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isString } from \"@stryke/type-checks/is-string\";\n\n/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nexport function splitSetCookieString(strCookie: string | string[]): string[] {\n if (Array.isArray(strCookie)) {\n return strCookie.flatMap(c => splitSetCookieString(c));\n }\n\n if (!isString(strCookie)) {\n return [];\n }\n\n const cookiesStrings: string[] = [];\n let pos: number = 0;\n let start: number;\n let ch: string;\n let lastComma: number;\n let nextStart: number;\n let cookiesSeparatorFound: boolean;\n\n const skipWhitespace = () => {\n while (pos < strCookie.length && /\\s/.test(strCookie.charAt(pos))) {\n pos += 1;\n }\n return pos < strCookie.length;\n };\n\n const notSpecialChar = () => {\n ch = strCookie.charAt(pos);\n return ch !== \"=\" && ch !== \";\" && ch !== \",\";\n };\n\n while (pos < strCookie.length) {\n start = pos;\n cookiesSeparatorFound = false;\n\n while (skipWhitespace()) {\n ch = strCookie.charAt(pos);\n if (ch === \",\") {\n // ',' is a cookie separator if we have later first '=', not ';' or ','\n lastComma = pos;\n pos += 1;\n\n skipWhitespace();\n nextStart = pos;\n\n while (pos < strCookie.length && notSpecialChar()) {\n pos += 1;\n }\n\n // currently special character\n if (pos < strCookie.length && strCookie.charAt(pos) === \"=\") {\n // we found cookies separator\n cookiesSeparatorFound = true;\n // pos is inside the next cookie, so back up and return it.\n pos = nextStart;\n cookiesStrings.push(strCookie.slice(start, lastComma));\n start = pos;\n } else {\n // in param ',' or param separator ';',\n // we continue from that comma\n pos = lastComma + 1;\n }\n } else {\n pos += 1;\n }\n }\n\n if (!cookiesSeparatorFound || pos >= strCookie.length) {\n cookiesStrings.push(strCookie.slice(start));\n }\n }\n\n return cookiesStrings;\n}\n"],"mappings":";;;AAsCA,SAAgB,qBAAqB,WAAwC;AAC3E,KAAI,MAAM,QAAQ,UAAU,CAC1B,QAAO,UAAU,SAAQ,MAAK,qBAAqB,EAAE,CAAC;AAGxD,KAAI,CAAC,SAAS,UAAU,CACtB,QAAO,EAAE;CAGX,MAAMA,iBAA2B,EAAE;CACnC,IAAIC,MAAc;CAClB,IAAIC;CACJ,IAAIC;CACJ,IAAIC;CACJ,IAAIC;CACJ,IAAIC;CAEJ,MAAM,uBAAuB;AAC3B,SAAO,MAAM,UAAU,UAAU,KAAK,KAAK,UAAU,OAAO,IAAI,CAAC,CAC/D,QAAO;AAET,SAAO,MAAM,UAAU;;CAGzB,MAAM,uBAAuB;AAC3B,OAAK,UAAU,OAAO,IAAI;AAC1B,SAAO,OAAO,OAAO,OAAO,OAAO,OAAO;;AAG5C,QAAO,MAAM,UAAU,QAAQ;AAC7B,UAAQ;AACR,0BAAwB;AAExB,SAAO,gBAAgB,EAAE;AACvB,QAAK,UAAU,OAAO,IAAI;AAC1B,OAAI,OAAO,KAAK;AAEd,gBAAY;AACZ,WAAO;AAEP,oBAAgB;AAChB,gBAAY;AAEZ,WAAO,MAAM,UAAU,UAAU,gBAAgB,CAC/C,QAAO;AAIT,QAAI,MAAM,UAAU,UAAU,UAAU,OAAO,IAAI,KAAK,KAAK;AAE3D,6BAAwB;AAExB,WAAM;AACN,oBAAe,KAAK,UAAU,MAAM,OAAO,UAAU,CAAC;AACtD,aAAQ;UAIR,OAAM,YAAY;SAGpB,QAAO;;AAIX,MAAI,CAAC,yBAAyB,OAAO,UAAU,OAC7C,gBAAe,KAAK,UAAU,MAAM,MAAM,CAAC;;AAI/C,QAAO"}
|
package/dist/stringify.cjs
CHANGED
|
@@ -1 +1,83 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_is_date = require('./type-checks/src/is-date.cjs');
|
|
2
|
+
const require_is_function = require('./type-checks/src/is-function.cjs');
|
|
3
|
+
const require_is_string = require('./type-checks/src/is-string.cjs');
|
|
4
|
+
const require_is_set = require('./type-checks/src/is-set.cjs');
|
|
5
|
+
require('./type-checks/src/index.cjs');
|
|
6
|
+
|
|
7
|
+
//#region src/stringify.ts
|
|
8
|
+
/**
|
|
9
|
+
* RegExp to match field-content in RFC 7230 sec 3.2
|
|
10
|
+
*
|
|
11
|
+
* field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
|
|
12
|
+
* field-vchar = VCHAR / obs-text
|
|
13
|
+
* obs-text = %x80-FF
|
|
14
|
+
*/
|
|
15
|
+
const fieldContentRegExp = /^[\t\u0020-\u007E\u0080-\u00FF]+$/;
|
|
16
|
+
/**
|
|
17
|
+
* Serialize a cookie name-value pair into a `Set-Cookie` header string.
|
|
18
|
+
*
|
|
19
|
+
* @param name - the name for the cookie
|
|
20
|
+
* @param value - value to set the cookie to
|
|
21
|
+
* @param options - object containing serialization options
|
|
22
|
+
* @returns a `Set-Cookie` header string
|
|
23
|
+
*/
|
|
24
|
+
function stringifyCookie(name, value, options) {
|
|
25
|
+
const opt = options ?? {};
|
|
26
|
+
const enc = opt.encode ?? encodeURIComponent;
|
|
27
|
+
if (!require_is_function.isFunction(enc)) throw new Error("option encode is invalid");
|
|
28
|
+
if (!fieldContentRegExp.test(name)) throw new Error("argument name is invalid");
|
|
29
|
+
const encodedValue = enc(value);
|
|
30
|
+
if (encodedValue && !fieldContentRegExp.test(encodedValue)) throw new Error("argument val is invalid");
|
|
31
|
+
let str = `${name}=${encodedValue}`;
|
|
32
|
+
if (!require_is_set.isSet(opt.maxAge)) {
|
|
33
|
+
const maxAge = Number(opt.maxAge);
|
|
34
|
+
if (Number.isNaN(maxAge) || !Number.isFinite(maxAge)) throw new TypeError("option maxAge is invalid");
|
|
35
|
+
str += `; Max-Age=${Math.floor(maxAge)}`;
|
|
36
|
+
}
|
|
37
|
+
if (opt.domain) {
|
|
38
|
+
if (!fieldContentRegExp.test(opt.domain)) throw new Error("option domain is invalid");
|
|
39
|
+
str += `; Domain=${opt.domain}`;
|
|
40
|
+
}
|
|
41
|
+
if (opt.path) {
|
|
42
|
+
if (!fieldContentRegExp.test(opt.path)) throw new Error("option path is invalid");
|
|
43
|
+
str += `; Path=${opt.path}`;
|
|
44
|
+
}
|
|
45
|
+
if (opt.expires) {
|
|
46
|
+
if (!require_is_date.isDate(opt.expires) || Number.isNaN(opt.expires.valueOf())) throw new Error("option expires is invalid");
|
|
47
|
+
str += `; Expires=${opt.expires.toUTCString()}`;
|
|
48
|
+
}
|
|
49
|
+
if (opt.httpOnly) str += "; HttpOnly";
|
|
50
|
+
if (opt.secure) str += "; Secure";
|
|
51
|
+
if (opt.priority) switch (require_is_string.isString(opt.priority) ? opt.priority.toLowerCase() : opt.priority) {
|
|
52
|
+
case "low":
|
|
53
|
+
str += "; Priority=Low";
|
|
54
|
+
break;
|
|
55
|
+
case "medium":
|
|
56
|
+
str += "; Priority=Medium";
|
|
57
|
+
break;
|
|
58
|
+
case "high":
|
|
59
|
+
str += "; Priority=High";
|
|
60
|
+
break;
|
|
61
|
+
default: throw new Error("option priority is invalid");
|
|
62
|
+
}
|
|
63
|
+
if (opt.sameSite) switch (require_is_string.isString(opt.sameSite) ? opt.sameSite.toLowerCase() : opt.sameSite) {
|
|
64
|
+
case true:
|
|
65
|
+
str += "; SameSite=Strict";
|
|
66
|
+
break;
|
|
67
|
+
case "lax":
|
|
68
|
+
str += "; SameSite=Lax";
|
|
69
|
+
break;
|
|
70
|
+
case "strict":
|
|
71
|
+
str += "; SameSite=Strict";
|
|
72
|
+
break;
|
|
73
|
+
case "none":
|
|
74
|
+
str += "; SameSite=None";
|
|
75
|
+
break;
|
|
76
|
+
default: throw new Error("option sameSite is invalid");
|
|
77
|
+
}
|
|
78
|
+
if (opt.partitioned) str += "; Partitioned";
|
|
79
|
+
return str;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
//#endregion
|
|
83
|
+
exports.stringifyCookie = stringifyCookie;
|
package/dist/stringify.mjs
CHANGED
|
@@ -1,2 +1,84 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { isDate } from "./type-checks/src/is-date.mjs";
|
|
2
|
+
import { isFunction } from "./type-checks/src/is-function.mjs";
|
|
3
|
+
import { isString } from "./type-checks/src/is-string.mjs";
|
|
4
|
+
import { isSet } from "./type-checks/src/is-set.mjs";
|
|
5
|
+
import "./type-checks/src/index.mjs";
|
|
6
|
+
|
|
7
|
+
//#region src/stringify.ts
|
|
8
|
+
/**
|
|
9
|
+
* RegExp to match field-content in RFC 7230 sec 3.2
|
|
10
|
+
*
|
|
11
|
+
* field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
|
|
12
|
+
* field-vchar = VCHAR / obs-text
|
|
13
|
+
* obs-text = %x80-FF
|
|
14
|
+
*/
|
|
15
|
+
const fieldContentRegExp = /^[\t\u0020-\u007E\u0080-\u00FF]+$/;
|
|
16
|
+
/**
|
|
17
|
+
* Serialize a cookie name-value pair into a `Set-Cookie` header string.
|
|
18
|
+
*
|
|
19
|
+
* @param name - the name for the cookie
|
|
20
|
+
* @param value - value to set the cookie to
|
|
21
|
+
* @param options - object containing serialization options
|
|
22
|
+
* @returns a `Set-Cookie` header string
|
|
23
|
+
*/
|
|
24
|
+
function stringifyCookie(name, value, options) {
|
|
25
|
+
const opt = options ?? {};
|
|
26
|
+
const enc = opt.encode ?? encodeURIComponent;
|
|
27
|
+
if (!isFunction(enc)) throw new Error("option encode is invalid");
|
|
28
|
+
if (!fieldContentRegExp.test(name)) throw new Error("argument name is invalid");
|
|
29
|
+
const encodedValue = enc(value);
|
|
30
|
+
if (encodedValue && !fieldContentRegExp.test(encodedValue)) throw new Error("argument val is invalid");
|
|
31
|
+
let str = `${name}=${encodedValue}`;
|
|
32
|
+
if (!isSet(opt.maxAge)) {
|
|
33
|
+
const maxAge = Number(opt.maxAge);
|
|
34
|
+
if (Number.isNaN(maxAge) || !Number.isFinite(maxAge)) throw new TypeError("option maxAge is invalid");
|
|
35
|
+
str += `; Max-Age=${Math.floor(maxAge)}`;
|
|
36
|
+
}
|
|
37
|
+
if (opt.domain) {
|
|
38
|
+
if (!fieldContentRegExp.test(opt.domain)) throw new Error("option domain is invalid");
|
|
39
|
+
str += `; Domain=${opt.domain}`;
|
|
40
|
+
}
|
|
41
|
+
if (opt.path) {
|
|
42
|
+
if (!fieldContentRegExp.test(opt.path)) throw new Error("option path is invalid");
|
|
43
|
+
str += `; Path=${opt.path}`;
|
|
44
|
+
}
|
|
45
|
+
if (opt.expires) {
|
|
46
|
+
if (!isDate(opt.expires) || Number.isNaN(opt.expires.valueOf())) throw new Error("option expires is invalid");
|
|
47
|
+
str += `; Expires=${opt.expires.toUTCString()}`;
|
|
48
|
+
}
|
|
49
|
+
if (opt.httpOnly) str += "; HttpOnly";
|
|
50
|
+
if (opt.secure) str += "; Secure";
|
|
51
|
+
if (opt.priority) switch (isString(opt.priority) ? opt.priority.toLowerCase() : opt.priority) {
|
|
52
|
+
case "low":
|
|
53
|
+
str += "; Priority=Low";
|
|
54
|
+
break;
|
|
55
|
+
case "medium":
|
|
56
|
+
str += "; Priority=Medium";
|
|
57
|
+
break;
|
|
58
|
+
case "high":
|
|
59
|
+
str += "; Priority=High";
|
|
60
|
+
break;
|
|
61
|
+
default: throw new Error("option priority is invalid");
|
|
62
|
+
}
|
|
63
|
+
if (opt.sameSite) switch (isString(opt.sameSite) ? opt.sameSite.toLowerCase() : opt.sameSite) {
|
|
64
|
+
case true:
|
|
65
|
+
str += "; SameSite=Strict";
|
|
66
|
+
break;
|
|
67
|
+
case "lax":
|
|
68
|
+
str += "; SameSite=Lax";
|
|
69
|
+
break;
|
|
70
|
+
case "strict":
|
|
71
|
+
str += "; SameSite=Strict";
|
|
72
|
+
break;
|
|
73
|
+
case "none":
|
|
74
|
+
str += "; SameSite=None";
|
|
75
|
+
break;
|
|
76
|
+
default: throw new Error("option sameSite is invalid");
|
|
77
|
+
}
|
|
78
|
+
if (opt.partitioned) str += "; Partitioned";
|
|
79
|
+
return str;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
//#endregion
|
|
83
|
+
export { stringifyCookie };
|
|
2
84
|
//# sourceMappingURL=stringify.mjs.map
|
package/dist/stringify.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stringify.mjs","names":[],"sources":["../src/stringify.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isDate, isFunction, isSet, isString } from \"@stryke/type-checks\";\nimport type { CookieSerializeOptions } from \"./types\";\n\n/**\n * RegExp to match field-content in RFC 7230 sec 3.2\n *\n * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]\n * field-vchar = VCHAR / obs-text\n * obs-text = %x80-FF\n */\n\nconst fieldContentRegExp = /^[\\t\\u0020-\\u007E\\u0080-\\u00FF]+$/;\n\n/**\n * Serialize a cookie name-value pair into a `Set-Cookie` header string.\n *\n * @param name - the name for the cookie\n * @param value - value to set the cookie to\n * @param options - object containing serialization options\n * @returns a `Set-Cookie` header string\n */\nexport function stringifyCookie(\n name: string,\n value: string,\n options?: CookieSerializeOptions\n): string {\n const opt = options ?? {};\n const enc = opt.encode ?? encodeURIComponent;\n\n if (!isFunction(enc)) {\n throw new Error(\"option encode is invalid\");\n }\n\n if (!fieldContentRegExp.test(name)) {\n throw new Error(\"argument name is invalid\");\n }\n\n const encodedValue = enc(value);\n\n if (encodedValue && !fieldContentRegExp.test(encodedValue)) {\n throw new Error(\"argument val is invalid\");\n }\n\n let str = `${name}=${encodedValue}`;\n\n if (!isSet(opt.maxAge)) {\n const maxAge = Number(opt.maxAge);\n\n if (Number.isNaN(maxAge) || !Number.isFinite(maxAge)) {\n throw new TypeError(\"option maxAge is invalid\");\n }\n\n str += `; Max-Age=${Math.floor(maxAge)}`;\n }\n\n if (opt.domain) {\n if (!fieldContentRegExp.test(opt.domain)) {\n throw new Error(\"option domain is invalid\");\n }\n\n str += `; Domain=${opt.domain}`;\n }\n\n if (opt.path) {\n if (!fieldContentRegExp.test(opt.path)) {\n throw new Error(\"option path is invalid\");\n }\n\n str += `; Path=${opt.path}`;\n }\n\n if (opt.expires) {\n if (!isDate(opt.expires) || Number.isNaN(opt.expires.valueOf())) {\n throw new Error(\"option expires is invalid\");\n }\n\n str += `; Expires=${opt.expires.toUTCString()}`;\n }\n\n if (opt.httpOnly) {\n str += \"; HttpOnly\";\n }\n\n if (opt.secure) {\n str += \"; Secure\";\n }\n\n if (opt.priority) {\n const priority = isString(opt.priority)\n ? opt.priority.toLowerCase()\n : opt.priority;\n\n switch (priority) {\n case \"low\": {\n str += \"; Priority=Low\";\n break;\n }\n case \"medium\": {\n str += \"; Priority=Medium\";\n break;\n }\n case \"high\": {\n str += \"; Priority=High\";\n break;\n }\n default: {\n throw new Error(\"option priority is invalid\");\n }\n }\n }\n\n if (opt.sameSite) {\n const sameSite = isString(opt.sameSite)\n ? opt.sameSite.toLowerCase()\n : opt.sameSite;\n\n switch (sameSite) {\n case true: {\n str += \"; SameSite=Strict\";\n break;\n }\n case \"lax\": {\n str += \"; SameSite=Lax\";\n break;\n }\n case \"strict\": {\n str += \"; SameSite=Strict\";\n break;\n }\n case \"none\": {\n str += \"; SameSite=None\";\n break;\n }\n default: {\n throw new Error(\"option sameSite is invalid\");\n }\n }\n }\n\n if (opt.partitioned) {\n str += \"; Partitioned\";\n }\n\n return str;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"stringify.mjs","names":[],"sources":["../src/stringify.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isDate, isFunction, isSet, isString } from \"@stryke/type-checks\";\nimport type { CookieSerializeOptions } from \"./types\";\n\n/**\n * RegExp to match field-content in RFC 7230 sec 3.2\n *\n * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]\n * field-vchar = VCHAR / obs-text\n * obs-text = %x80-FF\n */\n\nconst fieldContentRegExp = /^[\\t\\u0020-\\u007E\\u0080-\\u00FF]+$/;\n\n/**\n * Serialize a cookie name-value pair into a `Set-Cookie` header string.\n *\n * @param name - the name for the cookie\n * @param value - value to set the cookie to\n * @param options - object containing serialization options\n * @returns a `Set-Cookie` header string\n */\nexport function stringifyCookie(\n name: string,\n value: string,\n options?: CookieSerializeOptions\n): string {\n const opt = options ?? {};\n const enc = opt.encode ?? encodeURIComponent;\n\n if (!isFunction(enc)) {\n throw new Error(\"option encode is invalid\");\n }\n\n if (!fieldContentRegExp.test(name)) {\n throw new Error(\"argument name is invalid\");\n }\n\n const encodedValue = enc(value);\n\n if (encodedValue && !fieldContentRegExp.test(encodedValue)) {\n throw new Error(\"argument val is invalid\");\n }\n\n let str = `${name}=${encodedValue}`;\n\n if (!isSet(opt.maxAge)) {\n const maxAge = Number(opt.maxAge);\n\n if (Number.isNaN(maxAge) || !Number.isFinite(maxAge)) {\n throw new TypeError(\"option maxAge is invalid\");\n }\n\n str += `; Max-Age=${Math.floor(maxAge)}`;\n }\n\n if (opt.domain) {\n if (!fieldContentRegExp.test(opt.domain)) {\n throw new Error(\"option domain is invalid\");\n }\n\n str += `; Domain=${opt.domain}`;\n }\n\n if (opt.path) {\n if (!fieldContentRegExp.test(opt.path)) {\n throw new Error(\"option path is invalid\");\n }\n\n str += `; Path=${opt.path}`;\n }\n\n if (opt.expires) {\n if (!isDate(opt.expires) || Number.isNaN(opt.expires.valueOf())) {\n throw new Error(\"option expires is invalid\");\n }\n\n str += `; Expires=${opt.expires.toUTCString()}`;\n }\n\n if (opt.httpOnly) {\n str += \"; HttpOnly\";\n }\n\n if (opt.secure) {\n str += \"; Secure\";\n }\n\n if (opt.priority) {\n const priority = isString(opt.priority)\n ? opt.priority.toLowerCase()\n : opt.priority;\n\n switch (priority) {\n case \"low\": {\n str += \"; Priority=Low\";\n break;\n }\n case \"medium\": {\n str += \"; Priority=Medium\";\n break;\n }\n case \"high\": {\n str += \"; Priority=High\";\n break;\n }\n default: {\n throw new Error(\"option priority is invalid\");\n }\n }\n }\n\n if (opt.sameSite) {\n const sameSite = isString(opt.sameSite)\n ? opt.sameSite.toLowerCase()\n : opt.sameSite;\n\n switch (sameSite) {\n case true: {\n str += \"; SameSite=Strict\";\n break;\n }\n case \"lax\": {\n str += \"; SameSite=Lax\";\n break;\n }\n case \"strict\": {\n str += \"; SameSite=Strict\";\n break;\n }\n case \"none\": {\n str += \"; SameSite=None\";\n break;\n }\n default: {\n throw new Error(\"option sameSite is invalid\");\n }\n }\n }\n\n if (opt.partitioned) {\n str += \"; Partitioned\";\n }\n\n return str;\n}\n"],"mappings":";;;;;;;;;;;;;;AA6BA,MAAM,qBAAqB;;;;;;;;;AAU3B,SAAgB,gBACd,MACA,OACA,SACQ;CACR,MAAM,MAAM,WAAW,EAAE;CACzB,MAAM,MAAM,IAAI,UAAU;AAE1B,KAAI,CAAC,WAAW,IAAI,CAClB,OAAM,IAAI,MAAM,2BAA2B;AAG7C,KAAI,CAAC,mBAAmB,KAAK,KAAK,CAChC,OAAM,IAAI,MAAM,2BAA2B;CAG7C,MAAM,eAAe,IAAI,MAAM;AAE/B,KAAI,gBAAgB,CAAC,mBAAmB,KAAK,aAAa,CACxD,OAAM,IAAI,MAAM,0BAA0B;CAG5C,IAAI,MAAM,GAAG,KAAK,GAAG;AAErB,KAAI,CAAC,MAAM,IAAI,OAAO,EAAE;EACtB,MAAM,SAAS,OAAO,IAAI,OAAO;AAEjC,MAAI,OAAO,MAAM,OAAO,IAAI,CAAC,OAAO,SAAS,OAAO,CAClD,OAAM,IAAI,UAAU,2BAA2B;AAGjD,SAAO,aAAa,KAAK,MAAM,OAAO;;AAGxC,KAAI,IAAI,QAAQ;AACd,MAAI,CAAC,mBAAmB,KAAK,IAAI,OAAO,CACtC,OAAM,IAAI,MAAM,2BAA2B;AAG7C,SAAO,YAAY,IAAI;;AAGzB,KAAI,IAAI,MAAM;AACZ,MAAI,CAAC,mBAAmB,KAAK,IAAI,KAAK,CACpC,OAAM,IAAI,MAAM,yBAAyB;AAG3C,SAAO,UAAU,IAAI;;AAGvB,KAAI,IAAI,SAAS;AACf,MAAI,CAAC,OAAO,IAAI,QAAQ,IAAI,OAAO,MAAM,IAAI,QAAQ,SAAS,CAAC,CAC7D,OAAM,IAAI,MAAM,4BAA4B;AAG9C,SAAO,aAAa,IAAI,QAAQ,aAAa;;AAG/C,KAAI,IAAI,SACN,QAAO;AAGT,KAAI,IAAI,OACN,QAAO;AAGT,KAAI,IAAI,SAKN,SAJiB,SAAS,IAAI,SAAS,GACnC,IAAI,SAAS,aAAa,GAC1B,IAAI,UAER;EACE,KAAK;AACH,UAAO;AACP;EAEF,KAAK;AACH,UAAO;AACP;EAEF,KAAK;AACH,UAAO;AACP;EAEF,QACE,OAAM,IAAI,MAAM,6BAA6B;;AAKnD,KAAI,IAAI,SAKN,SAJiB,SAAS,IAAI,SAAS,GACnC,IAAI,SAAS,aAAa,GAC1B,IAAI,UAER;EACE,KAAK;AACH,UAAO;AACP;EAEF,KAAK;AACH,UAAO;AACP;EAEF,KAAK;AACH,UAAO;AACP;EAEF,KAAK;AACH,UAAO;AACP;EAEF,QACE,OAAM,IAAI,MAAM,6BAA6B;;AAKnD,KAAI,IAAI,YACN,QAAO;AAGT,QAAO"}
|
|
@@ -1 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
//#region ../type-checks/src/get-object-tag.ts
|
|
3
|
+
/**
|
|
4
|
+
* Gets the `toStringTag` of `obj`.
|
|
5
|
+
*
|
|
6
|
+
* @param value - The obj to query.
|
|
7
|
+
* @returns Returns the `toStringTag`.
|
|
8
|
+
*/
|
|
9
|
+
const getObjectTag = (value) => {
|
|
10
|
+
if (value == null) return value === void 0 ? "[object Undefined]" : "[object Null]";
|
|
11
|
+
return Object.prototype.toString.call(value);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
exports.getObjectTag = getObjectTag;
|