@freemework/hosting
Version:
Hosting library of the Freemework Project.
120 lines • 5.59 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.FConfigurationToml = void 0;
const fs_1 = require("fs");
const util_1 = require("util");
const url_1 = require("url");
const common_1 = require("@freemework/common");
const readFileAsync = (0, util_1.promisify)(fs_1.readFile);
let parse = null;
const parseLoadPromise = Promise.resolve().then(() => __importStar(require("@iarna/toml"))).then((module) => {
parse = module.parse;
})
.catch((e) => {
const ex = common_1.FException.wrapIfNeeded(e);
function raiseModuleNotFound() {
throw new common_1.FExceptionInvalidOperation("Unable to use FConfigurationToml. Please install module '@iarna/toml' before.", ex);
}
parse = raiseModuleNotFound;
});
class FConfigurationToml extends common_1.FConfigurationDictionary {
static async fromFile(tomlConfigFile, arrayIndexKey = common_1.FConfiguration.DEFAULT_INDEX_KEY, arrayIndexesKey = common_1.FConfiguration.DEFAULT_INDEXES_KEY) {
await parseLoadPromise;
const tomlConfigFileURL = (0, url_1.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 = common_1.FConfiguration.DEFAULT_INDEX_KEY, arrayIndexesKey = common_1.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 common_1.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 common_1.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);
}
}
exports.FConfigurationToml = FConfigurationToml;
//# sourceMappingURL=FConfigurationToml.js.map