Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@humanwhocodes/config-array

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@humanwhocodes/config-array - npm Package Compare versions

Comparing version 0.10.4 to 0.10.5

424

api.js

@@ -139,3 +139,3 @@ 'use strict';

async function *flatTraverse(array) {
async function* flatTraverse(array) {
for (let item of array) {

@@ -157,3 +157,3 @@ if (typeof item === 'function') {

}
yield * flatTraverse(item);
yield* flatTraverse(item);
} else if (typeof item === 'function') {

@@ -196,3 +196,3 @@ throw new TypeError('A config function can only return an object or array.');

function *flatTraverse(array) {
function* flatTraverse(array) {
for (let item of array) {

@@ -217,3 +217,3 @@ if (typeof item === 'function') {

yield * flatTraverse(item);
yield* flatTraverse(item);
} else if (typeof item === 'function') {

@@ -407,35 +407,34 @@ throw new TypeError('A config function can only return an object or array.');

constructor(configs, {
basePath = '',
normalized = false,
schema: customSchema,
extraConfigTypes = []
} = {}
) {
basePath = '',
normalized = false,
schema: customSchema,
extraConfigTypes = []
} = {}
) {
super();
/**
* Tracks if the array has been normalized.
* @property isNormalized
* @type boolean
* @private
*/
* Tracks if the array has been normalized.
* @property isNormalized
* @type boolean
* @private
*/
this[ConfigArraySymbol.isNormalized] = normalized;
/**
* The schema used for validating and merging configs.
* @property schema
* @type ObjectSchema
* @private
*/
this[ConfigArraySymbol.schema] = new objectSchema.ObjectSchema({
...customSchema,
...baseSchema
});
* The schema used for validating and merging configs.
* @property schema
* @type ObjectSchema
* @private
*/
this[ConfigArraySymbol.schema] = new objectSchema.ObjectSchema(
Object.assign({}, customSchema, baseSchema)
);
/**
* The path of the config file that this array was loaded from.
* This is used to calculate filename matches.
* @property basePath
* @type string
*/
* The path of the config file that this array was loaded from.
* This is used to calculate filename matches.
* @property basePath
* @type string
*/
this.basePath = basePath;

@@ -446,14 +445,14 @@

/**
* The supported config types.
* @property configTypes
* @type Array<string>
*/
* The supported config types.
* @property configTypes
* @type Array<string>
*/
this.extraConfigTypes = Object.freeze([...extraConfigTypes]);
/**
* A cache to store calculated configs for faster repeat lookup.
* @property configCache
* @type Map
* @private
*/
* A cache to store calculated configs for faster repeat lookup.
* @property configCache
* @type Map
* @private
*/
this[ConfigArraySymbol.configCache] = new Map();

@@ -466,5 +465,5 @@

if (Array.isArray(configs)) {
this.push(...configs);
this.push(...configs);
} else {
this.push(configs);
this.push(configs);
}

@@ -474,3 +473,3 @@

/**
/**
* Prevent normal array methods from creating a new `ConfigArray` instance.

@@ -482,7 +481,7 @@ * This is to ensure that methods such as `slice()` won't try to create a

*/
static get [Symbol.species]() {
return Array;
}
static get [Symbol.species]() {
return Array;
}
/**
/**
* Returns the `files` globs from every config object in the array.

@@ -494,33 +493,33 @@ * This can be used to determine which files will be matched by a

*/
get files() {
get files() {
assertNormalized(this);
assertNormalized(this);
// if this data has been cached, retrieve it
const cache = dataCache.get(this);
// if this data has been cached, retrieve it
const cache = dataCache.get(this);
if (cache.files) {
return cache.files;
}
if (cache.files) {
return cache.files;
}
// otherwise calculate it
// otherwise calculate it
const result = [];
const result = [];
for (const config of this) {
if (config.files) {
config.files.forEach(filePattern => {
result.push(filePattern);
});
for (const config of this) {
if (config.files) {
config.files.forEach(filePattern => {
result.push(filePattern);
});
}
}
}
// store result
cache.files = result;
dataCache.set(this, cache);
// store result
cache.files = result;
dataCache.set(this, cache);
return result;
}
return result;
}
/**
/**
* Returns ignore matchers that should always be ignored regardless of

@@ -532,45 +531,45 @@ * the matching `files` fields in any configs. This is necessary to mimic

*/
get ignores() {
get ignores() {
assertNormalized(this);
assertNormalized(this);
// if this data has been cached, retrieve it
const cache = dataCache.get(this);
// if this data has been cached, retrieve it
const cache = dataCache.get(this);
if (cache.ignores) {
return cache.ignores;
}
if (cache.ignores) {
return cache.ignores;
}
// otherwise calculate it
// otherwise calculate it
const result = [];
const result = [];
for (const config of this) {
for (const config of this) {
/*
* We only count ignores if there are no other keys in the object.
* In this case, it acts list a globally ignored pattern. If there
* are additional keys, then ignores act like exclusions.
*/
if (config.ignores && Object.keys(config).length === 1) {
result.push(...config.ignores);
/*
* We only count ignores if there are no other keys in the object.
* In this case, it acts list a globally ignored pattern. If there
* are additional keys, then ignores act like exclusions.
*/
if (config.ignores && Object.keys(config).length === 1) {
result.push(...config.ignores);
}
}
}
// store result
cache.ignores = result;
dataCache.set(this, cache);
// store result
cache.ignores = result;
dataCache.set(this, cache);
return result;
}
return result;
}
/**
/**
* Indicates if the config array has been normalized.
* @returns {boolean} True if the config array is normalized, false if not.
*/
isNormalized() {
return this[ConfigArraySymbol.isNormalized];
}
isNormalized() {
return this[ConfigArraySymbol.isNormalized];
}
/**
/**
* Normalizes a config array by flattening embedded arrays and executing

@@ -581,18 +580,18 @@ * config functions.

*/
async normalize(context = {}) {
async normalize(context = {}) {
if (!this.isNormalized()) {
const normalizedConfigs = await normalize(this, context, this.extraConfigTypes);
this.length = 0;
this.push(...normalizedConfigs.map(this[ConfigArraySymbol.preprocessConfig].bind(this)));
this[ConfigArraySymbol.isNormalized] = true;
if (!this.isNormalized()) {
const normalizedConfigs = await normalize(this, context, this.extraConfigTypes);
this.length = 0;
this.push(...normalizedConfigs.map(this[ConfigArraySymbol.preprocessConfig].bind(this)));
this[ConfigArraySymbol.isNormalized] = true;
// prevent further changes
Object.freeze(this);
// prevent further changes
Object.freeze(this);
}
return this;
}
return this;
}
/**
/**
* Normalizes a config array by flattening embedded arrays and executing

@@ -603,18 +602,18 @@ * config functions.

*/
normalizeSync(context = {}) {
normalizeSync(context = {}) {
if (!this.isNormalized()) {
const normalizedConfigs = normalizeSync(this, context, this.extraConfigTypes);
this.length = 0;
this.push(...normalizedConfigs.map(this[ConfigArraySymbol.preprocessConfig].bind(this)));
this[ConfigArraySymbol.isNormalized] = true;
if (!this.isNormalized()) {
const normalizedConfigs = normalizeSync(this, context, this.extraConfigTypes);
this.length = 0;
this.push(...normalizedConfigs.map(this[ConfigArraySymbol.preprocessConfig].bind(this)));
this[ConfigArraySymbol.isNormalized] = true;
// prevent further changes
Object.freeze(this);
// prevent further changes
Object.freeze(this);
}
return this;
}
return this;
}
/**
/**
* Finalizes the state of a config before being cached and returned by

@@ -626,7 +625,7 @@ * `getConfig()`. Does nothing by default but is provided to be

*/
[ConfigArraySymbol.finalizeConfig](config) {
return config;
}
[ConfigArraySymbol.finalizeConfig](config) {
return config;
}
/**
/**
* Preprocesses a config during the normalization process. This is the

@@ -639,7 +638,7 @@ * method to override if you want to convert an array item before it is

*/
[ConfigArraySymbol.preprocessConfig](config) {
return config;
}
[ConfigArraySymbol.preprocessConfig](config) {
return config;
}
/**
/**
* Determines if a given file path explicitly matches a `files` entry

@@ -652,45 +651,45 @@ * and also doesn't match an `ignores` entry. Configs that don't have

*/
isExplicitMatch(filePath) {
isExplicitMatch(filePath) {
assertNormalized(this);
assertNormalized(this);
const cache = dataCache.get(this);
const cache = dataCache.get(this);
// first check the cache to avoid duplicate work
let result = cache.explicitMatches.get(filePath);
// first check the cache to avoid duplicate work
let result = cache.explicitMatches.get(filePath);
if (typeof result == 'boolean') {
return result;
}
if (typeof result == 'boolean') {
return result;
}
// TODO: Maybe move elsewhere? Maybe combine with getConfig() logic?
const relativeFilePath = path.relative(this.basePath, filePath);
// TODO: Maybe move elsewhere? Maybe combine with getConfig() logic?
const relativeFilePath = path.relative(this.basePath, filePath);
if (shouldIgnoreFilePath(this.ignores, filePath, relativeFilePath)) {
debug(`Ignoring ${filePath}`);
if (shouldIgnoreFilePath(this.ignores, filePath, relativeFilePath)) {
debug(`Ignoring ${filePath}`);
// cache and return result
cache.explicitMatches.set(filePath, false);
return false;
}
// cache and return result
cache.explicitMatches.set(filePath, false);
return false;
}
// filePath isn't automatically ignored, so try to find a match
// filePath isn't automatically ignored, so try to find a match
for (const config of this) {
for (const config of this) {
if (!config.files) {
continue;
if (!config.files) {
continue;
}
if (pathMatches(filePath, this.basePath, config)) {
debug(`Matching config found for ${filePath}`);
cache.explicitMatches.set(filePath, true);
return true;
}
}
if (pathMatches(filePath, this.basePath, config)) {
debug(`Matching config found for ${filePath}`);
cache.explicitMatches.set(filePath, true);
return true;
}
return false;
}
return false;
}
/**
/**
* Returns the config object for a given file path.

@@ -700,68 +699,85 @@ * @param {string} filePath The complete path of a file to get a config for.

*/
getConfig(filePath) {
getConfig(filePath) {
assertNormalized(this);
assertNormalized(this);
// first check the cache to avoid duplicate work
let finalConfig = this[ConfigArraySymbol.configCache].get(filePath);
const cache = this[ConfigArraySymbol.configCache];
if (finalConfig) {
return finalConfig;
}
// first check the cache for a filename match to avoid duplicate work
let finalConfig = cache.get(filePath);
// TODO: Maybe move elsewhere?
const relativeFilePath = path.relative(this.basePath, filePath);
if (finalConfig) {
return finalConfig;
}
if (shouldIgnoreFilePath(this.ignores, filePath, relativeFilePath)) {
debug(`Ignoring ${filePath}`);
// next check to see if the file should be ignored
// cache and return result - finalConfig is undefined at this point
this[ConfigArraySymbol.configCache].set(filePath, finalConfig);
return finalConfig;
}
// TODO: Maybe move elsewhere?
const relativeFilePath = path.relative(this.basePath, filePath);
// filePath isn't automatically ignored, so try to construct config
if (shouldIgnoreFilePath(this.ignores, filePath, relativeFilePath)) {
debug(`Ignoring ${filePath}`);
const matchingConfigs = [];
let matchFound = false;
// cache and return result - finalConfig is undefined at this point
cache.set(filePath, finalConfig);
return finalConfig;
}
for (const config of this) {
// filePath isn't automatically ignored, so try to construct config
if (!config.files) {
debug(`Universal config found for ${filePath}`);
matchingConfigs.push(config);
continue;
}
const matchingConfigIndices = [];
let matchFound = false;
if (pathMatches(filePath, this.basePath, config)) {
debug(`Matching config found for ${filePath}`);
matchingConfigs.push(config);
matchFound = true;
continue;
this.forEach((config, index) => {
if (!config.files) {
debug(`Universal config found for ${filePath}`);
matchingConfigIndices.push(index);
return;
}
if (pathMatches(filePath, this.basePath, config)) {
debug(`Matching config found for ${filePath}`);
matchingConfigIndices.push(index);
matchFound = true;
return;
}
});
// if matching both files and ignores, there will be no config to create
if (!matchFound) {
debug(`No matching configs found for ${filePath}`);
// cache and return result - finalConfig is undefined at this point
cache.set(filePath, finalConfig);
return finalConfig;
}
}
// if matching both files and ignores, there will be no config to create
if (!matchFound) {
debug(`No matching configs found for ${filePath}`);
// check to see if there is a config cached by indices
finalConfig = cache.get(matchingConfigIndices.toString());
// cache and return result - finalConfig is undefined at this point
this[ConfigArraySymbol.configCache].set(filePath, finalConfig);
return finalConfig;
}
if (finalConfig) {
// otherwise construct the config
// also store for filename for faster lookup next time
cache.set(filePath, finalConfig);
finalConfig = matchingConfigs.reduce((result, config) => {
return this[ConfigArraySymbol.schema].merge(result, config);
}, {}, this);
return finalConfig;
}
finalConfig = this[ConfigArraySymbol.finalizeConfig](finalConfig);
// otherwise construct the config
this[ConfigArraySymbol.configCache].set(filePath, finalConfig);
finalConfig = matchingConfigIndices.reduce((result, index) => {
return this[ConfigArraySymbol.schema].merge(result, this[index]);
}, {}, this);
return finalConfig;
}
finalConfig = this[ConfigArraySymbol.finalizeConfig](finalConfig);
/**
cache.set(filePath, finalConfig);
cache.set(matchingConfigIndices.toString(), finalConfig);
return finalConfig;
}
/**
* Determines if the given filepath is ignored based on the configs.

@@ -771,5 +787,5 @@ * @param {string} filePath The complete path of a file to check.

*/
isIgnored(filePath) {
return this.getConfig(filePath) === undefined;
}
isIgnored(filePath) {
return this.getConfig(filePath) === undefined;
}

@@ -776,0 +792,0 @@ }

# Changelog
## [0.10.5](https://github.com/humanwhocodes/config-array/compare/v0.10.4...v0.10.5) (2022-09-21)
### Bug Fixes
* Improve caching to improve performance ([#50](https://github.com/humanwhocodes/config-array/issues/50)) ([8a7e8ab](https://github.com/humanwhocodes/config-array/commit/8a7e8ab499bcbb10d7cbdd676197fc686966a64e))
### [0.10.4](https://www.github.com/humanwhocodes/config-array/compare/v0.10.3...v0.10.4) (2022-07-29)

@@ -4,0 +11,0 @@

{
"name": "@humanwhocodes/config-array",
"version": "0.10.4",
"version": "0.10.5",
"description": "Glob-based configuration matching.",

@@ -49,8 +49,8 @@ "author": "Nicholas C. Zakas",

"devDependencies": {
"@nitpik/javascript": "0.3.3",
"@nitpik/javascript": "0.4.0",
"@nitpik/node": "0.0.5",
"chai": "4.2.0",
"eslint": "6.7.1",
"eslint": "8.23.1",
"esm": "3.2.25",
"lint-staged": "10.2.8",
"lint-staged": "13.0.3",
"mocha": "6.2.3",

@@ -57,0 +57,0 @@ "nyc": "14.1.1",

@@ -77,7 +77,7 @@ # Config Array

This example reads in an object or array from `my.config.js` and passes it into the `ConfigArray` constructor as the first argument. The second argument is an object specifying the `basePath` (the directoy in which `my.config.js` is found) and a `schema` to define the additional properties of a config object beyond `files`, `ignores`, and `name`.
This example reads in an object or array from `my.config.js` and passes it into the `ConfigArray` constructor as the first argument. The second argument is an object specifying the `basePath` (the directory in which `my.config.js` is found) and a `schema` to define the additional properties of a config object beyond `files`, `ignores`, and `name`.
### Specifying a Schema
The `schema` option is required for you to use additional properties in config objects. The schema is object that follows the format of an [`ObjectSchema`](https://npmjs.com/package/@humanwhocodes/object-schema). The schema specifies both validation and merge rules that the `ConfigArray` instance needs to combine configs when there are multiple matches. Here's an example:
The `schema` option is required for you to use additional properties in config objects. The schema is an object that follows the format of an [`ObjectSchema`](https://npmjs.com/package/@humanwhocodes/object-schema). The schema specifies both validation and merge rules that the `ConfigArray` instance needs to combine configs when there are multiple matches. Here's an example:

@@ -178,3 +178,3 @@ ```js

If a pattern in the files array begins with `!` then it excludes that pattern. In the preceding example, any filename that doesn't end with `.js` will automatically getting a `settings.js` property set to `false`.
If a pattern in the files array begins with `!` then it excludes that pattern. In the preceding example, any filename that doesn't end with `.js` will automatically get a `settings.js` property set to `false`.

@@ -295,2 +295,9 @@ You can also specify an `ignores` key that will force files matching those patterns to not be included. If the `ignores` key is in a config object without any other keys, then those ignores will always be applied; otherwise those ignores act as exclusions. Here's an example:

## Caching Mechanisms
Each `ConfigArray` aggressively caches configuration objects to avoid unnecessary work. This caching occurs in two ways:
1. **File-based Caching.** For each filename that is passed into a method, the resulting config is cached against that filename so you're always guaranteed to get the same object returned from `getConfig()` whenever you pass the same filename in.
2. **Index-based Caching.** Whenever a config is calculated, the config elements that were used to create the config are also cached. So if a given filename matches elements 1, 5, and 7, the resulting config is cached with a key of `1,5,7`. That way, if another file is passed that matches the same config elements, the result is already known and doesn't have to be recalculated. That means two files that match all the same elements will return the same config from `getConfig()`.
## Acknowledgements

@@ -297,0 +304,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc