[Pkg-javascript-commits] [node-cookiejar] 02/08: Imported Upstream version 2.0.1
Leo Iannacone
l3on-guest at moszumanska.debian.org
Sun Oct 12 10:17:51 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 20d8df66d3f406ea76aa8e728385b9e794bbcd07
Author: Leo Iannacone <l3on at ubuntu.com>
Date: Sun Oct 12 12:07:06 2014 +0200
Imported Upstream version 2.0.1
---
cookiejar.js | 464 +++++++++++++++++++++++++++++++---------------------------
package.json | 9 +-
readme.md | 24 ++-
tests/test.js | 36 ++++-
4 files changed, 305 insertions(+), 228 deletions(-)
diff --git a/cookiejar.js b/cookiejar.js
index deda7c7..f2f263e 100644
--- a/cookiejar.js
+++ b/cookiejar.js
@@ -1,226 +1,260 @@
-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)
+/* jshint node: true */
+(function () {
+ "use strict";
+
+ 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;
+ }
+ return new CookieAccessInfo(domain, path, secure, script);
}
-}
+ exports.CookieAccessInfo = CookieAccessInfo;
-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)
+ function Cookie(cookiestr, request_domain, request_path) {
+ if (cookiestr instanceof Cookie) {
+ return cookiestr;
+ }
+ if (this instanceof Cookie) {
+ this.name = null;
+ this.value = null;
+ this.expiration_date = Infinity;
+ this.path = String(request_path || "/");
+ this.explicit_path = false;
+ this.domain = request_domain || null;
+ this.explicit_domain = false;
+ this.secure = false; //how to define default?
+ this.noscript = false; //httponly
+ if (cookiestr) {
+ this.parse(cookiestr, request_domain, request_path);
+ }
+ return this;
+ }
+ return new Cookie(cookiestr);
}
-}
+ exports.Cookie = Cookie;
+
+ 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;
+ };
-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("; ");
-}
+ var cookie_str_splitter = /[:](?=\s*[a-zA-Z0-9_\-]+\s*[=])/g;
+ Cookie.prototype.parse = function parse(str, request_domain, request_path) {
+ if (this instanceof Cookie) {
+ var parts = str.split(";").filter(function (value) {
+ return !!value;
+ }),
+ pair = parts[0].match(/([^=]+)=([\s\S]*)/),
+ key = pair[1],
+ value = pair[2],
+ i;
+ this.name = key;
+ this.value = value;
-Cookie.prototype.toValueString = function toValueString() {
- return this.name+"="+this.value;
-}
+ for (i = 1; i < parts.length; i += 1) {
+ pair = parts[i].match(/([^=]+)(?:=([\s\S]*))?/);
+ 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() :
+ "";
+ this.explicit_path = true;
+ break;
+ case "domain":
+ this.domain = value ?
+ value.trim() :
+ "";
+ this.explicit_domain = !!this.domain;
+ break;
+ case "secure":
+ this.secure = true;
+ break;
+ }
+ }
-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)
-}
+ if (!this.explicit_path) {
+ this.path = request_path || "/";
+ }
+ if (!this.explicit_domain) {
+ this.domain = request_domain;
+ }
-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;
-}
+ return this;
+ }
+ return new Cookie().parse(str, request_domain, request_path);
+ };
+
+ 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.explicit_path) {
+ if (this.path !== access_info.path) {
+ return false;
+ }
+ }
+ var access_domain = access_info.domain && access_info.domain.replace(/^[\.]/,'');
+ var cookie_domain = this.domain && this.domain.replace(/^[\.]/,'');
+ if (cookie_domain === access_domain) {
+ return true;
+ }
+ if (cookie_domain) {
+ if (!this.explicit_domain) {
+ return false; // we already checked if the domains were exactly the same
+ }
+ var wildcard = access_domain.indexOf(cookie_domain);
+ if (wildcard === -1 || wildcard !== access_domain.length - cookie_domain.length) {
+ return false;
+ }
+ return true;
+ }
+ 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;
-}
+ function CookieJar() {
+ var cookies, cookies_list, collidable_cookie;
+ if (this instanceof CookieJar) {
+ cookies = Object.create(null); //name: [Cookie]
-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()
-}
+ this.setCookie = function setCookie(cookie, request_domain, request_path) {
+ var remove, i;
+ cookie = new Cookie(cookie, request_domain, request_path);
+ //Delete the cookie if the set is past the current time
+ remove = cookie.expiration_date <= Date.now();
+ if (cookies[cookie.name] !== undefined) {
+ cookies_list = cookies[cookie.name];
+ for (i = 0; i < cookies_list.length; i += 1) {
+ 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;
+ }
+ cookies_list[i] = cookie;
+ return cookie;
+ }
+ }
+ if (remove) {
+ return false;
+ }
+ cookies_list.push(cookie);
+ return cookie;
+ }
+ if (remove) {
+ return false;
+ }
+ cookies[cookie.name] = [cookie];
+ return cookies[cookie.name];
+ };
+ //returns a cookie
+ this.getCookie = function getCookie(cookie_name, access_info) {
+ var cookie, i;
+ cookies_list = cookies[cookie_name];
+ if (!cookies_list) {
+ return;
+ }
+ for (i = 0; i < cookies_list.length; i += 1) {
+ 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 = [], cookie_name, cookie;
+ for (cookie_name in cookies) {
+ cookie = this.getCookie(cookie_name, access_info);
+ if (cookie) {
+ matches.push(cookie);
+ }
+ }
+ matches.toString = function toString() {
+ return matches.join(":");
+ };
+ matches.toValueString = function toValueString() {
+ return matches.map(function (c) {
+ return c.toValueString();
+ }).join(';');
+ };
+ return matches;
+ };
+ return this;
+ }
+ return new CookieJar();
+ }
+ exports.CookieJar = 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;
-}
+ //returns list of cookies that were set correctly. Cookies that are expired and removed are not returned.
+ CookieJar.prototype.setCookies = function setCookies(cookies, request_domain, request_path) {
+ cookies = Array.isArray(cookies) ?
+ cookies :
+ cookies.split(cookie_str_splitter);
+ var successful = [],
+ i,
+ cookie;
+ cookies = cookies.map(Cookie);
+ for (i = 0; i < cookies.length; i += 1) {
+ cookie = cookies[i];
+ if (this.setCookie(cookie, request_domain, request_path)) {
+ successful.push(cookie);
+ }
+ }
+ return successful;
+ };
+}());
diff --git a/package.json b/package.json
index a4b0ddd..d35732e 100644
--- a/package.json
+++ b/package.json
@@ -1,12 +1,19 @@
{
"name": "cookiejar",
- "version": "1.3.2",
+ "version": "2.0.1",
"author": {
"name": "bradleymeck"
},
"main": "cookiejar.js",
"description": "simple persistent cookiejar system",
"license": "MIT",
+ "jshintConfig": {
+ "node": true
+ },
+ "scripts": {
+ "prepublish": "jshint cookiejar.js && git tag $npm_package_version && git push origin master && git push origin --tags",
+ "test": "tests/test.js"
+ },
"repository": {
"type": "git",
"url": "https://github.com/bmeck/node-cookiejar.git"
diff --git a/readme.md b/readme.md
index 43f1799..3d39ec6 100644
--- a/readme.md
+++ b/readme.md
@@ -4,42 +4,54 @@ 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)
+###Cookie(cookiestr_or_cookie, request_domain, request_path)
+
turns input into a Cookie (singleton if given a Cookie)
+ the `request_domain` argument is used to default the domain if it is not explicit in the cookie string
+ the `request_path` argument is used to set the path if it is not explicit in a cookie String.
+
+ explicit domains/paths will cascade, implied domains/paths must *exactly* match (see http://en.wikipedia.org/wiki/HTTP_cookie#Domain_and_Pat)
#####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)
+* Boolean explicit_domain - if the domain was explicitly set via the cookie string
* String path - base path to match (matches any path starting with this '/' is root)
+* Boolean explicit_path - if the path was explicitly set via the cookie string
* 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
+* Cookie parse(cookiestr, request_domain, request_path) - 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 setCookie(cookie, request_domain, request_path) - add a cookie to the jar
+* Cookie[] setCookies(cookiestr_or_list, request_domain, request_path) - 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
+* Cookie[] getCookies(access_info) - grab all cookies matching this access_info
diff --git a/tests/test.js b/tests/test.js
old mode 100644
new mode 100755
index b2aba44..c39d4da
--- a/tests/test.js
+++ b/tests/test.js
@@ -1,7 +1,8 @@
-var Cookie=require("../cookiejar")
-, CookieAccessInfo = Cookie.CookieAccessInfo
-, CookieJar = Cookie.CookieJar
-, Cookie = Cookie.Cookie
+#!/usr/bin/env node
+var Cookie=require("../cookiejar"),
+ CookieAccessInfo = Cookie.CookieAccessInfo,
+ CookieJar = Cookie.CookieJar,
+ Cookie = Cookie.Cookie;
var assert = require('assert');
@@ -17,6 +18,9 @@ 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=/")));
+var cookie = new Cookie("a=1;path=/", ".test.com");
+assert.equal(cookie.domain, ".test.com");
+
// Test CookieJar
var test_jar = CookieJar();
@@ -29,7 +33,7 @@ assert.equal(cookies.length, 2, "Expires on setCookies fail\n" + cookies.toStrin
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());
+assert.equal(cookies.length, 2, "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","/"))
@@ -55,4 +59,24 @@ 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
+assert.deepEqual(cookie, new Cookie("a=1;domain=.test.com;path=/"));
+
+// Test request_path and request_domain
+test_jar.setCookie(new Cookie("sub=4;path=/", "test.com"));
+var cookie = test_jar.getCookie("sub", CookieAccessInfo("sub.test.com", "/"));
+assert.equal(cookie, undefined);
+
+var cookie = test_jar.getCookie("sub", CookieAccessInfo("test.com", "/"));
+assert.equal(cookie.name, "sub");
+assert.equal(cookie.domain, "test.com");
+
+test_jar.setCookie(new Cookie("sub=4;", "test.com", "/accounts"));
+var cookie = test_jar.getCookie("sub", CookieAccessInfo("test.com", "/"));
+assert.equal(cookie, undefined);
+
+var cookie = test_jar.getCookie("sub", CookieAccessInfo("test.com", "/accounts"));
+assert.equal(cookie.path, "/accounts");
+
+test_jar.setCookie(new Cookie("sub=5;path=/", "test.com", "/accounts"));
+var cookies = test_jar.getCookies(CookieAccessInfo("test.com"));
+assert.equal(cookies.length, 3);
--
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