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

Conversation

vedantmgoyal9
Copy link

@vedantmgoyal9 vedantmgoyal9 commented Nov 14, 2024

PR Details

Fixes read-only filesystem error on AWS Lambda.

Description

When deploying on AWS Lambda, the filesystem is read-only, with /tmp only being writable. This causes error while using LocalAuth or RemoteAuth strategy. This PR fixes it by detecting the runtime environment is AWS Lambda, and use /tmp/wwebjs_auth and /tmp/wwebjs_cache directories, instead of ./.wwebjs_auth and ./.wwebjs_cache.

Although, we could partially fix the issue by using the below config, there is no way to tell the library, a custom path for where to store ZIP files when using RemoteAuth strategy.

const client = new Client({
  authStrategy: new RemoteAuth({
    store: ... , // store
    dataPath: '/tmp/wwebjs_auth', // <- use /tmp
    backupSyncIntervalMs: 120000,
  }),
  webVersionCache: {
    type: 'local',
    path: '/tmp/wwebjs_cache', // <- use /tmp
  },
  puppeteer: ... // puppeteer config
});

Another possible way to fix this issue is to create RemoteAuth ZIP files inside wwebjs_auth directory itself, instead of current working directory. This could be a better way to handle this issue instead of adding environment-specific workarounds to the library itself.

image

Related Issue(s)

N/A

Motivation and Context

Enables to run whatsapp-web.js on AWS Lambda with zero-config.

How Has This Been Tested

Tested manually.

Environment

Types of changes

  • Dependency change
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • [N/A] I have updated the documentation accordingly (index.d.ts).
  • [N/A] I have updated the usage example accordingly (example.js)

@msgadi
Copy link

msgadi commented Nov 16, 2024

@vedantmgoyal9 Storing session or authentication-related data in the /tmp ephemeral storage of AWS Lambda is not recommended. Since the data in /tmp is specific to an instance of a Lambda execution environment, it is not guaranteed to persist across multiple invocations. This could lead to inconsistencies or loss of critical data.

A more reliable approach is to leverage AWS S3 for storing such data. S3 provides persistent, scalable, and secure storage, making it an excellent choice for managing session or authentication-related information. You can explore an example implementation using S3 for authentication here: Remote Auth Strategy Using S3.

@vedantmgoyal9
Copy link
Author

Yes, I know that. I store sessions in Firebase Storage, but when "restoring" the session, the library writes to current working directory which is read-only. I have patched it to use /tmp while downloading and decompressing the ZIP file so it does not fail.

@@ -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
});

@tuyuribr tuyuribr added the waiting for a response Waiting for a response from the author of an issue or a PR label Nov 22, 2024
@vedantmgoyal9 vedantmgoyal9 changed the title Use /tmp when running inside AWS Lambda Store RemoteAuth ZIP files inside dataPath Nov 22, 2024
@alechkos alechkos removed the waiting for a response Waiting for a response from the author of an issue or a PR label Nov 22, 2024
@vedantmgoyal9
Copy link
Author

hello @tuyuribr, any updates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants