@tinyhttp/cookie
Advanced tools
Comparing version 0.1.18 to 0.1.19
# @tinyhttp/cookie | ||
## 0.1.19 | ||
### Patch Changes | ||
- Split app into req, res, send and router | ||
## 0.1.18 | ||
@@ -4,0 +10,0 @@ |
@@ -8,6 +8,6 @@ /** | ||
*/ | ||
declare function parse(str: string, options?: { | ||
export declare function parse(str: string, options?: { | ||
decode: (str: string) => any; | ||
}): {}; | ||
declare type SerializeOptions = Partial<{ | ||
export declare type SerializeOptions = Partial<{ | ||
encode: (str: string) => string; | ||
@@ -22,4 +22,2 @@ maxAge: number; | ||
}>; | ||
declare function serialize(name: string, val: string, opt?: SerializeOptions): string; | ||
export { SerializeOptions, parse, serialize }; | ||
export declare function serialize(name: string, val: string, opt?: SerializeOptions): string; |
@@ -1,96 +0,112 @@ | ||
// src/index.ts | ||
const pairSplitRegExp = /; */; | ||
/** | ||
* RegExp to match field-content in RFC 7230 sec 3.2 | ||
* | ||
* field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] | ||
* field-vchar = VCHAR / obs-text | ||
* obs-text = %x80-FF | ||
*/ | ||
// eslint-disable-next-line no-control-regex | ||
const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/; | ||
function tryDecode(str, decode = decodeURIComponent) { | ||
try { | ||
return decode(str); | ||
} catch (e) { | ||
return str; | ||
} | ||
try { | ||
return decode(str); | ||
} | ||
catch (e) { | ||
return str; | ||
} | ||
} | ||
/** | ||
* Parse a cookie header. | ||
* | ||
* Parse the given cookie header string into an object | ||
* The object has the various cookies as keys(names) => values | ||
* | ||
*/ | ||
function parse(str, options = { | ||
decode: decodeURIComponent | ||
decode: decodeURIComponent, | ||
}) { | ||
const obj = {}; | ||
const pairs = str.split(pairSplitRegExp); | ||
for (const pair of pairs) { | ||
let eqIdx = pair.indexOf("="); | ||
if (eqIdx < 0) { | ||
continue; | ||
const obj = {}; | ||
const pairs = str.split(pairSplitRegExp); | ||
for (const pair of pairs) { | ||
let eqIdx = pair.indexOf('='); | ||
// skip things that don't look like key=value | ||
if (eqIdx < 0) { | ||
continue; | ||
} | ||
const key = pair.substr(0, eqIdx).trim(); | ||
let val = pair.substr(++eqIdx, pair.length).trim(); | ||
// quoted values | ||
if ('"' == val[0]) | ||
val = val.slice(1, -1); | ||
// only assign once | ||
if (obj[key] == null) | ||
obj[key] = tryDecode(val, options.decode); | ||
} | ||
const key = pair.substr(0, eqIdx).trim(); | ||
let val = pair.substr(++eqIdx, pair.length).trim(); | ||
if (val[0] == '"') | ||
val = val.slice(1, -1); | ||
if (obj[key] == null) | ||
obj[key] = tryDecode(val, options.decode); | ||
} | ||
return obj; | ||
return obj; | ||
} | ||
function serialize(name, val, opt = {}) { | ||
if (!opt.encode) | ||
opt.encode = encodeURIComponent; | ||
if (!fieldContentRegExp.test(name)) { | ||
throw new TypeError("argument name is invalid"); | ||
} | ||
const value = opt.encode(val); | ||
if (value && !fieldContentRegExp.test(value)) { | ||
throw new TypeError("argument val is invalid"); | ||
} | ||
let str = name + "=" + value; | ||
if (opt.maxAge != null) { | ||
const maxAge = opt.maxAge - 0; | ||
if (isNaN(maxAge) || !isFinite(maxAge)) { | ||
throw new TypeError("option maxAge is invalid"); | ||
if (!opt.encode) | ||
opt.encode = encodeURIComponent; | ||
if (!fieldContentRegExp.test(name)) { | ||
throw new TypeError('argument name is invalid'); | ||
} | ||
str += "; Max-Age=" + Math.floor(maxAge); | ||
} | ||
if (opt.domain) { | ||
if (!fieldContentRegExp.test(opt.domain)) { | ||
throw new TypeError("option domain is invalid"); | ||
const value = opt.encode(val); | ||
if (value && !fieldContentRegExp.test(value)) { | ||
throw new TypeError('argument val is invalid'); | ||
} | ||
str += "; Domain=" + opt.domain; | ||
} | ||
if (opt.path) { | ||
if (!fieldContentRegExp.test(opt.path)) { | ||
throw new TypeError("option path is invalid"); | ||
let str = name + '=' + value; | ||
if (null != opt.maxAge) { | ||
const maxAge = opt.maxAge - 0; | ||
if (isNaN(maxAge) || !isFinite(maxAge)) { | ||
throw new TypeError('option maxAge is invalid'); | ||
} | ||
str += '; Max-Age=' + Math.floor(maxAge); | ||
} | ||
str += "; Path=" + opt.path; | ||
} | ||
if (opt.expires) { | ||
if (typeof opt.expires.toUTCString !== "function") { | ||
throw new TypeError("option expires is invalid"); | ||
if (opt.domain) { | ||
if (!fieldContentRegExp.test(opt.domain)) { | ||
throw new TypeError('option domain is invalid'); | ||
} | ||
str += '; Domain=' + opt.domain; | ||
} | ||
str += "; Expires=" + opt.expires.toUTCString(); | ||
} | ||
if (opt.httpOnly) { | ||
str += "; HttpOnly"; | ||
} | ||
if (opt.secure) { | ||
str += "; Secure"; | ||
} | ||
if (opt.sameSite) { | ||
const sameSite = typeof opt.sameSite === "string" ? opt.sameSite.toLowerCase() : opt.sameSite; | ||
switch (sameSite) { | ||
case true: | ||
str += "; SameSite=Strict"; | ||
break; | ||
case "lax": | ||
str += "; SameSite=Lax"; | ||
break; | ||
case "strict": | ||
str += "; SameSite=Strict"; | ||
break; | ||
case "none": | ||
str += "; SameSite=None"; | ||
break; | ||
default: | ||
throw new TypeError("option sameSite is invalid"); | ||
if (opt.path) { | ||
if (!fieldContentRegExp.test(opt.path)) { | ||
throw new TypeError('option path is invalid'); | ||
} | ||
str += '; Path=' + opt.path; | ||
} | ||
} | ||
return str; | ||
if (opt.expires) { | ||
if (typeof opt.expires.toUTCString !== 'function') { | ||
throw new TypeError('option expires is invalid'); | ||
} | ||
str += '; Expires=' + opt.expires.toUTCString(); | ||
} | ||
if (opt.httpOnly) { | ||
str += '; HttpOnly'; | ||
} | ||
if (opt.secure) { | ||
str += '; Secure'; | ||
} | ||
if (opt.sameSite) { | ||
const sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite; | ||
switch (sameSite) { | ||
case true: | ||
str += '; SameSite=Strict'; | ||
break; | ||
case 'lax': | ||
str += '; SameSite=Lax'; | ||
break; | ||
case 'strict': | ||
str += '; SameSite=Strict'; | ||
break; | ||
case 'none': | ||
str += '; SameSite=None'; | ||
break; | ||
default: | ||
throw new TypeError('option sameSite is invalid'); | ||
} | ||
} | ||
return str; | ||
} | ||
export { | ||
parse, | ||
serialize | ||
}; | ||
export { parse, serialize }; |
{ | ||
"name": "@tinyhttp/cookie", | ||
"version": "0.1.18", | ||
"version": "0.1.19", | ||
"type": "module", | ||
@@ -36,10 +36,6 @@ "description": "HTTP cookie parser and serializer for Node.js", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"tsup": "^3.6.1", | ||
"typescript": "^4.0.2" | ||
}, | ||
"scripts": { | ||
"build": "tsup src/index.ts --format cjs,esm --dts", | ||
"build": "rollup -c ../../build/defaultConfig.js", | ||
"prepublishOnly": "pnpm build" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20723
0
370