mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-21 07:56:16 -04:00
Added env configuration for plugins
* Added env. * Added tree config. * Added tree. * Fixed settings test. * Added test cases.
This commit is contained in:
parent
e61e8ebd9e
commit
bf7cd11b59
3 changed files with 667 additions and 470 deletions
File diff suppressed because it is too large
Load diff
112
src/node/utils/SettingsTree.ts
Normal file
112
src/node/utils/SettingsTree.ts
Normal file
|
@ -0,0 +1,112 @@
|
|||
import {MapArrayType} from "../types/MapType";
|
||||
|
||||
export class SettingsTree {
|
||||
private children: Map<string, SettingsNode>;
|
||||
constructor() {
|
||||
this.children = new Map();
|
||||
}
|
||||
|
||||
public addChild(key: string, value: string) {
|
||||
this.children.set(key, new SettingsNode(key, value));
|
||||
}
|
||||
|
||||
public removeChild(key: string) {
|
||||
this.children.delete(key);
|
||||
}
|
||||
|
||||
public getChild(key: string) {
|
||||
return this.children.get(key);
|
||||
}
|
||||
|
||||
public hasChild(key: string) {
|
||||
return this.children.has(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export class SettingsNode {
|
||||
private readonly key: string;
|
||||
private value: string | number | boolean | null | undefined;
|
||||
private children: MapArrayType<SettingsNode>;
|
||||
|
||||
constructor(key: string, value?: string | number | boolean | null | undefined) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
this.children = {}
|
||||
}
|
||||
|
||||
public addChild(path: string[], value: string) {
|
||||
let currentNode:SettingsNode = this;
|
||||
for (let i = 0; i < path.length; i++) {
|
||||
const key = path[i];
|
||||
/*
|
||||
Skip the current node if the key is the same as the current node's key
|
||||
*/
|
||||
if (key === this.key ) {
|
||||
continue
|
||||
}
|
||||
/*
|
||||
If the current node does not have a child with the key, create a new node with the key
|
||||
*/
|
||||
if (!currentNode.hasChild(key)) {
|
||||
currentNode = currentNode.children[key] = new SettingsNode(key, this.coerceValue(value));
|
||||
} else {
|
||||
/*
|
||||
Else move to the child node
|
||||
*/
|
||||
currentNode = currentNode.getChild(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public collectFromLeafsUpwards() {
|
||||
let collected:MapArrayType<any> = {};
|
||||
for (const key in this.children) {
|
||||
const child = this.children[key];
|
||||
if (child.hasChildren()) {
|
||||
collected[key] = child.collectFromLeafsUpwards();
|
||||
} else {
|
||||
collected[key] = child.value;
|
||||
}
|
||||
}
|
||||
return collected;
|
||||
}
|
||||
|
||||
coerceValue = (stringValue: string) => {
|
||||
// cooked from https://stackoverflow.com/questions/175739/built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number
|
||||
// @ts-ignore
|
||||
const isNumeric = !isNaN(stringValue) && !isNaN(parseFloat(stringValue) && isFinite(stringValue));
|
||||
|
||||
if (isNumeric) {
|
||||
// detected numeric string. Coerce to a number
|
||||
|
||||
return +stringValue;
|
||||
}
|
||||
|
||||
switch (stringValue) {
|
||||
case 'true':
|
||||
return true;
|
||||
case 'false':
|
||||
return false;
|
||||
case 'undefined':
|
||||
return undefined;
|
||||
case 'null':
|
||||
return null;
|
||||
default:
|
||||
return stringValue;
|
||||
}
|
||||
};
|
||||
|
||||
public hasChildren() {
|
||||
return Object.keys(this.children).length > 0;
|
||||
}
|
||||
|
||||
public getChild(key: string) {
|
||||
return this.children[key];
|
||||
}
|
||||
|
||||
public hasChild(key: string) {
|
||||
return this.children[key] !== undefined;
|
||||
}
|
||||
}
|
|
@ -6,56 +6,87 @@ import path from 'path';
|
|||
import process from 'process';
|
||||
|
||||
describe(__filename, function () {
|
||||
describe('parseSettings', function () {
|
||||
let settings:any;
|
||||
const envVarSubstTestCases = [
|
||||
{name: 'true', val: 'true', var: 'SET_VAR_TRUE', want: true},
|
||||
{name: 'false', val: 'false', var: 'SET_VAR_FALSE', want: false},
|
||||
{name: 'null', val: 'null', var: 'SET_VAR_NULL', want: null},
|
||||
{name: 'undefined', val: 'undefined', var: 'SET_VAR_UNDEFINED', want: undefined},
|
||||
{name: 'number', val: '123', var: 'SET_VAR_NUMBER', want: 123},
|
||||
{name: 'string', val: 'foo', var: 'SET_VAR_STRING', want: 'foo'},
|
||||
{name: 'empty string', val: '', var: 'SET_VAR_EMPTY_STRING', want: ''},
|
||||
];
|
||||
describe('parseSettings', function () {
|
||||
let settings: any;
|
||||
const envVarSubstTestCases = [
|
||||
{name: 'true', val: 'true', var: 'SET_VAR_TRUE', want: true},
|
||||
{name: 'false', val: 'false', var: 'SET_VAR_FALSE', want: false},
|
||||
{name: 'null', val: 'null', var: 'SET_VAR_NULL', want: null},
|
||||
{name: 'undefined', val: 'undefined', var: 'SET_VAR_UNDEFINED', want: undefined},
|
||||
{name: 'number', val: '123', var: 'SET_VAR_NUMBER', want: 123},
|
||||
{name: 'string', val: 'foo', var: 'SET_VAR_STRING', want: 'foo'},
|
||||
{name: 'empty string', val: '', var: 'SET_VAR_EMPTY_STRING', want: ''},
|
||||
];
|
||||
|
||||
before(async function () {
|
||||
for (const tc of envVarSubstTestCases) process.env[tc.var] = tc.val;
|
||||
delete process.env.UNSET_VAR;
|
||||
settings = parseSettings(path.join(__dirname, 'settings.json'), true);
|
||||
assert(settings != null);
|
||||
});
|
||||
|
||||
describe('environment variable substitution', function () {
|
||||
describe('set', function () {
|
||||
for (const tc of envVarSubstTestCases) {
|
||||
it(tc.name, async function () {
|
||||
const obj = settings['environment variable substitution'].set;
|
||||
if (tc.name === 'undefined') {
|
||||
assert(!(tc.name in obj));
|
||||
} else {
|
||||
assert.equal(obj[tc.name], tc.want);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('unset', function () {
|
||||
it('no default', async function () {
|
||||
const obj = settings['environment variable substitution'].unset;
|
||||
assert.equal(obj['no default'], null);
|
||||
before(async function () {
|
||||
for (const tc of envVarSubstTestCases) process.env[tc.var] = tc.val;
|
||||
delete process.env.UNSET_VAR;
|
||||
settings = parseSettings(path.join(__dirname, 'settings.json'), true);
|
||||
assert(settings != null);
|
||||
});
|
||||
|
||||
for (const tc of envVarSubstTestCases) {
|
||||
it(tc.name, async function () {
|
||||
const obj = settings['environment variable substitution'].unset;
|
||||
if (tc.name === 'undefined') {
|
||||
assert(!(tc.name in obj));
|
||||
} else {
|
||||
assert.equal(obj[tc.name], tc.want);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
describe('environment variable substitution', function () {
|
||||
describe('set', function () {
|
||||
for (const tc of envVarSubstTestCases) {
|
||||
it(tc.name, async function () {
|
||||
const obj = settings['environment variable substitution'].set;
|
||||
if (tc.name === 'undefined') {
|
||||
assert(!(tc.name in obj));
|
||||
} else {
|
||||
assert.equal(obj[tc.name], tc.want);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('unset', function () {
|
||||
it('no default', async function () {
|
||||
const obj = settings['environment variable substitution'].unset;
|
||||
assert.equal(obj['no default'], null);
|
||||
});
|
||||
|
||||
for (const tc of envVarSubstTestCases) {
|
||||
it(tc.name, async function () {
|
||||
const obj = settings['environment variable substitution'].unset;
|
||||
if (tc.name === 'undefined') {
|
||||
assert(!(tc.name in obj));
|
||||
} else {
|
||||
assert.equal(obj[tc.name], tc.want);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe("Parse plugin settings", function () {
|
||||
|
||||
before(async function () {
|
||||
process.env["EP__ADMIN__PASSWORD"] = "test"
|
||||
})
|
||||
|
||||
it('should parse plugin settings', async function () {
|
||||
let settings = parseSettings(path.join(__dirname, 'settings.json'), true);
|
||||
assert.equal(settings.ADMIN.PASSWORD, "test");
|
||||
})
|
||||
|
||||
it('should bundle settings with same path', async function () {
|
||||
process.env["EP__ADMIN__USERNAME"] = "test"
|
||||
let settings = parseSettings(path.join(__dirname, 'settings.json'), true);
|
||||
assert.deepEqual(settings.ADMIN, {PASSWORD: "test", USERNAME: "test"});
|
||||
})
|
||||
|
||||
it("Can set the ep themes", async function () {
|
||||
process.env["EP__ep_themes__default_theme"] = "hacker"
|
||||
let settings = parseSettings(path.join(__dirname, 'settings.json'), true);
|
||||
assert.deepEqual(settings.ep_themes, {"default_theme": "hacker"});
|
||||
})
|
||||
|
||||
it("can set the ep_webrtc settings", async function () {
|
||||
process.env["EP__ep_webrtc__enabled"] = "true"
|
||||
let settings = parseSettings(path.join(__dirname, 'settings.json'), true);
|
||||
assert.deepEqual(settings.ep_webrtc, {"enabled": true});
|
||||
})
|
||||
})
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue