helper.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var helper_exports = {};
  20. __export(helper_exports, {
  21. helper: () => helper
  22. });
  23. module.exports = __toCommonJS(helper_exports);
  24. var import_debugLogger = require("./utils/debugLogger");
  25. var import_eventsHelper = require("./utils/eventsHelper");
  26. const MAX_LOG_LENGTH = process.env.MAX_LOG_LENGTH ? +process.env.MAX_LOG_LENGTH : Infinity;
  27. class Helper {
  28. static completeUserURL(urlString) {
  29. if (urlString.startsWith("localhost") || urlString.startsWith("127.0.0.1"))
  30. urlString = "http://" + urlString;
  31. return urlString;
  32. }
  33. static enclosingIntRect(rect) {
  34. const x = Math.floor(rect.x + 1e-3);
  35. const y = Math.floor(rect.y + 1e-3);
  36. const x2 = Math.ceil(rect.x + rect.width - 1e-3);
  37. const y2 = Math.ceil(rect.y + rect.height - 1e-3);
  38. return { x, y, width: x2 - x, height: y2 - y };
  39. }
  40. static enclosingIntSize(size) {
  41. return { width: Math.floor(size.width + 1e-3), height: Math.floor(size.height + 1e-3) };
  42. }
  43. static getViewportSizeFromWindowFeatures(features) {
  44. const widthString = features.find((f) => f.startsWith("width="));
  45. const heightString = features.find((f) => f.startsWith("height="));
  46. const width = widthString ? parseInt(widthString.substring(6), 10) : NaN;
  47. const height = heightString ? parseInt(heightString.substring(7), 10) : NaN;
  48. if (!Number.isNaN(width) && !Number.isNaN(height))
  49. return { width, height };
  50. return null;
  51. }
  52. static waitForEvent(progress, emitter, event, predicate) {
  53. const listeners = [];
  54. const dispose = () => import_eventsHelper.eventsHelper.removeEventListeners(listeners);
  55. const promise = progress.race(new Promise((resolve, reject) => {
  56. listeners.push(import_eventsHelper.eventsHelper.addEventListener(emitter, event, (eventArg) => {
  57. try {
  58. if (predicate && !predicate(eventArg))
  59. return;
  60. resolve(eventArg);
  61. } catch (e) {
  62. reject(e);
  63. }
  64. }));
  65. })).finally(() => dispose());
  66. return { promise, dispose };
  67. }
  68. static secondsToRoundishMillis(value) {
  69. return (value * 1e6 | 0) / 1e3;
  70. }
  71. static millisToRoundishMillis(value) {
  72. return (value * 1e3 | 0) / 1e3;
  73. }
  74. static debugProtocolLogger(protocolLogger) {
  75. return (direction, message) => {
  76. if (protocolLogger)
  77. protocolLogger(direction, message);
  78. if (import_debugLogger.debugLogger.isEnabled("protocol")) {
  79. let text = JSON.stringify(message);
  80. if (text.length > MAX_LOG_LENGTH)
  81. text = text.substring(0, MAX_LOG_LENGTH / 2) + " <<<<<( LOG TRUNCATED )>>>>> " + text.substring(text.length - MAX_LOG_LENGTH / 2);
  82. import_debugLogger.debugLogger.log("protocol", (direction === "send" ? "SEND \u25BA " : "\u25C0 RECV ") + text);
  83. }
  84. };
  85. }
  86. static formatBrowserLogs(logs, disconnectReason) {
  87. if (!disconnectReason && !logs.length)
  88. return "";
  89. return "\n" + (disconnectReason ? disconnectReason + "\n" : "") + logs.join("\n");
  90. }
  91. }
  92. const helper = Helper;
  93. // Annotate the CommonJS export names for ESM import in node:
  94. 0 && (module.exports = {
  95. helper
  96. });