@@ -14,6 +14,7 @@ import {
1414} from "@acme/contracts" ;
1515import { WebSocketServer , type WebSocket } from "ws" ;
1616
17+ import { createLogger } from "./logger" ;
1718import { ProviderManager } from "./providerManager" ;
1819
1920const MIME_TYPES : Record < string , string > = {
@@ -36,12 +37,35 @@ export interface ServerOptions {
3637 cwd : string ;
3738 staticDir ?: string | undefined ;
3839 devUrl ?: string | undefined ;
40+ logWebSocketEvents ?: boolean | undefined ;
41+ }
42+
43+ function parseBooleanEnv ( value : string | undefined ) : boolean | undefined {
44+ if ( value === undefined ) return undefined ;
45+ const normalized = value . trim ( ) . toLowerCase ( ) ;
46+ if ( [ "1" , "true" , "yes" , "on" ] . includes ( normalized ) ) return true ;
47+ if ( [ "0" , "false" , "no" , "off" ] . includes ( normalized ) ) return false ;
48+ return undefined ;
3949}
4050
4151export function createServer ( options : ServerOptions ) {
42- const { port, cwd, staticDir, devUrl } = options ;
52+ const { port, cwd, staticDir, devUrl, logWebSocketEvents : explicitLogWsEvents } = options ;
4353 const providerManager = new ProviderManager ( ) ;
4454 const clients = new Set < WebSocket > ( ) ;
55+ const logger = createLogger ( "ws" ) ;
56+ const logWebSocketEvents =
57+ explicitLogWsEvents ??
58+ parseBooleanEnv ( process . env . CODETHING_LOG_WS_EVENTS ) ??
59+ Boolean ( devUrl ) ;
60+
61+ function logOutgoingPush ( push : WsPush , recipients : number ) {
62+ if ( ! logWebSocketEvents ) return ;
63+ logger . event ( "outgoing push" , {
64+ channel : push . channel ,
65+ recipients,
66+ payload : push . data ,
67+ } ) ;
68+ }
4569
4670 // Forward provider events to all connected WebSocket clients
4771 providerManager . on ( "event" , ( event ) => {
@@ -51,11 +75,14 @@ export function createServer(options: ServerOptions) {
5175 data : event ,
5276 } ;
5377 const message = JSON . stringify ( push ) ;
78+ let recipients = 0 ;
5479 for ( const client of clients ) {
5580 if ( client . readyState === client . OPEN ) {
5681 client . send ( message ) ;
82+ recipients += 1 ;
5783 }
5884 }
85+ logOutgoingPush ( push , recipients ) ;
5986 } ) ;
6087
6188 // HTTP server — serves static files or redirects to Vite dev server
@@ -129,6 +156,7 @@ export function createServer(options: ServerOptions) {
129156 channel : WS_CHANNELS . serverWelcome ,
130157 data : { cwd, projectName } ,
131158 } ;
159+ logOutgoingPush ( welcome , 1 ) ;
132160 ws . send ( JSON . stringify ( welcome ) ) ;
133161
134162 ws . on ( "message" , ( raw ) => {
0 commit comments