bootstrap.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. //@ts-check
  6. 'use strict';
  7. // Simple module style to support node.js and browser environments
  8. (function (globalThis, factory) {
  9. // Node.js
  10. if (typeof exports === 'object') {
  11. module.exports = factory();
  12. }
  13. // Browser
  14. else {
  15. // @ts-ignore
  16. globalThis.MonacoBootstrap = factory();
  17. }
  18. }(this, function () {
  19. const Module = typeof require === 'function' ? require('module') : undefined;
  20. const path = typeof require === 'function' ? require('path') : undefined;
  21. const fs = typeof require === 'function' ? require('fs') : undefined;
  22. const util = typeof require === 'function' ? require('util') : undefined;
  23. //#region global bootstrapping
  24. // increase number of stack frames(from 10, https://github.com/v8/v8/wiki/Stack-Trace-API)
  25. Error.stackTraceLimit = 100;
  26. if (typeof process !== 'undefined' && !process.env['VSCODE_HANDLES_SIGPIPE']) {
  27. // Workaround for Electron not installing a handler to ignore SIGPIPE
  28. // (https://github.com/electron/electron/issues/13254)
  29. let didLogAboutSIGPIPE = false;
  30. process.on('SIGPIPE', () => {
  31. // See https://github.com/microsoft/vscode-remote-release/issues/6543
  32. // We would normally install a SIGPIPE listener in bootstrap.js
  33. // But in certain situations, the console itself can be in a broken pipe state
  34. // so logging SIGPIPE to the console will cause an infinite async loop
  35. if (!didLogAboutSIGPIPE) {
  36. didLogAboutSIGPIPE = true;
  37. console.error(new Error(`Unexpected SIGPIPE`));
  38. }
  39. });
  40. }
  41. //#endregion
  42. //#region Add support for using node_modules.asar
  43. function enableASARSupport() {
  44. if (!path || !Module || typeof process === 'undefined') {
  45. console.warn('enableASARSupport() is only available in node.js environments');
  46. return;
  47. }
  48. const NODE_MODULES_PATH = path.join(__dirname, '../node_modules');
  49. const NODE_MODULES_ASAR_PATH = `${NODE_MODULES_PATH}.asar`;
  50. // @ts-ignore
  51. const originalResolveLookupPaths = Module._resolveLookupPaths;
  52. // @ts-ignore
  53. Module._resolveLookupPaths = function (request, parent) {
  54. const paths = originalResolveLookupPaths(request, parent);
  55. if (Array.isArray(paths)) {
  56. for (let i = 0, len = paths.length; i < len; i++) {
  57. if (paths[i] === NODE_MODULES_PATH) {
  58. paths.splice(i, 0, NODE_MODULES_ASAR_PATH);
  59. break;
  60. }
  61. }
  62. }
  63. return paths;
  64. };
  65. }
  66. //#endregion
  67. //#region URI helpers
  68. /**
  69. * @param {string} path
  70. * @param {{ isWindows?: boolean, scheme?: string, fallbackAuthority?: string }} config
  71. * @returns {string}
  72. */
  73. function fileUriFromPath(path, config) {
  74. // Since we are building a URI, we normalize any backslash
  75. // to slashes and we ensure that the path begins with a '/'.
  76. let pathName = path.replace(/\\/g, '/');
  77. if (pathName.length > 0 && pathName.charAt(0) !== '/') {
  78. pathName = `/${pathName}`;
  79. }
  80. /** @type {string} */
  81. let uri;
  82. // Windows: in order to support UNC paths (which start with '//')
  83. // that have their own authority, we do not use the provided authority
  84. // but rather preserve it.
  85. if (config.isWindows && pathName.startsWith('//')) {
  86. uri = encodeURI(`${config.scheme || 'file'}:${pathName}`);
  87. }
  88. // Otherwise we optionally add the provided authority if specified
  89. else {
  90. uri = encodeURI(`${config.scheme || 'file'}://${config.fallbackAuthority || ''}${pathName}`);
  91. }
  92. return uri.replace(/#/g, '%23');
  93. }
  94. //#endregion
  95. //#region NLS helpers
  96. /**
  97. * @returns {{locale?: string, availableLanguages: {[lang: string]: string;}, pseudo?: boolean } | undefined}
  98. */
  99. function setupNLS() {
  100. // Get the nls configuration as early as possible.
  101. const process = safeProcess();
  102. /** @type {{ availableLanguages: {}; loadBundle?: (bundle: string, language: string, cb: (err: Error | undefined, result: string | undefined) => void) => void; _resolvedLanguagePackCoreLocation?: string; _corruptedFile?: string }} */
  103. let nlsConfig = { availableLanguages: {} };
  104. if (process && process.env['VSCODE_NLS_CONFIG']) {
  105. try {
  106. nlsConfig = JSON.parse(process.env['VSCODE_NLS_CONFIG']);
  107. } catch (e) {
  108. // Ignore
  109. }
  110. }
  111. if (nlsConfig._resolvedLanguagePackCoreLocation) {
  112. const bundles = Object.create(null);
  113. /**
  114. * @param {string} bundle
  115. * @param {string} language
  116. * @param {(err: Error | undefined, result: string | undefined) => void} cb
  117. */
  118. nlsConfig.loadBundle = function (bundle, language, cb) {
  119. const result = bundles[bundle];
  120. if (result) {
  121. cb(undefined, result);
  122. return;
  123. }
  124. // @ts-ignore
  125. safeReadNlsFile(nlsConfig._resolvedLanguagePackCoreLocation, `${bundle.replace(/\//g, '!')}.nls.json`).then(function (content) {
  126. const json = JSON.parse(content);
  127. bundles[bundle] = json;
  128. cb(undefined, json);
  129. }).catch((error) => {
  130. try {
  131. if (nlsConfig._corruptedFile) {
  132. safeWriteNlsFile(nlsConfig._corruptedFile, 'corrupted').catch(function (error) { console.error(error); });
  133. }
  134. } finally {
  135. cb(error, undefined);
  136. }
  137. });
  138. };
  139. }
  140. return nlsConfig;
  141. }
  142. /**
  143. * @returns {typeof import('./vs/base/parts/sandbox/electron-sandbox/globals') | undefined}
  144. */
  145. function safeSandboxGlobals() {
  146. const globals = (typeof self === 'object' ? self : typeof global === 'object' ? global : {});
  147. // @ts-ignore
  148. return globals.vscode;
  149. }
  150. /**
  151. * @returns {import('./vs/base/parts/sandbox/electron-sandbox/globals').ISandboxNodeProcess | NodeJS.Process | undefined}
  152. */
  153. function safeProcess() {
  154. const sandboxGlobals = safeSandboxGlobals();
  155. if (sandboxGlobals) {
  156. return sandboxGlobals.process; // Native environment (sandboxed)
  157. }
  158. if (typeof process !== 'undefined') {
  159. return process; // Native environment (non-sandboxed)
  160. }
  161. return undefined;
  162. }
  163. /**
  164. * @returns {import('./vs/base/parts/sandbox/electron-sandbox/electronTypes').IpcRenderer | undefined}
  165. */
  166. function safeIpcRenderer() {
  167. const sandboxGlobals = safeSandboxGlobals();
  168. if (sandboxGlobals) {
  169. return sandboxGlobals.ipcRenderer;
  170. }
  171. return undefined;
  172. }
  173. /**
  174. * @param {string[]} pathSegments
  175. * @returns {Promise<string>}
  176. */
  177. async function safeReadNlsFile(...pathSegments) {
  178. const ipcRenderer = safeIpcRenderer();
  179. if (ipcRenderer) {
  180. return ipcRenderer.invoke('vscode:readNlsFile', ...pathSegments);
  181. }
  182. if (fs && path && util) {
  183. return (await util.promisify(fs.readFile)(path.join(...pathSegments))).toString();
  184. }
  185. throw new Error('Unsupported operation (read NLS files)');
  186. }
  187. /**
  188. * @param {string} path
  189. * @param {string} content
  190. * @returns {Promise<void>}
  191. */
  192. function safeWriteNlsFile(path, content) {
  193. const ipcRenderer = safeIpcRenderer();
  194. if (ipcRenderer) {
  195. return ipcRenderer.invoke('vscode:writeNlsFile', path, content);
  196. }
  197. if (fs && util) {
  198. return util.promisify(fs.writeFile)(path, content);
  199. }
  200. throw new Error('Unsupported operation (write NLS files)');
  201. }
  202. //#endregion
  203. return {
  204. enableASARSupport,
  205. setupNLS,
  206. fileUriFromPath
  207. };
  208. }));