Skip to content
Draft
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
5 changes: 5 additions & 0 deletions .changeset/sandbox-eve-client-header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eve": patch
---

Sandbox API requests now send an `x-eve-client: eve/<version>` header.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, it, vi } from "vitest";

import {
EVE_SANDBOX_CLIENT_HEADER,
withEveSandboxClientHeader,
} from "#execution/sandbox/bindings/vercel-client-header.js";

function headerOf(init: RequestInit | undefined): string | null {
return new Headers(init?.headers).get(EVE_SANDBOX_CLIENT_HEADER);
}

describe("withEveSandboxClientHeader", () => {
it("stamps the eve client header as eve/<version>", async () => {
const inner = vi.fn<typeof globalThis.fetch>().mockResolvedValue(new Response());
const wrapped = withEveSandboxClientHeader(inner);

await wrapped("https://api.vercel.com/sandboxes");

const [, init] = inner.mock.calls[0]!;
expect(headerOf(init)).toMatch(/^eve\/.+/);
});

it("preserves existing headers", async () => {
const inner = vi.fn<typeof globalThis.fetch>().mockResolvedValue(new Response());
const wrapped = withEveSandboxClientHeader(inner);

await wrapped("https://api.vercel.com/sandboxes", {
headers: { "user-agent": "vercel/sandbox/1.0.0" },
});

const [, init] = inner.mock.calls[0]!;
const headers = new Headers(init?.headers);
expect(headers.get("user-agent")).toBe("vercel/sandbox/1.0.0");
expect(headers.get(EVE_SANDBOX_CLIENT_HEADER)).toMatch(/^eve\/.+/);
});

it("delegates to globalThis.fetch when no inner fetch is supplied", () => {
const wrapped = withEveSandboxClientHeader();
expect(typeof wrapped).toBe("function");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { resolveInstalledPackageInfo } from "#internal/application/package.js";

/**
* Request header eve stamps on every Vercel Sandbox API call so the sandbox
* control plane can attribute traffic to eve and its version.
*/
export const EVE_SANDBOX_CLIENT_HEADER = "x-eve-client";

/**
* Wraps a `fetch` implementation so every request carries the
* {@link EVE_SANDBOX_CLIENT_HEADER} identifying the eve client and version.
*/
export function withEveSandboxClientHeader(
inner: typeof globalThis.fetch = globalThis.fetch,
): typeof globalThis.fetch {
const { name, version } = resolveInstalledPackageInfo();
const clientId = `${name}/${version}`;

return (input, init) => {
const headers = new Headers(
init?.headers ??
(typeof input === "object" && input !== null && "headers" in input
? (input as Request).headers
: undefined),
);
headers.set(EVE_SANDBOX_CLIENT_HEADER, clientId);
return inner(input, { ...init, headers });
};
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getVercelSandboxFetch } from "#execution/sandbox/bindings/vercel-credentials.js";
import type {
VercelCreateOptions,
VercelModule,
Expand Down Expand Up @@ -27,6 +28,7 @@ export async function createVercelEveImageSandbox(input: {
const createOptions: VercelSandboxCreateParams = {
...input.createOptions,
__image: VERCEL_EVE_SANDBOX_IMAGE,
fetch: getVercelSandboxFetch(input.createOptions),
};
return await input.sandboxModule.Sandbox.create(createOptions);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { getVercelOidcToken } from "#compiled/@vercel/oidc/index.js";
import { withEveSandboxClientHeader } from "#execution/sandbox/bindings/vercel-client-header.js";
import type { VercelCreateOptions } from "#execution/sandbox/bindings/vercel-sdk-types.js";

export function getVercelSandboxFetch(createOptions: VercelCreateOptions): typeof globalThis.fetch {
const fetchOverride = (createOptions as { readonly fetch?: typeof globalThis.fetch }).fetch;
return fetchOverride ?? globalThis.fetch;
return withEveSandboxClientHeader(fetchOverride ?? globalThis.fetch);
}

export async function getVercelSandboxCredentials(
Expand Down
Loading