-
Notifications
You must be signed in to change notification settings - Fork 1
feat(fetch): identify CLI with a Clerk-CLI User-Agent header #301
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "clerk": patch | ||
| --- | ||
|
|
||
| Identify the CLI in outbound HTTP requests with a `User-Agent` like `Clerk-CLI/<version> (Bun/<bun-version>; <platform>-<arch>)` instead of the default Bun user agent. Allow callers to override the header. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { test, expect, describe, afterEach, mock } from "bun:test"; | ||
| import { loggedFetch } from "./fetch.ts"; | ||
|
|
||
| const originalFetch = globalThis.fetch; | ||
|
|
||
| describe("loggedFetch", () => { | ||
| afterEach(() => { | ||
| globalThis.fetch = originalFetch; | ||
| }); | ||
|
|
||
| test("sets a Clerk-CLI User-Agent on outbound requests", async () => { | ||
| globalThis.fetch = mock( | ||
| async () => new Response("ok", { status: 200 }), | ||
| ) as unknown as typeof fetch; | ||
| await loggedFetch("https://example.test/x", { tag: "test" }); | ||
| const [, init] = (globalThis.fetch as unknown as ReturnType<typeof mock>).mock.calls[0]!; | ||
| expect(init.headers.get("User-Agent")).toMatch(/^Clerk-CLI\//); | ||
| }); | ||
|
|
||
| test("preserves a caller-provided User-Agent", async () => { | ||
| globalThis.fetch = mock( | ||
| async () => new Response("ok", { status: 200 }), | ||
| ) as unknown as typeof fetch; | ||
| await loggedFetch("https://example.test/x", { | ||
| tag: "test", | ||
| headers: { "User-Agent": "Custom/1.0" }, | ||
| }); | ||
| const [, init] = (globalThis.fetch as unknown as ReturnType<typeof mock>).mock.calls[0]!; | ||
| expect(init.headers.get("User-Agent")).toBe("Custom/1.0"); | ||
| }); | ||
|
|
||
| test("preserves other caller-provided headers", async () => { | ||
| globalThis.fetch = mock( | ||
| async () => new Response("ok", { status: 200 }), | ||
| ) as unknown as typeof fetch; | ||
| await loggedFetch("https://example.test/x", { | ||
| tag: "test", | ||
| headers: { Authorization: "Bearer abc" }, | ||
| }); | ||
| const [, init] = (globalThis.fetch as unknown as ReturnType<typeof mock>).mock.calls[0]!; | ||
| expect(init.headers.get("Authorization")).toBe("Bearer abc"); | ||
| expect(init.headers.get("User-Agent")).toMatch(/^Clerk-CLI\//); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { test, expect, describe, afterEach } from "bun:test"; | ||
| import { buildUserAgent } from "./user-agent.ts"; | ||
|
|
||
| describe("buildUserAgent", () => { | ||
| const originalCi = process.env.CI; | ||
| afterEach(() => { | ||
| if (originalCi === undefined) delete process.env.CI; | ||
| else process.env.CI = originalCi; | ||
| }); | ||
|
|
||
| test("starts with Clerk-CLI/<version>", () => { | ||
| expect(buildUserAgent()).toMatch(/^Clerk-CLI\/\S+ /); | ||
| }); | ||
|
|
||
| test("includes Bun/<bun-version> and platform-arch", () => { | ||
| const ua = buildUserAgent(); | ||
| expect(ua).toContain(`Bun/${Bun.version}`); | ||
| expect(ua).toContain(`${process.platform}-${process.arch}`); | ||
| }); | ||
|
|
||
| test("appends ci segment when CI env is set", () => { | ||
| process.env.CI = "1"; | ||
| expect(buildUserAgent()).toMatch(/; ci\)$/); | ||
| }); | ||
|
|
||
| test("omits ci segment when CI env is unset", () => { | ||
| delete process.env.CI; | ||
| expect(buildUserAgent()).not.toMatch(/; ci\)/); | ||
| }); | ||
|
|
||
| test("uses only printable ASCII characters", () => { | ||
| expect(buildUserAgent()).toMatch(/^[\x20-\x7e]+$/); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /** | ||
| * Identifies the CLI in outbound HTTP calls so Clerk's edge can route or filter | ||
| * CLI traffic separately (e.g. to dedicated Cloud Run services). Without this | ||
| * we fall through to Bun's default `User-Agent: Bun/<version>`, which is | ||
| * indistinguishable from any other Bun-based client. | ||
| * | ||
| * Format: `Clerk-CLI/<version> (Bun/<bun-version>; <platform>-<arch>[; ci])` | ||
| * - <platform>: darwin | linux | win32 | … (process.platform) | ||
| * - <arch>: arm64 | x64 | … (process.arch) | ||
| * - `ci` segment is appended when running under a recognized CI environment. | ||
| */ | ||
|
|
||
| import { DEV_CLI_VERSION, resolveCliVersion } from "./version.ts"; | ||
|
|
||
| export function buildUserAgent(): string { | ||
| const version = resolveCliVersion() ?? DEV_CLI_VERSION; | ||
| const segments = [`Bun/${Bun.version}`, `${process.platform}-${process.arch}`]; | ||
| if (process.env.CI) segments.push("ci"); | ||
| return `Clerk-CLI/${version} (${segments.join("; ")})`; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future it would be nice to encapsulate this as like
getCliVersion()so callers don't need to worry about the fallback.