refactor: remove ternary soup and tighten E2EE config
This commit is contained in:
@@ -128,12 +128,16 @@ export function DaemonRegistryProvider({ children }: { children: ReactNode }) {
|
|||||||
const existing = readDaemons();
|
const existing = readDaemons();
|
||||||
const now = new Date().toISOString();
|
const now = new Date().toISOString();
|
||||||
const normalizedEndpoints = offer.endpoints.map((endpoint) => normalizeHostPort(endpoint));
|
const normalizedEndpoints = offer.endpoints.map((endpoint) => normalizeHostPort(endpoint));
|
||||||
const relayEndpoint =
|
let relayEndpoint: string | null = null;
|
||||||
offer.relay?.endpoint
|
if (offer.relay?.endpoint) {
|
||||||
? normalizeHostPort(offer.relay.endpoint)
|
relayEndpoint = normalizeHostPort(offer.relay.endpoint);
|
||||||
: offer.relay === undefined && normalizedEndpoints.length > 0
|
} else if (offer.relay === undefined && normalizedEndpoints.length > 0) {
|
||||||
? normalizedEndpoints[normalizedEndpoints.length - 1]
|
// Back-compat: older offers encoded relay endpoint as the last entry.
|
||||||
: null;
|
relayEndpoint = normalizedEndpoints[normalizedEndpoints.length - 1];
|
||||||
|
}
|
||||||
|
const relay = relayEndpoint
|
||||||
|
? { endpoint: relayEndpoint, sessionId: offer.sessionId }
|
||||||
|
: null;
|
||||||
|
|
||||||
const matchIndex = existing.findIndex((daemon) => daemon.daemonPublicKeyB64 === offer.daemonPublicKeyB64);
|
const matchIndex = existing.findIndex((daemon) => daemon.daemonPublicKeyB64 === offer.daemonPublicKeyB64);
|
||||||
if (matchIndex !== -1) {
|
if (matchIndex !== -1) {
|
||||||
@@ -141,7 +145,7 @@ export function DaemonRegistryProvider({ children }: { children: ReactNode }) {
|
|||||||
...existing[matchIndex],
|
...existing[matchIndex],
|
||||||
daemonPublicKeyB64: offer.daemonPublicKeyB64,
|
daemonPublicKeyB64: offer.daemonPublicKeyB64,
|
||||||
endpoints: normalizedEndpoints,
|
endpoints: normalizedEndpoints,
|
||||||
relay: relayEndpoint ? { endpoint: relayEndpoint, sessionId: offer.sessionId } : null,
|
relay,
|
||||||
updatedAt: now,
|
updatedAt: now,
|
||||||
};
|
};
|
||||||
const next = [...existing];
|
const next = [...existing];
|
||||||
@@ -155,7 +159,7 @@ export function DaemonRegistryProvider({ children }: { children: ReactNode }) {
|
|||||||
label: deriveLabelFromEndpoint(normalizedEndpoints[0] ?? "Unnamed Host"),
|
label: deriveLabelFromEndpoint(normalizedEndpoints[0] ?? "Unnamed Host"),
|
||||||
endpoints: normalizedEndpoints,
|
endpoints: normalizedEndpoints,
|
||||||
daemonPublicKeyB64: offer.daemonPublicKeyB64,
|
daemonPublicKeyB64: offer.daemonPublicKeyB64,
|
||||||
relay: relayEndpoint ? { endpoint: relayEndpoint, sessionId: offer.sessionId } : null,
|
relay,
|
||||||
createdAt: now,
|
createdAt: now,
|
||||||
updatedAt: now,
|
updatedAt: now,
|
||||||
metadata: null,
|
metadata: null,
|
||||||
|
|||||||
@@ -323,13 +323,19 @@ export class DaemonClientV2 {
|
|||||||
const shouldUseRelayE2ee =
|
const shouldUseRelayE2ee =
|
||||||
this.config.e2ee?.enabled === true &&
|
this.config.e2ee?.enabled === true &&
|
||||||
isRelayClientWebSocketUrl(this.config.url);
|
isRelayClientWebSocketUrl(this.config.url);
|
||||||
const transportFactory = shouldUseRelayE2ee
|
|
||||||
? createRelayE2eeTransportFactory(
|
let transportFactory = baseTransportFactory;
|
||||||
baseTransportFactory,
|
if (shouldUseRelayE2ee) {
|
||||||
this.config.e2ee?.daemonPublicKeyB64,
|
const daemonPublicKeyB64 = this.config.e2ee?.daemonPublicKeyB64;
|
||||||
this.logger
|
if (!daemonPublicKeyB64) {
|
||||||
)
|
throw new Error("daemonPublicKeyB64 is required for relay E2EE");
|
||||||
: baseTransportFactory;
|
}
|
||||||
|
transportFactory = createRelayE2eeTransportFactory({
|
||||||
|
baseFactory: baseTransportFactory,
|
||||||
|
daemonPublicKeyB64,
|
||||||
|
logger: this.logger,
|
||||||
|
});
|
||||||
|
}
|
||||||
const transport = transportFactory({ url: this.config.url, headers });
|
const transport = transportFactory({ url: this.config.url, headers });
|
||||||
this.transport = transport;
|
this.transport = transport;
|
||||||
|
|
||||||
@@ -2345,18 +2351,14 @@ function createWebSocketTransportFactory(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function createRelayE2eeTransportFactory(
|
function createRelayE2eeTransportFactory(args: {
|
||||||
baseFactory: DaemonTransportFactory,
|
baseFactory: DaemonTransportFactory;
|
||||||
daemonPublicKeyB64: string | undefined,
|
daemonPublicKeyB64: string;
|
||||||
logger: Logger
|
logger: Logger;
|
||||||
): DaemonTransportFactory {
|
}): DaemonTransportFactory {
|
||||||
if (!daemonPublicKeyB64) {
|
|
||||||
throw new Error("daemonPublicKeyB64 is required for relay E2EE");
|
|
||||||
}
|
|
||||||
|
|
||||||
return ({ url, headers }) => {
|
return ({ url, headers }) => {
|
||||||
const base = baseFactory({ url, headers });
|
const base = args.baseFactory({ url, headers });
|
||||||
return createEncryptedTransport(base, daemonPublicKeyB64, logger);
|
return createEncryptedTransport(base, args.daemonPublicKeyB64, args.logger);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user