-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathanalysis.ts
352 lines (334 loc) · 10.8 KB
/
analysis.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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
/**
* Utilities for analyzing modules at a detailed level.
*
* @module
*/
import $ from "dax";
import { createGraph } from "deno_graph";
import {
type Datastore,
DatastoreError,
entityToObject,
type KeyInit,
objectGetKey,
objectSetKey,
objectToEntity,
} from "google_datastore";
import type { Mutation } from "google_datastore/types";
import { parseFromJson } from "import_map";
import { getDatastore } from "./auth.ts";
import { lookup } from "./cache.ts";
import { kinds, patterns } from "./consts.ts";
import { getImportMapSpecifier, load } from "./docs.ts";
import { clearAppend } from "./modules.ts";
import type {
DependencyError,
Module,
ModuleDependency,
ModuleEntry,
ModuleVersion,
} from "./types.d.ts";
import { assert } from "./util.ts";
const ANALYSIS_VERSION = "1";
function resolveSpecifiers(
specifier: string,
redirects: Record<string, string>,
) {
return redirects[specifier] ?? specifier;
}
function parse(specifier: string): ModuleDependency {
for (
const [src, urlPatterns] of Object.entries(patterns) as [
keyof typeof patterns,
URLPattern[],
][]
) {
for (const pattern of urlPatterns) {
const match = pattern.exec(specifier);
if (match) {
const { org, pkg = "std", ver } = match.pathname.groups;
return { src, org, pkg, ver };
}
}
}
return { src: "other", pkg: specifier };
}
function depKey({ src, org, pkg, ver }: ModuleDependency): string {
return `${src}:${org ? `${org}/` : ""}${pkg}${ver ? `@${ver}` : ""}`;
}
const externalPattern = new URLPattern("https://deno.land/x/:mod/:path*");
function isExternal(specifier: string, referrer: string): boolean {
const referrerMatch = externalPattern.exec(referrer);
assert(referrerMatch, `unexpected referrer: ${referrer}`);
const { mod } = referrerMatch.pathname.groups;
const specifierMatch = externalPattern.exec(specifier);
if (!specifierMatch) {
return true;
}
return mod !== specifierMatch.pathname.groups.mod;
}
interface MappedDependency {
code?: { specifier?: string; error?: string };
type?: { specifier?: string; error?: string };
}
interface MappedModule {
error?: string;
deps?: Map<string, MappedDependency>;
}
function resolveModule(
modules: Map<string, MappedModule>,
redirects: Record<string, string>,
errors: DependencyError[],
specifier: string,
): MappedModule | undefined {
const resolved = resolveSpecifiers(specifier, redirects);
const mod = modules.get(resolved);
assert(mod, `cannot find module: ${resolved}`);
if (mod.error) {
errors.push({ specifier: resolved, error: mod.error });
return;
}
return mod;
}
function analyzeDeps(
deps: Map<string, ModuleDependency>,
modules: Map<string, MappedModule>,
redirects: Record<string, string>,
errors: DependencyError[],
seen: Set<string>,
specifier: string,
) {
if (seen.has(specifier)) {
return;
}
seen.add(specifier);
const mod = resolveModule(modules, redirects, errors, specifier);
if (mod && mod.deps) {
for (const [dep, { code, type }] of mod.deps) {
if (code) {
if (code.error) {
errors.push({ specifier: code.specifier ?? dep, error: code.error });
} else if (code.specifier) {
if (isExternal(code.specifier, specifier)) {
const parsedDep = parse(code.specifier);
deps.set(depKey(parsedDep), parsedDep);
resolveModule(modules, redirects, errors, code.specifier);
} else {
analyzeDeps(deps, modules, redirects, errors, seen, code.specifier);
}
}
}
if (type) {
if (type.error) {
errors.push({ specifier: type.specifier ?? dep, error: type.error });
} else if (type.specifier) {
if (isExternal(type.specifier, specifier)) {
const parsedDep = parse(type.specifier);
deps.set(depKey(parsedDep), parsedDep);
resolveModule(modules, redirects, errors, type.specifier);
} else {
analyzeDeps(deps, modules, redirects, errors, seen, type.specifier);
}
}
}
}
}
}
let datastore: Datastore | undefined;
async function getRoots(module: string, version: string): Promise<string[]> {
datastore = datastore ?? await getDatastore();
const res = await datastore.lookup(datastore.key(
[kinds.MODULE_KIND, module],
[kinds.MODULE_VERSION_KIND, version],
[kinds.MODULE_ENTRY_KIND, "/"],
));
assert(
res.found && res.found.length === 1,
"was unable to lookup root path of module",
);
const rootEntry = entityToObject<ModuleEntry>(res.found[0].entity);
if (rootEntry.default) {
return [`https://deno.land/x/${module}@${version}${rootEntry.default}`];
} else {
const query = datastore
.createQuery(kinds.MODULE_ENTRY_KIND)
.filter("docable", true)
.hasAncestor(datastore.key(
[kinds.MODULE_KIND, module],
[kinds.MODULE_VERSION_KIND, version],
));
const roots: string[] = [];
for (const entry of await datastore.query<ModuleEntry>(query)) {
if (entry.path.lastIndexOf("/") === 0) {
roots.push(`https://deno.land/x/${module}@${version}${entry.path}`);
}
}
return roots;
}
}
async function isAnalyzed(module: string, version: string): Promise<boolean> {
const [, moduleVersion] = await lookup(module, version);
assert(moduleVersion, `Cannot find module version: ${module}@${version}`);
return moduleVersion.analysis_version === ANALYSIS_VERSION;
}
/** Perform an analysis of a module and version, resolving with a tuple of
* arrays of dependencies and dependency errors. */
export async function analyze(
module: string,
version: string,
force: boolean,
): Promise<[ModuleDependency[], DependencyError[]]> {
if (!force && await isAnalyzed(module, version)) {
$.logStep(`Skipping ${module}@${version}. Already analyzed.`);
return [[], []];
}
$.logStep(`Analyzing dependencies of ${module}@${version}...`);
const importMapSpecifier = await getImportMapSpecifier(module, version);
let resolve: ((specifier: string, referrer: string) => string) | undefined;
if (importMapSpecifier) {
try {
const res = await fetch(importMapSpecifier);
if (res.status === 200) {
const content = await res.text();
const importMap = await parseFromJson(importMapSpecifier, content);
resolve = (specifier, referrer) =>
importMap.resolve(specifier, referrer);
}
} catch {
$.logError(`Cannot load identified import map: ${importMapSpecifier}`);
}
}
const graphRoots = await getRoots(module, version);
if (!graphRoots.length) {
$.logError("No root docable modules found.");
const [, moduleVersion] = await lookup(module, version);
assert(
moduleVersion,
`unexpected missing module version: ${module}@${version}`,
);
moduleVersion.analysis_version = ANALYSIS_VERSION;
assert(
objectGetKey(moduleVersion),
"module version is missing a key, unexpectedly",
);
const mutations: Mutation[] = [{ upsert: objectToEntity(moduleVersion) }];
datastore = datastore ?? await getDatastore();
for await (
const _ of datastore.commit(mutations, { transactional: false })
) {
// just empty here
}
$.logLight(` updated module version.`);
return [[], []];
}
$.logLight(` generating module graph...`);
const { modules, redirects, roots } = await createGraph(graphRoots, {
load,
resolve,
});
const mods = modules.reduce((map, { specifier, error, dependencies }) => {
const deps = dependencies?.reduce(
(map, { specifier, code, type }) => map.set(specifier, { code, type }),
new Map<string, MappedDependency>(),
);
return map.set(specifier, { error, deps });
}, new Map<string, MappedModule>());
const deps = new Map<string, ModuleDependency>();
const errors: DependencyError[] = [];
const seen = new Set<string>();
for (const root of roots) {
$.logLight(` analyzing dependencies for "${root}"...`);
analyzeDeps(deps, mods, redirects, errors, seen, root);
}
const mutations: Mutation[] = [];
datastore = datastore ?? await getDatastore();
const keyInit: KeyInit[] = [[kinds.MODULE_KIND, module], [
kinds.MODULE_VERSION_KIND,
version,
]];
await clearAppend(
datastore,
mutations,
["info_page"],
datastore.key(keyInit[0]),
);
await clearAppend(
datastore,
mutations,
[kinds.MODULE_DEP_KIND, kinds.DEP_ERROR_KIND],
datastore.key(...keyInit),
);
for (const error of errors) {
objectSetKey(error, datastore.key(...keyInit, kinds.DEP_ERROR_KIND));
mutations.push({ upsert: objectToEntity(error) });
}
for (const [key, value] of deps) {
objectSetKey(
value,
datastore.key(...keyInit, [kinds.MODULE_DEP_KIND, key]),
);
mutations.push({ upsert: objectToEntity(value) });
}
const [, moduleVersion] = await lookup(module, version);
assert(
moduleVersion,
`unexpected missing module version: ${module}@${version}`,
);
moduleVersion.analysis_version = ANALYSIS_VERSION;
assert(
objectGetKey(moduleVersion),
"module version is missing a key, unexpectedly",
);
mutations.push({ upsert: objectToEntity(moduleVersion) });
let remaining = mutations.length;
$.logStep(` Committing to datastore ${remaining} changes...`);
try {
for await (
const res of datastore.commit(mutations, { transactional: false })
) {
remaining -= res.mutationResults.length;
$.logLight(
` ${res.mutationResults.length} committed. ${remaining} to go.`,
);
}
} catch (err) {
if (err instanceof DatastoreError) {
$.logError(
"DatastoreError",
err.statusText,
JSON.stringify(err.statusInfo, undefined, " "),
);
} else {
throw err;
}
}
$.logStep("Done.");
return [[...deps.values()], errors];
}
/** Attempt to retrieve a module and versions analysis from the datastore, or
* otherwise perform an analysis. */
export async function getAnalysis(
module: Module,
version: ModuleVersion,
force = false,
): Promise<[ModuleDependency[], DependencyError[]]> {
if (!force && await isAnalyzed(module.name, version.version)) {
datastore = datastore ?? await getDatastore();
const ancestor = datastore.key(
[kinds.MODULE_KIND, module.name],
[kinds.MODULE_VERSION_KIND, version.version],
);
const depsQuery = datastore
.createQuery(kinds.MODULE_DEP_KIND)
.hasAncestor(ancestor);
const deps = await datastore.query<ModuleDependency>(depsQuery);
const errorQuery = datastore
.createQuery(kinds.DEP_ERROR_KIND)
.hasAncestor(ancestor);
const errors = await datastore.query<DependencyError>(errorQuery);
return [deps, errors];
} else {
return analyze(module.name, version.version, force);
}
}