-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugins.ts
More file actions
215 lines (199 loc) · 7.05 KB
/
plugins.ts
File metadata and controls
215 lines (199 loc) · 7.05 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
import { checkGithubRepoExists, getLatestReleaseInfo } from "./utils/mod.js";
const tagPattern = "([A-Za-z0-9\._]+)";
// repos may only contain alphanumeric, underscores, hyphens, and period
const repoNamePattern = "([A-Za-z0-9\-\._]+)";
const dprintWasmPluginPattern = new URLPattern({
pathname: `/${repoNamePattern}-${tagPattern}.wasm`,
});
const dprintProcessPluginPattern = new URLPattern({
pathname: `/${repoNamePattern}-${tagPattern}.json`,
});
// usernames may only contain alphanumeric and hypens
const usernamePattern = `([A-Za-z0-9\-]+)`;
const userRepoPattern = `${usernamePattern}/${repoNamePattern}`;
const userWasmPluginPattern = new URLPattern({
pathname: `/${userRepoPattern}-${tagPattern}.wasm`,
});
const userProcessPluginPattern = new URLPattern({
pathname: `/${userRepoPattern}-${tagPattern}.json`,
});
const userSchemaPattern = new URLPattern({
pathname: `/${userRepoPattern}/${tagPattern}/schema.json`,
});
// known repos where shortname resolves to dprint-plugin-<name>,
// avoiding a GitHub API call to check existence
const KNOWN_DPRINT_PLUGIN_REPOS = new Set([
// dprint org
"dprint/dprint-plugin-typescript",
"dprint/dprint-plugin-json",
"dprint/dprint-plugin-markdown",
"dprint/dprint-plugin-toml",
"dprint/dprint-plugin-dockerfile",
"dprint/dprint-plugin-biome",
"dprint/dprint-plugin-oxc",
"dprint/dprint-plugin-mago",
"dprint/dprint-plugin-ruff",
"dprint/dprint-plugin-jupyter",
"dprint/dprint-plugin-prettier",
"dprint/dprint-plugin-roslyn",
"dprint/dprint-plugin-rustfmt",
"dprint/dprint-plugin-yapf",
"dprint/dprint-plugin-exec",
"dprint/dprint-plugin-sql",
// community
"jakebailey/dprint-plugin-gofumpt",
"malobre/dprint-plugin-vue",
]);
// repos where the short name IS the repo name (no dprint-plugin- prefix)
const KNOWN_NON_PREFIXED_REPOS = new Set([
"g-plane/malva",
"g-plane/markup_fmt",
"g-plane/pretty_yaml",
"g-plane/pretty_graphql",
"lucacasonato/mf2-tools",
]);
export function isAssetAllowedRepo(username: string, _repo: string) {
switch (username) {
case "dprint":
return true;
default:
return false;
}
}
const assetNamePattern = "([A-Za-z0-9\\-\\._]+)";
const assetPattern = new URLPattern({
pathname: `/${userRepoPattern}/${tagPattern}/asset/${assetNamePattern}`,
});
export function tryResolveAssetUrl(url: URL): { githubUrl: string; shouldCache: boolean } | undefined {
const result = assetPattern.exec(url);
if (!result) {
return undefined;
}
const username = result.pathname.groups[0]!;
const repo = result.pathname.groups[1]!;
const tag = result.pathname.groups[2]!;
const assetName = result.pathname.groups[3]!;
const githubUrl = `https://github.com/${username}/${repo}/releases/download/${tag}/${assetName}`;
const shouldCache = isAssetAllowedRepo(username, repo);
return { githubUrl, shouldCache };
}
export interface PluginUrlResult {
githubUrl: string;
username: string;
repo: string;
tag: string;
}
export async function tryResolvePluginUrl(url: URL): Promise<PluginUrlResult | undefined> {
return dprintPluginTagPatternMapper(dprintWasmPluginPattern, url, "plugin.wasm")
?? dprintPluginTagPatternMapper(dprintProcessPluginPattern, url, "plugin.json")
?? (await userRepoTagPatternMapper(userWasmPluginPattern, url, "plugin.wasm"))
?? (await userRepoTagPatternMapper(userProcessPluginPattern, url, "plugin.json"));
}
export async function tryResolveSchemaUrl(url: URL) {
const result = await userRepoTagPatternMapper(userSchemaPattern, url, "schema.json");
return result?.githubUrl;
}
const userLatestPattern = new URLPattern({
pathname: `/${userRepoPattern}/latest.json`,
});
export async function tryResolveLatestJson(url: URL) {
const result = userLatestPattern.exec(url);
if (!result) {
return undefined;
}
const username = result.pathname.groups[0]!;
const shortRepoName = result.pathname.groups[1]!;
const latestInfo = await getLatestInfo(username, shortRepoName, url.origin);
if (latestInfo == null) {
return 404;
}
// include the bare minimum in case someone else wants to implement
// this behaviour on their server
return {
schemaVersion: 1,
url: latestInfo.url,
version: latestInfo.version,
checksum: latestInfo.checksum,
};
}
export async function getLatestInfo(username: string, repoName: string, origin: string) {
repoName = await getFullRepoName(username, repoName);
const releaseInfo = await getLatestReleaseInfo(username, repoName);
if (releaseInfo == null) {
return undefined;
}
const displayRepoName = repoName.replace(/^dprint-plugin-/, "");
const extension = releaseInfo.kind === "wasm" ? "wasm" : "json";
// include the bare minimum in case someone else wants to implement
// this behaviour on their server
return {
schemaVersion: 1,
url: username === "dprint"
? `${origin}/${displayRepoName}-${releaseInfo.tagName}.${extension}`
: `${origin}/${username}/${displayRepoName}-${releaseInfo.tagName}.${extension}`,
version: releaseInfo.tagName.replace(/^v/, ""),
checksum: releaseInfo.checksum,
downloadCount: releaseInfo.downloadCount,
};
}
function dprintPluginTagPatternMapper(
pattern: URLPattern,
url: URL,
fileName: string,
): PluginUrlResult | undefined {
const result = pattern.exec(url);
if (result) {
const pluginShortName = result.pathname.groups[0]!;
const tag = result.pathname.groups[1]!;
const repo = `dprint-plugin-${pluginShortName}`;
const githubUrl = tag === "latest"
? `https://github.com/dprint/${repo}/releases/latest/download/${fileName}`
: `https://github.com/dprint/${repo}/releases/download/${tag}/${fileName}`;
return { githubUrl, username: "dprint", repo, tag };
}
return undefined;
}
async function userRepoTagPatternMapper(
pattern: URLPattern,
url: URL,
fileName: string,
): Promise<PluginUrlResult | undefined> {
const result = pattern.exec(url);
if (result) {
const username = result.pathname.groups[0]!;
const repo = await getFullRepoName(username, result.pathname.groups[1]!);
if (username === "lucacasonato" && repo === "mf2-tools") {
switch (fileName) {
case "plugin.wasm":
fileName = "dprint-plugin-mf2.wasm";
break;
case "schema.json":
fileName = "dprint-plugin-mf2.schema.json";
break;
}
}
const tag = result.pathname.groups[2]!;
const githubUrl = tag === "latest"
? `https://github.com/${username}/${repo}/releases/latest/download/${fileName}`
: `https://github.com/${username}/${repo}/releases/download/${tag}/${fileName}`;
return { githubUrl, username, repo, tag };
}
return undefined;
}
async function getFullRepoName(username: string, repoName: string) {
if (repoName.startsWith("dprint-plugin-")) {
return repoName;
}
const fullName = `dprint-plugin-${repoName}`;
if (KNOWN_NON_PREFIXED_REPOS.has(`${username}/${repoName}`)) {
return repoName;
}
if (KNOWN_DPRINT_PLUGIN_REPOS.has(`${username}/${fullName}`)) {
return fullName;
}
if (await checkGithubRepoExists(username, fullName)) {
return fullName;
} else {
return repoName;
}
}