unleash-server
Version:
Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.
46 lines • 1.3 kB
JavaScript
import { join } from 'path';
import { promises } from 'fs';
import { safeName } from '../helpers.js';
const { writeFile, readFile } = promises;
export class FileStorageProvider {
constructor(backupPath) {
if (!backupPath) {
throw new Error('backup Path is required');
}
this.backupPath = backupPath;
}
getPath(key) {
return join(this.backupPath, `/unleash-backup-${safeName(key)}.json`);
}
async set(key, data) {
return writeFile(this.getPath(key), JSON.stringify(data));
}
async get(key) {
const path = this.getPath(key);
let data;
try {
data = await readFile(path, 'utf8');
}
catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
else {
return undefined;
}
}
if (!data || data.trim().length === 0) {
return undefined;
}
try {
return JSON.parse(data);
}
catch (error) {
if (error instanceof Error) {
error.message = `Unleash storage failed parsing file ${path}: ${error.message}`;
}
throw error;
}
}
}
//# sourceMappingURL=storage-provider.js.map