This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 624
/
Copy pathregistry_utils.ts
384 lines (348 loc) · 9.53 KB
/
registry_utils.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
// Copyright 2022-2023 the Deno authors. All rights reserved. MIT license.
import type { DocNode } from "deno_doc/types";
import type { LibDocPage, ModuleEntry } from "$apiland_types";
const NAME_REGEX = /^[a-z0-9_]{3,40}$/;
export const CDN_ENDPOINT = "https://cdn.deno.land/";
export interface CommonProps<T> {
isStd: boolean;
/** module name */
name: string;
/** module version */
version: string;
/** path in module */
path: string;
/** request URL */
url: URL;
/** url of the repo */
repositoryURL: string;
data: T;
}
// 100kb
export const MAX_SYNTAX_HIGHLIGHT_FILE_SIZE = 100 * 1024;
// 500kb
export const MAX_FILE_SIZE = 500 * 1024;
export async function getReadme(
name: string,
version: string,
entry: ModuleEntry,
): Promise<string | undefined> {
const url = getSourceURL(name, version, entry.path);
const res = await fetch(url);
if (!res.ok) {
await res.body?.cancel();
if (
res.status !== 400 &&
res.status !== 403 &&
res.status !== 404
) {
console.error(new Error(`${res.status}: ${res.statusText}`));
}
return undefined;
}
if (entry.size < MAX_SYNTAX_HIGHLIGHT_FILE_SIZE) {
return await res.text();
} else {
await res.body!.cancel();
return undefined;
}
}
export interface RawFile {
content: string;
highlight: boolean;
url: string;
}
export async function getRawFile(
name: string,
version: string,
path: string,
size: number,
): Promise<RawFile | Error> {
const url = getSourceURL(name, version, path);
const res = await fetch(url, { method: "GET" });
if (size < MAX_SYNTAX_HIGHLIGHT_FILE_SIZE) {
return {
content: await res.text(),
highlight: true,
url,
};
} else if (size < MAX_FILE_SIZE) {
return {
content: await res.text(),
highlight: false,
url,
};
} else {
await res.body!.cancel();
return new Error("Max display filesize exceeded");
}
}
export function getSourceURL(
module: string,
version: string,
path: string,
): string {
return `${CDN_ENDPOINT}${encodeURIComponent(module)}` +
`/versions/${encodeURIComponent(version)}/raw${encodeURI(path)}`;
}
function pathJoin(...parts: string[]) {
const replace = new RegExp("/{1,}", "g");
return parts.join("/").replace(replace, "/");
}
export function getRepositoryURL(
meta: {
repository: string;
ref: string;
subdir?: string;
},
path: string,
type = "blob",
): string {
return `https://github.com/${
pathJoin(
meta.repository,
type,
meta.ref,
meta.subdir ?? "",
path,
)
}`;
}
export interface VersionInfo {
latest: string;
versions: string[];
isLegacy: true;
}
export async function getVersionList(
module: string,
signal?: AbortSignal,
): Promise<VersionInfo | null> {
const url = `${CDN_ENDPOINT}${module}/meta/versions.json`;
const res = await fetch(url, {
headers: {
accept: "application/json",
},
signal,
});
if (res.status === 403 || res.status === 404) {
await res.body?.cancel();
return null;
}
if (res.status !== 200) {
throw Error(
`Got an error (${res.status}) while getting the version list:\n${await res
.text()}`,
);
}
return res.json();
}
export function fileTypeFromURL(filename: string): string | undefined {
const f = filename.toLowerCase();
if (f.endsWith(".ts")) {
return "typescript";
} else if (f.endsWith(".js") || f.endsWith(".mjs") || f.endsWith(".cjs")) {
return "javascript";
} else if (f.endsWith(".tsx")) {
return "tsx";
} else if (f.endsWith(".jsx")) {
return "jsx";
} else if (f.endsWith(".json")) {
return "json";
} else if (f.endsWith(".toml")) {
return "toml";
} else if (f.endsWith(".lock")) {
return "toml";
} else if (f.endsWith(".rs")) {
return "rust";
} else if (f.endsWith(".py")) {
return "python";
} else if (f.endsWith(".wasm")) {
return "wasm";
} else if (f.toLocaleLowerCase().endsWith("makefile")) {
return "makefile";
} else if (f.endsWith(".dockerfile") || f.endsWith("dockerfile")) {
return "dockerfile";
} else if (f.endsWith(".yml") || f.endsWith(".yaml")) {
return "yaml";
} else if (f.endsWith(".htm") || f.endsWith(".html")) {
return "html";
} else if (f.match(`\\.(?:markdown|mdown|mkdn|mdwn|mkd|md)$`)) {
return "markdown";
} else if (f.match(`\\.org$`)) {
return "org";
} else if (f.match(/\.(png|jpe?g|svg|webm|webp|avif)/)) {
return "image";
}
}
export function getModulePath(
name: string,
version?: string,
path?: string,
) {
return `${name === "std" ? "" : "/x"}/${name}${
version ? `@${encodeURIComponent(version)}` : ""
}${path ?? ""}`;
}
export async function fetchSource(
name: string,
version: string,
path: string,
): Promise<Response> {
const url = getSourceURL(
name,
version,
path.startsWith("/") ? path : `/${path}`,
);
const serverHeaders = [];
let lastErr;
for (let i = 0; i < 3; i++) {
try {
const resp = await fetch(url);
if (resp.status === 403 || resp.status === 404) {
await resp.body?.cancel();
return new Response("404 Not Found", {
status: 404,
headers: { "Access-Control-Allow-Origin": "*" },
});
}
if (!resp.ok) {
throw new TypeError(`non 2xx status code returned: ${resp.status}`, {
cause: await resp.text(),
});
}
resp.headers.get("server-timing")
? serverHeaders.push(
`attempt;val=${i + 1},${resp.headers.get("server-timing")}`,
)
: null;
const headers = new Headers(resp.headers);
if (serverHeaders.length > 0) {
headers.set("server-timing", serverHeaders.join(","));
}
if (
path.endsWith(".jsx") &&
!headers.get("content-type")?.includes("javascript")
) {
headers.set("content-type", "application/javascript");
} else if (
path.endsWith(".tsx") &&
!headers.get("content-type")?.includes("typescript")
) {
headers.set("content-type", "application/typescript");
}
headers.set("Access-Control-Allow-Origin", "*");
return new Response(resp.body, {
headers,
status: resp.status,
});
} catch (err) {
// TODO(lucacasonato): only retry on known retryable errors
console.warn("retrying on proxy error", err);
lastErr = err;
}
}
throw lastErr;
}
const ALT_LINENUMBER_MATCHER = /(.*):(\d+):\d+$/;
export function extractAltLineNumberReference(
url: string,
): { rest: string; line: number } | null {
const matches = ALT_LINENUMBER_MATCHER.exec(url);
if (matches === null) return null;
return {
rest: matches[1],
line: parseInt(matches[2]),
};
}
/** Matches to https://example.com/foo.ts type string */
const RE_IS_DENO_LAND_SCRIPT =
/^https:\/\/deno\.land\/.*\.(ts|js|tsx|jsx|mts|cts|mjs|cjs)$/;
const RE_IS_SCRIPT = /\.(ts|js|tsx|jsx|mts|cts|mjs|cjs)$/;
/** Matches to ./path/to/foo.ts type string */
const RE_IS_RELATIVE_PATH_SCRIPT =
/^\.\.?\/.*\.(ts|js|tsx|jsx|mts|cts|mjs|cjs)$/;
/** Tries to extract link target url from the given target specifier and baseUrl.
* The return value is 3-tuple of the link url, the specifier, and the quote character.
* Returns undefined if the target isn't url. */
export function extractLinkUrl(
target: string,
baseUrl: string,
): [string, string, string] | undefined {
const quote = target[0];
const specifier = target.slice(1, -1);
if (RE_IS_DENO_LAND_SCRIPT.test(specifier)) {
return [specifier, specifier, quote];
} else if (
RE_IS_RELATIVE_PATH_SCRIPT.test(specifier) &&
RE_IS_SCRIPT.test(baseUrl)
) {
try {
return [new URL(specifier, baseUrl).href, specifier, quote];
} catch {
return undefined;
}
}
return undefined;
}
function docAsDescription(doc: string) {
return doc.split("\n\n")[0].slice(0, 199);
}
/** Search parameters which are considered part of a canonical URL. */
const CANONICAL_SEARCH_PARAMS = ["s", "source", "doc", "unstable"];
export function getCanonicalUrl(url: URL, latestVersion: string) {
const canonical = new URL(url);
canonical.hostname = "deno.land";
canonical.port = "";
canonical.protocol = "https:";
canonical.pathname = canonical.pathname.replace(
/@[^/]+/,
`@${latestVersion}`,
);
canonical.search = "";
for (const param of CANONICAL_SEARCH_PARAMS) {
if (url.searchParams.has(param)) {
canonical.searchParams.set(param, url.searchParams.get(param)!);
}
}
return canonical;
}
/** For a LibDocPage, attempt to extract a description to be used with the
* content meta for the page. */
export function getLibDocPageDescription(data: LibDocPage): string | undefined {
if (data.kind === "librarySymbol") {
for (const docNode of data.docNodes) {
if (docNode.jsDoc?.doc) {
return docAsDescription(docNode.jsDoc.doc);
}
}
}
}
export function getDocAsDescription(
docNodes: DocNode[],
modDoc = false,
): string | undefined {
for (const docNode of docNodes) {
if (modDoc) {
if (docNode.kind === "moduleDoc") {
if (docNode.jsDoc.doc) {
return docAsDescription(docNode.jsDoc.doc);
} else {
return;
}
}
} else if (docNode.jsDoc?.doc) {
return docAsDescription(docNode.jsDoc.doc);
}
}
}
export async function validateModuleName(
name: string,
controller: AbortController,
) {
if (name === "" || !NAME_REGEX.test(name)) {
return "invalid";
} else {
return await getVersionList(name, controller.signal)
.then((e) => !e)
.catch(() => false);
}
}