Skip to content

Commit ed37d98

Browse files
committed
feat(rivetkit): expose getGatewayUrl
1 parent a9f131b commit ed37d98

File tree

7 files changed

+58
-10
lines changed

7 files changed

+58
-10
lines changed

rivetkit-typescript/packages/cloudflare-workers/src/manager-driver.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
135135
return webSocket as unknown as UniversalWebSocket;
136136
}
137137

138+
async buildGatewayUrl(actorId: string): Promise<string> {
139+
return `http://actor/gateway/${encodeURIComponent(actorId)}`;
140+
}
141+
138142
async proxyRequest(
139143
c: HonoContext<{ Bindings: Bindings }>,
140144
actorRequest: Request,

rivetkit-typescript/packages/rivetkit/src/client/actor-handle.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ export class ActorHandleRaw {
243243
*
244244
* @returns {Promise<string>} - A promise that resolves to the actor's ID
245245
*/
246+
async getGatewayUrl(): Promise<string> {
247+
const { actorId } = await queryActor(
248+
undefined,
249+
this.#actorQuery,
250+
this.#driver,
251+
);
252+
return await this.#driver.buildGatewayUrl(actorId);
253+
}
254+
246255
async resolve({ signal }: { signal?: AbortSignal } = {}): Promise<string> {
247256
if (
248257
"getForKey" in this.#actorQuery ||

rivetkit-typescript/packages/rivetkit/src/drivers/file-system/manager.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ export class FileSystemManagerDriver implements ManagerDriver {
147147
return upgradeWebSocket(() => wsHandler)(c, noopNext());
148148
}
149149

150+
async buildGatewayUrl(actorId: string): Promise<string> {
151+
const port = this.#config.managerPort ?? 6420;
152+
return `http://127.0.0.1:${port}/gateway/${encodeURIComponent(actorId)}`;
153+
}
154+
150155
async getForId({
151156
actorId,
152157
}: GetForIdInput): Promise<ActorOutput | undefined> {

rivetkit-typescript/packages/rivetkit/src/manager/driver.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ export interface ManagerDriver {
3232
params: unknown,
3333
): Promise<Response>;
3434

35+
/**
36+
* Build a public gateway URL for a specific actor.
37+
*
38+
* This lives on the driver because the base endpoint varies by runtime.
39+
*/
40+
buildGatewayUrl(actorId: string): Promise<string>;
41+
3542
displayInformation(): ManagerDisplayInformation;
3643

3744
extraStartupLog?: () => Record<string, unknown>;

rivetkit-typescript/packages/rivetkit/src/remote-manager-driver/actor-http-client.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ClientConfig } from "@/client/config";
22
import { HEADER_RIVET_TOKEN } from "@/common/actor-router-consts";
3-
import { combineUrlPath } from "@/utils";
3+
import { buildActorGatewayUrl } from "./actor-websocket-client";
44
import { getEndpoint } from "./api-utils";
55

66
export async function sendHttpRequestToActor(
@@ -11,9 +11,11 @@ export async function sendHttpRequestToActor(
1111
// Route through guard port
1212
const url = new URL(actorRequest.url);
1313
const endpoint = getEndpoint(runConfig);
14-
const guardUrl = combineUrlPath(
14+
const guardUrl = buildActorGatewayUrl(
1515
endpoint,
16-
`/gateway/${actorId}${url.pathname}${url.search}`,
16+
actorId,
17+
runConfig.token,
18+
`${url.pathname}${url.search}`,
1719
);
1820

1921
// Handle body properly based on method and presence

rivetkit-typescript/packages/rivetkit/src/remote-manager-driver/actor-websocket-client.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ import { combineUrlPath } from "@/utils";
1313
import { getEndpoint } from "./api-utils";
1414
import { logger } from "./log";
1515

16+
export function buildActorGatewayUrl(
17+
endpoint: string,
18+
actorId: string,
19+
token: string | undefined,
20+
path = "",
21+
): string {
22+
const tokenSegment =
23+
token !== undefined ? `@${encodeURIComponent(token)}` : "";
24+
const gatewayPath = `/gateway/${encodeURIComponent(actorId)}${tokenSegment}${path}`;
25+
return combineUrlPath(endpoint, gatewayPath);
26+
}
27+
1628
export async function openWebSocketToActor(
1729
runConfig: ClientConfig,
1830
path: string,
@@ -24,13 +36,12 @@ export async function openWebSocketToActor(
2436

2537
// WebSocket connections go through guard
2638
const endpoint = getEndpoint(runConfig);
27-
let gatewayPath;
28-
if (runConfig.token !== undefined) {
29-
gatewayPath = `/gateway/${encodeURIComponent(actorId)}@${encodeURIComponent(runConfig.token)}${path}`;
30-
} else {
31-
gatewayPath = `/gateway/${encodeURIComponent(actorId)}${path}`;
32-
}
33-
const guardUrl = combineUrlPath(endpoint, gatewayPath);
39+
const guardUrl = buildActorGatewayUrl(
40+
endpoint,
41+
actorId,
42+
runConfig.token,
43+
path,
44+
);
3445

3546
logger().debug({
3647
msg: "opening websocket to actor via guard",

rivetkit-typescript/packages/rivetkit/src/remote-manager-driver/mod.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { combineUrlPath, type GetUpgradeWebSocket } from "@/utils";
2121
import { getNextPhase } from "@/utils/env-vars";
2222
import { sendHttpRequestToActor } from "./actor-http-client";
2323
import {
24+
buildActorGatewayUrl,
2425
buildWebSocketProtocols,
2526
openWebSocketToActor,
2627
} from "./actor-websocket-client";
@@ -309,6 +310,15 @@ export class RemoteManagerDriver implements ManagerDriver {
309310
);
310311
}
311312

313+
async buildGatewayUrl(actorId: string): Promise<string> {
314+
if (this.#metadataPromise) {
315+
await this.#metadataPromise;
316+
}
317+
318+
const endpoint = getEndpoint(this.#config);
319+
return buildActorGatewayUrl(endpoint, actorId, this.#config.token);
320+
}
321+
312322
async proxyRequest(
313323
_c: HonoContext,
314324
actorRequest: Request,

0 commit comments

Comments
 (0)