mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-18 03:59:38 +00:00
* 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>
56 lines
1.0 KiB
JavaScript
56 lines
1.0 KiB
JavaScript
'use strict';
|
|
const fs = require('fs');
|
|
|
|
class NodeCache {
|
|
constructor() {
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {string} key
|
|
* @returns {Promise<ArrayBuffer>}
|
|
*/
|
|
async read(key) {
|
|
return (await fs.promises.readFile(key)).buffer;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {string} key
|
|
* @param {ArrayBuffer} data
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async write(key, data) {
|
|
// Make sure the directory exists
|
|
const parts = key.split('/').slice(0, -1);
|
|
let current = '';
|
|
for (let i = 0; i < parts.length; i++) {
|
|
current += `${parts[i]}/`;
|
|
if (!(await this.exists(current))) {
|
|
await fs.promises.mkdir(current);
|
|
}
|
|
}
|
|
|
|
return await fs.promises.writeFile(key, data);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {string} key
|
|
* @returns {Promise<boolean>}
|
|
*/
|
|
async exists(key) {
|
|
return await fs.promises.stat(key).then(() => true).catch(() => false);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {string} key
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async remove(key) {
|
|
return await fs.promises.rm(key);
|
|
}
|
|
}
|
|
|
|
module.exports = new NodeCache(); |