Skip to content

Commit da22acf

Browse files
committed
fix(server): GitLab uploads fall back to the browser when the server cannot fetch them
1 parent ccc9c49 commit da22acf

3 files changed

Lines changed: 30 additions & 21 deletions

File tree

‎apps/server/src/assets/AssetAccess.test.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import * as Path from "effect/Path";
2121
import * as PlatformError from "effect/PlatformError";
2222
import * as Schema from "effect/Schema";
2323
import * as TestClock from "effect/testing/TestClock";
24-
import { HttpClient, HttpClientResponse, HttpServerResponse } from "effect/http";
24+
import { HttpClient, HttpClientError, HttpClientResponse, HttpServerResponse } from "effect/http";
2525
import { vi } from "vite-plus/test";
2626

2727
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
@@ -235,7 +235,8 @@ describe("AssetAccess", () => {
235235
};
236236
const serve = (
237237
token: string | null,
238-
respond: (url: string) => Response,
238+
/** `null` stands for a host the server cannot reach. */
239+
respond: (url: string) => Response | null,
239240
invalidated: Array<string> = [],
240241
) => {
241242
const requests: Array<{ url: string; authorization: string | undefined }> = [];
@@ -250,7 +251,14 @@ describe("AssetAccess", () => {
250251
HttpClient.HttpClient,
251252
HttpClient.make((request) => {
252253
requests.push({ url: request.url, authorization: request.headers.authorization });
253-
return Effect.succeed(HttpClientResponse.fromWeb(request, respond(request.url)));
254+
const answer = respond(request.url);
255+
return answer === null
256+
? Effect.fail(
257+
new HttpClientError.HttpClientError({
258+
reason: new HttpClientError.TransportError({ request }),
259+
}),
260+
)
261+
: Effect.succeed(HttpClientResponse.fromWeb(request, answer));
254262
}),
255263
),
256264
Effect.scoped,
@@ -313,6 +321,12 @@ describe("AssetAccess", () => {
313321
const refused = serve("expired", () => new Response(null, { status: 401 }), invalidated);
314322
expect((yield* refused.response).headers.location).toBe(asset.url);
315323
expect(invalidated).toEqual(["gitlab.example.com"]);
324+
325+
// A server that cannot reach GitLab leaves the browser to try, as it did before.
326+
const unreachable = serve("glab-token", () => null);
327+
expect((yield* unreachable.response).headers.location).toBe(asset.url);
328+
const failing = serve("glab-token", () => new Response(null, { status: 503 }));
329+
expect((yield* failing.response).headers.location).toBe(asset.url);
316330
}),
317331
);
318332

‎apps/server/src/assets/SourceControlMedia.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ export const githubMediaResponse = Effect.fn("SourceControlMedia.githubMediaResp
172172

173173
/**
174174
* Serves a GitLab upload through the upload API with the `glab` login for its host. Whatever
175-
* that cannot serve (no login, GitLab before 17.4) redirects to the upload itself, which is
176-
* what loaded before and still works for a public project.
175+
* that cannot serve (no login, GitLab before 17.4 or out of the server's reach) redirects to the
176+
* upload itself, which is what loaded before and still works for a public project.
177177
*/
178178
export const gitlabMediaResponse = Effect.fn("SourceControlMedia.gitlabMediaResponse")(function* (
179179
asset: { readonly url: string; readonly expiresAt: number },
@@ -191,9 +191,17 @@ export const gitlabMediaResponse = Effect.fn("SourceControlMedia.gitlabMediaResp
191191
const response = yield* fetchFollowingRedirects(gitlabUploadApiUrl(upload), requestHeaders, {
192192
authorization: `Bearer ${Redacted.value(token.value)}`,
193193
appliesTo: (url) => URL.canParse(url) && new URL(url).origin === upload.origin,
194-
});
194+
}).pipe(
195+
// Request errors can carry the token, and the URL the upload secret.
196+
Effect.tapError(() =>
197+
Effect.logWarning("Failed to fetch GitLab media.", { host: upload.host }),
198+
),
199+
Effect.orElseSucceed(() => null),
200+
);
195201
if (response?.status === 401) yield* credentials.invalidate(upload.host);
196-
if (response === null || [401, 403, 404].includes(response.status)) return original;
202+
if (response === null || response.status >= 500 || [401, 403, 404].includes(response.status)) {
203+
return original;
204+
}
197205
return mediaResponse(
198206
response,
199207
gitlabMediaFileName(asset.url),

‎apps/server/src/http.ts‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -446,20 +446,7 @@ export const layerAssetRoute = HttpRouter.add(
446446
);
447447
}
448448
if (asset.kind === "gitlab-media") {
449-
return yield* gitlabMediaResponse(asset, request.headers).pipe(
450-
// Errors can carry the token, and the URL the upload secret, so only the host is logged.
451-
Effect.tapError(() =>
452-
Effect.logWarning("Failed to fetch GitLab media.", {
453-
host: URL.canParse(asset.url) ? new URL(asset.url).host : "invalid",
454-
}),
455-
),
456-
Effect.orElseSucceed(() =>
457-
HttpServerResponse.empty({
458-
status: 502,
459-
headers: { "cache-control": "private, no-store", "x-content-type-options": "nosniff" },
460-
}),
461-
),
462-
);
449+
return yield* gitlabMediaResponse(asset, request.headers);
463450
}
464451
return yield* assetFileResponse(
465452
asset,

0 commit comments

Comments
 (0)