-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregistry.ts
79 lines (66 loc) · 2.34 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
import type {Flags} from "../flag_parser.ts";
export interface ModulesListPage {
modules: {
name: string;
description?: string;
starCount?: number;
owner?: string;
}[];
page: number;
pageSize: number;
totalModules: number;
totalPages: number;
query?: string;
}
export interface ModuleInfo {
origin?: string;
info?: {
name: string;
description?: string;
start_count?: number;
versions?: string[];
latestVersion?: string;
repository?: string;
moduleRoute?: string;
},
invalidVersion?: boolean;
currentVersion?: string,
uploadedAt?: Date;
flags?: Flags;
readmePath?: string;
readmeText?: string;
}
export abstract class Registry {
addonUrl?: string;
fromSettings?: boolean;
static cache = new Map<string, unknown>();
abstract getRegistryInfo(): {name: string, key: string, icon?: string, description?: string, url?: string};
abstract getWellKnownPath(): string;
abstract getModulesList(query?: string, page?: number, pageSize?: number): Promise<ModulesListPage>;
abstract getModuleInfo(moduleName: string, version?: string): Promise<ModuleInfo | undefined>;
abstract getAllModuleNames(): Promise<string[]>;
abstract getVersionsOfModule(moduleName: string, version?: string): Promise<string[]>;
async fetch<T>(path: string, opts?: {cache?: boolean, text?: boolean}): Promise<T|undefined> { // TODO cli flag to overwrite cache --no-cache
const pathUrl = new URL(path);
if((await Deno.permissions.request({name: "net", host: pathUrl.host})).state !== 'granted') {
console.error('NO NET');
return undefined;
}
if(opts?.cache && Registry.cache.has(path)) {
return Registry.cache.get(path) as T;
}
try {
const fetched = await fetch(pathUrl)
const result = await (opts?.text ? fetched.text() : fetched.json());
if(opts?.cache) {
Registry.cache.set(pathUrl.toString(), result);
}
return result;
} catch (e){
return undefined;
}
}
guessReadmePath(paths: string[]) {
return paths.map(p => ({original: p, lower: p.toLowerCase()})).sort().find(p => p.lower.includes("readme.md"))?.original;
}
}