Skip to content

Commit c89e030

Browse files
fix(web): compare against the primary URL clients were built with
The stale-port guard recorded the primary URL at the first bearer read. A client built on the old port before a move that landed before that read then saw the new URL on both sides and got the bearer. The first URL the page resolves for the desktop primary target is now recorded where every client reads it, and the guard compares the current URL against that. AI-assisted; verified with the tests and tools listed above.
1 parent fe50287 commit c89e030

3 files changed

Lines changed: 35 additions & 11 deletions

File tree

‎apps/web/src/environments/primary/desktopAuth.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { PRIMARY_LOCAL_ENVIRONMENT_ID, type DesktopBridge } from "@t3tools/contracts";
22

3+
import {
4+
__resetFirstDesktopPrimaryHttpBaseUrlForTests,
5+
readFirstDesktopPrimaryHttpBaseUrl,
6+
} from "./target";
7+
38
let desktopBearerTokenPromise: Promise<string> | null = null;
4-
let loadedPrimaryHttpBaseUrl: string | null | undefined;
59

610
function readPrimaryHttpBaseUrl(bridge: DesktopBridge): string | null {
711
return (
@@ -20,14 +24,11 @@ export function readDesktopPrimaryBearerToken(): Promise<string | null> {
2024
return Promise.resolve(null);
2125
}
2226

23-
// Desktop opens this page before readiness; a port collision can move the backend afterwards.
24-
if (loadedPrimaryHttpBaseUrl === undefined) {
25-
loadedPrimaryHttpBaseUrl = readPrimaryHttpBaseUrl(bridge);
26-
}
27-
const loadedUrl = loadedPrimaryHttpBaseUrl;
2827
desktopBearerTokenPromise ??= bridge.getLocalEnvironmentBearerToken().then(
2928
(token) => {
30-
if (readPrimaryHttpBaseUrl(bridge) === loadedUrl) {
29+
// Desktop opens this page before readiness; a port collision can move the backend afterwards.
30+
const loadedUrl = readFirstDesktopPrimaryHttpBaseUrl();
31+
if (loadedUrl === undefined || readPrimaryHttpBaseUrl(bridge) === loadedUrl) {
3132
return token;
3233
}
3334
// Clients here were built for the old port; reload rather than send the token there.
@@ -44,5 +45,5 @@ export function readDesktopPrimaryBearerToken(): Promise<string | null> {
4445

4546
export function __resetDesktopPrimaryAuthForTests(): void {
4647
desktopBearerTokenPromise = null;
47-
loadedPrimaryHttpBaseUrl = undefined;
48+
__resetFirstDesktopPrimaryHttpBaseUrlForTests();
4849
}

‎apps/web/src/environments/primary/httpLayer.test.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { PRIMARY_LOCAL_ENVIRONMENT_ID, type DesktopBridge } from "@t3tools/contr
22
import { afterEach, describe, expect, it, vi } from "@effect/vitest";
33
import * as Effect from "effect/Effect";
44
import * as Fiber from "effect/Fiber";
5+
import * as Layer from "effect/Layer";
56
import { HttpClient } from "effect/http";
67

78
import { __resetDesktopPrimaryAuthForTests } from "./desktopAuth";
9+
import * as PrimaryHttpClient from "./httpClient";
810
import * as PrimaryEnvironmentHttpLayer from "./httpLayer";
911

1012
function primaryBootstrap(port: number) {
@@ -101,6 +103,7 @@ describe("primary environment HTTP layer", { concurrent: false }, () => {
101103
value: {
102104
location: { origin: "t3code://app", reload: () => reloaded.resolve(undefined) },
103105
desktopBridge: {
106+
getLocalEnvironmentEnabled: () => true,
104107
getLocalEnvironmentBootstraps: () => [primaryBootstrap(port)],
105108
getLocalEnvironmentBearerToken: () => {
106109
requested.resolve(undefined);
@@ -111,12 +114,20 @@ describe("primary environment HTTP layer", { concurrent: false }, () => {
111114
});
112115

113116
return Effect.gen(function* () {
114-
yield* HttpClient.get("http://127.0.0.1:3774/api/auth/session").pipe(Effect.forkChild);
115-
yield* Effect.promise(() => requested.promise);
117+
// The client captures 3774; the backend moves before the first bearer read.
118+
const client = yield* PrimaryHttpClient.PrimaryEnvironmentHttpClient;
116119
port = 3775;
120+
yield* client.auth.session({ headers: {} }).pipe(Effect.forkChild);
121+
yield* Effect.promise(() => requested.promise);
117122
bearerToken.resolve("desktop-bearer-token");
118123
yield* Effect.promise(() => reloaded.promise);
119124
expect(fetchMock).not.toHaveBeenCalled();
120-
}).pipe(Effect.provide(PrimaryEnvironmentHttpLayer.layerForCurrentOrigin()));
125+
}).pipe(
126+
Effect.provide(
127+
PrimaryHttpClient.layer.pipe(
128+
Layer.provide(PrimaryEnvironmentHttpLayer.layerForCurrentOrigin()),
129+
),
130+
),
131+
);
121132
});
122133
});

‎apps/web/src/environments/primary/target.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,23 @@ function resolveWindowOriginPrimaryTarget(): PrimaryEnvironmentTarget {
236236
};
237237
}
238238

239+
let firstDesktopPrimaryHttpBaseUrl: string | undefined;
240+
241+
/** The desktop primary URL this page first resolved; clients built from it may outlive a port move. */
242+
export function readFirstDesktopPrimaryHttpBaseUrl(): string | undefined {
243+
return firstDesktopPrimaryHttpBaseUrl;
244+
}
245+
246+
export function __resetFirstDesktopPrimaryHttpBaseUrlForTests(): void {
247+
firstDesktopPrimaryHttpBaseUrl = undefined;
248+
}
249+
239250
function resolveDesktopPrimaryTarget(): PrimaryEnvironmentTarget | null {
240251
const desktopBootstrap = getDesktopLocalEnvironmentBootstrap();
241252
if (!desktopBootstrap) {
242253
return null;
243254
}
255+
firstDesktopPrimaryHttpBaseUrl ??= desktopBootstrap.httpBaseUrl ?? undefined;
244256
if (!desktopBootstrap.httpBaseUrl && !desktopBootstrap.wsBaseUrl) {
245257
return null;
246258
}

0 commit comments

Comments
 (0)