Skip to content

Commit 0d8d67c

Browse files
committed
fix: code quality — structured logging, Workers-safe show(), dead export, private field access
- master-do: add structured log() method, replace console.warn with this.log("warn", ...) - client: DataFrame.show() falls back to JSON.stringify when console.table unavailable (Workers) - query-schema: remove dead ValidatedQuery type export (never imported) - local: add getExecutor() public method for pg-wire integration - pg-wire/server: use qm.getExecutor() instead of unsafe `as unknown as` private field access
1 parent f589bf9 commit 0d8d67c

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

src/client.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,11 @@ export class DataFrame<T extends Row = Row> {
708708
async show(n = 10): Promise<void> {
709709
const rows = await this.head(n);
710710
if (rows.length === 0) { console.log("(empty)"); return; }
711-
console.table(rows);
711+
if (typeof console.table === "function") {
712+
console.table(rows);
713+
} else {
714+
console.log(JSON.stringify(rows, null, 2));
715+
}
712716
}
713717

714718
/** Streaming iteration over results in batches. */

src/local.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ export class QueryMode {
6262
static demo(tableName?: string): DataFrame {
6363
return createDemo(tableName);
6464
}
65+
66+
/** @internal Get the underlying executor (for pg-wire and similar integrations). */
67+
getExecutor(): QueryExecutor {
68+
return this.executor;
69+
}
6570
}
6671

6772
export { LocalExecutor } from "./local-executor.js";

src/master-do.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ export class MasterDO extends DurableObject<Env> {
2222
super(ctx, env);
2323
}
2424

25+
private log(level: "info" | "warn" | "error", msg: string, data?: Record<string, unknown>): void {
26+
console[level === "error" ? "error" : level === "warn" ? "warn" : "log"](
27+
JSON.stringify({ ts: new Date().toISOString(), level, msg, ...data }),
28+
);
29+
}
30+
2531
// ── RPC methods ────────────────────────────────────────────────────────
2632

2733
async registerRpc(queryDoId: string, region: string): Promise<{ registered: boolean; region: string; tableVersions?: Record<string, { r2Key: string; updatedAt: number }> }> {
@@ -412,10 +418,10 @@ export class MasterDO extends DurableObject<Env> {
412418
const count = (this.broadcastFailures.get(region) ?? 0) + 1;
413419
this.broadcastFailures.set(region, count);
414420
if (count >= 3) {
415-
console.warn(`Broadcast to ${region} failed ${count} times, removing`);
421+
this.log("warn", "broadcast_region_removed", { region, failures: count });
416422
deadRegions.push(region);
417423
} else {
418-
console.warn(`Broadcast to ${region} failed (${count}/3)`);
424+
this.log("warn", "broadcast_failed", { region, failures: count, threshold: 3 });
419425
}
420426
}
421427
}));

src/pg-wire/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const qm = QueryMode.local();
1919

2020
const server = net.createServer((socket) => {
2121
const handler = new PgConnectionHandler({
22-
executor: (qm as unknown as { executor: import("../client.js").QueryExecutor }).executor,
22+
executor: qm.getExecutor(),
2323
send: (data) => {
2424
if (!socket.destroyed) socket.write(data);
2525
},

src/query-schema.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ export const queryDescriptorSchema = z.object({
5252
cacheTTL: z.number().int().positive().optional(),
5353
});
5454

55-
export type ValidatedQuery = z.infer<typeof queryDescriptorSchema>;
56-
5755
/**
5856
* Parse and validate a raw request body into a QueryDescriptor.
5957
* Throws a formatted error string on validation failure.

0 commit comments

Comments
 (0)