Skip to content

Commit f5ebdf3

Browse files
committed
chore: add user agent to all fetch requests
1 parent c5d61c6 commit f5ebdf3

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

packages/actor-core-cli/src/utils/rivet-api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { z, type ZodTypeAny } from "zod";
2+
import { httpUserAgent } from "actor-core/utils";
23

34
export async function getServiceToken(
45
api: ReturnType<typeof createRivetApi>,
@@ -75,6 +76,7 @@ export function createRivetApi(endpoint: string, accessToken: string) {
7576
headers: {
7677
...opts.headers,
7778
"Content-Type": "application/json",
79+
"User-Agent": httpUserAgent(),
7880
Authorization: `Bearer ${accessToken}`,
7981
},
8082
});

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type * as wsToServer from "@/actor/protocol/message/to-server";
55
import type { Encoding } from "@/actor/protocol/serde";
66
import { importEventSource } from "@/common/eventsource";
77
import { MAX_CONN_PARAMS_SIZE } from "@/common/network";
8+
import { httpUserAgent } from "@/utils";
89
import { assertUnreachable, stringifyError } from "@/common/utils";
910
import { importWebSocket } from "@/common/websocket";
1011
import type { ActorQuery } from "@/manager/protocol/query";
@@ -686,6 +687,9 @@ enc
686687
const messageSerialized = this.#serialize(message);
687688
const res = await fetch(url, {
688689
method: "POST",
690+
headers: {
691+
"User-Agent": httpUserAgent(),
692+
},
689693
body: messageSerialized,
690694
});
691695

@@ -845,4 +849,4 @@ enc
845849
*/
846850

847851
export type ActorConn<AD extends AnyActorDefinition> = ActorConnRaw &
848-
ActorDefinitionRpcs<AD>;
852+
ActorDefinitionRpcs<AD>;

packages/actor-core/src/client/utils.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { deserialize } from "@/actor/protocol/serde";
22
import { assertUnreachable, stringifyError } from "@/common/utils";
3+
import { httpUserAgent } from "@/utils";
34
import { Encoding } from "@/mod";
45
import * as cbor from "cbor-x";
56
import { ActorError, HttpRequestError } from "./errors";
@@ -62,11 +63,14 @@ export async function sendHttpRequest<
6263
// Make the HTTP request
6364
response = await fetch(opts.url, {
6465
method: opts.method,
65-
headers: contentType
66-
? {
67-
"Content-Type": contentType,
68-
}
69-
: {},
66+
headers: {
67+
"User-Agent": httpUserAgent(),
68+
...(contentType
69+
? {
70+
"Content-Type": contentType,
71+
}
72+
: {}),
73+
},
7074
body: bodyData,
7175
});
7276
} catch (error) {

packages/actor-core/src/utils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,24 @@
11
export { assertUnreachable } from "./common/utils";
2+
import pkgJson from "../package.json" with { type: "json" };
3+
4+
export const VERSION = pkgJson.version;
5+
6+
let _userAgent: string | undefined = undefined;
7+
8+
export function httpUserAgent(): string {
9+
// Return cached value if already initialized
10+
if (_userAgent !== undefined) {
11+
return _userAgent;
12+
}
13+
14+
// Library
15+
let userAgent = `ActorCore/${VERSION}`;
16+
17+
// Navigator
18+
const navigatorObj = typeof navigator !== "undefined" ? navigator : undefined;
19+
if (navigatorObj?.userAgent) userAgent += ` ${navigatorObj.userAgent}`;
20+
21+
_userAgent = userAgent;
22+
23+
return userAgent;
24+
}

packages/platforms/rivet/src/rivet-client.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { httpUserAgent } from "actor-core/utils";
2+
13
export interface RivetClientConfig {
24
endpoint: string;
35
token: string;
@@ -23,6 +25,7 @@ export async function rivetRequest<RequestBody, ResponseBody>(
2325
method,
2426
headers: {
2527
"Content-Type": "application/json",
28+
"User-Agent": httpUserAgent(),
2629
Authorization: `Bearer ${config.token}`,
2730
},
2831
body: body ? JSON.stringify(body) : undefined,

0 commit comments

Comments
 (0)