/
node-file-trace.ts
385 lines (352 loc) · 11.9 KB
/
node-file-trace.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
import { NodeFileTraceOptions, NodeFileTraceResult, NodeFileTraceReasons, Stats } from './types';
import { basename, dirname, extname, relative, resolve, sep } from 'path';
import fs from 'fs';
import { promisify } from 'util'
import analyze, { AnalyzeResult } from './analyze';
import resolveDependency from './resolve-dependency';
import { isMatch } from 'micromatch';
import { sharedLibEmit } from './utils/sharedlib-emit';
import { join } from 'path';
const fsReadFile = promisify(fs.readFile)
const fsReadlink = promisify(fs.readlink)
const fsStat = promisify(fs.stat)
const { gracefulify } = require('graceful-fs');
gracefulify(fs);
function inPath (path: string, parent: string) {
const pathWithSep = join(parent, sep);
return path.startsWith(pathWithSep) && path !== pathWithSep;
}
export async function nodeFileTrace(files: string[], opts: NodeFileTraceOptions = {}): Promise<NodeFileTraceResult> {
const job = new Job(opts);
if (opts.readFile)
job.readFile = opts.readFile
if (opts.stat)
job.stat = opts.stat
if (opts.readlink)
job.readlink = opts.readlink
if (opts.resolve)
job.resolve = opts.resolve
job.ts = true;
await Promise.all(files.map(async file => {
const path = resolve(file);
await job.emitFile(path, 'initial');
if (path.endsWith('.js') || path.endsWith('.cjs') || path.endsWith('.mjs') || path.endsWith('.node') || job.ts && (path.endsWith('.ts') || path.endsWith('.tsx'))) {
return job.emitDependency(path);
}
return undefined;
}));
const result: NodeFileTraceResult = {
fileList: job.fileList,
esmFileList: job.esmFileList,
reasons: job.reasons,
warnings: job.warnings
};
return result;
};
export class Job {
public ts: boolean;
public base: string;
public cwd: string;
public conditions: string[];
public exportsOnly: boolean;
public paths: Record<string, string>;
public ignoreFn: (path: string, parent?: string) => boolean;
public log: boolean;
public mixedModules: boolean;
public analysis: { emitGlobs?: boolean, computeFileReferences?: boolean, evaluatePureExpressions?: boolean };
private fileCache: Map<string, string | null>;
private statCache: Map<string, Stats | null>;
private symlinkCache: Map<string, string | null>;
private analysisCache: Map<string, AnalyzeResult>;
public fileList: Set<string>;
public esmFileList: Set<string>;
public processed: Set<string>;
public warnings: Set<Error>;
public reasons: NodeFileTraceReasons = new Map()
constructor ({
base = process.cwd(),
processCwd,
exports,
conditions = exports || ['node'],
exportsOnly = false,
paths = {},
ignore,
log = false,
mixedModules = false,
ts = true,
analysis = {},
cache,
}: NodeFileTraceOptions) {
this.ts = ts;
base = resolve(base);
this.ignoreFn = (path: string) => {
if (path.startsWith('..' + sep)) return true;
return false;
};
if (typeof ignore === 'string') ignore = [ignore];
if (typeof ignore === 'function') {
const ig = ignore;
this.ignoreFn = (path: string) => {
if (path.startsWith('..' + sep)) return true;
if (ig(path)) return true;
return false;
};
}
else if (Array.isArray(ignore)) {
const resolvedIgnores = ignore.map(ignore => relative(base, resolve(base || process.cwd(), ignore)));
this.ignoreFn = (path: string) => {
if (path.startsWith('..' + sep)) return true;
if (isMatch(path, resolvedIgnores)) return true;
return false;
}
}
this.base = base;
this.cwd = resolve(processCwd || base);
this.conditions = conditions;
this.exportsOnly = exportsOnly;
const resolvedPaths: Record<string, string> = {};
for (const path of Object.keys(paths)) {
const trailer = paths[path].endsWith('/');
const resolvedPath = resolve(base, paths[path]);
resolvedPaths[path] = resolvedPath + (trailer ? '/' : '');
}
this.paths = resolvedPaths;
this.log = log;
this.mixedModules = mixedModules;
this.analysis = {};
if (analysis !== false) {
Object.assign(this.analysis, {
// whether to glob any analysis like __dirname + '/dir/' or require('x/' + y)
// that might output any file in a directory
emitGlobs: true,
// whether __filename and __dirname style
// expressions should be analyzed as file references
computeFileReferences: true,
// evaluate known bindings to assist with glob and file reference analysis
evaluatePureExpressions: true,
}, analysis === true ? {} : analysis);
}
this.fileCache = cache && cache.fileCache || new Map();
this.statCache = cache && cache.statCache || new Map();
this.symlinkCache = cache && cache.symlinkCache || new Map();
this.analysisCache = cache && cache.analysisCache || new Map();
if (cache) {
cache.fileCache = this.fileCache;
cache.statCache = this.statCache;
cache.symlinkCache = this.symlinkCache;
cache.analysisCache = this.analysisCache;
}
this.fileList = new Set();
this.esmFileList = new Set();
this.processed = new Set();
this.warnings = new Set();
}
async readlink (path: string) {
const cached = this.symlinkCache.get(path);
if (cached !== undefined) return cached;
try {
const link = await fsReadlink(path);
// also copy stat cache to symlink
const stats = this.statCache.get(path);
if (stats)
this.statCache.set(resolve(path, link), stats);
this.symlinkCache.set(path, link);
return link;
}
catch (e) {
if (e.code !== 'EINVAL' && e.code !== 'ENOENT' && e.code !== 'UNKNOWN')
throw e;
this.symlinkCache.set(path, null);
return null;
}
}
async isFile (path: string) {
const stats = await this.stat(path);
if (stats)
return stats.isFile();
return false;
}
async isDir (path: string) {
const stats = await this.stat(path);
if (stats)
return stats.isDirectory();
return false;
}
async stat (path: string) {
const cached = this.statCache.get(path);
if (cached) return cached;
try {
const stats = await fsStat(path);
this.statCache.set(path, stats);
return stats;
}
catch (e) {
if (e.code === 'ENOENT') {
this.statCache.set(path, null);
return null;
}
throw e;
}
}
async resolve (id: string, parent: string, job: Job, cjsResolve: boolean): Promise<string | string[]> {
return resolveDependency(id, parent, job, cjsResolve);
}
async readFile (path: string): Promise<string | Buffer | null> {
const cached = this.fileCache.get(path);
if (cached !== undefined) return cached;
try {
const source = (await fsReadFile(path)).toString();
this.fileCache.set(path, source);
return source;
}
catch (e) {
if (e.code === 'ENOENT' || e.code === 'EISDIR') {
this.fileCache.set(path, null);
return null;
}
throw e;
}
}
async realpath (path: string, parent?: string, seen = new Set()): Promise<string> {
if (seen.has(path)) throw new Error('Recursive symlink detected resolving ' + path);
seen.add(path);
const symlink = await this.readlink(path);
// emit direct symlink paths only
if (symlink) {
const parentPath = dirname(path);
const resolved = resolve(parentPath, symlink);
const realParent = await this.realpath(parentPath, parent);
if (inPath(path, realParent))
await this.emitFile(path, 'resolve', parent, true);
return this.realpath(resolved, parent, seen);
}
// keep backtracking for realpath, emitting folder symlinks within base
if (!inPath(path, this.base))
return path;
return join(await this.realpath(dirname(path), parent, seen), basename(path));
}
async emitFile (path: string, reason: string, parent?: string, isRealpath = false) {
if (!isRealpath) {
path = await this.realpath(path, parent);
}
path = relative(this.base, path);
if (parent) {
parent = relative(this.base, parent);
}
let reasonEntry = this.reasons.get(path)
if (!reasonEntry) {
reasonEntry = {
type: reason,
ignored: false,
parents: new Set()
};
this.reasons.set(path, reasonEntry)
}
if (parent && this.ignoreFn(path, parent)) {
if (!this.fileList.has(path) && reasonEntry) {
reasonEntry.ignored = true;
}
return false;
}
if (parent) {
reasonEntry.parents.add(parent);
}
this.fileList.add(path);
return true;
}
async getPjsonBoundary (path: string) {
const rootSeparatorIndex = path.indexOf(sep);
let separatorIndex: number;
while ((separatorIndex = path.lastIndexOf(sep)) > rootSeparatorIndex) {
path = path.substr(0, separatorIndex);
if (await this.isFile(path + sep + 'package.json'))
return path;
}
return undefined;
}
async emitDependency (path: string, parent?: string) {
if (this.processed.has(path)) {
if (parent) {
await this.emitFile(path, 'dependency', parent)
}
return
};
this.processed.add(path);
const emitted = await this.emitFile(path, 'dependency', parent);
if (!emitted) return;
if (path.endsWith('.json')) return;
if (path.endsWith('.node')) return await sharedLibEmit(path, this);
// js files require the "type": "module" lookup, so always emit the package.json
if (path.endsWith('.js')) {
const pjsonBoundary = await this.getPjsonBoundary(path);
if (pjsonBoundary)
await this.emitFile(pjsonBoundary + sep + 'package.json', 'resolve', path);
}
let analyzeResult: AnalyzeResult;
const cachedAnalysis = this.analysisCache.get(path);
if (cachedAnalysis) {
analyzeResult = cachedAnalysis;
}
else {
const source = await this.readFile(path);
if (source === null) throw new Error('File ' + path + ' does not exist.');
analyzeResult = await analyze(path, source.toString(), this);
this.analysisCache.set(path, analyzeResult);
}
const { deps, imports, assets, isESM } = analyzeResult;
if (isESM)
this.esmFileList.add(relative(this.base, path));
await Promise.all([
...[...assets].map(async asset => {
const ext = extname(asset);
if (ext === '.js' || ext === '.mjs' || ext === '.node' || ext === '' ||
this.ts && (ext === '.ts' || ext === '.tsx') && asset.startsWith(this.base) && asset.substr(this.base.length).indexOf(sep + 'node_modules' + sep) === -1)
await this.emitDependency(asset, path);
else
await this.emitFile(asset, 'asset', path);
}),
...[...deps].map(async dep => {
try {
var resolved = await this.resolve(dep, path, this, !isESM);
}
catch (e) {
this.warnings.add(new Error(`Failed to resolve dependency ${dep}:\n${e && e.message}`));
return;
}
if (Array.isArray(resolved)) {
for (const item of resolved) {
// ignore builtins
if (item.startsWith('node:')) return;
await this.emitDependency(item, path);
}
}
else {
// ignore builtins
if (resolved.startsWith('node:')) return;
await this.emitDependency(resolved, path);
}
}),
...[...imports].map(async dep => {
try {
var resolved = await this.resolve(dep, path, this, false);
}
catch (e) {
this.warnings.add(new Error(`Failed to resolve dependency ${dep}:\n${e && e.message}`));
return;
}
if (Array.isArray(resolved)) {
for (const item of resolved) {
// ignore builtins
if (item.startsWith('node:')) return;
await this.emitDependency(item, path);
}
}
else {
// ignore builtins
if (resolved.startsWith('node:')) return;
await this.emitDependency(resolved, path);
}
})
]);
}
}