esa-cli
Version:
A CLI for operating Alibaba Cloud ESA Functions and Pages.
51 lines (50 loc) • 1.63 kB
JavaScript
import fs from 'fs';
import path from 'path';
import t from '../../../i18n/index.js';
import { getRoot } from '../../../utils/fileUtils/base.js';
class EdgeKV {
constructor() {
const root = getRoot();
const kvPath = path.join(root, 'kv.json');
if (fs.existsSync(kvPath)) {
try {
const kvJson = fs.readFileSync(kvPath, 'utf8');
const kvJsonObj = JSON.parse(kvJson);
Object.keys(kvJsonObj).forEach((namespace) => {
const childMap = new Map();
Object.keys(kvJsonObj[namespace]).forEach((key) => {
childMap.set(key, JSON.stringify(kvJsonObj[namespace][key]));
});
EdgeKV.store.set(namespace, childMap);
});
}
catch (err) {
console.log(t('kv_parse_failed').d('kv.json parse failed, use empty local kv store.'));
}
}
}
get(key, namespace) {
const store = EdgeKV.store.get(namespace);
if (!store || !store.has(key)) {
return;
}
return store.get(key);
}
put(key, value, namespace) {
let store = EdgeKV.store.get(namespace);
if (!store) {
EdgeKV.store.set(namespace, new Map([[key, value]]));
}
else {
store.set(key, value);
}
}
delete(key, namespace) {
const store = EdgeKV.store.get(namespace);
if (!store)
return false;
return store.delete(key);
}
}
EdgeKV.store = new Map();
export default EdgeKV;