Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit af5e352

Browse files
committed
refactor(react): 0.9
1 parent 2766a1c commit af5e352

File tree

35 files changed

+2085
-94
lines changed

35 files changed

+2085
-94
lines changed

examples/chat-room-python/src/workers/registry.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export const chatRoom = worker({
2929
// Create and export the app
3030
export const registry = setup({
3131
workers: { chatRoom },
32+
cors: {
33+
origin: "*", // Allow all origins
34+
allowMethods: ["GET", "POST", "OPTIONS"], // Allow specific methods
35+
allowHeaders: ["Content-Type", "Authorization", "User-Agent"], // Allow specific headers
36+
},
3237
});
3338

3439
// Export type for client type checking

examples/chat-room/scripts/cli.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ async function main() {
1010

1111
// connect to chat room - now accessed via property
1212
// can still pass parameters like room
13-
const chatRoom = client.chatRoom.getOrCreate(room, {
14-
params: { room },
15-
}).connect();
13+
const chatRoom = client.chatRoom
14+
.getOrCreate(room, {
15+
params: { room },
16+
})
17+
.connect();
1618

1719
// fetch history
1820
const history = await chatRoom.getHistory();

packages/core/src/client/client.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ export interface ClientOptions {
9191
export interface QueryOptions {
9292
/** Parameters to pass to the connection. */
9393
params?: unknown;
94+
/** Signal to abort the request. */
95+
signal?: AbortSignal;
9496
}
9597

9698
/**
@@ -160,25 +162,29 @@ export interface ClientDriver {
160162
encoding: Encoding,
161163
params: unknown,
162164
name: string,
163-
...args: Args
165+
args: Args,
166+
opts: { signal?: AbortSignal } | undefined,
164167
): Promise<Response>;
165168
resolveWorkerId(
166169
c: HonoContext | undefined,
167170
workerQuery: WorkerQuery,
168171
encodingKind: Encoding,
169172
params: unknown,
173+
opts: { signal?: AbortSignal } | undefined,
170174
): Promise<string>;
171175
connectWebSocket(
172176
c: HonoContext | undefined,
173177
workerQuery: WorkerQuery,
174178
encodingKind: Encoding,
175179
params: unknown,
180+
opts: { signal?: AbortSignal } | undefined,
176181
): Promise<WebSocket>;
177182
connectSse(
178183
c: HonoContext | undefined,
179184
workerQuery: WorkerQuery,
180185
encodingKind: Encoding,
181186
params: unknown,
187+
opts: { signal?: AbortSignal } | undefined,
182188
): Promise<EventSource>;
183189
sendHttpMessage(
184190
c: HonoContext | undefined,
@@ -187,6 +193,7 @@ export interface ClientDriver {
187193
connectionId: string,
188194
connectionToken: string,
189195
message: wsToServer.ToServer,
196+
opts: { signal?: AbortSignal } | undefined,
190197
): Promise<Response>;
191198
}
192199

@@ -360,6 +367,7 @@ export class ClientRaw {
360367
createQuery,
361368
this.#encodingKind,
362369
opts?.params,
370+
opts?.signal ? { signal: opts.signal } : undefined,
363371
);
364372
logger().debug("created worker with ID", {
365373
name,
@@ -541,7 +549,7 @@ function createWorkerProxy<AD extends AnyWorkerDefinition>(
541549

542550
let method = methodCache.get(prop);
543551
if (!method) {
544-
method = (...args: unknown[]) => target.action(prop, ...args);
552+
method = (...args: unknown[]) => target.action({ name: prop, args });
545553
methodCache.set(prop, method);
546554
}
547555
return method;
@@ -580,7 +588,7 @@ function createWorkerProxy<AD extends AnyWorkerDefinition>(
580588
configurable: true,
581589
enumerable: false,
582590
writable: false,
583-
value: (...args: unknown[]) => target.action(prop, ...args),
591+
value: (...args: unknown[]) => target.action({ name: prop, args }),
584592
};
585593
}
586594
return undefined;

packages/core/src/client/http-client-driver.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export function createHttpClientDriver(managerEndpoint: string): ClientDriver {
5252
encoding: Encoding,
5353
params: unknown,
5454
name: string,
55-
...args: Args
55+
args: Args,
56+
opts: { signal?: AbortSignal } | undefined,
5657
): Promise<Response> => {
5758
logger().debug("worker handle action", {
5859
name,
@@ -73,6 +74,7 @@ export function createHttpClientDriver(managerEndpoint: string): ClientDriver {
7374
},
7475
body: { a: args } satisfies ActionRequest,
7576
encoding: encoding,
77+
signal: opts?.signal,
7678
},
7779
);
7880

packages/core/src/client/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface HttpRequestOpts<Body> {
3131
body?: Body;
3232
encoding: Encoding;
3333
skipParseResponse?: boolean;
34+
signal?: AbortSignal;
3435
customFetch?: (req: Request) => Promise<Response>;
3536
}
3637

@@ -75,6 +76,7 @@ export async function sendHttpRequest<
7576
"User-Agent": httpUserAgent(),
7677
},
7778
body: bodyData,
79+
signal: opts.signal,
7880
}),
7981
);
8082
} catch (error) {

packages/core/src/client/worker-conn.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
type WebSocketMessage as ConnMessage,
2424
messageLength,
2525
serializeWithEncoding,
26-
WebSocketMessage,
2726
} from "./utils";
2827
import {
2928
HEADER_WORKER_ID,
@@ -64,6 +63,7 @@ export type WorkerErrorCallback = (error: errors.WorkerError) => void;
6463

6564
export interface SendHttpMessageOpts {
6665
ephemeral: boolean;
66+
signal?: AbortSignal;
6767
}
6868

6969
export 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 = () => {
@@ -659,6 +665,7 @@ enc
659665
this.#connectionId,
660666
this.#connectionToken,
661667
message,
668+
opts?.signal ? { signal: opts.signal } : undefined,
662669
);
663670

664671
if (!res.ok) {

packages/core/src/client/worker-handle.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,23 @@ export class WorkerHandleRaw {
5252
* @see {@link WorkerHandle}
5353
* @template Args - The type of arguments to pass to the action function.
5454
* @template Response - The type of the response returned by the action function.
55-
* @param {string} name - The name of the action function to call.
56-
* @param {...Args} args - The arguments to pass to the action function.
57-
* @returns {Promise<Response>} - A promise that resolves to the response of the action function.
5855
*/
59-
async action<Args extends Array<unknown> = unknown[], Response = unknown>(
60-
name: string,
61-
...args: Args
62-
): Promise<Response> {
56+
async action<
57+
Args extends Array<unknown> = unknown[],
58+
Response = unknown,
59+
>(opts: {
60+
name: string;
61+
args: Args;
62+
signal?: AbortSignal;
63+
}): Promise<Response> {
6364
return await this.#driver.action<Args, Response>(
6465
undefined,
6566
this.#workerQuery,
6667
this.#encodingKind,
6768
this.#params,
68-
name,
69-
...args,
69+
opts.name,
70+
opts.args,
71+
{ signal: opts.signal },
7072
);
7173
}
7274

@@ -99,7 +101,7 @@ export class WorkerHandleRaw {
99101
*
100102
* @returns {Promise<string>} - A promise that resolves to the worker's ID
101103
*/
102-
async resolve(): Promise<string> {
104+
async resolve({ signal }: { signal?: AbortSignal } = {}): Promise<string> {
103105
if (
104106
"getForKey" in this.#workerQuery ||
105107
"getOrCreateForKey" in this.#workerQuery
@@ -110,6 +112,7 @@ export class WorkerHandleRaw {
110112
this.#workerQuery,
111113
this.#encodingKind,
112114
this.#params,
115+
signal ? { signal } : undefined,
113116
);
114117
this.#workerQuery = { getForId: { workerId } };
115118
return workerId;

packages/core/src/driver-test-suite/test-inline-client-driver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ export function createTestInlineClientDriver(
3030
encoding: Encoding,
3131
params: unknown,
3232
name: string,
33-
...args: Args
33+
args: Args
3434
): Promise<Response> => {
3535
return makeInlineRequest<Response>(
3636
endpoint,
3737
encoding,
3838
transport,
3939
"action",
40-
[undefined, workerQuery, encoding, params, name, ...args],
40+
[undefined, workerQuery, encoding, params, name, args],
4141
);
4242
},
4343

packages/core/src/inline-client-driver/mod.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export function createInlineClientDriver(
5555
encoding: Encoding,
5656
params: unknown,
5757
actionName: string,
58-
...args: Args
58+
args: Args,
59+
opts: { signal?: AbortSignal },
5960
): Promise<Response> => {
6061
try {
6162
// Get the worker ID
@@ -115,6 +116,7 @@ export function createInlineClientDriver(
115116
undefined,
116117
workerId,
117118
),
119+
signal: opts?.signal,
118120
});
119121

120122
return responseData.o as Response;

packages/core/src/manager/router.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ export function createManagerRouter(
429429
workerQuery,
430430
encodingKind,
431431
params,
432+
undefined,
432433
);
433434

434435
// Store a reference to the resolved WebSocket

0 commit comments

Comments
 (0)