diff --git a/index.js b/index.js index ea74eaf..855d127 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,23 @@ var cookie = require('cookie'); +var getter = require('./lib/getter'); var setter = require('./lib/setter'); -module.exports = function setCookie(name, value, options) { +function getCookie(name, options) { + var cookieSource = getter(name, options); + var parsedCookieMap; + + if (cookieSource) { + parsedCookieMap = cookie.parse(cookieSource); + return parsedCookieMap[name]; + } +} + +function setCookie(name, value, options) { var cookieStr = cookie.serialize(name, value, options); setter(cookieStr, options); }; + +module.exports = { + getCookie: getCookie, + setCookie: setCookie +}; diff --git a/lib/getter/client.js b/lib/getter/client.js new file mode 100644 index 0000000..999e036 --- /dev/null +++ b/lib/getter/client.js @@ -0,0 +1,6 @@ +/** + * Cookie getter for browser environment. + */ +module.exports = function getter(cookieName, _) { + return document.cookie; +}; diff --git a/lib/getter/index.js b/lib/getter/index.js new file mode 100644 index 0000000..21ef88a --- /dev/null +++ b/lib/getter/index.js @@ -0,0 +1,9 @@ +/** + * Cookie getter for Node.js environment. + */ +module.exports = function getter(cookieName, options) { + var req = options && options.req; + if (!req) throw new Error('Must specify `req` when setting cookie.'); + + return req.headers.cookie; +}; diff --git a/package.json b/package.json index a89d9e5..1093dc7 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "set-cookie", - "version": "0.0.4", - "description": "Set a cookie using the same API on both the client and the server.", + "version": "0.1.0", + "description": "Get and set cookies using the same API on both the client and the server.", "main": "index.js", "scripts": { "test": "mocha", @@ -24,6 +24,7 @@ "cookie": "^0.1.2" }, "browser": { + "./lib/getter/index.js": "./lib/getter/client.js", "./lib/setter/index.js": "./lib/setter/client.js" }, "devDependencies": { diff --git a/test/browser/bundle.js b/test/browser/bundle.js index 2d611c7..b860bf0 100644 --- a/test/browser/bundle.js +++ b/test/browser/bundle.js @@ -1,13 +1,37 @@ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 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; -/// 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)]; - - 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); + 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; - - pairs.forEach(function(pair) { - var eq_idx = pair.indexOf('=') - - // skip things that don't look like key=value - if (eq_idx < 0) { - return; - } - - 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]) { - try { - obj[key] = dec(val); - } catch (e) { - obj[key] = val; - } - } - }); - - return obj; -}; + // only assign once + if (undefined == obj[key]) { + obj[key] = tryDecode(val, dec); + } + }); -var encode = encodeURIComponent; -var decode = decodeURIComponent; + return obj; +} -module.exports.serialize = serialize; -module.exports.parse = parse; +/** + * 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 + */ -},{}],4:[function(require,module,exports){ -var setCookie = require('../../'); +function serialize(name, val, options) { + var opt = options || {}; + var enc = opt.encode || encode; + var pairs = [name + '=' + enc(val)]; + + 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); + } + + 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'); -setCookie('thisIsA', 'cookie in a browser!', { + return pairs.join('; '); +} + +/** + * Try decoding a string using a decoding function. + * + * @param {string} str + * @param {function} decode + * @private + */ + +function tryDecode(str, decode) { + try { + return decode(str); + } catch (e) { + return str; + } +} + +},{}],5:[function(require,module,exports){ +var isoCookie = require('../../'); +var readValue; + +isoCookie.setCookie('thisIsA', 'cookie in a browser!', { expires: new Date(2020, 1, 1) }); console.log(document.cookie); -},{"../../":1}]},{},[4]) -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ2VuZXJhdGVkLmpzIiwic291cmNlcyI6WyIvVXNlcnMvc3Bpa2UvY29kZS9zZXQtY29va2llL25vZGVfbW9kdWxlcy9icm93c2VyaWZ5L25vZGVf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dWVzXG4gICAgICAgIGlmICgnXCInID09IHZhbFswXSkge1xuICAgICAgICAgICAgdmFsID0gdmFsLnNsaWNlKDEsIC0xKTtcbiAgICAgICAgfVxuXG4gICAgICAgIC8vIG9ubHkgYXNzaWduIG9uY2VcbiAgICAgICAgaWYgKHVuZGVmaW5lZCA9PSBvYmpba2V5XSkge1xuICAgICAgICAgICAgdHJ5IHtcbiAgICAgICAgICAgICAgICBvYmpba2V5XSA9IGRlYyh2YWwpO1xuICAgICAgICAgICAgfSBjYXRjaCAoZSkge1xuICAgICAgICAgICAgICAgIG9ialtrZXldID0gdmFsO1xuICAgICAgICAgICAgfVxuICAgICAgICB9XG4gICAgfSk7XG5cbiAgICByZXR1cm4gb2JqO1xufTtcblxudmFyIGVuY29kZSA9IGVuY29kZVVSSUNvbXBvbmVudDtcbnZhciBkZWNvZGUgPSBkZWNvZGVVUklDb21wb25lbnQ7XG5cbm1vZHVsZS5leHBvcnRzLnNlcmlhbGl6ZSA9IHNlcmlhbGl6ZTtcbm1vZHVsZS5leHBvcnRzLnBhcnNlID0gcGFyc2U7XG4iLCJ2YXIgc2V0Q29va2llID0gcmVxdWlyZSgnLi4vLi4vJyk7XG5cbnNldENvb2tpZSgndGhpc0lzQScsICdjb29raWUgaW4gYSBicm93c2VyIScsIHtcbiAgZXhwaXJlczogbmV3IERhdGUoMjAyMCwgMSwgMSlcbn0pO1xuXG5jb25zb2xlLmxvZyhkb2N1bWVudC5jb29raWUpO1xuIl19 +readValue = isoCookie.getCookie('thisIsA'); + +console.log(readValue); + +},{"../../":1}]},{},[5]) +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi9Vc2Vycy9hbHVubnkvZGV2L3NldC1jb29raWUvbm9kZV9tb2R1bGVzL2Jyb3dzZXJpZnkvbm9kZV9tb2R1bGVzL2Jyb3dzZXItcGFjay9fcHJlbHVkZS5qcyIsIi9Vc2Vycy9hbHVubnkvZGV2L3NldC1jb29raWUv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cmU7Zm9yKHZhciBvPTA7bzxyLmxlbmd0aDtvKyspcyhyW29dKTtyZXR1cm4gc30pIiwidmFyIGNvb2tpZSA9IHJlcXVpcmUoJ2Nvb2tpZScpO1xudmFyIGdldHRlciA9IHJlcXVpcmUoJy4vbGliL2dldHRlcicpO1xudmFyIHNldHRlciA9IHJlcXVpcmUoJy4vbGliL3NldHRlcicpO1xuXG5mdW5jdGlvbiBnZXRDb29raWUobmFtZSwgb3B0aW9ucykge1xuICB2YXIgY29va2llU291cmNlID0gZ2V0dGVyKG5hbWUsIG9wdGlvbnMpO1xuICB2YXIgcGFyc2VkQ29va2llTWFwO1xuXG4gIGlmIChjb29raWVTb3VyY2UpIHtcbiAgICBwYXJzZWRDb29raWVNYXAgPSBjb29raWUucGFyc2UoY29va2llU291cmNlKTtcbiAgICByZXR1cm4gcGFyc2VkQ29va2llTWFwW25hbWVdO1xuICB9XG59XG5cbmZ1bmN0aW9uIHNldENvb2tpZShuYW1lLCB2YWx1ZSwgb3B0aW9ucykge1xuICB2YXIgY29va2llU3RyID0gY29va2llLnNlcmlhbGl6ZShuYW1lLCB2YWx1ZSwgb3B0aW9ucyk7XG4gIHNldHRlcihjb29raWVTdHIsIG9wdGlvbnMpO1xufTtcblxubW9kdWxlLmV4cG9ydHMgPSB7XG4gIGdldENvb2tpZTogZ2V0Q29va2llLFxuICBzZXRDb29raWU6IHNldENvb2tpZVxufTtcbiIsIi8qKlxuICogQ29va2llIGdldHRlciBmb3IgYnJvd3NlciBlbnZpcm9ubWVudC5cbiAqL1xubW9kdWxlLmV4cG9ydHMgPSBmdW5jdGlvbiBnZXR0ZXIoY29va2llTmFtZSwgXykge1xuICByZXR1cm4gZG9jdW1lbnQuY29va2llO1xufTtcbiIsIi8qKlxuICogQ29va2llIHNldHRlciBmb3IgYnJvd3NlciBlbnZpcm9ubWVudC5cbiAqL1xuXG5tb2R1bGUuZXhwb3J0cyA9IGZ1bmN0aW9uIHNldHRlcihjb29raWVTdHIsIF8pIHt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vZGluZyBhIHN0cmluZyB1c2luZyBhIGRlY29kaW5nIGZ1bmN0aW9uLlxyXG4gKlxyXG4gKiBAcGFyYW0ge3N0cmluZ30gc3RyXHJcbiAqIEBwYXJhbSB7ZnVuY3Rpb259IGRlY29kZVxyXG4gKiBAcHJpdmF0ZVxyXG4gKi9cclxuXHJcbmZ1bmN0aW9uIHRyeURlY29kZShzdHIsIGRlY29kZSkge1xyXG4gIHRyeSB7XHJcbiAgICByZXR1cm4gZGVjb2RlKHN0cik7XHJcbiAgfSBjYXRjaCAoZSkge1xyXG4gICAgcmV0dXJuIHN0cjtcclxuICB9XHJcbn1cclxuIiwidmFyIGlzb0Nvb2tpZSA9IHJlcXVpcmUoJy4uLy4uLycpO1xudmFyIHJlYWRWYWx1ZTtcblxuaXNvQ29va2llLnNldENvb2tpZSgndGhpc0lzQScsICdjb29raWUgaW4gYSBicm93c2VyIScsIHtcbiAgZXhwaXJlczogbmV3IERhdGUoMjAyMCwgMSwgMSlcbn0pO1xuXG5jb25zb2xlLmxvZyhkb2N1bWVudC5jb29raWUpO1xuXG5yZWFkVmFsdWUgPSBpc29Db29raWUuZ2V0Q29va2llKCd0aGlzSXNBJyk7XG5cbmNvbnNvbGUubG9nKHJlYWRWYWx1ZSk7XG4iXX0= diff --git a/test/browser/index.js b/test/browser/index.js index 07f29a9..933b7c7 100644 --- a/test/browser/index.js +++ b/test/browser/index.js @@ -1,7 +1,12 @@ -var setCookie = require('../../'); +var isoCookie = require('../../'); +var readValue; -setCookie('thisIsA', 'cookie in a browser!', { +isoCookie.setCookie('thisIsA', 'cookie in a browser!', { expires: new Date(2020, 1, 1) }); console.log(document.cookie); + +readValue = isoCookie.getCookie('thisIsA'); + +console.log(readValue); diff --git a/test/index.js b/test/index.js index 9e7da8b..bcb47f0 100644 --- a/test/index.js +++ b/test/index.js @@ -1,4 +1,5 @@ -var setCookie = require('../'); +var getCookie = require('../').getCookie; +var setCookie = require('../').setCookie; var chai = require('chai'); var sinon = require('sinon'); var superagent = require('superagent'); @@ -69,3 +70,45 @@ describe('set-cookie', function() { }); }); }); + +describe('get-cookie', function() { + describe('Node getter', function() { + it('throws when not passed `req`', function() { + expect(function() { + getCookie('myName', {}); + }).to.throw(/req/); + }); + + it('returns undefined when passed `req` without a Cookie header', function() { + var req = { + headers: {} + }; + + var cookie = getCookie('myName', {req: req}); + expect(cookie).to.eql(undefined); + }); + + describe('when passed a Cookie header', function() { + var req = { + headers: { + cookie: 'foo=bar%20baz; cat=meow; dog=ruff' + } + }; + + it('returns undefined if the key is not present', function() { + var cookie = getCookie('myName', {req: req}); + expect(cookie).to.eql(undefined); + }); + + it('returns the value if the key is present', function() { + var cookie = getCookie('cat', {req: req}); + expect(cookie).to.eql('meow'); + }); + + it('decodes the parsed value', function() { + var cookie = getCookie('foo', {req: req}); + expect(cookie).to.eql('bar baz'); + }); + }); + }); +});