src/cache/FileProvider.js
import path from 'path';
import readFile from '../readFile';
import writeFile from '../writeFile';
/**
* FileProvider stores a given string in the file on Linux file system.
*/
export default class FileProvider {
constructor({
folder = '/tmp'
} = {}) {
this.folder = folder;
}
/*
* Get path to the file based on unique hash that is generated by Cache class based on JavaScript
* string, query parameters and options passed to Krtek. Files will be stored in folder that is
* passed to the constructor of FileProvider.
*/
getFilePath(hash) {
return path.resolve(
this.folder,
`krtek-${hash}.cache`
);
}
/**
* Cache given string in a file and call callback function when done.
*/
set(hash, code) {
const file = this.getFilePath(hash);
return writeFile(file, code);
}
/**
* Read content of the cached file.
*/
get(hash) {
const file = this.getFilePath(hash);
return readFile(file);
}
}