Permalink
Browse files

Add request module support

  • Loading branch information...
1 parent 34fc62f commit 95f480675eaa8216f896fd5b434938ce2ff910a9 @mxfli committed Jan 3, 2012
Showing with 51 additions and 46 deletions.
  1. +46 −40 index.js
  2. +5 −6 test/test.js
View
@@ -2,56 +2,62 @@
//Author:@mxfli
//date::2011年 12月 13日 星期二 13:33:06 UTC
-var fs = require('fs'), url = require('url');
+var fs = require('fs');
+
var that = [];
+
/**
* Parse cookies file and return the result to cb;
*/
-function parse(file,cb){
- fs.readFile(file, function(err,buffer){
- if(err){ throw err;}
-
- var str=buffer.toString('utf8');
- console.log('Cookies.txt content: \n',str);
-
- var cookies=str.split('\n');
- //console.log(cookies.length);
- var result = [],cookieDefine=['domain','httponly','path','secure','expires','name','value'];
-
- cookies.forEach(function(line,index){
- //console.log(index,':',line);
- if(line.trim().length>0){
- var cookie = {};
- line.split(/\s/).forEach(function(c,index){
- if(cookieDefine[index] === 'expires'){c=(new Date(c*1000))}
- cookie[cookieDefine[index]]=c;
- });
-
- result.push(cookie);
- }
-
+function parse(file, cb) {
+ fs.readFile(file, function (err, buffer) {
+ if (err) { throw err;}
+
+ var str = buffer.toString('utf8');
+
+ //console.log('Cookies.txt content: \n', str);
+
+ var cookies = str.split('\n');
+ //console.log(cookies.length);
+ var result = [], cookieDefine = ['domain', 'httponly', 'path', 'secure', 'expires', 'name', 'value'];
+
+ cookies.forEach(function (line) {
+ //console.log(index,':',line);
+ line = line.trim();
+ if (line.length > 0 && !/^#/.test(line)) {
+ var cookie = {};
+ line.split(/\s/).forEach(function (c, index) {
+ if (cookieDefine[index] === 'expires') {c = (new Date(parseInt(c, 10) * 1000))}
+ cookie[cookieDefine[index]] = c;
});
- that = result;
- cb && cb(result);
+ result.push(cookie);
+ }
+
});
+ console.log("node-Cookies.txt load:", result.length, 'cookies.');
+ that = result;
+ cb && cb(result);
+ });
}
-
module.exports.parse = parse;
-module.exports.add = function(cookie){
- that = that.filter(function(c){
- // Avoid duplication (same path, same name)
- return !(c.name == cookie.name && c.path == cookie.path);
- });
- that.push(cookie);
-}
-module.exports.get = function(){return that};
-module.exports.getCookieString = function(){
- var result = that.map(function(cookie){
+module.exports.add = function (cookie) {
+ that = that.filter(function (c) {
+ // Avoid duplication (same path, same name)
+ return !(c.name == cookie.name && c.path == cookie.path);
+ });
+ that.push(cookie);
+};
+
+module.exports.get = function () {return that};
+
+module.exports.getCookieString = function () {
+ var result = that.map(
+ function (cookie) {
return cookie['name'] + '=' + cookie['value'];
- }).join(';');
+ }).join(';');
- console.log('Get cookies:',result);
- return result;
+ console.log('Get cookies:', result);
+ return result;
};
View
@@ -1,10 +1,9 @@
var ctp = require('../index.js');
+var cookieFile = __dirname + '/cookies.txt';
-ctp.parse(__dirname + '/cookies.txt', function(result){
-
- console.log('Cookie parse result:', result);
-
- console.log('get Cookie String:', ctp.getCookieString());
-});
+ctp.parse(cookieFile, function (result) {
+ console.log('Cookie parse result:', result);
+ console.log('get Cookie String:', ctp.getCookieString());
+});

0 comments on commit 95f4806

Please sign in to comment.