Skip to content

Commit

Permalink
Version 1.9.5 with loadJson() helper and crypto null fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
igoramadas committed Nov 28, 2024
1 parent b399265 commit 84f0406
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 42 deletions.
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog for SetMeUp

1.9.5
=====
* Exposed loadJson() helper to load settings directly from a JSON object.
* Fixed issues trying to encrypt / decrypt null values.

1.9.4
=====
* Improved extend() helper to avoid replacing existing properties with null values.
Expand Down
6 changes: 3 additions & 3 deletions docs/assets/main.js

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions docs/assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,20 @@
cursor: pointer;
}

.tsd-full-hierarchy:not(:last-child) {
margin-bottom: 1em;
padding-bottom: 1em;
border-bottom: 1px solid var(--color-accent);
}
.tsd-full-hierarchy,
.tsd-full-hierarchy ul {
list-style: none;
margin: 0;
padding: 0;
}
.tsd-full-hierarchy ul {
padding-left: 1.5rem;
}
.tsd-full-hierarchy a {
padding: 0.25rem 0 !important;
font-size: 1rem;
Expand Down
50 changes: 25 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "setmeup",
"version": "1.9.4",
"version": "1.9.5",
"description": "SetMeUp, your single and only settings manager.",
"keywords": [
"setmeup",
Expand Down Expand Up @@ -40,7 +40,7 @@
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/mocha": "^10.0.10",
"@types/node": "^22.10.0",
"@types/node": "^22.10.1",
"anyhow": "^3.4.0",
"chai": "^4.5.0",
"coveralls": "^3.1.1",
Expand All @@ -49,7 +49,7 @@
"nyc": "^17.1.0",
"source-map-support": "^0.5.21",
"ts-node": "^10.9.2",
"typedoc": "^v0.27.0",
"typedoc": "^v0.27.1",
"typescript": "^5.7.2"
},
"peerDependencies": {
Expand Down
8 changes: 5 additions & 3 deletions src/cryptohelper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SetMeUp: crypto.ts

import {execSync} from "child_process"
import {isArray, isBoolean, isNumber, isString, loadJson} from "./utils"
import {isArray, isBoolean, isNumber, isString, loadFile} from "./utils"
import crypto from "crypto"

/** Default IV value in case one is not provided. */
Expand Down Expand Up @@ -75,7 +75,7 @@ export function cryptoMethod(action: string, filename: string, options?: CryptoO
options.iv = defaultIV
}

const settingsJson = loadJson(filename, false)
const settingsJson = loadFile(filename, false)

// Settings file not found or invalid? Stop here.
if (settingsJson == null) {
Expand All @@ -102,6 +102,8 @@ export function cryptoMethod(action: string, filename: string, options?: CryptoO
// the key based on true / false.
if (isBoolean(currentValue)) {
newValue = currentValue
} else if (currentValue === null) {
newValue = null
} else if (action == "encrypt") {
// Value already encrypted? Skip!
if (isString(currentValue) && currentValue.substring(0, 4) == "enc-") {
Expand All @@ -121,7 +123,7 @@ export function cryptoMethod(action: string, filename: string, options?: CryptoO
newValue = "enc-s:"
}

// Create cipher amd encrypt data.
// Create cipher and encrypt data.
c = crypto.createCipheriv(options.cipher, options.key, options.iv)
newValue += c.update(currentValue.toString(), "utf8", "hex")
newValue += c.final("hex")
Expand Down
37 changes: 33 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// SetMeUp: index.ts

import {cryptoMethod, CryptoOptions} from "./cryptohelper"
import {extend, getFilePath, isString, loadJson} from "./utils"
import {extend, getFilePath, isString, loadFile} from "./utils"
import EventEmitter from "eventemitter3"
import fs from "fs"
import path from "path"

const defaultOptions = {overwrite: true, lowercase: false, rootKey: ""}

/** @hidden */
let rootFolder = process.cwd()
/** @hidden */
Expand Down Expand Up @@ -171,7 +173,7 @@ class SetMeUp {

// Set default options.
if (!options) options = {}
options = Object.assign({overwrite: true, rootKey: ""}, options)
options = Object.assign({...defaultOptions}, options)

// No filenames passed? Load the default ones.
/* istanbul ignore else */
Expand All @@ -195,7 +197,7 @@ class SetMeUp {
const isSecret = (filename && path.basename(filename).toLowerCase() == "settings.secret.json") || null

// When loading, force crypto if file is settings.secret.json.
let settingsJson = loadJson(filename, options.crypto || isSecret)
let settingsJson = loadFile(filename, options.crypto || isSecret)

// File not found?
if (settingsJson == null) {
Expand Down Expand Up @@ -260,6 +262,33 @@ class SetMeUp {
return result
}

/**
* Load settings directly from the passed JSON object.
* @param data JSON data to be loaded to the settings.
* @param options Load options defining if properties should be overwritten, and root settings key.
* @returns Returns the loaded JSON object itself.
* @event loadJson
*/
loadJson = (data: any, options?: LoadOptions): any => {
if (!data) return data

// Set default options.
if (!options) options = {}
options = Object.assign({...defaultOptions}, options)

// Extend loaded settings.
if (options.rootKey) {
extend(data[options.rootKey], this.settings, options.overwrite)
} else {
extend(data, this.settings, options.overwrite)
}

// Emit load passing prefix and loaded settings result.
this.events.emit("loadJson", data)

if (logger) logger.info("SetMeUp.loadJson", `Loaded keys: ${Object.keys(data).join(", ")}`)
}

/**
* Load settings from environment variables, restricting to the passed prefix.
* Environment settings as variables will be split by underscore to define its tree.
Expand All @@ -283,7 +312,7 @@ class SetMeUp {

// Set default options.
if (!options) options = {}
options = Object.assign({overwrite: true, lowercase: false}, options)
options = Object.assign({...defaultOptions}, options)

// Iterate and process relevant variables.
// Each underscore defines a level on the result tree.
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export function parseJson(value: string | any) {
* @returns The parsed JSON object.
* @protected
*/
export function loadJson(filename: string, cryptoOptions?: CryptoOptions | boolean): any {
export function loadFile(filename: string, cryptoOptions?: CryptoOptions | boolean): any {
let result = null

// Try loading the anyhow module.
Expand Down Expand Up @@ -208,7 +208,7 @@ export function extend(source: any, target: any, overwrite: boolean): void {
for (let prop in source) {
const value = source[prop]
if (value && value.constructor === Object) {
if (!(prop in target)) {
if (target[prop] === null || !(prop in target)) {
target[prop] = {}
}
extend(source[prop], target[prop], overwrite)
Expand Down
3 changes: 2 additions & 1 deletion test/settings.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"a",
true
],
"testingFileWatcher": true
"testingFileWatcher": true,
"nullKey": null
}
7 changes: 6 additions & 1 deletion test/test-a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ describe("SetMeUp Main Tests", function () {
date: "01/01/2000"
},
array: [1, -1, "a", true],
testingFileWatcher: true
testingFileWatcher: true,
nullKey: null
}

before(function () {
Expand Down Expand Up @@ -84,6 +85,10 @@ describe("SetMeUp Main Tests", function () {
}
})

it("Replace null with object when loading settings", function () {
setmeup.loadJson({nullKey: {test: {value: 1}}})
})

it("Load in readOnly mode, should not destroy", function (done) {
setmeup.readOnly = true
setmeup.load()
Expand Down

0 comments on commit 84f0406

Please sign in to comment.