Skip to content

Commit

Permalink
fix: unexpected authentication window for anonymous user
Browse files Browse the repository at this point in the history
  • Loading branch information
conwnet committed Feb 29, 2024
1 parent 85217b7 commit b5e83e2
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions extensions/github1s/src/adapters/sourcegraph/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,15 @@ export class SourcegraphDataSource extends DataSource {
async provideFile(repo: string, ref: string, path: string): Promise<File> {
// sourcegraph api break binary files and text coding, so we use github api first here
if (this.platform === 'github') {
return fetch(encodeURI(`https://raw.githubusercontent.com/${repo}/${ref}/${path}`))
.then((response) => (response.ok ? response.arrayBuffer() : Promise.reject({ response })))
.then((buffer) => ({ content: new Uint8Array(buffer) }));
// For GitHub repositories, request GitHub User Content API first (it seems no Rate Limit),
// because Sourcegraph API returns binary data such as pictures in error. If the GitHub User
// Content API goes wrong, then try Sourcegraph API. Use `try catch` because if fallback to
// GitHub REST API may trigger a pop-up window to request authentication for anonymous users.
try {
return fetch(encodeURI(`https://raw.githubusercontent.com/${repo}/${ref}/${path}`))
.then((response) => (response.ok ? response.arrayBuffer() : Promise.reject({ response })))
.then((buffer) => ({ content: new Uint8Array(buffer) }));
} catch {}
}
// TODO: support binary files for other platforms
const { content } = await readFile(this.buildRepository(repo), ref, path);
Expand Down

0 comments on commit b5e83e2

Please sign in to comment.