Files
Netcatty/domain/portForwardingPersistence.ts
T
8ee55963d8 feat(portforward): make main process authoritative for runtime state (#2288) (#2819)
* feat(portforward): make main process authoritative for runtime state

Phase 1 of #2288: add snapshot/runtime event protocol with epoch+revision,
stop persisting active/connecting/error into localStorage, migrate legacy
stored phases on read, and project unknown when snapshots are unavailable.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix(portforward): access runtime bridge via netcattyBridge

Satisfy the no-restricted-properties lint rule for window.netcatty.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix(portforward): honor snapshot authority and prune on resync

Default runtime projection to unknown until a successful snapshot,
reconcile (not sync-only) on epoch/revision gaps, serialize runtime
event handling, clear worker runtime subscribers on window destroy,
and align tray unknown UX with RuleCard.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-08-08 02:50:41 +00:00

49 lines
1.4 KiB
TypeScript

import type { PortForwardingRule } from './models/portForwarding';
/**
* Fields that describe a rule's configuration / user metadata.
* Runtime phase (`status`, `error`) is owned by the main-process registry
* and must not be treated as durable storage.
*/
export type PersistedPortForwardingRule = Omit<PortForwardingRule, 'status' | 'error'> & {
status: 'inactive';
error?: undefined;
};
/** Strip live runtime fields before writing localStorage / sync payloads. */
export function toPersistedPortForwardingRule(
rule: PortForwardingRule,
): PersistedPortForwardingRule {
return {
...rule,
status: 'inactive',
error: undefined,
};
}
export function toPersistedPortForwardingRules(
rules: readonly PortForwardingRule[],
): PersistedPortForwardingRule[] {
return rules.map(toPersistedPortForwardingRule);
}
/**
* Migrate legacy stored rules that still carry active/connecting phases.
* Historical `error` is kept as a disposable diagnostic until a successful
* backend snapshot proves the rule inactive (or a live connection overlays it).
*/
export function migratePortForwardingRulesFromStorage(
rules: readonly PortForwardingRule[],
): PortForwardingRule[] {
return rules.map((rule) => {
if (rule.status === 'active' || rule.status === 'connecting') {
return {
...rule,
status: 'inactive' as const,
error: undefined,
};
}
return rule;
});
}