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

configuration

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

configuration - npm Package Compare versions

Comparing version 1.1.1 to 1.2.0

dist/utils/parser.d.ts

4

dist/config.d.ts

@@ -5,4 +5,4 @@ declare const DEFAULTS: {

indentation: number;
dataRaw: string;
readonly data: {};
defaults: {};
defaultsRaw: string;
};

@@ -9,0 +9,0 @@ declare const SCOPE_ALL = "*";

"use strict";
/* CONFIG */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SCOPE_DEFAULTS = exports.SCOPE_ALL = exports.DEFAULTS = void 0;
const DEFAULTS = {

@@ -8,6 +9,4 @@ id: 'confId',

indentation: 2,
dataRaw: '{\n \n}',
get data() {
return {};
}
defaults: {},
defaultsRaw: '{\n \n}'
};

@@ -14,0 +13,0 @@ exports.DEFAULTS = DEFAULTS;

@@ -7,2 +7,3 @@ import { ValidateFunction } from 'ajv';

defaults: Provider;
isArray: boolean;
schema?: Schema;

@@ -9,0 +10,0 @@ validator?: ValidateFunction;

@@ -23,2 +23,3 @@ "use strict";

this.handlers = [];
this.isArray = Array.isArray(options.defaults);
this.defaults = new memory_1.default({ scope: config_1.SCOPE_DEFAULTS });

@@ -107,4 +108,4 @@ this.defaults.writeSync(options.defaults ? path_prop_1.default.unflat(options.defaults) : {}, true);

refresh() {
const datas = this.providers.map(provider => provider.dataSchema).reverse();
this.dataSchema = plain_object_merge_1.default(datas);
const datas = this.providers.map(provider => provider.dataSchema).reverse(), datasFiltered = datas.filter(data => Array.isArray(data) === this.isArray);
this.dataSchema = this.isArray ? Array.prototype.concat(...datasFiltered) : plain_object_merge_1.default(datasFiltered);
this.triggerChange();

@@ -117,2 +118,4 @@ }

this.validator(clone);
if (Array.isArray(clone))
return clone.filter(x => !type_1.default.isUndefined(x));
return clone;

@@ -235,3 +238,17 @@ }

reset(scope = config_1.SCOPE_ALL) {
return this.update(scope, config_1.DEFAULTS.dataRaw);
if (scope === config_1.SCOPE_ALL) { // All
for (let scope in this.scopes) {
if (scope === config_1.SCOPE_DEFAULTS)
continue;
this.scopes[scope].write(this.scopes[scope].defaultsRaw);
}
}
else { // Scope
if (scope === config_1.SCOPE_DEFAULTS)
throw new Error('You can\'t reset the "defaults" scope');
const provider = this.scopes[scope];
if (!provider)
throw new Error('You can\'t reset unknown scopes');
provider.write(provider.defaultsRaw);
}
}

