From 8e5324786e5b0067498717d3827937dac0f1edb6 Mon Sep 17 00:00:00 2001 From: Vedant <83997633+vedantmgoyal9@users.noreply.github.com> Date: Thu, 14 Nov 2024 00:05:17 +0530 Subject: [PATCH 1/5] Use `/tmp` when running inside AWS Lambda --- src/authStrategies/LocalAuth.js | 3 ++- src/authStrategies/RemoteAuth.js | 9 +++++---- src/util/Util.js | 8 ++++++++ src/webCache/LocalWebCache.js | 4 ++-- src/webCache/WebCache.js | 3 +++ 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/authStrategies/LocalAuth.js b/src/authStrategies/LocalAuth.js index 543a6b9ba1..ec69a1889f 100644 --- a/src/authStrategies/LocalAuth.js +++ b/src/authStrategies/LocalAuth.js @@ -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 @@ -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; } diff --git a/src/authStrategies/RemoteAuth.js b/src/authStrategies/RemoteAuth.js index 99e688ba2f..7f952c28b8 100644 --- a/src/authStrategies/RemoteAuth.js +++ b/src/authStrategies/RemoteAuth.js @@ -13,6 +13,7 @@ try { const path = require('path'); const { Events } = require('./../util/Constants'); +const { isRunningInAwsLambda } = require('./../util/Util'); const BaseAuthStrategy = require('./BaseAuthStrategy'); /** @@ -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 */ } @@ -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`); await fs.promises.rm(`${this.tempDir}`, { recursive: true, force: true @@ -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, { @@ -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(); diff --git a/src/util/Util.js b/src/util/Util.js index 39a8a01e09..ae6d9e10ce 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -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; diff --git a/src/webCache/LocalWebCache.js b/src/webCache/LocalWebCache.js index 9c429683f8..52548d5bf1 100644 --- a/src/webCache/LocalWebCache.js +++ b/src/webCache/LocalWebCache.js @@ -1,6 +1,6 @@ const path = require('path'); const fs = require('fs'); - +const { isRunningInAwsLambda } = require('../util/Util'); const { WebCache, VersionResolveError } = require('./WebCache'); /** @@ -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; } diff --git a/src/webCache/WebCache.js b/src/webCache/WebCache.js index 4460218325..fed8aa604e 100644 --- a/src/webCache/WebCache.js +++ b/src/webCache/WebCache.js @@ -4,6 +4,9 @@ class WebCache { async resolve() { return null; } async persist() { } + isRunningInAwsLambda() { + return process.env.AWS_LAMBDA_FUNCTION_NAME || process.env.AWS_EXECUTION_ENV; + } } class VersionResolveError extends Error { } From ac8227d5da654a837961f0e20fb8dba2a0f8847f Mon Sep 17 00:00:00 2001 From: Vedant Mohan Goyal <83997633+vedantmgoyal9@users.noreply.github.com> Date: Thu, 14 Nov 2024 14:01:22 +0530 Subject: [PATCH 2/5] Revert unintended change accidentally committed --- src/webCache/WebCache.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/webCache/WebCache.js b/src/webCache/WebCache.js index fed8aa604e..d9a9ac44df 100644 --- a/src/webCache/WebCache.js +++ b/src/webCache/WebCache.js @@ -4,9 +4,6 @@ class WebCache { async resolve() { return null; } async persist() { } - isRunningInAwsLambda() { - return process.env.AWS_LAMBDA_FUNCTION_NAME || process.env.AWS_EXECUTION_ENV; - } } class VersionResolveError extends Error { } @@ -14,4 +11,4 @@ class VersionResolveError extends Error { } module.exports = { WebCache, VersionResolveError -}; \ No newline at end of file +}; From 24c03d6d97f01c427f976609d280b2b43648118e Mon Sep 17 00:00:00 2001 From: Vedant Mohan Goyal <83997633+vedantmgoyal9@users.noreply.github.com> Date: Fri, 22 Nov 2024 22:16:29 +0000 Subject: [PATCH 3/5] Revert "Use `/tmp` when running inside AWS Lambda" This reverts commit 8e5324786e5b0067498717d3827937dac0f1edb6. --- src/authStrategies/LocalAuth.js | 3 +-- src/authStrategies/RemoteAuth.js | 9 ++++----- src/util/Util.js | 8 -------- src/webCache/LocalWebCache.js | 4 ++-- 4 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/authStrategies/LocalAuth.js b/src/authStrategies/LocalAuth.js index ec69a1889f..543a6b9ba1 100644 --- a/src/authStrategies/LocalAuth.js +++ b/src/authStrategies/LocalAuth.js @@ -3,7 +3,6 @@ const path = require('path'); const fs = require('fs'); const BaseAuthStrategy = require('./BaseAuthStrategy'); -const { isRunningInAwsLambda } = require('../util/Util'); /** * Local directory-based authentication @@ -20,7 +19,7 @@ class LocalAuth extends BaseAuthStrategy { throw new Error('Invalid clientId. Only alphanumeric characters, underscores and hyphens are allowed.'); } - this.dataPath = path.resolve(dataPath || isRunningInAwsLambda() ? '/tmp/wwebjs_auth' : './.wwebjs_auth'); + this.dataPath = path.resolve(dataPath || './.wwebjs_auth/'); this.clientId = clientId; } diff --git a/src/authStrategies/RemoteAuth.js b/src/authStrategies/RemoteAuth.js index 7f952c28b8..99e688ba2f 100644 --- a/src/authStrategies/RemoteAuth.js +++ b/src/authStrategies/RemoteAuth.js @@ -13,7 +13,6 @@ try { const path = require('path'); const { Events } = require('./../util/Constants'); -const { isRunningInAwsLambda } = require('./../util/Util'); const BaseAuthStrategy = require('./BaseAuthStrategy'); /** @@ -41,7 +40,7 @@ class RemoteAuth extends BaseAuthStrategy { this.store = store; this.clientId = clientId; this.backupSyncIntervalMs = backupSyncIntervalMs; - this.dataPath = path.resolve(dataPath || isRunningInAwsLambda() ? '/tmp/wwebjs_auth' : './.wwebjs_auth'); + this.dataPath = path.resolve(dataPath || './.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 */ } @@ -105,7 +104,7 @@ class RemoteAuth extends BaseAuthStrategy { if (pathExists) { await this.compressSession(); await this.store.save({session: this.sessionName}); - await fs.promises.unlink(isRunningInAwsLambda() ? `/tmp/${this.sessionName}.zip` : `${this.sessionName}.zip`); + await fs.promises.unlink(`${this.sessionName}.zip`); await fs.promises.rm(`${this.tempDir}`, { recursive: true, force: true @@ -116,7 +115,7 @@ class RemoteAuth extends BaseAuthStrategy { async extractRemoteSession() { const pathExists = await this.isValidPath(this.userDataDir); - const compressedSessionPath = isRunningInAwsLambda ? `/tmp/${this.sessionName}.zip` : `${this.sessionName}.zip`; + const compressedSessionPath = `${this.sessionName}.zip`; const sessionExists = await this.store.sessionExists({session: this.sessionName}); if (pathExists) { await fs.promises.rm(this.userDataDir, { @@ -139,7 +138,7 @@ class RemoteAuth extends BaseAuthStrategy { async compressSession() { const archive = archiver('zip'); - const stream = fs.createWriteStream(isRunningInAwsLambda() ? `/tmp/${this.sessionName}.zip` : `${this.sessionName}.zip`); + const stream = fs.createWriteStream(`${this.sessionName}.zip`); await fs.copy(this.userDataDir, this.tempDir).catch(() => {}); await this.deleteMetadata(); diff --git a/src/util/Util.js b/src/util/Util.js index ae6d9e10ce..39a8a01e09 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -181,14 +181,6 @@ 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; diff --git a/src/webCache/LocalWebCache.js b/src/webCache/LocalWebCache.js index 52548d5bf1..9c429683f8 100644 --- a/src/webCache/LocalWebCache.js +++ b/src/webCache/LocalWebCache.js @@ -1,6 +1,6 @@ const path = require('path'); const fs = require('fs'); -const { isRunningInAwsLambda } = require('../util/Util'); + const { WebCache, VersionResolveError } = require('./WebCache'); /** @@ -13,7 +13,7 @@ class LocalWebCache extends WebCache { constructor(options = {}) { super(); - this.path = options.path || isRunningInAwsLambda() ? '/tmp/wwebjs_cache' : './.wwebjs_cache'; + this.path = options.path || './.wwebjs_cache/'; this.strict = options.strict || false; } From b3cfe7dfe35168cdb13e6d64c5bdfb02aeed59ea Mon Sep 17 00:00:00 2001 From: Vedant Mohan Goyal <83997633+vedantmgoyal9@users.noreply.github.com> Date: Fri, 22 Nov 2024 22:36:12 +0000 Subject: [PATCH 4/5] Store RemoteAuth ZIP files inside dataPath --- src/authStrategies/RemoteAuth.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/authStrategies/RemoteAuth.js b/src/authStrategies/RemoteAuth.js index 99e688ba2f..7d06f15586 100644 --- a/src/authStrategies/RemoteAuth.js +++ b/src/authStrategies/RemoteAuth.js @@ -103,8 +103,8 @@ class RemoteAuth extends BaseAuthStrategy { const pathExists = await this.isValidPath(this.userDataDir); if (pathExists) { await this.compressSession(); - await this.store.save({session: this.sessionName}); - await fs.promises.unlink(`${this.sessionName}.zip`); + await this.store.save({session: path.join(this.dataPath, this.sessionName)}); + await fs.promises.unlink(path.join(this.dataPath, `${this.sessionName}.zip`)); await fs.promises.rm(`${this.tempDir}`, { recursive: true, force: true @@ -115,7 +115,7 @@ class RemoteAuth extends BaseAuthStrategy { async extractRemoteSession() { const pathExists = await this.isValidPath(this.userDataDir); - const compressedSessionPath = `${this.sessionName}.zip`; + const compressedSessionPath = path.join(this.dataPath, `${this.sessionName}.zip`); const sessionExists = await this.store.sessionExists({session: this.sessionName}); if (pathExists) { await fs.promises.rm(this.userDataDir, { @@ -138,7 +138,7 @@ class RemoteAuth extends BaseAuthStrategy { async compressSession() { const archive = archiver('zip'); - const stream = fs.createWriteStream(`${this.sessionName}.zip`); + const stream = fs.createWriteStream(path.join(this.dataPath, `${this.sessionName}.zip`)); await fs.copy(this.userDataDir, this.tempDir).catch(() => {}); await this.deleteMetadata(); From a7e1e16b0309129944c1b428a08a8daedd7c448c Mon Sep 17 00:00:00 2001 From: Vedant Mohan Goyal <83997633+vedantmgoyal9@users.noreply.github.com> Date: Sat, 23 Nov 2024 04:08:29 +0530 Subject: [PATCH 5/5] Discard changes to src/webCache/WebCache.js --- src/webCache/WebCache.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webCache/WebCache.js b/src/webCache/WebCache.js index d9a9ac44df..4460218325 100644 --- a/src/webCache/WebCache.js +++ b/src/webCache/WebCache.js @@ -11,4 +11,4 @@ class VersionResolveError extends Error { } module.exports = { WebCache, VersionResolveError -}; +}; \ No newline at end of file