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
loader.config({
baseUrl: bootstrap.fileUriFromPath(__dirname),
baseUrl: bootstrap.fileUriFromPath(__dirname, process.platform === 'win32'),
catchError: true,
nodeRequire: require,
nodeMain: __filename,

View file

@ -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

50
src/bootstrap.js vendored
View file

@ -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

View file

@ -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`,