1 parent
639362fbf5
commit
bdd69f0aad
101 files changed
+22691
-531
No files matched your search
@@ -22,6 +22,7 @@ export type ViewName =
|
||||
| 'library'
|
||||
| 'stats'
|
||||
| 'server'
|
||||
| 'updates'
|
||||
| 'rpc';
|
||||
|
||||
export interface AppState {
|
||||
|
||||
@@ -9,5 +9,7 @@ describe('audit RPC method mapping', () => {
|
||||
expect(auditMethodName(72)).toBe('Reserved72');
|
||||
expect(auditMethodName(73)).toBe('GetTeamSpeakDirectory');
|
||||
expect(auditMethodName(88)).toBe('SendTeamSpeakServerMessage');
|
||||
expect(auditMethodName(89)).toBe('GetUpdateStatus');
|
||||
expect(auditMethodName(95)).toBe('ConfirmUnsignedUpdate');
|
||||
});
|
||||
});
|
||||
@@ -94,6 +94,13 @@ const RPC_METHOD_BY_INDEX: readonly string[] = [
|
||||
'SendTeamSpeakPrivateMessage',
|
||||
'SendTeamSpeakChannelMessage',
|
||||
'SendTeamSpeakServerMessage',
|
||||
'GetUpdateStatus', // 89
|
||||
'GetUpdateConfig',
|
||||
'SetUpdateConfig',
|
||||
'CheckForUpdates',
|
||||
'ApplyUpdate',
|
||||
'RollbackUpdate',
|
||||
'ConfirmUnsignedUpdate', // 95
|
||||
];
|
||||
|
||||
export function auditMethodName(method: number): string {
|
||||
|
||||
@@ -11,5 +11,6 @@ describe('dashboard routing', () => {
|
||||
it('keeps the remaining flat views bookmarkable', () => {
|
||||
expect(viewFromHash('#queue')).toBe('queue');
|
||||
expect(viewFromHash('#server')).toBe('server');
|
||||
expect(viewFromHash('#updates')).toBe('updates');
|
||||
});
|
||||
});
|
||||
@@ -11,6 +11,7 @@ const VIEWS: readonly ViewName[] = [
|
||||
'library',
|
||||
'stats',
|
||||
'server',
|
||||
'updates',
|
||||
'rpc',
|
||||
];
|
||||
|
||||
|
||||
@@ -75,6 +75,18 @@ describe('RpcClient wire contract', () => {
|
||||
expect(frame.authToken).toBe('tok-abc');
|
||||
});
|
||||
|
||||
it('sends updater requests through their append-only RPC enum names', async () => {
|
||||
installFakes();
|
||||
const rpc = new RpcClient();
|
||||
await rpc.connect();
|
||||
const ws = FakeWebSocket.instances[0];
|
||||
|
||||
rpc.call('GetUpdateStatus');
|
||||
const frame = JSON.parse(ws.sent[0]);
|
||||
expect(frame.method).toBe('GetUpdateStatus');
|
||||
expect(JSON.parse(frame.params)).toEqual({});
|
||||
});
|
||||
|
||||
it('resolves a call by requestId and JSON-decodes the result string', async () => {
|
||||
installFakes();
|
||||
const rpc = new RpcClient();
|
||||
@@ -125,6 +137,21 @@ describe('RpcClient wire contract', () => {
|
||||
expect(seen).toEqual([{ position: 1000, duration: 200000 }]);
|
||||
});
|
||||
|
||||
it('dispatches the updater change event without renaming it', async () => {
|
||||
installFakes();
|
||||
const rpc = new RpcClient();
|
||||
await rpc.connect();
|
||||
const ws = FakeWebSocket.instances[0];
|
||||
|
||||
const seen: unknown[] = [];
|
||||
rpc.on('updates.changed', (data) => seen.push(data));
|
||||
ws.receive({
|
||||
event: 'updates.changed',
|
||||
data: JSON.stringify({ component: 'core', state: 'staged' }),
|
||||
});
|
||||
expect(seen).toEqual([{ component: 'core', state: 'staged' }]);
|
||||
});
|
||||
|
||||
it('fires the wildcard listener with event name and data', async () => {
|
||||
installFakes();
|
||||
const rpc = new RpcClient();
|
||||
|
||||
@@ -80,7 +80,14 @@ export type RpcMethodName =
|
||||
| 'RemoveTeamSpeakServerGroupMember'
|
||||
| 'SendTeamSpeakPrivateMessage'
|
||||
| 'SendTeamSpeakChannelMessage'
|
||||
| 'SendTeamSpeakServerMessage';
|
||||
| 'SendTeamSpeakServerMessage'
|
||||
| 'GetUpdateStatus'
|
||||
| 'GetUpdateConfig'
|
||||
| 'SetUpdateConfig'
|
||||
| 'CheckForUpdates'
|
||||
| 'ApplyUpdate'
|
||||
| 'RollbackUpdate'
|
||||
| 'ConfirmUnsignedUpdate';
|
||||
|
||||
|
||||
type Pending = {
|
||||
|
||||
@@ -28,5 +28,9 @@ export class Store<T> {
|
||||
}
|
||||
|
||||
export function useStore<T>(store: Store<T>): T {
|
||||
return useSyncExternalStore(store.subscribe, () => store.get());
|
||||
return useSyncExternalStore(
|
||||
store.subscribe,
|
||||
() => store.get(),
|
||||
() => store.get()
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
configForSave,
|
||||
isValidSourceId,
|
||||
normalizeUpdateConfig,
|
||||
normalizeUpdateStatus,
|
||||
} from './updates';
|
||||
|
||||
describe('update payload normalization', () => {
|
||||
it('normalizes direct config and strips read-only secret presence on save', () => {
|
||||
const config = normalizeUpdateConfig({
|
||||
enabled: true,
|
||||
checkInterval: '12h',
|
||||
sources: {
|
||||
private: {
|
||||
url: ' https://updates.example/feed.json ',
|
||||
publicKeys: [{ id: ' key-1 ', ed25519: ' abc123 ' }],
|
||||
allowUnsigned: false,
|
||||
bearerTokenEnv: ' UPDATE_TOKEN ',
|
||||
bearerTokenPresent: true,
|
||||
bearerToken: 'must-not-enter-browser-state',
|
||||
},
|
||||
},
|
||||
runtime: { source: 'private', channel: 'stable' },
|
||||
plugins: { managedOnly: false },
|
||||
});
|
||||
|
||||
expect(config.checkInterval).toBe('12h');
|
||||
expect(config.sources.private.bearerTokenPresent).toBe(true);
|
||||
expect(config.sources.private).not.toHaveProperty('bearerToken');
|
||||
expect(config.runtime.source).toBe('private');
|
||||
expect(config.plugins.managedOnly).toBe(true);
|
||||
const writable = configForSave(config);
|
||||
expect(writable.sources.private).not.toHaveProperty(
|
||||
'bearerTokenPresent'
|
||||
);
|
||||
expect(writable.sources.private.bearerTokenEnv).toBe('UPDATE_TOKEN');
|
||||
expect(writable.sources.private.url).toBe(
|
||||
'https://updates.example/feed.json'
|
||||
);
|
||||
expect(writable.sources.private.publicKeys).toEqual([
|
||||
{ id: 'key-1', ed25519: 'abc123' },
|
||||
]);
|
||||
});
|
||||
|
||||
it('normalizes component candidates, plugin inventory, and history', () => {
|
||||
const status = normalizeUpdateStatus({
|
||||
enabled: true,
|
||||
checking: false,
|
||||
components: {
|
||||
runtime: {
|
||||
installedVersion: '1.0.0',
|
||||
availableVersion: '1.1.0',
|
||||
signed: false,
|
||||
requiresRestart: true,
|
||||
},
|
||||
core: { currentVersion: '4.0.0', state: 'healthy' },
|
||||
plugins: { state: 'current' },
|
||||
},
|
||||
pluginEntries: [
|
||||
{
|
||||
pluginId: 'weather',
|
||||
managed: true,
|
||||
installedVersion: '2.0.0',
|
||||
candidate: { version: '2.1.0', signed: true },
|
||||
},
|
||||
],
|
||||
history: [{ timestamp: 42, component: 'runtime', result: 'success' }],
|
||||
});
|
||||
|
||||
expect(status.components.runtime.currentVersion).toBe('1.0.0');
|
||||
expect(status.components.runtime.candidate).toMatchObject({
|
||||
version: '1.1.0',
|
||||
signed: false,
|
||||
requiresRestart: true,
|
||||
});
|
||||
expect(status.plugins[0]).toMatchObject({
|
||||
id: 'weather',
|
||||
managed: true,
|
||||
currentVersion: '2.0.0',
|
||||
});
|
||||
expect(status.history).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('accepts safe source identifiers only', () => {
|
||||
expect(isValidSourceId('private-beta_2')).toBe(true);
|
||||
expect(isValidSourceId('../escape')).toBe(false);
|
||||
expect(isValidSourceId('has spaces')).toBe(false);
|
||||
expect(isValidSourceId('')).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,481 @@
|
||||
export type UpdateComponent = 'runtime' | 'core' | 'plugins';
|
||||
|
||||
export interface UpdatePublicKey {
|
||||
id: string;
|
||||
ed25519: string;
|
||||
}
|
||||
|
||||
export interface UpdateSourceConfig {
|
||||
url: string;
|
||||
publicKeys: UpdatePublicKey[];
|
||||
allowUnsigned: boolean;
|
||||
bearerTokenEnv: string;
|
||||
// Read-only status supplied by the server. It is stripped before save so
|
||||
// the browser can never turn secret-presence metadata into configuration.
|
||||
bearerTokenPresent?: boolean;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export interface ComponentUpdatePolicy {
|
||||
enabled: boolean;
|
||||
source: string;
|
||||
channel: string;
|
||||
autoApply: boolean;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export interface PluginOverridePolicy {
|
||||
source: string;
|
||||
channel: string;
|
||||
autoApply: boolean;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export interface PluginUpdatePolicy extends ComponentUpdatePolicy {
|
||||
managedOnly: boolean;
|
||||
overrides: Record<string, PluginOverridePolicy>;
|
||||
}
|
||||
|
||||
export interface UpdateConfig {
|
||||
enabled: boolean;
|
||||
checkInterval: string;
|
||||
jitter: string;
|
||||
maintenanceWindowUtc: {
|
||||
start: string;
|
||||
duration: string;
|
||||
[key: string]: unknown;
|
||||
};
|
||||
maxRuntimeDeferral: string;
|
||||
retainHealthyVersions: number;
|
||||
sources: Record<string, UpdateSourceConfig>;
|
||||
runtime: ComponentUpdatePolicy;
|
||||
core: ComponentUpdatePolicy;
|
||||
plugins: PluginUpdatePolicy;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export interface UpdateCandidate {
|
||||
id?: string;
|
||||
version: string;
|
||||
channel?: string;
|
||||
source?: string;
|
||||
signed?: boolean;
|
||||
compatible?: boolean;
|
||||
downgrade?: boolean;
|
||||
requiresRestart?: boolean;
|
||||
cohort?: string;
|
||||
message?: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export interface ComponentUpdateStatus {
|
||||
component: UpdateComponent;
|
||||
currentVersion?: string;
|
||||
state?: string;
|
||||
message?: string;
|
||||
channel?: string;
|
||||
source?: string;
|
||||
candidate?: UpdateCandidate;
|
||||
rollbackVersion?: string;
|
||||
lastChangedAt?: string | number;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export interface PluginUpdateStatus {
|
||||
id: string;
|
||||
name?: string;
|
||||
managed: boolean;
|
||||
currentVersion?: string;
|
||||
state?: string;
|
||||
channel?: string;
|
||||
source?: string;
|
||||
candidate?: UpdateCandidate;
|
||||
rollbackVersion?: string;
|
||||
message?: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export interface UpdateHistoryEntry {
|
||||
timestamp?: string | number;
|
||||
component?: UpdateComponent | string;
|
||||
pluginId?: string;
|
||||
action?: string;
|
||||
version?: string;
|
||||
result?: string;
|
||||
message?: string;
|
||||
actor?: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export interface UpdateStatus {
|
||||
enabled: boolean;
|
||||
checking: boolean;
|
||||
lastCheckAt?: string | number;
|
||||
nextCheckAt?: string | number;
|
||||
components: Record<UpdateComponent, ComponentUpdateStatus>;
|
||||
plugins: PluginUpdateStatus[];
|
||||
history: UpdateHistoryEntry[];
|
||||
message?: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
const DEFAULT_COMPONENT_POLICY: ComponentUpdatePolicy = {
|
||||
enabled: true,
|
||||
source: 'official',
|
||||
channel: 'stable',
|
||||
autoApply: true,
|
||||
};
|
||||
|
||||
export const DEFAULT_UPDATE_CONFIG: UpdateConfig = {
|
||||
enabled: true,
|
||||
checkInterval: '6h',
|
||||
jitter: '30m',
|
||||
maintenanceWindowUtc: {
|
||||
start: '03:00',
|
||||
duration: '2h',
|
||||
},
|
||||
maxRuntimeDeferral: '24h',
|
||||
retainHealthyVersions: 2,
|
||||
sources: {
|
||||
official: {
|
||||
url: '',
|
||||
publicKeys: [],
|
||||
allowUnsigned: false,
|
||||
bearerTokenEnv: '',
|
||||
},
|
||||
},
|
||||
runtime: { ...DEFAULT_COMPONENT_POLICY },
|
||||
core: { ...DEFAULT_COMPONENT_POLICY },
|
||||
plugins: {
|
||||
...DEFAULT_COMPONENT_POLICY,
|
||||
managedOnly: true,
|
||||
overrides: {},
|
||||
},
|
||||
};
|
||||
|
||||
function record(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === 'object' && !Array.isArray(value)
|
||||
? (value as Record<string, unknown>)
|
||||
: {};
|
||||
}
|
||||
|
||||
function stringValue(value: unknown, fallback = ''): string {
|
||||
return typeof value === 'string' ? value : fallback;
|
||||
}
|
||||
|
||||
function booleanValue(value: unknown, fallback: boolean): boolean {
|
||||
return typeof value === 'boolean' ? value : fallback;
|
||||
}
|
||||
|
||||
function numberValue(value: unknown, fallback: number): number {
|
||||
return typeof value === 'number' && Number.isFinite(value)
|
||||
? value
|
||||
: fallback;
|
||||
}
|
||||
|
||||
function unwrap(payload: unknown, key: string): Record<string, unknown> {
|
||||
const outer = record(payload);
|
||||
const nested = record(outer[key]);
|
||||
return Object.keys(nested).length ? nested : outer;
|
||||
}
|
||||
|
||||
function sourceFieldsWithoutSecrets(
|
||||
source: Record<string, unknown>
|
||||
): Record<string, unknown> {
|
||||
return Object.fromEntries(
|
||||
Object.entries(source).filter(([key]) => {
|
||||
if (key === 'bearerTokenEnv' || key === 'bearerTokenPresent') {
|
||||
return true;
|
||||
}
|
||||
return !/(token|secret|password|authorization|privatekey)/i.test(key);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
function normalizeComponentPolicy(
|
||||
value: unknown,
|
||||
fallback: ComponentUpdatePolicy
|
||||
): ComponentUpdatePolicy {
|
||||
const raw = record(value);
|
||||
return {
|
||||
...raw,
|
||||
enabled: booleanValue(raw.enabled, fallback.enabled),
|
||||
source: stringValue(raw.source, fallback.source),
|
||||
channel: stringValue(raw.channel, fallback.channel),
|
||||
autoApply: booleanValue(raw.autoApply, fallback.autoApply),
|
||||
};
|
||||
}
|
||||
|
||||
function normalizePluginOverride(
|
||||
value: unknown,
|
||||
fallback: ComponentUpdatePolicy
|
||||
): PluginOverridePolicy {
|
||||
const raw = record(value);
|
||||
return {
|
||||
...raw,
|
||||
source: stringValue(raw.source, fallback.source),
|
||||
channel: stringValue(raw.channel, fallback.channel),
|
||||
autoApply: booleanValue(raw.autoApply, fallback.autoApply),
|
||||
};
|
||||
}
|
||||
|
||||
export function normalizeUpdateConfig(payload: unknown): UpdateConfig {
|
||||
const raw = unwrap(payload, 'config');
|
||||
const rawSources = record(raw.sources);
|
||||
const sources: Record<string, UpdateSourceConfig> = {};
|
||||
|
||||
for (const [id, value] of Object.entries(rawSources)) {
|
||||
const source = record(value);
|
||||
const safeSource = sourceFieldsWithoutSecrets(source);
|
||||
const rawKeys = Array.isArray(source.publicKeys)
|
||||
? source.publicKeys
|
||||
: [];
|
||||
sources[id] = {
|
||||
...safeSource,
|
||||
url: stringValue(source.url),
|
||||
publicKeys: rawKeys.map((key) => {
|
||||
const rawKey = record(key);
|
||||
return {
|
||||
id: stringValue(rawKey.id),
|
||||
ed25519: stringValue(rawKey.ed25519),
|
||||
};
|
||||
}),
|
||||
allowUnsigned: booleanValue(source.allowUnsigned, false),
|
||||
bearerTokenEnv: stringValue(source.bearerTokenEnv),
|
||||
...(typeof source.bearerTokenPresent === 'boolean'
|
||||
? { bearerTokenPresent: source.bearerTokenPresent }
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
|
||||
if (!Object.keys(sources).length) {
|
||||
sources.official = { ...DEFAULT_UPDATE_CONFIG.sources.official };
|
||||
}
|
||||
|
||||
const runtime = normalizeComponentPolicy(
|
||||
raw.runtime,
|
||||
DEFAULT_UPDATE_CONFIG.runtime
|
||||
);
|
||||
const core = normalizeComponentPolicy(raw.core, DEFAULT_UPDATE_CONFIG.core);
|
||||
const rawPlugins = record(raw.plugins);
|
||||
const pluginBase = normalizeComponentPolicy(
|
||||
rawPlugins,
|
||||
DEFAULT_UPDATE_CONFIG.plugins
|
||||
);
|
||||
const overrides: Record<string, PluginOverridePolicy> = {};
|
||||
for (const [id, value] of Object.entries(record(rawPlugins.overrides))) {
|
||||
overrides[id] = normalizePluginOverride(value, pluginBase);
|
||||
}
|
||||
|
||||
const rawWindow = record(raw.maintenanceWindowUtc);
|
||||
return {
|
||||
...raw,
|
||||
enabled: booleanValue(raw.enabled, DEFAULT_UPDATE_CONFIG.enabled),
|
||||
checkInterval: stringValue(
|
||||
raw.checkInterval,
|
||||
DEFAULT_UPDATE_CONFIG.checkInterval
|
||||
),
|
||||
jitter: stringValue(raw.jitter, DEFAULT_UPDATE_CONFIG.jitter),
|
||||
maintenanceWindowUtc: {
|
||||
...rawWindow,
|
||||
start: stringValue(
|
||||
rawWindow.start,
|
||||
DEFAULT_UPDATE_CONFIG.maintenanceWindowUtc.start
|
||||
),
|
||||
duration: stringValue(
|
||||
rawWindow.duration,
|
||||
DEFAULT_UPDATE_CONFIG.maintenanceWindowUtc.duration
|
||||
),
|
||||
},
|
||||
maxRuntimeDeferral: stringValue(
|
||||
raw.maxRuntimeDeferral,
|
||||
DEFAULT_UPDATE_CONFIG.maxRuntimeDeferral
|
||||
),
|
||||
retainHealthyVersions: numberValue(
|
||||
raw.retainHealthyVersions,
|
||||
DEFAULT_UPDATE_CONFIG.retainHealthyVersions
|
||||
),
|
||||
sources,
|
||||
runtime,
|
||||
core,
|
||||
plugins: {
|
||||
...rawPlugins,
|
||||
...pluginBase,
|
||||
// Loose local shared objects are never updater-owned. Keep this an
|
||||
// invariant even if an older or hand-written config says otherwise.
|
||||
managedOnly: true,
|
||||
overrides,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeCandidate(value: unknown): UpdateCandidate | undefined {
|
||||
const raw = record(value);
|
||||
const version = stringValue(raw.version || raw.availableVersion);
|
||||
if (!version) return undefined;
|
||||
return {
|
||||
...raw,
|
||||
id: stringValue(raw.id || raw.candidateId) || undefined,
|
||||
version,
|
||||
channel: stringValue(raw.channel) || undefined,
|
||||
source: stringValue(raw.source) || undefined,
|
||||
...(typeof raw.signed === 'boolean' ? { signed: raw.signed } : {}),
|
||||
...(typeof raw.compatible === 'boolean'
|
||||
? { compatible: raw.compatible }
|
||||
: {}),
|
||||
...(typeof raw.downgrade === 'boolean'
|
||||
? { downgrade: raw.downgrade }
|
||||
: {}),
|
||||
...(typeof raw.requiresRestart === 'boolean'
|
||||
? { requiresRestart: raw.requiresRestart }
|
||||
: {}),
|
||||
cohort: stringValue(raw.cohort) || undefined,
|
||||
message: stringValue(raw.message) || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeComponent(
|
||||
component: UpdateComponent,
|
||||
value: unknown
|
||||
): ComponentUpdateStatus {
|
||||
const raw = record(value);
|
||||
const candidate =
|
||||
normalizeCandidate(raw.candidate) ||
|
||||
normalizeCandidate({
|
||||
version: raw.availableVersion,
|
||||
id: raw.candidateId,
|
||||
channel: raw.availableChannel,
|
||||
source: raw.availableSource,
|
||||
signed: raw.signed,
|
||||
compatible: raw.compatible,
|
||||
downgrade: raw.downgrade,
|
||||
requiresRestart: raw.requiresRestart,
|
||||
});
|
||||
return {
|
||||
...raw,
|
||||
component,
|
||||
currentVersion:
|
||||
stringValue(raw.currentVersion || raw.installedVersion) || undefined,
|
||||
state: stringValue(raw.state || raw.status) || undefined,
|
||||
message: stringValue(raw.message) || undefined,
|
||||
channel: stringValue(raw.channel) || undefined,
|
||||
source: stringValue(raw.source) || undefined,
|
||||
candidate,
|
||||
rollbackVersion: stringValue(raw.rollbackVersion) || undefined,
|
||||
lastChangedAt:
|
||||
typeof raw.lastChangedAt === 'string' ||
|
||||
typeof raw.lastChangedAt === 'number'
|
||||
? raw.lastChangedAt
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizePlugin(value: unknown): PluginUpdateStatus | null {
|
||||
const raw = record(value);
|
||||
const id = stringValue(raw.id || raw.pluginId);
|
||||
if (!id) return null;
|
||||
return {
|
||||
...raw,
|
||||
id,
|
||||
name: stringValue(raw.name) || undefined,
|
||||
managed: booleanValue(raw.managed, false),
|
||||
currentVersion:
|
||||
stringValue(raw.currentVersion || raw.installedVersion) || undefined,
|
||||
state: stringValue(raw.state || raw.status) || undefined,
|
||||
channel: stringValue(raw.channel) || undefined,
|
||||
source: stringValue(raw.source) || undefined,
|
||||
candidate:
|
||||
normalizeCandidate(raw.candidate) ||
|
||||
normalizeCandidate({
|
||||
version: raw.availableVersion,
|
||||
id: raw.candidateId,
|
||||
signed: raw.signed,
|
||||
compatible: raw.compatible,
|
||||
downgrade: raw.downgrade,
|
||||
requiresRestart: raw.requiresRestart,
|
||||
}),
|
||||
rollbackVersion: stringValue(raw.rollbackVersion) || undefined,
|
||||
message: stringValue(raw.message) || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
export function normalizeUpdateStatus(payload: unknown): UpdateStatus {
|
||||
const raw = unwrap(payload, 'status');
|
||||
const rawComponents = record(raw.components);
|
||||
const pluginComponent = record(
|
||||
rawComponents.plugins ||
|
||||
(!Array.isArray(raw.plugins) ? raw.plugins : undefined)
|
||||
);
|
||||
const rawPluginEntries =
|
||||
(Array.isArray(raw.pluginEntries) && raw.pluginEntries) ||
|
||||
(Array.isArray(raw.managedPlugins) && raw.managedPlugins) ||
|
||||
(Array.isArray(raw.plugins) && raw.plugins) ||
|
||||
(Array.isArray(pluginComponent.items) && pluginComponent.items) ||
|
||||
[];
|
||||
const rawHistory = Array.isArray(raw.history) ? raw.history : [];
|
||||
|
||||
return {
|
||||
...raw,
|
||||
enabled: booleanValue(raw.enabled, true),
|
||||
checking: booleanValue(raw.checking, false),
|
||||
lastCheckAt:
|
||||
typeof raw.lastCheckAt === 'string' ||
|
||||
typeof raw.lastCheckAt === 'number'
|
||||
? raw.lastCheckAt
|
||||
: undefined,
|
||||
nextCheckAt:
|
||||
typeof raw.nextCheckAt === 'string' ||
|
||||
typeof raw.nextCheckAt === 'number'
|
||||
? raw.nextCheckAt
|
||||
: undefined,
|
||||
components: {
|
||||
runtime: normalizeComponent(
|
||||
'runtime',
|
||||
rawComponents.runtime || raw.runtime
|
||||
),
|
||||
core: normalizeComponent('core', rawComponents.core || raw.core),
|
||||
plugins: normalizeComponent('plugins', pluginComponent),
|
||||
},
|
||||
plugins: rawPluginEntries
|
||||
.map(normalizePlugin)
|
||||
.filter((plugin): plugin is PluginUpdateStatus => plugin !== null),
|
||||
history: rawHistory.map((entry) => record(entry) as UpdateHistoryEntry),
|
||||
message: stringValue(raw.message) || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
export function configForSave(config: UpdateConfig): UpdateConfig {
|
||||
const normalized = normalizeUpdateConfig(config);
|
||||
const sources: Record<string, UpdateSourceConfig> = {};
|
||||
for (const [id, source] of Object.entries(normalized.sources)) {
|
||||
const { bearerTokenPresent: _present, ...writable } = source;
|
||||
sources[id] = {
|
||||
...sourceFieldsWithoutSecrets(writable),
|
||||
url: source.url.trim(),
|
||||
allowUnsigned: source.allowUnsigned,
|
||||
bearerTokenEnv: source.bearerTokenEnv.trim(),
|
||||
publicKeys: source.publicKeys
|
||||
.map((key) => ({
|
||||
id: key.id.trim(),
|
||||
ed25519: key.ed25519.trim(),
|
||||
}))
|
||||
.filter((key) => key.id || key.ed25519),
|
||||
};
|
||||
}
|
||||
return {
|
||||
...normalized,
|
||||
sources,
|
||||
retainHealthyVersions: Math.max(
|
||||
1,
|
||||
Math.min(10, Math.trunc(normalized.retainHealthyVersions))
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
export function isValidSourceId(value: string): boolean {
|
||||
return /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/.test(value);
|
||||
}
|
||||
|
||||
export function cloneUpdateConfig(config: UpdateConfig): UpdateConfig {
|
||||
return normalizeUpdateConfig(JSON.parse(JSON.stringify(config)));
|
||||
}
|
||||
Reference in new issue
Block a user