What is Nostr?
DanConwayDev
npub15qy…yejr
2025-01-29 09:38:43
in reply to nevent1q…st0a

DanConwayDev on Nostr: unfortunately, the only unified way of doing it is by cloning the repo. Someone made ...

unfortunately, the only unified way of doing it is by cloning the repo. Someone made a WASM compiled wrapper for gitlib2 for the browser so I plan to try shallow cloning in the browser.
For now I'm doing the below. I'm no longer going through a proxy so I hit a CORS error for most hosts. github also rate limit so you might encounter that when debugging.

From commit 19471e7c190cab4f2f823fb4e279066c5e211c5f
```typescript
/** most servers will produce a CORS error so a proxy should be used */
export const cloneArrayToReadMeUrls = (clone: string[]): string[] => {
const addresses = clone.map(extractRepoAddress)
/**
* at the time of this commit these urls work for:
* self-hosted gitea (or forgejo), gitlab
* github.com
* bitbucket.org
* gitlab.org
* gitea.com
* codeberg.org (forgejo instance)
* sourcehut (git.sr.ht)
* launchpad.net
* It doesnt work for:
* self-hosted gogs (requires branch name repo/raw/master/README.md)
* sourceforge.net (https://sourceforge.net/p/mingw/catgets/ci/master/tree/README?format=raw)
* notabug.org (requires branch name notabug.org/org/repo/raw/master/README.md)
*/
return [
...addresses.flatMap((address) => {
let prefix = 'raw/HEAD'
if (address.includes('sr.ht')) prefix = 'blob/HEAD'
if (
address.includes('git.launchpad.net') ||
address.includes('git.savannah.gnu.org')
)
prefix = 'plain'
if (address.includes('github.com')) {
// raw.githubusercontent.com can be used without CORS error
address = address.replace('github.com', 'raw.githubusercontent.com')
prefix = 'HEAD'
}
return ['README.md', 'readme.md'].map(
(filename) => `https://${address}/${prefix}/${filename}`
)
}),
]
}

const extractRepoAddress = (clone_string: string): string => {
let s = clone_string
// remove trailing slash
if (s.endsWith('/')) s = s.substring(0, s.length - 1)
// remove trailing .git
if (s.endsWith('.git')) s = s.substring(0, s.length - 4)
// remove :// and anything before
if (s.includes('://')) s = s.split('://')[1]
// remove @ and anything before
if (s.includes('@')) s = s.split('@')[1]
// replace : with /
s = s.replace(/\s|:[0-9]+/g, '')
s = s.replace(':', '/')
return s
}

```
Author Public Key
npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr