From 5497e60ed2cde7565766dea5151d0972159d23bc Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Tue, 22 Sep 2020 08:14:45 +0200 Subject: [PATCH] sandbox - consolidate fileUriFromPath into one --- src/bootstrap-amd.js | 2 +- src/bootstrap-window.js | 40 ++++++++------------------- src/bootstrap.js | 50 +++++++++++++++++++++++----------- test/unit/electron/renderer.js | 2 +- 4 files changed, 48 insertions(+), 46 deletions(-) diff --git a/src/bootstrap-amd.js b/src/bootstrap-amd.js index b0b266557a6..8b0ec252d25 100644 --- a/src/bootstrap-amd.js +++ b/src/bootstrap-amd.js @@ -14,7 +14,7 @@ const nlsConfig = bootstrap.setupNLS(); // Bootstrap: Loader loader.config({ - baseUrl: bootstrap.fileUriFromPath(__dirname), + baseUrl: bootstrap.fileUriFromPath(__dirname, process.platform === 'win32'), catchError: true, nodeRequire: require, nodeMain: __filename, diff --git a/src/bootstrap-window.js b/src/bootstrap-window.js index 436f7b50758..133587e4aae 100644 --- a/src/bootstrap-window.js +++ b/src/bootstrap-window.js @@ -21,6 +21,7 @@ globalThis.MonacoBootstrapWindow = factory(); } }(this, function () { + const bootstrapLib = bootstrap(); const preloadGlobals = globals(); const sandbox = preloadGlobals.context.sandbox; const webFrame = preloadGlobals.webFrame; @@ -89,7 +90,7 @@ window.document.documentElement.setAttribute('lang', locale); - // do not advertise AMD to avoid confusing UMD modules loaded with nodejs (TODO@sandbox non-sandboxed only) + // do not advertise AMD to avoid confusing UMD modules loaded with nodejs if (!sandbox) { window['define'] = undefined; } @@ -102,8 +103,8 @@ window['MonacoEnvironment'] = {}; const loaderConfig = { - baseUrl: `${uriFromPath(configuration.appRoot)}/out`, - 'vs/nls': nlsConfig, + baseUrl: `${bootstrapLib.fileUriFromPath(configuration.appRoot, safeProcess.platform === 'win32')}/out`, + 'vs/nls': nlsConfig }; if (sandbox) { @@ -235,6 +236,14 @@ } } + /** + * @return {{ fileUriFromPath: (path: string, isWindows: boolean) => string; }} + */ + function bootstrap() { + // @ts-ignore (defined in bootstrap.js) + return window.MonacoBootstrap; + } + /** * @return {typeof import('./vs/base/parts/sandbox/electron-sandbox/globals')} */ @@ -243,31 +252,6 @@ return window.vscode; } - /** - * TODO@sandbox this should not use the file:// protocol at all - * and be consolidated with the fileUriFromPath() method in - * bootstrap.js. - * - * @param {string} path - * @returns {string} - */ - function uriFromPath(path) { - let pathName = path.replace(/\\/g, '/'); - if (pathName.length > 0 && pathName.charAt(0) !== '/') { - pathName = `/${pathName}`; - } - - /** @type {string} */ - let uri; - if (safeProcess.platform === 'win32' && pathName.startsWith('//')) { // specially handle Windows UNC paths - uri = encodeURI(`file:${pathName}`); - } else { - uri = encodeURI(`file://${pathName}`); - } - - return uri.replace(/#/g, '%23'); - } - return { load, globals diff --git a/src/bootstrap.js b/src/bootstrap.js index d1abd5502df..58c6103d9be 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -16,16 +16,12 @@ // Browser else { - try { - globalThis.MonacoBootstrap = factory(); - } catch (error) { - console.warn(error); // expected when e.g. running with sandbox: true (TODO@sandbox eventually consolidate this) - } + globalThis.MonacoBootstrap = factory(); } }(this, function () { - const Module = require('module'); - const path = require('path'); - const fs = require('fs'); + const Module = typeof require === 'function' ? require('module') : undefined; + const path = typeof require === 'function' ? require('path') : undefined; + const fs = typeof require === 'function' ? require('fs') : undefined; //#region global bootstrapping @@ -34,9 +30,11 @@ // Workaround for Electron not installing a handler to ignore SIGPIPE // (https://github.com/electron/electron/issues/13254) - process.on('SIGPIPE', () => { - console.error(new Error('Unexpected SIGPIPE')); - }); + if (typeof process !== 'undefined') { + process.on('SIGPIPE', () => { + console.error(new Error('Unexpected SIGPIPE')); + }); + } //#endregion @@ -47,6 +45,11 @@ * @param {string} appRoot */ function enableASARSupport(appRoot) { + if (!path || !Module) { + console.warn('enableASARSupport() is only available in node.js environments'); + return; + } + let NODE_MODULES_PATH = appRoot ? path.join(appRoot, 'node_modules') : undefined; if (!NODE_MODULES_PATH) { NODE_MODULES_PATH = path.join(__dirname, '../node_modules'); @@ -84,18 +87,19 @@ //#region URI helpers /** - * @param {string} _path + * @param {string} path + * @param {boolean} isWindows * @returns {string} */ - function fileUriFromPath(_path) { - let pathName = path.resolve(_path).replace(/\\/g, '/'); + function fileUriFromPath(path, isWindows) { + let pathName = path.replace(/\\/g, '/'); if (pathName.length > 0 && pathName.charAt(0) !== '/') { pathName = `/${pathName}`; } /** @type {string} */ let uri; - if (process.platform === 'win32' && pathName.startsWith('//')) { // specially handle Windows UNC paths + if (isWindows && pathName.startsWith('//')) { // specially handle Windows UNC paths uri = encodeURI(`file:${pathName}`); } else { uri = encodeURI(`file://${pathName}`); @@ -113,6 +117,10 @@ * @returns {{locale?: string, availableLanguages: {[lang: string]: string;}, pseudo?: boolean }} */ function setupNLS() { + if (!path || !fs) { + console.warn('setupNLS() is only available in node.js environments'); + return; + } // Get the nls configuration into the process.env as early as possible. let nlsConfig = { availableLanguages: {} }; @@ -163,9 +171,14 @@ /** * @param {{ portable: string; applicationName: string; }} product - * @returns {{portableDataPath: string;isPortable: boolean;}} + * @returns {{ portableDataPath: string; isPortable: boolean; }} */ function configurePortable(product) { + if (!path || !fs) { + console.warn('configurePortable() is only available in node.js environments'); + return; + } + const appRoot = path.dirname(__dirname); function getApplicationPath() { @@ -228,6 +241,11 @@ // Prevents appinsights from monkey patching modules. // This should be called before importing the applicationinsights module function avoidMonkeyPatchFromAppInsights() { + if (typeof process === 'undefined') { + console.warn('avoidMonkeyPatchFromAppInsights() is only available in node.js environments'); + return; + } + // @ts-ignore process.env['APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL'] = true; // Skip monkey patching of 3rd party modules by appinsights global['diagnosticsSource'] = {}; // Prevents diagnostic channel (which patches "require") from initializing entirely diff --git a/test/unit/electron/renderer.js b/test/unit/electron/renderer.js index e2a4b435690..3f29ac6cfa8 100644 --- a/test/unit/electron/renderer.js +++ b/test/unit/electron/renderer.js @@ -32,7 +32,7 @@ function initLoader(opts) { nodeRequire: require, nodeMain: __filename, catchError: true, - baseUrl: bootstrap.fileUriFromPath(path.join(__dirname, '../../../src')), + baseUrl: bootstrap.fileUriFromPath(path.join(__dirname, '../../../src'), process.platform === 'win32'), paths: { 'vs': `../${outdir}/vs`, 'lib': `../${outdir}/lib`,