Merge Tag v0.2.22

This commit is contained in:
2025-06-13 21:57:27 +02:00
12 changed files with 400 additions and 72 deletions

View File

@@ -21,13 +21,14 @@ import {
WishlistData,
WishlistItemType
} from '@/lib/types';
import { d2t, getNow, uuid } from '@/lib/utils';
import { d2t, generateCryptoHash, getNow, prepareDataForHashing, uuid } from '@/lib/utils';
import { signInSchema } from '@/lib/zod';
import fs from 'fs/promises';
import _ from 'lodash';
import path from 'path';
type ResourceType = 'habit' | 'wishlist' | 'coins'
type ActionType = 'write' | 'interact'
@@ -119,6 +120,33 @@ async function saveData<T>(type: DataType, data: T): Promise<void> {
}
}
/**
* Calculates the server's global freshness token based on all core data files.
* This is an expensive operation as it reads all data files.
*/
async function calculateServerFreshnessToken(): Promise<string> {
try {
const settings = await loadSettings();
const habits = await loadHabitsData();
const coins = await loadCoinsData();
const wishlist = await loadWishlistData();
const users = await loadUsersData();
const dataString = prepareDataForHashing(
settings,
habits,
coins,
wishlist,
users
);
const serverToken = await generateCryptoHash(dataString);
return serverToken;
} catch (error) {
console.error("Error calculating server freshness token:", error);
throw error;
}
}
// Wishlist specific functions
export async function loadWishlistData(): Promise<WishlistData> {
const user = await getCurrentUser()
@@ -591,3 +619,24 @@ export async function loadServerSettings(): Promise<ServerSettings> {
isDemo: !!process.env.DEMO,
}
}
/**
* Checks if the client's data is fresh by comparing its token with the server's token.
* @param clientToken The freshness token calculated by the client.
* @returns A promise that resolves to an object { isFresh: boolean }.
*/
export async function checkDataFreshness(clientToken: string): Promise<{ isFresh: boolean }> {
try {
const serverToken = await calculateServerFreshnessToken();
const isFresh = clientToken === serverToken;
if (!isFresh) {
console.log(`Data freshness check: Stale. Client token: ${clientToken}, Server token: ${serverToken}`);
}
return { isFresh };
} catch (error) {
console.error("Error in checkDataFreshness:", error);
// If server fails to determine its token, assume client might be stale to be safe,
// or handle error reporting differently.
return { isFresh: false };
}
}