@@ -238,0 +255,0 @@ triggerChange() {

@@ -1,8 +0,10 @@

import { Disposer, Data, DataRaw, DataUpdate, ProviderChangeHandler, ProviderAbstractOptions } from '../types';
import { Disposer, Data, DataRaw, DataUpdate, DataParser, ProviderChangeHandler, ProviderAbstractOptions } from '../types';
declare abstract class ProviderAbstract<Options extends ProviderAbstractOptions = ProviderAbstractOptions> {
scope: string;
indentation: string | number;
data: Data;
dataRaw: DataRaw;
dataSchema: Data;
dataParser: DataParser;
defaults: Data;
defaultsRaw: DataRaw;
handlers: ProviderChangeHandler[];

@@ -9,0 +11,0 @@ constructor(options?: Partial<Options>);

@@ -6,2 +6,3 @@ "use strict";

const config_1 = require("../config");
const parser_1 = require("../utils/parser");
const type_1 = require("../utils/type");

@@ -11,7 +12,9 @@ /* ABSTRACT */

constructor(options) {
var _a, _b;
var _a, _b, _c, _d, _e;
if ((options === null || options === void 0 ? void 0 : options.scope) === config_1.SCOPE_ALL)
throw new Error(`"${config_1.SCOPE_ALL}" is not a valid scope name for a provider`);
this.scope = (_a = options === null || options === void 0 ? void 0 : options.scope) !== null && _a !== void 0 ? _a : config_1.DEFAULTS.scope;
this.indentation = (_b = options === null || options === void 0 ? void 0 : options.indentation) !== null && _b !== void 0 ? _b : config_1.DEFAULTS.indentation;
this.dataParser = (_b = options === null || options === void 0 ? void 0 : options.parser) !== null && _b !== void 0 ? _b : new parser_1.default((_c = options === null || options === void 0 ? void 0 : options.indentation) !== null && _c !== void 0 ? _c : config_1.DEFAULTS.indentation);
this.defaults = (_d = options === null || options === void 0 ? void 0 : options.defaults) !== null && _d !== void 0 ? _d : config_1.DEFAULTS.defaults;
this.defaultsRaw = (_e = options === null || options === void 0 ? void 0 : options.defaultsRaw) !== null && _e !== void 0 ? _e : config_1.DEFAULTS.defaultsRaw;
this.handlers = [];

@@ -18,0 +21,0 @@ this.init();

@@ -32,8 +32,8 @@ "use strict";

this.watcher = file_1.default.watch(path, async () => {
const { data, dataRaw } = await this.read();
const { dataRaw } = await this.read();
if (path !== this.path)
return;
if (this.isEqual(dataRaw) && this.isEqual(data))
if (this.isEqual(dataRaw))
return;
super.write(data, true);
super.write(dataRaw, true);
});

@@ -40,0 +40,0 @@ }

"use strict";
/* IMPORT */
Object.defineProperty(exports, "__esModule", { value: true });
const config_1 = require("../config");
const plain_object_clone_1 = require("plain-object-clone");
const file_1 = require("../utils/file");
const serializer_1 = require("../utils/serializer");
const file_2 = require("./file");

@@ -11,23 +10,29 @@ /* JSON */

async read() {
var _a, _b;
if (!this.path)
return super.read();
try {
const dataRaw = await file_1.default.read(this.path, { encoding: 'utf8' }) || config_1.DEFAULTS.dataRaw, data = serializer_1.default.deserialize(dataRaw);
const dataRaw = (_a = await file_1.default.read(this.path, { encoding: 'utf8' })) !== null && _a !== void 0 ? _a : this.defaultsRaw, data = (_b = this.dataParser.parse(dataRaw)) !== null && _b !== void 0 ? _b : plain_object_clone_1.default(this.defaults);
return { data, dataRaw };
}
catch (_a) {
const { data, dataRaw } = config_1.DEFAULTS;
return { data, dataRaw };
catch (_c) {
return {
data: plain_object_clone_1.default(this.defaults),
dataRaw: this.defaultsRaw
};
}
}
readSync() {
var _a, _b;
if (!this.path)
return super.readSync();
try {
const dataRaw = file_1.default.readSync(this.path, { encoding: 'utf8' }) || config_1.DEFAULTS.dataRaw, data = serializer_1.default.deserialize(dataRaw);
const dataRaw = (_a = file_1.default.readSync(this.path, { encoding: 'utf8' })) !== null && _a !== void 0 ? _a : this.defaultsRaw, data = (_b = this.dataParser.parse(dataRaw)) !== null && _b !== void 0 ? _b : plain_object_clone_1.default(this.defaults);
return { data, dataRaw };
}
catch (_a) {
const { data, dataRaw } = config_1.DEFAULTS;
return { data, dataRaw };
catch (_c) {
return {
data: plain_object_clone_1.default(this.defaults),
dataRaw: this.defaultsRaw
};
}

@@ -34,0 +39,0 @@ }

"use strict";
/* IMPORT */
Object.defineProperty(exports, "__esModule", { value: true });
const config_1 = require("../config");
const serializer_1 = require("../utils/serializer");
const plain_object_clone_1 = require("plain-object-clone");
const type_1 = require("../utils/type");

@@ -14,3 +13,4 @@ const abstract_1 = require("./abstract");

readSync() {
const data = this.data || config_1.DEFAULTS.data, dataRaw = this.dataRaw || config_1.DEFAULTS.dataRaw;
var _a, _b;
const data = (_a = this.data) !== null && _a !== void 0 ? _a : plain_object_clone_1.default(this.defaults), dataRaw = (_b = this.dataRaw) !== null && _b !== void 0 ? _b : this.defaultsRaw;
return { data, dataRaw };

@@ -22,6 +22,7 @@ }

writeSync(data, force = false) {
var _a, _b;
if (!force && this.isEqual(data))
return;
if (type_1.default.isString(data)) {
this.data = serializer_1.default.deserialize(data);
this.data = (_a = this.dataParser.parse(data)) !== null && _a !== void 0 ? _a : plain_object_clone_1.default(this.defaults);
this.dataRaw = data;

@@ -32,3 +33,3 @@ this.dataSchema = this.validate(this.data);

this.data = data;
this.dataRaw = serializer_1.default.serialize(this.data, this.indentation);
this.dataRaw = (_b = this.dataParser.stringify(this.data)) !== null && _b !== void 0 ? _b : this.defaultsRaw;
this.dataSchema = this.validate(this.data);

@@ -35,0 +36,0 @@ }

"use strict";
/* IMPORT */
Object.defineProperty(exports, "__esModule", { value: true });
const plain_object_clone_1 = require("plain-object-clone");
const config_1 = require("../config");
const serializer_1 = require("../utils/serializer");
const memory_1 = require("./memory");

@@ -19,5 +19,6 @@ /* STORAGE */

readSync() {
var _a, _b;
if (!this.storage)
return super.readSync();
const dataRaw = this.storage.getItem(this.id) || config_1.DEFAULTS.dataRaw, data = serializer_1.default.deserialize(dataRaw);
const dataRaw = (_a = this.storage.getItem(this.id)) !== null && _a !== void 0 ? _a : this.defaultsRaw, data = (_b = this.dataParser.parse(dataRaw)) !== null && _b !== void 0 ? _b : plain_object_clone_1.default(this.defaults);
return { data, dataRaw };

@@ -24,0 +25,0 @@ }

@@ -9,3 +9,3 @@ import { ValidateFunction } from 'ajv';

declare type Path = string;
declare type Data = ValueObject;
declare type Data = ValueArray | ValueObject;
declare type DataRaw = string;

@@ -16,2 +16,6 @@ declare type DataUpdate = {

};
declare type DataParser = {
parse: (raw: DataRaw) => Data | undefined;
stringify: (data: Data) => DataRaw | undefined;
};
declare type Value = ValuePrimitive | ValueArray | ValueObject;

@@ -46,3 +50,6 @@ declare type ValuePrimitive = null | boolean | number | string;

scope: string;
indentation: string | number;
defaults?: Data;
defaultsRaw?: DataRaw;
indentation?: string | number;
parser?: DataParser;
};

@@ -59,2 +66,2 @@ declare type ProviderFileOptions = ProviderAbstractOptions & {

};
export { Scope, ScopeAll, Scopes, Path, Data, DataRaw, DataUpdate, ExtendData, Value, ValueObject, Schema, ChangeHandler, ChangeHandlerData, Disposer, Options, Provider, ProviderChangeHandler, ProviderAbstractOptions, ProviderFileOptions, ProviderJSONOptions, ProviderMemoryOptions, ProviderStorageOptions };
export { Scope, ScopeAll, Scopes, Path, Data, DataRaw, DataUpdate, DataParser, ExtendData, Value, ValueArray, ValueObject, Schema, ChangeHandler, ChangeHandlerData, Disposer, Options, Provider, ProviderChangeHandler, ProviderAbstractOptions, ProviderFileOptions, ProviderJSONOptions, ProviderMemoryOptions, ProviderStorageOptions };

@@ -25,4 +25,4 @@ "use strict";

const watcher = require('chokidar-watcher'); // Lazy import for performance
const change = () => callback();
return watcher(filePath, {}, { change });
const handler = () => callback();
return watcher(filePath, {}, handler);
}

@@ -29,0 +29,0 @@ };

@@ -8,3 +8,3 @@ "use strict";

ensure(folderPath) {
return fs.promises.mkdir(folderPath, { recursive: true }).catch(() => { });
return fs.promises.mkdir(folderPath, { recursive: true }).then(() => { }, () => { });
},

@@ -11,0 +11,0 @@ ensureSync(folderPath) {

@@ -1,5 +0,5 @@

import { ValueObject } from '../types';
import { ValueArray, ValueObject } from '../types';
declare const Type: {
isNull(x: any): x is null;
isObject(x: any): x is ValueObject;
isObject(x: any): x is ValueArray | ValueObject;
isString(x: any): x is string;

@@ -6,0 +6,0 @@ isUndefined(x: any): x is undefined;

{
"name": "configuration",
"description": "Performant and feature rich library for managing configurations/settings.",
"version": "1.1.1",
"version": "1.2.0",
"main": "dist/index.js",

@@ -49,9 +49,9 @@ "types": "dist/index.d.ts",

"dependencies": {
"ajv": "^6.12.0",
"ajv": "^6.12.2",
"ajv-filter": "^1.1.0",
"chokidar-watcher": "^1.1.2",
"graceful-fs": "^4.2.3",
"chokidar-watcher": "^1.4.1",
"graceful-fs": "^4.2.4",
"is-primitive": "^3.0.1",
"json5": "^2.1.1",
"path-prop": "^1.0.1",
"json5": "^2.1.3",
"path-prop": "^1.1.0",
"plain-object-clone": "^1.1.0",

@@ -69,3 +69,3 @@ "plain-object-is-equal": "^1.0.0",

"@types/json5": "0.0.30",
"@types/node": "^13.9.0",
"@types/node": "^14.0.5",
"@types/write-file-atomic": "^3.0.0",

@@ -77,8 +77,8 @@ "ava": "^2.4.0",

"lodash": "^4.17.15",
"nyc": "^15.0.0",
"nyc": "^15.0.1",
"rimraf": "^3.0.2",
"tempy": "^0.4.0",
"typescript": "^3.8.3",
"tempy": "^0.5.0",
"typescript": "^3.9.3",
"typescript-transform-export-interop": "^1.0.2"
}
}

@@ -8,6 +8,4 @@

indentation: 2,
dataRaw: '{\n \n}',
get data () {
return {};
}
defaults: {},
defaultsRaw: '{\n \n}'
};

@@ -14,0 +12,0 @@

@@ -11,3 +11,3 @@

import {Scope, ScopeAll, Scopes, Path, Value, Data, DataRaw, Schema, ExtendData, Disposer, ChangeHandler, ChangeHandlerData, Options, Provider} from './types';
import {DEFAULTS, SCOPE_ALL, SCOPE_DEFAULTS} from './config';
import {SCOPE_ALL, SCOPE_DEFAULTS} from './config';
import ProviderMemory from './providers/memory';

@@ -26,2 +26,3 @@ import AJV from './utils/ajv';

defaults: Provider;
isArray: boolean;
schema?: Schema;

@@ -44,2 +45,4 @@ validator?: ValidateFunction;

this.isArray = Array.isArray ( options.defaults );
this.defaults = new ProviderMemory ({ scope: SCOPE_DEFAULTS });

@@ -194,5 +197,6 @@ this.defaults.writeSync ( options.defaults ? pp.unflat ( options.defaults ) : {}, true );

const datas = this.providers.map ( provider => provider.dataSchema ).reverse ();
const datas = this.providers.map ( provider => provider.dataSchema ).reverse (),
datasFiltered = datas.filter ( data => Array.isArray ( data ) === this.isArray );
this.dataSchema = merge ( datas );
this.dataSchema = this.isArray ? Array.prototype.concat ( ...datasFiltered ) : merge ( datasFiltered );

@@ -211,2 +215,4 @@ this.triggerChange ();

if ( Array.isArray ( clone ) ) return clone.filter ( x => !Type.isUndefined ( x ) );
return clone;

@@ -410,4 +416,24 @@

return this.update ( scope, DEFAULTS.dataRaw );
if ( scope === SCOPE_ALL ) { // All
for ( let scope in this.scopes ) {
if ( scope === SCOPE_DEFAULTS ) continue;
this.scopes[scope].write ( this.scopes[scope].defaultsRaw );
}
} else { // Scope
if ( scope === SCOPE_DEFAULTS ) throw new Error ( 'You can\'t reset the "defaults" scope' );
const provider = this.scopes[scope];
if ( !provider ) throw new Error ( 'You can\'t reset unknown scopes' );
provider.write ( provider.defaultsRaw );
}
}

@@ -414,0 +440,0 @@

@@ -5,4 +5,5 @@

import isEqual from 'plain-object-is-equal';
import {Disposer, Data, DataRaw, DataUpdate, ProviderChangeHandler, ProviderAbstractOptions} from '../types';
import {Disposer, Data, DataRaw, DataUpdate, DataParser, ProviderChangeHandler, ProviderAbstractOptions} from '../types';
import {DEFAULTS, SCOPE_ALL} from '../config';
import Parser from '../utils/parser';
import Type from '../utils/type';

@@ -15,6 +16,8 @@

scope: string;
indentation: string | number;
data: Data;
dataRaw: DataRaw;
dataSchema: Data;
dataParser: DataParser;
defaults: Data;
defaultsRaw: DataRaw;
handlers: ProviderChangeHandler[];

@@ -27,3 +30,5 @@

this.scope = options?.scope ?? DEFAULTS.scope;
this.indentation = options?.indentation ?? DEFAULTS.indentation;
this.dataParser = options?.parser ?? new Parser ( options?.indentation ?? DEFAULTS.indentation );
this.defaults = options?.defaults ?? DEFAULTS.defaults;
this.defaultsRaw = options?.defaultsRaw ?? DEFAULTS.defaultsRaw;
this.handlers = [];

@@ -30,0 +35,0 @@

@@ -57,9 +57,9 @@

const {data, dataRaw} = await this.read ();
const {dataRaw} = await this.read ();
if ( path !== this.path ) return;
if ( this.isEqual ( dataRaw ) && this.isEqual ( data ) ) return;
if ( this.isEqual ( dataRaw ) ) return;
super.write ( data, true );
super.write ( dataRaw, true );

@@ -66,0 +66,0 @@ });

