userAgent.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getEmbedderName = getEmbedderName;
  6. exports.getPlaywrightVersion = getPlaywrightVersion;
  7. exports.getUserAgent = getUserAgent;
  8. exports.userAgentVersionMatchesErrorMessage = userAgentVersionMatchesErrorMessage;
  9. var _child_process = require("child_process");
  10. var _os = _interopRequireDefault(require("os"));
  11. var _linuxUtils = require("../utils/linuxUtils");
  12. var _ascii = require("./ascii");
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. /**
  15. * Copyright (c) Microsoft Corporation.
  16. *
  17. * Licensed under the Apache License, Version 2.0 (the "License");
  18. * you may not use this file except in compliance with the License.
  19. * You may obtain a copy of the License at
  20. *
  21. * http://www.apache.org/licenses/LICENSE-2.0
  22. *
  23. * Unless required by applicable law or agreed to in writing, software
  24. * distributed under the License is distributed on an "AS IS" BASIS,
  25. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26. * See the License for the specific language governing permissions and
  27. * limitations under the License.
  28. */
  29. let cachedUserAgent;
  30. function getUserAgent() {
  31. if (cachedUserAgent) return cachedUserAgent;
  32. try {
  33. cachedUserAgent = determineUserAgent();
  34. } catch (e) {
  35. cachedUserAgent = 'Playwright/unknown';
  36. }
  37. return cachedUserAgent;
  38. }
  39. function determineUserAgent() {
  40. let osIdentifier = 'unknown';
  41. let osVersion = 'unknown';
  42. if (process.platform === 'win32') {
  43. const version = _os.default.release().split('.');
  44. osIdentifier = 'windows';
  45. osVersion = `${version[0]}.${version[1]}`;
  46. } else if (process.platform === 'darwin') {
  47. const version = (0, _child_process.execSync)('sw_vers -productVersion', {
  48. stdio: ['ignore', 'pipe', 'ignore']
  49. }).toString().trim().split('.');
  50. osIdentifier = 'macOS';
  51. osVersion = `${version[0]}.${version[1]}`;
  52. } else if (process.platform === 'linux') {
  53. const distroInfo = (0, _linuxUtils.getLinuxDistributionInfoSync)();
  54. if (distroInfo) {
  55. osIdentifier = distroInfo.id || 'linux';
  56. osVersion = distroInfo.version || 'unknown';
  57. } else {
  58. // Linux distribution without /etc/os-release.
  59. // Default to linux/unknown.
  60. osIdentifier = 'linux';
  61. }
  62. }
  63. const additionalTokens = [];
  64. if (process.env.CI) additionalTokens.push('CI/1');
  65. const serializedTokens = additionalTokens.length ? ' ' + additionalTokens.join(' ') : '';
  66. const {
  67. embedderName,
  68. embedderVersion
  69. } = getEmbedderName();
  70. return `Playwright/${getPlaywrightVersion()} (${_os.default.arch()}; ${osIdentifier} ${osVersion}) ${embedderName}/${embedderVersion}${serializedTokens}`;
  71. }
  72. function getEmbedderName() {
  73. let embedderName = 'unknown';
  74. let embedderVersion = 'unknown';
  75. if (!process.env.PW_LANG_NAME) {
  76. embedderName = 'node';
  77. embedderVersion = process.version.substring(1).split('.').slice(0, 2).join('.');
  78. } else if (['node', 'python', 'java', 'csharp'].includes(process.env.PW_LANG_NAME)) {
  79. var _process$env$PW_LANG_;
  80. embedderName = process.env.PW_LANG_NAME;
  81. embedderVersion = (_process$env$PW_LANG_ = process.env.PW_LANG_NAME_VERSION) !== null && _process$env$PW_LANG_ !== void 0 ? _process$env$PW_LANG_ : 'unknown';
  82. }
  83. return {
  84. embedderName,
  85. embedderVersion
  86. };
  87. }
  88. function getPlaywrightVersion(majorMinorOnly = false) {
  89. const version = process.env.PW_VERSION_OVERRIDE || require('./../../package.json').version;
  90. return majorMinorOnly ? version.split('.').slice(0, 2).join('.') : version;
  91. }
  92. function userAgentVersionMatchesErrorMessage(userAgent) {
  93. const match = userAgent.match(/^Playwright\/(\d+\.\d+\.\d+)/);
  94. if (!match) {
  95. // Cannot parse user agent - be lax.
  96. return;
  97. }
  98. const received = match[1].split('.').slice(0, 2).join('.');
  99. const expected = getPlaywrightVersion(true);
  100. if (received !== expected) {
  101. return (0, _ascii.wrapInASCIIBox)([`Playwright version mismatch:`, ` - server version: v${expected}`, ` - client version: v${received}`, ``, `If you are using VSCode extension, restart VSCode.`, ``, `If you are connecting to a remote service,`, `keep your local Playwright version in sync`, `with the remote service version.`, ``, `<3 Playwright Team`].join('\n'), 1);
  102. }
  103. }