@open-wa/wa-automate 4.50.0 → 4.50.2

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.
@@ -713,7 +713,7 @@ export declare class Client {
713
713
  * @param hideTags boolean default: false [INSIDERS] set this to try silent tag someone in the caption
714
714
  * @returns `Promise <boolean | MessageId>` This will either return true or the id of the message. It will return true after 10 seconds even if waitForId is true
715
715
  */
716
- sendFile(to: ChatId, file: AdvancedFile, filename: string, caption: Content, quotedMsgId?: MessageId, waitForId?: boolean, ptt?: boolean, withoutPreview?: boolean, hideTags?: boolean, viewOnce?: boolean): Promise<MessageId | boolean>;
716
+ sendFile(to: ChatId, file: AdvancedFile, filename: string, caption: Content, quotedMsgId?: MessageId, waitForId?: boolean, ptt?: boolean, withoutPreview?: boolean, hideTags?: boolean, viewOnce?: boolean, requestConfig?: any): Promise<MessageId | boolean>;
717
717
  /**
718
718
  * {@license:insiders@}
719
719
  *
@@ -307,7 +307,7 @@ class Client {
307
307
  yield ((_q = (_o = this._queues) === null || _o === void 0 ? void 0 : _o.onLogout) === null || _q === void 0 ? void 0 : _q.onIdle());
308
308
  yield (0, browser_1.invalidateSesssionData)(this._createConfig);
309
309
  if ((_r = this._createConfig) === null || _r === void 0 ? void 0 : _r.deleteSessionDataOnLogout)
310
- (0, browser_1.deleteSessionData)(this._createConfig);
310
+ yield (0, browser_1.deleteSessionData)(this._createConfig);
311
311
  if ((_s = this._createConfig) === null || _s === void 0 ? void 0 : _s.killClientOnLogout) {
312
312
  console.log("Session logged out. Killing client");
313
313
  logging_1.log.warn("Session logged out. Killing client");
@@ -1781,9 +1781,9 @@ class Client {
1781
1781
  * @param hideTags boolean default: false [INSIDERS] set this to try silent tag someone in the caption
1782
1782
  * @returns `Promise <boolean | MessageId>` This will either return true or the id of the message. It will return true after 10 seconds even if waitForId is true
1783
1783
  */
1784
- sendFile(to, file, filename, caption, quotedMsgId, waitForId, ptt, withoutPreview, hideTags, viewOnce) {
1784
+ sendFile(to, file, filename, caption, quotedMsgId, waitForId, ptt, withoutPreview, hideTags, viewOnce, requestConfig) {
1785
1785
  return __awaiter(this, void 0, void 0, function* () {
1786
- return this.sendImage(to, file, filename, caption, quotedMsgId, waitForId, ptt, withoutPreview, hideTags, viewOnce);
1786
+ return this.sendImage(to, file, filename, caption, quotedMsgId, waitForId, ptt, withoutPreview, hideTags, viewOnce, requestConfig);
1787
1787
  });
1788
1788
  }
1789
1789
  /**
@@ -1909,8 +1909,7 @@ class Client {
1909
1909
  */
1910
1910
  sendFileFromUrl(to, url, filename, caption, quotedMsgId, requestConfig = {}, waitForId, ptt, withoutPreview, hideTags, viewOnce) {
1911
1911
  return __awaiter(this, void 0, void 0, function* () {
1912
- const base64 = yield (0, tools_1.getDUrl)(url, requestConfig);
1913
- return yield this.sendFile(to, base64, filename, caption, quotedMsgId, waitForId, ptt, withoutPreview, hideTags, viewOnce);
1912
+ return yield this.sendFile(to, url, filename, caption, quotedMsgId, waitForId, ptt, withoutPreview, hideTags, viewOnce, requestConfig);
1914
1913
  });
1915
1914
  }
1916
1915
  /**
@@ -4,9 +4,9 @@ import { ConfigObject } from '../api/model';
4
4
  import { QRManager } from './auth';
5
5
  export declare let BROWSER_START_TS: number;
6
6
  export declare function initPage(sessionId?: string, config?: ConfigObject, qrManager?: QRManager, customUserAgent?: string, spinner?: Spin, _page?: Page, skipAuth?: boolean): Promise<Page>;
7
- export declare const deleteSessionData: (config: ConfigObject) => boolean;
8
- export declare const invalidateSesssionData: (config: ConfigObject) => boolean;
9
- export declare const getSessionDataFilePath: (sessionId: string, config: ConfigObject) => string | false;
7
+ export declare const deleteSessionData: (config: ConfigObject) => Promise<boolean>;
8
+ export declare const invalidateSesssionData: (config: ConfigObject) => Promise<boolean>;
9
+ export declare const getSessionDataFilePath: (sessionId: string, config: ConfigObject) => Promise<string | false>;
10
10
  export declare const addScript: (page: Page, js: string) => Promise<unknown>;
11
11
  export declare function injectPreApiScripts(page: Page, spinner?: Spin): Promise<Page>;
12
12
  export declare function injectWapi(page: Page, spinner?: Spin, force?: boolean): Promise<Page>;
@@ -33,7 +33,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
34
  exports.kill = exports.injectApi = exports.injectWapi = exports.injectPreApiScripts = exports.addScript = exports.getSessionDataFilePath = exports.invalidateSesssionData = exports.deleteSessionData = exports.initPage = exports.BROWSER_START_TS = void 0;
35
35
  const path = __importStar(require("path"));
36
- const fs = __importStar(require("fs"));
36
+ const fs = __importStar(require("fs/promises"));
37
37
  const death_1 = __importDefault(require("death"));
38
38
  // import puppeteer from 'puppeteer-extra';
39
39
  const puppeteer_config_1 = require("../config/puppeteer.config");
@@ -181,7 +181,7 @@ function initPage(sessionId, config, qrManager, customUserAgent, spinner, _page,
181
181
  * AUTH
182
182
  */
183
183
  spinner === null || spinner === void 0 ? void 0 : spinner.info('Loading session data');
184
- let sessionjson = getSessionDataFromFile(sessionId, config, spinner);
184
+ let sessionjson = yield getSessionDataFromFile(sessionId, config, spinner);
185
185
  if (!sessionjson && sessionjson !== "" && config.sessionDataBucketAuth) {
186
186
  try {
187
187
  spinner === null || spinner === void 0 ? void 0 : spinner.info('Unable to find session data file locally, attempting to find session data in cloud storage..');
@@ -255,17 +255,17 @@ function initPage(sessionId, config, qrManager, customUserAgent, spinner, _page,
255
255
  });
256
256
  }
257
257
  exports.initPage = initPage;
258
- const getSessionDataFromFile = (sessionId, config, spinner) => {
258
+ const getSessionDataFromFile = (sessionId, config, spinner) => __awaiter(void 0, void 0, void 0, function* () {
259
259
  if ((config === null || config === void 0 ? void 0 : config.sessionData) == "NUKE")
260
260
  return '';
261
261
  //check if [session].json exists in __dirname
262
- const sessionjsonpath = (0, exports.getSessionDataFilePath)(sessionId, config);
262
+ const sessionjsonpath = yield (0, exports.getSessionDataFilePath)(sessionId, config);
263
263
  let sessionjson = '';
264
264
  const sd = process.env[`${sessionId.toUpperCase()}_DATA_JSON`] ? JSON.parse(process.env[`${sessionId.toUpperCase()}_DATA_JSON`]) : config === null || config === void 0 ? void 0 : config.sessionData;
265
265
  sessionjson = (typeof sd === 'string' && sd !== "") ? JSON.parse(Buffer.from(sd, 'base64').toString('ascii')) : sd;
266
- if (sessionjsonpath && typeof sessionjsonpath == 'string' && fs.existsSync(sessionjsonpath)) {
266
+ if (sessionjsonpath && typeof sessionjsonpath == 'string' && (yield (0, tools_1.pathExists)(sessionjsonpath))) {
267
267
  spinner.succeed(`Found session data file: ${sessionjsonpath}`);
268
- const s = fs.readFileSync(sessionjsonpath, "utf8");
268
+ const s = yield fs.readFile(sessionjsonpath, "utf8");
269
269
  try {
270
270
  sessionjson = JSON.parse(s);
271
271
  }
@@ -288,53 +288,48 @@ const getSessionDataFromFile = (sessionId, config, spinner) => {
288
288
  spinner.succeed(`No session data file found for session : ${sessionId}`);
289
289
  }
290
290
  return sessionjson;
291
- };
292
- const deleteSessionData = (config) => {
293
- const sessionjsonpath = (0, exports.getSessionDataFilePath)((config === null || config === void 0 ? void 0 : config.sessionId) || 'session', config);
294
- if (typeof sessionjsonpath == 'string' && fs.existsSync(sessionjsonpath)) {
291
+ });
292
+ const deleteSessionData = (config) => __awaiter(void 0, void 0, void 0, function* () {
293
+ const sessionjsonpath = yield (0, exports.getSessionDataFilePath)((config === null || config === void 0 ? void 0 : config.sessionId) || 'session', config);
294
+ if (typeof sessionjsonpath == 'string' && (yield (0, tools_1.pathExists)(sessionjsonpath))) {
295
295
  const l = `logout detected, deleting session data file: ${sessionjsonpath}`;
296
296
  console.log(l);
297
297
  logging_1.log.info(l);
298
- fs.unlinkSync(sessionjsonpath);
298
+ yield fs.unlink(sessionjsonpath);
299
299
  }
300
- const mdDir = config['userDataDir'];
301
- if (mdDir) {
300
+ const mdDir = yield (0, tools_1.pathExists)(config['userDataDir']);
301
+ if (config['userDataDir'] && mdDir) {
302
302
  logging_1.log.info(`Deleting MD session directory: ${mdDir}`);
303
- //@ts-ignore
304
- fs.rmdirSync(mdDir, { force: true, recursive: true });
303
+ yield fs.rm(mdDir, { force: true, recursive: true });
304
+ logging_1.log.info(`MD directory ${mdDir} deleted: ${!(yield (0, tools_1.pathExists)(mdDir, true))}`);
305
305
  }
306
306
  return true;
307
- };
307
+ });
308
308
  exports.deleteSessionData = deleteSessionData;
309
- const invalidateSesssionData = (config) => {
310
- const sessionjsonpath = (0, exports.getSessionDataFilePath)((config === null || config === void 0 ? void 0 : config.sessionId) || 'session', config);
311
- if (typeof sessionjsonpath == 'string' && fs.existsSync(sessionjsonpath)) {
309
+ const invalidateSesssionData = (config) => __awaiter(void 0, void 0, void 0, function* () {
310
+ const sessionjsonpath = yield (0, exports.getSessionDataFilePath)((config === null || config === void 0 ? void 0 : config.sessionId) || 'session', config);
311
+ if (typeof sessionjsonpath == 'string' && (yield (0, tools_1.pathExists)(sessionjsonpath))) {
312
312
  const l = `logout detected, invalidating session data file: ${sessionjsonpath}`;
313
313
  console.log(l);
314
314
  logging_1.log.info(l);
315
- fs.writeFile(sessionjsonpath, "LOGGED OUT", (err) => {
316
- if (err) {
317
- console.error(err);
318
- return;
319
- }
320
- });
315
+ fs.writeFile(sessionjsonpath, "LOGGED OUT");
321
316
  }
322
317
  return true;
323
- };
318
+ });
324
319
  exports.invalidateSesssionData = invalidateSesssionData;
325
- const getSessionDataFilePath = (sessionId, config) => {
320
+ const getSessionDataFilePath = (sessionId, config) => __awaiter(void 0, void 0, void 0, function* () {
326
321
  var _a, _b;
327
322
  const p = ((_a = require === null || require === void 0 ? void 0 : require.main) === null || _a === void 0 ? void 0 : _a.path) || ((_b = process === null || process === void 0 ? void 0 : process.mainModule) === null || _b === void 0 ? void 0 : _b.path);
328
323
  const sessionjsonpath = ((config === null || config === void 0 ? void 0 : config.sessionDataPath) && (config === null || config === void 0 ? void 0 : config.sessionDataPath.includes('.data.json'))) ? path.join(path.resolve(process.cwd(), (config === null || config === void 0 ? void 0 : config.sessionDataPath) || '')) : path.join(path.resolve(process.cwd(), (config === null || config === void 0 ? void 0 : config.sessionDataPath) || ''), `${sessionId || 'session'}.data.json`);
329
324
  const altSessionJsonPath = p ? ((config === null || config === void 0 ? void 0 : config.sessionDataPath) && (config === null || config === void 0 ? void 0 : config.sessionDataPath.includes('.data.json'))) ? path.join(path.resolve(p, (config === null || config === void 0 ? void 0 : config.sessionDataPath) || '')) : path.join(path.resolve(p, (config === null || config === void 0 ? void 0 : config.sessionDataPath) || ''), `${sessionId || 'session'}.data.json`) : false;
330
- if (fs.existsSync(sessionjsonpath)) {
325
+ if ((0, tools_1.pathExists)(sessionjsonpath)) {
331
326
  return sessionjsonpath;
332
327
  }
333
- else if (p && altSessionJsonPath && fs.existsSync(altSessionJsonPath)) {
328
+ else if (p && altSessionJsonPath && (yield (0, tools_1.pathExists)(altSessionJsonPath))) {
334
329
  return altSessionJsonPath;
335
330
  }
336
331
  return false;
337
- };
332
+ });
338
333
  exports.getSessionDataFilePath = getSessionDataFilePath;
339
334
  const addScript = (page, js) => __awaiter(void 0, void 0, void 0, function* () { return page.evaluate(yield script_preloader_1.scriptLoader.getScript(js)).catch(e => logging_1.log.error(`Injection error: ${js}`, e)); });
340
335
  exports.addScript = addScript;
@@ -453,9 +448,9 @@ function initBrowser(sessionId, config = {}, spinner) {
453
448
  }
454
449
  if (config === null || config === void 0 ? void 0 : config.corsFix)
455
450
  args.push('--disable-web-security');
456
- if (config["userDataDir"] && !fs.existsSync(config["userDataDir"])) {
451
+ if (config["userDataDir"] && !(yield (0, tools_1.pathExists)(config["userDataDir"]))) {
457
452
  spinner === null || spinner === void 0 ? void 0 : spinner.info(`Data dir doesnt exist, creating...: ${config["userDataDir"]}`);
458
- fs.mkdirSync(config["userDataDir"], { recursive: true });
453
+ fs.mkdir(config["userDataDir"], { recursive: true });
459
454
  }
460
455
  const browser = (config === null || config === void 0 ? void 0 : config.browserWSEndpoint) ? yield puppeteer.connect(Object.assign({}, config)) : yield puppeteer.launch(Object.assign(Object.assign({ headless: true, args }, config), { devtools: false }));
461
456
  exports.BROWSER_START_TS = Date.now();
@@ -251,7 +251,7 @@ function create(config = {}) {
251
251
  spinner.fail("Session data most likely expired due to manual host account logout. Please re-authenticate this session.");
252
252
  yield (0, browser_1.kill)(waPage);
253
253
  if (config === null || config === void 0 ? void 0 : config.deleteSessionDataOnLogout)
254
- (0, browser_1.deleteSessionData)(config);
254
+ yield (0, browser_1.deleteSessionData)(config);
255
255
  if (config === null || config === void 0 ? void 0 : config.throwOnExpiredSessionData) {
256
256
  throw new index_1.SessionExpiredError();
257
257
  }
@@ -117,3 +117,19 @@ export declare function rmFileAsync(file: string): Promise<unknown>;
117
117
  * @param requestConfig optional axios config if file parameter is a url
118
118
  */
119
119
  export declare const assertFile: (file: AdvancedFile | Buffer, outfileName: string, desiredOutputType: keyof typeof FileOutputTypes, requestConfig?: any) => Promise<string | Buffer | Readable>;
120
+ /**
121
+ * Checks if a given path exists.
122
+ *
123
+ * If exists, returns the resolved absolute path. Otherwise returns false.
124
+ *
125
+ * @param _path a relative, absolute or homedir path to a folder or a file
126
+ * @param failSilent If you're expecting for the file to not exist and just want the `false` response then set this to true to prevent false-positive error messages in the logs.
127
+ * @returns string | false
128
+ */
129
+ export declare const pathExists: (_path: string, failSilent?: boolean) => Promise<string | false>;
130
+ /**
131
+ * Returns an absolute file path reference
132
+ * @param _path a relative, absolute or homedir path to a folder or a file
133
+ * @returns string
134
+ */
135
+ export declare const fixPath: (_path: string) => string;
@@ -42,18 +42,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
42
42
  return (mod && mod.__esModule) ? mod : { "default": mod };
43
43
  };
44
44
  Object.defineProperty(exports, "__esModule", { value: true });
45
- exports.assertFile = exports.rmFileAsync = exports.FileOutputTypes = exports.FileInputTypes = exports.ensureDUrl = exports.generateGHIssueLink = exports.processSendData = exports.timePromise = exports.now = exports.perf = exports.processSend = exports.base64MimeType = exports.getDUrl = exports.isDataURL = exports.isBase64 = exports.camelize = exports.without = exports.getConfigFromProcessEnv = exports.smartUserAgent = exports.timeout = void 0;
45
+ exports.fixPath = exports.pathExists = exports.assertFile = exports.rmFileAsync = exports.FileOutputTypes = exports.FileInputTypes = exports.ensureDUrl = exports.generateGHIssueLink = exports.processSendData = exports.timePromise = exports.now = exports.perf = exports.processSend = exports.base64MimeType = exports.getDUrl = exports.isDataURL = exports.isBase64 = exports.camelize = exports.without = exports.getConfigFromProcessEnv = exports.smartUserAgent = exports.timeout = void 0;
46
46
  const crypto_1 = __importDefault(require("crypto"));
47
47
  const fs = __importStar(require("fs"));
48
+ const fsp = __importStar(require("fs/promises"));
48
49
  const path = __importStar(require("path"));
49
50
  const datauri_1 = __importDefault(require("datauri"));
50
51
  const is_url_superb_1 = __importDefault(require("is-url-superb"));
51
52
  const model_1 = require("../api/model");
52
53
  const axios_1 = __importDefault(require("axios"));
53
54
  const child_process_1 = require("child_process");
55
+ const os_1 = require("os");
54
56
  const perf_hooks_1 = require("perf_hooks");
55
57
  const mime_1 = __importDefault(require("mime"));
56
- const os_1 = require("os");
58
+ const os_2 = require("os");
57
59
  const stream_1 = require("stream");
58
60
  const logging_1 = require("../logging/logging");
59
61
  //@ts-ignore
@@ -367,7 +369,7 @@ const assertFile = (file, outfileName, desiredOutputType, requestConfig) => __aw
367
369
  /**
368
370
  * Create a temp file in tempdir, return the tempfile.
369
371
  */
370
- const tempFilePath = path.join((0, os_1.tmpdir)(), `${crypto_1.default.randomBytes(6).readUIntLE(0, 6).toString(36)}.${outfileName}`);
372
+ const tempFilePath = path.join((0, os_2.tmpdir)(), `${crypto_1.default.randomBytes(6).readUIntLE(0, 6).toString(36)}.${outfileName}`);
371
373
  logging_1.log.info(`Saved temporary file to ${tempFilePath}`);
372
374
  if (inputType != exports.FileInputTypes.BUFFER) {
373
375
  file = yield (0, exports.ensureDUrl)(file, requestConfig, outfileName);
@@ -392,3 +394,36 @@ const assertFile = (file, outfileName, desiredOutputType, requestConfig) => __aw
392
394
  return file;
393
395
  });
394
396
  exports.assertFile = assertFile;
397
+ /**
398
+ * Checks if a given path exists.
399
+ *
400
+ * If exists, returns the resolved absolute path. Otherwise returns false.
401
+ *
402
+ * @param _path a relative, absolute or homedir path to a folder or a file
403
+ * @param failSilent If you're expecting for the file to not exist and just want the `false` response then set this to true to prevent false-positive error messages in the logs.
404
+ * @returns string | false
405
+ */
406
+ const pathExists = (_path, failSilent) => __awaiter(void 0, void 0, void 0, function* () {
407
+ _path = (0, exports.fixPath)(_path);
408
+ try {
409
+ yield fsp.access(_path, fsp.constants.R_OK | fsp.constants.W_OK);
410
+ return _path;
411
+ }
412
+ catch (error) {
413
+ if (!failSilent)
414
+ logging_1.log.error('Given check path threw an error', error);
415
+ return false;
416
+ }
417
+ });
418
+ exports.pathExists = pathExists;
419
+ /**
420
+ * Returns an absolute file path reference
421
+ * @param _path a relative, absolute or homedir path to a folder or a file
422
+ * @returns string
423
+ */
424
+ const fixPath = (_path) => {
425
+ _path = _path.replace("~", (0, os_1.homedir)());
426
+ _path = _path.includes('./') ? path.join(process.cwd(), _path) : _path;
427
+ return _path;
428
+ };
429
+ exports.fixPath = fixPath;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-wa/wa-automate",
3
- "version": "4.50.0",
3
+ "version": "4.50.2",
4
4
  "licenseCheckUrl": "https://funcs.openwa.dev/license-check",
5
5
  "brokenMethodReportUrl": "https://funcs.openwa.dev/report-bm",
6
6
  "patches": "https://cdn.openwa.dev/patches.json",