refactor: close type holes on the transport and error axes
- OpenApiRoute.transport was typed `string`, so the compiler could not check the transport branching at all. Type it Transport and key the documented 200 off a Record, so a new transport fails the build instead of silently going undocumented. - pickEncoder fell through to ndjson for unknown transports; make the mapping exhaustive so a new transport cannot inherit the wrong wire format by accident. - HttpError carries optional headers, removing the instanceof Redirect special case that coupled the error renderer to one subclass. Custom errors needing headers (retry-after, etag) no longer require touching transformers. - Drop the module-global connSeq in mesh/link: `pending` is already per-connection, so a per-link counter suffices and connection ids stop depending on test order. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
5 files changed
+55
-36
No files matched your search
+5
-12
@@ -2,8 +2,6 @@ import type { Bus } from '../bus';
|
||||
import { HttpError } from '../signals';
|
||||
import { encode, decode, type Manifest, type RequestEnvelope, type Frame } from './protocol';
|
||||
|
||||
let connSeq = 0; // module-level: deterministic unique connection-id prefix (no Date.now/Math.random)
|
||||
|
||||
/** Client handle to a remote mesh server: its manifest, an RPC caller, and a close(). */
|
||||
export interface Link {
|
||||
manifest: Manifest;
|
||||
@@ -56,19 +54,15 @@ function sendRpc(
|
||||
}
|
||||
|
||||
/** Assemble the client-facing Link once the manifest arrives. */
|
||||
function buildLink(
|
||||
ws: any,
|
||||
pending: Map<string, PendingEntry>,
|
||||
manifest: Manifest,
|
||||
connId: string,
|
||||
timeoutMs: number,
|
||||
): Link {
|
||||
function buildLink(ws: any, pending: Map<string, PendingEntry>, manifest: Manifest, timeoutMs: number): Link {
|
||||
// ids only need to be unique within this link: `pending` is per-connection, so a
|
||||
// per-link counter suffices (no module-global sequence, no test-order coupling).
|
||||
let counter = 0;
|
||||
|
||||
return {
|
||||
manifest,
|
||||
rpc(kind, name, ctx) {
|
||||
const id = `${connId}:${counter++}`;
|
||||
const id = String(counter++);
|
||||
|
||||
return sendRpc(ws, pending, id, kind, name, ctx, timeoutMs);
|
||||
},
|
||||
@@ -103,7 +97,6 @@ export function connectLink(args: { url: string; secret: string; timeoutMs?: num
|
||||
const WS = loadWs();
|
||||
const ws = new WS(args.url);
|
||||
const pending = new Map<string, PendingEntry>();
|
||||
const connId = `c${connSeq++}`;
|
||||
let manifest: Manifest | undefined;
|
||||
|
||||
return new Promise<Link>((resolve, reject) => {
|
||||
@@ -134,7 +127,7 @@ export function connectLink(args: { url: string; secret: string; timeoutMs?: num
|
||||
manifest = { scopes: frame.scopes, routes: frame.routes };
|
||||
clearTimeout(connectTimer);
|
||||
args.bus?.emit('mesh:connect', { name: args.url });
|
||||
resolve(buildLink(ws, pending, manifest, connId, timeoutMs));
|
||||
resolve(buildLink(ws, pending, manifest, timeoutMs));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user