@open-wa/wa-automate 4.54.5 → 4.55.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,9 +13,11 @@ export declare const setupAuthenticationLayer: (cliConfig: cliFlags) => void;
13
13
  export declare const setupApiDocs: (cliConfig: cliFlags) => void;
14
14
  export declare const setupSwaggerStatsMiddleware: (cliConfig: cliFlags) => Promise<void>;
15
15
  export declare const setupRefocusDisengageMiddleware: (cliConfig: cliFlags) => void;
16
+ export declare const setupMetaProcessMiddleware: (client: Client, cliConfig: any) => void;
16
17
  export declare const getCommands: () => any;
17
18
  export declare const listListeners: () => string[];
18
19
  export declare const setupMediaMiddleware: () => void;
20
+ export declare const setupTunnel: (cliConfig: any, tunnelCode: string, PORT: number) => Promise<string>;
19
21
  export declare const setupTwilioCompatibleWebhook: (cliConfig: cliFlags, client: Client) => void;
20
22
  export declare const setupChatwoot: (cliConfig: cliFlags, client: Client) => void;
21
23
  export declare const setupBotPressHandler: (cliConfig: cliFlags, client: Client) => void;
@@ -35,7 +35,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
35
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.setupSocketServer = exports.setupBotPressHandler = exports.setupChatwoot = exports.setupTwilioCompatibleWebhook = exports.setupMediaMiddleware = exports.listListeners = exports.getCommands = exports.setupRefocusDisengageMiddleware = exports.setupSwaggerStatsMiddleware = exports.setupApiDocs = exports.setupAuthenticationLayer = exports.enableCORSRequests = exports.setUpExpressApp = exports.setupHttpServer = exports.server = exports.app = void 0;
38
+ exports.setupSocketServer = exports.setupBotPressHandler = exports.setupChatwoot = exports.setupTwilioCompatibleWebhook = exports.setupTunnel = exports.setupMediaMiddleware = exports.listListeners = exports.getCommands = exports.setupMetaProcessMiddleware = exports.setupRefocusDisengageMiddleware = exports.setupSwaggerStatsMiddleware = exports.setupApiDocs = exports.setupAuthenticationLayer = exports.enableCORSRequests = exports.setUpExpressApp = exports.setupHttpServer = exports.server = exports.app = void 0;
39
39
  //@ts-ignore
40
40
  const express_1 = __importDefault(require("express"));
41
41
  const https_1 = __importDefault(require("https"));
@@ -52,8 +52,11 @@ const xmlbuilder2_1 = require("xmlbuilder2");
52
52
  const chatwoot_1 = require("./integrations/chatwoot");
53
53
  const express_ipfilter_1 = require("express-ipfilter");
54
54
  const helmet_1 = __importDefault(require("helmet"));
55
+ const localtunnel_1 = __importDefault(require("localtunnel"));
56
+ const child_process_1 = require("child_process");
55
57
  exports.app = (0, express_1.default)();
56
58
  exports.server = http_1.default.createServer(exports.app);
59
+ let tunnel;
57
60
  const trimChatId = (chatId) => chatId.replace("@c.us", "").replace("@g.us", "");
58
61
  const socketListenerCallbacks = {};
59
62
  // const existingListeners = () => Object.keys(Object.keys(socketListenerCallbacks).flatMap(id=>Object.keys(socketListenerCallbacks[id])).reduce((acc,curr)=>{acc[curr]=true;return acc},{}))
@@ -243,6 +246,69 @@ const setupMetaMiddleware = () => {
243
246
  }
244
247
  }));
245
248
  };
