sandbox - consolidate fileUriFromPath into one

This commit is contained in:
Benjamin Pasero 2020-09-22 08:14:45 +02:00
parent 7ebeea736f
commit 5497e60ed2
4 changed files with 48 additions and 46 deletions

View file

@ -14,7 +14,7 @@ const nlsConfig = bootstrap.setupNLS();
// Bootstrap: Loader // Bootstrap: Loader
loader.config({ loader.config({
baseUrl: bootstrap.fileUriFromPath(__dirname), baseUrl: bootstrap.fileUriFromPath(__dirname, process.platform === 'win32'),
catchError: true, catchError: true,
nodeRequire: require, nodeRequire: require,
nodeMain: __filename, nodeMain: __filename,

View file

@ -21,6 +21,7 @@
globalThis.MonacoBootstrapWindow = factory(); globalThis.MonacoBootstrapWindow = factory();
} }
}(this, function () { }(this, function () {
const bootstrapLib = bootstrap();
const preloadGlobals = globals(); const preloadGlobals = globals();
const sandbox = preloadGlobals.context.sandbox; const sandbox = preloadGlobals.context.sandbox;
const webFrame = preloadGlobals.webFrame; const webFrame = preloadGlobals.webFrame;
@ -89,7 +90,7 @@
window.document.documentElement.setAttribute('lang', locale); 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) { if (!sandbox) {
window['define'] = undefined; window['define'] = undefined;
} }
@ -102,8 +103,8 @@
window['MonacoEnvironment'] = {}; window['MonacoEnvironment'] = {};
const loaderConfig = { const loaderConfig = {
baseUrl: `${uriFromPath(configuration.appRoot)}/out`, baseUrl: `${bootstrapLib.fileUriFromPath(configuration.appRoot, safeProcess.platform === 'win32')}/out`,
'vs/nls': nlsConfig, 'vs/nls': nlsConfig
}; };
if (sandbox) { 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')} * @return {typeof import('./vs/base/parts/sandbox/electron-sandbox/globals')}
*/ */
@ -243,31 +252,6 @@
return window.vscode; 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 { return {
load, load,
globals globals

50
src/bootstrap.js vendored
View file

@ -16,16 +16,12 @@
// Browser // Browser
else { else {
try { globalThis.MonacoBootstrap = factory();
globalThis.MonacoBootstrap = factory();
} catch (error) {
console.warn(error); // expected when e.g. running with sandbox: true (TODO@sandbox eventually consolidate this)
}
} }
}(this, function () { }(this, function () {
const Module = require('module'); const Module = typeof require === 'function' ? require('module') : undefined;
const path = require('path'); const path = typeof require === 'function' ? require('path') : undefined;
const fs = require('fs'); const fs = typeof require === 'function' ? require('fs') : undefined;
//#region global bootstrapping //#region global bootstrapping
@ -34,9 +30,11 @@
// Workaround for Electron not installing a handler to ignore SIGPIPE // Workaround for Electron not installing a handler to ignore SIGPIPE
// (https://github.com/electron/electron/issues/13254) // (https://github.com/electron/electron/issues/13254)
process.on('SIGPIPE', () => { if (typeof process !== 'undefined') {
console.error(new Error('Unexpected SIGPIPE')); process.on('SIGPIPE', () => {
}); console.error(new Error('Unexpected SIGPIPE'));
});
}
//#endregion //#endregion
@ -47,6 +45,11 @@
* @param {string} appRoot * @param {string} appRoot
*/ */
function enableASARSupport(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; let NODE_MODULES_PATH = appRoot ? path.join(appRoot, 'node_modules') : undefined;
if (!NODE_MODULES_PATH) { if (!NODE_MODULES_PATH) {
NODE_MODULES_PATH = path.join(__dirname, '../node_modules'); NODE_MODULES_PATH = path.join(__dirname, '../node_modules');
@ -84,18 +87,19 @@
//#region URI helpers //#region URI helpers
/** /**
* @param {string} _path * @param {string} path
* @param {boolean} isWindows
* @returns {string} * @returns {string}
*/ */
function fileUriFromPath(_path) { function fileUriFromPath(path, isWindows) {
let pathName = path.resolve(_path).replace(/\\/g, '/'); let pathName = path.replace(/\\/g, '/');
if (pathName.length > 0 && pathName.charAt(0) !== '/') { if (pathName.length > 0 && pathName.charAt(0) !== '/') {
pathName = `/${pathName}`; pathName = `/${pathName}`;
} }
/** @type {string} */ /** @type {string} */
let uri; 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}`); uri = encodeURI(`file:${pathName}`);
} else { } else {
uri = encodeURI(`file://${pathName}`); uri = encodeURI(`file://${pathName}`);
@ -113,6 +117,10 @@
* @returns {{locale?: string, availableLanguages: {[lang: string]: string;}, pseudo?: boolean }} * @returns {{locale?: string, availableLanguages: {[lang: string]: string;}, pseudo?: boolean }}
*/ */
function setupNLS() { 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. // Get the nls configuration into the process.env as early as possible.
let nlsConfig = { availableLanguages: {} }; let nlsConfig = { availableLanguages: {} };
@ -163,9 +171,14 @@
/** /**
* @param {{ portable: string; applicationName: string; }} product * @param {{ portable: string; applicationName: string; }} product
* @returns {{portableDataPath: string;isPortable: boolean;}} * @returns {{ portableDataPath: string; isPortable: boolean; }}
*/ */
function configurePortable(product) { function configurePortable(product) {
if (!path || !fs) {
console.warn('configurePortable() is only available in node.js environments');
return;
}
const appRoot = path.dirname(__dirname); const appRoot = path.dirname(__dirname);
function getApplicationPath() { function getApplicationPath() {
@ -228,6 +241,11 @@
// Prevents appinsights from monkey patching modules. // Prevents appinsights from monkey patching modules.
// This should be called before importing the applicationinsights module // This should be called before importing the applicationinsights module
function avoidMonkeyPatchFromAppInsights() { function avoidMonkeyPatchFromAppInsights() {
if (typeof process === 'undefined') {
console.warn('avoidMonkeyPatchFromAppInsights() is only available in node.js environments');
return;
}
// @ts-ignore // @ts-ignore
process.env['APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL'] = true; // Skip monkey patching of 3rd party modules by appinsights 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 global['diagnosticsSource'] = {}; // Prevents diagnostic channel (which patches "require") from initializing entirely

View file

@ -32,7 +32,7 @@ function initLoader(opts) {
nodeRequire: require, nodeRequire: require,
nodeMain: __filename, nodeMain: __filename,
catchError: true, catchError: true,
baseUrl: bootstrap.fileUriFromPath(path.join(__dirname, '../../../src')), baseUrl: bootstrap.fileUriFromPath(path.join(__dirname, '../../../src'), process.platform === 'win32'),
paths: { paths: {
'vs': `../${outdir}/vs`, 'vs': `../${outdir}/vs`,
'lib': `../${outdir}/lib`, 'lib': `../${outdir}/lib`,