@@ -23,7 +23,6 @@ import {
2323 type WebSocketMessage as ConnMessage ,
2424 messageLength ,
2525 serializeWithEncoding ,
26- WebSocketMessage ,
2726} from "./utils" ;
2827import {
2928 HEADER_WORKER_ID ,
@@ -64,6 +63,7 @@ export type WorkerErrorCallback = (error: errors.WorkerError) => void;
6463
6564export interface SendHttpMessageOpts {
6665 ephemeral : boolean ;
66+ signal ?: AbortSignal ;
6767}
6868
6969export type ConnTransport = { websocket : WebSocket } | { sse : EventSource } ;
@@ -152,26 +152,30 @@ export class WorkerConnRaw {
152152 * @param {...Args } args - The arguments to pass to the action function.
153153 * @returns {Promise<Response> } - A promise that resolves to the response of the action function.
154154 */
155- async action < Args extends Array < unknown > = unknown [ ] , Response = unknown > (
156- name : string ,
157- ...args : Args
158- ) : Promise < Response > {
159- logger ( ) . debug ( "action" , { name, args } ) ;
155+ async action <
156+ Args extends Array < unknown > = unknown [ ] ,
157+ Response = unknown ,
158+ > ( opts : {
159+ name : string ;
160+ args : Args ;
161+ signal ?: AbortSignal ;
162+ } ) : Promise < Response > {
163+ logger ( ) . debug ( "action" , { name : opts . name , args : opts . args } ) ;
160164
161165 // If we have an active connection, use the websockactionId
162166 const actionId = this . #actionIdCounter;
163167 this . #actionIdCounter += 1 ;
164168
165169 const { promise, resolve, reject } =
166170 Promise . withResolvers < wsToClient . ActionResponse > ( ) ;
167- this . #actionsInFlight. set ( actionId , { name, resolve, reject } ) ;
171+ this . #actionsInFlight. set ( actionId , { name : opts . name , resolve, reject } ) ;
168172
169173 this . #sendMessage( {
170174 b : {
171175 ar : {
172176 i : actionId ,
173- n : name ,
174- a : args ,
177+ n : opts . name ,
178+ a : opts . args ,
175179 } ,
176180 } ,
177181 } satisfies wsToServer . ToServer ) ;
@@ -255,12 +259,13 @@ enc
255259 }
256260 }
257261
258- async #connectWebSocket( ) {
262+ async #connectWebSocket( { signal } : { signal ?: AbortSignal } = { } ) {
259263 const ws = await this . #driver. connectWebSocket (
260264 undefined ,
261265 this . #workerQuery,
262266 this . #encodingKind,
263267 this . #params,
268+ signal ? { signal } : undefined ,
264269 ) ;
265270 this . #transport = { websocket : ws } ;
266271 ws . onopen = ( ) => {
@@ -277,12 +282,13 @@ enc
277282 } ;
278283 }
279284
280- async #connectSse( ) {
285+ async #connectSse( { signal } : { signal ?: AbortSignal } = { } ) {
281286 const eventSource = await this . #driver. connectSse (
282287 undefined ,
283288 this . #workerQuery,
284289 this . #encodingKind,
285290 this . #params,
291+ signal ? { signal } : undefined ,
286292 ) ;
287293 this . #transport = { sse : eventSource } ;
288294 eventSource . onopen = ( ) => {
659665 this . #connectionId,
660666 this . #connectionToken,
661667 message ,
668+ opts ?. signal ? { signal : opts . signal } : undefined ,
662669 ) ;
663670
664671 if ( ! res . ok ) {
0 commit comments