From 7ba3b0298b4978ebef67aeb47dff1b511e1bc0ad Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Fri, 31 Aug 2012 16:23:15 +0100 Subject: [PATCH] first commit --- README.md | 22 ++++++++++++++++++++++ index.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 17 +++++++++++++++++ test/index.js | 30 ++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 index.js create mode 100644 package.json create mode 100644 test/index.js diff --git a/README.md b/README.md index e69de29..b6fcbea 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,22 @@ +# Flatware::Cookie-Parser + +Cookie Parser middleware for Flatiron.js + +## Include as dependency + +Add "flatware-cookie-parser" to your dependencies in `package.json`. + + $ npm install + +## Add it to the server + +```javascript +var CookieParser = require('flatware-cookie-parser'); + + var server = union.createServer({ + before: [ + CookieParser(), + // ... + ] + }); +``` \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..34b32bc --- /dev/null +++ b/index.js @@ -0,0 +1,48 @@ +function parseCookie(str){ + var obj = {} + , pairs = str.split(/[;,] */); + + for (var i = 0, len = pairs.length; i < len; ++i) { + var pair = pairs[i] + , eqlIndex = pair.indexOf('=') + , key = pair.substr(0, eqlIndex).trim() + , val = pair.substr(++eqlIndex, pair.length).trim() + ; + + if ('"' == val[0]) val = val.slice(1, -1); + + 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; +}; + +function CookieParser(){ + return function cookieParser(req, res) { + var cookie = req.headers.cookie; + if (req.cookies) return res.emit('next'); + + req.cookies = {}; + + if (cookie) { + try { + req.cookies = parseCookie(cookie); + } catch (err) { + return res.emit('error', err); + } + } + res.emit('next'); + }; +}; + +module.exports = CookieParser; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..0b036dd --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "flatware-cookie-parser", + "version": "0.1.0", + "author": { + "name": "Pedro Teixeira", + "email": "pedro.teixeira@gmail.com" + }, + "devDependencies": { + "tap": "0.3.x" + }, + "engines": { + "node": ">= 0.8.0" + }, + "scripts": { + "test": "node test" + } +} \ No newline at end of file diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..36b9527 --- /dev/null +++ b/test/index.js @@ -0,0 +1,30 @@ +var test = require('tap').test, + EE = require('events').EventEmitter; + +var cookieParser = require('..'); + +test('should default to {} when no cookies are present', function(t) { + var req = {headers: {}}, + res = new EE(), + parser = cookieParser() + ; + + res.on('next', function() { + t.equivalent(req.cookies, {}); + t.end(); + }); + parser(req, res); +}); + +test('should populate req.cookies', function(t) { + var req = {headers: {'cookie': 'abc=def; ghi=jkl'}}, + res = new EE(), + parser = cookieParser() + ; + + res.on('next', function() { + t.equivalent(req.cookies, {abc:'def', ghi:'jkl'}); + t.end(); + }); + parser(req, res); +}); \ No newline at end of file