-
Notifications
You must be signed in to change notification settings - Fork 2
/
flat-module.js
585 lines (485 loc) · 16 KB
/
flat-module.js
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
"use strict";
const Module = require("module");
const fs = require("fs");
const assert = require("assert");
const path = require("path");
const ORIG_RESOLVE_LOOKUP_PATHS = Symbol("node-flat-module-orig-resolve-lookup-paths");
const ORIG_FIND_PATH = Symbol("node-flat-module-orig-find-path");
assert(!Module[ORIG_RESOLVE_LOOKUP_PATHS], "Flat Module system already installed");
Module[ORIG_RESOLVE_LOOKUP_PATHS] = Module._resolveLookupPaths;
Module[ORIG_FIND_PATH] = Module._findPath;
const DIR_MAP = new Map();
const __FV_DIR = "__fv_";
const __FYN_IGNORE__ = "__fyn_ignore__";
const debug = () => undefined;
// const debug = console.log;
const NODE_MODULES = "node_modules";
const PACKAGE_JSON = "package.json";
let internals = { FYN_LINK_CWD: process.cwd() };
internals.fynLinkCwd = () => {
return process.env.FYN_LINK_CWD || internals.FYN_LINK_CWD;
};
internals.getLinkedInfo = dir => {
const linkedF = path.join(dir, "__fyn_link__.json");
if (fs.existsSync(linkedF)) {
const linked = internals.readJSON(linkedF);
return linked[internals.fynLinkCwd()];
}
return false;
};
//
// There is only one node_modules rather than many nested ones.
// Set dir with node_modules as topDir.
//
// - directory of require origin <doro>
// -- parent.filename or CWD if it's is null.
//
// - To find topDir from <doro>:
//
// -- If <doro> is under CWD:
// ---- if <doro>/node_modules exist, then use <doro>.
// ---- if <doro> contains node_modules, then use up to the last node_modules.
// ---- finally search for existence of node_modules up to root.
// -- If <doro> is not under CWD (installations outside of CWD), then
// check for first node_modules up to root.
//
// There are two scenarios when <doro> is not under CWD.
//
// ** It could be a globally installed CLI program and whatever it is, it has no
// access to node_modules under CWD so must resolves its dependencies in its
// own node_modules.
//
// ** It could be a linked module.
// we could do what's always been done - force linked modules to have their
// own dependencies, or handle this in the module system. So under its own
// node_modules directory, have a __fyn_link__.json with a list
// of directories for apps that have linked to it. When that file is detected
// and CWD is listed in it, then switch to looking in CWD for node_modules.
//
// linked module should:
// - has all its dependencies installed in host's node_modules
// - has a __fyn_link__.json in its own node_modules
// - (optionally) has its own __fyn_resolutions__.json, use when being developed itself
//
// If a module is not installed by semver, then its resolve version is:
//
// symlink: v_symlink_<base64 of target full path md5>
// file: v_file_<base64 of full file path md5>
// git/url: v_<type>url_<base64 of URL md5>
//
//
// A package manager should:
// - save _depResolutions in an installed module's package.json.
// - create __fyn_resolutions__.json under CWD/node_modules
//
// _depResolutions should contain the exact version that was resolved
// for the given module's dependencies semver
//
//
// search from <dir> up to <stopDir> or /, or checkCb returns non-undefined
//
internals.searchUpDir = (dir, stopDir, singleStops, checkCb) => {
let up = dir;
while (up) {
dir = up;
const result = checkCb(dir);
if (result !== undefined) return result;
if (
(stopDir && dir === stopDir) ||
(singleStops && singleStops.indexOf(path.basename(dir)) >= 0)
) {
break;
}
up = path.join(dir, "..");
if (dir === up) {
break;
}
}
return false;
};
//
// search from originDir up to CWD or / looking for the first node_modules
// and use that as topDir
//
internals.searchTopDir = originDir => {
return internals.searchUpDir(originDir, null, null, dir => {
const dm = internals.getDirMap(dir);
if (dm.hasOwnProperty("top")) {
// already known to qualify as top dir or not
return dm.top && dm;
}
const nmDir = path.join(dir, NODE_MODULES);
if (fs.existsSync(nmDir) && !fs.existsSync(path.join(nmDir, __FYN_IGNORE__))) {
// yay, found node_modules
// but is it a linked module?
const linkedInfo = internals.getLinkedInfo(nmDir);
if (linkedInfo) {
// switch topDir to CWD for linked mod
dm.top = internals.fynLinkCwd();
dm.linkedInfo = linkedInfo;
} else {
dm.top = dir;
}
//
// Look for topDir/node_modules/__fyn_resolutions__.json
//
const ff = path.join(dm.top, NODE_MODULES, "__fyn_resolutions__.json");
if (fs.existsSync(ff)) {
dm.depRes = internals.readJSON(ff);
}
return dm;
}
// remember that dir has already been checked but not qualify as top dir
return (dm.top = undefined);
});
};
internals.readJSON = f => {
return JSON.parse(fs.readFileSync(f));
};
internals.getDirMap = dir => {
if (!DIR_MAP.has(dir)) {
DIR_MAP.set(dir, {});
}
return DIR_MAP.get(dir);
};
internals.readPackage = dir => {
const dm = internals.getDirMap(dir);
if (dm.hasOwnProperty("pkg")) return dm.pkg;
const pkgFile = path.join(dir, PACKAGE_JSON);
if (!fs.existsSync(pkgFile)) {
return (dm.pkg = undefined);
}
const pkg = internals.readJSON(pkgFile);
dm.pkg = {
name: pkg.name,
version: pkg.version,
dependencies: pkg.dependencies
};
const bd = pkg.bundledDependencies || pkg.bundleDependencies;
if (bd) dm.pkg.bundledDependencies = bd;
if (pkg._depResolutions) dm.pkg._depResolutions = pkg._depResolutions;
if (pkg._flatVersion) dm.pkg._flatVersion = pkg._flatVersion;
if (pkg.fyn) dm.pkg.fallbackToDefault = pkg.fyn.fallbackToDefault;
return dm.pkg;
};
internals.findMappedPackage = (dir, stopDir, singleStops) => {
return internals.searchUpDir(dir, stopDir, singleStops, x => {
return internals.getDirMap(dir).pkg;
});
};
// search from <dir> up to <stopDir> or / for the file package.json
internals.findNearestPackage = (dir, stopDir, singleStops) => {
const mappedPkg = internals.findMappedPackage(dir, stopDir, singleStops);
if (mappedPkg) return mappedPkg;
return internals.searchUpDir(dir, stopDir, singleStops, x => {
return internals.readPackage(x);
});
};
//
// Find module name from a require request
// For handling calls like: require("foo/lib/bar") and require("@ns/foo")
//
internals.findModuleName = (dir, request) => {
const splits = request.split("/");
// is it a simple require("foo")?
if (splits.length < 2) {
return request;
}
if (request.startsWith("@")) {
return path.join(splits[0], splits[1]);
}
return splits[0];
};
internals.isRelativePathRequest = request => {
if (request === "." || request === "..") {
return true;
}
if (request.startsWith("../") || request.startsWith("./")) {
return true;
}
/* istanbul ignore next */
if (path.sep !== "/") {
return request.startsWith("." + path.sep) || request.startsWith(".." + path.sep);
}
return false;
};
internals.useOriginalLookup = (request, parent) => {
/* istanbul ignore next */
if (request === "<repl>") return true;
return path.isAbsolute(request) || internals.isRelativePathRequest(request);
};
//
// - parse require string in the form of:
// 1. "name@version" => {request: "name", semVer: "version"}
// 2. "name@version/path/to/mod" => {request: "name/path/to/mod", semVer: "version"}
// ie: require("[email protected]")
// - version supports semver in the form of x.x.x, ie: 2.x.x
//
internals.parseRequest = request => {
let semVer = "";
const xAt = request.indexOf("@");
if (xAt > 0) {
const tmp = request.substr(0, xAt);
const xSep = request.indexOf("/", xAt);
let tail = "";
if (xSep > xAt) {
tail = request.substr(xSep);
semVer = request.substring(xAt + 1, xSep);
} else {
semVer = request.substr(xAt + 1);
}
request = tmp + tail;
}
return { request, semVer };
};
// from https://github.com/sindresorhus/semver-regex/blob/master/index.js
const semVerRegex = /\bv?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?\b/gi;
internals.semVerMatch = (semVer, ver) => {
// support x.x.x format ONLY
const isAny = v => {
return !v || (v === "x" || v === "X" || v === "*");
};
if (isAny(semVer)) {
return true;
}
const svSplits = semVer.split(".");
const verSplits = ver.split(".");
let i;
for (i = 0; i < verSplits.length; i++) {
if (i >= svSplits.length) {
return true;
} else if (!isAny(svSplits[i])) {
if (svSplits[i] !== verSplits[i]) {
return false;
}
}
}
return true;
};
internals.semVerCompare = (a, b) => {
if (a === b) {
return 0;
}
const mA = a.match(semVerRegex);
const mB = b.match(semVerRegex);
if (mA && mB) {
const aSp = mA[0].split(".");
const bSp = mB[0].split(".");
let i;
for (i = 0; i < aSp.length; i++) {
const aN = parseInt(aSp[i], 10);
const bN = parseInt(bSp[i], 10);
if (aN > bN) {
return 1;
}
if (aN < bN) {
return -1;
}
}
return 0;
}
return a > b ? 1 : -1;
};
internals.loadFV = dirInfo => {
if (dirInfo.hasOwnProperty("fvVersions")) return;
const nmDir = path.join(dirInfo.top, NODE_MODULES);
const fvDir = path.join(nmDir, __FV_DIR);
dirInfo.fvVersions = {};
if (!fs.existsSync(fvDir)) return;
const versions = fs.readdirSync(fvDir);
for (const v of versions) {
const verDir = path.join(fvDir, v);
const mods = fs.readdirSync(verDir);
for (let m of mods) {
if (m.startsWith("@")) {
const scopeDir = path.join(verDir, m);
const scope2 = fs.readdirSync(scopeDir);
m = path.join(m, scope2[0]);
}
if (!dirInfo.fvVersions[m]) {
dirInfo.fvVersions[m] = [];
}
dirInfo.fvVersions[m].push(v);
}
}
};
internals.loadModVersions = (dirInfo, modName) => {
const modDir = path.join(dirInfo.top, NODE_MODULES, modName);
const dm = internals.getDirMap(modDir);
if (dm.hasOwnProperty("versions")) return dm;
const versions = {};
const all = dirInfo.fvVersions[modName] || [];
if (all.length > 0) versions.fv = true;
//
// does there exist a default version
//
internals.readPackage(modDir);
if (dm.pkg) {
const dv = dm.pkg._flatVersion || dm.pkg.version;
all.push(dv);
versions.default = dv;
}
if (all.length > 0) {
versions.all = all.sort(internals.semVerCompare);
dm.versions = versions;
}
if (!dm.hasOwnProperty("versions")) {
dm.versions = undefined;
}
debug("loadModVersions modDir", modDir, dm.pkg, dm.versions);
return dm;
};
internals.getModuleVersions = (dirInfo, modName) => {
internals.loadFV(dirInfo);
const dm = internals.loadModVersions(dirInfo, modName);
return dm.versions || { all: [] };
};
// lookup specific version mapped for parent inside its nearest package.json
internals.getDepResolutions = (dirInfo, pkg, request) => {
if (!pkg) {
return {};
}
//
// common case - a package manager should've install a package with _depResolutions
// saved in its package.json file.
//
if (pkg._depResolutions) {
return pkg._depResolutions;
}
//
// package.json doesn't have _depResolutions entry
// is it linked module? Then look inside linked info
//
const linkedInfo = dirInfo.linkedInfo;
if (linkedInfo && linkedInfo._depResolutions) {
debug(`Using linkedInfo._depResolutions for ${request}`);
return linkedInfo._depResolutions;
}
if (dirInfo.depRes) {
return dirInfo.depRes;
}
//
// can't find _depResolutions, fallback to original node module resolution.
//
assert(
dirInfo.flat === undefined,
`${request} flat module can't determine dep resolution but flat mode is already ${dirInfo.flat}`
);
debug("no depRes found, setting dirInfo.flat to false");
dirInfo.flat = false;
return {};
};
function flatResolveLookupPaths(request, parent, newReturn) {
if (internals.useOriginalLookup(request, parent)) {
return this[ORIG_RESOLVE_LOOKUP_PATHS](request, parent, newReturn);
}
const reqParts = internals.parseRequest(request);
request = reqParts.request;
debug(`flat _resolveLookupPaths: request ${request} parent.id ${parent && parent.id}`);
const parentDir = parent.filename && path.dirname(parent.filename);
const originDir = parentDir || process.cwd();
const dirInfo = internals.searchTopDir(originDir);
debug("dirInfo.top", dirInfo.top);
if (dirInfo.flat === false) {
return this[ORIG_RESOLVE_LOOKUP_PATHS](request, parent, newReturn);
}
if (dirInfo.top !== internals.FYN_LINK_CWD) {
internals.FYN_LINK_CWD = dirInfo.top;
}
//
// search from parent's location up to topDir or / for package.json and load it
// stopping if a directory named node_modules is seen since
// that means what's being searched is an installed module and should have
// a package.json found already.
//
const pkg = internals.findNearestPackage(originDir, dirInfo.top, [NODE_MODULES]);
const moduleName = internals.findModuleName(dirInfo.top, request);
//
// Pkg has bundledDependencies: use original module system
//
if (pkg && pkg.bundledDependencies && pkg.bundledDependencies.indexOf(moduleName) >= 0) {
debug("has bundledDependencies", originDir, dirInfo.top);
dirInfo.flat = false;
return this[ORIG_RESOLVE_LOOKUP_PATHS](request, parent, newReturn);
}
// If can't figure out top dir, then give up.
if (!dirInfo.top) {
/* istanbul ignore next */
return newReturn ? null : [request, []]; // force not found error out
}
//
// Now we should've figured out where to find parent's dependencies.
// Next resolve the version of the module to load.
//
const matchLatestSemVer = (semVer, versions) => {
const matched = versions.all.filter(v => internals.semVerMatch(semVer, v));
debug("matched latest", matched, "for", semVer);
return matched.length > 0 && matched[matched.length - 1];
};
const getResolvedVersion = versions => {
const depRes = internals.getDepResolutions(dirInfo, pkg, request);
const r = depRes[moduleName];
if (!r || versions.all.indexOf(r.resolved) < 0) {
//
// dynamically match latest version
//
if (pkg && dirInfo.flat !== false) {
const resolved = matchLatestSemVer("*", versions);
depRes[moduleName] = { resolved };
return resolved;
}
return undefined;
}
return r.resolved;
};
const versions = internals.getModuleVersions(dirInfo, moduleName);
const version = reqParts.semVer
? matchLatestSemVer(reqParts.semVer, versions)
: getResolvedVersion(versions);
debug("versions", versions, reqParts, "version", version);
//
// unable to resolve a version for a dependency, error out
//
if (!version) {
if (dirInfo.flat === false) {
debug("flat false, original lookup");
return this[ORIG_RESOLVE_LOOKUP_PATHS](request, parent, newReturn);
}
if (versions.default && pkg && pkg.fallbackToDefault === true) {
debug("fallback to default");
version = versions.default;
} else {
debug("no version, fail");
/* istanbul ignore next */
return newReturn ? null : [request, []]; // force not found error out
}
}
if (dirInfo.flat === undefined) {
dirInfo.flat = true;
}
const versionFp =
version === versions.default
? path.join(dirInfo.top, NODE_MODULES)
: path.join(dirInfo.top, NODE_MODULES, __FV_DIR, version);
debug("versionFp", versionFp);
/* istanbul ignore next */
return newReturn ? [versionFp] : [request, [versionFp]];
}
function flatFindPath(request, paths, isMain) {
if (!internals.useOriginalLookup(request)) {
request = internals.parseRequest(request).request;
}
return this[ORIG_FIND_PATH](request, paths, isMain);
}
Module._resolveLookupPaths = flatResolveLookupPaths;
Module._findPath = flatFindPath;
module.exports = {
flatResolveLookupPaths,
restore: () => {
Module._resolveLookupPaths = Module[ORIG_RESOLVE_LOOKUP_PATHS];
Module._findPath = Module[ORIG_FIND_PATH];
Module[ORIG_RESOLVE_LOOKUP_PATHS] = undefined;
Module[ORIG_FIND_PATH] = undefined;
},
internals
};