249
+ const setupMetaProcessMiddleware = (client, cliConfig) => {
250
+ /**
251
+ * Kill the client. End the process.
252
+ */
253
+ let closing = false;
254
+ const nuke = (req, res, restart) => __awaiter(void 0, void 0, void 0, function* () {
255
+ res.set("Connection", "close");
256
+ res.send(closing ? `Already closing! Stop asking!!` : 'Closing after connections closed. Waiting max 5 seconds');
257
+ res.end();
258
+ res.connection.end();
259
+ res.connection.destroy();
260
+ if (closing)
261
+ return;
262
+ closing = true;
263
+ yield client.kill("API_KILL");
264
+ __1.log.info("Waiting for maximum ");
265
+ if (tunnel && tunnel.close && typeof tunnel.close === 'function')
266
+ tunnel.close();
267
+ yield Promise.race([
268
+ new Promise((resolve) => exports.server.close(() => {
269
+ console.log('Server closed');
270
+ resolve(true);
271
+ })),
272
+ new Promise(resolve => setTimeout(resolve, 5000, 'timeout'))
273
+ ]);
274
+ if (process.env.pm_id && process.env.PM2_USAGE) {
275
+ const cmd = `pm2 ${restart ? 'restart' : 'stop'} ${process.env.pm_id}`;
276
+ __1.log.info(`PM2 DETECTED, RUNNING COMMAND: ${cmd}`);
277
+ const cmda = cmd.split(' ');
278
+ (0, child_process_1.spawn)(cmda[0], cmda.splice(1), { stdio: 'inherit' });
279
+ }
280
+ else {
281
+ if (restart)
282
+ setTimeout(function () {
283
+ process.on("exit", function () {
284
+ (0, child_process_1.spawn)(process.argv.shift(), process.argv, {
285
+ cwd: process.cwd(),
286
+ detached: true,
287
+ stdio: "inherit"
288
+ });
289
+ });
290
+ process.exit();
291
+ }, 5000);
292
+ else
293
+ process.exit(restart ? 0 : 10);
294
+ }
295
+ });
296
+ /**
297
+ * Exit code 10 will prevent pm2 process from auto-restarting
298
+ */
299
+ exports.app.post('/meta/process/exit', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
300
+ return yield nuke(req, res, false);
301
+ }));
302
+ /**
303
+ * Will only restart if the process is managed by pm2
304
+ *
305
+ * Note: Only works when `--pm2` flag is enabled.
306
+ */
307
+ exports.app.post('/meta/process/restart', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
308
+ return yield nuke(req, res, true);
309
+ }));
310
+ };
311
+ exports.setupMetaProcessMiddleware = setupMetaProcessMiddleware;
246
312
  const getCommands = () => Object.entries(collections_1.collections['swagger'].paths).reduce((acc, [key, value]) => { var _a, _b, _c, _d; acc[key.replace("/", "")] = ((_d = (_c = (_b = (_a = value === null || value === void 0 ? void 0 : value.post) === null || _a === void 0 ? void 0 : _a.requestBody) === null || _b === void 0 ? void 0 : _b.content["application/json"]) === null || _c === void 0 ? void 0 : _c.example) === null || _d === void 0 ? void 0 : _d.args) || {}; return acc; }, {});
247
313
  exports.getCommands = getCommands;
248
314
  const listListeners = () => {
@@ -253,6 +319,16 @@ const setupMediaMiddleware = () => {
253
319
  exports.app.use("/media", express_1.default.static('media'));
254
320
  };
255
321
  exports.setupMediaMiddleware = setupMediaMiddleware;
322
+ const setupTunnel = (cliConfig, tunnelCode, PORT) => __awaiter(void 0, void 0, void 0, function* () {
323
+ tunnel = yield (0, localtunnel_1.default)({
324
+ port: PORT,
325
+ host: process.env.WA_TUNNEL_SERVER || "https://public.openwa.cloud",
326
+ subdomain: tunnelCode
327
+ });
328
+ cliConfig.apiHost = cliConfig.tunnel = tunnel.url;
329
+ return tunnel.url;
330
+ });
331
+ exports.setupTunnel = setupTunnel;
256
332
  const setupTwilioCompatibleWebhook = (cliConfig, client) => {
257
333
  const url = cliConfig.twilioWebhook;
258
334
  client.onMessage((message) => __awaiter(void 0, void 0, void 0, function* () {
@@ -1,6 +1,6 @@
1
1
  import { Client } from '../api/Client';
2
2
  import { AdvancedConfig, ConfigObject } from '../api/model/index';
3
- export declare const pkg: any, configWithCases: any, timeout: (ms: number) => Promise<unknown>;
3
+ export declare const pkg: any, configWithCases: any, timeout: (ms: number) => Promise<string>;
4
4
  export declare let screenshot: any;
5
5
  /**
6
6
  * Used to initialize the client session.
@@ -271,7 +271,10 @@ function create(config = {}) {
271
271
  _serialized: earlyWid
272
272
  }, debugInfo, spinner);
273
273
  if (authenticated == 'timeout') {
274
- const outOfReach = yield Promise.race([(0, auth_1.phoneIsOutOfReach)(waPage), (0, exports.timeout)(20 * 1000)]);
274
+ const oorProms = [(0, auth_1.phoneIsOutOfReach)(waPage)];
275
+ if ((config === null || config === void 0 ? void 0 : config.oorTimeout) !== 0)
276
+ oorProms.push((0, exports.timeout)(((config === null || config === void 0 ? void 0 : config.oorTimeout) || 60) * 1000));
277
+ const outOfReach = yield Promise.race(oorProms);
275
278
  spinner.emit(outOfReach && outOfReach !== 'timeout' ? 'appOffline' : 'authTimeout');
276
279
  spinner.fail(outOfReach && outOfReach !== 'timeout' ? 'Authentication timed out. Please open the app on the phone. Shutting down' : 'Authentication timed out. Shutting down. Consider increasing authTimeout config variable: https://open-wa.github.io/wa-automate-nodejs/interfaces/configobject.html#authtimeout');
277
280
  yield (0, browser_1.kill)(waPage);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-wa/wa-automate",
3
- "version": "4.54.5",
3
+ "version": "4.55.0",
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",