cookie-parser

function cookieParser(secret, options) {

? return function cookieParser(req, res, next) {

? ? if (req.cookies) {

? ? ? return next();

? ? }

? ? var cookies = req.headers.cookie;

? ? var secrets = !secret || Array.isArray(secret)

? ? ? ? (secret || [])

? ? ? : [secret];

? ? req.secret = secrets[0];

? ? req.cookies = Object.create(null);

? ? req.signedCookies = Object.create(null);

? ? // no cookies

? ? if (!cookies) {

? ? ? return next();

? ? }

? ? req.cookies = cookie.parse(cookies, options);

? ? // parse signed cookies

? ? if (secrets.length !== 0) {

? ? ? req.signedCookies = signedCookies(req.cookies, secrets);

? ? ? req.signedCookies = JSONCookies(req.signedCookies);

? ? }

? ? // parse JSON cookies

? ? req.cookies = JSONCookies(req.cookies);

? ? next();

? };

}

res.cookie = function (name, value, options) {

? var opts = merge({}, options);

? var secret = this.req.secret;

? var signed = opts.signed;

? if (signed && !secret) {

? ? throw new Error('cookieParser("secret") required for signed cookies');

? }

? var val = typeof value === 'object'

? ? ? 'j:' + JSON.stringify(value)

? ? : String(value);

? if (signed) {

? ? val = 's:' + sign(val, secret);

? }

? if ('maxAge' in opts) {

? ? opts.expires = new Date(Date.now() + opts.maxAge);

? ? opts.maxAge /= 1000;

? }

? if (opts.path == null) {

? ? opts.path = '/';

? }

? this.append('Set-Cookie', cookie.serialize(name, String(val), opts));

? return this;

};

var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;

function serialize(name, val, options) {

? var opt = options || {};

? var enc = opt.encode || encode;

? if (typeof enc !== 'function') {

? ? throw new TypeError('option encode is invalid');

? }

? if (!fieldContentRegExp.test(name)) {

? ? throw new TypeError('argument name is invalid');

? }

? var value = enc(val);

? if (value && !fieldContentRegExp.test(value)) {

? ? throw new TypeError('argument val is invalid');

? }

? var str = name + '=' + value;

? if (null != opt.maxAge) {

? ? var maxAge = opt.maxAge - 0;

? ? if (isNaN(maxAge)) throw new Error('maxAge should be a Number');

? ? str += '; Max-Age=' + Math.floor(maxAge);

? }

? if (opt.domain) {

? ? if (!fieldContentRegExp.test(opt.domain)) {

? ? ? throw new TypeError('option domain is invalid');

? ? }

? ? str += '; Domain=' + opt.domain;

? }

? if (opt.path) {

? ? if (!fieldContentRegExp.test(opt.path)) {

? ? ? throw new TypeError('option path is invalid');

? ? }

? ? str += '; Path=' + opt.path;

? }

? 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) {

? ? var 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;

? ? ? default:

? ? ? ? throw new TypeError('option sameSite is invalid');

? ? }

? }

? return str;

}

function JSONCookies(obj) {

? var cookies = Object.keys(obj);

? var key;

? var val;

? for (var i = 0; i < cookies.length; i++) {

? ? key = cookies[i];

? ? val = JSONCookie(obj[key]);

? ? if (val) {

? ? ? obj[key] = val;

? ? }

? }

? return obj;

}

function signedCookie(str, secret) {

? if (typeof str !== 'string') {

? ? return undefined;

? }

? if (str.substr(0, 2) !== 's:') {

? ? return str;

? }

? var secrets = !secret || Array.isArray(secret)

? ? ? (secret || [])

? ? : [secret];

? for (var i = 0; i < secrets.length; i++) {

? ? var val = signature.unsign(str.slice(2), secrets[i]);

? ? if (val !== false) {

? ? ? return val;

? ? }

? }

? return false;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容