Nouveautés dans l'app, et « Tout effacer » synchronisé : cet appareil ou partout (2d)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
0ea0ea109f
commit
8ebdca0c30
@@ -120,6 +120,8 @@ export function createApp(options) {
|
||||
const body = await readJson(req, RELAY_BODY_BYTES);
|
||||
return sendJson(res, 200, await relay.append(id, token, body));
|
||||
}
|
||||
// « Tout effacer partout » : le journal est vidé, les autres appareils l'apprennent.
|
||||
if (req.method === "DELETE") return sendJson(res, 200, await relay.erase(id, token));
|
||||
throw new HttpError(405, "method_not_allowed");
|
||||
}
|
||||
throw new HttpError(404, "not_found", "Adresse inconnue.");
|
||||
|
||||
+41
-5
@@ -5,8 +5,11 @@
|
||||
// <DATA_DIR>/journals/<journalId>.jsonl
|
||||
// ligne 1 : {"v":1,"auth":"<SHA-256 hexadécimal de la clé d'accès>"}
|
||||
// puis : {"seq":1,"blob":"<bloc base64url>"}, {"seq":2,…}, …
|
||||
// Le fichier ne fait que grandir (ajout seulement). Aucune date, aucune adresse :
|
||||
// la date de modification du fichier sert seule à repérer l'inactivité.
|
||||
// Le fichier ne fait que grandir (ajout seulement), sauf effacement demandé par
|
||||
// l'utilisateur (« Tout effacer partout ») : il ne garde alors que son en-tête, marqué
|
||||
// "erased":true, pour que les autres appareils l'apprennent (410) au lieu de recréer le
|
||||
// journal en renvoyant leurs données. Aucune date, aucune adresse : la date de
|
||||
// modification du fichier sert seule à repérer l'inactivité.
|
||||
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
@@ -39,6 +42,7 @@ const hashToken = (token) => crypto.createHash("sha256").update(token).digest();
|
||||
* @property {number} last dernier numéro d'ordre
|
||||
* @property {number} size taille du fichier
|
||||
* @property {number} touched dernière mise à jour de la date du fichier (lecture), en ms
|
||||
* @property {boolean} [erased] effacé par l'utilisateur : plus ni dépôt ni lecture
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -105,6 +109,7 @@ export function createRelay(dataDir, options = {}) {
|
||||
last: lines.length - 2,
|
||||
size: Buffer.byteLength(text),
|
||||
touched: 0,
|
||||
...(header.erased ? { erased: true } : {}),
|
||||
};
|
||||
if (metas.size > 10_000) metas.clear();
|
||||
metas.set(id, meta);
|
||||
@@ -121,6 +126,11 @@ export function createRelay(dataDir, options = {}) {
|
||||
throw new HttpError(403, "forbidden", "Clé d'accès refusée pour ce journal.");
|
||||
}
|
||||
|
||||
/** @param {JournalMeta} meta */
|
||||
function checkNotErased(meta) {
|
||||
if (meta.erased) throw new HttpError(410, "erased", "Ce journal a été effacé depuis un de tes appareils.");
|
||||
}
|
||||
|
||||
/** @param {string} id */
|
||||
function checkId(id) {
|
||||
if (!JOURNAL_ID.test(id)) throw new HttpError(404, "unknown_journal", "Journal inconnu.");
|
||||
@@ -164,8 +174,10 @@ export function createRelay(dataDir, options = {}) {
|
||||
return serialized(id, async () => {
|
||||
let meta = await load(id);
|
||||
let header = "";
|
||||
if (meta) checkToken(meta, token);
|
||||
else {
|
||||
if (meta) {
|
||||
checkToken(meta, token);
|
||||
checkNotErased(meta);
|
||||
} else {
|
||||
await fs.mkdir(dir, { recursive: true });
|
||||
header = JSON.stringify({ v: 1, auth: hashToken(token).toString("hex") }) + "\n";
|
||||
}
|
||||
@@ -206,6 +218,7 @@ export function createRelay(dataDir, options = {}) {
|
||||
const meta = await load(id);
|
||||
if (!meta) throw new HttpError(404, "unknown_journal", "Journal inconnu : aucun dépôt n'a encore été fait avec cette clé.");
|
||||
checkToken(meta, token);
|
||||
checkNotErased(meta);
|
||||
await touch(id, meta);
|
||||
/** @type {{seq: number, blob: string}[]} */
|
||||
const events = [];
|
||||
@@ -220,6 +233,29 @@ export function createRelay(dataDir, options = {}) {
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Efface le journal (« Tout effacer partout ») : les blocs disparaissent, l'en-tête
|
||||
* reste, marqué effacé. Effacer deux fois ne change rien.
|
||||
* @param {string} id
|
||||
* @param {string} token
|
||||
* @returns {Promise<{erased: true}>}
|
||||
*/
|
||||
erase(id, token) {
|
||||
checkId(id);
|
||||
if (!token) throw new HttpError(401, "unauthorized", "Clé d'accès manquante.");
|
||||
return serialized(id, async () => {
|
||||
const meta = await load(id);
|
||||
if (!meta) throw new HttpError(404, "unknown_journal", "Journal inconnu.");
|
||||
checkToken(meta, token);
|
||||
const header = JSON.stringify({ v: 1, auth: meta.auth.toString("hex"), erased: true }) + "\n";
|
||||
const tmp = fileOf(id) + ".tmp";
|
||||
await fs.writeFile(tmp, header);
|
||||
await fs.rename(tmp, fileOf(id));
|
||||
Object.assign(meta, { last: 0, size: Buffer.byteLength(header), erased: true, touched: now() });
|
||||
return { erased: true };
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Le journal existe-t-il, et cette clé d'accès est-elle la sienne ?
|
||||
* @param {string} id
|
||||
@@ -228,7 +264,7 @@ export function createRelay(dataDir, options = {}) {
|
||||
async owns(id, token) {
|
||||
if (!JOURNAL_ID.test(id) || !token) return false;
|
||||
const meta = await serialized(id, () => load(id));
|
||||
return !!meta && crypto.timingSafeEqual(meta.auth, hashToken(token));
|
||||
return !!meta && !meta.erased && crypto.timingSafeEqual(meta.auth, hashToken(token));
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user