@@ -3,11 +3,18 @@ import type { RunnerConfig, ActorConfig } from "@rivetkit/engine-runner";
33import  WebSocket  from  "ws" ; 
44import  {  serve  }  from  "@hono/node-server" ; 
55
6- const  INTERNAL_SERVER_PORT  =  process . env . INTERNAL_SERVER_PORT  ? Number ( process . env . INTERNAL_SERVER_PORT )  : 5051 ; 
7- const  RIVET_NAMESPACE  =  process . env . RIVET_NAMESPACE  ??  'default' ; 
8- const  RIVET_RUNNER_KEY  =  process . env . RIVET_RUNNER_KEY  ??  `key-${ Math . floor ( Math . random ( )  *  10000 ) }  ` ; 
9- const  RIVET_RUNNER_VERSION  =  process . env . RIVET_RUNNER_VERSION  ? Number ( process . env . RIVET_RUNNER_VERSION )  : 1 ; 
10- const  RIVET_RUNNER_TOTAL_SLOTS  =  process . env . RIVET_RUNNER_TOTAL_SLOTS  ? Number ( process . env . RIVET_RUNNER_TOTAL_SLOTS )  : 100 ; 
6+ const  INTERNAL_SERVER_PORT  =  process . env . INTERNAL_SERVER_PORT 
7+ 	? Number ( process . env . INTERNAL_SERVER_PORT ) 
8+ 	: 5051 ; 
9+ const  RIVET_NAMESPACE  =  process . env . RIVET_NAMESPACE  ??  "default" ; 
10+ const  RIVET_RUNNER_KEY  = 
11+ 	process . env . RIVET_RUNNER_KEY  ??  `key-${ Math . floor ( Math . random ( )  *  10000 ) }  ` ; 
12+ const  RIVET_RUNNER_VERSION  =  process . env . RIVET_RUNNER_VERSION 
13+ 	? Number ( process . env . RIVET_RUNNER_VERSION ) 
14+ 	: 1 ; 
15+ const  RIVET_RUNNER_TOTAL_SLOTS  =  process . env . RIVET_RUNNER_TOTAL_SLOTS 
16+ 	? Number ( process . env . RIVET_RUNNER_TOTAL_SLOTS ) 
17+ 	: 100 ; 
1118const  RIVET_ENDPOINT  =  process . env . RIVET_ENDPOINT  ??  "http://localhost:6420" ; 
1219
1320let  runnerStarted  =  Promise . withResolvers ( ) ; 
@@ -20,18 +27,22 @@ const actorWebSockets = new Map<string, WebSocket>();
2027serve ( { 
2128	fetch : async  ( request : Request )  =>  { 
2229		const  url  =  new  URL ( request . url ) ; 
23- 		if  ( url . pathname  ==  ' /wait-ready' )  { 
30+ 		if  ( url . pathname  ==  " /wait-ready" )  { 
2431			await  runnerStarted . promise ; 
25- 			return  new  Response ( JSON . stringify ( runner ?. runnerId ) ,  {  status : 200  } ) ; 
26- 		}  else  if  ( url . pathname  ==  '/has-actor' )  { 
27- 			let  actorIdQuery  =  url . searchParams . get ( 'actor' ) ; 
28- 			let  generationQuery  =  url . searchParams . get ( 'generation' ) ; 
29- 			let  generation  =  generationQuery  ? Number ( generationQuery )  : undefined ; 
32+ 			return  new  Response ( JSON . stringify ( runner ?. runnerId ) ,  { 
33+ 				status : 200 , 
34+ 			} ) ; 
35+ 		}  else  if  ( url . pathname  ==  "/has-actor" )  { 
36+ 			let  actorIdQuery  =  url . searchParams . get ( "actor" ) ; 
37+ 			let  generationQuery  =  url . searchParams . get ( "generation" ) ; 
38+ 			let  generation  =  generationQuery 
39+ 				? Number ( generationQuery ) 
40+ 				: undefined ; 
3041
3142			if  ( ! actorIdQuery  ||  ! runner ?. hasActor ( actorIdQuery ,  generation ) )  { 
3243				return  new  Response ( undefined ,  {  status : 404  } ) ; 
3344			} 
34- 		}  else  if  ( url . pathname  ==  ' /shutdown' )  { 
45+ 		}  else  if  ( url . pathname  ==  " /shutdown" )  { 
3546			await  runner ?. shutdown ( true ) ; 
3647		} 
3748
@@ -56,9 +67,11 @@ const config: RunnerConfig = {
5667	onConnected : ( )  =>  { 
5768		runnerStarted . resolve ( undefined ) ; 
5869	} , 
59- 	onDisconnected : ( )  =>  {   } , 
70+ 	onDisconnected : ( )  =>  { } , 
6071	fetch : async  ( actorId : string ,  request : Request )  =>  { 
61- 		console . log ( `[TEST-RUNNER] Fetch called for actor ${ actorId }  , URL: ${ request . url }  ` ) ; 
72+ 		console . log ( 
73+ 			`[TEST-RUNNER] Fetch called for actor ${ actorId }  , URL: ${ request . url }  ` , 
74+ 		) ; 
6275		const  url  =  new  URL ( request . url ) ; 
6376		if  ( url . pathname  ===  "/ping" )  { 
6477			// Return the actor ID in response 
@@ -68,13 +81,10 @@ const config: RunnerConfig = {
6881				timestamp : Date . now ( ) , 
6982			} ; 
7083			console . log ( `[TEST-RUNNER] Returning ping response:` ,  responseData ) ; 
71- 			return  new  Response ( 
72- 				JSON . stringify ( responseData ) , 
73- 				{ 
74- 					status : 200 , 
75- 					headers : {  "Content-Type" : "application/json"  } , 
76- 				} , 
77- 			) ; 
84+ 			return  new  Response ( JSON . stringify ( responseData ) ,  { 
85+ 				status : 200 , 
86+ 				headers : {  "Content-Type" : "application/json"  } , 
87+ 			} ) ; 
7888		} 
7989
8090		return  new  Response ( "ok" ,  {  status : 200  } ) ; 
@@ -84,33 +94,22 @@ const config: RunnerConfig = {
8494		_generation : number , 
8595		_config : ActorConfig , 
8696	)  =>  { 
87- 		console . log ( 
88- 			`Actor ${ _actorId }   started (generation ${ _generation }  )` , 
89- 		) ; 
97+ 		console . log ( `Actor ${ _actorId }   started (generation ${ _generation }  )` ) ; 
9098		startedRef . current . resolve ( undefined ) ; 
9199	} , 
92100	onActorStop : async  ( _actorId : string ,  _generation : number )  =>  { 
93- 		console . log ( 
94- 			`Actor ${ _actorId }   stopped (generation ${ _generation }  )` , 
95- 		) ; 
101+ 		console . log ( `Actor ${ _actorId }   stopped (generation ${ _generation }  )` ) ; 
96102		stoppedRef . current . resolve ( undefined ) ; 
97103	} , 
98- 	websocket : async  ( 
99- 		actorId : string , 
100- 		ws : WebSocket , 
101- 		request : Request , 
102- 	)  =>  { 
104+ 	websocket : async  ( actorId : string ,  ws : WebSocket ,  request : Request )  =>  { 
103105		console . log ( `WebSocket connected for actor ${ actorId }  ` ) ; 
104106		websocketOpen . resolve ( undefined ) ; 
105107		actorWebSockets . set ( actorId ,  ws ) ; 
106108
107109		// Echo server - send back any messages received 
108110		ws . addEventListener ( "message" ,  ( event )  =>  { 
109111			const  data  =  event . data ; 
110- 			console . log ( 
111- 				`WebSocket message from actor ${ actorId }  :` , 
112- 				data , 
113- 			) ; 
112+ 			console . log ( `WebSocket message from actor ${ actorId }  :` ,  data ) ; 
114113			ws . send ( `Echo: ${ data }  ` ) ; 
115114		} ) ; 
116115
0 commit comments