mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
4.4 KiB
131 lines
4.4 KiB
8 years ago
|
/**
|
||
|
* @fileoverview Responsible for caching config files
|
||
|
* @author Sylvan Mably
|
||
|
*/
|
||
|
|
||
|
"use strict";
|
||
|
|
||
|
//------------------------------------------------------------------------------
|
||
|
// Helpers
|
||
|
//------------------------------------------------------------------------------
|
||
|
|
||
|
/**
|
||
|
* Get a string hash for a config vector
|
||
|
* @param {Array<Object>} vector config vector to hash
|
||
|
* @returns {string} hash of the vector values
|
||
|
* @private
|
||
|
*/
|
||
|
function hash(vector) {
|
||
|
return JSON.stringify(vector);
|
||
|
}
|
||
|
|
||
|
//------------------------------------------------------------------------------
|
||
|
// API
|
||
|
//------------------------------------------------------------------------------
|
||
|
|
||
|
/**
|
||
|
* Configuration caching class (not exported)
|
||
|
*/
|
||
|
class ConfigCache {
|
||
|
|
||
|
constructor() {
|
||
|
this.filePathCache = new Map();
|
||
|
this.localHierarchyCache = new Map();
|
||
|
this.mergedVectorCache = new Map();
|
||
|
this.mergedCache = new Map();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Gets a config object from the cache for the specified config file path.
|
||
|
* @param {string} configFilePath the absolute path to the config file
|
||
|
* @returns {Object|null} config object, if found in the cache, otherwise null
|
||
|
* @private
|
||
|
*/
|
||
|
getConfig(configFilePath) {
|
||
|
return this.filePathCache.get(configFilePath);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Sets a config object in the cache for the specified config file path.
|
||
|
* @param {string} configFilePath the absolute path to the config file
|
||
|
* @param {Object} config the config object to add to the cache
|
||
|
* @returns {void}
|
||
|
* @private
|
||
|
*/
|
||
|
setConfig(configFilePath, config) {
|
||
|
this.filePathCache.set(configFilePath, config);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Gets a list of hierarchy-local config objects that apply to the specified directory.
|
||
|
* @param {string} directory the path to the directory
|
||
|
* @returns {Object[]|null} a list of config objects, if found in the cache, otherwise null
|
||
|
* @private
|
||
|
*/
|
||
|
getHierarchyLocalConfigs(directory) {
|
||
|
return this.localHierarchyCache.get(directory);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* For each of the supplied parent directories, sets the list of config objects for that directory to the
|
||
|
* appropriate subset of the supplied parent config objects.
|
||
|
* @param {string[]} parentDirectories a list of parent directories to add to the config cache
|
||
|
* @param {Object[]} parentConfigs a list of config objects that apply to the lowest directory in parentDirectories
|
||
|
* @returns {void}
|
||
|
* @private
|
||
|
*/
|
||
|
setHierarchyLocalConfigs(parentDirectories, parentConfigs) {
|
||
|
parentDirectories.forEach((localConfigDirectory, i) => {
|
||
|
const directoryParentConfigs = parentConfigs.slice(0, parentConfigs.length - i);
|
||
|
|
||
|
this.localHierarchyCache.set(localConfigDirectory, directoryParentConfigs);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Gets a merged config object corresponding to the supplied vector.
|
||
|
* @param {Array<Object>} vector the vector to find a merged config for
|
||
|
* @returns {Object|null} a merged config object, if found in the cache, otherwise null
|
||
|
* @private
|
||
|
*/
|
||
|
getMergedVectorConfig(vector) {
|
||
|
return this.mergedVectorCache.get(hash(vector));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Sets a merged config object in the cache for the supplied vector.
|
||
|
* @param {Array<Object>} vector the vector to save a merged config for
|
||
|
* @param {Object} config the merged config object to add to the cache
|
||
|
* @returns {void}
|
||
|
* @private
|
||
|
*/
|
||
|
setMergedVectorConfig(vector, config) {
|
||
|
this.mergedVectorCache.set(hash(vector), config);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Gets a merged config object corresponding to the supplied vector, including configuration options from outside
|
||
|
* the vector.
|
||
|
* @param {Array<Object>} vector the vector to find a merged config for
|
||
|
* @returns {Object|null} a merged config object, if found in the cache, otherwise null
|
||
|
* @private
|
||
|
*/
|
||
|
getMergedConfig(vector) {
|
||
|
return this.mergedCache.get(hash(vector));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Sets a merged config object in the cache for the supplied vector, including configuration options from outside
|
||
|
* the vector.
|
||
|
* @param {Array<Object>} vector the vector to save a merged config for
|
||
|
* @param {Object} config the merged config object to add to the cache
|
||
|
* @returns {void}
|
||
|
* @private
|
||
|
*/
|
||
|
setMergedConfig(vector, config) {
|
||
|
this.mergedCache.set(hash(vector), config);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = ConfigCache;
|