simple-cookie
Advanced tools
Comparing version 1.0.4 to 1.0.5
104
index.js
function printExpires(expires){ | ||
if(!expires) return false; | ||
if(typeof expires == 'string') expires = new Date(expires); | ||
if(typeof expires == 'number') expires = new Date(expires); | ||
var n = ( expires.valueOf() - (new Date()).valueOf() ) / 1000; | ||
return 'Expires='+expires.toGMTString()+';Max-Age='+Math.round( n ); | ||
if(!expires) return false; | ||
if(typeof expires == 'string') expires = new Date(expires); | ||
if(typeof expires == 'number') expires = new Date(expires); | ||
var n = ( expires.valueOf() - (new Date()).valueOf() ) / 1000; | ||
return 'Expires='+expires.toGMTString()+';Max-Age='+Math.round( n ); | ||
} | ||
var cookie = { | ||
stringify: function( obj ){ | ||
var value; | ||
try{ | ||
value = encodeURIComponent(obj.value); | ||
}catch(e){ | ||
value = obj.value; | ||
} | ||
return [ | ||
obj.name+'='+value, | ||
stringify: function( obj ){ | ||
var value; | ||
try{ | ||
value = encodeURIComponent(obj.value); | ||
}catch(e){ | ||
value = obj.value; | ||
} | ||
return [ | ||
obj.name+'='+value, | ||
( typeof obj.expires != 'undefined' && obj.expires ? printExpires(obj.expires) : '' ), | ||
@@ -26,45 +26,45 @@ ( typeof obj.path != 'undefined' ? (obj.path ? 'Path='+obj.path : '') : 'Path=/' ), | ||
].join(';').replace(/;+/g,';').replace(/;$/,'').replace(/;/g,'; '); | ||
}, | ||
parse: function( string, path, domain ){ | ||
var s = string.replace(/;\s+/g,';').split(';') | ||
].join(';').replace(/;+/g,';').replace(/;$/,'').replace(/;/g,'; '); | ||
}, | ||
parse: function( string, path, domain ){ | ||
var s = string.replace(/;\s+/g,';').split(';') | ||
.map(function(s){return s.replace(/\s+\=\s+/g,'=').split('=');}); | ||
var n = s.shift(); | ||
var obj = {}; | ||
obj.expires = false; | ||
obj.httponly = false; | ||
obj.secure = false; | ||
obj.path = path || '/'; | ||
obj.domain = domain || ''; | ||
var n = s.shift(); | ||
var I, f = { | ||
'httponly': function(){ obj.httponly = true; }, | ||
'secure': function(){ obj.secure = true; }, | ||
'expires': function(v){ obj.expires = new Date(v); }, | ||
'max-age': function(v){ if(obj.expires) return; obj.expires = new Date((new Date()).valueOf()+(v*1000)); }, | ||
'path': function(v){ obj.path = v; }, | ||
'domain': function(v){ obj.domain = v; } | ||
}; | ||
var obj = {}; | ||
obj.expires = false; | ||
obj.httponly = false; | ||
obj.secure = false; | ||
obj.path = path || '/'; | ||
obj.domain = domain || ''; | ||
for(var i in s) { | ||
I = s[i][0].toLowerCase(); | ||
if( typeof f[I] != 'undefined' ) f[I]( s[i].length==2 ? s[i][1] : '' ); | ||
} | ||
var I, f = { | ||
httponly: function(){ obj.httponly = true; }, | ||
secure: function(){ obj.secure = true; }, | ||
expires: function(v){ obj.expires = new Date(v); }, | ||
'max-age': function(v){ if(obj.expires) return; obj.expires = new Date((new Date()).valueOf()+(v*1000)); }, | ||
path: function(v){ obj.path = v; }, | ||
domain: function(v){ obj.domain = v; } | ||
}; | ||
if( !obj.expires ) obj.expires = 0; | ||
obj.name = n[0]; | ||
try{ | ||
obj.value = decodeURIComponent(n[1]); | ||
}catch(e){ | ||
obj.value = n[1]; | ||
} | ||
return obj; | ||
}, | ||
tokenize: function( array ){ | ||
return array.map(function(s){ return s.name+'='+encodeURIComponent(s.value); }).join('; '); | ||
} | ||
for(var i in s) { | ||
I = s[i][0].toLowerCase(); | ||
if( typeof f[I] != 'undefined' ) f[I]( s[i].length==2 ? s[i][1] : '' ); | ||
} | ||
if( !obj.expires ) obj.expires = 0; | ||
obj.name = n.shift(); | ||
try{ | ||
obj.value = decodeURIComponent(n.join('=')); | ||
}catch(e){ | ||
obj.value = n.join('='); | ||
} | ||
return obj; | ||
}, | ||
tokenize: function( array ){ | ||
return array.map(function(s){ return s.name+'='+encodeURIComponent(s.value); }).join('; '); | ||
} | ||
}; | ||
module.exports = exports = cookie; | ||
module.exports = exports = cookie; |
{ | ||
"name": "simple-cookie", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Simple cookie parser & serializer", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"lint": "eslint .", | ||
"lint:fix": "eslint --fix .", | ||
"test": "mocha", | ||
"posttest": "npm run lint" | ||
}, | ||
@@ -25,4 +28,5 @@ "repository": { | ||
"chai": "^3.5.0", | ||
"eslint": "^3.17.1", | ||
"mocha": "^3.2.0" | ||
} | ||
} |
# simple-cookie | ||
simple cookie serializer & parser for node.js | ||
[![NPM Version][npm-image]][npm-url] | ||
[![NPM Version](https://img.shields.io/npm/v/simple-cookie.svg?style=flat)](https://npmjs.org/package/simple-cookie) | ||
[![Dependency Status](https://david-dm.org/juji/simple-cookie.svg)](https://david-dm.org/juji/simple-cookie) | ||
@@ -66,6 +66,4 @@ [![devDependency Status](https://david-dm.org/juji/simple-cookie/dev-status.svg)](https://david-dm.org/juji/simple-cookie#info=devDependencies) | ||
[jujiyangasli.com](http://jujiyangasli.com) | ||
[npm-image]: https://img.shields.io/npm/v/simple-cookie.svg?style=flat | ||
[npm-url]: https://npmjs.org/package/simple-cookie | ||
[travis-image]: https://img.shields.io/travis/juji/simple-cookie.svg?style=flat | ||
[travis-url]: https://travis-ci.org/juji/simple-cookie |
@@ -5,6 +5,6 @@ | ||
var c = { | ||
name : 'cookiename', | ||
value: 'cookie value with space', | ||
expires: (new Date()).valueOf() + 500000 | ||
}; | ||
name : 'cookiename', | ||
value: 'cookie value with space', | ||
expires: (new Date()).valueOf() + 500000 | ||
}; | ||
@@ -16,49 +16,49 @@ var co; | ||
describe('#stringify',function(){ | ||
describe('#stringify',function(){ | ||
it("should return cookie string from object",function(){ | ||
it('should return cookie string from object',function(){ | ||
co = cookie.stringify(c); | ||
co.should.be.a.string; | ||
(new RegExp('^'+c.name)).test(co).should.be.ok; | ||
}); | ||
co = cookie.stringify(c); | ||
co.should.be.a.string; | ||
(new RegExp('^'+c.name)).test(co).should.be.ok; | ||
}); | ||
}); | ||
}); | ||
describe('#parse',function(){ | ||
describe('#parse',function(){ | ||
it("should return object from cookie string",function(){ | ||
it('should return object from cookie string',function(){ | ||
var h = cookie.parse(co,false,'juji.com'); | ||
h.name.should.equal( c.name ); | ||
h.expires.should.be.a( 'date' ); | ||
h.path.should.equal( '/' ); | ||
h.domain.should.equal( 'juji.com' ); | ||
var h = cookie.parse(co,false,'juji.com'); | ||
h.name.should.equal( c.name ); | ||
h.expires.should.be.a( 'date' ); | ||
h.path.should.equal( '/' ); | ||
h.domain.should.equal( 'juji.com' ); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('#parseString',function(){ | ||
it("should return the right object from cookie string",function(){ | ||
describe('#parseString',function(){ | ||
it('should return the right object from cookie string',function(){ | ||
var theCookie = 'cnameSecure=cval1sec; expires='+date+ | ||
var theCookie = 'cnameSecure=cval1sec; expires='+date+ | ||
'; domain=.example.com; path=/; secure'; | ||
var h = cookie.parse( theCookie, 'example.com', '/is/cool' ); | ||
h.name.should.equal( 'cnameSecure' ); | ||
h.value.should.equal( 'cval1sec' ); | ||
h.expires.should.be.a( 'date' ); | ||
h.expires.toGMTString().should.equal( date ); | ||
h.secure.should.equal( true ); | ||
h.httponly.should.equal( false ); | ||
h.path.should.equal( '/' ); | ||
h.domain.should.equal( '.example.com' ); | ||
var h = cookie.parse( theCookie, 'example.com', '/is/cool' ); | ||
h.name.should.equal( 'cnameSecure' ); | ||
h.value.should.equal( 'cval1sec' ); | ||
h.expires.should.be.a( 'date' ); | ||
h.expires.toGMTString().should.equal( date ); | ||
h.secure.should.equal( true ); | ||
h.httponly.should.equal( false ); | ||
h.path.should.equal( '/' ); | ||
h.domain.should.equal( '.example.com' ); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('#tokeninze',function(){ | ||
it('should return tokenized cookies ready to send', function(){ | ||
var cc = [ | ||
describe('#tokeninze',function(){ | ||
it('should return tokenized cookies ready to send', function(){ | ||
var cc = [ | ||
{name:'cookiename1',value:'cookie name 1'}, | ||
@@ -69,11 +69,11 @@ {name:'cookiename2',value:'cookie name 2'}, | ||
{name:'cookiename5',value:'cookie name 5'} | ||
]; | ||
]; | ||
var h = cookie.tokenize(cc); | ||
h.should.be.a('string'); | ||
var h = cookie.tokenize(cc); | ||
h.should.be.a('string'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7513
128
3
69