From 4d74164f33cf802a151f2e6b09cb666a55e66327 Mon Sep 17 00:00:00 2001 From: Bertrand Fan Date: Fri, 30 Jan 2015 13:44:35 -0800 Subject: [PATCH] Bug where the sqlite DB was closed so subsequent getCookies failed --- example/double-smarf.js | 14 ++++++++++++++ index.js | 17 +++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 example/double-smarf.js diff --git a/example/double-smarf.js b/example/double-smarf.js new file mode 100644 index 0000000..0f5e0a9 --- /dev/null +++ b/example/double-smarf.js @@ -0,0 +1,14 @@ +var chrome = require('../index'); + +chrome.getCookies('http://smarf.toomanycooks.kitchen', function (err, cookies) { + chrome.getCookies('http://smarf.toomanycooks.kitchen', function (err, cookies) { + + if (err) { + console.error(err); + return; + } + + console.log(cookies); + + }); +}); diff --git a/index.js b/index.js index 80abc85..f54db21 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,8 @@ var sqlite3 = require('sqlite3'), crypto = require('crypto'), Cookie = tough.Cookie, path, - ITERATIONS; + ITERATIONS, + dbClosed = false; if (process.platform === 'darwin') { @@ -223,6 +224,11 @@ var getCookies = function (uri, format, callback) { return callback(new Error('Could not parse URI, format should be http://www.example.com/path/')); } + if (dbClosed) { + db = new sqlite3.Database(path); + dbClosed = false; + } + getDerivedKey(function (err, derivedKey) { if (err) { @@ -312,14 +318,17 @@ var getCookies = function (uri, format, callback) { } - return callback(null, output); + db.close(function(err) { + if (!err) { + dbClosed = true; + } + return callback(null, output); + }); }); }); - db.close(); - }); };