/
s.homyakov
/
dev2harness
Обзор
Документация
Войти
/
s.homyakov
/
dev2harness
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
src/storage.js
95 строк
3 KB
Sergey Homyakov
Harden user state and sandbox flows
27 май 2026, 09:28
27 май 2026, 09:28
1813063
Код
Авторство
О чём код?
import { seedComments, seedHelpRequests } from "./courseData.js"; const KEY = "opencode-harness-state-v1"; const defaultState = { currentUser: null, users: { guest: { username: "guest", displayName: "Гость", goal: "Собрать безопасный workflow для работы с OpenCode", role: "Начинающий agent builder", progress: {}, streak: 1, createdAt: new Date().toISOString() } }, comments: seedComments, helpRequests: seedHelpRequests, feedback: [ { id: "f1", type: "idea", text: "Добавить больше интерактивных проверок для opencode.json и permissions.", status: "planned", votes: 6 } ] }; export function loadState() { try { const stored = localStorage.getItem(KEY); if (!stored) return defaultState; return normalizeState(JSON.parse(stored)); } catch { return defaultState; } } export function saveState(state) { localStorage.setItem(KEY, JSON.stringify(state)); } export function exportState(state) { return JSON.stringify(state, null, 2); } export function importState(raw) { const parsed = JSON.parse(raw); return normalizeState(parsed); } export function makeId(prefix) { return `${prefix}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 7)}`; } function normalizeState(candidate) { if (!candidate || typeof candidate !== "object" || Array.isArray(candidate)) { throw new Error("State must be an object."); } if ( candidate.users !== undefined && (!candidate.users || typeof candidate.users !== "object" || Array.isArray(candidate.users)) ) { throw new Error("State users must be an object."); } const users = { ...defaultState.users, ...(candidate.users || {}) }; for (const [username, user] of Object.entries(users)) { if (!user || typeof user !== "object" || Array.isArray(user)) { throw new Error(`Invalid user: ${username}`); } users[username] = { ...user, username: user.username || username, progress: user.progress && typeof user.progress === "object" ? user.progress : {} }; } const currentUser = users[candidate.currentUser] ? candidate.currentUser : defaultState.currentUser; return { ...defaultState, ...candidate, currentUser, users, comments: Array.isArray(candidate.comments) ? candidate.comments : defaultState.comments, helpRequests: Array.isArray(candidate.helpRequests) ? candidate.helpRequests : defaultState.helpRequests, feedback: Array.isArray(candidate.feedback) ? candidate.feedback : defaultState.feedback }; }