Skip to content

Commit

Permalink
Require Node.js 18
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 24, 2023
1 parent ddcfa1d commit aa12658
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 35 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ jobs:
matrix:
node-version:
- 18
- 16
- 14
os:
- ubuntu-latest
- macos-latest
Expand Down
36 changes: 18 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"default": "./dist/source/index.js"
},
"engines": {
"node": ">=14.16"
"node": ">=18"
},
"sideEffects": false,
"scripts": {
"test": "xo && npm run build && ava",
"build": "del-cli dist && tsc",
Expand Down Expand Up @@ -48,30 +49,29 @@
"dependencies": {
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1",
"atomically": "^2.0.0",
"atomically": "^2.0.2",
"debounce-fn": "^5.1.2",
"dot-prop": "^7.2.0",
"dot-prop": "^8.0.2",
"env-paths": "^3.0.0",
"json-schema-typed": "^8.0.1",
"semver": "^7.3.8",
"uint8array-extras": "^0.2.0"
"semver": "^7.5.4",
"uint8array-extras": "^0.3.0"
},
"devDependencies": {
"@sindresorhus/tsconfig": "^3.0.1",
"@types/node": "^18.11.18",
"@types/semver": "^7.3.13",
"ava": "^5.1.0",
"del": "^7.0.0",
"del-cli": "^5.0.0",
"delay": "^5.0.0",
"p-event": "^5.0.1",
"tempy": "^3.0.0",
"@sindresorhus/tsconfig": "^5.0.0",
"@types/node": "^20.8.8",
"@types/semver": "^7.5.4",
"ava": "^5.3.1",
"del": "^7.1.0",
"del-cli": "^5.1.0",
"delay": "^6.0.0",
"p-event": "^6.0.0",
"tempy": "^3.1.0",
"ts-node": "^10.9.1",
"tsd": "^0.28.1",
"typescript": "^5.1.6",
"xo": "^0.54.0"
"tsd": "^0.29.0",
"typescript": "^5.2.2",
"xo": "^0.56.0"
},
"sideEffects": false,
"ava": {
"files": [
"test/*",
Expand Down
23 changes: 12 additions & 11 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* eslint-disable @typescript-eslint/naming-convention, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-redundant-type-constituents */
/* eslint-disable @typescript-eslint/naming-convention, @typescript-eslint/no-unsafe-return */
import {isDeepStrictEqual} from 'node:util';
import process from 'node:process';
import fs from 'node:fs';
import path from 'node:path';
import crypto from 'node:crypto';
import assert from 'node:assert';
import {EventEmitter} from 'node:events';
import {getProperty, hasProperty, setProperty, deleteProperty} from 'dot-prop';
import envPaths from 'env-paths';
import {writeFileSync as atomicWriteFileSync} from 'atomically';
Expand All @@ -16,7 +15,6 @@ import semver from 'semver';
import {type JSONSchema} from 'json-schema-typed';
import {
concatUint8Arrays,
isUint8Array,
stringToUint8Array,
uint8ArrayToString,
} from 'uint8array-extras';
Expand Down Expand Up @@ -60,7 +58,7 @@ const MIGRATION_KEY = `${INTERNAL_KEY}.migrations.version`;

export default class Conf<T extends Record<string, any> = Record<string, unknown>> implements Iterable<[keyof T, T[keyof T]]> {
readonly path: string;
readonly events: EventEmitter;
readonly events: EventTarget;
readonly #validator?: AjvValidateFunction;
readonly #encryptionKey?: string | Uint8Array | NodeJS.TypedArray | DataView;
readonly #options: Readonly<Partial<Options<T>>>;
Expand Down Expand Up @@ -127,7 +125,7 @@ export default class Conf<T extends Record<string, any> = Record<string, unknown
this._deserialize = options.deserialize;
}

this.events = new EventEmitter();
this.events = new EventTarget();
this.#encryptionKey = options.encryptionKey;

const fileExtension = options.fileExtension ? `.${options.fileExtension}` : '';
Expand Down Expand Up @@ -349,7 +347,7 @@ export default class Conf<T extends Record<string, any> = Record<string, unknown
this._validate(value);
this._write(value);

this.events.emit('change');
this.events.dispatchEvent(new Event('change'));
}

* [Symbol.iterator](): IterableIterator<[keyof T, T[keyof T]]> {
Expand All @@ -369,7 +367,7 @@ export default class Conf<T extends Record<string, any> = Record<string, unknown
const password = crypto.pbkdf2Sync(this.#encryptionKey, initializationVector.toString(), 10_000, 32, 'sha512');
const decipher = crypto.createDecipheriv(encryptionAlgorithm, password, initializationVector);
const slice = data.slice(17);
const dataUpdate = isUint8Array(slice) ? slice : stringToUint8Array(slice);
const dataUpdate = typeof slice === 'string' ? stringToUint8Array(slice) : slice;
return uint8ArrayToString(concatUint8Arrays([decipher.update(dataUpdate), decipher.final()]));
} catch {}

Expand Down Expand Up @@ -404,8 +402,11 @@ export default class Conf<T extends Record<string, any> = Record<string, unknown
callback.call(this, newValue, oldValue);
};

this.events.on('change', onChange);
return () => this.events.removeListener('change', onChange);
this.events.addEventListener('change', onChange);

return () => {
this.events.removeEventListener('change', onChange);
};
}

private readonly _deserialize: Deserialize<T> = value => JSON.parse(value);
Expand Down Expand Up @@ -472,11 +473,11 @@ export default class Conf<T extends Record<string, any> = Record<string, unknown
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');
this.events.dispatchEvent(new Event('change'));
}, {wait: 100}));
} else {
fs.watchFile(this.path, {persistent: false}, debounceFn(() => {
this.events.emit('change');
this.events.dispatchEvent(new Event('change'));
}, {wait: 5000}));
}
}
Expand Down
3 changes: 1 addition & 2 deletions source/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {type EventEmitter} from 'node:events';
import {type JSONSchema as TypedJSONSchema} from 'json-schema-typed';
// eslint-disable unicorn/import-index
import type Conf from './index.js';
Expand Down Expand Up @@ -259,4 +258,4 @@ export type Deserialize<T> = (text: string) => T;
export type OnDidChangeCallback<T> = (newValue?: T, oldValue?: T) => void;
export type OnDidAnyChangeCallback<T> = (newValue?: Readonly<T>, oldValue?: Readonly<T>) => void;

export type Unsubscribe = () => EventEmitter;
export type Unsubscribe = () => void;
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"extends": "@sindresorhus/tsconfig",
"compilerOptions": {
"outDir": "dist",
"target": "es2020", // Node.js 14
"target": "es2022", // Node.js 18
"lib": [
"es2020"
"es2022"
],
"noPropertyAccessFromIndexSignature": false,
"isolatedModules": true
Expand Down

0 comments on commit aa12658

Please sign in to comment.