@open-wa/wa-automate 4.28.9 → 4.28.13

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,6 +40,7 @@ export declare class Client {
40
40
  private _prio;
41
41
  private _pageListeners;
42
42
  private _registeredPageListeners;
43
+ private _onLogoutCallbacks;
43
44
  private _queues;
44
45
  /**
45
46
  * 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.
@@ -111,13 +112,18 @@ export declare class Client {
111
112
  *
112
113
  * @event
113
114
  * @param fn callback
115
+ * @param priority A priority of -1 will mean the callback will be triggered after all the non -1 callbacks
114
116
  * @fires `true`
115
117
  */
116
118
  onLogout(fn: (loggedOut?: boolean) => any, priority?: number): Promise<boolean>;
117
119
  /**
118
120
  * Wait for the webhook queue to become idle. This is useful for ensuring webhooks are cleared before ending a process.
119
121
  */
120
- waitWhQIdle(): Promise<any>;
122
+ waitWhQIdle(): Promise<true | void>;
123
+ /**
124
+ * Wait for all queues to be empty
125
+ */
126
+ waitAllQEmpty(): Promise<true | void[]>;
121
127
  /**
122
128
  * If you have set `onAnyMessage` or `onMessage` with the second parameter (PQueue options) then you may want to inspect their respective PQueue's.
123
129
  */
@@ -794,6 +800,10 @@ export declare class Client {
794
800
  */
795
801
  getAllContacts(): Promise<Contact[]>;
796
802
  getWAVersion(): Promise<string>;
803
+ /**
804
+ * Generate a pre-filled github issue link to easily report a bug
805
+ */
806
+ getIssueLink(): Promise<string>;
797
807
  /**
798
808
  * Retrieves if the phone is online. Please note that this may not be real time.
799
809
  * @returns Boolean
@@ -90,6 +90,7 @@ class Client {
90
90
  this._prio = Number.MAX_SAFE_INTEGER;
91
91
  this._pageListeners = [];
92
92
  this._registeredPageListeners = [];
93
+ this._onLogoutCallbacks = [];
93
94
  this._queues = {};
94
95
  /**
95
96
  * 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.
@@ -260,16 +261,17 @@ class Client {
260
261
  }));
261
262
  }
262
263
  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)
264
+ this.onLogout(() => __awaiter(this, void 0, void 0, function* () {
265
+ var _k, _l;
266
+ yield this.waitAllQEmpty();
267
+ if ((_k = this._createConfig) === null || _k === void 0 ? void 0 : _k.deleteSessionDataOnLogout)
266
268
  (0, browser_1.deleteSessionData)(this._createConfig);
267
- if ((_b = this._createConfig) === null || _b === void 0 ? void 0 : _b.killClientOnLogout) {
269
+ if ((_l = this._createConfig) === null || _l === void 0 ? void 0 : _l.killClientOnLogout) {
268
270
  console.log("Session logged out. Killing client");
269
271
  logging_1.log.warn("Session logged out. Killing client");
270
272
  this.kill("LOGGED_OUT");
271
273
  }
272
- });
274
+ }), -1);
273
275
  }
274
276
  });
275
277
  }
@@ -567,27 +569,47 @@ class Client {
567
569
  });
568
570
  if (this._registeredPageListeners.includes(event))
569
571
  return true;
572
+ this._registeredPageListeners.push(event);
573
+ logging_1.log.info(`setting page listener: ${event}`, this._registeredPageListeners);
570
574
  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)));
575
+ 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)));
576
+ 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)));
577
+ return;
572
578
  }));
573
- this._registeredPageListeners.push(event);
574
579
  }
575
580
  /**
576
581
  * Listens to a log out event
577
582
  *
578
583
  * @event
579
584
  * @param fn callback
585
+ * @param priority A priority of -1 will mean the callback will be triggered after all the non -1 callbacks
580
586
  * @fires `true`
581
587
  */
582
588
  onLogout(fn, priority) {
583
589
  return __awaiter(this, void 0, void 0, function* () {
584
- this.registerPageEventListener('framenavigated', (frame) => __awaiter(this, void 0, void 0, function* () {
590
+ const event = 'framenavigated';
591
+ this._onLogoutCallbacks.push({
592
+ callback: fn,
593
+ priority
594
+ });
595
+ if (!this._queues[event])
596
+ this._queues[event] = new p_queue_1.default({
597
+ concurrency: 1,
598
+ intervalCap: 1,
599
+ carryoverConcurrencyCount: true
600
+ });
601
+ if (this._registeredPageListeners.includes(event))
602
+ return true;
603
+ this.registerPageEventListener(event, (frame) => __awaiter(this, void 0, void 0, function* () {
585
604
  if (frame.url().includes('post_logout=1')) {
586
605
  console.log("LOGGED OUT");
587
606
  logging_1.log.warn("LOGGED OUT");
588
- yield fn(true);
607
+ yield Promise.all(this._onLogoutCallbacks.filter(c => c.priority !== -1).map(({ callback }) => this._queues[event].add(() => callback(true))));
608
+ yield this._queues[event].onEmpty();
609
+ yield Promise.all(this._onLogoutCallbacks.filter(c => c.priority == -1).map(({ callback }) => this._queues[event].add(() => callback(true))));
610
+ yield this._queues[event].onEmpty();
589
611
  }
590
- }), priority);
612
+ }), priority || 1);
591
613
  return true;
592
614
  });
593
615
  }
@@ -602,6 +624,18 @@ class Client {
602
624
  return true;
603
625
  });
604
626
  }
