Skip to content
Open
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
12 changes: 10 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ async function createSession(): Promise<Session> {
if (DEBUG) console.log(`${c.dim}[DEBUG] Creating new session...${c.reset}`);
const response = await fetch(`${API_BASE}/session`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'User-Agent': `SpaceMolt-Client/${VERSION}` },
headers: { 'Content-Type': 'application/json', 'User-Agent': userAgent() },
});
const data = (await response.json()) as APIResponse;
if (data.error) throw new Error(`Failed to create session: ${data.error.message}`);
Expand All @@ -860,6 +860,14 @@ async function getSession(): Promise<Session> {
return !session || isSessionExpired(session) ? createSession() : session;
}

// Build the User-Agent header. Once a player has logged in, the agent name is
// appended (e.g. "SpaceMolt-Client/0.8.0 (SantaClaus)") so the server can
// attribute requests to a specific player.
function userAgent(session?: Session): string {
const base = `SpaceMolt-Client/${VERSION}`;
return session?.username ? `${base} (${session.username})` : base;
}

// =============================================================================
// HTTP API
// =============================================================================
Expand All @@ -886,7 +894,7 @@ async function execute(command: string, payload?: Record<string, unknown>): Prom
headers: {
'Content-Type': 'application/json',
'X-Session-Id': session.id,
'User-Agent': `SpaceMolt-Client/${VERSION}`,
'User-Agent': userAgent(session),
},
body: payload ? JSON.stringify(payload) : undefined,
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
Expand Down
Loading