Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
defunctzombie committed May 28, 2012
0 parents commit a25d241
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
3 changes: 3 additions & 0 deletions .travis.yml
@@ -0,0 +1,3 @@
language: node_js
node_js:
- 0.6
44 changes: 44 additions & 0 deletions README.md
@@ -0,0 +1,44 @@
# cookie [![Build Status](https://secure.travis-ci.org/shtylman/node-cookie.png?branch=master)](http://travis-ci.org/shtylman/node-cookie) #

cookie is a basic cookie parser and serializer. It doesn't make assumptions about how you are going to deal with your cookies. It basically just provides a way to read and write the HTTP cookie headers.

See [RFC6265](http://tools.ietf.org/html/rfc6265) for details about the http header for cookies.

## how?

```
npm install cookie
```

```javascript
var cookie = require('cookie');

var hdr = cookie.serialize('foo', 'bar');
// hdr = 'foo=bar';

var cookies = cookie.parse('foo=bar; cat=meow; dog=ruff');
// cookies = { foo: 'bar', cat: 'meow', dog: 'ruff' };
```

## more

The serialize function takes a third parameter, an object, to set cookie options. See the RFC for valid values.

### path
> cookie path
### expires
> absolute expiration date for the cookie (Date object)
### maxAge
> relative max age of the cookie from when the client receives it (seconds)
### domain
> domain for the cookie
### secure
> true or false
### httpOnly
> true or false
63 changes: 63 additions & 0 deletions index.js
@@ -0,0 +1,63 @@

/// Serialize the a name value pair into a cookie string suitable for
/// http headers. An optional options object specified cookie parameters
///
/// serialize('foo', 'bar', { httpOnly: true })
/// => "foo=bar; httpOnly"
///
/// @param {String} name
/// @param {String} val
/// @param {Object} options
/// @return {String}
var serialize = function(name, val, opt){
var pairs = [name + '=' + encodeURIComponent(val)];
opt = opt || {};

if (opt.maxAge) pais.push('Max-Age=' + opt.maxAge);
if (opt.domain) pairs.push('Domain=' + opt.domain);
if (opt.path) pairs.push('Path=' + opt.path);
if (opt.expires) pairs.push('Expires=' + opt.expires.toUTCString());
if (opt.httpOnly) pairs.push('HttpOnly');
if (opt.secure) pairs.push('Secure');

return pairs.join('; ');
};

/// Parse the given cookie header string into an object
/// The object has the various cookies as keys(names) => values
/// @param {String} str
/// @return {Object}
var parse = function(str) {
var obj = {}
var pairs = str.split(/[;,] */);

pairs.forEach(function(pair) {
var eq_idx = pair.indexOf('=')
var key = pair.substr(0, eq_idx).trim()
var val = pair.substr(++eq_idx, pair.length).trim();

// quoted values
if ('"' == val[0]) {
val = val.slice(1, -1);
}

// only assign once
if (undefined == obj[key]) {
val = val.replace(/\+/g, ' ');
try {
obj[key] = decodeURIComponent(val);
} catch (err) {
if (err instanceof URIError) {
obj[key] = val;
} else {
throw err;
}
}
}
});

return obj;
};

module.exports.serialize = serialize;
module.exports.parse = parse;
26 changes: 26 additions & 0 deletions package.json
@@ -0,0 +1,26 @@
{
"author": "Roman Shtylman <shtylman@gmail.com>",
"name": "cookie",
"description": "cookie parsing and serialization",
"version": "0.0.0",
"repository": {
"type": "git",
"url": "git://github.com/shtylman/node-cookie.git"
},
"keywords": [
"cookie",
"cookies"
],
"main": "index.js",
"scripts": {
"test": "mocha"
},
"dependencies": {},
"devDependencies": {
"mocha": "1.x.x"
},
"optionalDependencies": {},
"engines": {
"node": "*"
}
}
1 change: 1 addition & 0 deletions test/mocha.opts
@@ -0,0 +1 @@
--ui qunit
24 changes: 24 additions & 0 deletions test/serialize.js
@@ -0,0 +1,24 @@
// builtin
var assert = require('assert');

var crumbs = require('..');

test('serialize', function() {
assert.equal('foo=bar', crumbs.serialize('foo', 'bar'));

assert.equal('foo=bar; Path=/', crumbs.serialize('foo', 'bar', {
path: '/'
}));

assert.equal('foo=bar; Secure', crumbs.serialize('foo', 'bar', {
secure: true
}));

assert.equal('foo=bar; Domain=example.com', crumbs.serialize('foo', 'bar', {
domain: 'example.com'
}));

assert.equal('foo=bar; HttpOnly', crumbs.serialize('foo', 'bar', {
httpOnly: true
}));
});

0 comments on commit a25d241

Please sign in to comment.