627
+ /**
628
+ * Wait for all queues to be empty
629
+ */
630
+ waitAllQEmpty() {
631
+ return __awaiter(this, void 0, void 0, function* () {
632
+ return yield Promise.all([
633
+ this._webhookQueue,
634
+ ...Object.values(this._queues)
635
+ ].filter(q => q).map(q => q === null || q === void 0 ? void 0 : q.onEmpty()));
636
+ return true;
637
+ });
638
+ }
605
639
  /**
606
640
  * If you have set `onAnyMessage` or `onMessage` with the second parameter (PQueue options) then you may want to inspect their respective PQueue's.
607
641
  */
@@ -1816,6 +1850,14 @@ class Client {
1816
1850
  return yield this.pup(() => WAPI.getWAVersion());
1817
1851
  });
1818
1852
  }
1853
+ /**
1854
+ * Generate a pre-filled github issue link to easily report a bug
1855
+ */
1856
+ getIssueLink() {
1857
+ return __awaiter(this, void 0, void 0, function* () {
1858
+ return (0, tools_1.generateGHIssueLink)(this.getConfig(), this.getSessionInfo());
1859
+ });
1860
+ }
1819
1861
  /**
1820
1862
  * Retrieves if the phone is online. Please note that this may not be real time.
1821
1863
  * @returns Boolean
@@ -12,6 +12,9 @@ export interface SessionInfo {
12
12
  NUM_HASH?: string;
13
13
  OW_KEY?: string;
14
14
  INSTANCE_ID?: string;
15
+ LATEST_VERSION?: boolean;
16
+ CLI?: boolean;
17
+ ACC_TYPE?: 'PERSONAL' | 'BUSINESS';
15
18
  }
16
19
  export interface HealthCheck {
17
20
  /**
package/dist/cli/index.js CHANGED
@@ -39,6 +39,7 @@ const ready = (config) => __awaiter(void 0, void 0, void 0, function* () {
39
39
  function start() {
40
40
  return __awaiter(this, void 0, void 0, function* () {
41
41
  const { cliConfig, createConfig, PORT, spinner } = (0, setup_1.cli)();
42
+ process.env.OWA_CLI = "true";
42
43
  spinner.start("Launching EASY API");
43
44
  (0, server_1.setUpExpressApp)();
44
45
  if (cliConfig.cors)
@@ -108,11 +109,11 @@ function start() {
108
109
  client.onLogout(() => __awaiter(this, void 0, void 0, function* () {
109
110
  console.error('!!!! CLIENT LOGGED OUT !!!!');
110
111
  if (cliConfig && !cliConfig.noKillOnLogout) {
111
- yield client.waitWhQIdle();
112
+ yield client.waitAllQEmpty();
112
113
  console.error("Shutting down.");
113
114
  process.exit();
114
115
  }
115
- }));
116
+ }), -1);
116
117
  if (cliConfig === null || cliConfig === void 0 ? void 0 : cliConfig.botPressUrl) {
117
118
  spinner.info('Setting Up Botpress handler');
118
119
  (0, server_1.setupBotPressHandler)(cliConfig, client);
@@ -27,17 +27,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
27
27
  step((generator = generator.apply(thisArg, _arguments || [])).next());
28
28
  });
29
29
  };
30
- var __rest = (this && this.__rest) || function (s, e) {
31
- var t = {};
32
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
33
- t[p] = s[p];
34
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
35
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
36
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
37
- t[p[i]] = s[p[i]];
38
- }
39
- return t;
40
- };
41
30
  var __importDefault = (this && this.__importDefault) || function (mod) {
42
31
  return (mod && mod.__esModule) ? mod : { "default": mod };
43
32
  };
@@ -218,11 +207,10 @@ function create(config = {}) {
218
207
  console.table(debugInfo);
219
208
  logging_1.log.info('Debug info:', debugInfo);
220
209
  }
210
+ debugInfo.LATEST_VERSION = !((notifier === null || notifier === void 0 ? void 0 : notifier.update) && ((notifier === null || notifier === void 0 ? void 0 : notifier.update.latest) !== exports.pkg.version));
211
+ debugInfo.CLI = process.env.OWA_CLI && true || false;
221
212
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
222
- spinner.succeed('Use this easy pre-filled link to report an issue: ' + `https://github.com/open-wa/wa-automate-nodejs/issues/new?template=bug_report.yaml&debug_info=${encodeURI(JSON.stringify(((_a) => {
223
- var { OS, PAGE_UA } = _a, o = __rest(_a, ["OS", "PAGE_UA"]);
224
- return o;
225
- })(debugInfo), null, 2))}&environment=${`-%20OS:%20${encodeURI(debugInfo.OS)}%0A-%20Node:%20${encodeURI(process.versions.node)}%0A-%20npm:%20%0A`}`);
213
+ spinner.succeed('Use this easy pre-filled link to report an issue: ' + (0, tools_1.generateGHIssueLink)(config, debugInfo));
226
214
  if (canInjectEarly) {
227
215
  if (attemptingReauth)
228
216
  yield waPage.evaluate(`window.Store = {"Msg": true}`);
@@ -1,5 +1,6 @@
1
- import { DataURL } from '../api/model';
1
+ import { ConfigObject, DataURL } from '../api/model';
2
2
  import { AxiosRequestConfig } from 'axios';
3
+ import { SessionInfo } from '../api/model/sessionInfo';
3
4
  export declare const timeout: (ms: any) => Promise<unknown>;
4
5
  /**
5
6
  * Use this to generate a more likely valid user agent. It makes sure it has the WA part and replaces any windows or linux os info with mac.
@@ -27,3 +28,4 @@ export declare const getDUrl: (url: string, optionsOverride?: AxiosRequestConfig
27
28
  export declare const base64MimeType: (dUrl: DataURL) => string;
28
29
  export declare const processSend: (message: string) => void;
29
30
  export declare const processSendData: (data?: any) => void;
31
+ export declare const generateGHIssueLink: (config: ConfigObject, sessionInfo: SessionInfo, extras?: any) => string;
@@ -23,8 +23,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
23
23
  return (mod && mod.__esModule) ? mod : { "default": mod };
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.processSendData = exports.processSend = exports.base64MimeType = exports.getDUrl = exports.isDataURL = exports.isBase64 = exports.camelize = exports.without = exports.getConfigFromProcessEnv = exports.smartUserAgent = exports.timeout = void 0;
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 child_process_1 = require("child_process");
28
29
  //@ts-ignore
29
30
  process.send = process.send || function () { };
30
31
  const timeout = ms => new Promise(resolve => setTimeout(resolve, ms, 'timeout'));
@@ -145,3 +146,24 @@ const processSendData = (data = {}) => {
145
146
  return;
146
147
  };
147
148
  exports.processSendData = processSendData;
149
+ const generateGHIssueLink = (config, sessionInfo, extras = {}) => {
150
+ const npm_ver = (0, child_process_1.execSync)('npm -v');
151
+ const labels = [];
152
+ if (sessionInfo.CLI)
153
+ labels.push('CLI');
154
+ if (!sessionInfo.LATEST_VERSION)
155
+ labels.push('NCV');
156
+ labels.push(config.multiDevice ? 'MD' : 'Legacy');
157
+ if (sessionInfo.ACC_TYPE === 'BUSINESS')
158
+ labels.push('BHA');
159
+ if (sessionInfo.ACC_TYPE === 'PERSONAL')
160
+ labels.push('PHA');
161
+ const qp = Object.assign({ "template": "bug_report.yaml",
162
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
163
+ "d_info": `${encodeURI(JSON.stringify(((_a) => {
164
+ var { OS, PAGE_UA } = _a, o = __rest(_a, ["OS", "PAGE_UA"]);
165
+ return o;
166
+ })(sessionInfo), null, 2))}`, "enviro": `${`-%20OS:%20${encodeURI(sessionInfo.OS)}%0A-%20Node:%20${encodeURI(process.versions.node)}%0A-%20npm:%20${(String(npm_ver)).replace(/\s/g, '')}`}`, "labels": labels.join(',') }, extras);
167
+ return `https://github.com/open-wa/wa-automate-nodejs/issues/new?${Object.keys(qp).map(k => `${k}=${qp[k]}`).join('&')}`;
168
+ };
169
+ exports.generateGHIssueLink = generateGHIssueLink;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-wa/wa-automate",
3
- "version": "4.28.9",
3
+ "version": "4.28.13",
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",