/* IMPORT */
import cloneDeep from 'plain-object-clone';
import {Data, DataRaw, DataUpdate, ProviderJSONOptions} from '../types';
import {DEFAULTS} from '../config';
import File from '../utils/file';
import Serializer from '../utils/serializer';
import ProviderFile from './file';

@@ -20,4 +19,4 @@

const dataRaw = await File.read ( this.path, { encoding: 'utf8' } ) || DEFAULTS.dataRaw,
data = Serializer.deserialize ( dataRaw );
const dataRaw = await File.read ( this.path, { encoding: 'utf8' } ) ?? this.defaultsRaw,
data = this.dataParser.parse ( dataRaw ) ?? cloneDeep ( this.defaults );

@@ -28,6 +27,7 @@ return {data, dataRaw};

const {data, dataRaw} = DEFAULTS;
return {
data: cloneDeep ( this.defaults ),
dataRaw: this.defaultsRaw
};
return {data, dataRaw};
}

@@ -43,4 +43,4 @@

const dataRaw = File.readSync ( this.path, { encoding: 'utf8' } ) || DEFAULTS.dataRaw,
data = Serializer.deserialize ( dataRaw );
const dataRaw = File.readSync ( this.path, { encoding: 'utf8' } ) ?? this.defaultsRaw,
data = this.dataParser.parse ( dataRaw ) ?? cloneDeep ( this.defaults );

