Files
YouTube.js/lib/utils/wrappers/BrowserCache.js
Bob Varioa f52d15cdb0 Make project multiplatform (#91)
* Prefer `c ? x : y` over `c && x || y`

* Avoid unnecessary asssignment expressions

* Prefer switch statements over object lookup tables

* Add an .editorconfig

* Fix style issues

* Fix mentioned issues

* remove dynamic require

* Introduce esbuild as a build system

* Add cross platform stream api

* Replace 'fs' with custom cache api

* Add cross platform crypto api

* Add misc. dependencies

* Create multi-platform tests

* Update package-lock, Add build files

* Pull from upstream

* Fix linting issues, and update build files

* Fix comments issues

* Regenerate types, add source maps

Co-authored-by: bob <bob.varioa@gmail.com>
2022-07-06 16:47:48 -03:00

53 lines
953 B
JavaScript

'use strict';
const { openDB } = require('idb');
class BrowserCache {
constructor() {
this._db = openDB('yt-cache', 1, {
upgrade(db) {
db.createObjectStore('cache');
}
});
}
/**
*
* @param {string} key
* @returns {Promise<ArrayBuffer>}
*/
async read(key) {
const data = await (await this._db).get('cache', key);
return data;
}
/**
*
* @param {string} key
* @param {ArrayBuffer} data
* @returns {Promise<void>}
*/
async write(key, data) {
await (await this._db).put('cache', data, key);
}
/**
*
* @param {string} key
* @returns {Promise<boolean>}
*/
async exists(key) {
const data = await (await this._db).get('cache', key);
return data !== undefined;
}
/**
*
* @param {string} key
* @returns {Promise<void>}
*/
async remove(key) {
await (await this._db).delete('cache', key);
}
}
module.exports = new BrowserCache();