feat(reliability): WhatsApp Web version caching (#1847)
* feat: use specific whatsapp web version * add constants to version updater * configurable caches * rename * typings * rm extraneous export * missing from the rename * rm version resolver * match readme version * use try-catch * only persist if cache miss
This commit is contained in:
parent
915763a8d0
commit
8ae0c0d484
1 changed files with 197 additions and 9 deletions
43
src/webCache/LocalWebCache.js
Normal file
43
src/webCache/LocalWebCache.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
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;
|
||||
Loading…
Reference in a new issue