@@ -152,147 +152,35 @@ export class ActorConnRaw {
152152
153153 logger ( ) . debug ( "action" , { name, args } ) ;
154154
155- // Check if we have an active websocket connection
156- if ( this . #transport) {
157- // If we have an active connection, use the websocket RPC
158- const rpcId = this . #rpcIdCounter;
159- this . #rpcIdCounter += 1 ;
160-
161- const { promise, resolve, reject } =
162- Promise . withResolvers < wsToClient . RpcResponseOk > ( ) ;
163- this . #rpcInFlight. set ( rpcId , { name, resolve, reject } ) ;
164-
165- this . #sendMessage( {
166- b : {
167- rr : {
168- i : rpcId ,
169- n : name ,
170- a : args ,
171- } ,
155+ // If we have an active connection, use the websocket RPC
156+ const rpcId = this . #rpcIdCounter;
157+ this . #rpcIdCounter += 1 ;
158+
159+ const { promise, resolve, reject } =
160+ Promise . withResolvers < wsToClient . RpcResponseOk > ( ) ;
161+ this . #rpcInFlight. set ( rpcId , { name, resolve, reject } ) ;
162+
163+ this . #sendMessage( {
164+ b : {
165+ rr : {
166+ i : rpcId ,
167+ n : name ,
168+ a : args ,
172169 } ,
173- } satisfies wsToServer . ToServer ) ;
174-
175- // TODO: Throw error if disconnect is called
176-
177- const { i : responseId , o : output } = await promise ;
178- if ( responseId !== rpcId )
179- throw new Error (
180- `Request ID ${ rpcId } does not match response ID ${ responseId } ` ,
181- ) ;
182-
183- return output as Response ;
184- } else {
185- // If no websocket connection, use HTTP RPC via manager
186- try {
187- // Get the manager endpoint from the endpoint provided
188- const actorQueryStr = encodeURIComponent (
189- JSON . stringify ( this . actorQuery ) ,
190- ) ;
191-
192- const url = `${ this . endpoint } /actors/rpc/${ name } ?query=${ actorQueryStr } ` ;
193- logger ( ) . debug ( "http rpc: request" , {
194- url,
195- name,
196- } ) ;
170+ } ,
171+ } satisfies wsToServer . ToServer ) ;
197172
198- try {
199- const response = await fetch ( url , {
200- method : "POST" ,
201- headers : {
202- "Content-Type" : "application/json" ,
203- } ,
204- body : JSON . stringify ( {
205- a : args ,
206- } ) ,
207- } ) ;
173+ // TODO: Throw error if disconnect is called
208174
209- logger ( ) . debug ( "http rpc: response" , {
210- status : response . status ,
211- ok : response . ok ,
212- } ) ;
175+ const { i : responseId , o : output } = await promise ;
176+ if ( responseId !== rpcId )
177+ throw new Error (
178+ `Request ID ${ rpcId } does not match response ID ${ responseId } ` ,
179+ ) ;
213180
214- if ( ! response . ok ) {
215- try {
216- const errorData = await response . json ( ) ;
217- logger ( ) . error ( "http rpc error response" , { errorData } ) ;
218- throw new errors . ActionError (
219- errorData . c || "RPC_ERROR" ,
220- errorData . m || "RPC call failed" ,
221- errorData . md ,
222- ) ;
223- } catch ( parseError ) {
224- // If response is not JSON, get it as text and throw generic error
225- const errorText = await response . text ( ) ;
226- logger ( ) . error ( "http rpc: error parsing response" , {
227- errorText,
228- } ) ;
229- throw new errors . ActionError (
230- "RPC_ERROR" ,
231- `RPC call failed: ${ errorText } ` ,
232- { } ,
233- ) ;
234- }
235- }
236-
237- // Clone response to avoid consuming it
238- const responseClone = response . clone ( ) ;
239- const responseText = await responseClone . text ( ) ;
240-
241- // Parse response body
242- try {
243- const responseData = JSON . parse ( responseText ) ;
244- return responseData . o as Response ;
245- } catch ( parseError ) {
246- logger ( ) . error ( "http rpc: error parsing json" , {
247- parseError,
248- } ) ;
249- throw new errors . ActionError (
250- "RPC_ERROR" ,
251- `Failed to parse response: ${ parseError } ` ,
252- { responseText } ,
253- ) ;
254- }
255- } catch ( fetchError ) {
256- logger ( ) . error ( "http rpc: fetch error" , {
257- error : fetchError ,
258- } ) ;
259- throw new errors . ActionError (
260- "RPC_ERROR" ,
261- `Fetch failed: ${ fetchError } ` ,
262- { cause : fetchError } ,
263- ) ;
264- }
265- } catch ( error ) {
266- if ( error instanceof errors . ActionError ) {
267- throw error ;
268- }
269- throw new errors . ActionError (
270- "RPC_ERROR" ,
271- `Failed to execute RPC ${ name } : ${ error } ` ,
272- { cause : error } ,
273- ) ;
274- }
275- }
181+ return output as Response ;
276182 }
277183
278- //async #rpcHttp<Args extends Array<unknown> = unknown[], Response = unknown>(name: string, ...args: Args): Promise<Response> {
279- // const origin = `${resolved.isTls ? "https": "http"}://${resolved.publicHostname}:${resolved.publicPort}`;
280- // const url = `${origin}/rpc/${encodeURIComponent(name)}`;
281- // const res = await fetch(url, {
282- // method: "POST",
283- // // TODO: Import type from protocol
284- // body: JSON.stringify({
285- // args,
286- // })
287- // });
288- // if (!res.ok) {
289- // throw new Error(`RPC error (${res.statusText}):\n${await res.text()}`);
290- // }
291- // // TODO: Import type from protocol
292- // const resJson: httpRpc.ResponseOk<Response> = await res.json();
293- // return resJson.output;
294- //}
295-
296184 /**
297185 * Do not call this directly.
298186enc
0 commit comments