gologin-commonjs 3.0.2 → 3.0.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.
@@ -1,17 +1,9 @@
1
1
  "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
- value: true,
4
+ value: true
5
5
  });
6
- exports.uploadCookies =
7
- exports.setOriginalExtPaths =
8
- exports.setExtPathsAndRemoveDeleted =
9
- exports.recalculateId =
10
- exports.downloadFonts =
11
- exports.downloadCookies =
12
- exports.copyFontsConfigFile =
13
- exports.composeFonts =
14
- void 0;
6
+ exports.uploadCookies = exports.setOriginalExtPaths = exports.setExtPathsAndRemoveDeleted = exports.recalculateId = exports.downloadFonts = exports.downloadCookies = exports.copyFontsConfigFile = exports.composeFonts = void 0;
15
7
  var _crypto = require("crypto");
16
8
  var _fs = require("fs");
17
9
  var _os = require("os");
@@ -19,69 +11,65 @@ var _path = require("path");
19
11
  var _requestretry = _interopRequireDefault(require("requestretry"));
20
12
  var _url = require("url");
21
13
  var _fonts = require("../../fonts.js");
22
- function _interopRequireDefault(e) {
23
- return e && e.__esModule ? e : { default: e };
24
- }
25
- const { access, readFile, writeFile, mkdir, readdir, copyFile, rename } =
26
- _fs.promises;
27
- const _filename = (0, _url.fileURLToPath)(
28
- require("url").pathToFileURL(__filename).toString()
29
- );
14
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
+ const {
16
+ access,
17
+ readFile,
18
+ writeFile,
19
+ mkdir,
20
+ readdir,
21
+ copyFile,
22
+ rename
23
+ } = _fs.promises;
24
+ const _filename = (0, _url.fileURLToPath)(require('url').pathToFileURL(_filename).toString());
30
25
  const _dirname = (0, _path.dirname)(_filename);
31
- const FONTS_URL = "https://fonts.gologin.com/";
32
- const FONTS_DIR_NAME = "fonts";
26
+ const FONTS_URL = 'https://fonts.gologin.com/';
27
+ const FONTS_DIR_NAME = 'fonts';
33
28
  const HOMEDIR = (0, _os.homedir)();
34
- const BROWSER_PATH = (0, _path.join)(HOMEDIR, ".gologin", "browser");
29
+ const BROWSER_PATH = (0, _path.join)(HOMEDIR, '.gologin', 'browser');
35
30
  const OS_PLATFORM = process.platform;
36
- const DEFAULT_ORBITA_EXTENSIONS_NAMES = [
37
- "Google Hangouts",
38
- "Chromium PDF Viewer",
39
- "CryptoTokenExtension",
40
- "Web Store",
41
- ];
42
- const GOLOGIN_BASE_FOLDER_NAME = ".gologin";
43
- const GOLOGIN_TEST_FOLDER_NAME = ".gologin_test";
31
+ const DEFAULT_ORBITA_EXTENSIONS_NAMES = ['Google Hangouts', 'Chromium PDF Viewer', 'CryptoTokenExtension', 'Web Store'];
32
+ const GOLOGIN_BASE_FOLDER_NAME = '.gologin';
33
+ const GOLOGIN_TEST_FOLDER_NAME = '.gologin_test';
44
34
  const osPlatform = process.platform;
45
- const downloadCookies = ({ profileId, ACCESS_TOKEN, API_BASE_URL }) =>
46
- _requestretry.default
47
- .get(`${API_BASE_URL}/browser/${profileId}/cookies`, {
48
- headers: {
49
- Authorization: `Bearer ${ACCESS_TOKEN}`,
50
- "user-agent": "gologin-api",
51
- },
52
- json: true,
53
- maxAttempts: 3,
54
- retryDelay: 2000,
55
- timeout: 10 * 1000,
56
- })
57
- .catch((e) => {
58
- console.log(e);
59
- return {
60
- body: [],
61
- };
62
- });
35
+ const downloadCookies = ({
36
+ profileId,
37
+ ACCESS_TOKEN,
38
+ API_BASE_URL
39
+ }) => _requestretry.default.get(`${API_BASE_URL}/browser/${profileId}/cookies`, {
40
+ headers: {
41
+ Authorization: `Bearer ${ACCESS_TOKEN}`,
42
+ 'user-agent': 'gologin-api'
43
+ },
44
+ json: true,
45
+ maxAttempts: 3,
46
+ retryDelay: 2000,
47
+ timeout: 10 * 1000
48
+ }).catch(e => {
49
+ console.log(e);
50
+ return {
51
+ body: []
52
+ };
53
+ });
63
54
  exports.downloadCookies = downloadCookies;
