Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store RemoteAuth ZIP files inside dataPath #3375

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/authStrategies/LocalAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const path = require('path');
const fs = require('fs');
const BaseAuthStrategy = require('./BaseAuthStrategy');
const { isRunningInAwsLambda } = require('../util/Util');

/**
* Local directory-based authentication
Expand All @@ -19,7 +20,7 @@ class LocalAuth extends BaseAuthStrategy {
throw new Error('Invalid clientId. Only alphanumeric characters, underscores and hyphens are allowed.');
}

this.dataPath = path.resolve(dataPath || './.wwebjs_auth/');
this.dataPath = path.resolve(dataPath || isRunningInAwsLambda() ? '/tmp/wwebjs_auth' : './.wwebjs_auth');
this.clientId = clientId;
}

Expand Down
9 changes: 5 additions & 4 deletions src/authStrategies/RemoteAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ try {

const path = require('path');
const { Events } = require('./../util/Constants');
const { isRunningInAwsLambda } = require('./../util/Util');
const BaseAuthStrategy = require('./BaseAuthStrategy');

/**
Expand Down Expand Up @@ -40,7 +41,7 @@ class RemoteAuth extends BaseAuthStrategy {
this.store = store;
this.clientId = clientId;
this.backupSyncIntervalMs = backupSyncIntervalMs;
this.dataPath = path.resolve(dataPath || './.wwebjs_auth/');
this.dataPath = path.resolve(dataPath || isRunningInAwsLambda() ? '/tmp/wwebjs_auth' : './.wwebjs_auth');
this.tempDir = `${this.dataPath}/wwebjs_temp_session_${this.clientId}`;
this.requiredDirs = ['Default', 'IndexedDB', 'Local Storage']; /* => Required Files & Dirs in WWebJS to restore session */
}
Expand Down Expand Up @@ -104,7 +105,7 @@ class RemoteAuth extends BaseAuthStrategy {
if (pathExists) {
await this.compressSession();
await this.store.save({session: this.sessionName});
await fs.promises.unlink(`${this.sessionName}.zip`);
await fs.promises.unlink(isRunningInAwsLambda() ? `/tmp/${this.sessionName}.zip` : `${this.sessionName}.zip`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just include one 'remoteSessionPath' that works like dataPath ?

The function 'isRunningInAwsLambda' wont go to production, convert it to an option or make the remoteSessionZipPath to fix your problems

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tuyuribr I think I have solved the issue without adding another option. It would store ZIP files inside the dataPath instead of the current working directory.

const client = new Client({
  authStrategy: new RemoteAuth({
    store: ... , // store
    // use /tmp, and since ZIP files will be stored inside dataPath, it resolves the issue
    dataPath: '/tmp/wwebjs_auth',
    backupSyncIntervalMs: 120000,
  }),
  ... // other config
});

await fs.promises.rm(`${this.tempDir}`, {
recursive: true,
force: true
Expand All @@ -115,7 +116,7 @@ class RemoteAuth extends BaseAuthStrategy {

async extractRemoteSession() {
const pathExists = await this.isValidPath(this.userDataDir);
const compressedSessionPath = `${this.sessionName}.zip`;
const compressedSessionPath = isRunningInAwsLambda ? `/tmp/${this.sessionName}.zip` : `${this.sessionName}.zip`;
const sessionExists = await this.store.sessionExists({session: this.sessionName});
if (pathExists) {
await fs.promises.rm(this.userDataDir, {
Expand All @@ -138,7 +139,7 @@ class RemoteAuth extends BaseAuthStrategy {

async compressSession() {
const archive = archiver('zip');
const stream = fs.createWriteStream(`${this.sessionName}.zip`);
const stream = fs.createWriteStream(isRunningInAwsLambda() ? `/tmp/${this.sessionName}.zip` : `${this.sessionName}.zip`);

await fs.copy(this.userDataDir, this.tempDir).catch(() => {});
await this.deleteMetadata();
Expand Down
8 changes: 8 additions & 0 deletions src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ class Util {
static setFfmpegPath(path) {
ffmpeg.setFfmpegPath(path);
}

/**
* Checks if running inside AWS Lambda using environment variables
* @returns {boolean}
*/
static isRunningInAwsLambda() {
return process.env.AWS_LAMBDA_FUNCTION_NAME || process.env.AWS_EXECUTION_ENV;
}
}

module.exports = Util;
4 changes: 2 additions & 2 deletions src/webCache/LocalWebCache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
const fs = require('fs');

const { isRunningInAwsLambda } = require('../util/Util');
const { WebCache, VersionResolveError } = require('./WebCache');

/**
Expand All @@ -13,7 +13,7 @@ class LocalWebCache extends WebCache {
constructor(options = {}) {
super();

this.path = options.path || './.wwebjs_cache/';
this.path = options.path || isRunningInAwsLambda() ? '/tmp/wwebjs_cache' : './.wwebjs_cache';
this.strict = options.strict || false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/webCache/WebCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ class VersionResolveError extends Error { }
module.exports = {
WebCache,
VersionResolveError
};
};