186
Ft
fractal-tree / files
fractal-tree / node_modules / connect / lib / middleware / session / cookie.js
2.0 KB
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 15/** 16 * Initialize a new `Cookie` with the given `options`. 17 * 18 * @param {Object} options 19 * @api private 20 */ 21 22var Cookie = module.exports = function Cookie(options) { 23 this.path = '/'; 24 this.httpOnly = true; 25 this.maxAge = 14400000; 26 if (options) utils.merge(this, options); 27 this.originalMaxAge = undefined == this.originalMaxAge 28 ? this.maxAge 29 : this.originalMaxAge; 30}; 31 32/** 33 * Prototype. 34 */ 35 36Cookie.prototype = { 37 38 /** 39 * Set expires `date`. 40 * 41 * @param {Date} date 42 * @api public 43 */ 44 45 set expires(date) { 46 this._expires = date; 47 this.originalMaxAge = this.maxAge; 48 }, 49 50 /** 51 * Get expires `date`. 52 * 53 * @return {Date} 54 * @api public 55 */ 56 57 get expires() { 58 return this._expires; 59 }, 60 61 /** 62 * Set expires via max-age in `ms`. 63 * 64 * @param {Number} ms 65 * @api public 66 */ 67 68 set maxAge(ms) { 69 this.expires = 'number' == typeof ms 70 ? new Date(Date.now() + ms) 71 : ms; 72 }, 73 74 /** 75 * Get expires max-age in `ms`. 76 * 77 * @return {Number} 78 * @api public 79 */ 80 81 get maxAge() { 82 return this.expires instanceof Date 83 ? this.expires.valueOf() - Date.now() 84 : this.expires; 85 }, 86 87 /** 88 * Return cookie data object. 89 * 90 * @return {Object} 91 * @api private 92 */ 93 94 get data() { 95 return { 96 originalMaxAge: this.originalMaxAge 97 , expires: this._expires 98 , secure: this.secure 99 , httpOnly: this.httpOnly 100 , domain: this.domain 101 , path: this.path 102 } 103 }, 104 105 /** 106 * Return a serialized cookie string. 107 * 108 * @return {String} 109 * @api public 110 */ 111 112 serialize: function(name, val){ 113 return utils.serializeCookie(name, val, this.data); 114 }, 115 116 /** 117 * Return JSON representation of this cookie. 118 * 119 * @return {Object} 120 * @api private 121 */ 122 123 toJSON: function(){ 124 return this.data; 125 } 126};