chore(v1.34.7): bump version and generate docs
This commit is contained in:
parent
211205363c
commit
f935b50011
77 changed files with 5207 additions and 2669 deletions
212
docs/authStrategies_RemoteAuth.js.html
generated
212
docs/authStrategies_RemoteAuth.js.html
generated
|
|
@ -4,7 +4,7 @@
|
|||
<head>
|
||||
<meta name="generator" content="JSDoc 3.6.11">
|
||||
<meta charset="utf-8">
|
||||
<title>whatsapp-web.js 1.34.6 » Source: authStrategies/RemoteAuth.js</title>
|
||||
<title>whatsapp-web.js 1.34.7 » Source: authStrategies/RemoteAuth.js</title>
|
||||
<link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Karla:400,400i,700,700i" type="text/css">
|
||||
<link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Noto+Serif:400,400i,700,700i" type="text/css">
|
||||
<link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Inconsolata:500" type="text/css">
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
<nav id="jsdoc-navbar" role="navigation" class="jsdoc-navbar">
|
||||
<div id="jsdoc-navbar-container">
|
||||
<div id="jsdoc-navbar-content">
|
||||
<a href="index.html" class="jsdoc-navbar-package-name">whatsapp-web.<wbr>js 1.<wbr>34.<wbr>6</a>
|
||||
<a href="index.html" class="jsdoc-navbar-package-name">whatsapp-web.<wbr>js 1.<wbr>34.<wbr>7</a>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
|
@ -51,40 +51,64 @@ const BaseAuthStrategy = require('./BaseAuthStrategy');
|
|||
* @param {object} options - options
|
||||
* @param {object} options.store - Remote database store instance
|
||||
* @param {string} options.clientId - Client id to distinguish instances if you are using multiple, otherwise keep null if you are using only one instance
|
||||
* @param {string} options.dataPath - Change the default path for saving session files, default is: "./.wwebjs_auth/"
|
||||
* @param {string} options.dataPath - Change the default path for saving session files, default is: "./.wwebjs_auth/"
|
||||
* @param {number} options.backupSyncIntervalMs - Sets the time interval for periodic session backups. Accepts values starting from 60000ms {1 minute}
|
||||
* @param {number} options.rmMaxRetries - Sets the maximum number of retries for removing the session directory
|
||||
*/
|
||||
class RemoteAuth extends BaseAuthStrategy {
|
||||
constructor({ clientId, dataPath, store, backupSyncIntervalMs, rmMaxRetries } = {}) {
|
||||
if (!fs &amp;&amp; !unzipper &amp;&amp; !archiver) throw new Error('Optional Dependencies [fs-extra, unzipper, archiver] are required to use RemoteAuth. Make sure to run npm install correctly and remove the --no-optional flag');
|
||||
constructor({
|
||||
clientId,
|
||||
dataPath,
|
||||
store,
|
||||
backupSyncIntervalMs,
|
||||
rmMaxRetries,
|
||||
} = {}) {
|
||||
if (!fs &amp;&amp; !unzipper &amp;&amp; !archiver)
|
||||
throw new Error(
|
||||
'Optional Dependencies [fs-extra, unzipper, archiver] are required to use RemoteAuth. Make sure to run npm install correctly and remove the --no-optional flag',
|
||||
);
|
||||
super();
|
||||
|
||||
const idRegex = /^[-_\w]+$/i;
|
||||
if (clientId &amp;&amp; !idRegex.test(clientId)) {
|
||||
throw new Error('Invalid clientId. Only alphanumeric characters, underscores and hyphens are allowed.');
|
||||
throw new Error(
|
||||
'Invalid clientId. Only alphanumeric characters, underscores and hyphens are allowed.',
|
||||
);
|
||||
}
|
||||
if (!backupSyncIntervalMs || backupSyncIntervalMs &lt; 60000) {
|
||||
throw new Error('Invalid backupSyncIntervalMs. Accepts values starting from 60000ms {1 minute}.');
|
||||
throw new Error(
|
||||
'Invalid backupSyncIntervalMs. Accepts values starting from 60000ms {1 minute}.',
|
||||
);
|
||||
}
|
||||
if(!store) throw new Error('Remote database store is required.');
|
||||
if (!store) throw new Error('Remote database store is required.');
|
||||
|
||||
this.store = store;
|
||||
this.clientId = clientId;
|
||||
this.backupSyncIntervalMs = backupSyncIntervalMs;
|
||||
this.dataPath = path.resolve(dataPath || './.wwebjs_auth/');
|
||||
this.tempDir = `${this.dataPath}/wwebjs_temp_session_${this.clientId}`;
|
||||
this.requiredDirs = ['Default', 'IndexedDB', 'Local Storage']; /* => Required Files &amp; Dirs in WWebJS to restore session */
|
||||
this.requiredDirs = [
|
||||
'Default',
|
||||
'IndexedDB',
|
||||
'Local Storage',
|
||||
]; /* => Required Files &amp; Dirs in WWebJS to restore session */
|
||||
this.rmMaxRetries = rmMaxRetries ?? 4;
|
||||
}
|
||||
|
||||
async beforeBrowserInitialized() {
|
||||
const puppeteerOpts = this.client.options.puppeteer;
|
||||
const sessionDirName = this.clientId ? `RemoteAuth-${this.clientId}` : 'RemoteAuth';
|
||||
const sessionDirName = this.clientId
|
||||
? `RemoteAuth-${this.clientId}`
|
||||
: 'RemoteAuth';
|
||||
const dirPath = path.join(this.dataPath, sessionDirName);
|
||||
|
||||
if (puppeteerOpts.userDataDir &amp;&amp; puppeteerOpts.userDataDir !== dirPath) {
|
||||
throw new Error('RemoteAuth is not compatible with a user-supplied userDataDir.');
|
||||
if (
|
||||
puppeteerOpts.userDataDir &amp;&amp;
|
||||
puppeteerOpts.userDataDir !== dirPath
|
||||
) {
|
||||
throw new Error(
|
||||
'RemoteAuth is not compatible with a user-supplied userDataDir.',
|
||||
);
|
||||
}
|
||||
|
||||
this.userDataDir = dirPath;
|
||||
|
|
@ -94,7 +118,7 @@ class RemoteAuth extends BaseAuthStrategy {
|
|||
|
||||
this.client.options.puppeteer = {
|
||||
...puppeteerOpts,
|
||||
userDataDir: dirPath
|
||||
userDataDir: dirPath,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -111,20 +135,26 @@ class RemoteAuth extends BaseAuthStrategy {
|
|||
|
||||
let pathExists = await this.isValidPath(this.userDataDir);
|
||||
if (pathExists) {
|
||||
await fs.promises.rm(this.userDataDir, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
maxRetries: this.rmMaxRetries,
|
||||
}).catch(() => {});
|
||||
await fs.promises
|
||||
.rm(this.userDataDir, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
maxRetries: this.rmMaxRetries,
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
clearInterval(this.backupSync);
|
||||
}
|
||||
|
||||
async afterAuthReady() {
|
||||
const sessionExists = await this.store.sessionExists({session: this.sessionName});
|
||||
if(!sessionExists) {
|
||||
await this.delay(60000); /* Initial delay sync required for session to be stable enough to recover */
|
||||
await this.storeRemoteSession({emit: true});
|
||||
const sessionExists = await this.store.sessionExists({
|
||||
session: this.sessionName,
|
||||
});
|
||||
if (!sessionExists) {
|
||||
await this.delay(
|
||||
60000,
|
||||
); /* Initial delay sync required for session to be stable enough to recover */
|
||||
await this.storeRemoteSession({ emit: true });
|
||||
}
|
||||
var self = this;
|
||||
this.backupSync = setInterval(async function () {
|
||||
|
|
@ -133,34 +163,57 @@ class RemoteAuth extends BaseAuthStrategy {
|
|||
}
|
||||
|
||||
async storeRemoteSession(options) {
|
||||
/* Compress &amp; Store Session */
|
||||
const pathExists = await this.isValidPath(this.userDataDir);
|
||||
if (pathExists) {
|
||||
await this.compressSession();
|
||||
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,
|
||||
maxRetries: this.rmMaxRetries,
|
||||
}).catch(() => {});
|
||||
if(options &amp;&amp; options.emit) this.client.emit(Events.REMOTE_SESSION_SAVED);
|
||||
if (!pathExists) return;
|
||||
|
||||
let compressedSessionPath;
|
||||
try {
|
||||
compressedSessionPath = await this.compressSession();
|
||||
await this.store.save({
|
||||
session: this.sessionName,
|
||||
});
|
||||
if (options &amp;&amp; options.emit)
|
||||
this.client.emit(Events.REMOTE_SESSION_SAVED);
|
||||
} finally {
|
||||
const paths = [
|
||||
this.tempDir,
|
||||
...(compressedSessionPath ? [compressedSessionPath] : []),
|
||||
];
|
||||
await Promise.allSettled(
|
||||
paths.map((p) =>
|
||||
fs.promises.rm(p, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
maxRetries: this.rmMaxRetries,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async extractRemoteSession() {
|
||||
const pathExists = await this.isValidPath(this.userDataDir);
|
||||
const compressedSessionPath = path.join(this.dataPath, `${this.sessionName}.zip`);
|
||||
const sessionExists = await this.store.sessionExists({session: this.sessionName});
|
||||
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, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
maxRetries: this.rmMaxRetries,
|
||||
}).catch(() => {});
|
||||
await fs.promises
|
||||
.rm(this.userDataDir, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
maxRetries: this.rmMaxRetries,
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
if (sessionExists) {
|
||||
await this.store.extract({session: this.sessionName, path: compressedSessionPath});
|
||||
await this.store.extract({
|
||||
session: this.sessionName,
|
||||
path: compressedSessionPath,
|
||||
});
|
||||
await this.unCompressSession(compressedSessionPath);
|
||||
} else {
|
||||
fs.mkdirSync(this.userDataDir, { recursive: true });
|
||||
|
|
@ -168,59 +221,62 @@ class RemoteAuth extends BaseAuthStrategy {
|
|||
}
|
||||
|
||||
async deleteRemoteSession() {
|
||||
const sessionExists = await this.store.sessionExists({session: this.sessionName});
|
||||
if (sessionExists) await this.store.delete({session: this.sessionName});
|
||||
const sessionExists = await this.store.sessionExists({
|
||||
session: this.sessionName,
|
||||
});
|
||||
if (sessionExists)
|
||||
await this.store.delete({ session: this.sessionName });
|
||||
}
|
||||
|
||||
async compressSession() {
|
||||
const stageDefaultPath = path.join(this.tempDir, 'Default');
|
||||
const userDataDefaultPath = path.join(this.userDataDir, 'Default');
|
||||
|
||||
await fs.emptyDir(stageDefaultPath);
|
||||
await this.copyByRequiredDirs(userDataDefaultPath, stageDefaultPath);
|
||||
|
||||
const archive = archiver('zip');
|
||||
const stream = fs.createWriteStream(path.join(this.dataPath, `${this.sessionName}.zip`));
|
||||
const outPath = path.join(this.dataPath, `${this.sessionName}.zip`);
|
||||
const out = fs.createWriteStream(outPath);
|
||||
|
||||
await fs.copy(this.userDataDir, this.tempDir).catch(() => {});
|
||||
await this.deleteMetadata();
|
||||
return new Promise((resolve, reject) => {
|
||||
archive
|
||||
.directory(this.tempDir, false)
|
||||
.on('error', err => reject(err))
|
||||
.pipe(stream);
|
||||
await new Promise((resolve, reject) => {
|
||||
out.once('close', resolve);
|
||||
out.once('error', reject);
|
||||
archive.once('error', reject);
|
||||
|
||||
stream.on('close', () => resolve());
|
||||
archive.pipe(out);
|
||||
archive.directory(this.tempDir, false);
|
||||
archive.finalize();
|
||||
});
|
||||
return outPath;
|
||||
}
|
||||
|
||||
async unCompressSession(compressedSessionPath) {
|
||||
var stream = fs.createReadStream(compressedSessionPath);
|
||||
await new Promise((resolve, reject) => {
|
||||
stream.pipe(unzipper.Extract({
|
||||
path: this.userDataDir,
|
||||
concurrency: 10
|
||||
}))
|
||||
.on('error', err => reject(err))
|
||||
stream
|
||||
.pipe(
|
||||
unzipper.Extract({
|
||||
path: this.userDataDir,
|
||||
concurrency: 10,
|
||||
}),
|
||||
)
|
||||
.on('error', (err) => reject(err))
|
||||
.on('finish', () => resolve());
|
||||
});
|
||||
await fs.promises.unlink(compressedSessionPath);
|
||||
}
|
||||
|
||||
async deleteMetadata() {
|
||||
const sessionDirs = [this.tempDir, path.join(this.tempDir, 'Default')];
|
||||
for (const dir of sessionDirs) {
|
||||
const sessionFiles = await fs.promises.readdir(dir);
|
||||
for (const element of sessionFiles) {
|
||||
if (!this.requiredDirs.includes(element)) {
|
||||
const dirElement = path.join(dir, element);
|
||||
const stats = await fs.promises.lstat(dirElement);
|
||||
|
||||
if (stats.isDirectory()) {
|
||||
await fs.promises.rm(dirElement, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
maxRetries: this.rmMaxRetries,
|
||||
}).catch(() => {});
|
||||
} else {
|
||||
await fs.promises.unlink(dirElement).catch(() => {});
|
||||
}
|
||||
}
|
||||
async copyByRequiredDirs(from, to) {
|
||||
for (const d of this.requiredDirs) {
|
||||
const src = path.join(from, d);
|
||||
if (await this.isValidPath(src)) {
|
||||
const dest = path.join(to, path.basename(src));
|
||||
await fs.promises.cp(src, dest, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
errorOnExist: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -235,7 +291,7 @@ class RemoteAuth extends BaseAuthStrategy {
|
|||
}
|
||||
|
||||
async delay(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -250,7 +306,7 @@ module.exports = RemoteAuth;
|
|||
<footer id="jsdoc-footer" class="jsdoc-footer">
|
||||
<div id="jsdoc-footer-container">
|
||||
<p>
|
||||
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc</a> 3.6.11 on January 30, 2026.
|
||||
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc</a> 3.6.11 on April 24, 2026.
|
||||
</p>
|
||||
</div>
|
||||
</footer>
|
||||
|
|
|
|||
Loading…
Reference in a new issue