platform.js 6.0 KB

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