@mherod/get-cookie 1.1.2 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +62 -5
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -70,19 +70,24 @@ async function getFirefoxCookie({name, domain}) {
70
70
  return await doSqliteQuery1(file, sql);
71
71
  }
72
72
 
73
- async function getEncryptedChromeCookie(name, domain) {
73
+ const defaultChromeRoot = `${process.env.HOME}/Library/Application Support/Google/Chrome`;
74
+ const defaultChromeCookies = `${defaultChromeRoot}/Default/Cookies`;
75
+
76
+ async function getEncryptedChromeCookie(name, domain, file = defaultChromeCookies) {
74
77
  if (name && typeof name !== 'string') {
75
78
  throw new Error('name must be a string');
76
79
  }
77
80
  if (domain && typeof domain !== 'string') {
78
81
  throw new Error('domain must be a string');
79
82
  }
80
- const file = `${process.env.HOME}/Library/Application Support/Google/Chrome/Default/Cookies`;
83
+ if (file && typeof file !== 'string') {
84
+ throw new Error('file must be a string');
85
+ }
81
86
  if (!fs.existsSync(file)) {
82
87
  throw new Error(`File ${file} does not exist`);
83
88
  }
84
89
  if (process.env.VERBOSE) {
85
- console.log(`Trying Chrome cookie ${name} for domain ${domain}`);
90
+ console.log(`Trying Chrome (at ${file}) cookie ${name} for domain ${domain}`);
86
91
  }
87
92
  let sql;
88
93
  sql = `SELECT encrypted_value FROM cookies`;
@@ -248,9 +253,10 @@ async function getChromeCookie({name, domain}) {
248
253
  throw new Error('domain must be a string');
249
254
  }
250
255
  const chromePasswordPromise = getChromePassword();
251
- const encryptedChromeCookiePromise = getEncryptedChromeCookie(name, domain);
256
+ const [encryptedData] = await findAllFiles({path: defaultChromeRoot, name: 'Cookies'}).then(files => {
257
+ return Promise.all(files.map(file => getEncryptedChromeCookie(name, domain, file)));
258
+ });
252
259
  const password = await chromePasswordPromise;
253
- const encryptedData = await encryptedChromeCookiePromise;
254
260
  if (process.env.VERBOSE) {
255
261
  console.log("Received encrypted", encryptedData);
256
262
  }
@@ -269,6 +275,57 @@ async function getChromeCookie({name, domain}) {
269
275
  return s;
270
276
  }
271
277
 
278
+ /**
279
+ *
280
+ * @param path
281
+ * @param name
282
+ * @param rootSegments
283
+ * @param maxDepth
284
+ * @returns {Promise<[string]>}
285
+ */
286
+ async function findAllFiles({path, name, rootSegments = path.split('/').length, maxDepth = 2}) {
287
+ if (typeof path !== 'string') {
288
+ throw new Error('path must be a string');
289
+ }
290
+ if (typeof name !== 'string') {
291
+ throw new Error('name must be a string');
292
+ }
293
+ if (process.env.VERBOSE) {
294
+ console.log(`Searching for ${name} in ${path}`);
295
+ }
296
+ const files = [];
297
+ for (const file of fs.readdirSync(path)) {
298
+ const filePath = path + '/' + file;
299
+ const stat = fs.statSync(filePath);
300
+ if (stat.isDirectory()) {
301
+ if (filePath.split('/').length < rootSegments + maxDepth) {
302
+ try {
303
+ const subFiles = await findAllFiles({
304
+ path: filePath,
305
+ name: name,
306
+ rootSegments: rootSegments,
307
+ maxDepth: 2
308
+ });
309
+ files.push(...subFiles);
310
+ } catch (e) {
311
+ if (process.env.VERBOSE) {
312
+ console.error(e);
313
+ }
314
+ }
315
+ }
316
+ } else if (file === name) {
317
+ files.push(filePath);
318
+ }
319
+ }
320
+ if (process.env.VERBOSE) {
321
+ if (files.length > 0) {
322
+ console.log(`Found ${(files.length)} ${name} files`);
323
+ console.log(files);
324
+ }
325
+ }
326
+ return files;
327
+ }
328
+
272
329
  /**
273
330
  *
274
331
  * @param path
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mherod/get-cookie",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "Node.js module for querying a local user's Chrome cookie",
5
5
  "main": "index.js",
6
6
  "bin": "index.js",