-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
index.ts
610 lines (491 loc) · 17.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
import {isDeepStrictEqual} from 'util';
import fs = require('fs');
import path = require('path');
import crypto = require('crypto');
import assert = require('assert');
import {EventEmitter} from 'events';
import dotProp = require('dot-prop');
import pkgUp = require('pkg-up');
import envPaths = require('env-paths');
import atomically = require('atomically');
import Ajv, {ValidateFunction as AjvValidateFunction} from 'ajv';
import ajvFormats from 'ajv-formats';
import debounceFn = require('debounce-fn');
import semver = require('semver');
import onetime = require('onetime');
import {JSONSchema} from 'json-schema-typed';
import {Deserialize, Migrations, OnDidChangeCallback, Options, Serialize, Unsubscribe, Schema, OnDidAnyChangeCallback, BeforeEachMigrationCallback} from './types';
const encryptionAlgorithm = 'aes-256-cbc';
const createPlainObject = <T = Record<string, unknown>>(): T => {
return Object.create(null);
};
const isExist = <T = unknown>(data: T): boolean => {
return data !== undefined && data !== null;
};
let parentDir = '';
try {
// Prevent caching of this module so module.parent is always accurate.
// Note: This trick won't work with ESM or inside a webworker
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete require.cache[__filename];
parentDir = path.dirname(module.parent?.filename ?? '.');
} catch {}
const checkValueType = (key: string, value: unknown): void => {
const nonJsonTypes = new Set([
'undefined',
'symbol',
'function'
]);
const type = typeof value;
if (nonJsonTypes.has(type)) {
throw new TypeError(`Setting a value of type \`${type}\` for key \`${key}\` is not allowed as it's not supported by JSON`);
}
};
const INTERNAL_KEY = '__internal__';
const MIGRATION_KEY = `${INTERNAL_KEY}.migrations.version`;
class Conf<T extends Record<string, any> = Record<string, unknown>> implements Iterable<[keyof T, T[keyof T]]> {
readonly path: string;
readonly events: EventEmitter;
readonly #validator?: AjvValidateFunction;
readonly #encryptionKey?: string | Buffer | NodeJS.TypedArray | DataView;
readonly #options: Readonly<Partial<Options<T>>>;
readonly #defaultValues: Partial<T> = {};
constructor(partialOptions: Readonly<Partial<Options<T>>> = {}) {
const options: Partial<Options<T>> = {
configName: 'config',
fileExtension: 'json',
projectSuffix: 'nodejs',
clearInvalidConfig: false,
accessPropertiesByDotNotation: true,
configFileMode: 0o666,
...partialOptions
};
const getPackageData = onetime(() => {
const packagePath = pkgUp.sync({cwd: parentDir});
// Can't use `require` because of Webpack being annoying:
// https://github.com/webpack/webpack/issues/196
const packageData = packagePath && JSON.parse(fs.readFileSync(packagePath, 'utf8'));
return packageData ?? {};
});
if (!options.cwd) {
if (!options.projectName) {
options.projectName = getPackageData().name;
}
if (!options.projectName) {
throw new Error('Project name could not be inferred. Please specify the `projectName` option.');
}
options.cwd = envPaths(options.projectName, {suffix: options.projectSuffix}).config;
}
this.#options = options;
if (options.schema) {
if (typeof options.schema !== 'object') {
throw new TypeError('The `schema` option must be an object.');
}
const ajv = new Ajv({
allErrors: true,
useDefaults: true
});
ajvFormats(ajv);
const schema: JSONSchema = {
type: 'object',
properties: options.schema
};
this.#validator = ajv.compile(schema);
for (const [key, value] of Object.entries<JSONSchema>(options.schema)) {
if (value?.default) {
this.#defaultValues[key as keyof T] = value.default;
}
}
}
if (options.defaults) {
this.#defaultValues = {
...this.#defaultValues,
...options.defaults
};
}
if (options.serialize) {
this._serialize = options.serialize;
}
if (options.deserialize) {
this._deserialize = options.deserialize;
}
this.events = new EventEmitter();
this.#encryptionKey = options.encryptionKey;
const fileExtension = options.fileExtension ? `.${options.fileExtension}` : '';
this.path = path.resolve(options.cwd, `${options.configName ?? 'config'}${fileExtension}`);
const fileStore = this.store;
const store = Object.assign(createPlainObject(), options.defaults, fileStore);
this._validate(store);
try {
assert.deepEqual(fileStore, store);
} catch {
this.store = store;
}
if (options.watch) {
this._watch();
}
if (options.migrations) {
if (!options.projectVersion) {
options.projectVersion = getPackageData().version;
}
if (!options.projectVersion) {
throw new Error('Project version could not be inferred. Please specify the `projectVersion` option.');
}
this._migrate(options.migrations, options.projectVersion, options.beforeEachMigration);
}
}
/**
Get an item.
@param key - The key of the item to get.
@param defaultValue - The default value if the item does not exist.
*/
get<Key extends keyof T>(key: Key): T[Key];
get<Key extends keyof T>(key: Key, defaultValue: Required<T>[Key]): Required<T>[Key];
// This overload is used for dot-notation access.
// We exclude `keyof T` as an incorrect type for the default value should not fall through to this overload.
get<Key extends string, Value = unknown>(key: Exclude<Key, keyof T>, defaultValue?: Value): Value;
get(key: string, defaultValue?: unknown): unknown {
if (this.#options.accessPropertiesByDotNotation) {
return this._get(key, defaultValue);
}
const {store} = this;
return key in store ? store[key] : defaultValue;
}
/**
Set an item or multiple items at once.
@param {key|object} - You can use [dot-notation](https://github.com/sindresorhus/dot-prop) in a key to access nested properties. Or a hashmap of items to set at once.
@param value - Must be JSON serializable. Trying to set the type `undefined`, `function`, or `symbol` will result in a `TypeError`.
*/
set<Key extends keyof T>(key: Key, value?: T[Key]): void;
set(key: string, value: unknown): void;
set(object: Partial<T>): void;
set<Key extends keyof T>(key: Partial<T> | Key | string, value?: T[Key] | unknown): void {
if (typeof key !== 'string' && typeof key !== 'object') {
throw new TypeError(`Expected \`key\` to be of type \`string\` or \`object\`, got ${typeof key}`);
}
if (typeof key !== 'object' && value === undefined) {
throw new TypeError('Use `delete()` to clear values');
}
if (this._containsReservedKey(key)) {
throw new TypeError(`Please don't use the ${INTERNAL_KEY} key, as it's used to manage this module internal operations.`);
}
const {store} = this;
const set = (key: string, value?: T[Key] | T | unknown): void => {
checkValueType(key, value);
if (this.#options.accessPropertiesByDotNotation) {
dotProp.set(store, key, value);
} else {
store[key as Key] = value as T[Key];
}
};
if (typeof key === 'object') {
const object = key;
for (const [key, value] of Object.entries(object)) {
set(key, value);
}
} else {
set(key, value);
}
this.store = store;
}
/**
Check if an item exists.
@param key - The key of the item to check.
*/
has<Key extends keyof T>(key: Key | string): boolean {
if (this.#options.accessPropertiesByDotNotation) {
return dotProp.has(this.store, key as string);
}
return (key as string) in this.store;
}
/**
Reset items to their default values, as defined by the `defaults` or `schema` option.
@see `clear()` to reset all items.
@param keys - The keys of the items to reset.
*/
reset<Key extends keyof T>(...keys: Key[]): void {
for (const key of keys) {
if (isExist(this.#defaultValues[key])) {
this.set(key, this.#defaultValues[key]);
}
}
}
/**
Delete an item.
@param key - The key of the item to delete.
*/
delete<Key extends keyof T>(key: Key): void {
const {store} = this;
if (this.#options.accessPropertiesByDotNotation) {
dotProp.delete(store, key as string);
} else {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete store[key];
}
this.store = store;
}
/**
Delete all items.
This resets known items to their default values, if defined by the `defaults` or `schema` option.
*/
clear(): void {
this.store = createPlainObject();
for (const key of Object.keys(this.#defaultValues)) {
this.reset(key);
}
}
/**
Watches the given `key`, calling `callback` on any changes.
@param key - The key wo watch.
@param callback - A callback function that is called on any changes. When a `key` is first set `oldValue` will be `undefined`, and when a key is deleted `newValue` will be `undefined`.
@returns A function, that when called, will unsubscribe.
*/
onDidChange<Key extends keyof T>(
key: Key,
callback: OnDidChangeCallback<T[Key]>
): Unsubscribe {
if (typeof key !== 'string') {
throw new TypeError(`Expected \`key\` to be of type \`string\`, got ${typeof key}`);
}
if (typeof callback !== 'function') {
throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof callback}`);
}
return this._handleChange(() => this.get(key), callback);
}
/**
Watches the whole config object, calling `callback` on any changes.
@param callback - A callback function that is called on any changes. When a `key` is first set `oldValue` will be `undefined`, and when a key is deleted `newValue` will be `undefined`.
@returns A function, that when called, will unsubscribe.
*/
onDidAnyChange(
callback: OnDidAnyChangeCallback<T>
): Unsubscribe {
if (typeof callback !== 'function') {
throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof callback}`);
}
return this._handleChange(() => this.store, callback);
}
get size(): number {
return Object.keys(this.store).length;
}
get store(): T {
try {
const data = fs.readFileSync(this.path, this.#encryptionKey ? null : 'utf8');
const dataString = this._encryptData(data);
const deserializedData = this._deserialize(dataString);
this._validate(deserializedData);
return Object.assign(createPlainObject(), deserializedData);
} catch (error: any) {
if (error?.code === 'ENOENT') {
this._ensureDirectory();
return createPlainObject();
}
if (this.#options.clearInvalidConfig && error.name === 'SyntaxError') {
return createPlainObject();
}
throw error;
}
}
set store(value: T) {
this._ensureDirectory();
this._validate(value);
this._write(value);
this.events.emit('change');
}
* [Symbol.iterator](): IterableIterator<[keyof T, T[keyof T]]> {
for (const [key, value] of Object.entries(this.store)) {
yield [key, value];
}
}
private _encryptData(data: string | Buffer): string {
if (!this.#encryptionKey) {
return data.toString();
}
try {
// Check if an initialization vector has been used to encrypt the data
if (this.#encryptionKey) {
try {
if (data.slice(16, 17).toString() === ':') {
const initializationVector = data.slice(0, 16);
const password = crypto.pbkdf2Sync(this.#encryptionKey, initializationVector.toString(), 10000, 32, 'sha512');
const decipher = crypto.createDecipheriv(encryptionAlgorithm, password, initializationVector);
data = Buffer.concat([decipher.update(Buffer.from(data.slice(17))), decipher.final()]).toString('utf8');
} else {
// TODO: Remove this in the next major version.
const decipher = crypto.createDecipher(encryptionAlgorithm, this.#encryptionKey);
data = Buffer.concat([decipher.update(Buffer.from(data)), decipher.final()]).toString('utf8');
}
} catch {}
}
} catch {}
return data.toString();
}
private _handleChange<Key extends keyof T>(
getter: () => T | undefined,
callback: OnDidAnyChangeCallback<T[Key]>
): Unsubscribe;
private _handleChange<Key extends keyof T>(
getter: () => T[Key] | undefined,
callback: OnDidChangeCallback<T[Key]>
): Unsubscribe;
private _handleChange<Key extends keyof T>(
getter: () => T | T[Key] | undefined,
callback: OnDidAnyChangeCallback<T | T[Key]> | OnDidChangeCallback<T | T[Key]>
): Unsubscribe {
let currentValue = getter();
const onChange = (): void => {
const oldValue = currentValue;
const newValue = getter();
if (isDeepStrictEqual(newValue, oldValue)) {
return;
}
currentValue = newValue;
callback.call(this, newValue, oldValue);
};
this.events.on('change', onChange);
return () => this.events.removeListener('change', onChange);
}
private readonly _deserialize: Deserialize<T> = value => JSON.parse(value);
private readonly _serialize: Serialize<T> = value => JSON.stringify(value, undefined, '\t');
private _validate(data: T | unknown): void {
if (!this.#validator) {
return;
}
const valid = this.#validator(data);
if (valid || !this.#validator.errors) {
return;
}
const errors = this.#validator.errors
.map(({instancePath, message = ''}) => `\`${instancePath.slice(1)}\` ${message}`);
throw new Error('Config schema violation: ' + errors.join('; '));
}
private _ensureDirectory(): void {
// Ensure the directory exists as it could have been deleted in the meantime.
fs.mkdirSync(path.dirname(this.path), {recursive: true});
}
private _write(value: T): void {
let data: string | Buffer = this._serialize(value);
if (this.#encryptionKey) {
const initializationVector = crypto.randomBytes(16);
const password = crypto.pbkdf2Sync(this.#encryptionKey, initializationVector.toString(), 10000, 32, 'sha512');
const cipher = crypto.createCipheriv(encryptionAlgorithm, password, initializationVector);
data = Buffer.concat([initializationVector, Buffer.from(':'), cipher.update(Buffer.from(data)), cipher.final()]);
}
// Temporary workaround for Conf being packaged in a Ubuntu Snap app.
// See https://github.com/sindresorhus/conf/pull/82
if (process.env.SNAP) {
fs.writeFileSync(this.path, data, {mode: this.#options.configFileMode});
} else {
try {
atomically.writeFileSync(this.path, data, {mode: this.#options.configFileMode});
} catch (error: any) {
// Fix for https://github.com/sindresorhus/electron-store/issues/106
// Sometimes on Windows, we will get an EXDEV error when atomic writing
// (even though to the same directory), so we fall back to non atomic write
if (error?.code === 'EXDEV') {
fs.writeFileSync(this.path, data, {mode: this.#options.configFileMode});
return;
}
throw error;
}
}
}
private _watch(): void {
this._ensureDirectory();
if (!fs.existsSync(this.path)) {
this._write(createPlainObject<T>());
}
if (process.platform === 'win32') {
fs.watch(this.path, {persistent: false}, debounceFn(() => {
// On Linux and Windows, writing to the config file emits a `rename` event, so we skip checking the event type.
this.events.emit('change');
}, {wait: 100}));
} else {
fs.watchFile(this.path, {persistent: false}, debounceFn(() => {
this.events.emit('change');
}, {wait: 5000}));
}
}
private _migrate(migrations: Migrations<T>, versionToMigrate: string, beforeEachMigration?: BeforeEachMigrationCallback<T>): void {
let previousMigratedVersion = this._get(MIGRATION_KEY, '0.0.0');
const newerVersions = Object.keys(migrations)
.filter(candidateVersion => this._shouldPerformMigration(candidateVersion, previousMigratedVersion, versionToMigrate));
let storeBackup = {...this.store};
for (const version of newerVersions) {
try {
if (beforeEachMigration) {
beforeEachMigration(this, {
fromVersion: previousMigratedVersion,
toVersion: version,
finalVersion: versionToMigrate,
versions: newerVersions
});
}
const migration = migrations[version];
migration(this);
this._set(MIGRATION_KEY, version);
previousMigratedVersion = version;
storeBackup = {...this.store};
} catch (error) {
this.store = storeBackup;
throw new Error(
`Something went wrong during the migration! Changes applied to the store until this failed migration will be restored. ${error as string}`
);
}
}
if (this._isVersionInRangeFormat(previousMigratedVersion) || !semver.eq(previousMigratedVersion, versionToMigrate)) {
this._set(MIGRATION_KEY, versionToMigrate);
}
}
private _containsReservedKey(key: string | Partial<T>): boolean {
if (typeof key === 'object') {
const firsKey = Object.keys(key)[0];
if (firsKey === INTERNAL_KEY) {
return true;
}
}
if (typeof key !== 'string') {
return false;
}
if (this.#options.accessPropertiesByDotNotation) {
if (key.startsWith(`${INTERNAL_KEY}.`)) {
return true;
}
return false;
}
return false;
}
private _isVersionInRangeFormat(version: string): boolean {
return semver.clean(version) === null;
}
private _shouldPerformMigration(candidateVersion: string, previousMigratedVersion: string, versionToMigrate: string): boolean {
if (this._isVersionInRangeFormat(candidateVersion)) {
if (previousMigratedVersion !== '0.0.0' && semver.satisfies(previousMigratedVersion, candidateVersion)) {
return false;
}
return semver.satisfies(versionToMigrate, candidateVersion);
}
if (semver.lte(candidateVersion, previousMigratedVersion)) {
return false;
}
if (semver.gt(candidateVersion, versionToMigrate)) {
return false;
}
return true;
}
private _get<Key extends keyof T>(key: Key): T[Key] | undefined;
private _get<Key extends keyof T, Default = unknown>(key: Key, defaultValue: Default): T[Key] | Default;
private _get<Key extends keyof T, Default = unknown>(key: Key | string, defaultValue?: Default): Default | undefined {
return dotProp.get<T[Key] | undefined>(this.store, key as string, defaultValue as T[Key]);
}
private _set(key: string, value: unknown): void {
const {store} = this;
dotProp.set(store, key, value);
this.store = store;
}
}
export {Schema, Options};
export default Conf;
// For CommonJS default export support
module.exports = Conf;
module.exports.default = Conf;