64
55
  const uploadCookies = ({
65
56
  cookies = [],
66
57
  profileId,
67
58
  ACCESS_TOKEN,
68
- API_BASE_URL,
69
- }) =>
70
- _requestretry.default
71
- .post(`${API_BASE_URL}/browser/${profileId}/cookies?encrypted=true`, {
72
- headers: {
73
- Authorization: `Bearer ${ACCESS_TOKEN}`,
74
- "User-Agent": "gologin-api",
75
- },
76
- json: cookies,
77
- maxAttempts: 3,
78
- retryDelay: 2000,
79
- timeout: 20 * 1000,
80
- })
81
- .catch((e) => {
82
- console.log(e);
83
- return e;
84
- });
59
+ API_BASE_URL
60
+ }) => _requestretry.default.post(`${API_BASE_URL}/browser/${profileId}/cookies?encrypted=true`, {
61
+ headers: {
62
+ Authorization: `Bearer ${ACCESS_TOKEN}`,
63
+ 'User-Agent': 'gologin-api'
64
+ },
65
+ json: cookies,
66
+ maxAttempts: 3,
67
+ retryDelay: 2000,
68
+ timeout: 20 * 1000
69
+ }).catch(e => {
70
+ console.log(e);
71
+ return e;
72
+ });
85
73
  exports.uploadCookies = uploadCookies;
86
74
  const downloadFonts = async (fontsList = [], profilePath) => {
87
75
  if (!fontsList.length) {
@@ -89,159 +77,107 @@ const downloadFonts = async (fontsList = [], profilePath) => {
89
77
  }
90
78
  const browserFontsPath = (0, _path.join)(BROWSER_PATH, FONTS_DIR_NAME);
91
79
  await mkdir(browserFontsPath, {
92
- recursive: true,
80
+ recursive: true
93
81
  });
94
82
  const files = await readdir(browserFontsPath);
95
- const fontsToDownload = fontsList.filter((font) => !files.includes(font));
96
- let promises = fontsToDownload.map((font) =>
97
- _requestretry.default
98
- .get(FONTS_URL + font, {
99
- maxAttempts: 5,
100
- retryDelay: 2000,
101
- timeout: 30 * 1000,
102
- })
103
- .pipe((0, _fs.createWriteStream)((0, _path.join)(browserFontsPath, font)))
104
- );
83
+ const fontsToDownload = fontsList.filter(font => !files.includes(font));
84
+ let promises = fontsToDownload.map(font => _requestretry.default.get(FONTS_URL + font, {
85
+ maxAttempts: 5,
86
+ retryDelay: 2000,
87
+ timeout: 30 * 1000
88
+ }).pipe((0, _fs.createWriteStream)((0, _path.join)(browserFontsPath, font))));
105
89
  if (promises.length) {
106
90
  await Promise.all(promises);
107
91
  }
108
- promises = fontsList.map((font) =>
109
- copyFile(
110
- (0, _path.join)(browserFontsPath, font),
111
- (0, _path.join)(profilePath, FONTS_DIR_NAME, font)
112
- )
113
- );
92
+ promises = fontsList.map(font => copyFile((0, _path.join)(browserFontsPath, font), (0, _path.join)(profilePath, FONTS_DIR_NAME, font)));
114
93
  await Promise.all(promises);
115
94
  };
116
95
  exports.downloadFonts = downloadFonts;
117
- const composeFonts = async (
118
- fontsList = [],
119
- profilePath,
120
- differentOs = false
121
- ) => {
96
+ const composeFonts = async (fontsList = [], profilePath, differentOs = false) => {
122
97
  if (!(fontsList.length && profilePath)) {
123
98
  return;
124
99
  }
125
- const fontsToDownload = _fonts.fontsCollection
126
- .filter((elem) => fontsList.includes(elem.value))
127
- .reduce((res, elem) => res.concat(elem.fileNames || []), []);
100
+ const fontsToDownload = _fonts.fontsCollection.filter(elem => fontsList.includes(elem.value)).reduce((res, elem) => res.concat(elem.fileNames || []), []);
128
101
  if (differentOs && !fontsToDownload.length) {
129
- throw new Error(
130
- "No fonts to download found. Use getAvailableFonts() method and set some fonts from this list"
131
- );
102
+ throw new Error('No fonts to download found. Use getAvailableFonts() method and set some fonts from this list');
132
103
  }
133
- fontsToDownload.push("LICENSE.txt");
134
- fontsToDownload.push("OFL.txt");
104
+ fontsToDownload.push('LICENSE.txt');
105
+ fontsToDownload.push('OFL.txt');
135
106
  const pathToFontsDir = (0, _path.join)(profilePath, FONTS_DIR_NAME);
136
- const fontsDirExists = await access(pathToFontsDir).then(
137
- () => true,
138
- () => false
139
- );
107
+ const fontsDirExists = await access(pathToFontsDir).then(() => true, () => false);
140
108
  if (fontsDirExists) {
141
109
  (0, _fs.rmdirSync)(pathToFontsDir, {
142
- recursive: true,
110
+ recursive: true
143
111
  });
144
112
  }
145
113
  await mkdir(pathToFontsDir, {
146
- recursive: true,
114
+ recursive: true
147
115
  });
148
116
  await downloadFonts(fontsToDownload, profilePath);
149
- if (OS_PLATFORM === "linux") {
117
+ if (OS_PLATFORM === 'linux') {
150
118
  await copyFontsConfigFile(profilePath);
151
119
  }
152
120
  };
153
121
  exports.composeFonts = composeFonts;
154
- const copyFontsConfigFile = async (profilePath) => {
122
+ const copyFontsConfigFile = async profilePath => {
155
123
  if (!profilePath) {
156
124
  return;
157
125
  }
158
- const fileContent = await readFile(
159
- (0, _path.resolve)(_dirname, "..", "..", "fonts_config"),
160
- "utf-8"
161
- );
162
- const result = fileContent.replace(
163
- /\$\$GOLOGIN_FONTS\$\$/g,
164
- (0, _path.join)(profilePath, FONTS_DIR_NAME)
165
- );
166
- const defaultFolderPath = (0, _path.join)(profilePath, "Default");
126
+ const fileContent = await readFile((0, _path.resolve)(_dirname, '..', '..', 'fonts_config'), 'utf-8');
127
+ const result = fileContent.replace(/\$\$GOLOGIN_FONTS\$\$/g, (0, _path.join)(profilePath, FONTS_DIR_NAME));
128
+ const defaultFolderPath = (0, _path.join)(profilePath, 'Default');
167
129
  await mkdir(defaultFolderPath, {
168
- recursive: true,
130
+ recursive: true
169
131
  });
170
- await writeFile((0, _path.join)(defaultFolderPath, "fonts_config"), result);
132
+ await writeFile((0, _path.join)(defaultFolderPath, 'fonts_config'), result);
171
133
  };
172
134
  exports.copyFontsConfigFile = copyFontsConfigFile;
173
- const setExtPathsAndRemoveDeleted = (
174
- settings = {},
175
- profileExtensionsCheckRes = [],
176
- profileId = ""
177
- ) => {
178
- const formattedLocalExtArray = profileExtensionsCheckRes
179
- .map((el) => {
180
- const [extFolderName = ""] = el.split(_path.sep).reverse();
181
- const [originalId] = extFolderName.split("@");
182
- if (!originalId) {
183
- return null;
184
- }
185
- return {
186
- path: el,
187
- originalId,
188
- };
189
- })
190
- .filter(Boolean);
135
+ const setExtPathsAndRemoveDeleted = (settings = {}, profileExtensionsCheckRes = [], profileId = '') => {
136
+ const formattedLocalExtArray = profileExtensionsCheckRes.map(el => {
137
+ const [extFolderName = ''] = el.split(_path.sep).reverse();
138
+ const [originalId] = extFolderName.split('@');
139
+ if (!originalId) {
140
+ return null;
141
+ }
142
+ return {
143
+ path: el,
144
+ originalId
145
+ };
146
+ }).filter(Boolean);
191
147
  const extensionsSettings = settings.extensions?.settings || {};
192
148
  const extensionsEntries = Object.entries(extensionsSettings);
193
- const promises = extensionsEntries.map(async (extensionObj) => {
149
+ const promises = extensionsEntries.map(async extensionObj => {
194
150
  let [extensionId, currentExtSettings = {}] = extensionObj;
195
- const extName = currentExtSettings.manifest?.name || "";
196
- let extPath = currentExtSettings.path || "";
197
- let originalId = "";
198
- const isExtensionToBeDeleted =
199
- (["resources", "passwords-ext", "cookies-ext"].some((substring) =>
200
- extPath.includes(substring)
201
- ) &&
202
- [GOLOGIN_BASE_FOLDER_NAME, GOLOGIN_TEST_FOLDER_NAME].some((substring) =>
203
- extPath.includes(substring)
204
- )) ||
205
- (DEFAULT_ORBITA_EXTENSIONS_NAMES.includes(extName) &&
206
- [GOLOGIN_BASE_FOLDER_NAME, GOLOGIN_TEST_FOLDER_NAME].some((substring) =>
207
- extPath.includes(substring)
208
- ));
151
+ const extName = currentExtSettings.manifest?.name || '';
152
+ let extPath = currentExtSettings.path || '';
153
+ let originalId = '';
154
+ const isExtensionToBeDeleted = ['resources', 'passwords-ext', 'cookies-ext'].some(substring => extPath.includes(substring)) && [GOLOGIN_BASE_FOLDER_NAME, GOLOGIN_TEST_FOLDER_NAME].some(substring => extPath.includes(substring)) || DEFAULT_ORBITA_EXTENSIONS_NAMES.includes(extName) && [GOLOGIN_BASE_FOLDER_NAME, GOLOGIN_TEST_FOLDER_NAME].some(substring => extPath.includes(substring));
209
155
  if (isExtensionToBeDeleted) {
210
156
  delete extensionsSettings[extensionId];
211
157
  return;
212
158
  }
213
- if (osPlatform === "win32") {
214
- extPath = extPath.replace(/\//g, "\\");
159
+ if (osPlatform === 'win32') {
160
+ extPath = extPath.replace(/\//g, '\\');
215
161
  } else {
216
- extPath = extPath.replace(/\\/g, "/");
162
+ extPath = extPath.replace(/\\/g, '/');
217
163
  }
218
164
  extensionsSettings[extensionId].path = extPath;
219
165
  const splittedPath = extPath.split(_path.sep);
220
- const isExtensionManageable =
221
- ["chrome-extensions", "user-extensions"].some((substring) =>
222
- extPath.includes(substring)
223
- ) &&
224
- [GOLOGIN_BASE_FOLDER_NAME, GOLOGIN_TEST_FOLDER_NAME].some((substring) =>
225
- extPath.includes(substring)
226
- );
166
+ const isExtensionManageable = ['chrome-extensions', 'user-extensions'].some(substring => extPath.includes(substring)) && [GOLOGIN_BASE_FOLDER_NAME, GOLOGIN_TEST_FOLDER_NAME].some(substring => extPath.includes(substring));
227
167
  if (isExtensionManageable) {
228
168
  const [extFolderName] = extPath.split(_path.sep).reverse();
229
- [originalId] = extFolderName.split("@");
169
+ [originalId] = extFolderName.split('@');
230
170
  } else if (splittedPath.length === 2) {
231
171
  [originalId] = splittedPath;
232
172
  }
233
173
  if (isExtensionManageable || splittedPath.length === 2) {
234
- const isExtensionInProfileSettings = formattedLocalExtArray.find((el) =>
235
- el.path.includes(originalId)
236
- );
174
+ const isExtensionInProfileSettings = formattedLocalExtArray.find(el => el.path.includes(originalId));
237
175
  if (!isExtensionInProfileSettings) {
238
176
  delete extensionsSettings[extensionId];
239
177
  return;
240
178
  }
241
179
  }
242
- const localExtObj =
243
- originalId &&
244
- formattedLocalExtArray.find((el) => el.path.includes(originalId));
180
+ const localExtObj = originalId && formattedLocalExtArray.find(el => el.path.includes(originalId));
245
181
  if (!localExtObj) {
246
182
  return;
247
183
  }
@@ -250,82 +186,62 @@ const setExtPathsAndRemoveDeleted = (
250
186
  localExtObj,
251
187
  extensionId,
252
188
  extensionsSettings,
253
- currentExtSettings,
189
+ currentExtSettings
254
190
  });
255
191
  if (initialExtName !== extensionId) {
256
- const profilePath = (0, _path.join)(
257
- (0, _os.tmpdir)(),
258
- `gologin_profile_${profileId}`
259
- );
260
- const extSyncFolder = (0, _path.join)(
261
- profilePath,
262
- "Default",
263
- "Sync Extension Settings",
264
- initialExtName
265
- );
266
- const newSyncFolder = (0, _path.join)(
267
- profilePath,
268
- "Default",
269
- "Sync Extension Settings",
270
- extensionId
271
- );
192
+ const profilePath = (0, _path.join)((0, _os.tmpdir)(), `gologin_profile_${profileId}`);
193
+ const extSyncFolder = (0, _path.join)(profilePath, 'Default', 'Sync Extension Settings', initialExtName);
194
+ const newSyncFolder = (0, _path.join)(profilePath, 'Default', 'Sync Extension Settings', extensionId);
272
195
  await rename(extSyncFolder, newSyncFolder).catch(() => null);
273
196
  }
274
- if (localExtObj.path.endsWith(".zip")) {
275
- localExtObj.path = localExtObj.path.replace(".zip", "");
197
+ if (localExtObj.path.endsWith('.zip')) {
198
+ localExtObj.path = localExtObj.path.replace('.zip', '');
276
199
  }
277
- extensionsSettings[extensionId].path = localExtObj.path || "";
200
+ extensionsSettings[extensionId].path = localExtObj.path || '';
278
201
  });
279
202
  return Promise.all(promises).then(() => extensionsSettings);
280
203
  };
281
204
  exports.setExtPathsAndRemoveDeleted = setExtPathsAndRemoveDeleted;
282
- const setOriginalExtPaths = async (
283
- settings = {},
284
- originalExtensionsFolder = ""
285
- ) => {
205
+ const setOriginalExtPaths = async (settings = {}, originalExtensionsFolder = '') => {
286
206
  if (!originalExtensionsFolder) {
287
207
  return null;
288
208
  }
289
209
  const extensionsSettings = settings.extensions?.settings || {};
290
210
  const extensionsEntries = Object.entries(extensionsSettings);
291
- const originalExtensionsList = await readdir(originalExtensionsFolder).catch(
292
- () => []
293
- );
211
+ const originalExtensionsList = await readdir(originalExtensionsFolder).catch(() => []);
294
212
  if (!originalExtensionsList.length) {
295
213
  return null;
296
214
  }
297
- const promises = originalExtensionsList.map(async (originalId) => {
215
+ const promises = originalExtensionsList.map(async originalId => {
298
216
  const extFolderPath = (0, _path.join)(originalExtensionsFolder, originalId);
299
217
  const extFolderContent = await readdir(extFolderPath);
300
218
  if (!extFolderPath.length) {
301
219
  return {};
302
220
  }
303
- if (extFolderContent.includes("manifest.json")) {
221
+ if (extFolderContent.includes('manifest.json')) {
304
222
  return {
305
223
  originalId,
306
- path: (0, _path.join)(originalExtensionsFolder, originalId),
224
+ path: (0, _path.join)(originalExtensionsFolder, originalId)
307
225
  };
308
226
  }
309
227
  const [version] = extFolderContent;
310
228
  return {
311
229
  originalId,
312
- path: (0, _path.join)(originalExtensionsFolder, originalId, version),
230
+ path: (0, _path.join)(originalExtensionsFolder, originalId, version)
313
231
  };
314
232
  });
315
233
  const originalExtPaths = await Promise.all(promises);
316
- extensionsEntries.forEach((extensionObj) => {
234
+ extensionsEntries.forEach(extensionObj => {
317
235
  const [extensionsId] = extensionObj;
318
236
  const extPath = extensionsSettings[extensionsId].path;
319
237
  if (!/chrome-extensions/.test(extPath)) {
320
238
  return;
321
239
  }
322
- const originalExtPath = originalExtPaths.find(
323
- (el) => el.originalId === extensionsId
324
- );
240
+ const originalExtPath = originalExtPaths.find(el => el.originalId === extensionsId);
325
241
  if (!originalExtPath) {
326
242
  return;
327
243
  }
328
- extensionsSettings[extensionsId].path = originalExtPath.path || "";
244
+ extensionsSettings[extensionsId].path = originalExtPath.path || '';
329
245
  });
330
246
  return extensionsSettings;
331
247
  };
@@ -334,14 +250,14 @@ const recalculateId = async ({
334
250
  localExtObj,
335
251
  extensionId,
336
252
  extensionsSettings,
337
- currentExtSettings,
253
+ currentExtSettings
338
254
  }) => {
339
255
  if (currentExtSettings.manifest?.key) {
340
256
  return extensionId;
341
257
  }
342
- const manifestFilePath = (0, _path.join)(localExtObj.path, "manifest.json");
258
+ const manifestFilePath = (0, _path.join)(localExtObj.path, 'manifest.json');
343
259
  const manifestString = await readFile(manifestFilePath, {
344
- encoding: "utf8",
260
+ encoding: 'utf8'
345
261
  }).catch(() => ({}));
346
262
  if (!manifestString) {
347
263
  return extensionId;
@@ -355,19 +271,13 @@ const recalculateId = async ({
355
271
  if (manifestObject.key) {
356
272
  return extensionId;
357
273
  }
358
- let encoding = "utf8";
359
- if (osPlatform === "win32") {
360
- encoding = "utf16le";
274
+ let encoding = 'utf8';
275
+ if (osPlatform === 'win32') {
276
+ encoding = 'utf16le';
361
277
  }
362
278
  const extPathToEncode = Buffer.from(localExtObj.path, encoding);
363
- const hexEncodedPath = (0, _crypto.createHash)("sha256")
364
- .update(extPathToEncode)
365
- .digest("hex");
366
- const newId = hexEncodedPath
367
- .split("")
368
- .slice(0, 32)
369
- .map((symbol) => extIdEncoding[symbol])
370
- .join("");
279
+ const hexEncodedPath = (0, _crypto.createHash)('sha256').update(extPathToEncode).digest('hex');
280
+ const newId = hexEncodedPath.split('').slice(0, 32).map(symbol => extIdEncoding[symbol]).join('');
371
281
  if (extensionId !== newId) {
372
282
  delete extensionsSettings[extensionId];
373
283
  extensionsSettings[newId] = currentExtSettings;
@@ -377,20 +287,20 @@ const recalculateId = async ({
377
287
  };
378
288
  exports.recalculateId = recalculateId;
379
289
  const extIdEncoding = {
380
- 0: "a",
381
- 1: "b",
382
- 2: "c",
383
- 3: "d",
384
- 4: "e",
385
- 5: "f",
386
- 6: "g",
387
- 7: "h",
388
- 8: "i",
389
- 9: "j",
390
- a: "k",
391
- b: "l",
392
- c: "m",
393
- d: "n",
394
- e: "o",
395
- f: "p",
396
- };
290
+ 0: 'a',
291
+ 1: 'b',
292
+ 2: 'c',
293
+ 3: 'd',
294
+ 4: 'e',
295
+ 5: 'f',
296
+ 6: 'g',
297
+ 7: 'h',
298
+ 8: 'i',
299
+ 9: 'j',
300
+ a: 'k',
301
+ b: 'l',
302
+ c: 'm',
303
+ d: 'n',
304
+ e: 'o',
305
+ f: 'p'
306
+ };