@open-wa/wa-automate 4.28.10 → 4.29.0

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.
@@ -40,7 +40,10 @@ export declare class Client {
40
40
  private _prio;
41
41
  private _pageListeners;
42
42
  private _registeredPageListeners;
43
+ private _onLogoutCallbacks;
43
44
  private _queues;
45
+ private _autoEmojiSet;
46
+ private _onLogoutSet;
44
47
  /**
45
48
  * This is used to track if a listener is already used via webhook. Before, webhooks used to be set once per listener. Now a listener can be set via multiple webhooks, or revoked from a specific webhook.
46
49
  * For this reason, listeners assigned to a webhook are only set once and map through all possible webhooks to and fire only if the specific listener is assigned.
@@ -111,13 +114,18 @@ export declare class Client {
111
114
  *
112
115
  * @event
113
116
  * @param fn callback
117
+ * @param priority A priority of -1 will mean the callback will be triggered after all the non -1 callbacks
114
118
  * @fires `true`
115
119
  */
116
120
  onLogout(fn: (loggedOut?: boolean) => any, priority?: number): Promise<boolean>;
117
121
  /**
118
122
  * Wait for the webhook queue to become idle. This is useful for ensuring webhooks are cleared before ending a process.
119
123
  */
120
- waitWhQIdle(): Promise<any>;
124
+ waitWhQIdle(): Promise<true | void>;
125
+ /**
126
+ * Wait for all queues to be empty
127
+ */
128
+ waitAllQEmpty(): Promise<true | void[]>;
121
129
  /**
122
130
  * If you have set `onAnyMessage` or `onMessage` with the second parameter (PQueue options) then you may want to inspect their respective PQueue's.
123
131
  */
@@ -56,7 +56,6 @@ const fs = __importStar(require("fs"));
56
56
  const datauri_1 = __importDefault(require("datauri"));
57
57
  const is_url_superb_1 = __importDefault(require("is-url-superb"));
58
58
  const fs_extra_1 = require("fs-extra");
59
- const tree_kill_1 = __importDefault(require("tree-kill"));
60
59
  const browser_1 = require("../controllers/browser");
61
60
  const auth_1 = require("../controllers/auth");
62
61
  const wa_decrypt_1 = require("@open-wa/wa-decrypt");
@@ -90,7 +89,10 @@ class Client {
90
89
  this._prio = Number.MAX_SAFE_INTEGER;
91
90
  this._pageListeners = [];
92
91
  this._registeredPageListeners = [];
92
+ this._onLogoutCallbacks = [];
93
93
  this._queues = {};
94
+ this._autoEmojiSet = false;
95
+ this._onLogoutSet = false;
94
96
  /**
95
97
  * This is used to track if a listener is already used via webhook. Before, webhooks used to be set once per listener. Now a listener can be set via multiple webhooks, or revoked from a specific webhook.
96
98
  * For this reason, listeners assigned to a webhook are only set once and map through all possible webhooks to and fire only if the specific listener is assigned.
@@ -248,7 +250,7 @@ class Client {
248
250
  logging_1.log.info('LOADED', {
249
251
  PHONE_VERSION: this._sessionInfo.PHONE_VERSION
250
252
  });
251
- if (((_d = this._createConfig) === null || _d === void 0 ? void 0 : _d.autoEmoji) === undefined || ((_e = this._createConfig) === null || _e === void 0 ? void 0 : _e.autoEmoji)) {
253
+ if ((((_d = this._createConfig) === null || _d === void 0 ? void 0 : _d.autoEmoji) === undefined || ((_e = this._createConfig) === null || _e === void 0 ? void 0 : _e.autoEmoji)) && !this._autoEmojiSet) {
252
254
  const ident = typeof ((_f = this._createConfig) === null || _f === void 0 ? void 0 : _f.autoEmoji) === "string" ? (_g = this._createConfig) === null || _g === void 0 ? void 0 : _g.autoEmoji : ":";
253
255
  this.onMessage((message) => __awaiter(this, void 0, void 0, function* () {
254
256
  if (message.body && message.body.startsWith(ident) && message.body.endsWith(ident)) {
@@ -258,18 +260,21 @@ class Client {
258
260
  return yield this.sendEmoji(message.from, emojiId, message.id);
259
261
  }
260
262
  }));
263
+ this._autoEmojiSet = true;
261
264
  }
262
- if (((_h = this._createConfig) === null || _h === void 0 ? void 0 : _h.deleteSessionDataOnLogout) || ((_j = this._createConfig) === null || _j === void 0 ? void 0 : _j.killClientOnLogout)) {
263
- this.onLogout(() => {
264
- var _a, _b;
265
- if ((_a = this._createConfig) === null || _a === void 0 ? void 0 : _a.deleteSessionDataOnLogout)
265
+ if ((((_h = this._createConfig) === null || _h === void 0 ? void 0 : _h.deleteSessionDataOnLogout) || ((_j = this._createConfig) === null || _j === void 0 ? void 0 : _j.killClientOnLogout)) && !this._onLogoutSet) {
266
+ this.onLogout(() => __awaiter(this, void 0, void 0, function* () {
267
+ var _k, _l;
268
+ yield this.waitAllQEmpty();
269
+ if ((_k = this._createConfig) === null || _k === void 0 ? void 0 : _k.deleteSessionDataOnLogout)
266
270
  (0, browser_1.deleteSessionData)(this._createConfig);
267
- if ((_b = this._createConfig) === null || _b === void 0 ? void 0 : _b.killClientOnLogout) {
271
+ if ((_l = this._createConfig) === null || _l === void 0 ? void 0 : _l.killClientOnLogout) {
268
272
  console.log("Session logged out. Killing client");
269
273
  logging_1.log.warn("Session logged out. Killing client");
270
274
  this.kill("LOGGED_OUT");
271
275
  }
272
- });
276
+ }), -1);
277
+ this._onLogoutSet = true;
273
278
  }
274
279
  });
275
280
  }
@@ -536,7 +541,7 @@ class Client {
536
541
  fn = _fn;
537
542
  }
538
543
  if (this._registeredEvListeners && this._registeredEvListeners[funcName]) {
539
- return events_1.ev.on(this.getEventSignature(funcName), ({ data }) => fn(data));
544
+ return events_1.ev.on(this.getEventSignature(funcName), ({ data }) => fn(data), { objectify: true });
540
545
  }
541
546
  /**
542
547
  * If evMode is on then make the callback come from ev.
@@ -567,27 +572,47 @@ class Client {
567
572
  });
568
573
  if (this._registeredPageListeners.includes(event))
569
574
  return true;
575
+ this._registeredPageListeners.push(event);
576
+ logging_1.log.info(`setting page listener: ${event}`, this._registeredPageListeners);
570
577
  this._page.on(event, (...args) => __awaiter(this, void 0, void 0, function* () {
571
- return yield Promise.all(this._pageListeners.filter(l => l.event === event).sort((a, b) => (b.priority || 0) - (a.priority || 0)).map(l => l.callback(...args)));
578
+ yield Promise.all(this._pageListeners.filter(l => l.event === event).filter(({ priority }) => priority !== -1).sort((a, b) => (b.priority || 0) - (a.priority || 0)).map(l => l.callback(...args)));
579
+ yield Promise.all(this._pageListeners.filter(l => l.event === event).filter(({ priority }) => priority == -1).sort((a, b) => (b.priority || 0) - (a.priority || 0)).map(l => l.callback(...args)));
580
+ return;
572
581
  }));
573
- this._registeredPageListeners.push(event);
574
582
  }
575
583
  /**
576
584
  * Listens to a log out event
577
585
  *
578
586
  * @event
579
587
  * @param fn callback
588
+ * @param priority A priority of -1 will mean the callback will be triggered after all the non -1 callbacks
580
589
  * @fires `true`
581
590
  */
582
591
  onLogout(fn, priority) {
583
592
  return __awaiter(this, void 0, void 0, function* () {
584
- this.registerPageEventListener('framenavigated', (frame) => __awaiter(this, void 0, void 0, function* () {
593
+ const event = 'framenavigated';
594
+ this._onLogoutCallbacks.push({
595
+ callback: fn,
596
+ priority
597
+ });
598
+ if (!this._queues[event])
599
+ this._queues[event] = new p_queue_1.default({
600
+ concurrency: 1,
601
+ intervalCap: 1,
602
+ carryoverConcurrencyCount: true
603
+ });
604
+ if (this._registeredPageListeners.includes(event))
605
+ return true;
606
+ this.registerPageEventListener(event, (frame) => __awaiter(this, void 0, void 0, function* () {
585
607
  if (frame.url().includes('post_logout=1')) {
586
608
  console.log("LOGGED OUT");
587
609
  logging_1.log.warn("LOGGED OUT");
588
- yield fn(true);
610
+ yield Promise.all(this._onLogoutCallbacks.filter(c => c.priority !== -1).map(({ callback }) => this._queues[event].add(() => callback(true))));
611
+ yield this._queues[event].onEmpty();
612
+ yield Promise.all(this._onLogoutCallbacks.filter(c => c.priority == -1).map(({ callback }) => this._queues[event].add(() => callback(true))));
613
+ yield this._queues[event].onEmpty();
589
614
  }
590
- }), priority);
615
+ }), priority || 1);
591
616
  return true;
592
617
  });
593
618
  }
@@ -602,6 +627,18 @@ class Client {
602
627
  return true;
603
628
  });
604
629
  }
