/
streamlive
/
embedstream
Обзор
Документация
Войти
/
streamlive
/
embedstream
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
lib/store.js
69 строк
1 KB
TwixoffStudio
Create: package.json, .gitkeep, parser.js, store.js, embed.ejs, error.ejs, index.ejs, .env.example, .gitignore, LICENSE, README.md, server.js
10 авг 2026, 11:06
Верифицирован
10 авг 2026, 11:06
6adfd59
Код
Авторство
О чём код?
/** * Simple persistent store for embeds (JSON file) * For production you can swap to Redis / SQLite / Postgres. */ const fs = require('fs'); const path = require('path'); const { v4: uuidv4 } = require('uuid'); const DATA_DIR = path.join(__dirname, '..', 'data'); const STORE_FILE = path.join(DATA_DIR, 'embeds.json'); if (!fs.existsSync(DATA_DIR)) { fs.mkdirSync(DATA_DIR, { recursive: true }); } function load() { try { if (fs.existsSync(STORE_FILE)) { const raw = fs.readFileSync(STORE_FILE, 'utf8'); return JSON.parse(raw); } } catch (e) { console.error('Store load error:', e.message); } return {}; } function save(data) { try { fs.writeFileSync(STORE_FILE, JSON.stringify(data, null, 2), 'utf8'); } catch (e) { console.error('Store save error:', e.message); } } let cache = load(); function create(meta) { // short readable id const id = uuidv4().replace(/-/g, '').slice(0, 10); const record = { id, ...meta, createdAt: new Date().toISOString(), hits: 0, }; cache[id] = record; save(cache); return record; } function get(id) { const rec = cache[id]; if (rec) { rec.hits = (rec.hits || 0) + 1; // async save later to not block setImmediate(() => save(cache)); } return rec || null; } function list(limit = 50) { return Object.values(cache) .sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt)) .slice(0, limit); } module.exports = { create, get, list };