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.
20 lines
691 B
TypeScript
20 lines
691 B
TypeScript
/*
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
import { readSubscriptions, writeSubscriptions } from "~/server/database";
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const session = await requireServerSessionWithUser(event);
|
|
const subscriptions = readSubscriptions();
|
|
const existingIndex = subscriptions.findIndex(
|
|
sub => sub.type === "push" && sub.sessionId === session.id
|
|
);
|
|
if (existingIndex !== -1) {
|
|
subscriptions.splice(existingIndex, 1);
|
|
} else {
|
|
return { message: "No subscription registered."};
|
|
}
|
|
writeSubscriptions(subscriptions);
|
|
return { message: "Existing subscription removed."};
|
|
});
|