BREAKING CHANGE(db): replace StorageManager and CacheDb with a unified smartdata-backed database layer
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
import type { StorageManager } from '../storage/classes.storagemanager.js';
|
||||
import type { IRemoteIngress, IDcRouterRouteConfig } from '../../ts_interfaces/data/remoteingress.js';
|
||||
|
||||
const STORAGE_PREFIX = '/remote-ingress/';
|
||||
import { RemoteIngressEdgeDoc } from '../db/index.js';
|
||||
|
||||
/**
|
||||
* Flatten a port range (number | number[] | Array<{from, to}>) to a sorted unique number array.
|
||||
@@ -27,33 +25,40 @@ function extractPorts(portRange: number | Array<number | { from: number; to: num
|
||||
|
||||
/**
|
||||
* Manages CRUD for remote ingress edge registrations.
|
||||
* Persists edge configs via StorageManager and provides
|
||||
* Persists edge configs via smartdata document classes and provides
|
||||
* the allowed edges list for the Rust hub.
|
||||
*/
|
||||
export class RemoteIngressManager {
|
||||
private storageManager: StorageManager;
|
||||
private edges: Map<string, IRemoteIngress> = new Map();
|
||||
private routes: IDcRouterRouteConfig[] = [];
|
||||
|
||||
constructor(storageManager: StorageManager) {
|
||||
this.storageManager = storageManager;
|
||||
constructor() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Load all edge registrations from storage into memory.
|
||||
* Load all edge registrations from the database into memory.
|
||||
*/
|
||||
public async initialize(): Promise<void> {
|
||||
const keys = await this.storageManager.list(STORAGE_PREFIX);
|
||||
for (const key of keys) {
|
||||
const edge = await this.storageManager.getJSON<IRemoteIngress>(key);
|
||||
if (edge) {
|
||||
// Migration: old edges without autoDerivePorts default to true
|
||||
if ((edge as any).autoDerivePorts === undefined) {
|
||||
edge.autoDerivePorts = true;
|
||||
await this.storageManager.setJSON(key, edge);
|
||||
}
|
||||
this.edges.set(edge.id, edge);
|
||||
const docs = await RemoteIngressEdgeDoc.findAll();
|
||||
for (const doc of docs) {
|
||||
// Migration: old edges without autoDerivePorts default to true
|
||||
if ((doc as any).autoDerivePorts === undefined) {
|
||||
doc.autoDerivePorts = true;
|
||||
await doc.save();
|
||||
}
|
||||
const edge: IRemoteIngress = {
|
||||
id: doc.id,
|
||||
name: doc.name,
|
||||
secret: doc.secret,
|
||||
listenPorts: doc.listenPorts,
|
||||
listenPortsUdp: doc.listenPortsUdp,
|
||||
enabled: doc.enabled,
|
||||
autoDerivePorts: doc.autoDerivePorts,
|
||||
tags: doc.tags,
|
||||
createdAt: doc.createdAt,
|
||||
updatedAt: doc.updatedAt,
|
||||
};
|
||||
this.edges.set(edge.id, edge);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,7 +194,9 @@ export class RemoteIngressManager {
|
||||
updatedAt: now,
|
||||
};
|
||||
|
||||
await this.storageManager.setJSON(`${STORAGE_PREFIX}${id}`, edge);
|
||||
const doc = new RemoteIngressEdgeDoc();
|
||||
Object.assign(doc, edge);
|
||||
await doc.save();
|
||||
this.edges.set(id, edge);
|
||||
return edge;
|
||||
}
|
||||
@@ -233,7 +240,11 @@ export class RemoteIngressManager {
|
||||
if (updates.tags !== undefined) edge.tags = updates.tags;
|
||||
edge.updatedAt = Date.now();
|
||||
|
||||
await this.storageManager.setJSON(`${STORAGE_PREFIX}${id}`, edge);
|
||||
const doc = await RemoteIngressEdgeDoc.findById(id);
|
||||
if (doc) {
|
||||
Object.assign(doc, edge);
|
||||
await doc.save();
|
||||
}
|
||||
this.edges.set(id, edge);
|
||||
return edge;
|
||||
}
|
||||
@@ -245,7 +256,10 @@ export class RemoteIngressManager {
|
||||
if (!this.edges.has(id)) {
|
||||
return false;
|
||||
}
|
||||
await this.storageManager.delete(`${STORAGE_PREFIX}${id}`);
|
||||
const doc = await RemoteIngressEdgeDoc.findById(id);
|
||||
if (doc) {
|
||||
await doc.delete();
|
||||
}
|
||||
this.edges.delete(id);
|
||||
return true;
|
||||
}
|
||||
@@ -262,7 +276,11 @@ export class RemoteIngressManager {
|
||||
edge.secret = plugins.crypto.randomBytes(32).toString('hex');
|
||||
edge.updatedAt = Date.now();
|
||||
|
||||
await this.storageManager.setJSON(`${STORAGE_PREFIX}${id}`, edge);
|
||||
const doc = await RemoteIngressEdgeDoc.findById(id);
|
||||
if (doc) {
|
||||
Object.assign(doc, edge);
|
||||
await doc.save();
|
||||
}
|
||||
this.edges.set(id, edge);
|
||||
return edge.secret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user