@@ -3,11 +3,18 @@ import type { RunnerConfig, ActorConfig } from "@rivetkit/engine-runner";
3
3
import WebSocket from "ws" ;
4
4
import { serve } from "@hono/node-server" ;
5
5
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 ;
11
18
const RIVET_ENDPOINT = process . env . RIVET_ENDPOINT ?? "http://localhost:6420" ;
12
19
13
20
let runnerStarted = Promise . withResolvers ( ) ;
@@ -20,18 +27,22 @@ const actorWebSockets = new Map<string, WebSocket>();
20
27
serve ( {
21
28
fetch : async ( request : Request ) => {
22
29
const url = new URL ( request . url ) ;
23
- if ( url . pathname == ' /wait-ready' ) {
30
+ if ( url . pathname == " /wait-ready" ) {
24
31
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 ;
30
41
31
42
if ( ! actorIdQuery || ! runner ?. hasActor ( actorIdQuery , generation ) ) {
32
43
return new Response ( undefined , { status : 404 } ) ;
33
44
}
34
- } else if ( url . pathname == ' /shutdown' ) {
45
+ } else if ( url . pathname == " /shutdown" ) {
35
46
await runner ?. shutdown ( true ) ;
36
47
}
37
48
@@ -56,9 +67,11 @@ const config: RunnerConfig = {
56
67
onConnected : ( ) => {
57
68
runnerStarted . resolve ( undefined ) ;
58
69
} ,
59
- onDisconnected : ( ) => { } ,
70
+ onDisconnected : ( ) => { } ,
60
71
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
+ ) ;
62
75
const url = new URL ( request . url ) ;
63
76
if ( url . pathname === "/ping" ) {
64
77
// Return the actor ID in response
@@ -68,13 +81,10 @@ const config: RunnerConfig = {
68
81
timestamp : Date . now ( ) ,
69
82
} ;
70
83
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
+ } ) ;
78
88
}
79
89
80
90
return new Response ( "ok" , { status : 200 } ) ;
@@ -84,33 +94,22 @@ const config: RunnerConfig = {
84
94
_generation : number ,
85
95
_config : ActorConfig ,
86
96
) => {
87
- console . log (
88
- `Actor ${ _actorId } started (generation ${ _generation } )` ,
89
- ) ;
97
+ console . log ( `Actor ${ _actorId } started (generation ${ _generation } )` ) ;
90
98
startedRef . current . resolve ( undefined ) ;
91
99
} ,
92
100
onActorStop : async ( _actorId : string , _generation : number ) => {
93
- console . log (
94
- `Actor ${ _actorId } stopped (generation ${ _generation } )` ,
95
- ) ;
101
+ console . log ( `Actor ${ _actorId } stopped (generation ${ _generation } )` ) ;
96
102
stoppedRef . current . resolve ( undefined ) ;
97
103
} ,
98
- websocket : async (
99
- actorId : string ,
100
- ws : WebSocket ,
101
- request : Request ,
102
- ) => {
104
+ websocket : async ( actorId : string , ws : WebSocket , request : Request ) => {
103
105
console . log ( `WebSocket connected for actor ${ actorId } ` ) ;
104
106
websocketOpen . resolve ( undefined ) ;
105
107
actorWebSockets . set ( actorId , ws ) ;
106
108
107
109
// Echo server - send back any messages received
108
110
ws . addEventListener ( "message" , ( event ) => {
109
111
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 ) ;
114
113
ws . send ( `Echo: ${ data } ` ) ;
115
114
} ) ;
116
115
0 commit comments