sokko-g/website/src/features/toolboxes/storage/modules/shared.js
Shinuwa fe21c2d899 Give the toolbox files headers that explain their role, callers and pitfalls
Document the storage chain (optimistic state, write queue, compare-and-write
transactions, cross-tab sync), the tool registry, actions and pages, the
contract a tool receives through its context, and the encrypted sharing
whose key never reaches the server. The compactor registry header no longer
calls itself strict: a missing compactor writes data as-is, now tracked in
sokko-g-afbw.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 10:14:46 +01:00

76 lines
2.1 KiB
JavaScript

/**
* @file Primitives communes de normalisation du stockage des toolboxes : identifiants préfixés par type (`uid`),
* entiers, dates et contrôle des images acceptées.
*
* Utilisé par : tous les normalisateurs de storage/modules/, l'import et l'export texte, la visionneuse d'images.
*/
// Formats bitmap uniquement : un SVG ou un document HTML exécuterait ses scripts sur l'origine du site une fois ouvert.
const SAFE_IMAGE_DATA_URL = /^data:image\/(?:jpeg|jpg|png|webp|gif|avif);base64,[a-z0-9+/]+=*$/i;
export function isSafeImageDataUrl(value) {
return typeof value === "string" && SAFE_IMAGE_DATA_URL.test(value);
}
export const ID_PREFIXES = {
tbx: "t",
space: "sp",
mod: "m",
item: "i",
section: "g",
image: "s",
link: "l",
counter: "c",
combo: "o",
calc: "r",
calendar: "h",
marker: "k",
timer: "z",
task: "a",
goal: "g",
relation: "e",
stroke: "d",
page: "p",
memo: "w",
equipmentType: "y",
equipment: "q",
trait: "v",
material: "n",
socketItem: "j",
socketLink: "u",
socketBonus: "b",
randomizer: "x",
rng: "f"
};
const ID_ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789";
function randomToken(length = 6) {
const bytes = new Uint8Array(length);
if (globalThis.crypto?.getRandomValues) {
globalThis.crypto.getRandomValues(bytes);
return [...bytes].map((byte) => ID_ALPHABET[byte % ID_ALPHABET.length]).join("");
}
return Math.random().toString(36).slice(2, 2 + length).padEnd(length, "0");
}
export function uid(prefix) {
return `${ID_PREFIXES[prefix] || "x"}${randomToken()}`;
}
export function parsePositiveInt(value, fallback = 1) {
const parsed = Number.parseInt(value, 10);
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
}
export function normalizeIsoDate(value) {
const text = String(value || "").trim();
if (!text) return "";
const timestamp = Date.parse(text);
return Number.isFinite(timestamp) ? new Date(timestamp).toISOString() : "";
}
export function normalizeTimestamp(value) {
const parsed = Number(value);
return Number.isFinite(parsed) && parsed > 0 ? parsed : 0;
}