@@ -51,6 +51,7 @@ return {data, dataRaw};

const {data, dataRaw} = DEFAULTS;
return {
data: cloneDeep ( this.defaults ),
dataRaw: this.defaultsRaw
};
return {data, dataRaw};
}

@@ -57,0 +58,0 @@

/* IMPORT */
import cloneDeep from 'plain-object-clone';
import {Data, DataRaw, DataUpdate, ProviderMemoryOptions} from '../types';
import {DEFAULTS} from '../config';
import Serializer from '../utils/serializer';
import Type from '../utils/type';

@@ -22,4 +21,4 @@ import ProviderAbstract from './abstract';

const data = this.data || DEFAULTS.data,
dataRaw = this.dataRaw || DEFAULTS.dataRaw;
const data = this.data ?? cloneDeep ( this.defaults ),
dataRaw = this.dataRaw ?? this.defaultsRaw;

@@ -42,3 +41,3 @@ return {data, dataRaw};

this.data = Serializer.deserialize ( data );
this.data = this.dataParser.parse ( data ) ?? cloneDeep ( this.defaults );
this.dataRaw = data;

@@ -50,3 +49,3 @@ this.dataSchema = this.validate ( this.data );

this.data = data;
this.dataRaw = Serializer.serialize ( this.data, this.indentation );
this.dataRaw = this.dataParser.stringify ( this.data ) ?? this.defaultsRaw;
this.dataSchema = this.validate ( this.data );

