@sap_oss/wdio-qmate-service
Version:
[](https://api.reuse.software/info/github.com/SAP/wdio-qmate-service)[](http
210 lines • 10.1 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 () {
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;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Data = void 0;
// Common imports
const fs_1 = require("fs");
const path = __importStar(require("path"));
// Own imports
const dataExchangeUtil_1 = __importDefault(require("../../../scripts/dataExchange/dataExchangeUtil"));
const verboseLogger_1 = require("../../helper/verboseLogger");
const errorHandler_1 = __importDefault(require("../../helper/errorHandler"));
/**
* @class data
* @memberof util
*/
class Data {
// ========================== Class variables ==========================
vlf = new verboseLogger_1.VerboseLoggerFactory("util", "data");
ErrorHandler = new errorHandler_1.default();
_alreadyDecryptedData = new Set();
// ========================== Public functions ==========================
/**
* @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.
* @param {Object} [options] - The options object.
* @returns {Object} The encrypted or local data object.
* @example const secureData = util.data.getSecureData("myTest");
*/
getSecureData(filename, source = "data", options) {
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];
const dataIdentifier = `${source}_${filename}`;
// Decrypt data if not already decrypted and private key is found
if (!this._alreadyDecryptedData.has(dataIdentifier) && privateKeyFound) {
this._decryptRecursively(data, options);
}
// Make sure data is not decrypted again (Set ignores duplicates)
this._alreadyDecryptedData.add(dataIdentifier);
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 | Array<String>} data - The encrypted data to decrypt. Single value or array of values for different keys.
* @param {Object} options - The decryption options.
* @returns {String} The decrypted data.
* @example const decrypted = util.data.decrypt("d704004c262faa8ef4bdcf34c8a94883e15524872c7bef334d26a391a1934cf47338b749d99426980ee4cc7a81deaef21439c6894ab0324cdb29b9b6332635baca442651c5d37847f52bb90b8868e037271a7d456024b39b65fdf4dc62e8d82a3d5683a72e4324c59d339742fc79749f0ee74abef916d38e306218adc48e3547a2b346962249320c962d22cb46d9998de36d8219a2496c0997d0fc389f76fb1431a9b57c317886e9c9795c0a09ad98d9fa0b7687d10814dc7973397f3f72a227a04ead4c3d1d428c096a51922ffc4d7afc3952df1c130def5c5fb3e834605cbf1454885966cc65c77046343f4c678e74931fb2dd6cac8dae17837cf674f288d6550dd7fa6b01f5b7ea68aa6bd27d79dd5d53edb5fd4b4edce824bd31b3939352ad7a71a16bab8c54025c2bb92c54e022fcd23ff08bc54a17fc50d00dc3b884cadbfdefe1e75901fdf80e7324ad02a891f2c4863fa120ca238520b79126c65a03");
*/
decrypt(data, options) {
const vl = this.vlf.initLog(this.decrypt);
return global.util.data.decrypt(data, options); // Function is defined under: scripts\hooks\utils\decryption.js
}
// ========================== Private functions ==========================
_decryptRecursively(data, options) {
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], options);
}
else if (Array.isArray(data[key])) {
data[key] = global.util.data.decrypt(data[key], options);
}
else if (typeof data[key] === "string") {
const isPlainHex = this._isHex(data[key]);
const isBase64Hex = !isPlainHex && this._isBase64EncodedHex(data[key]);
if (isPlainHex || isBase64Hex) {
const effectiveOptions = isBase64Hex ? { useBase64Input: true, ...options } : options; // User-provided options override auto-detected values
data[key] = global.util.data.decrypt(data[key], effectiveOptions);
}
}
}
return data;
}
_isHex(str) {
return /^[0-9a-fA-F]+$/.test(str) && str.length % 2 === 0 && str.length >= 64;
}
_isBase64EncodedHex(str) {
try {
const decoded = Buffer.from(str, "base64").toString("utf8");
return this._isHex(decoded);
}
catch {
return false;
}
}
}
exports.Data = Data;
exports.default = new Data();
//# sourceMappingURL=data.js.map