gologin 2.1.21 → 2.1.22

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.21",
3
+ "version": "2.1.22",
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",
@@ -46,4 +46,4 @@
46
46
  "format": "npx prettier --single-quote src/* --write",
47
47
  "iphey": "GOLOGIN_PROFILE_ID= GOLOGIN_API_TOKEN= node examples/example-iphey "
48
48
  }
49
- }
49
+ }
@@ -7,11 +7,12 @@ import { homedir } from 'os';
7
7
  import { join } from 'path';
8
8
  import ProgressBar from 'progress';
9
9
  import util from 'util';
10
+ import { createInterface } from 'readline';
10
11
 
11
12
  import { API_URL, getOS } from '../utils/common.js';
12
13
 
13
14
  const exec = util.promisify(execNonPromise);
14
- const { access, mkdir, readdir, rmdir, unlink, copyFile, readlink, symlink, lstat, rename, writeFile } = _promises;
15
+ const { access, mkdir, readdir, rmdir, unlink, copyFile, readlink, symlink, lstat, rename, writeFile, readFile } = _promises;
15
16
 
16
17
  const PLATFORM = process.platform;
17
18
  const ARCH = process.arch;
@@ -44,15 +45,47 @@ export class BrowserChecker {
44
45
 
45
46
  }
46
47
 
47
- async checkBrowser(majorVersion) {
48
+ async checkBrowser({ autoUpdateBrowser, checkBrowserUpdate, majorVersion }) {
48
49
  const isBrowserFolderExists = await access(join(this.#browserPath, `orbita-browser-${majorVersion}`)).then(() => true).catch(() => false);
49
- if (isBrowserFolderExists) {
50
+ if (!isBrowserFolderExists) {
51
+ await this.downloadBrowser(majorVersion);
52
+
50
53
  return this.getBrowserExecutablePath(majorVersion);
51
54
  }
52
55
 
53
- await this.downloadBrowser(majorVersion);
56
+ const { latestVersion: browserLatestVersion } = await this.getLatestBrowserVersion();
57
+ const [latestBrowserMajorVersion] = browserLatestVersion.split('.');
58
+ const currentVersion = await this.getCurrentVersion(majorVersion);
59
+
60
+ const isCurrentVersionsLatest = majorVersion === latestBrowserMajorVersion;
61
+ if (browserLatestVersion === currentVersion || !(checkBrowserUpdate && isCurrentVersionsLatest)) {
62
+ return this.getBrowserExecutablePath(majorVersion);
63
+ }
64
+
65
+ if (autoUpdateBrowser) {
66
+ await this.downloadBrowser(majorVersion);
67
+
68
+ return this.getBrowserExecutablePath(majorVersion);
69
+ }
54
70
 
55
- return this.getBrowserExecutablePath(majorVersion);
71
+ return new Promise(resolve => {
72
+ const rl = createInterface(process.stdin, process.stdout);
73
+ const timeout = setTimeout(() => {
74
+ console.log(`\nContinue with current ${currentVersion} version.`);
75
+ resolve();
76
+ }, 10000);
77
+
78
+ rl.question(`New Orbita ${browserLatestVersion} is available. Update? [y/n] `, (answer) => {
79
+ clearTimeout(timeout);
80
+ rl.close();
81
+ if (answer && answer[0].toString().toLowerCase() === 'y') {
82
+ return this.downloadBrowser(majorVersion).then(() => resolve(this.getBrowserExecutablePath(majorVersion)));
83
+ }
84
+
85
+ console.log(`Continue with current ${currentVersion} version.`);
86
+ resolve(this.getBrowserExecutablePath(majorVersion));
87
+ });
88
+ });
56
89
  }
57
90
 
58
91
  async downloadBrowser(majorVersion) {
@@ -274,11 +307,7 @@ export class BrowserChecker {
274
307
  );
275
308
  }
276
309
 
277
- async deleteOldArchives(deleteCurrentBrowser = false) {
278
- if (deleteCurrentBrowser) {
279
- return this.deleteDir(join(this.#browserPath, 'orbita-browser'));
280
- }
281
-
310
+ async deleteOldArchives() {
282
311
  await this.deleteDir(join(this.#browserPath, EXTRACTED_FOLDER));
283
312
 
284
313
  return readdir(this.#browserPath)
@@ -315,15 +344,13 @@ export class BrowserChecker {
315
344
  }
316
345
  }
317
346
 
318
- getCurrentVersion() {
319
- let command = `if [ -f ${join(this.#browserPath, 'orbita-browser', 'version')} ]; then cat ${join(this.#browserPath, 'orbita-browser', 'version')}; else echo 0.0.0; fi`;
320
- if (PLATFORM === 'win32') {
321
- command = `if exist "${join(this.#browserPath, 'orbita-browser', 'version')}" (type "${join(this.#browserPath, 'orbita-browser', 'version')}") else (echo 0.0.0)`;
322
- } else if (PLATFORM === 'darwin') {
323
- command = `if [ -f ${join(this.#browserPath, 'orbita-browser', 'version', VERSION_FILE)} ]; then cat ${join(this.#browserPath, 'orbita-browser', 'version', VERSION_FILE)}; else echo 0.0.0; fi`;
347
+ async getCurrentVersion(majorVersion) {
348
+ let versionFilePath = join(this.#browserPath, `orbita-browser-${majorVersion}`, 'version');
349
+ if (PLATFORM === 'darwin') {
350
+ versionFilePath = join(this.#browserPath, `orbita-browser-${majorVersion}`, 'version', VERSION_FILE);
324
351
  }
325
352
 
326
- return exec(command);
353
+ return (await readFile(versionFilePath, 'utf8').catch(() => '0.0.0')).replace(/[\r\n\t\f\v\u0000-\u001F\u007F]/g, '');
327
354
  }
328
355
 
329
356
  getLatestBrowserVersion() {
package/src/gologin.js CHANGED
@@ -93,7 +93,38 @@ export class GoLogin {
93
93
  }
94
94
 
95
95
  async checkBrowser(majorVersion) {
96
- this.executablePath = await this.browserChecker.checkBrowser(majorVersion);
96
+ this.executablePath = await this.browserChecker.checkBrowser({
97
+ autoUpdateBrowser: this.autoUpdateBrowser,
98
+ checkBrowserUpdate: this.checkBrowserUpdate,
99
+ majorVersion,
100
+ });
101
+ }
102
+
103
+ async checkAndDownloadBrowserByOpts(opts = {}) {
104
+ const { majorVersions = [], lastActualCount = 5 } = opts;
105
+
106
+ let versionsToDownload = majorVersions;
107
+ if (!(Array.isArray(versionsToDownload) && versionsToDownload.length)) {
108
+ versionsToDownload = [];
109
+
110
+ const { latestVersion: browserLatestVersion } = await this.browserChecker.getLatestBrowserVersion();
111
+ const [latestBrowserMajorVersion] = browserLatestVersion.split('.');
112
+ const latestVersionNumber = Number(latestBrowserMajorVersion);
113
+
114
+ for (let i = latestVersionNumber; i > latestVersionNumber - lastActualCount; i--) {
115
+ versionsToDownload.push(i.toString());
116
+ }
117
+ }
118
+
119
+ console.log('versions to download:', versionsToDownload);
120
+
121
+ for (const majorVersion of versionsToDownload) {
122
+ await this.browserChecker.checkBrowser({
123
+ autoUpdateBrowser: true,
124
+ checkBrowserUpdate: true,
125
+ majorVersion,
126
+ });
127
+ }
97
128
  }
98
129
 
99
130
  async setProfileId(profile_id) {