session worker compatibility

This commit is contained in:
Davidson Gomes 2024-06-03 19:06:00 -03:00
parent dd123c6a99
commit 52230edc5c
3 changed files with 31 additions and 19 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "evolution-api", "name": "evolution-api",
"version": "1.8.0", "version": "1.8.1",
"description": "Rest api for communication with WhatsApp", "description": "Rest api for communication with WhatsApp",
"main": "./dist/src/main.js", "main": "./dist/src/main.js",
"scripts": { "scripts": {
@ -49,7 +49,7 @@
"amqplib": "^0.10.3", "amqplib": "^0.10.3",
"@aws-sdk/client-sqs": "^3.569.0", "@aws-sdk/client-sqs": "^3.569.0",
"axios": "^1.6.5", "axios": "^1.6.5",
"@whiskeysockets/baileys": "6.7.2", "@whiskeysockets/baileys": "6.7.4",
"class-validator": "^0.14.1", "class-validator": "^0.14.1",
"compression": "^1.7.4", "compression": "^1.7.4",
"cors": "^2.8.5", "cors": "^2.8.5",

View File

@ -1,7 +1,7 @@
import axios from 'axios'; import axios from 'axios';
import { execSync } from 'child_process'; import { execSync } from 'child_process';
import { ConfigService, ProviderSession } from '../../config/env.config'; import { Auth, ConfigService, ProviderSession } from '../../config/env.config';
import { Logger } from '../../config/logger.config'; import { Logger } from '../../config/logger.config';
type ResponseSuccess = { status: number; data?: any }; type ResponseSuccess = { status: number; data?: any };
@ -10,11 +10,13 @@ type ResponseProvider = Promise<[ResponseSuccess?, Error?]>;
export class ProviderFiles { export class ProviderFiles {
constructor(private readonly configService: ConfigService) { constructor(private readonly configService: ConfigService) {
this.baseUrl = `http://${this.config.HOST}:${this.config.PORT}/session/${this.config.PREFIX}`; this.baseUrl = `http://${this.config.HOST}:${this.config.PORT}/session/${this.config.PREFIX}`;
this.globalApiToken = this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY;
} }
private readonly logger = new Logger(ProviderFiles.name); private readonly logger = new Logger(ProviderFiles.name);
private baseUrl: string; private baseUrl: string;
private globalApiToken: string;
private readonly config = Object.freeze(this.configService.get<ProviderSession>('PROVIDER')); private readonly config = Object.freeze(this.configService.get<ProviderSession>('PROVIDER'));
@ -24,14 +26,14 @@ export class ProviderFiles {
public async onModuleInit() { public async onModuleInit() {
if (this.config.ENABLED) { if (this.config.ENABLED) {
const client = axios.create({ const url = `http://${this.config.HOST}:${this.config.PORT}`;
baseURL: this.baseUrl,
});
try { try {
const response = await client.options('/ping'); const response = await axios.options(url + '/ping');
if (!response?.data?.pong) { if (response?.data != 'pong') {
throw new Error('Offline file provider.'); throw new Error('Offline file provider.');
} }
await axios.post(`${url}/session`, { group: this.config.PREFIX }, { headers: { apikey: this.globalApiToken } });
} catch (error) { } catch (error) {
this.logger.error(['Failed to connect to the file server', error?.message, error?.stack]); this.logger.error(['Failed to connect to the file server', error?.message, error?.stack]);
const pid = process.pid; const pid = process.pid;
@ -46,9 +48,13 @@ export class ProviderFiles {
public async create(instance: string): ResponseProvider { public async create(instance: string): ResponseProvider {
try { try {
const response = await axios.post(`${this.baseUrl}`, { const response = await axios.post(
instance, `${this.baseUrl}`,
}); {
instance,
},
{ headers: { apikey: this.globalApiToken } },
);
return [{ status: response.status, data: response?.data }]; return [{ status: response.status, data: response?.data }];
} catch (error) { } catch (error) {
return [ return [
@ -63,7 +69,9 @@ export class ProviderFiles {
public async write(instance: string, key: string, data: any): ResponseProvider { public async write(instance: string, key: string, data: any): ResponseProvider {
try { try {
const response = await axios.post(`${this.baseUrl}/${instance}/${key}`, data); const response = await axios.post(`${this.baseUrl}/${instance}/${key}`, data, {
headers: { apikey: this.globalApiToken },
});
return [{ status: response.status, data: response?.data }]; return [{ status: response.status, data: response?.data }];
} catch (error) { } catch (error) {
return [ return [
@ -78,7 +86,9 @@ export class ProviderFiles {
public async read(instance: string, key: string): ResponseProvider { public async read(instance: string, key: string): ResponseProvider {
try { try {
const response = await axios.get(`${this.baseUrl}/${instance}/${key}`); const response = await axios.get(`${this.baseUrl}/${instance}/${key}`, {
headers: { apikey: this.globalApiToken },
});
return [{ status: response.status, data: response?.data }]; return [{ status: response.status, data: response?.data }];
} catch (error) { } catch (error) {
return [ return [
@ -93,7 +103,9 @@ export class ProviderFiles {
public async delete(instance: string, key: string): ResponseProvider { public async delete(instance: string, key: string): ResponseProvider {
try { try {
const response = await axios.delete(`${this.baseUrl}/${instance}/${key}`); const response = await axios.delete(`${this.baseUrl}/${instance}/${key}`, {
headers: { apikey: this.globalApiToken },
});
return [{ status: response.status, data: response?.data }]; return [{ status: response.status, data: response?.data }];
} catch (error) { } catch (error) {
return [ return [
@ -108,7 +120,7 @@ export class ProviderFiles {
public async allInstances(): ResponseProvider { public async allInstances(): ResponseProvider {
try { try {
const response = await axios.get(`${this.baseUrl}/list-instances`); const response = await axios.get(`${this.baseUrl}/list-instances`, { headers: { apikey: this.globalApiToken } });
return [{ status: response.status, data: response?.data as string[] }]; return [{ status: response.status, data: response?.data as string[] }];
} catch (error) { } catch (error) {
return [ return [
@ -123,7 +135,7 @@ export class ProviderFiles {
public async removeSession(instance: string): ResponseProvider { public async removeSession(instance: string): ResponseProvider {
try { try {
const response = await axios.delete(`${this.baseUrl}/${instance}`); const response = await axios.delete(`${this.baseUrl}/${instance}`, { headers: { apikey: this.globalApiToken } });
return [{ status: response.status, data: response?.data }]; return [{ status: response.status, data: response?.data }];
} catch (error) { } catch (error) {
return [ return [

View File

@ -67,7 +67,7 @@ export class AuthStateProvider {
data: json, data: json,
}); });
if (error) { if (error) {
this.logger.error(['writeData', error?.message, error?.stack]); // this.logger.error(['writeData', error?.message, error?.stack]);
return; return;
} }
return response; return response;
@ -76,7 +76,7 @@ export class AuthStateProvider {
const readData = async (key: string): Promise<any> => { const readData = async (key: string): Promise<any> => {
const [response, error] = await this.providerFiles.read(instance, key); const [response, error] = await this.providerFiles.read(instance, key);
if (error) { if (error) {
this.logger.error(['readData', error?.message, error?.stack]); // this.logger.error(['readData', error?.message, error?.stack]);
return; return;
} }
if (isNotEmpty(response?.data)) { if (isNotEmpty(response?.data)) {
@ -87,7 +87,7 @@ export class AuthStateProvider {
const removeData = async (key: string) => { const removeData = async (key: string) => {
const [response, error] = await this.providerFiles.delete(instance, key); const [response, error] = await this.providerFiles.delete(instance, key);
if (error) { if (error) {
this.logger.error(['removeData', error?.message, error?.stack]); // this.logger.error(['removeData', error?.message, error?.stack]);
return; return;
} }