whatsapp-web.js
A WhatsApp API client that connects through the WhatsApp Web browser app
It uses Puppeteer to run a real instance of Whatsapp Web to avoid getting blocked.
@@ -2055,6 +2055,33 @@ limitations under the License.diff --git a/docs/Base.html b/docs/Base.html index c8c8fdb..50d0054 100644 --- a/docs/Base.html +++ b/docs/Base.html @@ -4,7 +4,7 @@
-Source: Client.
Source: Client.
Starting point for interacting with the WhatsApp Web API
Determines how to save and restore sessions. Will use LegacySessionAuth if options.session is set. Otherwise, NoAuth will be used.
+webVersion
++
+
The version of WhatsApp Web to use. Use options.webVersionCache to configure how the version is retrieved.
+webVersionCache
++
+
Determines how to retrieve the WhatsApp Web version. Defaults to a local cache (LocalWebCache) that falls back to latest if the requested version is not found.
+authTimeoutMs
@@ -2769,7 +2797,7 @@ diff --git a/docs/Client.js.html b/docs/Client.js.html index 80f345b..7842891 100644 --- a/docs/Client.js.html +++ b/docs/Client.js.html @@ -4,7 +4,7 @@ -Source: webCache/
LocalWebCache - Fetches a WhatsApp Web version from a local file store
+| Name | +Type | +Optional | +Description | +||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
+ options + |
+
+ + |
+
+ + |
+
+ options +Values in
|
+
Source: webCache/
RemoteWebCache - Fetches a WhatsApp Web version index from a remote server
+| Name | +Type | +Optional | +Description | +||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
+ options + |
+
+ + |
+
+ + |
+
+ options +Values in
|
+
Source: webCache/
Default implementation of a web version cache that does nothing.
+A WhatsApp API client that connects through the WhatsApp Web browser app
It uses Puppeteer to run a real instance of Whatsapp Web to avoid getting blocked.
@@ -2055,6 +2055,33 @@ limitations under the License.const path = require('path');
+const fs = require('fs');
+
+const { WebCache, VersionResolveError } = require('./WebCache');
+
+/**
+ * LocalWebCache - Fetches a WhatsApp Web version from a local file store
+ * @param {object} options - options
+ * @param {string} options.path - Path to the directory where cached versions are saved, default is: "./.wwebjs_cache/"
+ * @param {boolean} options.strict - If true, will throw an error if the requested version can't be fetched. If false, will resolve to the latest version.
+ */
+class LocalWebCache extends WebCache {
+ constructor(options = {}) {
+ super();
+
+ this.path = options.path || './.wwebjs_cache/';
+ this.strict = options.strict || false;
+ }
+
+ async resolve(version) {
+ const filePath = path.join(this.path, `${version}.html`);
+
+ try {
+ return fs.readFileSync(filePath, 'utf-8');
+ }
+ catch (err) {
+ if (this.strict) throw new VersionResolveError(`Couldn't load version ${version} from the cache`);
+ return null;
+ }
+ }
+
+ async persist(indexHtml) {
+ // extract version from index (e.g. manifest-2.2206.9.json -> 2.2206.9)
+ const version = indexHtml.match(/manifest-([\d\\.]+)\.json/)[1];
+ if(!version) return;
+
+ const filePath = path.join(this.path, `${version}.html`);
+ fs.mkdirSync(this.path, { recursive: true });
+ fs.writeFileSync(filePath, indexHtml);
+ }
+}
+
+module.exports = LocalWebCache;
+ const fetch = require('node-fetch');
+const { WebCache, VersionResolveError } = require('./WebCache');
+
+/**
+ * RemoteWebCache - Fetches a WhatsApp Web version index from a remote server
+ * @param {object} options - options
+ * @param {string} options.remotePath - Endpoint that should be used to fetch the version index. Use {version} as a placeholder for the version number.
+ * @param {boolean} options.strict - If true, will throw an error if the requested version can't be fetched. If false, will resolve to the latest version. Defaults to false.
+ */
+class RemoteWebCache extends WebCache {
+ constructor(options = {}) {
+ super();
+
+ if (!options.remotePath) throw new Error('webVersionCache.remotePath is required when using the remote cache');
+ this.remotePath = options.remotePath;
+ this.strict = options.strict || false;
+ }
+
+ async resolve(version) {
+ const remotePath = this.remotePath.replace('{version}', version);
+
+ try {
+ const cachedRes = await fetch(remotePath);
+ if (cachedRes.ok) {
+ return cachedRes.text();
+ }
+ } catch (err) {
+ console.error(`Error fetching version ${version} from remote`, err);
+ }
+
+ if (this.strict) throw new VersionResolveError(`Couldn't load version ${version} from the archive`);
+ return null;
+ }
+
+ async persist() {
+ // Nothing to do here
+ }
+}
+
+module.exports = RemoteWebCache;
+ /**
+ * Default implementation of a web version cache that does nothing.
+ */
+class WebCache {
+ async resolve() { return null; }
+ async persist() { }
+}
+
+class VersionResolveError extends Error { }
+
+module.exports = {
+ WebCache,
+ VersionResolveError
+};
+