-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathgit-providers.ts
More file actions
410 lines (388 loc) · 13.9 KB
/
git-providers.ts
File metadata and controls
410 lines (388 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import { withoutTrailingSlash } from 'ufo'
export type ProviderId =
| 'github'
| 'gitlab'
| 'bitbucket'
| 'gitea'
| 'forgejo'
| 'codeberg'
| 'sourcehut'
| 'gitee'
| 'tangled'
| 'radicle'
export interface RepoRef {
provider: ProviderId
owner: string
repo: string
host?: string
}
export interface RepositoryInfo extends RepoRef {
/** Raw file URL base (e.g., https://raw.githubusercontent.com/owner/repo/HEAD) */
rawBaseUrl: string
/** Blob/rendered file URL base (e.g., https://github.com/owner/repo/blob/HEAD) */
blobBaseUrl: string
/** Subdirectory within repo where package lives (e.g., packages/ai) */
directory?: string
}
/** Known GitLab instances (self-hosted) */
export const GITLAB_HOSTS = [
'gitlab.com',
'gitlab.gnome.org',
'gitlab.freedesktop.org',
'invent.kde.org',
'salsa.debian.org',
'framagit.org',
]
interface ProviderConfig {
id: ProviderId
/** Check if hostname matches this provider */
matchHost(host: string): boolean
/** Parse URL path into owner/repo, returns null if invalid */
parsePath(parts: string[]): { owner: string; repo: string } | null
/** Get raw file URL base for resolving relative paths */
getRawBaseUrl(ref: RepoRef, branch?: string): string
/** Get blob/rendered URL base for markdown files */
getBlobBaseUrl(ref: RepoRef, branch?: string): string
/** Convert file URLs to blob URLs (for images) */
fileToRaw?(url: string): string
/** Convert blob URLs to raw URLs (for images) */
blobToRaw?(url: string): string
}
const providers: ProviderConfig[] = [
{
id: 'github',
matchHost: host => host === 'github.com' || host === 'www.github.com',
parsePath: parts => {
if (parts.length < 2) return null
const owner = decodeURIComponent(parts[0] ?? '').trim()
const repo = decodeURIComponent(parts[1] ?? '')
.trim()
.replace(/\.git$/i, '')
if (!owner || !repo) return null
return { owner, repo }
},
getRawBaseUrl: (ref, branch = 'HEAD') =>
`https://raw.githubusercontent.com/${ref.owner}/${ref.repo}/${branch}`,
getBlobBaseUrl: (ref, branch = 'HEAD') =>
`https://github.com/${ref.owner}/${ref.repo}/blob/${branch}`,
fileToRaw: url => url.replace('/tree/', '/raw/'),
blobToRaw: url => url.replace('/blob/', '/raw/'),
},
{
id: 'gitlab',
matchHost: host => GITLAB_HOSTS.some(h => host === h || host === `www.${h}`),
parsePath: parts => {
if (parts.length < 2) return null
// GitLab supports nested groups
const repo = decodeURIComponent(parts[parts.length - 1] ?? '')
.trim()
.replace(/\.git$/i, '')
const owner = parts
.slice(0, -1)
.map(p => decodeURIComponent(p).trim())
.join('/')
if (!owner || !repo) return null
return { owner, repo }
},
getRawBaseUrl: (ref, branch = 'HEAD') => {
const host = ref.host ?? 'gitlab.com'
return `https://${host}/${ref.owner}/${ref.repo}/-/raw/${branch}`
},
getBlobBaseUrl: (ref, branch = 'HEAD') => {
const host = ref.host ?? 'gitlab.com'
return `https://${host}/${ref.owner}/${ref.repo}/-/blob/${branch}`
},
blobToRaw: url => url.replace('/-/blob/', '/-/raw/'),
},
{
id: 'bitbucket',
matchHost: host => host === 'bitbucket.org' || host === 'www.bitbucket.org',
parsePath: parts => {
if (parts.length < 2) return null
const owner = decodeURIComponent(parts[0] ?? '').trim()
const repo = decodeURIComponent(parts[1] ?? '')
.trim()
.replace(/\.git$/i, '')
if (!owner || !repo) return null
return { owner, repo }
},
getRawBaseUrl: (ref, branch = 'HEAD') =>
`https://bitbucket.org/${ref.owner}/${ref.repo}/raw/${branch}`,
getBlobBaseUrl: (ref, branch = 'HEAD') =>
`https://bitbucket.org/${ref.owner}/${ref.repo}/src/${branch}`,
blobToRaw: url => url.replace('/src/', '/raw/'),
},
{
id: 'codeberg',
matchHost: host => host === 'codeberg.org' || host === 'www.codeberg.org',
parsePath: parts => {
if (parts.length < 2) return null
const owner = decodeURIComponent(parts[0] ?? '').trim()
const repo = decodeURIComponent(parts[1] ?? '')
.trim()
.replace(/\.git$/i, '')
if (!owner || !repo) return null
return { owner, repo }
},
getRawBaseUrl: (ref, branch = 'HEAD') =>
`https://codeberg.org/${ref.owner}/${ref.repo}/raw/branch/${branch === 'HEAD' ? 'main' : branch}`,
getBlobBaseUrl: (ref, branch = 'HEAD') =>
`https://codeberg.org/${ref.owner}/${ref.repo}/src/branch/${branch === 'HEAD' ? 'main' : branch}`,
blobToRaw: url => url.replace('/src/', '/raw/'),
},
{
id: 'gitee',
matchHost: host => host === 'gitee.com' || host === 'www.gitee.com',
parsePath: parts => {
if (parts.length < 2) return null
const owner = decodeURIComponent(parts[0] ?? '').trim()
const repo = decodeURIComponent(parts[1] ?? '')
.trim()
.replace(/\.git$/i, '')
if (!owner || !repo) return null
return { owner, repo }
},
getRawBaseUrl: (ref, branch = 'master') =>
`https://gitee.com/${ref.owner}/${ref.repo}/raw/${branch}`,
getBlobBaseUrl: (ref, branch = 'master') =>
`https://gitee.com/${ref.owner}/${ref.repo}/blob/${branch}`,
blobToRaw: url => url.replace('/blob/', '/raw/'),
},
{
id: 'sourcehut',
matchHost: host => host === 'sr.ht' || host === 'git.sr.ht',
parsePath: parts => {
if (parts.length < 2) return null
// Sourcehut uses ~username/repo format
const owner = decodeURIComponent(parts[0] ?? '').trim()
const repo = decodeURIComponent(parts[1] ?? '')
.trim()
.replace(/\.git$/i, '')
if (!owner || !repo) return null
return { owner, repo }
},
getRawBaseUrl: (ref, branch = 'HEAD') =>
`https://git.sr.ht/${ref.owner}/${ref.repo}/blob/${branch}`,
getBlobBaseUrl: (ref, branch = 'HEAD') =>
`https://git.sr.ht/${ref.owner}/${ref.repo}/tree/${branch}/item`,
},
{
id: 'tangled',
matchHost: host =>
host === 'tangled.sh' ||
host === 'www.tangled.sh' ||
host === 'tangled.org' ||
host === 'www.tangled.org',
parsePath: parts => {
if (parts.length < 2) return null
// Tangled uses owner/repo format (owner is a domain-like identifier, e.g., nonbinary.computer)
const owner = decodeURIComponent(parts[0] ?? '').trim()
const repo = decodeURIComponent(parts[1] ?? '')
.trim()
.replace(/\.git$/i, '')
if (!owner || !repo) return null
return { owner, repo }
},
getRawBaseUrl: (ref, branch = 'main') =>
`https://tangled.sh/${ref.owner}/${ref.repo}/raw/branch/${branch}`,
getBlobBaseUrl: (ref, branch = 'main') =>
`https://tangled.sh/${ref.owner}/${ref.repo}/src/branch/${branch}`,
blobToRaw: url => url.replace('/blob/', '/raw/branch/'),
},
{
id: 'radicle',
matchHost: host =>
host === 'radicle.at' || host === 'app.radicle.at' || host === 'seed.radicle.at',
parsePath: parts => {
// Radicle URLs: app.radicle.at/nodes/seed.radicle.at/rad:z3nP4yT1PE3m1PxLEzr173sZtJVnT
// We extract the rad:... identifier as the "repo" with no owner
const path = parts.join('/')
const radMatch = path.match(/rad:[a-zA-Z0-9]+/)
if (!radMatch?.[0]) return null
// Use empty owner, store full rad: ID as repo
return { owner: '', repo: radMatch[0] }
},
getRawBaseUrl: (ref, branch = 'HEAD') =>
`https://seed.radicle.at/api/v1/projects/${ref.repo}/blob/${branch}`,
getBlobBaseUrl: (ref, branch = 'HEAD') =>
`https://app.radicle.at/nodes/seed.radicle.at/${ref.repo}/tree/${branch}`,
},
{
id: 'forgejo',
matchHost: host => {
// Match explicit Forgejo instances
const forgejoPatterns = [/^forgejo\./i, /\.forgejo\./i]
// Known Forgejo instances
const knownInstances = ['next.forgejo.org', 'try.next.forgejo.org']
if (knownInstances.some(h => host === h)) return true
return forgejoPatterns.some(p => p.test(host))
},
parsePath: parts => {
if (parts.length < 2) return null
const owner = decodeURIComponent(parts[0] ?? '').trim()
const repo = decodeURIComponent(parts[1] ?? '')
.trim()
.replace(/\.git$/i, '')
if (!owner || !repo) return null
return { owner, repo }
},
getRawBaseUrl: (ref, branch = 'HEAD') => {
const host = ref.host ?? 'codeberg.org'
return `https://${host}/${ref.owner}/${ref.repo}/raw/branch/${branch === 'HEAD' ? 'main' : branch}`
},
getBlobBaseUrl: (ref, branch = 'HEAD') => {
const host = ref.host ?? 'codeberg.org'
return `https://${host}/${ref.owner}/${ref.repo}/src/branch/${branch === 'HEAD' ? 'main' : branch}`
},
blobToRaw: url => url.replace('/src/', '/raw/'),
},
{
id: 'gitea',
matchHost: host => {
// Match common Gitea hosting patterns (Forgejo has its own adapter)
const giteaPatterns = [/^git\./i, /^gitea\./i, /^code\./i, /^src\./i, /gitea\.io$/i]
// Skip known providers (including Forgejo patterns)
const skipHosts = [
'github.com',
'gitlab.com',
'codeberg.org',
'bitbucket.org',
'gitee.com',
'sr.ht',
'git.sr.ht',
'tangled.sh',
'tangled.org',
'next.forgejo.org',
'try.next.forgejo.org',
...GITLAB_HOSTS,
]
if (skipHosts.some(h => host === h || host.endsWith(`.${h}`))) return false
// Skip Forgejo patterns
if (/^forgejo\./i.test(host) || /\.forgejo\./i.test(host)) return false
return giteaPatterns.some(p => p.test(host))
},
parsePath: parts => {
if (parts.length < 2) return null
const owner = decodeURIComponent(parts[0] ?? '').trim()
const repo = decodeURIComponent(parts[1] ?? '')
.trim()
.replace(/\.git$/i, '')
if (!owner || !repo) return null
return { owner, repo }
},
getRawBaseUrl: (ref, branch = 'HEAD') => {
const host = ref.host ?? 'gitea.io'
return `https://${host}/${ref.owner}/${ref.repo}/raw/branch/${branch === 'HEAD' ? 'main' : branch}`
},
getBlobBaseUrl: (ref, branch = 'HEAD') => {
const host = ref.host ?? 'gitea.io'
return `https://${host}/${ref.owner}/${ref.repo}/src/branch/${branch === 'HEAD' ? 'main' : branch}`
},
blobToRaw: url => url.replace('/src/', '/raw/'),
},
]
/**
* Normalize various git URL formats to a standard HTTPS URL.
* Handles: git+https://, git://, git@host:path, ssh://git@host/path
*/
export function normalizeGitUrl(input: string): string | null {
const url = input
.trim()
.replace(/^git\+/, '')
.replace(/\.git(?=[/#?]|$)/i, '')
.replace(/(^|\/)[^/]+?@/, '$1') // remove "user@" from "ssh://user@host.com:..."
.replace(/(\.[^./]+?):/, '$1/') // change ".com:" to ".com/" from "ssh://user@host.com:..."
.replace(/^git:\/\//, 'https://')
.replace(/^ssh:\/\//, 'https://')
if (!url) return null
return url.includes('://') ? url : `https://${url}`
}
export function parseRepoUrl(input: string): RepoRef | null {
const normalized = normalizeGitUrl(input)
if (!normalized) return null
try {
const url = new URL(normalized)
const host = url.hostname.toLowerCase()
const parts = url.pathname.split('/').filter(Boolean)
for (const provider of providers) {
if (!provider.matchHost(host)) continue
const parsed = provider.parsePath(parts)
if (parsed) {
const needsHost = ['gitlab', 'gitea', 'forgejo', 'radicle'].includes(provider.id)
return {
provider: provider.id,
owner: parsed.owner,
repo: parsed.repo,
host: needsHost ? host : undefined,
}
}
}
return null
} catch {
return null
}
}
/**
* Parse repository field from package.json into repository info.
* Supports both full objects and shorthand strings.
*/
export function parseRepositoryInfo(
repository?: { type?: string; url?: string; directory?: string } | string,
): RepositoryInfo | undefined {
if (!repository) return undefined
let url: string | undefined
let directory: string | undefined
if (typeof repository === 'string') {
url = repository
} else {
url = repository.url
directory = repository.directory
}
if (!url) return undefined
const ref = parseRepoUrl(url)
if (!ref) return undefined
const provider = providers.find(p => p.id === ref.provider)
if (!provider) return undefined
return {
...ref,
rawBaseUrl: provider.getRawBaseUrl(ref),
blobBaseUrl: provider.getBlobBaseUrl(ref),
directory: directory ? withoutTrailingSlash(directory) : undefined,
}
}
export function getProviderConfig(providerId: ProviderId): ProviderConfig | undefined {
return providers.find(p => p.id === providerId)
}
export function convertBlobOrFileToRawUrl(url: string, providerId: ProviderId): string {
const provider = providers.find(p => p.id === providerId)
let rawUrl = url
if (provider?.fileToRaw) {
rawUrl = provider.fileToRaw(url)
}
if (provider?.blobToRaw) {
rawUrl = provider.blobToRaw(rawUrl)
}
return rawUrl
}
export function isKnownGitProvider(url: string): boolean {
return parseRepoUrl(url) !== null
}
/**
* API origins used by each provider for client-side repo metadata fetches.
* Self-hosted providers are excluded because their origins can be anything.
*/
export const GIT_PROVIDER_API_ORIGINS = {
github: 'https://ungh.cc', // via UNGH proxy to avoid rate limits
bitbucket: 'https://api.bitbucket.org',
codeberg: 'https://codeberg.org',
gitee: 'https://gitee.com',
radicle: 'https://seed.radicle.at',
} as const satisfies Partial<Record<ProviderId, string>>
/**
* All known external API origins that git provider adapters may fetch from.
* Includes both the per-provider origins and known self-hosted instances.
*/
export const ALL_KNOWN_GIT_API_ORIGINS: readonly string[] = [
...Object.values(GIT_PROVIDER_API_ORIGINS),
...GITLAB_HOSTS.map(host => `https://${host}`),
]