@open-wa/wa-automate 4.30.6 → 4.30.7

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.
@@ -7,8 +7,8 @@ export declare const deleteSessionData: (config: ConfigObject) => boolean;
7
7
  export declare const getSessionDataFilePath: (sessionId: string, config: ConfigObject) => string | boolean;
8
8
  export declare const addScript: (page: Page, js: string) => Promise<unknown>;
9
9
  export declare function injectPreApiScripts(page: Page, spinner?: Spin): Promise<Page>;
10
- export declare function injectWapi(page: Page, spinner?: Spin): Promise<Page>;
11
- export declare function injectApi(page: Page, spinner?: Spin): Promise<Page>;
10
+ export declare function injectWapi(page: Page, spinner?: Spin, force?: boolean): Promise<Page>;
11
+ export declare function injectApi(page: Page, spinner?: Spin, force?: boolean): Promise<Page>;
12
12
  /**
13
13
  * @internal
14
14
  */
@@ -45,7 +45,7 @@ const promise_1 = __importDefault(require("terminate/promise"));
45
45
  const logging_1 = require("../logging/logging");
46
46
  const tools_1 = require("../utils/tools");
47
47
  const auth_1 = require("./auth");
48
- let browser;
48
+ let browser, wapiInjected = false, wapiAttempts = 1;
49
49
  exports.BROWSER_START_TS = 0;
50
50
  function initPage(sessionId, config, customUserAgent, spinner, _page, skipAuth) {
51
51
  var _a, _b, _c, _d, _e;
@@ -280,30 +280,29 @@ function injectPreApiScripts(page, spinner) {
280
280
  });
281
281
  }
282
282
  exports.injectPreApiScripts = injectPreApiScripts;
283
- function injectWapi(page, spinner) {
283
+ function injectWapi(page, spinner, force = false) {
284
284
  return __awaiter(this, void 0, void 0, function* () {
285
+ if (wapiInjected && !force)
286
+ return page;
287
+ const check = `window.WAPI && window.Store ? true : false`;
285
288
  const wapi = yield (0, tools_1.timePromise)(() => (0, exports.addScript)(page, 'wapi.js'));
286
289
  spinner === null || spinner === void 0 ? void 0 : spinner.info(`WAPI inject: ${wapi}ms`);
287
- spinner === null || spinner === void 0 ? void 0 : spinner.info("Checking WAPI Injection");
288
- const check = (c) => `window.${c} ? true : false`;
289
- yield page.waitForFunction(check('WAPI'), { timeout: 0, polling: 500 });
290
- return;
290
+ spinner === null || spinner === void 0 ? void 0 : spinner.info("Checking session integrity");
291
+ wapiAttempts++;
292
+ wapiInjected = !!(yield page.waitForFunction(check, { timeout: 3000, polling: 200 }).catch(e => false));
293
+ if (!wapiInjected) {
294
+ spinner === null || spinner === void 0 ? void 0 : spinner.info(`Session integrity check failed, trying again... ${wapiAttempts}`);
295
+ return yield injectWapi(page, spinner);
296
+ }
297
+ spinner === null || spinner === void 0 ? void 0 : spinner.info("Session integrity check passed");
298
+ return page;
291
299
  });
292
300
  }
293
301
  exports.injectWapi = injectWapi;
294
- function injectApi(page, spinner) {
302
+ function injectApi(page, spinner, force = false) {
295
303
  return __awaiter(this, void 0, void 0, function* () {
296
304
  yield injectPreApiScripts(page, spinner);
297
- yield injectWapi(page, spinner);
298
- // const wapi = await timePromise(()=>addScript(page,'wapi.js'))
299
- // spinner?.info(`WAPI inject: ${wapi}ms`)
300
- // await addScript(page,'wapi.js')
301
- // await addScript(page,'wapi.js')
302
- // await addScript(page,'wapi.js')
303
- // await addScript(page,'wapi.js')
304
- // await addScript(page,'wapi.js')
305
- // await addScript(page,'wapi.js')
306
- // await addScript(page,'wapi.js')
305
+ yield injectWapi(page, spinner, force);
307
306
  const launch = yield (0, tools_1.timePromise)(() => (0, exports.addScript)(page, 'launch.js'));
308
307
  spinner === null || spinner === void 0 ? void 0 : spinner.info(`Launch inject: ${launch}ms`);
309
308
  return page;
@@ -303,7 +303,7 @@ function create(config = {}) {
303
303
  }
304
304
  const pre = canInjectEarly ? 'Rei' : 'I';
305
305
  spinner.start(`${pre}njecting api`);
306
- waPage = yield (0, browser_1.injectApi)(waPage, spinner);
306
+ waPage = yield (0, browser_1.injectApi)(waPage, spinner, true);
307
307
  spinner.succeed(`WAPI ${pre}njected`);
308
308
  if (canInjectEarly) {
309
309
  //check if page is valid after 5 seconds
@@ -312,7 +312,7 @@ function create(config = {}) {
312
312
  yield (0, exports.timeout)(5000);
313
313
  }
314
314
  //@ts-ignore
315
- const VALID_SESSION = yield waPage.evaluate(() => window.Store && window.Store.Msg ? true : false);
315
+ const VALID_SESSION = yield waPage.waitForFunction(`window.Store && window.Store.Msg ? true : false`, { timeout: 9000, polling: 200 }).catch(e => false);
316
316
  if (VALID_SESSION) {
317
317
  /**
318
318
  * Session is valid, attempt to preload patches
@@ -426,6 +426,11 @@ function create(config = {}) {
426
426
  }
427
427
  catch (error) {
428
428
  spinner.emit(error.message);
429
+ logging_1.log.error(error.message);
430
+ if (error.stack) {
431
+ logging_1.log.error(error.stack);
432
+ console.error(error.stack);
433
+ }
429
434
  yield (0, browser_1.kill)(waPage);
430
435
  if (error.name === "ProtocolError" && ((_c = error.message) === null || _c === void 0 ? void 0 : _c.includes("Target closed"))) {
431
436
  spinner.fail(error.message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-wa/wa-automate",
3
- "version": "4.30.6",
3
+ "version": "4.30.7",
4
4
  "licenseCheckUrl": "https://openwa.dev/license-check",
5
5
  "brokenMethodReportUrl": "https://openwa.dev/report-bm",
6
6
  "patches": "https://cdn.openwa.dev/patches.json",