@@ -53,0 +52,0 @@

/* IMPORT */
import cloneDeep from 'plain-object-clone';
import {Data, DataRaw, DataUpdate, ProviderStorageOptions} from '../types';
import {DEFAULTS} from '../config';
import Serializer from '../utils/serializer';
import ProviderMemory from './memory';

@@ -33,4 +33,4 @@

const dataRaw = this.storage.getItem ( this.id ) || DEFAULTS.dataRaw,
data = Serializer.deserialize ( dataRaw );
const dataRaw = this.storage.getItem ( this.id ) ?? this.defaultsRaw,
data = this.dataParser.parse ( dataRaw ) ?? cloneDeep ( this.defaults );

@@ -37,0 +37,0 @@ return {data, dataRaw};

@@ -18,3 +18,3 @@

type Data = ValueObject;
type Data = ValueArray | ValueObject;
type DataRaw = string;

@@ -26,2 +26,7 @@ type DataUpdate = {

type DataParser = {
parse: ( raw: DataRaw ) => Data | undefined,
stringify: ( data: Data ) => DataRaw | undefined
};
type Value = ValuePrimitive | ValueArray | ValueObject;

@@ -64,3 +69,6 @@ type ValuePrimitive = null | boolean | number | string;

scope: string,
indentation: string | number
defaults?: Data,
defaultsRaw?: DataRaw,
indentation?: string | number,
parser?: DataParser
};

@@ -84,2 +92,2 @@

export {Scope, ScopeAll, Scopes, Path, Data, DataRaw, DataUpdate, ExtendData, Value, ValueObject, Schema, ChangeHandler, ChangeHandlerData, Disposer, Options, Provider, ProviderChangeHandler, ProviderAbstractOptions, ProviderFileOptions, ProviderJSONOptions, ProviderMemoryOptions, ProviderStorageOptions};
export {Scope, ScopeAll, Scopes, Path, Data, DataRaw, DataUpdate, DataParser, ExtendData, Value, ValueArray, ValueObject, Schema, ChangeHandler, ChangeHandlerData, Disposer, Options, Provider, ProviderChangeHandler, ProviderAbstractOptions, ProviderFileOptions, ProviderJSONOptions, ProviderMemoryOptions, ProviderStorageOptions};

@@ -28,4 +28,4 @@

const watcher = require ( 'chokidar-watcher' ); // Lazy import for performance
const change = () => callback ();
return watcher ( filePath, {}, {change} );
const handler = () => callback ();
return watcher ( filePath, {}, handler );
}

@@ -32,0 +32,0 @@ };

@@ -10,3 +10,3 @@

ensure ( folderPath: string ): Promise<void> {
return fs.promises.mkdir ( folderPath, { recursive: true } ).catch ( () => {} );
return fs.promises.mkdir ( folderPath, { recursive: true } ).then ( () => {}, () => {} );
},

@@ -13,0 +13,0 @@ ensureSync ( folderPath: string ): void {

@@ -5,3 +5,3 @@

import * as isPrimitive from 'is-primitive';
import {ValueObject} from '../types';
import {ValueArray, ValueObject} from '../types';

@@ -18,3 +18,3 @@ /* TYPE */

isObject ( x: any ): x is ValueObject {
isObject ( x: any ): x is ValueArray | ValueObject {

@@ -21,0 +21,0 @@ return !isPrimitive ( x );

@@ -6,3 +6,3 @@

{default: ProviderMemory} = require ( '../dist/providers/memory' ),
Fixtures = require ( '../test/fixtures' ),
{Fixtures} = require ( '../test/fixtures' ),
benchmark = require ( 'benchloop' );

@@ -20,4 +20,4 @@

],
defaults: Fixtures.defaults,
schema: Fixtures.schema,
defaults: Fixtures.defaults (),
schema: Fixtures.schema (),
validator

@@ -62,4 +62,4 @@ });

],
defaults: Fixtures.defaults,
schema: Fixtures.schema
defaults: Fixtures.defaults (),
schema: Fixtures.schema ()
});

