egg-cookies
Version:
cookies module for egg
100 lines (87 loc) • 3.15 kB
JavaScript
'use strict';
const assert = require('assert');
/**
* 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
*/
const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/; // eslint-disable-line no-control-regex
/**
* RegExp to match Same-Site cookie attribute value.
* https://en.wikipedia.org/wiki/HTTP_cookie#SameSite_cookie
*/
const sameSiteRegExp = /^(?:none|lax|strict)$/i;
/**
* RegExp to match Priority cookie attribute value.
*/
const PRIORITY_REGEXP = /^(?:low|medium|high)$/i;
class Cookie {
constructor(name, value, attrs) {
assert(fieldContentRegExp.test(name), 'argument name is invalid');
assert(!value || fieldContentRegExp.test(value), 'argument value is invalid');
this.name = name;
this.value = value || '';
this.attrs = mergeDefaultAttrs(attrs);
assert(!this.attrs.path || fieldContentRegExp.test(this.attrs.path),
'argument option path is invalid');
if (typeof this.attrs.domain === 'function') {
this.attrs.domain = this.attrs.domain();
}
assert(!this.attrs.domain || fieldContentRegExp.test(this.attrs.domain),
'argument option domain is invalid');
assert(!this.attrs.sameSite || this.attrs.sameSite === true || sameSiteRegExp.test(this.attrs.sameSite),
'argument option sameSite is invalid');
assert(!this.attrs.priority || PRIORITY_REGEXP.test(this.attrs.priority), 'argument option priority is invalid');
if (!value) {
this.attrs.expires = new Date(0);
// make sure maxAge is empty
this.attrs.maxAge = null;
}
}
toString() {
return this.name + '=' + this.value;
}
toHeader() {
let header = this.toString();
const attrs = this.attrs;
if (attrs.path) header += '; path=' + attrs.path;
const maxAge = parseInt(attrs.maxAge, 10);
// ignore 0, `session` and other invalid maxAge
if (maxAge) {
header += '; max-age=' + Math.round(maxAge / 1000);
attrs.expires = new Date(Date.now() + maxAge);
}
if (attrs.expires) header += '; expires=' + attrs.expires.toUTCString();
if (attrs.domain) header += '; domain=' + attrs.domain;
if (attrs.priority) header += '; priority=' + attrs.priority.toLowerCase();
if (attrs.sameSite) header += '; samesite=' + (attrs.sameSite === true ? 'strict' : attrs.sameSite.toLowerCase());
if (attrs.secure) header += '; secure';
if (attrs.httpOnly) header += '; httponly';
if (attrs.partitioned) header += '; partitioned';
return header;
}
}
const ATTRS = [
'path', 'expires', 'domain', 'httpOnly', 'secure', 'partitioned', 'maxAge', 'overwrite', 'sameSite',
'priority',
];
function mergeDefaultAttrs(attrs) {
const merged = {
path: '/',
httpOnly: true,
secure: false,
overwrite: false,
sameSite: false,
partitioned: false,
priority: undefined,
};
if (!attrs) return merged;
for (let i = 0; i < ATTRS.length; i++) {
const key = ATTRS[i];
if (key in attrs) merged[key] = attrs[key];
}
return merged;
}
module.exports = Cookie;