Skip to content

Commit 480186e

Browse files
committed
Make sure to use the isGithubUrl
1 parent 06b1ae5 commit 480186e

File tree

5 files changed

+60
-32
lines changed

5 files changed

+60
-32
lines changed

packages/playground/blueprints/src/lib/v1/compile.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ export interface CompileBlueprintV1Options {
8585
streamBundledFile?: StreamBundledFile;
8686
/**
8787
* Additional headers to pass to git operations.
88+
* A function that returns headers based on the URL being accessed.
8889
*/
89-
gitAdditionalHeaders?: Record<string, string>;
90+
gitAdditionalHeaders?: (url: string) => Record<string, string>;
9091
/**
9192
* Additional steps to add to the blueprint.
9293
*/
@@ -522,8 +523,9 @@ interface CompileStepArgsOptions {
522523
streamBundledFile?: StreamBundledFile;
523524
/**
524525
* Additional headers to pass to git operations.
526+
* A function that returns headers based on the URL being accessed.
525527
*/
526-
gitAdditionalHeaders?: Record<string, string>;
528+
gitAdditionalHeaders?: (url: string) => Record<string, string>;
527529
}
528530

529531
/**

packages/playground/blueprints/src/lib/v1/resources.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export abstract class Resource<T extends File | Directory> {
164164
progress?: ProgressTracker;
165165
corsProxy?: string;
166166
streamBundledFile?: StreamBundledFile;
167-
gitAdditionalHeaders?: Record<string, string>;
167+
gitAdditionalHeaders?: (url: string) => Record<string, string>;
168168
}
169169
): Resource<File | Directory> {
170170
let resource: Resource<File | Directory>;
@@ -561,15 +561,15 @@ export class GitDirectoryResource extends Resource<Directory> {
561561
private reference: GitDirectoryReference;
562562
private options?: {
563563
corsProxy?: string;
564-
additionalHeaders?: Record<string, string>;
564+
additionalHeaders?: (url: string) => Record<string, string>;
565565
};
566566

567567
constructor(
568568
reference: GitDirectoryReference,
569569
_progress?: ProgressTracker,
570570
options?: {
571571
corsProxy?: string;
572-
additionalHeaders?: Record<string, string>;
572+
additionalHeaders?: (url: string) => Record<string, string>;
573573
}
574574
) {
575575
super();

packages/playground/client/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ export interface StartPlaygroundOptions {
8787
corsProxy?: string;
8888
/**
8989
* Additional headers to pass to git operations.
90+
* A function that returns headers based on the URL being accessed.
9091
*/
91-
gitAdditionalHeaders?: Record<string, string>;
92+
gitAdditionalHeaders?: (url: string) => Record<string, string>;
9293
/**
9394
* The version of the SQLite driver to use.
9495
* Defaults to the latest development version.

packages/playground/storage/src/lib/git-sparse-checkout.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ export class GitAuthenticationError extends Error {
4242
}
4343
}
4444

45+
export type GitAdditionalHeaders = (url: string) => Record<string, string>;
46+
47+
function resolveGitHeaders(
48+
url: string,
49+
headers?: GitAdditionalHeaders
50+
): Record<string, string> {
51+
if (!headers || typeof headers !== 'function') {
52+
return {};
53+
}
54+
55+
return headers(url);
56+
}
57+
4558
/**
4659
* Downloads specific files from a git repository.
4760
* It uses the git protocol over HTTP to fetch the files. It only uses
@@ -79,20 +92,24 @@ export async function sparseCheckout(
7992
filesPaths: string[],
8093
options?: {
8194
withObjects?: boolean;
82-
additionalHeaders?: Record<string, string>;
95+
additionalHeaders?: GitAdditionalHeaders;
8396
}
8497
): Promise<SparseCheckoutResult> {
8598
const treesPack = await fetchWithoutBlobs(
8699
repoUrl,
87100
commitHash,
88-
options?.additionalHeaders
101+
resolveGitHeaders(repoUrl, options?.additionalHeaders)
89102
);
90103
const objects = await resolveObjects(treesPack.idx, commitHash, filesPaths);
91104

92105
const blobOids = filesPaths.map((path) => objects[path].oid);
93106
const blobsPack =
94107
blobOids.length > 0
95-
? await fetchObjects(repoUrl, blobOids, options?.additionalHeaders)
108+
? await fetchObjects(
109+
repoUrl,
110+
blobOids,
111+
resolveGitHeaders(repoUrl, options?.additionalHeaders)
112+
)
96113
: null;
97114

98115
const fetchedPaths: Record<string, any> = {};
@@ -197,12 +214,12 @@ const FULL_SHA_REGEX = /^[0-9a-f]{40}$/i;
197214
export async function listGitFiles(
198215
repoUrl: string,
199216
commitHash: string,
200-
additionalHeaders?: Record<string, string>
217+
additionalHeaders?: GitAdditionalHeaders
201218
): Promise<GitFileTree[]> {
202219
const treesPack = await fetchWithoutBlobs(
203220
repoUrl,
204221
commitHash,
205-
additionalHeaders
222+
resolveGitHeaders(repoUrl, additionalHeaders)
206223
);
207224
const rootTree = await resolveAllObjects(treesPack.idx, commitHash);
208225
if (!rootTree?.object) {
@@ -222,7 +239,7 @@ export async function listGitFiles(
222239
export async function resolveCommitHash(
223240
repoUrl: string,
224241
ref: GitRef,
225-
additionalHeaders?: Record<string, string>
242+
additionalHeaders?: GitAdditionalHeaders
226243
) {
227244
const parsed = await parseGitRef(repoUrl, ref);
228245
if (parsed.resolvedOid) {
@@ -268,7 +285,7 @@ function gitTreeToFileTree(tree: GitTree): GitFileTree[] {
268285
export async function listGitRefs(
269286
repoUrl: string,
270287
fullyQualifiedBranchPrefix: string,
271-
additionalHeaders?: Record<string, string>
288+
additionalHeaders?: GitAdditionalHeaders
272289
) {
273290
const packbuffer = Buffer.from(
274291
(await collect([
@@ -289,7 +306,7 @@ export async function listGitRefs(
289306
'content-type': 'application/x-git-upload-pack-request',
290307
'Content-Length': `${packbuffer.length}`,
291308
'Git-Protocol': 'version=2',
292-
...additionalHeaders,
309+
...resolveGitHeaders(repoUrl, additionalHeaders),
293310
},
294311
body: packbuffer as any,
295312
});
@@ -418,7 +435,7 @@ async function parseGitRef(
418435
async function fetchRefOid(
419436
repoUrl: string,
420437
refname: string,
421-
additionalHeaders?: Record<string, string>
438+
additionalHeaders?: GitAdditionalHeaders
422439
) {
423440
const refs = await listGitRefs(repoUrl, refname, additionalHeaders);
424441
const candidates = [refname, `${refname}^{}`];

packages/playground/website/src/github/git-auth-helpers.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,37 @@ const KNOWN_CORS_PROXY_URLS = [
77
];
88

99
export function isGitHubUrl(url: string): boolean {
10-
if (url.includes('github.com')) {
11-
return true;
12-
}
1310
for (const corsProxyUrl of KNOWN_CORS_PROXY_URLS) {
14-
if (
15-
url.startsWith(corsProxyUrl) &&
16-
url.substring(corsProxyUrl.length).includes('github.com')
17-
) {
18-
return true;
11+
if (url.startsWith(corsProxyUrl)) {
12+
url = url.substring(corsProxyUrl.length);
13+
break;
1914
}
2015
}
21-
return false;
16+
17+
try {
18+
const urlObj = new URL(url);
19+
const hostname = urlObj.hostname;
20+
return hostname === 'github.com';
21+
} catch {
22+
return false;
23+
}
2224
}
2325

24-
export function createGitHubAuthHeaders(): Record<string, string> {
26+
export function createGitHubAuthHeaders(): (
27+
url: string
28+
) => Record<string, string> {
2529
const token = oAuthState.value.token;
26-
if (!token) {
27-
return {};
28-
}
2930

30-
return {
31-
Authorization: `Basic ${btoa(`${token}:`)}`,
32-
// Tell the CORS proxy to forward the Authorization header
33-
'X-Cors-Proxy-Allowed-Request-Headers': 'Authorization',
31+
return (url: string) => {
32+
if (!token || !isGitHubUrl(url)) {
33+
return {};
34+
}
35+
36+
const headers = {
37+
Authorization: `Basic ${btoa(`${token}:`)}`,
38+
// Tell the CORS proxy to forward the Authorization header
39+
'X-Cors-Proxy-Allowed-Request-Headers': 'Authorization',
40+
};
41+
return headers;
3442
};
3543
}

0 commit comments

Comments
 (0)