Files
psu-schedule/server/schedules.js
2026-10-16 22:08:45 +03:00

35 lines
1.1 KiB
JavaScript

import { readFile, writeFile, mkdir } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { parseWord, PARSER_VERSION } from "./parser.js";
import { request } from "./source.js";
const data = fileURLToPath(new URL("../.data/parsed/", import.meta.url));
/** A parsed catalog document, from the disk cache while the file and parser are unchanged. */
export async function scheduleFor(file, { refresh = false } = {}) {
const cache = data + file.id + ".json";
if (!refresh)
try {
const cached = JSON.parse(await readFile(cache, "utf8"));
if (
cached.schemaVersion === PARSER_VERSION &&
cached.source.modified === file.modified
)
return cached;
} catch {}
const buffer = await request(file.path);
const parsed = await parseWord(buffer, {
name: file.name,
url: file.url,
faculty: file.faculty,
modified: file.modified,
fileId: file.id,
form: file.form,
course: file.course,
year: file.year,
});
await mkdir(data, { recursive: true });
await writeFile(cache, JSON.stringify(parsed));
return parsed;
}