gologin 2.1.6 → 2.1.8

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gologin",
3
- "version": "2.1.6",
3
+ "version": "2.1.8",
4
4
  "description": "A high-level API to control Orbita browser over GoLogin API",
5
5
  "types": "./index.d.ts",
6
6
  "main": "./src/gologin.js",
@@ -57,6 +57,13 @@ export const createDBFile = async ({
57
57
  cookiesFileSecondPath && await fsPromises.copyFile(cookiesFilePath, cookiesFileSecondPath).catch(console.log);
58
58
  };
59
59
 
60
+ export const getUniqueCookies = async (cookiesArr, cookiesFilePath) => {
61
+ const cookiesInFile = await loadCookiesFromFile(cookiesFilePath);
62
+ const existingCookieNames = new Set(cookiesInFile.map(c => `${c.name}-${c.value.toString('base64')}`));
63
+
64
+ return cookiesArr.filter(cookie => !existingCookieNames.has(`${cookie.name}-${cookie.value.toString('base64')}`));
65
+ };
66
+
60
67
  export const getChunckedInsertValues = (cookiesArr) => {
61
68
  const todayUnix = Math.floor(new Date().getTime() / 1000.0);
62
69
  const chunckedCookiesArr = chunk(cookiesArr, MAX_SQLITE_VARIABLES);
package/src/gologin.js CHANGED
@@ -22,6 +22,7 @@ import {
22
22
  getChunckedInsertValues,
23
23
  getCookiesFilePath,
24
24
  getDB,
25
+ getUniqueCookies,
25
26
  loadCookiesFromFile,
26
27
  } from './cookies/cookies-manager.js';
27
28
  import ExtensionsManager from './extensions/extensions-manager.js';
@@ -397,7 +398,7 @@ export class GoLogin {
397
398
  const preferencesFilePath = _resolve(defaultFilePath, 'Preferences');
398
399
  const bookmarksFilePath = _resolve(defaultFilePath, 'Bookmarks');
399
400
  const cookiesFilePath = _resolve(defaultFilePath, 'Network', 'Cookies');
400
- const secondCookiesFilePath = _resolve(defaultFilePath, 'Cookies');
401
+ const cookiesFileSecondPath = _resolve(defaultFilePath, 'Cookies');
401
402
 
402
403
  await mkdir(_resolve(defaultFilePath, 'Network'), { recursive: true }).catch(console.log);
403
404
 
@@ -406,7 +407,7 @@ export class GoLogin {
406
407
  writeFile(bookmarksFilePath, JSON.stringify(zeroProfileBookmarks), { mode: 0o666 }),
407
408
  createDBFile({
408
409
  cookiesFilePath,
409
- secondCookiesFilePath,
410
+ cookiesFileSecondPath,
410
411
  createCookiesTableQuery,
411
412
  }),
412
413
  ]);
@@ -609,6 +610,7 @@ export class GoLogin {
609
610
  gologin.screenHeight = this.resolution.height;
610
611
  debug('writeCookiesFromServer', this.writeCookiesFromServer);
611
612
  this.cookiesFilePath = await getCookiesFilePath(this.profile_id, this.tmpdir);
613
+
612
614
  if (this.writeCookiesFromServer) {
613
615
  await this.writeCookiesToFile(profile.cookies?.cookies);
614
616
  }
@@ -1353,28 +1355,29 @@ export class GoLogin {
1353
1355
  cookies = await this.getCookies(this.profile_id);
1354
1356
  }
1355
1357
 
1358
+ if (!cookies?.length) {
1359
+ return;
1360
+ }
1361
+
1356
1362
  const resultCookies = cookies.map((el) => ({ ...el, value: Buffer.from(el.value) }));
1357
1363
  let db;
1358
1364
  const profilePath = join(this.tmpdir, `gologin_profile_${this.profile_id}`);
1359
- console.log('profilePath', profilePath);
1365
+
1360
1366
  const defaultFilePath = _resolve(profilePath, 'Default');
1361
- const cookiesFilePath = _resolve(defaultFilePath, 'Network', 'Cookies');
1362
- const secondCookiesFilePath = _resolve(defaultFilePath, 'Cookies');
1367
+ const cookiesFilePath = _resolve(defaultFilePath, 'Cookies');
1368
+ const secondCookiesFilePath = _resolve(defaultFilePath, 'Network', 'Cookies');
1363
1369
  try {
1364
1370
  db = await getDB(cookiesFilePath, false);
1365
- if (resultCookies.length) {
1366
- const chunckInsertValues = getChunckedInsertValues(resultCookies);
1367
-
1371
+ const cookiesToInsert = await getUniqueCookies(resultCookies, cookiesFilePath);
1372
+ // console.log('cookiesToInsert', cookiesToInsert);
1373
+ if (cookiesToInsert.length) {
1374
+ const chunckInsertValues = getChunckedInsertValues(cookiesToInsert);
1375
+ console.log('chunckInsertValues', chunckInsertValues);
1368
1376
  for (const [query, queryParams] of chunckInsertValues) {
1369
1377
  const insertStmt = await db.prepare(query);
1370
1378
  await insertStmt.run(queryParams);
1371
1379
  await insertStmt.finalize();
1372
1380
  }
1373
- } else {
1374
- const query = 'delete from cookies';
1375
- const insertStmt = await db.prepare(query);
1376
- await insertStmt.run();
1377
- await insertStmt.finalize();
1378
1381
  }
1379
1382
  } catch (error) {
1380
1383
  console.log(error.message);