Skip to content

Commit 72edb6d

Browse files
committed
chore(core): add token & optional headers to client
1 parent c313f20 commit 72edb6d

File tree

6 files changed

+53
-7
lines changed

6 files changed

+53
-7
lines changed

packages/rivetkit/src/client/config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ export const ClientConfigSchema = z.object({
1414
})
1515
.default({}),
1616

17+
token: z
18+
.string()
19+
.nullable()
20+
.default(() => getEnvUniversal("RIVET_TOKEN") ?? null),
21+
22+
headers: z.record(z.string()).optional().default({}),
23+
1724
/** Endpoint to connect to the Rivet engine. Can be configured via RIVET_ENGINE env var. */
1825
endpoint: z
1926
.string()

packages/rivetkit/src/common/actor-router-consts.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export const HEADER_CONN_ID = "x-rivet-conn";
1818

1919
export const HEADER_CONN_TOKEN = "x-rivet-conn-token";
2020

21+
export const HEADER_RIVET_TOKEN = "x-rivet-token";
22+
2123
// MARK: Manager Gateway Headers
2224
export const HEADER_RIVET_TARGET = "x-rivet-target";
2325
export const HEADER_RIVET_ACTOR = "x-rivet-actor";
@@ -29,6 +31,7 @@ export const WS_PROTOCOL_TARGET = "rivet_target.";
2931
export const WS_PROTOCOL_ACTOR = "rivet_actor.";
3032
export const WS_PROTOCOL_ENCODING = "rivet_encoding.";
3133
export const WS_PROTOCOL_CONN_PARAMS = "rivet_conn_params.";
34+
export const WS_PROTOCOL_TOKEN = "rivet_token.";
3235

3336
// MARK: WebSocket Inline Test Protocol Prefixes
3437
export const WS_PROTOCOL_TRANSPORT = "test_transport.";
@@ -50,4 +53,5 @@ export const ALLOWED_PUBLIC_HEADERS = [
5053
HEADER_CONN_TOKEN,
5154
HEADER_RIVET_TARGET,
5255
HEADER_RIVET_ACTOR,
56+
HEADER_RIVET_TOKEN,
5357
];

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import type { ClientConfig } from "@/client/config";
2+
import {
3+
HEADER_RIVET_ACTOR,
4+
HEADER_RIVET_TARGET,
5+
HEADER_RIVET_TOKEN,
6+
} from "@/common/actor-router-consts";
27
import { combineUrlPath } from "@/utils";
38
import { getEndpoint } from "./api-utils";
49

@@ -14,7 +19,11 @@ export async function sendHttpRequestToActor(
1419

1520
// Handle body properly based on method and presence
1621
let bodyToSend: ArrayBuffer | null = null;
17-
const guardHeaders = buildGuardHeadersForHttp(actorRequest, actorId);
22+
const guardHeaders = buildGuardHeadersForHttp(
23+
runConfig,
24+
actorRequest,
25+
actorId,
26+
);
1827

1928
if (actorRequest.method !== "GET" && actorRequest.method !== "HEAD") {
2029
if (actorRequest.bodyUsed) {
@@ -53,6 +62,7 @@ function mutableResponse(fetchRes: Response): Response {
5362
}
5463

5564
function buildGuardHeadersForHttp(
65+
runConfig: ClientConfig,
5666
actorRequest: Request,
5767
actorId: string,
5868
): Headers {
@@ -61,9 +71,15 @@ function buildGuardHeadersForHttp(
6171
for (const [key, value] of actorRequest.headers.entries()) {
6272
headers.set(key, value);
6373
}
74+
// Add extra headers from config
75+
for (const [key, value] of Object.entries(runConfig.headers)) {
76+
headers.set(key, value);
77+
}
6478
// Add guard-specific headers
65-
headers.set("x-rivet-target", "actor");
66-
headers.set("x-rivet-actor", actorId);
67-
headers.set("x-rivet-port", "main");
79+
headers.set(HEADER_RIVET_TARGET, "actor");
80+
headers.set(HEADER_RIVET_ACTOR, actorId);
81+
if (runConfig.token) {
82+
headers.set(HEADER_RIVET_TOKEN, runConfig.token);
83+
}
6884
return headers;
6985
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
WS_PROTOCOL_ENCODING,
88
WS_PROTOCOL_STANDARD as WS_PROTOCOL_RIVETKIT,
99
WS_PROTOCOL_TARGET,
10+
WS_PROTOCOL_TOKEN,
1011
} from "@/common/actor-router-consts";
1112
import { importWebSocket } from "@/common/websocket";
1213
import type { Encoding, UniversalWebSocket } from "@/mod";
@@ -37,7 +38,7 @@ export async function openWebSocketToActor(
3738
// Create WebSocket connection
3839
const ws = new WebSocket(
3940
guardUrl,
40-
buildWebSocketProtocols(actorId, encoding, params),
41+
buildWebSocketProtocols(runConfig, actorId, encoding, params),
4142
);
4243

4344
// Set binary type to arraybuffer for proper encoding support
@@ -49,6 +50,7 @@ export async function openWebSocketToActor(
4950
}
5051

5152
export function buildWebSocketProtocols(
53+
runConfig: ClientConfig,
5254
actorId: string,
5355
encoding: Encoding,
5456
params?: unknown,
@@ -58,6 +60,9 @@ export function buildWebSocketProtocols(
5860
protocols.push(`${WS_PROTOCOL_TARGET}actor`);
5961
protocols.push(`${WS_PROTOCOL_ACTOR}${actorId}`);
6062
protocols.push(`${WS_PROTOCOL_ENCODING}${encoding}`);
63+
if (runConfig.token) {
64+
protocols.push(`${WS_PROTOCOL_TOKEN}${runConfig.token}`);
65+
}
6166
if (params) {
6267
protocols.push(
6368
`${WS_PROTOCOL_CONN_PARAMS}${encodeURIComponent(JSON.stringify(params))}`,

packages/rivetkit/src/remote-manager-driver/api-utils.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,19 @@ export async function apiCall<TInput = unknown, TOutput = unknown>(
3333

3434
logger().debug({ msg: "making api call", method, url });
3535

36+
const headers: Record<string, string> = {
37+
...config.headers,
38+
};
39+
40+
// Add Authorization header if token is provided
41+
if (config.token) {
42+
headers.Authorization = `Bearer ${config.token}`;
43+
}
44+
3645
return await sendHttpRequest<TInput, TOutput>({
3746
method,
3847
url,
39-
headers: {},
48+
headers,
4049
body,
4150
encoding: "json",
4251
skipParseResponse: false,

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,12 @@ export class RemoteManagerDriver implements ManagerDriver {
246246
});
247247

248248
// Build protocols
249-
const protocols = buildWebSocketProtocols(actorId, encoding, params);
249+
const protocols = buildWebSocketProtocols(
250+
this.#config,
251+
actorId,
252+
encoding,
253+
params,
254+
);
250255
const args = await createWebSocketProxy(c, wsGuardUrl, protocols);
251256

252257
return await upgradeWebSocket(() => args)(c, noopNext());

0 commit comments

Comments
 (0)