@freemework/hosting
Version:
Hosting library of the Freemework Project.
84 lines • 4.04 kB
JavaScript
import { readFile } from "fs";
import { promisify } from "util";
import { pathToFileURL } from "url";
import { FConfiguration, FConfigurationDictionary, FConfigurationException, FException, FExceptionInvalidOperation, } from "@freemework/common";
const readFileAsync = promisify(readFile);
let parse = null;
const parseLoadPromise = import("@iarna/toml")
.then((module) => {
parse = module.parse;
})
.catch((e) => {
const ex = FException.wrapIfNeeded(e);
function raiseModuleNotFound() {
throw new FExceptionInvalidOperation("Unable to use FConfigurationToml. Please install module '@iarna/toml' before.", ex);
}
parse = raiseModuleNotFound;
});
export class FConfigurationToml extends FConfigurationDictionary {
static async fromFile(tomlConfigFile, arrayIndexKey = FConfiguration.DEFAULT_INDEX_KEY, arrayIndexesKey = FConfiguration.DEFAULT_INDEXES_KEY) {
await parseLoadPromise;
const tomlConfigFileURL = pathToFileURL(tomlConfigFile);
const sourceURI = new URL(`configuration+file+toml://${tomlConfigFileURL.pathname}`);
const fileContent = await readFileAsync(tomlConfigFile);
return new FConfigurationToml(sourceURI, fileContent.toString("utf-8"), arrayIndexKey, arrayIndexesKey);
}
static async factory(tomlDocument, arrayIndexKey = FConfiguration.DEFAULT_INDEX_KEY, arrayIndexesKey = FConfiguration.DEFAULT_INDEXES_KEY) {
await parseLoadPromise;
const encodedTomlDocument = encodeURIComponent(tomlDocument);
const sourceURI = new URL(`configuration:toml?data=${encodedTomlDocument}`);
return new FConfigurationToml(sourceURI, tomlDocument, arrayIndexKey, arrayIndexesKey);
}
constructor(sourceURI, tomlDocument, arrayIndexKey, arrayIndexesKey) {
if (parse === null) {
throw new FExceptionInvalidOperation("Unable to use FConfigurationToml. Please install module '@iarna/toml' before.");
}
const dict = {};
const tomlData = parse(tomlDocument);
function recursiveWalker(sourceData, ns = "") {
if (typeof sourceData === "string") {
dict[ns] = sourceData;
}
else if (typeof sourceData === "number") {
dict[ns] = sourceData.toString();
}
else if (typeof sourceData === "boolean") {
dict[ns] = sourceData ? "true" : "false";
}
else if (Array.isArray(sourceData)) {
const indexes = [];
const indexerKey = `${ns}.${arrayIndexesKey}`;
for (let index = 0; index < sourceData.length; ++index) {
const innerSourceData = sourceData[index];
let indexName;
if (typeof innerSourceData === "object" &&
arrayIndexKey in innerSourceData) {
indexName = innerSourceData[arrayIndexKey];
delete innerSourceData[arrayIndexKey];
}
else {
indexName = index.toString();
}
const subKey = `${ns}.${indexName}`;
recursiveWalker(innerSourceData, subKey);
if (indexes.includes(indexName)) {
throw new FConfigurationException(`Index constraint violation, the index '${indexName}' appears twice.`, ns);
}
indexes.push(indexName);
}
if (!(indexerKey in dict)) {
dict[indexerKey] = indexes.join(" ");
}
}
else {
for (const [key, value] of Object.entries(sourceData)) {
const fullKey = ns !== "" ? `${ns}.${key}` : key;
recursiveWalker(value, fullKey);
}
}
}
recursiveWalker(tomlData);
super(sourceURI, dict);
}
}
//# sourceMappingURL=FConfigurationToml.js.map