Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support with sync mode #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

# Emacs temp file
*.js~
#*#
132 changes: 71 additions & 61 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use strict";

var FS = require('fs'),
var fs = require('fs'),
UTIL = require('util'),
Q = require('q'),
TOUGH = require('tough-cookie'),
canonicalDomain = TOUGH.canonicalDomain,
permuteDomain = TOUGH.permuteDomain,
permutePath = TOUGH.permutePath,
LOCKFILE = require('lockfile');
LOCKFILE = require('lockfile');

function isString (str) {
return typeof str === 'string' || str instanceof String;
Expand Down Expand Up @@ -43,13 +43,17 @@ function FileCookieStore(file, opt) {
throw new Error("Unknown file for read/write cookies");
}

if(!fs.existsSync(this.file)){
fs.writeFileSync(this.file,'');
}

this.idx = {};
}

UTIL.inherits(FileCookieStore, TOUGH.Store);

FileCookieStore.prototype.idx = null;
FileCookieStore.prototype.synchronous = false;
FileCookieStore.prototype.synchronous = true;


FileCookieStore.prototype.inspect = function() {
Expand All @@ -58,88 +62,93 @@ FileCookieStore.prototype.inspect = function() {


FileCookieStore.prototype._readFile = function (cb) {
var self = this;
Q.nfcall(FS.readFile, self.file, 'utf8').
then(function (data) {
self.readed = true;
if ( ! data ) { return cb(null, self); }
self.deserialize(data);
cb(null, self);
}).
catch(function(err){
if ( ! (err.code && err.code === 'ENOENT' && ! self.no_file_error ) )
cb(err);
else
cb();
}).done();
var data=null;

try{
data = fs.readFileSync(this.file, 'utf8');
}catch(e){
if(e.code === "ENOENT")
fs.writeFileSync(this.file, "# Netscape HTTP Cookie File\n" +
"# http://www.netscape.com/newsref/std/cookie_spec.html\n" +
"# This is a generated file! Do not edit.\n\n");
}

this.readed = true;
if(!data) {
return cb(null);
};

var err = null;
try{
this.deserialize(data);
}catch(e){
err = e;
}

cb(err);
};


FileCookieStore.prototype._read = function (cb) {
var self = this;
self._readFile(cb);
this._readFile(cb);
};


FileCookieStore.prototype._get_lock_func = function (disable_lock) {
var lock_file = lockFileName(this.file);

return ! disable_lock && this.lockfile ? Q.nfcall(LOCKFILE.lock, lock_file, {
retries : this.lockfile_retries,
retryWait : 50
}) : new Q();
if(! disable_lock && this.lockfile )
LOCKFILE.lockSync( lock_file);
};


FileCookieStore.prototype._get_unlock_func = function (disable_lock) {
var lock_file = lockFileName(this.file);
return ! disable_lock && this.lockfile ? Q.nfcall(LOCKFILE.unlock, lock_file)
: new Q();
if(! disable_lock && this.lockfile)
LOCKFILE.unlockSync( lock_file);
};


FileCookieStore.prototype._write = function (options, cb) {
var self = this,
data = this.serialize(this.idx);
var data = this.serialize(this.idx)
, err = null;

options = options || {};
cb = cb || noop;
self._get_lock_func(options.disable_lock).
then(function () {
return Q.nfcall(FS.writeFile, self.file, data, {mode : self.mode });
}).
then(function () {
cb();
}).
catch(function (err) {
cb(err);
}).
fin(function() {
return self._get_unlock_func(options.disable_lock);
}).
done();

this._get_lock_func(options.disable_lock);
try{
fs.writeFileSync(this.file, data, {mode : this.mode });
}catch(e){
err = e;
}

cb(err);

this._get_unlock_func(options.disable_lock);
};


FileCookieStore.prototype._update = function (updateFunc, cb) {
var self = this;
self._get_lock_func( ! self.auto_sync ).
then(function () {
return Q.nbind(self._read, self)();
}).
then(function () {
updateFunc();
return self.auto_sync ? Q.nbind(self._write, self)({disable_lock : true}) : new Q();
}).
then(function () {
cb();
}).
catch(function (err) {
cb(err);
}).
fin(function () {
return self._get_unlock_func(! self.auto_sync );
}).
done();
var err = null;
this._get_lock_func( ! this.auto_sync );// the file must be locked while auto_sync is true.
this._read(function(e){
if(e) err = e;
});

if(err){
cb(err);
return this._get_unlock_func(! this.auto_sync );
}

updateFunc();
if(this.auto_sync ){
this._write({disable_lock : true}, function(e){
if(e) err = e;
});
}

cb(err);
this._get_unlock_func(! this.auto_sync );
};


Expand Down Expand Up @@ -340,6 +349,7 @@ FileCookieStore.prototype._addCookie = function (cookie) {

FileCookieStore.prototype.putCookie = function (cookie, cb) {
var self = this;

this._update( function () {
self._addCookie(cookie);
}, cb);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"url": "https://github.com/JSBizon/file-cookie-store.git"
},
"scripts": {
"test": "grunt"
"test": "./node_modules/mocha/bin/mocha --reporter spec --bail --timeout 10000 test/*.js"
},
"keywords": [
"Netscape",
Expand Down
74 changes: 29 additions & 45 deletions test/file-store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('Test file cookie store', function() {

it('should throw exception if file not found', function (done) {
new FileCookieStore(COOKIES_TEST_FILE_NEW,{no_file_error: true}).findCookies('.ebay.com', null, function (err,cookies) {
expect(err).to.be.ok();
expect(err).not.to.be.ok();
done();
});
});
Expand Down Expand Up @@ -147,12 +147,9 @@ describe('Test file cookie store', function() {
it('should not find cookie(file not found)', function (done) {
var cookie_empty_store = new FileCookieStore(COOKIES_EMPTY_FILE);
cookie_empty_store.findCookie('.amazon.com', '/', 'skin', function (err, cookie) {
try {
expect(err).not.to.be.ok();
expect(cookie).not.to.be.ok();
} catch (e) {
return done(e);
}
expect(err).not.to.be.ok();
expect(cookie).not.to.be.ok();
FS.unlinkSync(COOKIES_EMPTY_FILE);
done();
});
});
Expand Down Expand Up @@ -263,51 +260,38 @@ describe('Test file cookie store', function() {


describe("#putCookie", function () {

afterEach(function(done){
try {
FS.unlinkSync(COOKIES_TEST_FILE_NEW);
if(FS.existsSync(COOKIES_TEST_FILE_NEW)){
FS.unlinkSync(COOKIES_TEST_FILE_NEW);
}

//FS.unlinkSync(COOKIES_TEST_FILE2);
} catch (err) {};
done();

done();
});


it ('should save cookie', function (done) {
cookie_store.findCookies('.ebay.com', null, function (err, cookies) {
try {
expect(err).not.to.be.ok();
expect(cookies).to.be.a(Array);
expect(cookies).to.have.length(5);

var fns = [],
cookie_store2 = new FileCookieStore(COOKIES_TEST_FILE_NEW);

cookies.forEach(function(cookie) {
var func = Q.nbind(cookie_store2.putCookie, cookie_store2);
fns.push(func(cookie));
});

var new_cookie_store = new FileCookieStore(COOKIES_TEST_FILE_NEW),
findCookies = Q.nbind(new_cookie_store.findCookies, new_cookie_store, '.ebay.com', null);

Q.all(fns).
then(function() {
return findCookies()
}).
then(function(cookies) {

expect(cookies).to.be.a(Array);
expect(cookies).to.have.length(5);
expect(err).not.to.be.ok();
expect(cookies).to.be.a(Array);
expect(cookies).to.have.length(5);

done();
}).
catch(function(err) {
done(err);
}).
done();
} catch (e) {
return done(e);
}
var cookie_store2 = new FileCookieStore(COOKIES_TEST_FILE_NEW);

cookies.forEach(function(cookie) {
cookie_store2.putCookie( cookie, function(e){
expect(e).not.to.be.ok();
} );
});

cookie_store2.findCookies( '.ebay.com', null, function(e, cookies){
expect(cookies).to.be.a(Array);
expect(cookies).to.have.length(5);
});

done();
});
});

Expand Down Expand Up @@ -969,4 +953,4 @@ describe('Test file cookie store', function() {
});
});

});
});