@@ -73,3 +73,3 @@ }

fn: ctx => {
ctx.conf = new Configuration ( Fixtures.options );
ctx.conf = new Configuration ( Fixtures.options () );
}

@@ -76,0 +76,0 @@ });

@@ -10,26 +10,26 @@

const Fixtures = {
get options () {
options ( providerOptions = {} ) {
const local = new ProviderJSON ({
const local = new ProviderJSON ( Object.assign ({
scope: 'local',
path: tempy.file ({ extension: 'json' })
});
}, providerOptions ));
local.writeSync ( Fixtures.local.data );
local.writeSync ( Fixtures.local ().data );
const global = new ProviderJSON ({
const global = new ProviderJSON ( Object.assign ({
scope: 'global',
path: tempy.file ({ extension: 'json' })
});
}, providerOptions ));
global.writeSync ( Fixtures.global.data );
global.writeSync ( Fixtures.global ().data );
return {
providers: [local, global],
defaults: Fixtures.defaults,
schema: Fixtures.schema
defaults: Fixtures.defaults (),
schema: Fixtures.schema ()
};
},
get defaults () {
defaults () {
return {

@@ -49,3 +49,3 @@ core: {

},
get schema () {
schema () {
return {

@@ -83,3 +83,3 @@ type: 'object',

},
get local () {
local () {
return {

@@ -94,3 +94,3 @@ data: {

},
get global () {
global () {
return {

@@ -107,4 +107,76 @@ data: {

const FixturesArray = {
options ( providerOptions = {} ) {
const local = new ProviderJSON ( Object.assign ({
scope: 'local',
path: tempy.file ({ extension: 'json' }),
defaults: [],
defaultsRaw: '[]',
}, providerOptions ));
local.writeSync ( FixturesArray.local ().data );
const global = new ProviderJSON ( Object.assign ({
scope: 'global',
path: tempy.file ({ extension: 'json' }),
defaults: [],
defaultsRaw: '[]'
}, providerOptions ));
global.writeSync ( FixturesArray.global ().data );
return {
providers: [local, global],
defaults: FixturesArray.defaults (),
schema: FixturesArray.schema ()
};
},
defaults () {
return [
{ foo: 'defaults' },
{ foo: 'defaults2' }
];
},
schema () {
return {
type: 'array',
items: {
type: 'object',
required: ['foo'],
properties: {
foo: {
type: 'string'
},
arr: {
type: 'array',
items: {
type: 'number'
}
}
}
}
};
},
local () {
return {
data: [
{ foo: 'local' },
{ foo: 'local', arr: ['1', '2', '3'] }
]
};
},
global () {
return {
data: [
{ foo: 'global', arr: [1, 2, 3] },
{ test: {} }
]
};
}
};
/* EXPORT */
module.exports = Fixtures;
module.exports = {Fixtures, FixturesArray};

@@ -12,3 +12,3 @@

import ProviderMemory from '../dist/providers/memory';
import Fixtures from './fixtures';
import {Fixtures, FixturesArray} from './fixtures';

@@ -18,2 +18,3 @@ /* CONFIGURATION */

//TODO: Add some tests for the other providers
//TODO: Add more array-based tests, and improve existing ones

@@ -26,3 +27,3 @@ describe ( 'Configuration', () => {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -39,3 +40,3 @@ t.is ( conf.scope, 'global' );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -46,2 +47,28 @@ t.true ( !!conf.scopes.defaults );

it ( 'supports a custom parser', t => {
const conf = new Configuration ( Fixtures.options ({ parser: JSON }) );
t.is ( conf.scopes.global.dataRaw, JSON.stringify ( conf.scopes.global.data ) );
});
it ( 'supports custom defaults', t => {
const conf = new Configuration ( Fixtures.options ({ defaults: { core: { bar: 'custom' } }, defaultsRaw: '{ // Custom }' }) );
t.is ( conf.get ( 'global', 'core.bar' ), 'global' );
conf.scopes.global.writeSync ( '{' ); // Unparseable data raw, forcing the use of defaults
t.is ( conf.get ( 'global', 'core.bar' ), 'custom' );
t.is ( conf.scopes.global.dataRaw, '{' );
conf.scopes.global.writeSync ({ toJSON: () => { throw new Error ( 'Unstringifiable') } }); // Unstringifiable data, forcing the use of defaults
t.is ( conf.get ( 'global', 'core.bar' ), undefined );
t.is ( conf.scopes.global.dataRaw, '{ // Custom }' );
});
it ( 'throws if no providers are passed', t => {

@@ -79,3 +106,3 @@

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options ({ watch: true }) );

@@ -85,3 +112,3 @@ conf.dispose ();

conf.providers.forEach ( provider => {
t.is ( provider.listener, undefined );
t.is ( provider.watcher, undefined );
});

@@ -97,3 +124,3 @@

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -133,3 +160,3 @@ conf.extend ( 'ext.test', {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -170,3 +197,3 @@ const disposer = conf.extend ( 'ext.test', {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -198,3 +225,3 @@ conf.extend ( 'flattened', {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -209,3 +236,3 @@ t.throws ( () => {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -220,3 +247,3 @@ t.throws ( () => {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -231,3 +258,3 @@ t.throws ( () => {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -247,3 +274,3 @@ t.throws ( () => {

const conf = new Configuration ( Fixtures.options ),
const conf = new Configuration ( Fixtures.options () ),
dataPrev = _.cloneDeep ( conf.get () );

@@ -262,5 +289,21 @@

it ( 'supports arrays', t => {
const conf = new Configuration ( FixturesArray.options () );
const dataExpected = [
{ foo: 'defaults' },
{ foo: 'defaults2' },
{ foo: 'global', arr: [1, 2, 3] },
{ foo: 'local' },
{ foo: 'local', arr: undefined } //FIXME: `arr` here should not exist at all
];
t.true ( _.isEqual ( conf.get (), dataExpected ) );
});
it ( 'doesn\'t mutate each provider data', t => {
const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -291,3 +334,3 @@ conf.scopes.global.dataSchema.core.test = true;

const conf = new Configuration ({ ...Fixtures.options, schema: false });
const conf = new Configuration ({ ...Fixtures.options (), schema: false });

@@ -300,3 +343,3 @@ t.is ( conf.validator, undefined );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -309,3 +352,3 @@ t.is ( conf.get ( 'extra' ), undefined );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -318,3 +361,3 @@ t.is ( conf.get ( 'core.test' ), undefined );

const conf = new Configuration ( Fixtures.options ),
const conf = new Configuration ( Fixtures.options () ),
data = { extra: 'extra' };

@@ -336,3 +379,3 @@

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -345,3 +388,3 @@ t.is ( conf.get (), conf.dataSchema );

const conf = new Configuration ( Fixtures.options ),
const conf = new Configuration ( Fixtures.options () ),
datas = conf.get ( '*' );

@@ -357,3 +400,3 @@

const conf = new Configuration ( Fixtures.options ),
const conf = new Configuration ( Fixtures.options () ),
datas = conf.get ( '*', 'core.baz' );

@@ -369,3 +412,3 @@

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -380,3 +423,3 @@ t.is ( conf.get ( 'defaults', 'core.baz' ), 'defaults' );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -393,3 +436,3 @@ t.is ( conf.get ( 'core.foo' ), 'local' );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -402,3 +445,3 @@ t.is ( conf.get ( 'core.flattened' ), true );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -417,3 +460,3 @@ t.throws ( () => {

const conf = new Configuration ( Fixtures.options ),
const conf = new Configuration ( Fixtures.options () ),
datas = conf.has ( '*', 'core.baz' );

@@ -429,3 +472,3 @@

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -440,3 +483,3 @@ t.true ( conf.has ( 'defaults', 'core.baz' ) );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -453,3 +496,3 @@ t.true ( conf.has ( 'core.foo' ) );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -464,3 +507,3 @@ conf.dataSchema.none = undefined;

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -473,3 +516,3 @@ t.true ( conf.has ( 'core.flattened' ) );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -488,3 +531,3 @@ t.throws ( () => {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -503,3 +546,3 @@ conf.set ( '*', 'core.test', 123 );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -520,3 +563,3 @@ conf.set ( 'global', 'core.test', 0 );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -533,3 +576,3 @@ conf.set ( 'core.test', 0 );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -558,3 +601,3 @@ conf.set ( 'core.foo', 'test' );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -569,3 +612,3 @@ t.throws ( () => {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -584,3 +627,3 @@ t.throws ( () => {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -599,3 +642,3 @@ conf.remove ( '*', 'core.bar' );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -616,3 +659,3 @@ conf.remove ( 'global', 'core.foo' );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -641,3 +684,3 @@ conf.remove ( 'core.foo' );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -652,3 +695,3 @@ t.throws ( () => {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -667,3 +710,3 @@ t.throws ( () => {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -682,3 +725,3 @@ conf.update ( '*', {} );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -693,3 +736,3 @@ conf.update ( 'global', {} );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -704,3 +747,3 @@ conf.update ( {} );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -715,3 +758,3 @@ conf.update ( '{}' );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -726,3 +769,3 @@ conf.update ({});

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -742,3 +785,3 @@ conf.update ( 'local', {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -753,3 +796,3 @@ t.throws ( () => {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -768,3 +811,3 @@ t.throws ( () => {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -783,3 +826,3 @@ conf.reset ();

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -798,3 +841,3 @@ conf.reset ( '*' );

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -811,5 +854,27 @@ conf.reset ( 'global' );

it ( 'supports arrays', t => {
const conf = new Configuration ( FixturesArray.options () );
conf.reset ( 'global' );
const datas = conf.get ( '*' );
t.true ( _.isEqual ( datas.defaults, [{ foo: 'defaults' }, { foo: 'defaults2' }] ) );
t.true ( _.isEqual ( datas.local, [{ foo: 'local' }, { foo: 'local', arr: undefined }] ) ); //FIXME: `arr` here should not exist at all
t.true ( _.isEqual ( datas.global, [] ) );
conf.reset ();
const datas2 = conf.get ( '*' );
t.true ( _.isEqual ( datas2.defaults, [{ foo: 'defaults' }, { foo: 'defaults2' }] ) );
t.true ( _.isEqual ( datas2.local, [] ) );
t.true ( _.isEqual ( datas2.global, [] ) );
});
it ( 'throws when trying to change defaults', t => {
const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -824,3 +889,3 @@ t.throws ( () => {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -841,3 +906,3 @@ t.throws ( () => {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -873,3 +938,3 @@ conf.onChange ( 'global', 'core.foo', ( curr, prev ) => {

const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options () );

@@ -901,4 +966,4 @@ const disposer = conf.onChange ( 'core.foo', t.fail );

providers: [foo],
defaults: Fixtures.defaults,
schema: Fixtures.schema
defaults: Fixtures.defaults (),
schema: Fixtures.schema ()
};

@@ -913,3 +978,3 @@

foo.writeSync ( Fixtures.local.data );
foo.writeSync ( Fixtures.local ().data );

@@ -955,8 +1020,8 @@ t.is ( conf.get ( 'core.foo' ), 'local' );

foo.writeSync ( Fixtures.local.data );
foo.writeSync ( Fixtures.local ().data );
const options = {
providers: [foo],
defaults: Fixtures.defaults,
schema: Fixtures.schema
defaults: Fixtures.defaults (),
schema: Fixtures.schema ()
};

@@ -971,8 +1036,10 @@

it.skip ( 'detects when a file gets updated', async t => { //FIXME: Not watching properly //FIXME: It blows up the heap for some reason
it ( 'detects when a file gets updated', async t => {
const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options ({ watch: true }) );
await new Promise ( resolve => conf.scopes.global.listener.on ( 'ready', resolve ) );
// await new Promise ( resolve => conf.scopes.global.watcher.on ( 'ready', resolve ) ); //FIXME: Not working for some reason
await delay ( 1000 );
fs.writeFileSync ( conf.scopes.global.path, JSON.stringify ({

@@ -985,18 +1052,20 @@ core: {

await delay ( 500 );
await delay ( 1500 );
t.is ( conf.get ( 'core.bar' ), 'custom' );
t.is ( conf.get ( 'core.test' ), 'custom' );
t.is ( conf.get ( 'core.test' ), undefined );
});
it.skip ( 'handles invalid data', async t => { //FIXME: Not watching properly //FIXME: It blows up the heap for some reason
it ( 'handles invalid data', async t => {
const conf = new Configuration ( Fixtures.options );
const conf = new Configuration ( Fixtures.options ({ watch: true }) );
await new Promise ( resolve => conf.scopes.global.listener.on ( 'ready', resolve ) );
// await new Promise ( resolve => conf.scopes.global.watcher.on ( 'ready', resolve ) ); //FIXME: Not working for some reason
await delay ( 1000 );
fs.writeFileSync ( conf.scopes.global.path, '{' );
await delay ( 500 );
await delay ( 1500 );

@@ -1008,4 +1077,47 @@ t.is ( conf.get ( 'core.bar' ), 'defaults' );

it ( 'preserves empty contents', async t => {
const conf = new Configuration ( Fixtures.options ({ watch: true }) );
// await new Promise ( resolve => conf.scopes.global.watcher.on ( 'ready', resolve ) ); //FIXME: Not working for some reason
await delay ( 1000 );
fs.writeFileSync ( conf.scopes.global.path, '' );
await delay ( 1500 );
t.is ( conf.get ( 'core.bar' ), 'defaults' );
t.is ( conf.get ( 'core.test' ), undefined );
t.is ( conf.scopes.global.dataRaw, '' );
});
it ( 'preserves the formatting in the new string', async t => {
const conf = new Configuration ( Fixtures.options ({ watch: true }) );
// await new Promise ( resolve => conf.scopes.global.watcher.on ( 'ready', resolve ) ); //FIXME: Not working for some reason
await delay ( 1000 );
const dataNext = JSON.stringify ({
core: {
bar: 'custom',
test: 'custom'
}
});
fs.writeFileSync ( conf.scopes.global.path, dataNext );
await delay ( 1500 );
t.is ( conf.get ( 'core.bar' ), 'custom' );
t.is ( conf.get ( 'core.test' ), undefined );
t.is ( conf.scopes.global.dataRaw, dataNext );
});
});
});
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