1 | ;
|
2 |
|
3 | var Cache = require('./cache');
|
4 |
|
5 | /*
|
6 | * CacheGroup is used to both speed up and ensure consistency of hashForDep.
|
7 | *
|
8 | * The CacheGroup contains three separate caches:
|
9 | *
|
10 | * - MODULE_ENTRY: the cache of realPathKey => ModuleEntry objects.
|
11 | * Each ModuleEntry contains information about a particular module
|
12 | * found during hash-for-dep processing. realPathKey is a hash
|
13 | * of the resolved real path to the module's package.json file.
|
14 | *
|
15 | * Having the real path means that when resolving dependencies, we can
|
16 | * take the resolved paths, hash them and check quickly for cache entries,
|
17 | * speeding creation of the cache. Because many modules may refer to
|
18 | * the same module as a dependency, this eliminates duplication and having
|
19 | * to reread package.json files.
|
20 | *
|
21 | * However, that also means we need a second cache to map from the original
|
22 | * name and dir passed to hashForDep to the final resolved path for the
|
23 | * module's package.json file.
|
24 | *
|
25 | * - PATH: the cache of nameDirKey => realPathKey. nameDirKey is a hash of the
|
26 | * combination of the name and starting directory passed to hashForDep.
|
27 | * realPathKey is a hash of the resulting real path to the relevant package.json file.
|
28 | *
|
29 | * - REAL_FILE_PATH: the cache of filePath => resolved realPath for relevant files.
|
30 | * When determining the location of a file, the file path may involve links.
|
31 | * This cache is keyed on the original file path, with a value of the real path.
|
32 | * This cache helps to speed up path resolution in both cases by minimizing
|
33 | * the use of costly 'fs.statSync' calls.
|
34 | *
|
35 | * - REAL_DIRECTORY_PATH: the cache of filePath => resolved realPath for relevant directories.
|
36 | * When determining the location of a file by going up the node_modules chain,
|
37 | * paths to intervening directories may contain links. Similar to REAL_FILE_PATH,
|
38 | * this cache is keyed on the original directory path, with a value of the real path.
|
39 | * This cache helps to speed up path resolution in both cases by minimizing
|
40 | * the use of costly 'fs.statSync' calls.
|
41 | *
|
42 | * Note: when discussing 'real paths' above, the paths are normalized by routines
|
43 | * like path.join() or path.resolve(). We do nothing beyond that (e.g., we do not attempt
|
44 | * to force the paths into POSIX style.)
|
45 | */
|
46 |
|
47 | module.exports = function CacheGroup() {
|
48 | this.MODULE_ENTRY = new Cache();
|
49 | this.PATH = new Cache();
|
50 | this.REAL_FILE_PATH = new Cache();
|
51 | this.REAL_DIRECTORY_PATH = new Cache();
|
52 | Object.freeze(this);
|
53 | };
|