Spaces:
Build error
Build error
File size: 1,350 Bytes
5858652 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import { randomUUID } from "node:crypto";
import { config } from "../config.js";
import { ensureJsonFile, readJson, writeJson } from "./fs.js";
export class ProxyStore {
constructor() {
this.routes = [];
}
async init() {
await ensureJsonFile(config.proxyStateFile, []);
this.routes = await readJson(config.proxyStateFile, []);
}
list() {
return [...this.routes].sort((a, b) => a.pathPrefix.localeCompare(b.pathPrefix));
}
match(requestPath) {
const sorted = [...this.routes].sort((a, b) => b.pathPrefix.length - a.pathPrefix.length);
return sorted.find((route) => requestPath === route.pathPrefix || requestPath.startsWith(route.pathPrefix + "/")) || null;
}
async add(route) {
const payload = {
id: randomUUID(),
pathPrefix: route.pathPrefix,
target: route.target,
projectId: route.projectId || null,
createdAt: new Date().toISOString()
};
this.routes.push(payload);
await this.save();
return payload;
}
async remove(routeId) {
const before = this.routes.length;
this.routes = this.routes.filter((route) => route.id !== routeId);
const changed = this.routes.length !== before;
if (changed) {
await this.save();
}
return changed;
}
async save() {
await writeJson(config.proxyStateFile, this.routes);
}
}
|