Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
auto-config-loader
Advanced tools
Find and load configuration from a package.json property, rc file, or CommonJS module.
Find and load configuration from a package.json
property, rc
file, or CommonJS
module. It has smart default based on traditional expectations in the JavaScript ecosystem. But it's also flexible enough to search anywhere you want and load whatever you want.
package.json
file$ npm i auto-config-loader
const autoConf = require('auto-config-loader');
import { autoConf } from 'auto-config-loader';
// will look for:
// process.cwd() + '.namespacerc'
// process.cwd() + '.namespacerc.js'
// process.cwd() + '.namespacerc.ts'
// process.cwd() + '.namespacerc.mjs'
// process.cwd() + '.namespacerc.cjs'
// process.cwd() + '.namespacerc.json'
// process.cwd() + '.namespacerc.json5'
// process.cwd() + '.namespacerc.jsonc'
// process.cwd() + '.namespacerc.yaml'
// process.cwd() + '.namespacerc.yml'
// process.cwd() + '.namespacerc.toml'
// process.cwd() + 'namespace.config.mjs'
// process.cwd() + 'namespace.config.cjs'
// process.cwd() + 'namespace.config.js'
// ........
const data = await autoConf('namespace', {
default: {
testItem2: 'some value'
}
});
Load the JS file and return the result, support .js
, .cjs
, .mjs
, .ts
.
// => ./app/app.config.js
export default {
name: 'app'
}
import { loadConf } from 'auto-config-loader/load-conf';
interface Config {
name: string;
}
const result = await loadConf<Config>('./app/app.config.js');
// => { name: 'app' }
import { LoadConfOption } from 'auto-config-loader';
export type LoaderFunc<T> = (filepath: string, content: string, jsOption?: LoadConfOption) => T | Promise<T>;
export type Loader<T> = Record<string, LoaderFunc<T>>;
export interface AutoConfOption<T> {
searchPlaces?: string[];
/** An object that maps extensions to the loader functions responsible for loading and parsing files with those extensions. */
loaders?: Loader<T>;
/** Specify default configuration. It has the lowest priority and is applied after extending config. */
default?: T;
/** Resolve configuration from this working directory. The default is `process.cwd()` */
cwd?: string;
/** Default transform js configuration */
jsOption?: LoadConfOption;
/** @deprecated use `mustExist` instead */
ignoreLog?: boolean;
mustExist?: boolean;
}
export declare const getConfigPath: () => string;
/**
* Find and load configuration from a `package.json` property, `rc` file, or `CommonJS` module.
* @param namespace {string} Configuration base name. The default is `autoconf`.
* @param option
*/
export declare function autoConf<T>(namespace?: string, option?: AutoConfOption<T>): Promise<{} & T>;
export default autoConf;
Discover configurations in the specified directory order. When configuring a tool, you can use multiple file formats and put these in multiple places. Usually, a tool would mention this in its own README file, but by default, these are the following places, where ${moduleName}
represents the name of the tool:
Default searchPlaces
:
[
'package.json',
`.${moduleName}rc`,
`.${moduleName}rc.json`,
`.${moduleName}rc.json5`,
`.${moduleName}rc.jsonc`,
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.toml`,
`.${moduleName}rc.ini`,
`.${moduleName}rc.js`,
`.${moduleName}rc.ts`,
`.${moduleName}rc.cjs`,
`.${moduleName}rc.mjs`,
`.config/${moduleName}rc`,
`.config/${moduleName}rc.json`,
`.config/${moduleName}rc.json5`,
`.config/${moduleName}rc.jsonc`,
`.config/${moduleName}rc.yaml`,
`.config/${moduleName}rc.yml`,
`.config/${moduleName}rc.toml`,
`.config/${moduleName}rc.ini`,
`.config/${moduleName}rc.js`,
`.config/${moduleName}rc.ts`,
`.config/${moduleName}rc.cjs`,
`.config/${moduleName}rc.mjs`,
`${moduleName}.config.js`,
`${moduleName}.config.ts`,
`${moduleName}.config.cjs`,
`${moduleName}.config.mjs`,
]
Configurations are loaded sequentially, and the configuration file search is terminated when a configuration file exists.
The content of these files is defined by the tool. For example, you can add a semi
configuration value to false
using a file called .config/autoconfig.yml
:
semi: true
Additionally, you have the option to put a property named after the tool in your package.json
file, with the contents of that property being the same as the file contents. To use the same example as above:
{
"name": "your-project",
"autoconfig": {
"semi": true
}
}
This has the advantage that you can put the configuration of all tools (at least the ones that use auto-config-loader
) in one file.
.js
,.ts
,.cjs
,.mjs
import type jiti from 'jiti';
import { Options } from 'sucrase';
type Jiti = ReturnType<typeof jiti>;
type JITIOptions = Jiti['options'];
export interface LoadConfOption {
jiti?: boolean;
jitiOptions?: JITIOptions;
transformOption?: Options;
}
export declare function loadConf<T>(path: string, option?: LoadConfOption): Promise<T>;
export declare function jsLoader<T>(
filepath: string,
content: string,
option?: LoadConfOption
): Promise<T>;
Modify default .js
,.ts
,.cjs
,.mjs
loader parameters.
import load, { jsLoader } from 'auto-config-loader';
function loadJS(filepath, content) {
return jsLoader(filepath, content, {
// change option...
});
}
const data = await load('namespace', {
loaders: {
'.js': loadJS,
'.ts': loadJS,
'.cjs': loadJS,
'.mjs': loadJS,
},
default: {
testItem2: 'some value'
}
});
example:
import { jsLoader } from 'auto-config-loader';
const data = jsLoader('/path/to/file/name.js')
.ini
export declare function iniLoader<T>(_: string, content: string): T;
example:
import { iniLoader } from 'auto-config-loader';
const data = iniLoader(undefined, `...`)
.json
,.jsonc
, json5
export declare function jsonLoader<T>(_: string, content: string): T;
example:
import { jsonLoader } from 'auto-config-loader';
const data = jsonLoader(undefined, `{ "a": 123 }`)
.toml
export declare function tomlLoader<T>(_: string, content: string): T;
example:
import { tomlLoader } from 'auto-config-loader';
const data = tomlLoader(undefined, `...`)
.yaml
export declare function yamlLoader<T>(_: string, content: string): T;
example:
import { yamlLoader } from 'auto-config-loader';
const data = yamlLoader(undefined, `...`)
Yaml
loaderThis is an example, the default yaml
/yml
does not require a loader.
import load from 'auto-config-loader';
import yaml from 'yaml';
function loadYaml(filepath, content) {
return yaml.parse(content);
}
const data = await load('namespace', {
searchPlaces: [
'.namespacerc.yaml',
'.namespacerc.yml',
],
loaders: {
'.yaml': loadYaml,
'.yml': loadYaml,
},
default: {
testItem2: 'some value'
}
});
export declare const merge: {
<TObject, TSource>(object: TObject, source: TSource): TObject & TSource;
<TObject, TSource1, TSource2>(object: TObject, source1: TSource1, source2: TSource2): TObject & TSource1 & TSource2;
<TObject, TSource1, TSource2, TSource3>(object: TObject, source1: TSource1, source2: TSource2, source3: TSource3): TObject & TSource1 & TSource2 & TSource3;
<TObject, TSource1, TSource2, TSource3, TSource4>(object: TObject, source1: TSource1, source2: TSource2, source3: TSource3, source4: TSource4): TObject & TSource1 & TSource2 & TSource3 & TSource4;
(object: any, ...otherArgs: any[]): any;
};
export declare function findConfigFile(
moduleName: string,
root: string,
searchPlaces?: string[]
): string;
export declare const getConfigPath: () => string;
Example:
import { autoConf, getConfigPath } from 'auto-config-loader';
const data = autoConf<Config>('idoc');
const configPath = getConfigPath();
// => /.autoconfrc.js
This guide provides the steps to migrate to the latest version of the configuration loader API.
Loader Functions Support Async
LoaderFunc<T>
now supports returning T
or Promise<T>
.Example:
export type LoaderFunc<T> = (
filepath: string,
content: string,
jsOption?: LoadConfOption
) => T | Promise<T>;
autoConf
Returns a Promise
autoConf
function now returns a Promise
instead of a synchronous result.Example:
export declare function autoConf<T>(
namespace?: string,
option?: AutoConfOption<T>
): Promise<{} & T>;
If you have custom loaders, update their return types to support asynchronous operations:
Example:
const jsonLoader: LoaderFunc<MyConfig> = async (
filepath, content
) => JSON.parse(content);
autoConf
CallsUpdate all calls to autoConf
to use await
or .then
to handle Promises:
Example Using await
:
const config = await autoConf('myNamespace', options);
console.log(config);
Example Using .then
:
autoConf('myNamespace', options).then(config => {
console.log(config);
});
As always, thanks to our amazing contributors!
Made with contributors.
This package is licensed under the MIT License.
FAQs
Find and load configuration from a package.json property, rc file, or CommonJS module.
The npm package auto-config-loader receives a total of 18,871 weekly downloads. As such, auto-config-loader popularity was classified as popular.
We found that auto-config-loader demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.