@vercel/nft 0.20.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/out/analyze.js CHANGED
@@ -70,6 +70,7 @@ const fsSymbols = {
70
70
  stat: FS_FN,
71
71
  statSync: FS_FN
72
72
  };
73
+ const fsExtraSymbols = Object.assign(Object.assign({}, fsSymbols), { pathExists: FS_FN, pathExistsSync: FS_FN, readJson: FS_FN, readJSON: FS_FN, readJsonSync: FS_FN, readJSONSync: FS_FN });
73
74
  const staticModules = Object.assign(Object.create(null), {
74
75
  bindings: {
75
76
  default: BINDINGS
@@ -84,6 +85,8 @@ const staticModules = Object.assign(Object.create(null), {
84
85
  }
85
86
  },
86
87
  fs: Object.assign({ default: fsSymbols }, fsSymbols),
88
+ 'fs-extra': Object.assign({ default: fsExtraSymbols }, fsExtraSymbols),
89
+ 'graceful-fs': Object.assign({ default: fsSymbols }, fsSymbols),
87
90
  process: Object.assign({ default: staticProcess }, staticProcess),
88
91
  // populated below
89
92
  path: {
@@ -356,7 +359,7 @@ async function analyze(id, code, job) {
356
359
  .filter(name => !excludeAssetExtensions.has(path_1.default.extname(name)) &&
357
360
  !excludeAssetFiles.has(path_1.default.basename(name)) &&
358
361
  !name.endsWith('/'))
359
- .forEach(file => assets.add(file));
362
+ .forEach(file => deps.add(file));
360
363
  });
361
364
  }
362
365
  async function processRequireArg(expression, isImport = false) {
@@ -389,7 +392,7 @@ async function analyze(id, code, job) {
389
392
  let scope = rollup_pluginutils_1.attachScopes(ast, 'scope');
390
393
  if (isAst(ast)) {
391
394
  wrappers_1.handleWrappers(ast);
392
- await special_cases_1.default({ id, ast, emitAsset: path => assets.add(path), emitAssetDirectory, job });
395
+ await special_cases_1.default({ id, ast, emitDependency: path => deps.add(path), emitAsset: path => assets.add(path), emitAssetDirectory, job });
393
396
  }
394
397
  async function backtrack(parent, context) {
395
398
  // computing a static expression outward
@@ -476,6 +479,18 @@ async function analyze(id, code, job) {
476
479
  await processRequireArg(node.arguments[0]);
477
480
  return;
478
481
  }
482
+ else if ((!isESM || job.mixedModules) &&
483
+ node.callee.type === 'MemberExpression' &&
484
+ node.callee.object.type === 'Identifier' &&
485
+ node.callee.object.name === 'require' &&
486
+ knownBindings.require.shadowDepth === 0 &&
487
+ node.callee.property.type === 'Identifier' &&
488
+ !node.callee.computed &&
489
+ node.callee.property.name === 'resolve' &&
490
+ node.arguments.length) {
491
+ await processRequireArg(node.arguments[0]);
492
+ return;
493
+ }
479
494
  const calleeValue = job.analysis.evaluatePureExpressions && await computePureStaticValue(node.callee, false);
480
495
  // if we have a direct pure static function,
481
496
  // and that function has a [TRIGGER] symbol -> trigger asset emission from it
@@ -25,7 +25,8 @@ export declare class Job {
25
25
  processed: Set<string>;
26
26
  warnings: Set<Error>;
27
27
  reasons: NodeFileTraceReasons;
28
- constructor({ base, processCwd, exports, conditions, exportsOnly, paths, ignore, log, mixedModules, ts, analysis, cache, }: NodeFileTraceOptions);
28
+ private fileIOQueue;
29
+ constructor({ base, processCwd, exports, conditions, exportsOnly, paths, ignore, log, mixedModules, ts, analysis, cache, fileIOConcurrency, }: NodeFileTraceOptions);
29
30
  readlink(path: string): Promise<string | null>;
30
31
  isFile(path: string): Promise<boolean>;
31
32
  isDir(path: string): Promise<boolean>;
@@ -11,6 +11,7 @@ const resolve_dependency_1 = __importDefault(require("./resolve-dependency"));
11
11
  const micromatch_1 = require("micromatch");
12
12
  const sharedlib_emit_1 = require("./utils/sharedlib-emit");
13
13
  const path_2 = require("path");
14
+ const async_sema_1 = require("async-sema");
14
15
  const fsReadFile = graceful_fs_1.default.promises.readFile;
15
16
  const fsReadlink = graceful_fs_1.default.promises.readlink;
16
17
  const fsStat = graceful_fs_1.default.promises.stat;
@@ -45,7 +46,10 @@ async function nodeFileTrace(files, opts = {}) {
45
46
  exports.nodeFileTrace = nodeFileTrace;
46
47
  ;
47
48
  class Job {
48
- constructor({ base = process.cwd(), processCwd, exports, conditions = exports || ['node'], exportsOnly = false, paths = {}, ignore, log = false, mixedModules = false, ts = true, analysis = {}, cache, }) {
49
+ constructor({ base = process.cwd(), processCwd, exports, conditions = exports || ['node'], exportsOnly = false, paths = {}, ignore, log = false, mixedModules = false, ts = true, analysis = {}, cache,
50
+ // we use a default of 1024 concurrency to balance
51
+ // performance and memory usage for fs operations
52
+ fileIOConcurrency = 1024, }) {
49
53
  this.reasons = new Map();
50
54
  this.ts = ts;
51
55
  base = path_1.resolve(base);
@@ -89,6 +93,7 @@ class Job {
89
93
  this.paths = resolvedPaths;
90
94
  this.log = log;
91
95
  this.mixedModules = mixedModules;
96
+ this.fileIOQueue = new async_sema_1.Sema(fileIOConcurrency);
92
97
  this.analysis = {};
93
98
  if (analysis !== false) {
94
99
  Object.assign(this.analysis, {
@@ -121,6 +126,7 @@ class Job {
121
126
  const cached = this.symlinkCache.get(path);
122
127
  if (cached !== undefined)
123
128
  return cached;
129
+ await this.fileIOQueue.acquire();
124
130
  try {
125
131
  const link = await fsReadlink(path);
126
132
  // also copy stat cache to symlink
@@ -136,6 +142,9 @@ class Job {
136
142
  this.symlinkCache.set(path, null);
137
143
  return null;
138
144
  }
145
+ finally {
146
+ this.fileIOQueue.release();
147
+ }
139
148
  }
140
149
  async isFile(path) {
141
150
  const stats = await this.stat(path);
@@ -153,6 +162,7 @@ class Job {
153
162
  const cached = this.statCache.get(path);
154
163
  if (cached)
155
164
  return cached;
165
+ await this.fileIOQueue.acquire();
156
166
  try {
157
167
  const stats = await fsStat(path);
158
168
  this.statCache.set(path, stats);
@@ -165,6 +175,9 @@ class Job {
165
175
  }
166
176
  throw e;
167
177
  }
178
+ finally {
179
+ this.fileIOQueue.release();
180
+ }
168
181
  }
169
182
  async resolve(id, parent, job, cjsResolve) {
170
183
  return resolve_dependency_1.default(id, parent, job, cjsResolve);
@@ -173,6 +186,7 @@ class Job {
173
186
  const cached = this.fileCache.get(path);
174
187
  if (cached !== undefined)
175
188
  return cached;
189
+ await this.fileIOQueue.acquire();
176
190
  try {
177
191
  const source = (await fsReadFile(path)).toString();
178
192
  this.fileCache.set(path, source);
@@ -185,6 +199,9 @@ class Job {
185
199
  }
186
200
  throw e;
187
201
  }
202
+ finally {
203
+ this.fileIOQueue.release();
204
+ }
188
205
  }
189
206
  async realpath(path, parent, seen = new Set()) {
190
207
  if (seen.has(path))
@@ -263,12 +280,15 @@ class Job {
263
280
  return;
264
281
  if (path.endsWith('.node'))
265
282
  return await sharedlib_emit_1.sharedLibEmit(path, this);
266
- // js files require the "type": "module" lookup, so always emit the package.json
267
- if (path.endsWith('.js')) {
268
- const pjsonBoundary = await this.getPjsonBoundary(path);
269
- if (pjsonBoundary)
270
- await this.emitFile(pjsonBoundary + path_1.sep + 'package.json', 'resolve', path);
271
- }
283
+ const handlePjsonBoundary = async (item) => {
284
+ // js files require the "type": "module" lookup, so always emit the package.json
285
+ if (item.endsWith(".js")) {
286
+ const pjsonBoundary = await this.getPjsonBoundary(item);
287
+ if (pjsonBoundary)
288
+ await this.emitFile(pjsonBoundary + path_1.sep + "package.json", "resolve", item);
289
+ }
290
+ };
291
+ await handlePjsonBoundary(path);
272
292
  let analyzeResult;
273
293
  const cachedAnalysis = this.analysisCache.get(path);
274
294
  if (cachedAnalysis) {
@@ -278,6 +298,9 @@ class Job {
278
298
  const source = await this.readFile(path);
279
299
  if (source === null)
280
300
  throw new Error('File ' + path + ' does not exist.');
301
+ // analyze should not have any side-effects e.g. calling `job.emitFile`
302
+ // directly as this will not be included in the cachedAnalysis and won't
303
+ // be emit for successive runs that leverage the cache
281
304
  analyzeResult = await analyze_1.default(path, source.toString(), this);
282
305
  this.analysisCache.set(path, analyzeResult);
283
306
  }
@@ -286,12 +309,8 @@ class Job {
286
309
  this.esmFileList.add(path_1.relative(this.base, path));
287
310
  await Promise.all([
288
311
  ...[...assets].map(async (asset) => {
289
- const ext = path_1.extname(asset);
290
- if (ext === '.js' || ext === '.mjs' || ext === '.node' || ext === '' ||
291
- this.ts && (ext === '.ts' || ext === '.tsx') && asset.startsWith(this.base) && asset.slice(this.base.length).indexOf(path_1.sep + 'node_modules' + path_1.sep) === -1)
292
- await this.emitDependency(asset, path);
293
- else
294
- await this.emitFile(asset, 'asset', path);
312
+ await handlePjsonBoundary(asset);
313
+ await this.emitFile(asset, 'asset', path);
295
314
  }),
296
315
  ...[...deps].map(async (dep) => {
297
316
  try {
package/out/types.d.ts CHANGED
@@ -48,6 +48,7 @@ export interface NodeFileTraceOptions {
48
48
  stat?: (path: string) => Promise<Stats | null>;
49
49
  readlink?: (path: string) => Promise<string | null>;
50
50
  resolve?: (id: string, parent: string, job: Job, cjsResolve: boolean) => Promise<string | string[]>;
51
+ fileIOConcurrency?: number;
51
52
  }
52
53
  export declare type NodeFileTraceReasonType = 'initial' | 'resolve' | 'dependency' | 'asset' | 'sharedlib';
53
54
  export interface NodeFileTraceReasons extends Map<string, {
@@ -7,8 +7,8 @@ exports.nbind = exports.pregyp = void 0;
7
7
  const path_1 = __importDefault(require("path"));
8
8
  const graceful_fs_1 = __importDefault(require("graceful-fs"));
9
9
  // pregyp
10
- const versioning = require("node-pre-gyp/lib/util/versioning.js");
11
- const napi = require("node-pre-gyp/lib/util/napi.js");
10
+ const versioning = require("@mapbox/node-pre-gyp/lib/util/versioning.js");
11
+ const napi = require("@mapbox/node-pre-gyp/lib/util/napi.js");
12
12
  const pregypFind = (package_json_path, opts) => {
13
13
  const package_json = JSON.parse(graceful_fs_1.default.readFileSync(package_json_path).toString());
14
14
  versioning.validate_config(package_json, opts);
@@ -229,18 +229,18 @@ const specialCases = {
229
229
  emitAsset(path_1.resolve(path_1.dirname(id), '../data/geo.dat'));
230
230
  }
231
231
  },
232
- 'pixelmatch'({ id, emitAsset }) {
232
+ 'pixelmatch'({ id, emitDependency }) {
233
233
  if (id.endsWith('pixelmatch/index.js')) {
234
- emitAsset(path_1.resolve(path_1.dirname(id), 'bin/pixelmatch'));
234
+ emitDependency(path_1.resolve(path_1.dirname(id), 'bin/pixelmatch'));
235
235
  }
236
236
  }
237
237
  };
238
- async function handleSpecialCases({ id, ast, emitAsset, emitAssetDirectory, job }) {
238
+ async function handleSpecialCases({ id, ast, emitDependency, emitAsset, emitAssetDirectory, job }) {
239
239
  const pkgName = get_package_base_1.getPackageName(id);
240
240
  const specialCase = specialCases[pkgName || ''];
241
241
  id = id.replace(/\\/g, '/');
242
242
  if (specialCase)
243
- await specialCase({ id, ast, emitAsset, emitAssetDirectory, job });
243
+ await specialCase({ id, ast, emitDependency, emitAsset, emitAssetDirectory, job });
244
244
  }
245
245
  exports.default = handleSpecialCases;
246
246
  ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/nft",
3
- "version": "0.20.0",
3
+ "version": "0.22.0",
4
4
  "repository": "vercel/nft",
5
5
  "license": "MIT",
6
6
  "main": "./out/index.js",
@@ -21,13 +21,13 @@
21
21
  "dependencies": {
22
22
  "@mapbox/node-pre-gyp": "^1.0.5",
23
23
  "acorn": "^8.6.0",
24
+ "async-sema": "^3.1.1",
24
25
  "bindings": "^1.4.0",
25
26
  "estree-walker": "2.0.2",
26
27
  "glob": "^7.1.3",
27
28
  "graceful-fs": "^4.2.9",
28
29
  "micromatch": "^4.0.2",
29
30
  "node-gyp-build": "^4.2.2",
30
- "node-pre-gyp": "^0.13.0",
31
31
  "resolve-from": "^5.0.0",
32
32
  "rollup-pluginutils": "^2.8.2"
33
33
  },
package/readme.md CHANGED
@@ -118,6 +118,18 @@ By its nature of integrating into existing build systems, the TypeScript
118
118
  compiler is not included in this project - rather the TypeScript transform
119
119
  layer requires separate integration into the `readFile` hook.
120
120
 
121
+ #### File IO Concurrency
122
+
123
+ In some large projects, the file tracing logic may process many files at the same time. In this case, if you do not limit the number of concurrent files IO, OOM problems are likely to occur.
124
+
125
+ We use a default of 1024 concurrency to balance performance and memory usage for fs operations. You can increase this value to a higher number for faster speed, but be aware of the memory issues if the concurrency is too high.
126
+
127
+ ```js
128
+ const { fileList } = await nodeFileTrace(files, {
129
+ fileIOConcurrency: 2048,
130
+ });
131
+ ```
132
+
121
133
  #### Analysis
122
134
 
123
135
  Analysis options allow customizing how much analysis should be performed to exactly work out the dependency list.