630
+ /**
631
+ * Wait for all queues to be empty
632
+ */
633
+ waitAllQEmpty() {
634
+ return __awaiter(this, void 0, void 0, function* () {
635
+ return yield Promise.all([
636
+ this._webhookQueue,
637
+ ...Object.values(this._queues)
638
+ ].filter(q => q).map(q => q === null || q === void 0 ? void 0 : q.onEmpty()));
639
+ return true;
640
+ });
641
+ }
605
642
  /**
606
643
  * If you have set `onAnyMessage` or `onMessage` with the second parameter (PQueue options) then you may want to inspect their respective PQueue's.
607
644
  */
@@ -1054,25 +1091,17 @@ class Client {
1054
1091
  * @returns true
1055
1092
  */
1056
1093
  kill(reason = "MANUALLY_KILLED") {
1057
- var _a, _b, _c, _d, _e, _f, _g;
1094
+ var _a, _b;
1058
1095
  return __awaiter(this, void 0, void 0, function* () {
1059
1096
  if (this._currentlyBeingKilled)
1060
1097
  return;
1061
1098
  this._currentlyBeingKilled = true;
1062
1099
  console.log('Killing client. Shutting Down');
1063
1100
  logging_1.log.info('Killing client. Shutting Down');
1064
- (0, tools_1.processSendData)({
1065
- reason
1066
- });
1067
1101
  const browser = yield ((_a = this === null || this === void 0 ? void 0 : this._page) === null || _a === void 0 ? void 0 : _a.browser());
1068
1102
  const pid = (browser === null || browser === void 0 ? void 0 : browser.process()) ? (_b = browser === null || browser === void 0 ? void 0 : browser.process()) === null || _b === void 0 ? void 0 : _b.pid : null;
1069
1103
  try {
1070
- if (this._page && !((_c = this._page) === null || _c === void 0 ? void 0 : _c.isClosed()))
1071
- yield ((_d = this._page) === null || _d === void 0 ? void 0 : _d.close());
1072
- if (this._page && ((_e = this._page) === null || _e === void 0 ? void 0 : _e.browser))
1073
- yield ((_g = (_f = this._page) === null || _f === void 0 ? void 0 : _f.browser()) === null || _g === void 0 ? void 0 : _g.close());
1074
- if (pid)
1075
- (0, tree_kill_1.default)(pid, 'SIGKILL');
1104
+ yield (0, browser_1.kill)(this._page, browser, false, pid, reason);
1076
1105
  }
1077
1106
  catch (error) {
1078
1107
  //ignore error
package/dist/cli/index.js CHANGED
@@ -109,11 +109,11 @@ function start() {
109
109
  client.onLogout(() => __awaiter(this, void 0, void 0, function* () {
110
110
  console.error('!!!! CLIENT LOGGED OUT !!!!');
111
111
  if (cliConfig && !cliConfig.noKillOnLogout) {
112
- yield client.waitWhQIdle();
112
+ yield client.waitAllQEmpty();
113
113
  console.error("Shutting down.");
114
114
  process.exit();
115
115
  }
116
- }));
116
+ }), -1);
117
117
  if (cliConfig === null || cliConfig === void 0 ? void 0 : cliConfig.botPressUrl) {
118
118
  spinner.info('Setting Up Botpress handler');
119
119
  (0, server_1.setupBotPressHandler)(cliConfig, client);
@@ -41,7 +41,7 @@ const events_1 = require("./events");
41
41
  const pico_s3_1 = require("pico-s3");
42
42
  // eslint-disable-next-line @typescript-eslint/no-var-requires
43
43
  const puppeteer = require('puppeteer-extra');
44
- const tree_kill_1 = __importDefault(require("tree-kill"));
44
+ 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
  let browser;
@@ -406,7 +406,7 @@ const kill = (p, b, exit, pid, reason = "LAUNCH_KILL") => __awaiter(void 0, void
406
406
  yield killBrowser(b);
407
407
  }
408
408
  if (pid)
409
- (0, tree_kill_1.default)(pid, 'SIGKILL');
409
+ yield (0, promise_1.default)(pid, 'SIGKILL').catch(e => console.error('Error while terminating browser PID. You can just ignore this, as the process has most likely been terminated successfully already:', e.message));
410
410
  if (exit)
411
411
  process.exit();
412
412
  return;
@@ -25,7 +25,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.generateGHIssueLink = exports.processSendData = exports.processSend = exports.base64MimeType = exports.getDUrl = exports.isDataURL = exports.isBase64 = exports.camelize = exports.without = exports.getConfigFromProcessEnv = exports.smartUserAgent = exports.timeout = void 0;
27
27
  const axios_1 = __importDefault(require("axios"));
28
- const node_child_process_1 = require("node:child_process");
28
+ const child_process_1 = require("child_process");
29
29
  //@ts-ignore
30
30
  process.send = process.send || function () { };
31
31
  const timeout = ms => new Promise(resolve => setTimeout(resolve, ms, 'timeout'));
@@ -147,7 +147,7 @@ const processSendData = (data = {}) => {
147
147
  };
148
148
  exports.processSendData = processSendData;
149
149
  const generateGHIssueLink = (config, sessionInfo, extras = {}) => {
150
- const npm_ver = (0, node_child_process_1.execSync)('npm -v');
150
+ const npm_ver = (0, child_process_1.execSync)('npm -v');
151
151
  const labels = [];
152
152
  if (sessionInfo.CLI)
153
153
  labels.push('CLI');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-wa/wa-automate",
3
- "version": "4.28.10",
3
+ "version": "4.29.0",
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",
@@ -60,6 +60,9 @@
60
60
  "node": ">=12.18.3",
61
61
  "npm": ">=7.9.0"
62
62
  },
63
+ "resolutions": {
64
+ "colors": "1.4.0"
65
+ },
63
66
  "devDependencies": {
64
67
  "@types/changelog-parser": "^2.7.1",
65
68
  "@types/command-line-args": "^5.0.0",
@@ -156,9 +159,9 @@
156
159
  "swagger-ui-express": "^4.1.4",
157
160
  "tcp-port-used": "^1.0.1",
158
161
  "terminal-link": "^2.1.1",
162
+ "terminate": "^2.5.0",
159
163
  "tiny-glob": "^0.2.9",
160
164
  "traverse": "^0.6.6",
161
- "tree-kill": "^1.2.2",
162
165
  "ts-json-schema-generator": "^0.95.0",
163
166
  "ts-morph": "^12.0.0",
164
167
  "type-fest": "^1.1.1",