@open-wa/wa-automate 4.27.8 → 4.28.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.
- package/README.md +1 -1
- package/bin/config-schema.json +1 -1
- package/dist/api/Client.d.ts +0 -1
- package/dist/api/Client.js +71 -78
- package/dist/api/model/config.d.ts +7 -0
- package/dist/build/build-postman.js +3 -3
- package/dist/cli/collections.js +8 -7
- package/dist/cli/index.js +22 -22
- package/dist/cli/integrations/chatwoot.js +1 -1
- package/dist/cli/server.js +11 -11
- package/dist/cli/setup.js +21 -15
- package/dist/config/puppeteer.config.js +1 -1
- package/dist/connect/socket.js +2 -2
- package/dist/controllers/auth.js +20 -11
- package/dist/controllers/browser.js +25 -19
- package/dist/controllers/events.js +17 -1
- package/dist/controllers/initializer.js +60 -40
- package/dist/controllers/launch_checks.js +2 -2
- package/dist/controllers/patch_manager.js +4 -4
- package/dist/controllers/popup/index.js +5 -5
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/logging/custom_transport.d.ts +9 -0
- package/dist/logging/custom_transport.js +35 -0
- package/dist/logging/logging.d.ts +50 -0
- package/dist/logging/logging.js +172 -0
- package/dist/structures/preProcessors.js +3 -3
- package/dist/utils/configSchema.js +2 -2
- package/dist/utils/tools.js +1 -1
- package/package.json +12 -9
@@ -42,6 +42,7 @@ 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
44
|
const tree_kill_1 = __importDefault(require("tree-kill"));
|
45
|
+
const logging_1 = require("../logging/logging");
|
45
46
|
let browser;
|
46
47
|
function initPage(sessionId, config, customUserAgent, spinner, _page, skipAuth) {
|
47
48
|
var _a, _b, _c, _d, _e;
|
@@ -131,7 +132,7 @@ function initPage(sessionId, config, customUserAgent, spinner, _page, skipAuth)
|
|
131
132
|
if (!sessionjson && sessionjson !== "" && config.sessionDataBucketAuth) {
|
132
133
|
try {
|
133
134
|
spinner === null || spinner === void 0 ? void 0 : spinner.info('Unable to find session data file locally, attempting to find session data in cloud storage..');
|
134
|
-
sessionjson = JSON.parse(Buffer.from(yield pico_s3_1.getTextFile(Object.assign(Object.assign({ directory: '_sessionData' }, JSON.parse(Buffer.from(config.sessionDataBucketAuth, 'base64').toString('ascii'))), { filename: `${config.sessionId || 'session'}.data.json` })), 'base64').toString('ascii'));
|
135
|
+
sessionjson = JSON.parse(Buffer.from(yield (0, pico_s3_1.getTextFile)(Object.assign(Object.assign({ directory: '_sessionData' }, JSON.parse(Buffer.from(config.sessionDataBucketAuth, 'base64').toString('ascii'))), { filename: `${config.sessionId || 'session'}.data.json` })), 'base64').toString('ascii'));
|
135
136
|
spinner === null || spinner === void 0 ? void 0 : spinner.succeed('Successfully downloaded session data file from cloud storage!');
|
136
137
|
}
|
137
138
|
catch (error) {
|
@@ -198,7 +199,7 @@ const getSessionDataFromFile = (sessionId, config, spinner) => {
|
|
198
199
|
if ((config === null || config === void 0 ? void 0 : config.sessionData) == "NUKE")
|
199
200
|
return '';
|
200
201
|
//check if [session].json exists in __dirname
|
201
|
-
const sessionjsonpath = exports.getSessionDataFilePath(sessionId, config);
|
202
|
+
const sessionjsonpath = (0, exports.getSessionDataFilePath)(sessionId, config);
|
202
203
|
let sessionjson = '';
|
203
204
|
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;
|
204
205
|
sessionjson = (typeof sd === 'string' && sd !== "") ? JSON.parse(Buffer.from(sd, 'base64').toString('ascii')) : sd;
|
@@ -229,9 +230,11 @@ const getSessionDataFromFile = (sessionId, config, spinner) => {
|
|
229
230
|
return sessionjson;
|
230
231
|
};
|
231
232
|
const deleteSessionData = (config) => {
|
232
|
-
const sessionjsonpath = exports.getSessionDataFilePath((config === null || config === void 0 ? void 0 : config.sessionId) || 'session', config);
|
233
|
+
const sessionjsonpath = (0, exports.getSessionDataFilePath)((config === null || config === void 0 ? void 0 : config.sessionId) || 'session', config);
|
233
234
|
if (typeof sessionjsonpath == 'string' && fs.existsSync(sessionjsonpath)) {
|
234
|
-
|
235
|
+
const l = `logout detected, deleting session data file: ${sessionjsonpath}`;
|
236
|
+
console.log(l);
|
237
|
+
logging_1.log.info(l);
|
235
238
|
fs.unlinkSync(sessionjsonpath);
|
236
239
|
}
|
237
240
|
return true;
|
@@ -263,16 +266,16 @@ function injectApi(page) {
|
|
263
266
|
'qr.min.js',
|
264
267
|
'base64.js',
|
265
268
|
'hash.js'
|
266
|
-
].map(js => exports.addScript(page, js)));
|
267
|
-
yield exports.addScript(page, 'wapi.js');
|
268
|
-
yield exports.addScript(page, 'wapi.js');
|
269
|
-
yield exports.addScript(page, 'wapi.js');
|
270
|
-
yield exports.addScript(page, 'wapi.js');
|
271
|
-
yield exports.addScript(page, 'wapi.js');
|
272
|
-
yield exports.addScript(page, 'wapi.js');
|
273
|
-
yield exports.addScript(page, 'wapi.js');
|
274
|
-
yield exports.addScript(page, 'wapi.js');
|
275
|
-
yield exports.addScript(page, 'launch.js');
|
269
|
+
].map(js => (0, exports.addScript)(page, js)));
|
270
|
+
yield (0, exports.addScript)(page, 'wapi.js');
|
271
|
+
yield (0, exports.addScript)(page, 'wapi.js');
|
272
|
+
yield (0, exports.addScript)(page, 'wapi.js');
|
273
|
+
yield (0, exports.addScript)(page, 'wapi.js');
|
274
|
+
yield (0, exports.addScript)(page, 'wapi.js');
|
275
|
+
yield (0, exports.addScript)(page, 'wapi.js');
|
276
|
+
yield (0, exports.addScript)(page, 'wapi.js');
|
277
|
+
yield (0, exports.addScript)(page, 'wapi.js');
|
278
|
+
yield (0, exports.addScript)(page, 'launch.js');
|
276
279
|
return page;
|
277
280
|
});
|
278
281
|
}
|
@@ -347,10 +350,13 @@ function initBrowser(sessionId, config = {}) {
|
|
347
350
|
try {
|
348
351
|
// const tunnel = await devtools.createTunnel(browser);
|
349
352
|
const tunnel = config.devtools == 'local' ? devtools.getLocalDevToolsUrl(browser) : (yield devtools.createTunnel(browser)).url;
|
350
|
-
|
353
|
+
const l = `\ndevtools URL: ${typeof config.devtools == 'object' ? Object.assign(Object.assign({}, config.devtools), { tunnel }) : tunnel}`;
|
354
|
+
console.log(l);
|
355
|
+
logging_1.log.info(l);
|
351
356
|
}
|
352
357
|
catch (error) {
|
353
|
-
console.
|
358
|
+
console.error("initBrowser -> error", error);
|
359
|
+
logging_1.log.error("initBrowser -> error", error);
|
354
360
|
}
|
355
361
|
}
|
356
362
|
return browser;
|
@@ -363,10 +369,10 @@ function getWAPage(browser) {
|
|
363
369
|
return pages[0];
|
364
370
|
});
|
365
371
|
}
|
366
|
-
death_1.default(() => __awaiter(void 0, void 0, void 0, function* () {
|
372
|
+
(0, death_1.default)(() => __awaiter(void 0, void 0, void 0, function* () {
|
367
373
|
//clean up code here
|
368
374
|
if (browser)
|
369
|
-
yield exports.kill(browser);
|
375
|
+
yield (0, exports.kill)(browser);
|
370
376
|
}));
|
371
377
|
/**
|
372
378
|
* @internal
|
@@ -391,7 +397,7 @@ const kill = (p, b, exit, pid) => __awaiter(void 0, void 0, void 0, function* ()
|
|
391
397
|
yield killBrowser(b);
|
392
398
|
}
|
393
399
|
if (pid)
|
394
|
-
tree_kill_1.default(pid, 'SIGKILL');
|
400
|
+
(0, tree_kill_1.default)(pid, 'SIGKILL');
|
395
401
|
if (exit)
|
396
402
|
process.exit();
|
397
403
|
return;
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Spin = exports.EvEmitter = exports.ev = void 0;
|
7
7
|
const eventemitter2_1 = require("eventemitter2");
|
8
8
|
const spinnies_1 = __importDefault(require("spinnies"));
|
9
|
+
const logging_1 = require("../logging/logging");
|
9
10
|
const spinner = {
|
10
11
|
"interval": 80,
|
11
12
|
"frames": [
|
@@ -85,7 +86,22 @@ class EvEmitter {
|
|
85
86
|
this.eventNamespace = eventNamespace;
|
86
87
|
}
|
87
88
|
emit(data, eventNamespaceOverride) {
|
88
|
-
|
89
|
+
const eventName = `${eventNamespaceOverride || this.eventNamespace}.${this.sessionId}`;
|
90
|
+
const sessionId = this.sessionId;
|
91
|
+
const eventNamespace = eventNamespaceOverride || this.eventNamespace;
|
92
|
+
exports.ev.emit(eventName, data, sessionId, eventNamespace);
|
93
|
+
if (![
|
94
|
+
//DO NOT ALLOW THESE NAMESPACES ON TRANSPORTS!!
|
95
|
+
"sessionData",
|
96
|
+
"sessionDataBase64",
|
97
|
+
"qr",
|
98
|
+
].find(x => eventNamespace == x))
|
99
|
+
logging_1.log.info(typeof data === 'string' ? data : eventName, {
|
100
|
+
eventName,
|
101
|
+
data,
|
102
|
+
sessionId,
|
103
|
+
eventNamespace
|
104
|
+
});
|
89
105
|
// ev.emit(`${this.sessionId}.${this.eventNamespace}`,data,this.sessionId,this.eventNamespace);
|
90
106
|
}
|
91
107
|
}
|
@@ -61,10 +61,11 @@ const fs_extra_1 = require("fs-extra");
|
|
61
61
|
const pico_s3_1 = require("pico-s3");
|
62
62
|
const init_patch_1 = require("./init_patch");
|
63
63
|
const patch_manager_1 = require("./patch_manager");
|
64
|
+
const logging_1 = require("../logging/logging");
|
64
65
|
const timeout = (ms) => {
|
65
66
|
return new Promise(resolve => setTimeout(resolve, ms, 'timeout'));
|
66
67
|
};
|
67
|
-
exports.pkg = fs_extra_1.readJsonSync(path.join(__dirname, '../../package.json')), exports.configWithCases = fs_extra_1.readJsonSync(path.join(__dirname, '../../bin/config-schema.json')), exports.timeout = timeout;
|
68
|
+
exports.pkg = (0, fs_extra_1.readJsonSync)(path.join(__dirname, '../../package.json')), exports.configWithCases = (0, fs_extra_1.readJsonSync)(path.join(__dirname, '../../bin/config-schema.json')), exports.timeout = timeout;
|
68
69
|
/**
|
69
70
|
* Used to initialize the client session.
|
70
71
|
*
|
@@ -87,6 +88,10 @@ function create(config = {}) {
|
|
87
88
|
var _a, _b, _c;
|
88
89
|
return __awaiter(this, void 0, void 0, function* () {
|
89
90
|
const START_TIME = Date.now();
|
91
|
+
if (config.logging) {
|
92
|
+
if (Array.isArray(config === null || config === void 0 ? void 0 : config.logging))
|
93
|
+
config.logging = (0, logging_1.setupLogging)(config === null || config === void 0 ? void 0 : config.logging, `owa-${(config === null || config === void 0 ? void 0 : config.sessionId) || 'session'}`);
|
94
|
+
}
|
90
95
|
let waPage = undefined;
|
91
96
|
let notifier;
|
92
97
|
let sessionId = '';
|
@@ -95,19 +100,22 @@ function create(config = {}) {
|
|
95
100
|
config.eventMode = true;
|
96
101
|
}
|
97
102
|
if (!(config === null || config === void 0 ? void 0 : config.skipUpdateCheck) || (config === null || config === void 0 ? void 0 : config.keepUpdated)) {
|
98
|
-
notifier = yield update_notifier_1.default({
|
103
|
+
notifier = yield (0, update_notifier_1.default)({
|
99
104
|
pkg: exports.pkg,
|
100
105
|
updateCheckInterval: 0
|
101
106
|
});
|
102
107
|
notifier.notify();
|
103
108
|
if ((notifier === null || notifier === void 0 ? void 0 : notifier.update) && (config === null || config === void 0 ? void 0 : config.keepUpdated) && (notifier === null || notifier === void 0 ? void 0 : notifier.update.latest) !== exports.pkg.version) {
|
104
109
|
console.log('UPDATING @OPEN-WA');
|
110
|
+
logging_1.log.info('UPDATING @OPEN-WA');
|
105
111
|
const crossSpawn = yield Promise.resolve().then(() => __importStar(require('cross-spawn')));
|
106
112
|
const result = crossSpawn.sync('npm', ['i', '@open-wa/wa-automate'], { stdio: 'inherit' });
|
107
113
|
if (!result.stderr) {
|
108
114
|
console.log('UPDATED SUCCESSFULLY');
|
115
|
+
logging_1.log.info('UPDATED SUCCESSFULLY');
|
109
116
|
}
|
110
117
|
console.log('RESTARTING PROCESS');
|
118
|
+
logging_1.log.info('RESTARTING PROCESS');
|
111
119
|
process.on("exit", function () {
|
112
120
|
crossSpawn.spawn(process.argv.shift(), process.argv, {
|
113
121
|
cwd: process.cwd(),
|
@@ -120,7 +128,7 @@ function create(config = {}) {
|
|
120
128
|
}
|
121
129
|
if (config === null || config === void 0 ? void 0 : config.inDocker) {
|
122
130
|
//try to infer config variables from process.env
|
123
|
-
config = Object.assign(Object.assign({}, config), tools_1.getConfigFromProcessEnv(exports.configWithCases));
|
131
|
+
config = Object.assign(Object.assign({}, config), (0, tools_1.getConfigFromProcessEnv)(exports.configWithCases));
|
124
132
|
config.chromiumArgs = (config === null || config === void 0 ? void 0 : config.chromiumArgs) || [];
|
125
133
|
customUserAgent = config.customUserAgent;
|
126
134
|
}
|
@@ -133,7 +141,7 @@ function create(config = {}) {
|
|
133
141
|
gradient: ["red", "#f80"],
|
134
142
|
lineHeight: 3
|
135
143
|
});
|
136
|
-
console.log((config === null || config === void 0 ? void 0 : config.disableSpins) ? boxen_1.default([
|
144
|
+
console.log((config === null || config === void 0 ? void 0 : config.disableSpins) ? (0, boxen_1.default)([
|
137
145
|
`@open-wa/wa-automate `,
|
138
146
|
`${exports.pkg.description}`,
|
139
147
|
`Version: ${exports.pkg.version} `,
|
@@ -143,6 +151,7 @@ function create(config = {}) {
|
|
143
151
|
const { popup } = yield Promise.resolve().then(() => __importStar(require('./popup')));
|
144
152
|
const popupaddr = yield popup(config);
|
145
153
|
console.log(`You can also authenticate the session at: ${popupaddr}`);
|
154
|
+
logging_1.log.info(`You can also authenticate the session at: ${popupaddr}`);
|
146
155
|
}
|
147
156
|
if (!sessionId)
|
148
157
|
sessionId = 'session';
|
@@ -165,12 +174,12 @@ function create(config = {}) {
|
|
165
174
|
spinner.info(`Using custom chromium args with multi device will cause issues! Please remove themm`);
|
166
175
|
if ((config === null || config === void 0 ? void 0 : config.multiDevice) && !(config === null || config === void 0 ? void 0 : config.useChrome))
|
167
176
|
spinner.info(`It is recommended to set useChrome: true or use the --use-chrome flag if you are experiencing issues with Multi device support`);
|
168
|
-
waPage = yield browser_1.initPage(sessionId, config, customUserAgent, spinner);
|
177
|
+
waPage = yield (0, browser_1.initPage)(sessionId, config, customUserAgent, spinner);
|
169
178
|
spinner.succeed('Browser Launched');
|
170
179
|
const throwOnError = config && config.throwErrorOnTosBlock == true;
|
171
180
|
const PAGE_UA = yield waPage.evaluate('navigator.userAgent');
|
172
181
|
const BROWSER_VERSION = yield waPage.browser().version();
|
173
|
-
const OS = os_name_1.default();
|
182
|
+
const OS = (0, os_name_1.default)();
|
174
183
|
const START_TS = Date.now();
|
175
184
|
const screenshotPath = `./logs/${config.sessionId || 'session'}/${START_TS}`;
|
176
185
|
exports.screenshot = (page) => __awaiter(this, void 0, void 0, function* () {
|
@@ -178,7 +187,7 @@ function create(config = {}) {
|
|
178
187
|
path: `${screenshotPath}/${Date.now()}.jpg`
|
179
188
|
}).catch(() => {
|
180
189
|
fs.mkdirSync(screenshotPath, { recursive: true });
|
181
|
-
return exports.screenshot(page);
|
190
|
+
return (0, exports.screenshot)(page);
|
182
191
|
});
|
183
192
|
console.log('Screenshot taken. path:', `${screenshotPath}`);
|
184
193
|
});
|
@@ -187,13 +196,13 @@ function create(config = {}) {
|
|
187
196
|
for (let i = 0; i < msg.args().length; ++i)
|
188
197
|
console.log(`${i}: ${msg.args()[i]}`);
|
189
198
|
if (msg.type() === 'error' && !msg.text().includes('apify') && !msg.text().includes('crashlogs'))
|
190
|
-
yield exports.screenshot(waPage);
|
199
|
+
yield (0, exports.screenshot)(waPage);
|
191
200
|
}));
|
192
201
|
const WA_AUTOMATE_VERSION = `${exports.pkg.version}${(notifier === null || notifier === void 0 ? void 0 : notifier.update) && ((notifier === null || notifier === void 0 ? void 0 : notifier.update.latest) !== exports.pkg.version) ? ` UPDATE AVAILABLE: ${notifier === null || notifier === void 0 ? void 0 : notifier.update.latest}` : ''}`;
|
193
202
|
yield waPage.waitForFunction('window.Debug!=undefined && window.Debug.VERSION!=undefined');
|
194
203
|
//@ts-ignore
|
195
204
|
const WA_VERSION = yield waPage.evaluate(() => window.Debug ? window.Debug.VERSION : 'I think you have been TOS_BLOCKed');
|
196
|
-
const canInjectEarly = yield patch_manager_1.earlyInjectionCheck(waPage);
|
205
|
+
const canInjectEarly = yield (0, patch_manager_1.earlyInjectionCheck)(waPage);
|
197
206
|
const attemptingReauth = yield waPage.evaluate(`!!(localStorage['WAToken2'] || localStorage['last-wid-md'])`);
|
198
207
|
let debugInfo = {
|
199
208
|
WA_VERSION,
|
@@ -205,8 +214,10 @@ function create(config = {}) {
|
|
205
214
|
};
|
206
215
|
if ((config === null || config === void 0 ? void 0 : config.logDebugInfoAsObject) || (config === null || config === void 0 ? void 0 : config.disableSpins))
|
207
216
|
spinner.succeed(`Debug info: ${JSON.stringify(debugInfo, null, 2)}`);
|
208
|
-
else
|
217
|
+
else {
|
209
218
|
console.table(debugInfo);
|
219
|
+
logging_1.log.info('Debug info:', debugInfo);
|
220
|
+
}
|
210
221
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
211
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) => {
|
212
223
|
var { OS, PAGE_UA } = _a, o = __rest(_a, ["OS", "PAGE_UA"]);
|
@@ -216,7 +227,7 @@ function create(config = {}) {
|
|
216
227
|
if (attemptingReauth)
|
217
228
|
yield waPage.evaluate(`window.Store = {"Msg": true}`);
|
218
229
|
spinner.start('Injecting api');
|
219
|
-
waPage = yield browser_1.injectApi(waPage);
|
230
|
+
waPage = yield (0, browser_1.injectApi)(waPage);
|
220
231
|
spinner.start('WAPI injected');
|
221
232
|
}
|
222
233
|
else {
|
@@ -226,17 +237,17 @@ function create(config = {}) {
|
|
226
237
|
}
|
227
238
|
spinner.start('Authenticating');
|
228
239
|
const authRace = [];
|
229
|
-
authRace.push(auth_1.isAuthenticated(waPage).catch(() => { }));
|
240
|
+
authRace.push((0, auth_1.isAuthenticated)(waPage).catch(() => { }));
|
230
241
|
if ((config === null || config === void 0 ? void 0 : config.authTimeout) !== 0) {
|
231
|
-
authRace.push(exports.timeout((config.authTimeout || config.multiDevice ? 120 : 60) * 1000));
|
242
|
+
authRace.push((0, exports.timeout)((config.authTimeout || config.multiDevice ? 120 : 60) * 1000));
|
232
243
|
}
|
233
244
|
const authenticated = yield Promise.race(authRace);
|
234
245
|
if (authenticated === 'NUKE' && !(config === null || config === void 0 ? void 0 : config.ignoreNuke)) {
|
235
246
|
//kill the browser
|
236
247
|
spinner.fail("Session data most likely expired due to manual host account logout. Please re-authenticate this session.");
|
237
|
-
yield browser_1.kill(waPage);
|
248
|
+
yield (0, browser_1.kill)(waPage);
|
238
249
|
if (config === null || config === void 0 ? void 0 : config.deleteSessionDataOnLogout)
|
239
|
-
browser_1.deleteSessionData(config);
|
250
|
+
(0, browser_1.deleteSessionData)(config);
|
240
251
|
if (config === null || config === void 0 ? void 0 : config.throwOnExpiredSessionData) {
|
241
252
|
throw new index_1.SessionExpiredError();
|
242
253
|
}
|
@@ -248,14 +259,14 @@ function create(config = {}) {
|
|
248
259
|
* Attempt to preload the license
|
249
260
|
*/
|
250
261
|
const earlyWid = yield waPage.evaluate(`(localStorage["last-wid"] || '').replace(/"/g,"")`);
|
251
|
-
const licensePromise = patch_manager_1.getLicense(config, {
|
262
|
+
const licensePromise = (0, patch_manager_1.getLicense)(config, {
|
252
263
|
_serialized: earlyWid
|
253
264
|
}, debugInfo, spinner);
|
254
265
|
if (authenticated == 'timeout') {
|
255
|
-
const outOfReach = yield Promise.race([auth_1.phoneIsOutOfReach(waPage), exports.timeout(20 * 1000)]);
|
266
|
+
const outOfReach = yield Promise.race([(0, auth_1.phoneIsOutOfReach)(waPage), (0, exports.timeout)(20 * 1000)]);
|
256
267
|
spinner.emit(outOfReach && outOfReach !== 'timeout' ? 'appOffline' : 'authTimeout');
|
257
268
|
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');
|
258
|
-
yield browser_1.kill(waPage);
|
269
|
+
yield (0, browser_1.kill)(waPage);
|
259
270
|
if (config === null || config === void 0 ? void 0 : config.killProcessOnTimeout)
|
260
271
|
process.exit();
|
261
272
|
throw new Error(outOfReach ? 'App Offline' : 'Auth Timeout. Consider increasing authTimeout config variable: https://open-wa.github.io/wa-automate-nodejs/interfaces/configobject.html#authtimeout');
|
@@ -266,22 +277,22 @@ function create(config = {}) {
|
|
266
277
|
else {
|
267
278
|
spinner.info('Authenticate to continue');
|
268
279
|
const race = [];
|
269
|
-
race.push(auth_1.smartQr(waPage, config, spinner));
|
280
|
+
race.push((0, auth_1.smartQr)(waPage, config, spinner));
|
270
281
|
if ((config === null || config === void 0 ? void 0 : config.qrTimeout) !== 0) {
|
271
282
|
let to = ((config === null || config === void 0 ? void 0 : config.qrTimeout) || 60) * 1000;
|
272
283
|
if (config === null || config === void 0 ? void 0 : config.multiDevice)
|
273
284
|
to = to * 2;
|
274
|
-
race.push(exports.timeout(to));
|
285
|
+
race.push((0, exports.timeout)(to));
|
275
286
|
}
|
276
287
|
const result = yield Promise.race(race);
|
277
288
|
if (result === "MULTI_DEVICE_DETECTED" && !(config === null || config === void 0 ? void 0 : config.multiDevice)) {
|
278
|
-
yield browser_1.kill(waPage);
|
289
|
+
yield (0, browser_1.kill)(waPage);
|
279
290
|
return create(Object.assign(Object.assign({}, config), { multiDevice: true }));
|
280
291
|
}
|
281
292
|
if (result == 'timeout') {
|
282
293
|
spinner.emit('qrTimeout');
|
283
294
|
spinner.fail('QR scan took too long. Session Timed Out. Shutting down. Consider increasing qrTimeout config variable: https://open-wa.github.io/wa-automate-nodejs/interfaces/configobject.html#qrtimeout');
|
284
|
-
yield browser_1.kill(waPage);
|
295
|
+
yield (0, browser_1.kill)(waPage);
|
285
296
|
if (config === null || config === void 0 ? void 0 : config.killProcessOnTimeout)
|
286
297
|
process.exit();
|
287
298
|
throw new Error('QR Timeout');
|
@@ -293,7 +304,7 @@ function create(config = {}) {
|
|
293
304
|
yield waPage.evaluate("window.Store = undefined");
|
294
305
|
if (config === null || config === void 0 ? void 0 : config.waitForRipeSession) {
|
295
306
|
spinner.start("Waiting for ripe session...");
|
296
|
-
if (yield auth_1.waitForRipeSession(waPage))
|
307
|
+
if (yield (0, auth_1.waitForRipeSession)(waPage))
|
297
308
|
spinner.succeed("Session ready for injection");
|
298
309
|
else
|
299
310
|
spinner.fail("You may experience issues in headless mode. Continuing...");
|
@@ -301,13 +312,13 @@ function create(config = {}) {
|
|
301
312
|
}
|
302
313
|
const pre = canInjectEarly ? 'Rei' : 'I';
|
303
314
|
spinner.start(`${pre}njecting api`);
|
304
|
-
waPage = yield browser_1.injectApi(waPage);
|
315
|
+
waPage = yield (0, browser_1.injectApi)(waPage);
|
305
316
|
spinner.succeed(`WAPI ${pre}njected`);
|
306
317
|
if (canInjectEarly) {
|
307
318
|
//check if page is valid after 5 seconds
|
308
319
|
spinner.start('Checking if session is valid');
|
309
320
|
if (config === null || config === void 0 ? void 0 : config.safeMode)
|
310
|
-
yield exports.timeout(5000);
|
321
|
+
yield (0, exports.timeout)(5000);
|
311
322
|
}
|
312
323
|
//@ts-ignore
|
313
324
|
const VALID_SESSION = yield waPage.evaluate(() => window.Store && window.Store.Msg ? true : false);
|
@@ -315,7 +326,7 @@ function create(config = {}) {
|
|
315
326
|
/**
|
316
327
|
* Session is valid, attempt to preload patches
|
317
328
|
*/
|
318
|
-
const patchPromise = patch_manager_1.getPatch(config, spinner, debugInfo);
|
329
|
+
const patchPromise = (0, patch_manager_1.getPatch)(config, spinner, debugInfo);
|
319
330
|
spinner.succeed('Client is ready');
|
320
331
|
const localStorage = JSON.parse(yield waPage.evaluate(() => {
|
321
332
|
return JSON.stringify(window.localStorage);
|
@@ -343,40 +354,49 @@ function create(config = {}) {
|
|
343
354
|
if (config === null || config === void 0 ? void 0 : config.sessionDataBucketAuth) {
|
344
355
|
try {
|
345
356
|
spinner === null || spinner === void 0 ? void 0 : spinner.info('Uploading new session data to cloud storage..');
|
346
|
-
yield pico_s3_1.upload(Object.assign(Object.assign({ directory: '_sessionData' }, JSON.parse(Buffer.from(config.sessionDataBucketAuth, 'base64').toString('ascii'))), { filename: `${config.sessionId || 'session'}.data.json`, file: `data:text/plain;base64,${Buffer.from(sdB64).toString('base64')}` }));
|
357
|
+
yield (0, pico_s3_1.upload)(Object.assign(Object.assign({ directory: '_sessionData' }, JSON.parse(Buffer.from(config.sessionDataBucketAuth, 'base64').toString('ascii'))), { filename: `${config.sessionId || 'session'}.data.json`, file: `data:text/plain;base64,${Buffer.from(sdB64).toString('base64')}` }));
|
347
358
|
spinner === null || spinner === void 0 ? void 0 : spinner.succeed('Successfully uploaded session data file to cloud storage!');
|
348
359
|
}
|
349
360
|
catch (error) {
|
350
361
|
spinner === null || spinner === void 0 ? void 0 : spinner.fail(`Something went wrong while uploading new session data to cloud storage bucket. Continuing...`);
|
351
362
|
}
|
352
363
|
}
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
364
|
+
/**
|
365
|
+
* Set page-level logging
|
366
|
+
*/
|
367
|
+
waPage.on('console', msg => {
|
368
|
+
if (config === null || config === void 0 ? void 0 : config.logConsole)
|
369
|
+
console.log(msg);
|
370
|
+
logging_1.log.info('Page Console:', msg.text());
|
371
|
+
});
|
372
|
+
waPage.on('error', error => {
|
373
|
+
if (config === null || config === void 0 ? void 0 : config.logConsoleErrors)
|
374
|
+
console.error(error);
|
375
|
+
logging_1.log.error('Page Console Error:', error.text());
|
376
|
+
});
|
357
377
|
if (config === null || config === void 0 ? void 0 : config.restartOnCrash)
|
358
378
|
waPage.on('error', (error) => __awaiter(this, void 0, void 0, function* () {
|
359
379
|
console.error('Page Crashed! Restarting...', error);
|
360
|
-
yield browser_1.kill(waPage);
|
380
|
+
yield (0, browser_1.kill)(waPage);
|
361
381
|
yield create(config).then(config.restartOnCrash);
|
362
382
|
}));
|
363
|
-
const pureWAPI = yield launch_checks_1.checkWAPIHash();
|
383
|
+
const pureWAPI = yield (0, launch_checks_1.checkWAPIHash)();
|
364
384
|
if (!pureWAPI) {
|
365
385
|
config.skipBrokenMethodsCheck = true;
|
366
386
|
// config.skipPatches = true;
|
367
387
|
}
|
368
388
|
debugInfo.NUM = yield waPage.evaluate(`(window.localStorage['last-wid'] || '').replace('@c.us','').replace(/"/g,"").slice(-4)`);
|
369
|
-
debugInfo.NUM_HASH = crypto_1.createHash('md5').update(yield waPage.evaluate(`(window.localStorage['last-wid'] || '').replace('@c.us','').replace(/"/g,"")`), 'utf8').digest('hex');
|
389
|
+
debugInfo.NUM_HASH = (0, crypto_1.createHash)('md5').update(yield waPage.evaluate(`(window.localStorage['last-wid'] || '').replace('@c.us','').replace(/"/g,"")`), 'utf8').digest('hex');
|
370
390
|
if (config === null || config === void 0 ? void 0 : config.hostNotificationLang) {
|
371
391
|
yield waPage.evaluate(`window.hostlang="${config.hostNotificationLang}"`);
|
372
392
|
}
|
373
393
|
//patch issues with wapi.js
|
374
394
|
if (!(config === null || config === void 0 ? void 0 : config.skipPatches)) {
|
375
|
-
yield patch_manager_1.getAndInjectLivePatch(waPage, spinner, yield patchPromise, config, debugInfo);
|
395
|
+
yield (0, patch_manager_1.getAndInjectLivePatch)(waPage, spinner, yield patchPromise, config, debugInfo);
|
376
396
|
debugInfo.OW_KEY = yield waPage.evaluate(`window.o()`);
|
377
397
|
}
|
378
398
|
if ((config === null || config === void 0 ? void 0 : config.skipBrokenMethodsCheck) !== true)
|
379
|
-
yield launch_checks_1.integrityCheck(waPage, notifier, spinner, debugInfo);
|
399
|
+
yield (0, launch_checks_1.integrityCheck)(waPage, notifier, spinner, debugInfo);
|
380
400
|
const LAUNCH_TIME_MS = Date.now() - START_TIME;
|
381
401
|
debugInfo = Object.assign(Object.assign({}, debugInfo), { LAUNCH_TIME_MS });
|
382
402
|
spinner.emit(debugInfo, "DebugInfo");
|
@@ -391,10 +411,10 @@ function create(config = {}) {
|
|
391
411
|
const client = new Client_1.Client(waPage, config, debugInfo);
|
392
412
|
const { me } = yield client.getMe();
|
393
413
|
if ((config === null || config === void 0 ? void 0 : config.licenseKey) || me._serialized !== earlyWid) {
|
394
|
-
yield patch_manager_1.getAndInjectLicense(waPage, config, me, debugInfo, spinner, me._serialized !== earlyWid ? false : yield licensePromise);
|
414
|
+
yield (0, patch_manager_1.getAndInjectLicense)(waPage, config, me, debugInfo, spinner, me._serialized !== earlyWid ? false : yield licensePromise);
|
395
415
|
}
|
396
416
|
spinner.info("Finalizing web session...");
|
397
|
-
yield init_patch_1.injectInitPatch(waPage);
|
417
|
+
yield (0, init_patch_1.injectInitPatch)(waPage);
|
398
418
|
spinner.info("Finalizing client...");
|
399
419
|
yield client.loaded();
|
400
420
|
if (config.ensureHeadfulIntegrity && !attemptingReauth) {
|
@@ -409,13 +429,13 @@ function create(config = {}) {
|
|
409
429
|
}
|
410
430
|
else {
|
411
431
|
spinner.fail('The session is invalid. Retrying');
|
412
|
-
yield browser_1.kill(waPage);
|
432
|
+
yield (0, browser_1.kill)(waPage);
|
413
433
|
return yield create(config);
|
414
434
|
}
|
415
435
|
}
|
416
436
|
catch (error) {
|
417
437
|
spinner.emit(error.message);
|
418
|
-
yield browser_1.kill(waPage);
|
438
|
+
yield (0, browser_1.kill)(waPage);
|
419
439
|
if (error.name === "ProtocolError" && ((_c = error.message) === null || _c === void 0 ? void 0 : _c.includes("Target closed"))) {
|
420
440
|
spinner.fail(error.message);
|
421
441
|
process.exit();
|
@@ -37,7 +37,7 @@ const hasha_1 = __importDefault(require("hasha"));
|
|
37
37
|
const lodash_uniq_1 = __importDefault(require("lodash.uniq"));
|
38
38
|
const fs_extra_1 = require("fs-extra");
|
39
39
|
const fs = __importStar(require("fs"));
|
40
|
-
const pkg = fs_extra_1.readJsonSync(path.join(__dirname, '../../package.json'));
|
40
|
+
const pkg = (0, fs_extra_1.readJsonSync)(path.join(__dirname, '../../package.json'));
|
41
41
|
const currentHash = '8d3a09fe3156605ac2cf55ce920bbbab';
|
42
42
|
function checkWAPIHash() {
|
43
43
|
return __awaiter(this, void 0, void 0, function* () {
|
@@ -52,7 +52,7 @@ function integrityCheck(waPage, notifier, spinner, debugInfo) {
|
|
52
52
|
spinner.start('Checking client integrity');
|
53
53
|
yield waitForIdle();
|
54
54
|
const wapi = fs.readFileSync(path.join(__dirname, '../lib', 'wapi.js'), 'utf8');
|
55
|
-
const methods = lodash_uniq_1.default(wapi.match(/(Store[.\w]*)\(/g).map((x) => x.replace("(", "")));
|
55
|
+
const methods = (0, lodash_uniq_1.default)(wapi.match(/(Store[.\w]*)\(/g).map((x) => x.replace("(", "")));
|
56
56
|
const check = () => __awaiter(this, void 0, void 0, function* () {
|
57
57
|
return yield waPage.evaluate((checkList) => {
|
58
58
|
return checkList.filter(check => {
|
@@ -55,9 +55,9 @@ function getPatch(config, spinner, sessionInfo) {
|
|
55
55
|
if (config === null || config === void 0 ? void 0 : config.cachedPatch) {
|
56
56
|
spinner.info('Searching for cached patch');
|
57
57
|
// open file called patches.json and read as string
|
58
|
-
if (fs_1.existsSync(patchFilePath)) {
|
58
|
+
if ((0, fs_1.existsSync)(patchFilePath)) {
|
59
59
|
spinner.info('Found cached patch');
|
60
|
-
const lastModifiedDate = fs_1.statSync(patchFilePath).mtimeMs;
|
60
|
+
const lastModifiedDate = (0, fs_1.statSync)(patchFilePath).mtimeMs;
|
61
61
|
/**
|
62
62
|
* Check if patchFilePath file is more than 1 day old
|
63
63
|
*/
|
@@ -66,7 +66,7 @@ function getPatch(config, spinner, sessionInfo) {
|
|
66
66
|
spinner.fail('Cached patch is stale.');
|
67
67
|
}
|
68
68
|
else {
|
69
|
-
const patch = fs_1.readFileSync(patchFilePath, 'utf8');
|
69
|
+
const patch = (0, fs_1.readFileSync)(patchFilePath, 'utf8');
|
70
70
|
data = JSON.parse(patch);
|
71
71
|
spinner.info('Cached patch loaded');
|
72
72
|
}
|
@@ -94,7 +94,7 @@ function getPatch(config, spinner, sessionInfo) {
|
|
94
94
|
if (config === null || config === void 0 ? void 0 : config.cachedPatch) {
|
95
95
|
//save patches.json to current working directory
|
96
96
|
spinner === null || spinner === void 0 ? void 0 : spinner.info('Saving patches to current working directory');
|
97
|
-
fs_1.writeFileSync(patchFilePath, JSON.stringify(data, null, 2));
|
97
|
+
(0, fs_1.writeFileSync)(patchFilePath, JSON.stringify(data, null, 2));
|
98
98
|
spinner === null || spinner === void 0 ? void 0 : spinner.succeed('Saved patches to current working directory');
|
99
99
|
}
|
100
100
|
return resolve({
|
@@ -79,7 +79,7 @@ function popup(config) {
|
|
79
79
|
if ((data === null || data === void 0 ? void 0 : data.includes) && (data === null || data === void 0 ? void 0 : data.includes("ready for account"))) {
|
80
80
|
//@ts-ignore
|
81
81
|
popupListener.off();
|
82
|
-
yield exports.closeHttp();
|
82
|
+
yield (0, exports.closeHttp)();
|
83
83
|
}
|
84
84
|
}), { objectify: true });
|
85
85
|
/**
|
@@ -87,7 +87,7 @@ function popup(config) {
|
|
87
87
|
*/
|
88
88
|
if (server)
|
89
89
|
return `http://localhost:${PORT}`;
|
90
|
-
PORT = yield get_port_1.default({ host: 'localhost', port: typeof preferredPort == 'number' ? [preferredPort, 7000, 7001, 7002] : [7000, 7001, 7002] });
|
90
|
+
PORT = yield (0, get_port_1.default)({ host: 'localhost', port: typeof preferredPort == 'number' ? [preferredPort, 7000, 7001, 7002] : [7000, 7001, 7002] });
|
91
91
|
server = http_1.default.createServer(app);
|
92
92
|
if (!(config === null || config === void 0 ? void 0 : config.qrPopUpOnly)) {
|
93
93
|
io = new socket_io_1.Server(server);
|
@@ -105,12 +105,12 @@ function popup(config) {
|
|
105
105
|
});
|
106
106
|
});
|
107
107
|
server.listen(PORT);
|
108
|
-
const os = os_name_1.default();
|
108
|
+
const os = (0, os_name_1.default)();
|
109
109
|
const appName = os.includes('macOS') ? 'google chrome' : os.includes('Windows') ? 'chrome' : 'google-chrome';
|
110
|
-
const hasChrome = yield command_exists_1.default(appName).then(() => true).catch(() => false);
|
110
|
+
const hasChrome = yield (0, command_exists_1.default)(appName).then(() => true).catch(() => false);
|
111
111
|
if (hasChrome) {
|
112
112
|
if (!(config === null || config === void 0 ? void 0 : config.inDocker))
|
113
|
-
yield open_1.default(`http://localhost:${PORT}${(config === null || config === void 0 ? void 0 : config.qrPopUpOnly) ? `/qr` : ``}`, { app: {
|
113
|
+
yield (0, open_1.default)(`http://localhost:${PORT}${(config === null || config === void 0 ? void 0 : config.qrPopUpOnly) ? `/qr` : ``}`, { app: {
|
114
114
|
name: (config === null || config === void 0 ? void 0 : config.executablePath) || appName,
|
115
115
|
arguments: ['--incognito']
|
116
116
|
}, allowNonzeroExitCode: true }).catch(() => { return; });
|
package/dist/index.d.ts
CHANGED
@@ -4,6 +4,7 @@ export { create } from './controllers/initializer';
|
|
4
4
|
export * from '@open-wa/wa-decrypt';
|
5
5
|
export { ev, Spin } from './controllers/events';
|
6
6
|
export * from './utils/tools';
|
7
|
+
export * from './logging/logging';
|
7
8
|
export * from './structures/preProcessors';
|
8
9
|
export * from './connect';
|
9
10
|
export * from './build/build-postman';
|
package/dist/index.js
CHANGED
@@ -20,6 +20,7 @@ var events_1 = require("./controllers/events");
|
|
20
20
|
Object.defineProperty(exports, "ev", { enumerable: true, get: function () { return events_1.ev; } });
|
21
21
|
Object.defineProperty(exports, "Spin", { enumerable: true, get: function () { return events_1.Spin; } });
|
22
22
|
__exportStar(require("./utils/tools"), exports);
|
23
|
+
__exportStar(require("./logging/logging"), exports);
|
23
24
|
__exportStar(require("./structures/preProcessors"), exports);
|
24
25
|
__exportStar(require("./connect"), exports);
|
25
26
|
//dont need to export this
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import TransportStream from 'winston-transport';
|
2
|
+
export declare class LogToEvTransport extends TransportStream {
|
3
|
+
constructor(opts?: any);
|
4
|
+
log(info: any, callback: any): any;
|
5
|
+
}
|
6
|
+
export declare class NoOpTransport extends TransportStream {
|
7
|
+
constructor(opts?: any);
|
8
|
+
log(info: any, callback: any): any;
|
9
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.NoOpTransport = exports.LogToEvTransport = void 0;
|
7
|
+
const winston_transport_1 = __importDefault(require("winston-transport"));
|
8
|
+
const events_1 = require("../controllers/events");
|
9
|
+
class LogToEvTransport extends winston_transport_1.default {
|
10
|
+
constructor(opts) {
|
11
|
+
super(opts);
|
12
|
+
}
|
13
|
+
log(info, callback) {
|
14
|
+
setImmediate(() => {
|
15
|
+
this.emit('logged', info);
|
16
|
+
});
|
17
|
+
events_1.ev.emit(`DEBUG.${info.level}`, Object.keys(info).reduce((p, c) => (p = Object.assign(Object.assign({}, p), { [c]: info[c] })), {}));
|
18
|
+
if (callback)
|
19
|
+
return callback(null, true);
|
20
|
+
}
|
21
|
+
}
|
22
|
+
exports.LogToEvTransport = LogToEvTransport;
|
23
|
+
class NoOpTransport extends winston_transport_1.default {
|
24
|
+
constructor(opts) {
|
25
|
+
super(opts);
|
26
|
+
}
|
27
|
+
log(info, callback) {
|
28
|
+
setImmediate(() => {
|
29
|
+
this.emit('logged', info);
|
30
|
+
});
|
31
|
+
if (callback)
|
32
|
+
return callback(null, true);
|
33
|
+
}
|
34
|
+
}
|
35
|
+
exports.NoOpTransport = NoOpTransport;
|