@open-wa/wa-automate 4.30.5 → 4.30.9

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.
@@ -434,6 +434,7 @@ class Client {
434
434
  pup(pageFunction, ...args) {
435
435
  var _a, _b, _c, _d, _e;
436
436
  return __awaiter(this, void 0, void 0, function* () {
437
+ const invocation_id = (0, uuid_1.v4)().slice(-5);
437
438
  const { safeMode, callTimeout, idCorrection, logging } = this._createConfig;
438
439
  let _t;
439
440
  if (safeMode) {
@@ -472,13 +473,13 @@ class Client {
472
473
  if (logging) {
473
474
  const wapis = (_e = (((_d = pageFunction === null || pageFunction === void 0 ? void 0 : pageFunction.toString()) === null || _d === void 0 ? void 0 : _d.match(/WAPI\.(\w*)\(/g)) || [])) === null || _e === void 0 ? void 0 : _e.map(s => s.replace(/WAPI|\.|\(/g, ''));
474
475
  _t = Date.now();
475
- logging_1.log.info(`Request ${_t}`, Object.assign({ _method: (wapis === null || wapis === void 0 ? void 0 : wapis.length) === 1 ? wapis[0] : wapis }, args[0]));
476
+ logging_1.log.info(`IN ${invocation_id}`, Object.assign({ _method: (wapis === null || wapis === void 0 ? void 0 : wapis.length) === 1 ? wapis[0] : wapis }, args[0]));
476
477
  }
477
478
  if (callTimeout)
478
479
  return yield Promise.race([this._page.evaluate(pageFunction, ...args), new Promise((resolve, reject) => { var _a; return setTimeout(reject, (_a = this._createConfig) === null || _a === void 0 ? void 0 : _a.callTimeout, new errors_1.PageEvaluationTimeout()); })]);
479
480
  const res = yield this._page.evaluate(pageFunction, ...args);
480
481
  if (_t && logging) {
481
- logging_1.log.info(`Response ${_t}: ${Date.now() - _t}ms`, { res });
482
+ logging_1.log.info(`OUT ${invocation_id}: ${Date.now() - _t}ms`, { res });
482
483
  }
483
484
  if (this._createConfig.onError && typeof res == "string" && (res.startsWith("Error") || res.startsWith("ERROR"))) {
484
485
  const e = this._createConfig.onError;
@@ -3393,7 +3394,7 @@ class Client {
3393
3394
  const t = ((0, tools_1.now)() - whStart).toFixed(0);
3394
3395
  logging_1.log.info("Client Webhook", event, status, t);
3395
3396
  })
3396
- .catch(err => console.error(`WEBHOOK ERROR: `, url, err.message));
3397
+ .catch(err => logging_1.log.error(`WEBHOOK ERROR: `, url, err.message));
3397
3398
  }));
3398
3399
  }));
3399
3400
  }), 10000);
package/dist/cli/index.js CHANGED
@@ -34,7 +34,7 @@ const ready = (config) => __awaiter(void 0, void 0, void 0, function* () {
34
34
  sessionId: config.sessionId,
35
35
  namespace: "READY"
36
36
  }
37
- }).catch(err => console.error(`WEBHOOK ERROR: ${config.readyWebhook} ${err.message}`));
37
+ }).catch(err => index_1.log.error(`WEBHOOK ERROR: ${config.readyWebhook} ${err.message}`));
38
38
  });
39
39
  function start() {
40
40
  return __awaiter(this, void 0, void 0, function* () {
@@ -93,7 +93,7 @@ function start() {
93
93
  }).then(({ status }) => {
94
94
  const t = ((0, index_1.now)() - whStart).toFixed(0);
95
95
  index_1.log.info("EV Webhook", namespace, status, t);
96
- }).catch(err => console.error(`WEBHOOK ERROR: ${cliConfig.ev} ${err.message}`));
96
+ }).catch(err => index_1.log.error(`WEBHOOK ERROR: ${cliConfig.ev} ${err.message}`));
97
97
  }));
98
98
  }
99
99
  //These things can be done before the client is created
@@ -232,7 +232,7 @@ const setupTwilioCompatibleWebhook = (cliConfig, client) => {
232
232
  return yield client.sendText(message.from, msg['#']);
233
233
  }
234
234
  catch (error) {
235
- console.error("TWILIO-COMPAT WEBHOOK ERROR", url, error.message);
235
+ __1.log.error("TWILIO-COMPAT WEBHOOK ERROR", url, error.message);
236
236
  }
237
237
  }));
238
238
  };
@@ -365,8 +365,16 @@ const setupSocketServer = (cliConfig, client) => __awaiter(void 0, void 0, void
365
365
  args = (0, parse_function_1.default)().parse(client[m]).args.map(argName => args[argName]);
366
366
  else if (!args)
367
367
  args = [];
368
- const data = yield client[m](...args);
369
- callbacks[0](data);
368
+ try {
369
+ const data = yield client[m](...args);
370
+ callbacks[0](data);
371
+ }
372
+ catch (error) {
373
+ callbacks[0]({ error: {
374
+ message: error.message,
375
+ stack: error.stack || ""
376
+ } });
377
+ }
370
378
  }
371
379
  }
372
380
  return;
@@ -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.5",
3
+ "version": "4.30.9",
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",
@@ -84,7 +84,6 @@
84
84
  "auto-changelog": "^2.0.0",
85
85
  "ava": "^3.13.0",
86
86
  "changelog-parser": "^2.8.0",
87
- "typeconv": "^1.7.0",
88
87
  "command-line-args": "^5.1.1",
89
88
  "eslint": "^8.1.0",
90
89
  "husky": "^7.0.0",
@@ -96,9 +95,10 @@
96
95
  "prettier": "^2.0.1",
97
96
  "release-it": "^14.0.3",
98
97
  "shelljs": "^0.8.3",
98
+ "tiny-glob": "^0.2.9",
99
99
  "ts-node": "^10.0.0",
100
100
  "tsc-watch": "^4.0.0",
101
- "tiny-glob": "^0.2.9",
101
+ "typeconv": "^1.7.0",
102
102
  "typedoc": "^0.21.6",
103
103
  "typedoc-plugin-pages": "^1.0.1",
104
104
  "typescript": "^4.2.4"