Skip to content

Commit d54cbb5

Browse files
committed
refactor(bridge): replace MeshStatePatch type assertion with type guard
Add isMeshStatePatch() that validates the object shape and checks the type discriminator against known literal values, replacing the sole production eslint-disable for consistent-type-assertions.
1 parent 5e4072e commit d54cbb5

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

src/bridges/user/web/frontend/relay-worker.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ type MeshStatePatch =
9393
}
9494
| { type: "message_read"; messageId: string; readBy: string; room?: string };
9595

96+
const PATCH_TYPES = new Set([
97+
"agent_upsert",
98+
"agent_offline",
99+
"room_upsert",
100+
"room_delete",
101+
"message_add",
102+
"dm_add",
103+
"delivery",
104+
"message_read",
105+
]);
106+
107+
function isMeshStatePatch(value: unknown): value is MeshStatePatch {
108+
if (typeof value !== "object" || value === null) return false;
109+
if (!("type" in value)) return false;
110+
if (typeof value.type !== "string") return false;
111+
return PATCH_TYPES.has(value.type);
112+
}
113+
96114
// ---------------------------------------------------------------------------
97115
// Wire message (subset — only what the relay needs to handle)
98116
// ---------------------------------------------------------------------------
@@ -308,15 +326,9 @@ function translateWireMessage(label: "a" | "b", msg: WireMessage): WireMessage {
308326
if (
309327
msg.method === "state_update" &&
310328
"patch" in msg &&
311-
typeof msg.patch === "object" &&
312-
msg.patch !== null &&
313-
"type" in msg.patch &&
314-
typeof msg.patch.type === "string"
329+
isMeshStatePatch(msg.patch)
315330
) {
316-
// Patch validated as object with string `type` — cast to MeshStatePatch discriminated union
317-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
318-
const patch = msg.patch as MeshStatePatch;
319-
return { ...msg, patch: translatePatch(label, patch) };
331+
return { ...msg, patch: translatePatch(label, msg.patch) };
320332
}
321333
if (
322334
msg.method === "peer_joined" &&

0 commit comments

Comments
 (0)