[Pkg-javascript-commits] [node-cookiejar] 01/02: Imported Upstream version 1.3.2
Leo Iannacone
l3on-guest at moszumanska.debian.org
Mon Jun 2 14:05:07 UTC 2014
This is an automated email from the git hooks/post-receive script.
l3on-guest pushed a commit to branch master
in repository node-cookiejar.
commit eff350c8c69fe3b1786d6f385be9b08dde60e164
Author: Leo Iannacone <l3on at ubuntu.com>
Date: Mon Jun 2 15:54:38 2014 +0200
Imported Upstream version 1.3.2
---
LICENSE | 9 +++
cookiejar.js | 226 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package.json | 14 ++++
readme.md | 45 ++++++++++++
tests/test.js | 58 +++++++++++++++
5 files changed, 352 insertions(+)
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..58a23ec
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,9 @@
+The MIT License (MIT)
+Copyright (c) 2013 Bradley Meck
+
+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/cookiejar.js b/cookiejar.js
new file mode 100644
index 0000000..deda7c7
--- /dev/null
+++ b/cookiejar.js
@@ -0,0 +1,226 @@
+var CookieAccessInfo=exports.CookieAccessInfo=function CookieAccessInfo(domain,path,secure,script) {
+ if(this instanceof CookieAccessInfo) {
+ this.domain=domain||undefined;
+ this.path=path||"/";
+ this.secure=!!secure;
+ this.script=!!script;
+ return this;
+ }
+ else {
+ return new CookieAccessInfo(domain,path,secure,script)
+ }
+}
+
+var Cookie=exports.Cookie=function Cookie(cookiestr) {
+ if(cookiestr instanceof Cookie) {
+ return cookiestr;
+ }
+ else {
+ if(this instanceof Cookie) {
+ this.name = null;
+ this.value = null;
+ this.expiration_date = Infinity;
+ this.path = "/";
+ this.domain = null;
+ this.secure = false; //how to define?
+ this.noscript = false; //httponly
+ if(cookiestr) {
+ try {
+ this.parse(cookiestr)
+ } catch(e) {}
+ }
+ return this;
+ }
+ return new Cookie(cookiestr)
+ }
+}
+
+Cookie.prototype.toString = function toString() {
+ var str=[this.name+"="+this.value];
+ if(this.expiration_date !== Infinity) {
+ str.push("expires="+(new Date(this.expiration_date)).toGMTString());
+ }
+ if(this.domain) {
+ str.push("domain="+this.domain);
+ }
+ if(this.path) {
+ str.push("path="+this.path);
+ }
+ if(this.secure) {
+ str.push("secure");
+ }
+ if(this.noscript) {
+ str.push("httponly");
+ }
+ return str.join("; ");
+}
+
+Cookie.prototype.toValueString = function toValueString() {
+ return this.name+"="+this.value;
+}
+
+var cookie_str_splitter=/[:](?=\s*[a-zA-Z0-9_\-]+\s*[=])/g
+Cookie.prototype.parse = function parse(str) {
+ if(this instanceof Cookie) {
+ var parts=str.split(";").filter(function(value){return !!value})
+ , pair=parts[0].match(/([^=]+)=((?:.|\n)*)/)
+ , key=pair[1]
+ , value=pair[2];
+ this.name = key;
+ this.value = value;
+
+ for(var i=1;i<parts.length;i++) {
+ pair=parts[i].match(/([^=]+)(?:=((?:.|\n)*))?/)
+ , key=pair[1].trim().toLowerCase()
+ , value=pair[2];
+ switch(key) {
+ case "httponly":
+ this.noscript = true;
+ break;
+ case "expires":
+ this.expiration_date = value
+ ? Number(Date.parse(value))
+ : Infinity;
+ break;
+ case "path":
+ this.path = value
+ ? value.trim()
+ : "";
+ break;
+ case "domain":
+ this.domain = value
+ ? value.trim()
+ : "";
+ break;
+ case "secure":
+ this.secure = true;
+ break
+ }
+ }
+
+ return this;
+ }
+ return new Cookie().parse(str)
+}
+
+Cookie.prototype.matches = function matches(access_info) {
+ if(this.noscript && access_info.script
+ || this.secure && !access_info.secure
+ || !this.collidesWith(access_info)) {
+ return false
+ }
+ return true;
+}
+
+Cookie.prototype.collidesWith = function collidesWith(access_info) {
+ if((this.path && !access_info.path) || (this.domain && !access_info.domain)) {
+ return false
+ }
+ if(this.path && access_info.path.indexOf(this.path) !== 0) {
+ return false;
+ }
+ if (this.domain===access_info.domain) {
+ return true;
+ }
+ else if(this.domain && this.domain.charAt(0)===".")
+ {
+ var wildcard=access_info.domain.indexOf(this.domain.slice(1))
+ if(wildcard===-1 || wildcard!==access_info.domain.length-this.domain.length+1) {
+ return false;
+ }
+ }
+ else if(this.domain){
+ return false
+ }
+ return true;
+}
+
+var CookieJar=exports.CookieJar=function CookieJar() {
+ if(this instanceof CookieJar) {
+ var cookies = {} //name: [Cookie]
+
+ this.setCookie = function setCookie(cookie) {
+ cookie = Cookie(cookie);
+ //Delete the cookie if the set is past the current time
+ var remove = cookie.expiration_date <= Date.now();
+ if(cookie.name in cookies) {
+ var cookies_list = cookies[cookie.name];
+ for(var i=0;i<cookies_list.length;i++) {
+ var collidable_cookie = cookies_list[i];
+ if(collidable_cookie.collidesWith(cookie)) {
+ if(remove) {
+ cookies_list.splice(i,1);
+ if(cookies_list.length===0) {
+ delete cookies[cookie.name]
+ }
+ return false;
+ }
+ else {
+ return cookies_list[i]=cookie;
+ }
+ }
+ }
+ if(remove) {
+ return false;
+ }
+ cookies_list.push(cookie);
+ return cookie;
+ }
+ else if(remove){
+ return false;
+ }
+ else {
+ return cookies[cookie.name]=[cookie];
+ }
+ }
+ //returns a cookie
+ this.getCookie = function getCookie(cookie_name,access_info) {
+ var cookies_list = cookies[cookie_name];
+ if (!cookies_list) return;
+ for(var i=0;i<cookies_list.length;i++) {
+ var cookie = cookies_list[i];
+ if(cookie.expiration_date <= Date.now()) {
+ if(cookies_list.length===0) {
+ delete cookies[cookie.name]
+ }
+ continue;
+ }
+ if(cookie.matches(access_info)) {
+ return cookie;
+ }
+ }
+ }
+ //returns a list of cookies
+ this.getCookies = function getCookies(access_info) {
+ var matches=[];
+ for(var cookie_name in cookies) {
+ var cookie=this.getCookie(cookie_name,access_info);
+ if (cookie) {
+ matches.push(cookie);
+ }
+ }
+ matches.toString=function toString(){return matches.join(":");}
+ matches.toValueString=function() {return matches.map(function(c){return c.toValueString();}).join(';');}
+ return matches;
+ }
+
+ return this;
+ }
+ return new CookieJar()
+}
+
+
+//returns list of cookies that were set correctly. Cookies that are expired and removed are not returned.
+CookieJar.prototype.setCookies = function setCookies(cookies) {
+ cookies=Array.isArray(cookies)
+ ?cookies
+ :cookies.split(cookie_str_splitter);
+ var successful=[]
+ for(var i=0;i<cookies.length;i++) {
+ var cookie = Cookie(cookies[i]);
+ if(this.setCookie(cookie)) {
+ successful.push(cookie);
+ }
+ }
+ return successful;
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..a4b0ddd
--- /dev/null
+++ b/package.json
@@ -0,0 +1,14 @@
+{
+ "name": "cookiejar",
+ "version": "1.3.2",
+ "author": {
+ "name": "bradleymeck"
+ },
+ "main": "cookiejar.js",
+ "description": "simple persistent cookiejar system",
+ "license": "MIT",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/bmeck/node-cookiejar.git"
+ }
+}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..43f1799
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,45 @@
+#CookieJar
+
+Simple robust cookie library
+
+##Exports
+
+
+###CookieAccessInfo(domain,path,secure,script)
+ class to determine matching qualities of a cookie
+
+#####Properties
+* String domain - domain to match
+* String path - path to match
+* Boolean secure - access is secure (ssl generally)
+* Boolean script - access is from a script
+
+
+###Cookie(cookiestr_or_cookie)
+ turns input into a Cookie (singleton if given a Cookie)
+
+#####Properties
+* String name - name of the cookie
+* String value - string associated with the cookie
+* String domain - domain to match (on a cookie a '.' at the start means a wildcard matching anything ending in the rest)
+* String path - base path to match (matches any path starting with this '/' is root)
+* Boolean noscript - if it should be kept from scripts
+* Boolean secure - should it only be transmitted over secure means
+* Number expiration_date - number of millis since 1970 at which this should be removed
+
+#####Methods
+* String toString() - the __set-cookie:__ string for this cookie
+* String toValueString() - the __cookie:__ string for this cookie
+* Cookie parse(cookiestr) - parses the string onto this cookie or a new one if called directly
+* Boolean matches(access_info) - returns true if the access_info allows retrieval of this cookie
+* Boolean collidesWith(cookie) - returns true if the cookies cannot exist in the same space (domain and path match)
+
+
+###CookieJar()
+ class to hold numerous cookies from multiple domains correctly
+
+#####Methods
+* Cookie setCookie(cookie) - add a cookie to the jar
+* Cookie[] setCookies(cookiestr_or_list) - add a large number of cookies to the jar
+* Cookie getCookie(cookie_name,access_info) - get a cookie with the name and access_info matching
+* Cookie[] getCookies(access_info) - grab all cookies matching this access_info
\ No newline at end of file
diff --git a/tests/test.js b/tests/test.js
new file mode 100644
index 0000000..b2aba44
--- /dev/null
+++ b/tests/test.js
@@ -0,0 +1,58 @@
+var Cookie=require("../cookiejar")
+, CookieAccessInfo = Cookie.CookieAccessInfo
+, CookieJar = Cookie.CookieJar
+, Cookie = Cookie.Cookie
+
+var assert = require('assert');
+
+// Test Cookie
+var cookie = new Cookie("a=1;domain=.test.com;path=/");
+assert.equal(cookie.name, "a");
+assert.equal(cookie.value, "1");
+assert.equal(cookie.domain, ".test.com");
+assert.equal(cookie.path, "/");
+assert.equal(cookie.secure, false);
+assert.equal(cookie.expiration_date, Infinity);
+
+assert.deepEqual(cookie, new Cookie("a=1;domain=.test.com;path=/"));
+assert.ok(cookie.collidesWith(new Cookie("a=1;domain=.test.com;path=/")));
+
+
+// Test CookieJar
+var test_jar = CookieJar();
+test_jar.setCookies(
+ "a=1;domain=.test.com;path=/"
+ +":b=2;domain=test.com;path=/"
+ +":c=3;domain=test.com;path=/;expires=January 1, 1970");
+var cookies=test_jar.getCookies(CookieAccessInfo("test.com","/"))
+assert.equal(cookies.length, 2, "Expires on setCookies fail\n" + cookies.toString());
+assert.equal(cookies.toValueString(), 'a=1;b=2', "Cannot get value string of multiple cookies");
+
+cookies=test_jar.getCookies(CookieAccessInfo("www.test.com","/"))
+assert.equal(cookies.length, 1, "Wildcard domain fail\n" + cookies.toString());
+
+test_jar.setCookies("b=2;domain=test.com;path=/;expires=January 1, 1970");
+cookies=test_jar.getCookies(CookieAccessInfo("test.com","/"))
+assert.equal(cookies.length, 1, "Delete cookie fail\n" + cookies.toString());
+assert.equal(String(test_jar.getCookies(CookieAccessInfo("test.com","/"))), "a=1; domain=.test.com; path=/");
+
+cookie=Cookie("a=1;domain=test.com;path=/;HttpOnly");
+assert.ok(cookie.noscript, "HttpOnly flag parsing failed\n" + cookie.toString());
+
+var test_jar = CookieJar();
+test_jar.setCookies([
+ "a=1;domain=.test.com;path=/"
+ , "a=1;domain=.test.com;path=/"
+ , "a=2;domain=.test.com;path=/"
+ , "b=3;domain=.test.com;path=/"]);
+var cookies=test_jar.getCookies(CookieAccessInfo("test.com","/"))
+assert.equal(cookies.length, 2);
+assert.equal(cookies[0].value, 2);
+
+// Test Ignore Trailing Semicolons (Github Issue #6)
+var cookie = new Cookie("a=1;domain=.test.com;path=/;;;;");
+assert.equal(cookie.name, "a");
+assert.equal(cookie.value, "1");
+assert.equal(cookie.domain, ".test.com");
+assert.equal(cookie.path, "/");
+assert.deepEqual(cookie, new Cookie("a=1;domain=.test.com;path=/"));
\ No newline at end of file
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-cookiejar.git
More information about the Pkg-javascript-commits
mailing list