Comparing version 0.1.2 to 0.1.3
163
index.js
@@ -0,75 +1,116 @@ | ||
/*! | ||
* cookie | ||
* Copyright(c) 2012-2014 Roman Shtylman | ||
* MIT Licensed | ||
*/ | ||
/// 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){ | ||
opt = opt || {}; | ||
var enc = opt.encode || encode; | ||
var pairs = [name + '=' + enc(val)]; | ||
/** | ||
* Module exports. | ||
* @public | ||
*/ | ||
if (null != opt.maxAge) { | ||
var maxAge = opt.maxAge - 0; | ||
if (isNaN(maxAge)) throw new Error('maxAge should be a Number'); | ||
pairs.push('Max-Age=' + maxAge); | ||
exports.parse = parse; | ||
exports.serialize = serialize; | ||
/** | ||
* Module variables. | ||
* @private | ||
*/ | ||
var decode = decodeURIComponent; | ||
var encode = encodeURIComponent; | ||
/** | ||
* Parse a cookie header. | ||
* | ||
* Parse the given cookie header string into an object | ||
* The object has the various cookies as keys(names) => values | ||
* | ||
* @param {string} str | ||
* @param {object} [options] | ||
* @return {string} | ||
* @public | ||
*/ | ||
function parse(str, options) { | ||
var obj = {} | ||
var opt = options || {}; | ||
var pairs = str.split(/; */); | ||
var dec = opt.decode || decode; | ||
pairs.forEach(function(pair) { | ||
var eq_idx = pair.indexOf('=') | ||
// skip things that don't look like key=value | ||
if (eq_idx < 0) { | ||
return; | ||
} | ||
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'); | ||
var key = pair.substr(0, eq_idx).trim() | ||
var val = pair.substr(++eq_idx, pair.length).trim(); | ||
return pairs.join('; '); | ||
}; | ||
// quoted values | ||
if ('"' == val[0]) { | ||
val = val.slice(1, -1); | ||
} | ||
/// 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, opt) { | ||
opt = opt || {}; | ||
var obj = {} | ||
var pairs = str.split(/; */); | ||
var dec = opt.decode || decode; | ||
// only assign once | ||
if (undefined == obj[key]) { | ||
obj[key] = tryDecode(val, dec); | ||
} | ||
}); | ||
pairs.forEach(function(pair) { | ||
var eq_idx = pair.indexOf('=') | ||
return obj; | ||
} | ||
// skip things that don't look like key=value | ||
if (eq_idx < 0) { | ||
return; | ||
} | ||
/** | ||
* Serialize data into a cookie header. | ||
* | ||
* 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} | ||
* @public | ||
*/ | ||
var key = pair.substr(0, eq_idx).trim() | ||
var val = pair.substr(++eq_idx, pair.length).trim(); | ||
function serialize(name, val, options) { | ||
var opt = options || {}; | ||
var enc = opt.encode || encode; | ||
var pairs = [name + '=' + enc(val)]; | ||
// quoted values | ||
if ('"' == val[0]) { | ||
val = val.slice(1, -1); | ||
} | ||
if (null != opt.maxAge) { | ||
var maxAge = opt.maxAge - 0; | ||
if (isNaN(maxAge)) throw new Error('maxAge should be a Number'); | ||
pairs.push('Max-Age=' + maxAge); | ||
} | ||
// only assign once | ||
if (undefined == obj[key]) { | ||
try { | ||
obj[key] = dec(val); | ||
} catch (e) { | ||
obj[key] = val; | ||
} | ||
} | ||
}); | ||
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 obj; | ||
}; | ||
return pairs.join('; '); | ||
} | ||
var encode = encodeURIComponent; | ||
var decode = decodeURIComponent; | ||
/** | ||
* Try decoding a string using a decoding function. | ||
* | ||
* @param {string} str | ||
* @param {function} decode | ||
* @private | ||
*/ | ||
module.exports.serialize = serialize; | ||
module.exports.parse = parse; | ||
function tryDecode(str, decode) { | ||
try { | ||
return decode(str); | ||
} catch (e) { | ||
return str; | ||
} | ||
} |
{ | ||
"author": "Roman Shtylman <shtylman@gmail.com>", | ||
"name": "cookie", | ||
"description": "cookie parsing and serialization", | ||
"version": "0.1.2", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/shtylman/node-cookie.git" | ||
}, | ||
"version": "0.1.3", | ||
"author": "Roman Shtylman <shtylman@gmail.com>", | ||
"license": "MIT", | ||
"keywords": [ | ||
@@ -14,14 +11,20 @@ "cookie", | ||
], | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"dependencies": {}, | ||
"repository": "jshttp/cookie", | ||
"devDependencies": { | ||
"istanbul": "0.3.9", | ||
"mocha": "1.x.x" | ||
}, | ||
"optionalDependencies": {}, | ||
"files": [ | ||
"LICENSE", | ||
"README.md", | ||
"index.js" | ||
], | ||
"engines": { | ||
"node": "*" | ||
}, | ||
"scripts": { | ||
"test": "mocha --reporter spec --bail --check-leaks test/", | ||
"test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/" | ||
} | ||
} |
@@ -1,3 +0,9 @@ | ||
# cookie [![Build Status](https://secure.travis-ci.org/defunctzombie/node-cookie.png?branch=master)](http://travis-ci.org/defunctzombie/node-cookie) # | ||
# cookie | ||
[![NPM Version][npm-image]][npm-url] | ||
[![NPM Downloads][downloads-image]][downloads-url] | ||
[![Node.js Version][node-version-image]][node-version-url] | ||
[![Build Status][travis-image]][travis-url] | ||
[![Test Coverage][coveralls-image]][coveralls-url] | ||
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. | ||
@@ -45,1 +51,15 @@ | ||
## License | ||
[MIT](LICENSE) | ||
[npm-image]: https://img.shields.io/npm/v/cookie.svg | ||
[npm-url]: https://npmjs.org/package/cookie | ||
[node-version-image]: https://img.shields.io/node/v/cookie.svg | ||
[node-version-url]: http://nodejs.org/download/ | ||
[travis-image]: https://img.shields.io/travis/jshttp/cookie/master.svg | ||
[travis-url]: https://travis-ci.org/jshttp/cookie | ||
[coveralls-image]: https://img.shields.io/coveralls/jshttp/cookie/master.svg | ||
[coveralls-url]: https://coveralls.io/r/jshttp/cookie?branch=master | ||
[downloads-image]: https://img.shields.io/npm/dm/cookie.svg | ||
[downloads-url]: https://npmjs.org/package/cookie |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
6245
97
65
2
4
1
1