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();
}
}(this, function () {
const bootstrapLib = bootstrap();
const preloadGlobals = globals();
const sandbox = preloadGlobals.context.sandbox;
const webFrame = preloadGlobals.webFrame;
@@ -56,15 +57,22 @@
developerToolsUnbind = registerDeveloperKeybindings(options && options.disallowReloadKeybinding);
}
// Enable ASAR support
globalThis.MonacoBootstrap.enableASARSupport(configuration.appRoot);
// Correctly inherit the parent's environment (TODO@sandbox non-sandboxed only)
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') {
options.canModifyDOM(configuration);
}
// Get the nls configuration into the process.env as early as possible
const nlsConfig = globalThis.MonacoBootstrap.setupNLS();
// Get the nls configuration into the process.env as early as possible (TODO@sandbox non-sandboxed only)
const nlsConfig = sandbox ? { availableLanguages: {} } : globalThis.MonacoBootstrap.setupNLS();
let locale = nlsConfig.availableLanguages['*'] || 'en';
if (locale === 'zh-tw') {
@@ -87,16 +95,27 @@
window['MonacoEnvironment'] = {};
// const baseUrl = sandbox ? // {{SQL CARBON EDIT}} Pending changes?
// `${bootstrapLib.fileUriFromPath(configuration.appRoot, { isWindows: safeProcess.platform === 'win32', scheme: 'vscode-file', fallbackAuthority: 'vscode-app' })}/out` :
// `${bootstrapLib.fileUriFromPath(configuration.appRoot, { isWindows: safeProcess.platform === 'win32' })}/out`;
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' })}/out`;
const loaderConfig = {
baseUrl: `${uriFromPath(configuration.appRoot)}/out`,
baseUrl: baseUrl,
'vs/nls': nlsConfig,
amdModulesPattern: /^(vs|sql)\//, // {{SQL CARBON EDIT}} include sql in regex
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
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')}
*/

39
src/bootstrap.js vendored
View File

@@ -23,9 +23,9 @@
}
}
}(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 +34,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
@@ -89,21 +91,32 @@
//#region URI helpers
/**
* @param {string} _path
* @param {string} path
* @param {{ isWindows?: boolean, scheme?: string, fallbackAuthority?: string }} config
* @returns {string}
*/
function fileUriFromPath(_path) {
let pathName = path.resolve(_path).replace(/\\/g, '/');
function fileUriFromPath(path, config) {
// 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) !== '/') {
pathName = `/${pathName}`;
}
/** @type {string} */
let uri;
if (process.platform === 'win32' && pathName.startsWith('//')) { // specially handle Windows UNC paths
uri = encodeURI(`file:${pathName}`);
} else {
uri = encodeURI(`file://${pathName}`);
// Windows: in order to support UNC paths (which start with '//')
// that have their own authority, we do not use the provided authority
// 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');