helper.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.helper = void 0;
  6. var _debugLogger = require("../common/debugLogger");
  7. var _eventsHelper = require("../utils/eventsHelper");
  8. /**
  9. * Copyright 2017 Google Inc. All rights reserved.
  10. * Modifications copyright (c) Microsoft Corporation.
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. const MAX_LOG_LENGTH = process.env.MAX_LOG_LENGTH ? +process.env.MAX_LOG_LENGTH : Infinity;
  25. class Helper {
  26. static completeUserURL(urlString) {
  27. if (urlString.startsWith('localhost') || urlString.startsWith('127.0.0.1')) urlString = 'http://' + urlString;
  28. return urlString;
  29. }
  30. static enclosingIntRect(rect) {
  31. const x = Math.floor(rect.x + 1e-3);
  32. const y = Math.floor(rect.y + 1e-3);
  33. const x2 = Math.ceil(rect.x + rect.width - 1e-3);
  34. const y2 = Math.ceil(rect.y + rect.height - 1e-3);
  35. return {
  36. x,
  37. y,
  38. width: x2 - x,
  39. height: y2 - y
  40. };
  41. }
  42. static enclosingIntSize(size) {
  43. return {
  44. width: Math.floor(size.width + 1e-3),
  45. height: Math.floor(size.height + 1e-3)
  46. };
  47. }
  48. static getViewportSizeFromWindowFeatures(features) {
  49. const widthString = features.find(f => f.startsWith('width='));
  50. const heightString = features.find(f => f.startsWith('height='));
  51. const width = widthString ? parseInt(widthString.substring(6), 10) : NaN;
  52. const height = heightString ? parseInt(heightString.substring(7), 10) : NaN;
  53. if (!Number.isNaN(width) && !Number.isNaN(height)) return {
  54. width,
  55. height
  56. };
  57. return null;
  58. }
  59. static waitForEvent(progress, emitter, event, predicate) {
  60. const listeners = [];
  61. const promise = new Promise((resolve, reject) => {
  62. listeners.push(_eventsHelper.eventsHelper.addEventListener(emitter, event, eventArg => {
  63. try {
  64. if (predicate && !predicate(eventArg)) return;
  65. _eventsHelper.eventsHelper.removeEventListeners(listeners);
  66. resolve(eventArg);
  67. } catch (e) {
  68. _eventsHelper.eventsHelper.removeEventListeners(listeners);
  69. reject(e);
  70. }
  71. }));
  72. });
  73. const dispose = () => _eventsHelper.eventsHelper.removeEventListeners(listeners);
  74. if (progress) progress.cleanupWhenAborted(dispose);
  75. return {
  76. promise,
  77. dispose
  78. };
  79. }
  80. static secondsToRoundishMillis(value) {
  81. return (value * 1000000 | 0) / 1000;
  82. }
  83. static millisToRoundishMillis(value) {
  84. return (value * 1000 | 0) / 1000;
  85. }
  86. static debugProtocolLogger(protocolLogger) {
  87. return (direction, message) => {
  88. if (protocolLogger) protocolLogger(direction, message);
  89. if (_debugLogger.debugLogger.isEnabled('protocol')) {
  90. let text = JSON.stringify(message);
  91. if (text.length > MAX_LOG_LENGTH) text = text.substring(0, MAX_LOG_LENGTH / 2) + ' <<<<<( LOG TRUNCATED )>>>>> ' + text.substring(text.length - MAX_LOG_LENGTH / 2);
  92. _debugLogger.debugLogger.log('protocol', (direction === 'send' ? 'SEND ► ' : '◀ RECV ') + text);
  93. }
  94. };
  95. }
  96. static formatBrowserLogs(logs) {
  97. if (!logs.length) return '';
  98. return '\n' + logs.join('\n');
  99. }
  100. }
  101. const helper = exports.helper = Helper;