platform.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. var _a;
  2. /*---------------------------------------------------------------------------------------------
  3. * Copyright (c) Microsoft Corporation. All rights reserved.
  4. * Licensed under the MIT License. See License.txt in the project root for license information.
  5. *--------------------------------------------------------------------------------------------*/
  6. import * as nls from '../../nls.js';
  7. const LANGUAGE_DEFAULT = 'en';
  8. let _isWindows = false;
  9. let _isMacintosh = false;
  10. let _isLinux = false;
  11. let _isLinuxSnap = false;
  12. let _isNative = false;
  13. let _isWeb = false;
  14. let _isElectron = false;
  15. let _isIOS = false;
  16. let _isCI = false;
  17. let _locale = undefined;
  18. let _language = LANGUAGE_DEFAULT;
  19. let _translationsConfigFile = undefined;
  20. let _userAgent = undefined;
  21. export const globals = (typeof self === 'object' ? self : typeof global === 'object' ? global : {});
  22. let nodeProcess = undefined;
  23. if (typeof globals.vscode !== 'undefined' && typeof globals.vscode.process !== 'undefined') {
  24. // Native environment (sandboxed)
  25. nodeProcess = globals.vscode.process;
  26. }
  27. else if (typeof process !== 'undefined') {
  28. // Native environment (non-sandboxed)
  29. nodeProcess = process;
  30. }
  31. const isElectronProcess = typeof ((_a = nodeProcess === null || nodeProcess === void 0 ? void 0 : nodeProcess.versions) === null || _a === void 0 ? void 0 : _a.electron) === 'string';
  32. const isElectronRenderer = isElectronProcess && (nodeProcess === null || nodeProcess === void 0 ? void 0 : nodeProcess.type) === 'renderer';
  33. // Web environment
  34. if (typeof navigator === 'object' && !isElectronRenderer) {
  35. _userAgent = navigator.userAgent;
  36. _isWindows = _userAgent.indexOf('Windows') >= 0;
  37. _isMacintosh = _userAgent.indexOf('Macintosh') >= 0;
  38. _isIOS = (_userAgent.indexOf('Macintosh') >= 0 || _userAgent.indexOf('iPad') >= 0 || _userAgent.indexOf('iPhone') >= 0) && !!navigator.maxTouchPoints && navigator.maxTouchPoints > 0;
  39. _isLinux = _userAgent.indexOf('Linux') >= 0;
  40. _isWeb = true;
  41. const configuredLocale = nls.getConfiguredDefaultLocale(
  42. // This call _must_ be done in the file that calls `nls.getConfiguredDefaultLocale`
  43. // to ensure that the NLS AMD Loader plugin has been loaded and configured.
  44. // This is because the loader plugin decides what the default locale is based on
  45. // how it's able to resolve the strings.
  46. nls.localize({ key: 'ensureLoaderPluginIsLoaded', comment: ['{Locked}'] }, '_'));
  47. _locale = configuredLocale || LANGUAGE_DEFAULT;
  48. _language = _locale;
  49. }
  50. // Native environment
  51. else if (typeof nodeProcess === 'object') {
  52. _isWindows = (nodeProcess.platform === 'win32');
  53. _isMacintosh = (nodeProcess.platform === 'darwin');
  54. _isLinux = (nodeProcess.platform === 'linux');
  55. _isLinuxSnap = _isLinux && !!nodeProcess.env['SNAP'] && !!nodeProcess.env['SNAP_REVISION'];
  56. _isElectron = isElectronProcess;
  57. _isCI = !!nodeProcess.env['CI'] || !!nodeProcess.env['BUILD_ARTIFACTSTAGINGDIRECTORY'];
  58. _locale = LANGUAGE_DEFAULT;
  59. _language = LANGUAGE_DEFAULT;
  60. const rawNlsConfig = nodeProcess.env['VSCODE_NLS_CONFIG'];
  61. if (rawNlsConfig) {
  62. try {
  63. const nlsConfig = JSON.parse(rawNlsConfig);
  64. const resolved = nlsConfig.availableLanguages['*'];
  65. _locale = nlsConfig.locale;
  66. // VSCode's default language is 'en'
  67. _language = resolved ? resolved : LANGUAGE_DEFAULT;
  68. _translationsConfigFile = nlsConfig._translationsConfigFile;
  69. }
  70. catch (e) {
  71. }
  72. }
  73. _isNative = true;
  74. }
  75. // Unknown environment
  76. else {
  77. console.error('Unable to resolve platform.');
  78. }
  79. let _platform = 0 /* Platform.Web */;
  80. if (_isMacintosh) {
  81. _platform = 1 /* Platform.Mac */;
  82. }
  83. else if (_isWindows) {
  84. _platform = 3 /* Platform.Windows */;
  85. }
  86. else if (_isLinux) {
  87. _platform = 2 /* Platform.Linux */;
  88. }
  89. export const isWindows = _isWindows;
  90. export const isMacintosh = _isMacintosh;
  91. export const isLinux = _isLinux;
  92. export const isNative = _isNative;
  93. export const isWeb = _isWeb;
  94. export const isWebWorker = (_isWeb && typeof globals.importScripts === 'function');
  95. export const isIOS = _isIOS;
  96. export const userAgent = _userAgent;
  97. /**
  98. * The language used for the user interface. The format of
  99. * the string is all lower case (e.g. zh-tw for Traditional
  100. * Chinese)
  101. */
  102. export const language = _language;
  103. export const setTimeout0IsFaster = (typeof globals.postMessage === 'function' && !globals.importScripts);
  104. /**
  105. * See https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#:~:text=than%204%2C%20then-,set%20timeout%20to%204,-.
  106. *
  107. * Works similarly to `setTimeout(0)` but doesn't suffer from the 4ms artificial delay
  108. * that browsers set when the nesting level is > 5.
  109. */
  110. export const setTimeout0 = (() => {
  111. if (setTimeout0IsFaster) {
  112. const pending = [];
  113. globals.addEventListener('message', (e) => {
  114. if (e.data && e.data.vscodeScheduleAsyncWork) {
  115. for (let i = 0, len = pending.length; i < len; i++) {
  116. const candidate = pending[i];
  117. if (candidate.id === e.data.vscodeScheduleAsyncWork) {
  118. pending.splice(i, 1);
  119. candidate.callback();
  120. return;
  121. }
  122. }
  123. }
  124. });
  125. let lastId = 0;
  126. return (callback) => {
  127. const myId = ++lastId;
  128. pending.push({
  129. id: myId,
  130. callback: callback
  131. });
  132. globals.postMessage({ vscodeScheduleAsyncWork: myId }, '*');
  133. };
  134. }
  135. return (callback) => setTimeout(callback);
  136. })();
  137. export const OS = (_isMacintosh || _isIOS ? 2 /* OperatingSystem.Macintosh */ : (_isWindows ? 1 /* OperatingSystem.Windows */ : 3 /* OperatingSystem.Linux */));
  138. let _isLittleEndian = true;
  139. let _isLittleEndianComputed = false;
  140. export function isLittleEndian() {
  141. if (!_isLittleEndianComputed) {
  142. _isLittleEndianComputed = true;
  143. const test = new Uint8Array(2);
  144. test[0] = 1;
  145. test[1] = 2;
  146. const view = new Uint16Array(test.buffer);
  147. _isLittleEndian = (view[0] === (2 << 8) + 1);
  148. }
  149. return _isLittleEndian;
  150. }
  151. export const isChrome = !!(userAgent && userAgent.indexOf('Chrome') >= 0);
  152. export const isFirefox = !!(userAgent && userAgent.indexOf('Firefox') >= 0);
  153. export const isSafari = !!(!isChrome && (userAgent && userAgent.indexOf('Safari') >= 0));
  154. export const isEdge = !!(userAgent && userAgent.indexOf('Edg/') >= 0);
  155. export const isAndroid = !!(userAgent && userAgent.indexOf('Android') >= 0);