Skip to content

chore: add user agent to all fetch requests #962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/actor-core-cli/src/utils/rivet-api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z, type ZodTypeAny } from "zod";
import { httpUserAgent } from "actor-core/utils";

export async function getServiceToken(
api: ReturnType<typeof createRivetApi>,
Expand Down Expand Up @@ -75,6 +76,7 @@ export function createRivetApi(endpoint: string, accessToken: string) {
headers: {
...opts.headers,
"Content-Type": "application/json",
"User-Agent": httpUserAgent(),
Authorization: `Bearer ${accessToken}`,
},
});
Expand Down
6 changes: 5 additions & 1 deletion packages/actor-core/src/client/actor-conn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type * as wsToServer from "@/actor/protocol/message/to-server";
import type { Encoding } from "@/actor/protocol/serde";
import { importEventSource } from "@/common/eventsource";
import { MAX_CONN_PARAMS_SIZE } from "@/common/network";
import { httpUserAgent } from "@/utils";
import { assertUnreachable, stringifyError } from "@/common/utils";
import { importWebSocket } from "@/common/websocket";
import type { ActorQuery } from "@/manager/protocol/query";
Expand Down Expand Up @@ -686,6 +687,9 @@ enc
const messageSerialized = this.#serialize(message);
const res = await fetch(url, {
method: "POST",
headers: {
"User-Agent": httpUserAgent(),
},
body: messageSerialized,
});

Expand Down Expand Up @@ -845,4 +849,4 @@ enc
*/

export type ActorConn<AD extends AnyActorDefinition> = ActorConnRaw &
ActorDefinitionRpcs<AD>;
ActorDefinitionRpcs<AD>;
14 changes: 9 additions & 5 deletions packages/actor-core/src/client/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { deserialize } from "@/actor/protocol/serde";
import { assertUnreachable, stringifyError } from "@/common/utils";
import { httpUserAgent } from "@/utils";
import { Encoding } from "@/mod";
import * as cbor from "cbor-x";
import { ActorError, HttpRequestError } from "./errors";
Expand Down Expand Up @@ -62,11 +63,14 @@ export async function sendHttpRequest<
// Make the HTTP request
response = await fetch(opts.url, {
method: opts.method,
headers: contentType
? {
"Content-Type": contentType,
}
: {},
headers: {
"User-Agent": httpUserAgent(),
...(contentType
? {
"Content-Type": contentType,
}
: {}),
},
body: bodyData,
});
} catch (error) {
Expand Down
23 changes: 23 additions & 0 deletions packages/actor-core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
export { assertUnreachable } from "./common/utils";
import pkgJson from "../package.json" with { type: "json" };

export const VERSION = pkgJson.version;

let _userAgent: string | undefined = undefined;

export function httpUserAgent(): string {
// Return cached value if already initialized
if (_userAgent !== undefined) {
return _userAgent;
}

// Library
let userAgent = `ActorCore/${VERSION}`;

// Navigator
const navigatorObj = typeof navigator !== "undefined" ? navigator : undefined;
if (navigatorObj?.userAgent) userAgent += ` ${navigatorObj.userAgent}`;

_userAgent = userAgent;

return userAgent;
}
3 changes: 3 additions & 0 deletions packages/platforms/rivet/src/rivet-client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { httpUserAgent } from "actor-core/utils";

export interface RivetClientConfig {
endpoint: string;
token: string;
Expand All @@ -23,6 +25,7 @@ export async function rivetRequest<RequestBody, ResponseBody>(
method,
headers: {
"Content-Type": "application/json",
"User-Agent": httpUserAgent(),
Authorization: `Bearer ${config.token}`,
},
body: body ? JSON.stringify(body) : undefined,
Expand Down
Loading