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

View File

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

View File

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