forked from denoland/docland
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.ts
553 lines (519 loc) · 15.1 KB
/
docs.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
// Copyright 2021-2022 the Deno authors. All rights reserved. MIT license.
import {
colors,
doc,
type DocNode,
type DocNodeInterface,
type DocNodeNamespace,
httpErrors,
JSONC,
type LoadResponse,
} from "./deps.ts";
import { assert } from "./util.ts";
interface ConfigFileJson {
importMap?: string;
}
/** An object which represents an "index" of a library/package. */
export interface IndexStructure {
/** An object that describes the structure of the library, where the key is
* the containing folder and the value is an array of modules that represent
* the the "contents" of the folder. */
structure: SerializeMap<string[]>;
/** For modules in the structure, any doc node entries available for the
* module. */
entries: SerializeMap<DocNode[]>;
}
interface ApiModuleData {
data: {
name: string;
description: string;
"star_count": number;
};
}
interface PackageMetaListing {
path: string;
size: number;
type: "file" | "dir";
}
interface PackageMeta {
"uploaded_at": string;
"directory_listing": PackageMetaListing[];
"upload_options": {
type: string;
repository: string;
ref: string;
};
}
interface PackageVersions {
latest: string;
versions: string[];
}
const EXT = [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"];
const INDEX_MODULES = ["mod", "lib", "main", "index"].flatMap((idx) =>
EXT.map((ext) => `${idx}${ext}`)
);
const MAX_CACHE_SIZE = parseInt(Deno.env.get("MAX_CACHE_SIZE") ?? "", 10) ||
25_000_000;
const RE_IGNORED_MODULE =
/(\/[_.].|(test|.+_test)\.(js|jsx|mjs|cjs|ts|tsx|mts|cts)$)/i;
const RE_MODULE_EXT = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/i;
const RE_PRIVATE_PATH = /\/([_.].|testdata)/;
const S3_BUCKET =
"http://deno-registry2-prod-storagebucket-b3a31d16.s3-website-us-east-1.amazonaws.com/";
const DENO_API = "https://api.deno.land/modules/";
export class SerializeMap<V> extends Map<string, V> {
toJSON(): Record<string, V> {
return Object.fromEntries(this.entries());
}
}
const cachedSpecifiers = new Set<string>();
const cachedResources = new Map<string, LoadResponse>();
const cachedEntries = new Map<string, DocNode[]>();
let cacheSize = 0;
const cachedIndexes = new Map<string, IndexStructure | undefined>();
const cachedPackageData = new Map<string, ApiModuleData | undefined>();
const cachedPackageMeta = new Map<
string,
Map<string, PackageMeta | undefined>
>();
const cachedPackageVersions = new Map<string, PackageVersions | undefined>();
let cacheCheckQueued = false;
/** Check the cache, evicting any cached data using a LRU schema, until the
* cache is below the threshold. */
function checkCache() {
if (cacheSize > MAX_CACHE_SIZE) {
const toEvict: string[] = [];
for (const specifier of cachedSpecifiers) {
const loadResponse = cachedResources.get(specifier);
assert(loadResponse);
assert(loadResponse.kind === "module");
toEvict.push(specifier);
cacheSize -= loadResponse.content.length;
if (cacheSize <= MAX_CACHE_SIZE) {
break;
}
}
console.log(
` ${colors.yellow("evicting")}: ${
colors.bold(`${toEvict.length} specifiers`)
} from cache`,
);
for (const evict of toEvict) {
cachedResources.delete(evict);
cachedSpecifiers.delete(evict);
cachedEntries.delete(evict);
}
}
cacheCheckQueued = false;
}
/** Determine if a given specifier actually resolves to a redirected
* specifier, caching the load responses */
export async function checkRedirect(
specifier: string,
): Promise<string | undefined> {
if (!specifier.startsWith("http")) {
return undefined;
}
const cached = cachedResources.get(specifier);
let finalSpecifier = specifier;
if (cached) {
finalSpecifier = cached.specifier;
} else {
try {
const res = await fetch(specifier, { redirect: "follow" });
if (res.status !== 200) {
// ensure that resources are not leaked
await res.arrayBuffer();
}
const content = await res.text();
const xTypeScriptTypes = res.headers.get("x-typescript-types");
const headers: Record<string, string> = {};
for (const [key, value] of res.headers) {
headers[key.toLowerCase()] = value;
}
cachedResources.set(specifier, {
specifier: res.url,
kind: "module",
headers,
content,
});
cachedSpecifiers.add(specifier);
cacheSize += content.length;
enqueueCheck();
finalSpecifier = xTypeScriptTypes
? new URL(xTypeScriptTypes, res.url).toString()
: res.url;
} catch {
// just swallow errors
}
}
return specifier === finalSpecifier ? undefined : finalSpecifier;
}
function enqueueCheck() {
if (!cacheCheckQueued) {
cacheCheckQueued = true;
queueMicrotask(checkCache);
}
}
const CONFIG_FILES = ["deno.jsonc", "deno.json"] as const;
/** Given a module and version, attempt to resolve an import map specifier from
* a Deno configuration file. If none can be resolved, `undefined` is
* resolved. */
export async function getImportMapSpecifier(
module: string,
version: string,
): Promise<string | undefined> {
let result;
for (const configFile of CONFIG_FILES) {
result = await load(
`https://deno.land/x/${module}@${version}/${configFile}`,
);
if (result) {
break;
}
}
if (result?.kind === "module") {
const { specifier, content } = result;
const configFileJson: ConfigFileJson = JSONC.parse(content);
if (typeof configFileJson.importMap === "string") {
return new URL(configFileJson.importMap, specifier).toString();
}
return undefined;
}
}
function getDirs(path: string, packageMeta: PackageMeta) {
if (path.endsWith("/")) {
path = path.slice(0, -1);
}
if (isDir(path, packageMeta)) {
const dirs: string[] = [];
for (const { path: p, type } of packageMeta.directory_listing) {
if (
p.startsWith(path) && type === "dir" &&
!p.slice(path.length).match(RE_PRIVATE_PATH)
) {
dirs.push(p);
}
}
return dirs;
}
}
export async function getEntries(
url: string,
importMap?: string,
): Promise<DocNode[]> {
let entries = cachedEntries.get(url);
if (!entries) {
try {
entries = mergeEntries(await doc(url, { load, importMap }));
cachedEntries.set(url, entries);
} catch (e) {
if (e instanceof Error) {
if (e.message.includes("Unable to load specifier")) {
throw new httpErrors.NotFound(`The module "${url}" cannot be found`);
} else {
throw new httpErrors.BadRequest(`Bad request: ${e.message}`);
}
} else {
throw new httpErrors.InternalServerError("Unexpected object.");
}
}
}
return entries;
}
function getIndex(dir: string, packageMeta: PackageMeta) {
const files: string[] = [];
for (const { path, type } of packageMeta.directory_listing) {
if (path.startsWith(dir) && type === "file") {
files.push(path);
}
}
for (const index of INDEX_MODULES) {
const needle = `${dir}/${index}`;
const item = files.find((file) => file.toLowerCase() === needle);
if (item) {
return item;
}
}
}
export async function getLatest(pkg: string): Promise<string | undefined> {
const packageVersions = await getPackageVersions(pkg);
return packageVersions?.latest;
}
function getModules(path: string, packageMeta: PackageMeta) {
if (path.endsWith("/")) {
path = path.slice(0, -1);
}
if (isDir(path, packageMeta)) {
const modules: string[] = [];
for (const { path: p, type } of packageMeta.directory_listing) {
const slice = p.slice(path.length);
if (
p.startsWith(path) && type === "file" && slice.lastIndexOf("/") === 0 &&
p.match(RE_MODULE_EXT) &&
!slice.match(RE_IGNORED_MODULE)
) {
modules.push(p);
}
}
if (modules.length) {
return modules;
}
}
}
export async function getPackageDescription(
pkg: string,
): Promise<string | undefined> {
if (!cachedPackageData.has(pkg)) {
const res = await fetch(`${DENO_API}${pkg}`);
let body: ApiModuleData | undefined;
if (res.status === 200) {
body = await res.json();
}
cachedPackageData.set(pkg, body);
}
return cachedPackageData.get(pkg)?.data.description;
}
async function getPackageMeta(
pkg: string,
version: string,
): Promise<PackageMeta | undefined> {
if (!cachedPackageMeta.has(pkg)) {
cachedPackageMeta.set(pkg, new Map());
}
const versionCache = cachedPackageMeta.get(pkg)!;
if (!versionCache.get(version)) {
const res = await fetch(
`${S3_BUCKET}${pkg}/versions/${version}/meta/meta.json`,
);
if (res.status === 200) {
const packageMeta = await res.json() as PackageMeta;
versionCache.set(version, packageMeta);
} else {
versionCache.set(version, undefined);
}
}
return versionCache.get(version);
}
export async function getPackageVersions(
pkg: string,
): Promise<PackageVersions | undefined> {
if (!cachedPackageVersions.has(pkg)) {
const res = await fetch(`${S3_BUCKET}${pkg}/meta/versions.json`);
if (res.status === 200) {
const packageVersions = await res.json() as PackageVersions;
cachedPackageVersions.set(pkg, packageVersions);
} else {
cachedPackageVersions.set(pkg, undefined);
}
}
return cachedPackageVersions.get(pkg);
}
async function getIndexEntries(
proto: string,
host: string,
pkg: string,
version: string,
structure: Map<string, string[]>,
): Promise<SerializeMap<DocNode[]>> {
const indexEntries = new SerializeMap<DocNode[]>();
for (const mods of structure.values()) {
for (const mod of mods) {
const url = pkg === "std"
? `${proto}/${host}/std@${version}${mod}`
: `${proto}/${host}/x/${pkg}@${version}${mod}`;
try {
const importMap = await getImportMapSpecifier(pkg, version);
const entries = await getEntries(url, importMap);
if (entries.length) {
indexEntries.set(mod, entries);
}
} catch {
// we just swallow errors here
}
}
}
return indexEntries;
}
export async function getIndexStructure(
proto: string,
host: string,
pkg: string,
version: string,
path = "/",
): Promise<IndexStructure | undefined> {
const packageMeta = await getPackageMeta(pkg, version);
if (packageMeta) {
const dirs = getDirs(path, packageMeta);
if (dirs) {
const structure = new SerializeMap<string[]>();
for (const dir of dirs) {
const index = getIndex(dir, packageMeta);
if (index) {
structure.set(dir, [index]);
} else {
const modules = getModules(dir, packageMeta);
if (modules) {
structure.set(dir, modules);
}
}
}
if (structure.size) {
const entries = await getIndexEntries(
proto,
host,
pkg,
version,
structure,
);
if (entries.size) {
return { structure, entries };
}
}
}
}
}
function isDir(path: string, packageMeta: PackageMeta) {
if (path === "") {
return true;
}
for (const { path: p, type } of packageMeta.directory_listing) {
if (path === p) {
return type === "dir";
}
}
return false;
}
async function load(
specifier: string,
): Promise<LoadResponse | undefined> {
const url = new URL(specifier);
try {
switch (url.protocol) {
case "file:": {
console.error(`local specifier requested: ${specifier}`);
return undefined;
}
case "http:":
case "https:": {
if (cachedResources.has(specifier)) {
cachedSpecifiers.delete(specifier);
cachedSpecifiers.add(specifier);
return cachedResources.get(specifier);
}
const response = await fetch(String(url), { redirect: "follow" });
if (response.status !== 200) {
// ensure that resources are not leaked
await response.arrayBuffer();
return undefined;
}
const content = await response.text();
const headers: Record<string, string> = {};
for (const [key, value] of response.headers) {
headers[key.toLowerCase()] = value;
}
const loadResponse: LoadResponse = {
kind: "module",
specifier: response.url,
headers,
content,
};
cachedResources.set(specifier, loadResponse);
cachedSpecifiers.add(specifier);
cacheSize += content.length;
enqueueCheck();
return loadResponse;
}
default:
return undefined;
}
} catch {
return undefined;
}
}
const decoder = new TextDecoder();
export async function maybeCacheStatic(url: string, host: string) {
if (url.startsWith("deno") && !cachedEntries.has(url)) {
try {
const [lib, version] = host.split("@");
const data = await Deno.readFile(
new URL(
`./static/${lib}${version ? `_${version}` : ""}.json`,
import.meta.url,
),
);
const entries = mergeEntries(JSON.parse(decoder.decode(data)));
cachedEntries.set(url, entries);
} catch (e) {
console.log("error fetching static");
console.log(e);
}
}
}
export async function getStaticIndex(
pkg: string,
version: string,
): Promise<IndexStructure | undefined> {
const key = `${pkg}_${version}`;
if (!cachedIndexes.has(key)) {
try {
const data = await Deno.readFile(
new URL(`./static/${key}.json`, import.meta.url),
);
const index = JSON.parse(decoder.decode(data), (key, value) => {
if (
typeof value === "object" &&
(key === "structure" || key === "entries")
) {
return new SerializeMap(Object.entries(value));
} else {
return value;
}
}) as IndexStructure;
cachedIndexes.set(key, index);
} catch {
// just swallow errors here
}
}
return cachedIndexes.get(key);
}
function mergeEntries(entries: DocNode[]) {
const merged: DocNode[] = [];
const namespaces = new Map<string, DocNodeNamespace>();
const interfaces = new Map<string, DocNodeInterface>();
for (const node of entries) {
if (node.kind === "namespace") {
const namespace = namespaces.get(node.name);
if (namespace) {
namespace.namespaceDef.elements.push(...node.namespaceDef.elements);
if (!namespace.jsDoc) {
namespace.jsDoc = node.jsDoc;
}
} else {
namespaces.set(node.name, node);
merged.push(node);
}
} else if (node.kind === "interface") {
const int = interfaces.get(node.name);
if (int) {
int.interfaceDef.callSignatures.push(
...node.interfaceDef.callSignatures,
);
int.interfaceDef.indexSignatures.push(
...node.interfaceDef.indexSignatures,
);
int.interfaceDef.methods.push(...node.interfaceDef.methods);
int.interfaceDef.properties.push(...node.interfaceDef.properties);
if (!int.jsDoc) {
int.jsDoc = node.jsDoc;
}
} else {
interfaces.set(node.name, node);
merged.push(node);
}
} else {
merged.push(node);
}
}
return merged;
}