/node_modules/express/node_modules/connect/lib/middleware/session/cookie.js
JavaScript | 128 lines | 46 code | 21 blank | 61 comment | 3 complexity | 4d6116c2fe8680736321e480f4cb5872 MD5 | raw file
Possible License(s): Apache-2.0, MIT
1 2/*! 3 * Connect - session - Cookie 4 * Copyright(c) 2010 Sencha Inc. 5 * Copyright(c) 2011 TJ Holowaychuk 6 * MIT Licensed 7 */ 8 9/** 10 * Module dependencies. 11 */ 12 13var utils = require('../../utils') 14 , cookie = require('cookie'); 15 16/** 17 * Initialize a new `Cookie` with the given `options`. 18 * 19 * @param {IncomingMessage} req 20 * @param {Object} options 21 * @api private 22 */ 23 24var Cookie = module.exports = function Cookie(options) { 25 this.path = '/'; 26 this.maxAge = null; 27 this.httpOnly = true; 28 if (options) utils.merge(this, options); 29 this.originalMaxAge = undefined == this.originalMaxAge 30 ? this.maxAge 31 : this.originalMaxAge; 32}; 33 34/*! 35 * Prototype. 36 */ 37 38Cookie.prototype = { 39 40 /** 41 * Set expires `date`. 42 * 43 * @param {Date} date 44 * @api public 45 */ 46 47 set expires(date) { 48 this._expires = date; 49 this.originalMaxAge = this.maxAge; 50 }, 51 52 /** 53 * Get expires `date`. 54 * 55 * @return {Date} 56 * @api public 57 */ 58 59 get expires() { 60 return this._expires; 61 }, 62 63 /** 64 * Set expires via max-age in `ms`. 65 * 66 * @param {Number} ms 67 * @api public 68 */ 69 70 set maxAge(ms) { 71 this.expires = 'number' == typeof ms 72 ? new Date(Date.now() + ms) 73 : ms; 74 }, 75 76 /** 77 * Get expires max-age in `ms`. 78 * 79 * @return {Number} 80 * @api public 81 */ 82 83 get maxAge() { 84 return this.expires instanceof Date 85 ? this.expires.valueOf() - Date.now() 86 : this.expires; 87 }, 88 89 /** 90 * Return cookie data object. 91 * 92 * @return {Object} 93 * @api private 94 */ 95 96 get data() { 97 return { 98 originalMaxAge: this.originalMaxAge 99 , expires: this._expires 100 , secure: this.secure 101 , httpOnly: this.httpOnly 102 , domain: this.domain 103 , path: this.path 104 } 105 }, 106 107 /** 108 * Return a serialized cookie string. 109 * 110 * @return {String} 111 * @api public 112 */ 113 114 serialize: function(name, val){ 115 return cookie.serialize(name, val, this.data); 116 }, 117 118 /** 119 * Return JSON representation of this cookie. 120 * 121 * @return {Object} 122 * @api private 123 */ 124 125 toJSON: function(){ 126 return this.data; 127 } 128};