Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.sleep = exports.canAccess = exports.isBase64 = exports.filterSpecArgs = exports.isFunctionAsync = exports.safeRequire = exports.getArgumentType = exports.isValidParameter = exports.transformCommandLogResult = exports.commandCallStructure = exports.overwriteElementCommands = void 0;
  7. const fs_1 = __importDefault(require("fs"));
  8. const path_1 = __importDefault(require("path"));
  9. const SCREENSHOT_REPLACEMENT = '"<Screenshot[base64]>"';
  10. function overwriteElementCommands(propertiesObject) {
  11. const elementOverrides = propertiesObject['__elementOverrides__'] ? propertiesObject['__elementOverrides__'].value : {};
  12. for (const [commandName, userDefinedCommand] of Object.entries(elementOverrides)) {
  13. if (typeof userDefinedCommand !== 'function') {
  14. throw new Error('overwriteCommand: commands be overwritten only with functions, command: ' + commandName);
  15. }
  16. if (!propertiesObject[commandName]) {
  17. throw new Error('overwriteCommand: no command to be overwritten: ' + commandName);
  18. }
  19. if (typeof propertiesObject[commandName].value !== 'function') {
  20. throw new Error('overwriteCommand: only functions can be overwritten, command: ' + commandName);
  21. }
  22. const origCommand = propertiesObject[commandName].value;
  23. delete propertiesObject[commandName];
  24. const newCommand = function (...args) {
  25. const element = this;
  26. return userDefinedCommand.apply(element, [
  27. function origCommandFunction() {
  28. const context = this || element;
  29. return origCommand.apply(context, arguments);
  30. },
  31. ...args
  32. ]);
  33. };
  34. propertiesObject[commandName] = {
  35. value: newCommand,
  36. configurable: true
  37. };
  38. }
  39. delete propertiesObject['__elementOverrides__'];
  40. propertiesObject['__elementOverrides__'] = { value: {} };
  41. }
  42. exports.overwriteElementCommands = overwriteElementCommands;
  43. function commandCallStructure(commandName, args) {
  44. const callArgs = args.map((arg) => {
  45. if (typeof arg === 'string' && (arg.startsWith('!function(') || arg.startsWith('return (function'))) {
  46. arg = '<fn>';
  47. }
  48. else if (typeof arg === 'string' &&
  49. !commandName.startsWith('findElement') &&
  50. isBase64(arg)) {
  51. arg = SCREENSHOT_REPLACEMENT;
  52. }
  53. else if (typeof arg === 'string') {
  54. arg = `"${arg}"`;
  55. }
  56. else if (typeof arg === 'function') {
  57. arg = '<fn>';
  58. }
  59. else if (arg === null) {
  60. arg = 'null';
  61. }
  62. else if (typeof arg === 'object') {
  63. arg = '<object>';
  64. }
  65. else if (typeof arg === 'undefined') {
  66. arg = typeof arg;
  67. }
  68. return arg;
  69. }).join(', ');
  70. return `${commandName}(${callArgs})`;
  71. }
  72. exports.commandCallStructure = commandCallStructure;
  73. function transformCommandLogResult(result) {
  74. if (typeof result.file === 'string' && isBase64(result.file)) {
  75. return SCREENSHOT_REPLACEMENT;
  76. }
  77. return result;
  78. }
  79. exports.transformCommandLogResult = transformCommandLogResult;
  80. function isValidParameter(arg, expectedType) {
  81. let shouldBeArray = false;
  82. if (expectedType.slice(-2) === '[]') {
  83. expectedType = expectedType.slice(0, -2);
  84. shouldBeArray = true;
  85. }
  86. if (shouldBeArray) {
  87. if (!Array.isArray(arg)) {
  88. return false;
  89. }
  90. }
  91. else {
  92. arg = [arg];
  93. }
  94. for (const argEntity of arg) {
  95. const argEntityType = getArgumentType(argEntity);
  96. if (!argEntityType.match(expectedType)) {
  97. return false;
  98. }
  99. }
  100. return true;
  101. }
  102. exports.isValidParameter = isValidParameter;
  103. function getArgumentType(arg) {
  104. return arg === null ? 'null' : typeof arg;
  105. }
  106. exports.getArgumentType = getArgumentType;
  107. function safeRequire(name) {
  108. var _a, _b, _c;
  109. let requirePath;
  110. try {
  111. const localNodeModules = path_1.default.join(process.cwd(), '/node_modules');
  112. if (!((_a = require === null || require === void 0 ? void 0 : require.main) === null || _a === void 0 ? void 0 : _a.paths.includes(localNodeModules))) {
  113. (_b = require === null || require === void 0 ? void 0 : require.main) === null || _b === void 0 ? void 0 : _b.paths.push(localNodeModules);
  114. const requireOpts = process.env.JEST_WORKER_ID
  115. ? {}
  116. : { paths: (_c = require === null || require === void 0 ? void 0 : require.main) === null || _c === void 0 ? void 0 : _c.paths };
  117. requirePath = require.resolve(name, requireOpts);
  118. }
  119. else {
  120. requirePath = require.resolve(name);
  121. }
  122. }
  123. catch (e) {
  124. return null;
  125. }
  126. try {
  127. return require(requirePath);
  128. }
  129. catch (e) {
  130. throw new Error(`Couldn't initialise "${name}".\n${e.stack}`);
  131. }
  132. }
  133. exports.safeRequire = safeRequire;
  134. function isFunctionAsync(fn) {
  135. return (fn.constructor && fn.constructor.name === 'AsyncFunction') || fn.name === 'async';
  136. }
  137. exports.isFunctionAsync = isFunctionAsync;
  138. function filterSpecArgs(args) {
  139. return args.filter((arg) => typeof arg !== 'function');
  140. }
  141. exports.filterSpecArgs = filterSpecArgs;
  142. function isBase64(str) {
  143. var notBase64 = new RegExp('[^A-Z0-9+\\/=]', 'i');
  144. if (typeof str !== 'string') {
  145. throw new Error('Expected string but received invalid type.');
  146. }
  147. const len = str.length;
  148. if (!len || len % 4 !== 0 || notBase64.test(str)) {
  149. return false;
  150. }
  151. const firstPaddingChar = str.indexOf('=');
  152. return firstPaddingChar === -1 ||
  153. firstPaddingChar === len - 1 ||
  154. (firstPaddingChar === len - 2 && str[len - 1] === '=');
  155. }
  156. exports.isBase64 = isBase64;
  157. exports.canAccess = (file) => {
  158. if (!file) {
  159. return false;
  160. }
  161. try {
  162. fs_1.default.accessSync(file);
  163. return true;
  164. }
  165. catch (e) {
  166. return false;
  167. }
  168. };
  169. exports.sleep = (ms = 0) => new Promise((r) => setTimeout(r, ms));