Replace all async reads and writes to the JSON database with the sync reads and writes to prevent a data corruption race condition where two requests are processed at the same time and write to the same file, or one reads while the other writes causing read of partially written data.
19 lines
703 B
TypeScript
19 lines
703 B
TypeScript
/*
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
import { readNextSessionId, readNextUserId, readSchedule, readSessions, readSubscriptions, readUsers } from "~/server/database";
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
await requireServerSessionWithAdmin(event);
|
|
setHeader(event, "Content-Disposition", 'attachment; filename="database-dump.json"');
|
|
setHeader(event, "Content-Type", "application/json; charset=utf-8");
|
|
return {
|
|
nextUserId: readNextUserId(),
|
|
users: readUsers(),
|
|
nextSessionId: readNextSessionId(),
|
|
sessions: readSessions(),
|
|
subscriptions: readSubscriptions(),
|
|
schedule: readSchedule(),
|
|
};
|
|
})
|