Rework as scripts-centric, upload-only, gated beta lifecycle
Scripts become first-class: named, versioned, uploaded via the UI, optionally scoped to a game or map. Any change that would land on scope-matched servers (marking-required or new version of a scoped script) must first pass a beta. The beta runs a per-server state machine — deploy -beta.gsc → wait empty → Komodo restart → wait healthy → optionally wait for a real match end (games_sp.log 'ExitLevel: executed') — before it's eligible for promotion. Rollback is always available and restores from .OLD. The shared/scripts mirror and its betas/ staging folder are gone. Every server now has a tracked script inventory (server_scripts table), populated only by explicit upload flows. New: /scripts, /scripts/[id], /betas/[id] UI; /api/scripts + /api/betas + /api/servers/[name]/scripts endpoints; scripts.ts, inventory.ts, beta.ts (rewritten), beta-runner.ts, match-log.ts, komodoInspectHealthy(). Removed: push.ts, /api/push, BetaPanel, legacy importer. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,235 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { db } from '../db/index.js';
|
||||
|
||||
export type ScopeType = 'game' | 'map';
|
||||
|
||||
export interface ScriptRow {
|
||||
id: number;
|
||||
name: string;
|
||||
target_rel_path: string;
|
||||
scope_type: ScopeType | null;
|
||||
scope_value: string | null;
|
||||
current_version_id: number | null;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface ScriptVersionRow {
|
||||
id: number;
|
||||
script_id: number;
|
||||
version: number;
|
||||
sha256: string;
|
||||
size: number;
|
||||
content: Buffer;
|
||||
notes: string | null;
|
||||
uploaded_at: string;
|
||||
}
|
||||
|
||||
// The script name is the primary user-facing identifier; slugify to derive a
|
||||
// URL-safe form. Same rules as the old beta slugify — lowercase, letters + digits.
|
||||
export function slugify(name: string): string {
|
||||
return name
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '');
|
||||
}
|
||||
|
||||
export class ScriptError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public readonly kind:
|
||||
| 'name-empty'
|
||||
| 'name-taken'
|
||||
| 'path-empty'
|
||||
| 'no-such-script'
|
||||
| 'no-such-version'
|
||||
| 'scope-requires-beta'
|
||||
| 'invalid-scope'
|
||||
| 'in-use-current'
|
||||
| 'in-use-beta'
|
||||
| 'has-scope'
|
||||
| 'has-active-beta'
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
function sha256(buf: Buffer): string {
|
||||
return createHash('sha256').update(buf).digest('hex');
|
||||
}
|
||||
|
||||
export function getScript(id: number): ScriptRow | undefined {
|
||||
return db().prepare('SELECT * FROM scripts WHERE id = ?').get(id) as ScriptRow | undefined;
|
||||
}
|
||||
|
||||
export function getScriptByName(name: string): ScriptRow | undefined {
|
||||
return db().prepare('SELECT * FROM scripts WHERE name = ?').get(name) as ScriptRow | undefined;
|
||||
}
|
||||
|
||||
export function listScripts(): ScriptRow[] {
|
||||
return db().prepare('SELECT * FROM scripts ORDER BY name').all() as ScriptRow[];
|
||||
}
|
||||
|
||||
export function listVersions(scriptId: number): ScriptVersionRow[] {
|
||||
return db()
|
||||
.prepare('SELECT * FROM script_versions WHERE script_id = ? ORDER BY version DESC')
|
||||
.all(scriptId) as ScriptVersionRow[];
|
||||
}
|
||||
|
||||
export function getVersion(id: number): ScriptVersionRow | undefined {
|
||||
return db().prepare('SELECT * FROM script_versions WHERE id = ?').get(id) as
|
||||
| ScriptVersionRow
|
||||
| undefined;
|
||||
}
|
||||
|
||||
// Latest version is 'max(version)' — used everywhere we want the freshest
|
||||
// upload, whether or not it has been promoted (current_version_id).
|
||||
export function getLatestVersion(scriptId: number): ScriptVersionRow | undefined {
|
||||
return db()
|
||||
.prepare('SELECT * FROM script_versions WHERE script_id = ? ORDER BY version DESC LIMIT 1')
|
||||
.get(scriptId) as ScriptVersionRow | undefined;
|
||||
}
|
||||
|
||||
export interface CreateScriptInput {
|
||||
name: string;
|
||||
targetRelPath: string;
|
||||
scopeType?: ScopeType | null;
|
||||
scopeValue?: string | null;
|
||||
content: Buffer;
|
||||
notes?: string;
|
||||
}
|
||||
|
||||
// Insert script + version 1 in one transaction. Scope may be set at creation
|
||||
// because the script has no current_version_id yet (nothing to gate).
|
||||
export function createScript(input: CreateScriptInput): { script: ScriptRow; version: ScriptVersionRow } {
|
||||
if (!input.name.trim()) throw new ScriptError('script name is required', 'name-empty');
|
||||
if (!input.targetRelPath.trim()) throw new ScriptError('target rel path is required', 'path-empty');
|
||||
validateScope(input.scopeType ?? null, input.scopeValue ?? null);
|
||||
if (getScriptByName(input.name)) throw new ScriptError(`script "${input.name}" already exists`, 'name-taken');
|
||||
|
||||
const sha = sha256(input.content);
|
||||
const size = input.content.byteLength;
|
||||
|
||||
const insertScript = db().prepare(
|
||||
`INSERT INTO scripts (name, target_rel_path, scope_type, scope_value)
|
||||
VALUES (?, ?, ?, ?)`
|
||||
);
|
||||
const insertVersion = db().prepare(
|
||||
`INSERT INTO script_versions (script_id, version, sha256, size, content, notes)
|
||||
VALUES (?, 1, ?, ?, ?, ?)`
|
||||
);
|
||||
|
||||
let scriptId!: number;
|
||||
let versionId!: number;
|
||||
db().transaction(() => {
|
||||
const s = insertScript.run(input.name, input.targetRelPath, input.scopeType ?? null, input.scopeValue ?? null);
|
||||
scriptId = Number(s.lastInsertRowid);
|
||||
const v = insertVersion.run(scriptId, sha, size, input.content, input.notes ?? null);
|
||||
versionId = Number(v.lastInsertRowid);
|
||||
})();
|
||||
|
||||
return {
|
||||
script: getScript(scriptId)!,
|
||||
version: getVersion(versionId)!
|
||||
};
|
||||
}
|
||||
|
||||
export function uploadNewVersion(scriptId: number, content: Buffer, notes?: string): ScriptVersionRow {
|
||||
const script = getScript(scriptId);
|
||||
if (!script) throw new ScriptError(`no such script (id=${scriptId})`, 'no-such-script');
|
||||
|
||||
const latest = getLatestVersion(scriptId);
|
||||
const nextVersion = (latest?.version ?? 0) + 1;
|
||||
const sha = sha256(content);
|
||||
|
||||
const res = db()
|
||||
.prepare(
|
||||
`INSERT INTO script_versions (script_id, version, sha256, size, content, notes)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`
|
||||
)
|
||||
.run(scriptId, nextVersion, sha, content.byteLength, content, notes ?? null);
|
||||
return getVersion(Number(res.lastInsertRowid))!;
|
||||
}
|
||||
|
||||
function validateScope(scopeType: ScopeType | null, scopeValue: string | null): void {
|
||||
if (scopeType === null && scopeValue === null) return;
|
||||
if (scopeType === 'game' || scopeType === 'map') {
|
||||
if (!scopeValue || !scopeValue.trim()) {
|
||||
throw new ScriptError(`scope_value required for scope_type=${scopeType}`, 'invalid-scope');
|
||||
}
|
||||
return;
|
||||
}
|
||||
throw new ScriptError(`unknown scope_type: ${scopeType}`, 'invalid-scope');
|
||||
}
|
||||
|
||||
export interface SetScopeInput {
|
||||
scriptId: number;
|
||||
scopeType: ScopeType | null;
|
||||
scopeValue: string | null;
|
||||
}
|
||||
|
||||
// Setting or changing a scope while the script already has a promoted version
|
||||
// requires a beta first (per plan §"Beta trigger"). Un-scoping is free.
|
||||
export function setScope(input: SetScopeInput): ScriptRow {
|
||||
const script = getScript(input.scriptId);
|
||||
if (!script) throw new ScriptError(`no such script (id=${input.scriptId})`, 'no-such-script');
|
||||
validateScope(input.scopeType, input.scopeValue);
|
||||
|
||||
const changed =
|
||||
input.scopeType !== script.scope_type || (input.scopeValue ?? null) !== (script.scope_value ?? null);
|
||||
const isSetOrChange = input.scopeType !== null && changed;
|
||||
|
||||
if (isSetOrChange && script.current_version_id !== null) {
|
||||
throw new ScriptError(
|
||||
'setting or changing scope requires opening a beta first',
|
||||
'scope-requires-beta'
|
||||
);
|
||||
}
|
||||
|
||||
db()
|
||||
.prepare('UPDATE scripts SET scope_type = ?, scope_value = ? WHERE id = ?')
|
||||
.run(input.scopeType, input.scopeValue, input.scriptId);
|
||||
return getScript(input.scriptId)!;
|
||||
}
|
||||
|
||||
// Version delete is metadata-only: it never touches server disks. But deleting
|
||||
// the current version or a version referenced by a running beta would break
|
||||
// downstream state, so those are blocked.
|
||||
export function deleteVersion(versionId: number): void {
|
||||
const version = getVersion(versionId);
|
||||
if (!version) throw new ScriptError(`no such version (id=${versionId})`, 'no-such-version');
|
||||
|
||||
const script = getScript(version.script_id)!;
|
||||
if (script.current_version_id === versionId) {
|
||||
throw new ScriptError('cannot delete the current version', 'in-use-current');
|
||||
}
|
||||
|
||||
const beta = db()
|
||||
.prepare(`SELECT id FROM betas WHERE script_version_id = ? AND state = 'running'`)
|
||||
.get(versionId) as { id: number } | undefined;
|
||||
if (beta) throw new ScriptError(`beta ${beta.id} is still running against this version`, 'in-use-beta');
|
||||
|
||||
db().prepare('DELETE FROM script_versions WHERE id = ?').run(versionId);
|
||||
}
|
||||
|
||||
export function deleteScript(scriptId: number): void {
|
||||
const script = getScript(scriptId);
|
||||
if (!script) throw new ScriptError(`no such script (id=${scriptId})`, 'no-such-script');
|
||||
if (script.scope_type !== null) {
|
||||
throw new ScriptError('un-set scope before deleting a script', 'has-scope');
|
||||
}
|
||||
const beta = db()
|
||||
.prepare(`SELECT id FROM betas WHERE script_id = ? AND state = 'running'`)
|
||||
.get(scriptId) as { id: number } | undefined;
|
||||
if (beta) throw new ScriptError(`beta ${beta.id} is still running against this script`, 'has-active-beta');
|
||||
db().prepare('DELETE FROM scripts WHERE id = ?').run(scriptId);
|
||||
}
|
||||
|
||||
// Servers that match a script's scope. Empty for unscoped scripts — those are
|
||||
// only ever pushed to explicitly chosen beta targets.
|
||||
export function serversMatchingScope(scopeType: ScopeType | null, scopeValue: string | null): string[] {
|
||||
if (scopeType === null || scopeValue === null) return [];
|
||||
const rows = db()
|
||||
.prepare(`SELECT name FROM servers WHERE ${scopeType} = ? ORDER BY name`)
|
||||
.all(scopeValue) as Array<{ name: string }>;
|
||||
return rows.map((r) => r.name);
|
||||
}
|
||||
Reference in New Issue
Block a user