@sap_oss/wdio-qmate-service
Version:
[](https://api.reuse.software/info/github.com/SAP/wdio-qmate-service)[](http
170 lines • 8.06 kB
JavaScript
"use strict";
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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Data = void 0;
const verboseLogger_1 = require("../../helper/verboseLogger");
const fs_1 = require("fs");
const path = __importStar(require("path"));
const dataExchangeUtil_1 = __importDefault(require("../../../scripts/dataExchange/dataExchangeUtil"));
const errorHandler_1 = __importDefault(require("../../helper/errorHandler"));
/**
* @class data
* @memberof util
*/
class Data {
constructor() {
this.vlf = new verboseLogger_1.VerboseLoggerFactory("util", "data");
this.ErrorHandler = new errorHandler_1.default();
}
/**
* @function getData
* @memberOf util.data
* @description Returns the data object with the given filename (JSON, stored in data folder).
* @param {String} filename - The name of the data file.
* @param {String} [source=data] - The source key defined under params.import of the config file.
* @returns {Object} The data object.
* @example const data = util.data.getData("myTest");
*/
getData(filename, source = "data") {
const vl = this.vlf.initLog(this.getData);
if (browser.config.params && browser.config.params.import && browser.config.params.import[source]) {
if (browser.config.params.import[source][filename]) {
return browser.config.params.import[source][filename];
}
else {
return this.ErrorHandler.logException(new Error(`File '${filename}.json' empty or not defined under '${source}'`));
}
}
else {
return this.ErrorHandler.logException(new Error(`Data path '${source}' not defined in config.`));
}
}
/**
* @function getSecureData
* @memberOf util.data
* @description Returns and encrypts the data object with the given filename (JSON, stored in data folder). Will return the local file object if private key is not accessible.
* @param {String} filename - The name of the data file (without suffix '.secure' or '.local').
* @param {String} [source=data] - The source key defined under params.import of the config file.
* @returns {Object} The encrypted or local data object.
* @example const secureData = util.data.getSecureData("myTest");
*/
getSecureData(filename, source = "data") {
const vl = this.vlf.initLog(this.getSecureData);
const privateKeyFound = global.util.data.privateKeyFound === true;
if (privateKeyFound) {
filename = `${filename}.secure`;
}
else {
util.console.info("getSecureData: No private key found. Continue using local file.");
filename = `${filename}.local`;
}
if (browser.config.params && browser.config.params.import && browser.config.params.import[source]) {
if (browser.config.params.import[source][filename]) {
const data = browser.config.params.import[source][filename];
if (privateKeyFound) {
this._decryptRecursively(data);
}
return data;
}
else {
return this.ErrorHandler.logException(new Error(`File '${filename}.json' empty or not defined under '${source}'`));
}
}
else {
return this.ErrorHandler.logException(new Error(`Data path '${source}' not defined in config.`));
}
}
/**
* @function readDataFromFile
* @memberOf util.data
* @description Reads the data object from the given filepath.
* @param {String} filePath - The filepath.
* @returns {Object} The data object.
* @example const data = util.data.readDataFromFile("./data/myData.json");
*/
async readDataFromFile(filePath) {
const relativeFilePath = dataExchangeUtil_1.default.getFileAbsPath(filePath);
if (!relativeFilePath) {
return this.ErrorHandler.logException(new Error("Filepath could not be resolved."));
}
try {
return JSON.parse(await fs_1.promises.readFile(path.resolve(relativeFilePath), "utf8"));
}
catch (error) {
return this.ErrorHandler.logException(new Error(), error.message);
}
}
/**
* @function writeDataToFile
* @memberOf util.data
* @description Writes the data object to the given filepath.
* @param {String} filePath - The filepath.
* @param {Object} data - The data object to write.
* @example const data = util.data.writeDataToFile("myTest");
*/
async writeDataToFile(filePath, data) {
const relativeFilePath = dataExchangeUtil_1.default.getFileAbsPath(filePath);
if (!relativeFilePath) {
return this.ErrorHandler.logException(new Error("Filepath could not be resolved."));
}
try {
await fs_1.promises.writeFile(path.resolve(relativeFilePath), JSON.stringify(data));
}
catch (error) {
return this.ErrorHandler.logException(new Error(), error.message);
}
}
/**
* @function decrypt
* @memberOf util.data
* @description Decrypts the passed input data.
* @param {String} data - The encrypted data to decrypt.
* @returns {String} The decrypted data.
* @example const decrypted = util.data.decrypt("d704004c262faa8ef4bdcf34c8a94883e15524872c7bef334d26a391a1934cf47338b749d99426980ee4cc7a81deaef21439c6894ab0324cdb29b9b6332635baca442651c5d37847f52bb90b8868e037271a7d456024b39b65fdf4dc62e8d82a3d5683a72e4324c59d339742fc79749f0ee74abef916d38e306218adc48e3547a2b346962249320c962d22cb46d9998de36d8219a2496c0997d0fc389f76fb1431a9b57c317886e9c9795c0a09ad98d9fa0b7687d10814dc7973397f3f72a227a04ead4c3d1d428c096a51922ffc4d7afc3952df1c130def5c5fb3e834605cbf1454885966cc65c77046343f4c678e74931fb2dd6cac8dae17837cf674f288d6550dd7fa6b01f5b7ea68aa6bd27d79dd5d53edb5fd4b4edce824bd31b3939352ad7a71a16bab8c54025c2bb92c54e022fcd23ff08bc54a17fc50d00dc3b884cadbfdefe1e75901fdf80e7324ad02a891f2c4863fa120ca238520b79126c65a03");
*/
decrypt(data) {
const vl = this.vlf.initLog(this.decrypt);
return global.util.data.decrypt(data); // Function is defined under: scripts\hooks\utils\decryption.js
}
_decryptRecursively(data) {
const vl = this.vlf.initLog(this._decryptRecursively);
vl.log(`Decrypting ${data}`);
for (const key in data) {
if (typeof data[key] === "object" && !Array.isArray(data[key])) {
data[key] = this._decryptRecursively(data[key]);
}
else if (typeof data[key] === "string" || Array.isArray(data[key])) {
data[key] = global.util.data.decrypt(data[key]);
}
}
return data;
}
}
exports.Data = Data;
exports.default = new Data();
//# sourceMappingURL=data.js.map