-
Notifications
You must be signed in to change notification settings - Fork 19
/
registry.ts
770 lines (626 loc) · 17.1 KB
/
registry.ts
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
export type RegistryCtor = new (url: string) => RegistryUrl;
export function lookup(url: string, registries: RegistryCtor[]):
| RegistryUrl
| undefined {
for (const R of registries) {
const u = new R(url);
if (u.regexp.test(url)) {
return u;
}
}
}
export interface RegistryUrl {
url: string;
// all versions of the url
all: () => Promise<string[]>;
// url at a given version
at(version: string): RegistryUrl;
// current version of url
version: () => string;
// is url valid for this RegistryUrl
regexp: RegExp;
}
export function defaultAt(that: RegistryUrl, version: string): string {
return that.url.replace(/@(.*?)(\/|$)/, `@${version}/`);
}
export function defaultVersion(that: RegistryUrl): string {
const v = that.url.match(/\@([^\/]+)[\/$]?/);
if (v === null) {
throw Error(`Unable to find version in ${that.url}`);
}
return v[1];
}
export function defaultName(that: RegistryUrl): string {
const n = that.url.match(/([^\/\"\']*?)\@[^\'\"]*/);
if (n === null) {
throw new Error(`Package name not found in ${that.url}`);
}
return n[1];
}
async function githubDownloadReleases(
owner: string,
repo: string,
lastVersion: string | undefined = undefined,
): Promise<string[]> {
let url = `https://github.com/${owner}/${repo}/releases.atom`;
if (lastVersion) {
url += `?after=${lastVersion}`;
}
// FIXME do we need to handle 404?
const page = await fetch(url);
const text = await page.text();
return [
...text.matchAll(
/\<id\>tag\:github\.com\,2008\:Repository\/\d+\/(.*?)\<\/id\>/g,
),
].map((x) => x[1]);
}
// export for testing purposes
// FIXME this should really be lazy, we shouldn't always iterate everything...
export const GR_CACHE: Map<string, string[]> = new Map<string, string[]>();
async function githubReleases(
owner: string,
repo: string,
cache: Map<string, string[]> = GR_CACHE,
): Promise<string[]> {
const cacheKey = `${owner}/${repo}`;
if (cache.has(cacheKey)) {
return cache.get(cacheKey)!;
}
const versions = await githubDownloadReleases(owner, repo);
if (versions.length === 10) {
let lastVersion: string | undefined = undefined;
// arbitrarily we're going to limit to 5 pages...?
let i = 0;
while (lastVersion !== versions[versions.length - 1] && i < 5) {
i++;
lastVersion = versions[versions.length - 1];
versions.push(...await githubDownloadReleases(owner, repo, lastVersion));
}
}
cache.set(cacheKey, versions);
return versions;
}
interface VersionsJson {
latest?: string;
versions?: string[];
}
const DL_CACHE: Map<string, string[]> = new Map<string, string[]>();
export class DenoLand implements RegistryUrl {
url: string;
constructor(url: string) {
this.url = url;
}
name(): string {
const [, stdGroup, xGroup] = this.url.match(
/deno\.land\/(?:(std)|x\/([^/@]*))/,
)!;
return stdGroup ?? xGroup;
}
async all(): Promise<string[]> {
const name = this.name();
if (DL_CACHE.has(name)) {
return DL_CACHE.get(name)!;
}
try {
const json: VersionsJson =
await (await fetch(`https://cdn.deno.land/${name}/meta/versions.json`))
.json();
if (!json.versions) {
throw new Error(`versions.json for ${name} has incorrect format`);
}
DL_CACHE.set(name, json.versions);
return json.versions;
} catch (err) {
// TODO this could be a permissions error e.g. no --allow-net...
console.error(`error getting versions for ${name}`);
throw err;
}
}
at(version: string): RegistryUrl {
const url = defaultAt(this, version);
return new DenoLand(url);
}
version(): string {
return defaultVersion(this);
}
regexp = /https?:\/\/deno.land\/(?:std\@[^\'\"]*|x\/[^\/\"\']*?\@[^\'\"]*)/;
}
const NPM_CACHE: Map<string, string[]> = new Map<string, string[]>();
export class Npm implements RegistryUrl {
url: string;
parseRegex = /^npm:(\@[^/]+\/[^@/]+|[^@/]+)(?:\@([^/]+))?(.*)/;
constructor(url: string) {
this.url = url;
}
name(): string {
const [, name] = this.url.match(this.parseRegex)!;
return name;
}
async all(): Promise<string[]> {
const name = this.name();
if (NPM_CACHE.has(name)) {
return NPM_CACHE.get(name)!;
}
try {
const json: VersionsJson =
await (await fetch(`https://registry.npmjs.org/${name}`))
.json();
if (!json.versions) {
throw new Error(`versions.json for ${name} has incorrect format`);
}
const versions = Object.keys(json.versions).reverse();
NPM_CACHE.set(name, versions);
return versions;
} catch (err) {
// TODO this could be a permissions error e.g. no --allow-net...
console.error(`error getting versions for ${name}`);
throw err;
}
}
at(version: string): RegistryUrl {
const [, name, _, files] = this.url.match(this.parseRegex)!;
const url = `npm:${name}@${version}${files}`;
return new Npm(url);
}
version(): string {
const [, _, version] = this.url.match(this.parseRegex)!;
if (version === null) {
throw Error(`Unable to find version in ${this.url}`);
}
return version;
}
regexp = /npm:(\@[^/]+\/[^@/]+|[^@/]+)(?:\@([^\/\"\']+))?[^\'\"]/;
}
async function unpkgVersions(name: string): Promise<string[]> {
const page = await fetch(`https://unpkg.com/browse/${name}/`);
const text = await page.text();
// naively, we grab all the options
const m = [...text.matchAll(/\<option[^\<\>]* value\=\"(.*?)\"\>/g)];
m.reverse();
return m.map((x) => x[1]);
}
interface PackageInfo {
parts: string[];
scope: string;
packageName: string;
version: string;
}
function defaultInfo(that: RegistryUrl): PackageInfo {
const parts = that.url.split("/");
const [packageName, version] = parts[4].split("@");
if (parts[3] === undefined) {
throw new Error(`Package scope not found in ${that.url}`);
}
if (packageName === undefined) {
throw new Error(`Package name not found in ${that.url}`);
}
if (version === undefined) {
throw new Error(`Unable to find version in ${that.url}`);
}
return {
scope: parts[3],
packageName,
version,
parts,
};
}
function defaultScopeAt(that: RegistryUrl, version: string): string {
const { parts, packageName } = defaultInfo(that);
parts[4] = `${packageName}@${version}`;
return parts.join("/");
}
export class UnpkgScope implements RegistryUrl {
url: string;
parts(): PackageInfo {
return defaultInfo(this);
}
constructor(url: string) {
this.url = url;
}
async all(): Promise<string[]> {
const { scope, packageName } = this.parts();
return await unpkgVersions(`${scope}/${packageName}`);
}
at(version: string): RegistryUrl {
const url = defaultScopeAt(this, version);
return new UnpkgScope(url);
}
version(): string {
return this.parts().version;
}
regexp = /https?:\/\/unpkg\.com\/@[^\/\"\']*?\/[^\/\"\']*?\@[^\'\"]*/;
}
export class Unpkg implements RegistryUrl {
url: string;
name(): string {
return defaultName(this);
}
constructor(url: string) {
this.url = url;
}
async all(): Promise<string[]> {
return await unpkgVersions(this.name());
}
at(version: string): RegistryUrl {
const url = defaultAt(this, version);
return new Unpkg(url);
}
version(): string {
return defaultVersion(this);
}
regexp = /https?:\/\/unpkg.com\/[^\/\"\']*?\@[^\'\"]*/;
}
export class Jspm implements RegistryUrl {
url: string;
name(): string {
return defaultName(this);
}
constructor(url: string) {
this.url = url;
}
async all(): Promise<string[]> {
return await unpkgVersions(this.name());
}
at(version: string): RegistryUrl {
const url = defaultAt(this, version);
return new Jspm(url);
}
version(): string {
return defaultVersion(this);
}
regexp = /https?:\/\/dev.jspm.io\/[^\/\"\']*?\@[^\'\"]*/;
}
export class Denopkg implements RegistryUrl {
url: string;
owner(): string {
return this.url.split("/")[3];
}
repo(): string {
return defaultName(this);
}
constructor(url: string) {
this.url = url;
}
async all(): Promise<string[]> {
return await githubReleases(this.owner(), this.repo());
}
at(version: string): RegistryUrl {
const url = defaultAt(this, version);
return new Denopkg(url);
}
version(): string {
return defaultVersion(this);
}
regexp = /https?:\/\/denopkg.com\/[^\/\"\']*?\/[^\/\"\']*?\@[^\'\"]*/;
}
export class PaxDenoDev implements RegistryUrl {
url: string;
owner(): string {
return this.url.split("/")[3];
}
repo(): string {
return defaultName(this);
}
constructor(url: string) {
this.url = url;
}
async all(): Promise<string[]> {
return await githubReleases(this.owner(), this.repo());
}
at(version: string): RegistryUrl {
const url = defaultAt(this, version);
return new PaxDenoDev(url);
}
version(): string {
return defaultVersion(this);
}
regexp = /https?:\/\/pax.deno.dev\/[^\/\"\']*?\/[^\/\"\']*?\@[^\'\"]*/;
}
export class PikaScope implements RegistryUrl {
url: string;
parts(): PackageInfo {
return defaultInfo(this);
}
constructor(url: string) {
this.url = url;
}
async all(): Promise<string[]> {
const { scope, packageName } = this.parts();
return await unpkgVersions(`${scope}/${packageName}`);
}
at(version: string): RegistryUrl {
const url = defaultScopeAt(this, version);
return new PikaScope(url);
}
version(): string {
return this.parts().version;
}
regexp =
/https?:\/\/cdn\.pika\.dev(\/\_)?\/@[^\/\"\']*?\/[^\/\"\']*?\@[^\'\"]*/;
}
export class Pika implements RegistryUrl {
url: string;
name(): string {
return defaultName(this);
}
constructor(url: string) {
this.url = url;
}
async all(): Promise<string[]> {
return await unpkgVersions(this.name());
}
at(version: string): RegistryUrl {
const url = defaultAt(this, version);
return new Pika(url);
}
version(): string {
return defaultVersion(this);
}
regexp = /https?:\/\/cdn.pika.dev(\/\_)?\/[^\/\"\']*?\@[^\'\"]*/;
}
export class SkypackScope implements RegistryUrl {
url: string;
parts(): PackageInfo {
return defaultInfo(this);
}
constructor(url: string) {
this.url = url;
}
async all(): Promise<string[]> {
const { scope, packageName } = this.parts();
return await unpkgVersions(`${scope}/${packageName}`);
}
at(version: string): RegistryUrl {
const url = defaultScopeAt(this, version);
return new SkypackScope(url);
}
version(): string {
return this.parts().version;
}
regexp =
/https?:\/\/cdn\.skypack\.dev(\/\_)?\/@[^\/\"\']*?\/[^\/\"\']*?\@[^\'\"]*/;
}
export class Skypack implements RegistryUrl {
url: string;
name(): string {
return defaultName(this);
}
constructor(url: string) {
this.url = url;
}
async all(): Promise<string[]> {
return await unpkgVersions(this.name());
}
at(version: string): RegistryUrl {
const url = defaultAt(this, version);
return new Skypack(url);
}
version(): string {
return defaultVersion(this);
}
regexp = /https?:\/\/cdn.skypack.dev(\/\_)?\/[^\/\"\']*?\@[^\'\"]*/;
}
export class EsmShScope implements RegistryUrl {
url: string;
parts(): PackageInfo {
return defaultInfo(this);
}
constructor(url: string) {
this.url = url;
}
async all(): Promise<string[]> {
const { scope, packageName } = this.parts();
return await unpkgVersions(`${scope}/${packageName}`);
}
at(version: string): RegistryUrl {
const url = defaultScopeAt(this, version);
return new EsmShScope(url);
}
version(): string {
return this.parts().version;
}
regexp = /https?:\/\/esm\.sh\/@[^\/\"\']*?\/[^\/\"\']*?\@[^\'\"]*/;
}
export class EsmSh implements RegistryUrl {
url: string;
name(): string {
return defaultName(this);
}
constructor(url: string) {
this.url = url;
}
async all(): Promise<string[]> {
return await unpkgVersions(this.name());
}
at(version: string): RegistryUrl {
const url = defaultAt(this, version);
return new EsmSh(url);
}
version(): string {
return defaultVersion(this);
}
regexp = /https?:\/\/esm.sh\/[^\/\"\']*?\@[^\'\"]*/;
}
export class GithubRaw implements RegistryUrl {
url: string;
constructor(url: string) {
this.url = url;
}
all(): Promise<string[]> {
const [, , , user, repo] = this.url.split("/");
return githubReleases(user, repo);
}
at(version: string): RegistryUrl {
const parts = this.url.split("/");
parts[5] = version;
return new GithubRaw(parts.join("/"));
}
version(): string {
const v = this.url.split("/")[5];
if (v === undefined) {
throw Error(`Unable to find version in ${this.url}`);
}
return v;
}
regexp =
/https?:\/\/raw\.githubusercontent\.com\/[^\/\"\']+\/[^\/\"\']+\/(?!master)[^\/\"\']+\/[^\'\"]*/;
}
export class JsDelivr implements RegistryUrl {
url: string;
constructor(url: string) {
this.url = url;
}
parts(): { parts: string[]; repo: string; user: string; version: string } {
const parts = this.url.split("/");
const [repo, version] = parts[5].split("@");
return {
user: parts[4],
repo,
version,
parts,
};
}
all(): Promise<string[]> {
const { user, repo } = this.parts();
return githubReleases(user, repo);
}
at(version: string): RegistryUrl {
const { parts, repo } = this.parts();
parts[5] = `${repo}@${version}`;
return new GithubRaw(parts.join("/"));
}
version(): string {
const { version } = this.parts();
if (version === undefined) {
throw Error(`Unable to find version in ${this.url}`);
}
return version;
}
regexp =
/https?:\/\/cdn\.jsdelivr\.net\/gh\/[^\/\"\']+\/[^\/\"\']+@(?!master)[^\/\"\']+\/[^\'\"]*/;
}
async function gitlabDownloadReleases(
owner: string,
repo: string,
page: number,
): Promise<string[]> {
const url =
`https://gitlab.com/${owner}/${repo}/-/tags?format=atom&page=${page}`;
const text = await (await fetch(url)).text();
return [
...text.matchAll(
/\<id\>https\:\/\/gitlab.com.+\/-\/tags\/(.+?)\<\/id\>/g,
),
].map((x) => x[1]);
}
// export for testing purposes
// FIXME this should really be lazy, we shouldn't always iterate everything...
export const GL_CACHE: Map<string, string[]> = new Map<string, string[]>();
async function gitlabReleases(
owner: string,
repo: string,
cache: Map<string, string[]> = GL_CACHE,
): Promise<string[]> {
const cacheKey = `${owner}/${repo}`;
if (cache.has(cacheKey)) {
return cache.get(cacheKey)!;
}
// to roughly match GitHub above (5 pages, 10 releases each), we'll
// limit to 3 pages, 20 releases each
let i = 1;
const versions = await gitlabDownloadReleases(owner, repo, i);
if (versions.length === 20) {
let lastVersion: string | undefined = undefined;
while (lastVersion !== versions[versions.length - 1] && i <= 3) {
i++;
lastVersion = versions[versions.length - 1];
versions.push(...await gitlabDownloadReleases(owner, repo, i));
}
}
cache.set(cacheKey, versions);
return versions;
}
export class GitlabRaw implements RegistryUrl {
url: string;
constructor(url: string) {
this.url = url;
}
all(): Promise<string[]> {
const [, , , user, repo] = this.url.split("/");
return gitlabReleases(user, repo);
}
at(version: string): RegistryUrl {
const parts = this.url.split("/");
parts[7] = version;
return new GithubRaw(parts.join("/"));
}
version(): string {
const v = this.url.split("/")[7];
if (v === undefined) {
throw Error(`Unable to find version in ${this.url}`);
}
return v;
}
regexp =
/https?:\/\/gitlab\.com\/[^\/\"\']+\/[^\/\"\']+\/-\/raw\/(?!master)[^\/\"\']+\/[^\'\"]*/;
}
interface NestLandResponse {
// a list of names of the form "<repo>@<version>"
packageUploadNames?: string[];
}
const NL_CACHE: Map<string, string[]> = new Map<string, string[]>();
async function nestlandReleases(
repo: string,
cache: Map<string, string[]> = NL_CACHE,
): Promise<string[]> {
if (cache.has(repo)) {
return cache.get(repo)!;
}
const url = `https://x.nest.land/api/package/${repo}`;
const { packageUploadNames }: NestLandResponse = await (await fetch(url))
.json();
if (!packageUploadNames) {
return [];
}
// reverse so newest versions are first
return packageUploadNames.map((name) => name.split("@")[1]).reverse();
}
export class NestLand implements RegistryUrl {
url: string;
constructor(url: string) {
this.url = url;
}
all(): Promise<string[]> {
const parts = this.url.split("/");
return nestlandReleases(parts[3].split("@")[0]);
}
at(version: string): RegistryUrl {
const url = defaultAt(this, version);
return new NestLand(url);
}
version(): string {
return defaultVersion(this);
}
regexp =
/https?:\/\/x\.nest\.land\/[^\/\"\']+@(?!master)[^\/\"\']+\/[^\'\"]*/;
}
export const REGISTRIES = [
DenoLand,
UnpkgScope,
Unpkg,
Denopkg,
PaxDenoDev,
Jspm,
PikaScope,
Pika,
SkypackScope,
Skypack,
EsmShScope,
EsmSh,
GithubRaw,
GitlabRaw,
JsDelivr,
NestLand,
Npm,
];