From 05c814810cd836ab749d586ea0757fe4f4700f5f Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 31 Aug 2013 09:50:36 -0400 Subject: [PATCH] initial commit --- .gitignore | 1 + Makefile | 4 +++ README.md | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 15 +++++++++++ package.json | 26 +++++++++++++++++++ 5 files changed, 116 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..87ae97a --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +test: + @echo "No Tests" + +.PHONY: test diff --git a/README.md b/README.md new file mode 100644 index 0000000..93af0ea --- /dev/null +++ b/README.md @@ -0,0 +1,70 @@ +YAPS Cookies +============ + +[![NPM version](https://badge.fury.io/js/yaps-cookies.png)](http://badge.fury.io/js/yaps-cookies) + +This is a cookie plugin for [YAPS](https://github.com/brettlangdon/yaps.git). + +It simply uses [node-cookie](https://github.com/shtylman/node-cookie) under the hood to parse and set cookie headers. + +## Installation +### Via NPM +```bash +npm install [-g] yaps-cookies +``` +### Via Git +```bash +git clone git://github.com/brettlangdon/yaps-cookies.git +cd ./yaps-cookies.git +npm install +``` + +## Usage +```javascript +var yaps = require("yaps"); +var yaps_cookies = require("yaps-cookies"); + +var app = new yaps.server(); +app.registerPlugin(yaps_cookies); + +var my_plugin = function(options){ + this.GET("/", function(request, server, respond){ + // this is an object of the cookies sent with the request + // {"name": "value"} + console.dir(request.cookies); + + // add a response cookie + request.setCookie("user-id", "some value"); + + respond(200, "thanks"); + }); +}; +app.registerPlugin(my_plugin); + +app.start(); +``` + +## License +``` +The MIT License (MIT) + +Copyright (c) 2013 Brett Langdon (http://www.brett.is) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +``` diff --git a/index.js b/index.js new file mode 100644 index 0000000..9847e53 --- /dev/null +++ b/index.js @@ -0,0 +1,15 @@ +var cookie = require("cookie"); + +var yaps_cookies = function(options){ + this.on("setup", this.on_setup); +}; + +yaps_cookies.prototype.on_setup = function(request, server, done){ + request.setCookie = function(name, value, options){ + request.addHeader("Set-Cookie", cookie.serialize(name, value, options)); + }; + request.cookies = cookie.parse(request.headers.cookie || ""); + done(); +}; + +module.exports = yaps_cookies; diff --git a/package.json b/package.json new file mode 100644 index 0000000..8fbe5e0 --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "author": "Brett Langdon (http://brett.is)", + "bugs": { + "url": "https://github.com/brettlangdon/yaps-cookies/issues" + }, + "dependencies": { + "cookie": "~0.1.0" + }, + "description": "Cookie management plugin for YAPS", + "keywords": [ + "yaps", + "http", + "cookie" + ], + "license": "MIT", + "main": "index.js", + "name": "yaps-cookies", + "repository": { + "type": "git", + "url": "git://github.com/brettlangdon/yaps-cookies.git" + }, + "scripts": { + "test": "make test" + }, + "version": "0.1.0" +}