refactor: lint cleanups and helpers
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
export type AgentEventStream =
|
||||
| "job"
|
||||
| "tool"
|
||||
| "assistant"
|
||||
| "error"
|
||||
| (string & {});
|
||||
|
||||
export type AgentEventPayload = {
|
||||
runId: string;
|
||||
seq: number;
|
||||
stream: "job" | "tool" | string;
|
||||
stream: AgentEventStream;
|
||||
ts: number;
|
||||
data: Record<string, unknown>;
|
||||
};
|
||||
|
||||
@@ -9,6 +9,9 @@ const logWarn = vi.fn();
|
||||
const logDebug = vi.fn();
|
||||
const getLoggerInfo = vi.fn();
|
||||
|
||||
const asString = (value: unknown, fallback: string) =>
|
||||
typeof value === "string" && value.trim() ? value : fallback;
|
||||
|
||||
vi.mock("../logger.js", () => {
|
||||
return {
|
||||
logWarn: (message: string) => logWarn(message),
|
||||
@@ -86,8 +89,8 @@ describe("gateway bonjour advertiser", () => {
|
||||
serviceState: "announced",
|
||||
on: vi.fn(),
|
||||
getFQDN: () =>
|
||||
`${String(options.type ?? "service")}.${String(options.domain ?? "local")}.`,
|
||||
getHostname: () => String(options.hostname ?? "unknown"),
|
||||
`${asString(options.type, "service")}.${asString(options.domain, "local")}.`,
|
||||
getHostname: () => asString(options.hostname, "unknown"),
|
||||
getPort: () => Number(options.port ?? -1),
|
||||
};
|
||||
});
|
||||
@@ -153,8 +156,8 @@ describe("gateway bonjour advertiser", () => {
|
||||
serviceState: "announced",
|
||||
on,
|
||||
getFQDN: () =>
|
||||
`${String(options.type ?? "service")}.${String(options.domain ?? "local")}.`,
|
||||
getHostname: () => String(options.hostname ?? "unknown"),
|
||||
`${asString(options.type, "service")}.${asString(options.domain, "local")}.`,
|
||||
getHostname: () => asString(options.hostname, "unknown"),
|
||||
getPort: () => Number(options.port ?? -1),
|
||||
};
|
||||
});
|
||||
@@ -195,8 +198,8 @@ describe("gateway bonjour advertiser", () => {
|
||||
serviceState: "unannounced",
|
||||
on: vi.fn(),
|
||||
getFQDN: () =>
|
||||
`${String(options.type ?? "service")}.${String(options.domain ?? "local")}.`,
|
||||
getHostname: () => String(options.hostname ?? "unknown"),
|
||||
`${asString(options.type, "service")}.${asString(options.domain, "local")}.`,
|
||||
getHostname: () => asString(options.hostname, "unknown"),
|
||||
getPort: () => Number(options.port ?? -1),
|
||||
};
|
||||
});
|
||||
@@ -245,8 +248,8 @@ describe("gateway bonjour advertiser", () => {
|
||||
serviceState: "unannounced",
|
||||
on: vi.fn(),
|
||||
getFQDN: () =>
|
||||
`${String(options.type ?? "service")}.${String(options.domain ?? "local")}.`,
|
||||
getHostname: () => String(options.hostname ?? "unknown"),
|
||||
`${asString(options.type, "service")}.${asString(options.domain, "local")}.`,
|
||||
getHostname: () => asString(options.hostname, "unknown"),
|
||||
getPort: () => Number(options.port ?? -1),
|
||||
};
|
||||
});
|
||||
@@ -281,8 +284,8 @@ describe("gateway bonjour advertiser", () => {
|
||||
serviceState: "announced",
|
||||
on: vi.fn(),
|
||||
getFQDN: () =>
|
||||
`${String(options.type ?? "service")}.${String(options.domain ?? "local")}.`,
|
||||
getHostname: () => String(options.hostname ?? "unknown"),
|
||||
`${asString(options.type, "service")}.${asString(options.domain, "local")}.`,
|
||||
getHostname: () => asString(options.hostname, "unknown"),
|
||||
getPort: () => Number(options.port ?? -1),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -329,8 +329,9 @@ export async function startNodeBridgeServer(
|
||||
? hello.commands.map((c) => String(c)).filter(Boolean)
|
||||
: verified.node.commands;
|
||||
const helloPermissions = normalizePermissions(hello.permissions);
|
||||
const basePermissions = verified.node.permissions ?? {};
|
||||
const permissions = helloPermissions
|
||||
? { ...(verified.node.permissions ?? {}), ...helloPermissions }
|
||||
? { ...basePermissions, ...helloPermissions }
|
||||
: verified.node.permissions;
|
||||
|
||||
isAuthenticated = true;
|
||||
|
||||
26
src/infra/errors.ts
Normal file
26
src/infra/errors.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
export function extractErrorCode(err: unknown): string | undefined {
|
||||
if (!err || typeof err !== "object") return undefined;
|
||||
const code = (err as { code?: unknown }).code;
|
||||
if (typeof code === "string") return code;
|
||||
if (typeof code === "number") return String(code);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function formatErrorMessage(err: unknown): string {
|
||||
if (err instanceof Error) {
|
||||
return err.message || err.name || "Error";
|
||||
}
|
||||
if (typeof err === "string") return err;
|
||||
if (
|
||||
typeof err === "number" ||
|
||||
typeof err === "boolean" ||
|
||||
typeof err === "bigint"
|
||||
) {
|
||||
return String(err);
|
||||
}
|
||||
try {
|
||||
return JSON.stringify(err);
|
||||
} catch {
|
||||
return Object.prototype.toString.call(err);
|
||||
}
|
||||
}
|
||||
@@ -240,7 +240,7 @@ export function listSystemPresence(): SystemPresence[] {
|
||||
ensureSelfPresence();
|
||||
// prune expired
|
||||
const now = Date.now();
|
||||
for (const [k, v] of [...entries]) {
|
||||
for (const [k, v] of entries) {
|
||||
if (now - v.ts > TTL_MS) entries.delete(k);
|
||||
}
|
||||
// enforce max size (LRU by ts)
|
||||
|
||||
13
src/infra/ws.ts
Normal file
13
src/infra/ws.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Buffer } from "node:buffer";
|
||||
|
||||
import type WebSocket from "ws";
|
||||
|
||||
export function rawDataToString(
|
||||
data: WebSocket.RawData,
|
||||
encoding: BufferEncoding = "utf8",
|
||||
): string {
|
||||
if (typeof data === "string") return data;
|
||||
if (Buffer.isBuffer(data)) return data.toString(encoding);
|
||||
if (Array.isArray(data)) return Buffer.concat(data).toString(encoding);
|
||||
return Buffer.from(data as ArrayBuffer | ArrayBufferView).toString(encoding);
|
||||
}
|
||||
Reference in New Issue
Block a user