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;

}

你可能感兴趣的:(cookie-parser)