Fix script loading in sandbox windows (#14727)

* Fix script loading in sandbox windows

* Avoid uglify error
This commit is contained in:
Karl Burtram
2021-03-15 11:41:41 -07:00
committed by GitHub
parent 4140d0da79
commit 05d88f4971
2 changed files with 61 additions and 21 deletions

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;
@@ -56,15 +57,22 @@
developerToolsUnbind = registerDeveloperKeybindings(options && options.disallowReloadKeybinding); developerToolsUnbind = registerDeveloperKeybindings(options && options.disallowReloadKeybinding);
} }
// Enable ASAR support // Correctly inherit the parent's environment (TODO@sandbox non-sandboxed only)
globalThis.MonacoBootstrap.enableASARSupport(configuration.appRoot); if (!sandbox) {
Object.assign(safeProcess.env, configuration.userEnv);
}
// Enable ASAR support (TODO@sandbox non-sandboxed only)
if (!sandbox) {
globalThis.MonacoBootstrap.enableASARSupport(configuration.appRoot);
}
if (options && typeof options.canModifyDOM === 'function') { if (options && typeof options.canModifyDOM === 'function') {
options.canModifyDOM(configuration); options.canModifyDOM(configuration);
} }
// Get the nls configuration into the process.env as early as possible // Get the nls configuration into the process.env as early as possible (TODO@sandbox non-sandboxed only)
const nlsConfig = globalThis.MonacoBootstrap.setupNLS(); const nlsConfig = sandbox ? { availableLanguages: {} } : globalThis.MonacoBootstrap.setupNLS();
let locale = nlsConfig.availableLanguages['*'] || 'en'; let locale = nlsConfig.availableLanguages['*'] || 'en';
if (locale === 'zh-tw') { if (locale === 'zh-tw') {
@@ -87,16 +95,27 @@
window['MonacoEnvironment'] = {}; window['MonacoEnvironment'] = {};
// const baseUrl = sandbox ? // {{SQL CARBON EDIT}} Pending changes? const baseUrl = sandbox ?
// `${bootstrapLib.fileUriFromPath(configuration.appRoot, { isWindows: safeProcess.platform === 'win32', scheme: 'vscode-file', fallbackAuthority: 'vscode-app' })}/out` : `${bootstrapLib.fileUriFromPath(configuration.appRoot, { isWindows: safeProcess.platform === 'win32', scheme: 'vscode-file', fallbackAuthority: 'vscode-app' })}/out` :
// `${bootstrapLib.fileUriFromPath(configuration.appRoot, { isWindows: safeProcess.platform === 'win32' })}/out`; `${bootstrapLib.fileUriFromPath(configuration.appRoot, { isWindows: safeProcess.platform === 'win32' })}/out`;
const loaderConfig = { const loaderConfig = {
baseUrl: `${uriFromPath(configuration.appRoot)}/out`, baseUrl: baseUrl,
'vs/nls': nlsConfig, 'vs/nls': nlsConfig,
amdModulesPattern: /^(vs|sql)\//, // {{SQL CARBON EDIT}} include sql in regex amdModulesPattern: /^(vs|sql)\//, // {{SQL CARBON EDIT}} include sql in regex
preferScriptTags: sandbox preferScriptTags: sandbox
}; };
// use a trusted types policy when loading via script tags
if (loaderConfig.preferScriptTags && window && window.trustedTypes) { // {{SQL CARBON EDIT}} fix uglify error
loaderConfig.trustedTypesPolicy = window.trustedTypes.createPolicy('amdLoader', {
createScriptURL(value) {
if (value.startsWith(window.location.origin)) {
return value;
}
throw new Error(`Invalid script url: ${value}`);
}
});
}
// cached data config // cached data config
if (configuration.nodeCachedDataDir) { if (configuration.nodeCachedDataDir) {
@@ -228,6 +247,14 @@
} }
} }
/**
* @return {{ fileUriFromPath: (path: string, config: { isWindows?: boolean, scheme?: string, fallbackAuthority?: string }) => string; }}
*/
function bootstrap() {
// @ts-ignore (defined in bootstrap.js)
return globalThis.MonacoBootstrap;
}
/** /**
* @return {typeof import('./vs/base/parts/sandbox/electron-sandbox/globals')} * @return {typeof import('./vs/base/parts/sandbox/electron-sandbox/globals')}
*/ */

39
src/bootstrap.js vendored
View File

@@ -23,9 +23,9 @@
} }
} }
}(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 +34,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
@@ -89,21 +91,32 @@
//#region URI helpers //#region URI helpers
/** /**
* @param {string} _path * @param {string} path
* @param {{ isWindows?: boolean, scheme?: string, fallbackAuthority?: string }} config
* @returns {string} * @returns {string}
*/ */
function fileUriFromPath(_path) { function fileUriFromPath(path, config) {
let pathName = path.resolve(_path).replace(/\\/g, '/');
// Since we are building a URI, we normalize any backlsash
// to slashes and we ensure that the path begins with a '/'.
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
uri = encodeURI(`file:${pathName}`); // Windows: in order to support UNC paths (which start with '//')
} else { // that have their own authority, we do not use the provided authority
uri = encodeURI(`file://${pathName}`); // but rather preserve it.
if (config.isWindows && pathName.startsWith('//')) {
uri = encodeURI(`${config.scheme || 'file'}:${pathName}`);
}
// Otherwise we optionally add the provided authority if specified
else {
uri = encodeURI(`${config.scheme || 'file'}://${config.fallbackAuthority || ''}${pathName}`);
} }
return uri.replace(/#/g, '%23'); return uri.replace(/#/g, '%23');