Skip to content

Commit 5497e60

Browse files
author
Benjamin Pasero
committed
sandbox - consolidate fileUriFromPath into one
1 parent 7ebeea7 commit 5497e60

4 files changed

Lines changed: 48 additions & 46 deletions

File tree

src/bootstrap-amd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const nlsConfig = bootstrap.setupNLS();
1414

1515
// Bootstrap: Loader
1616
loader.config({
17-
baseUrl: bootstrap.fileUriFromPath(__dirname),
17+
baseUrl: bootstrap.fileUriFromPath(__dirname, process.platform === 'win32'),
1818
catchError: true,
1919
nodeRequire: require,
2020
nodeMain: __filename,

src/bootstrap-window.js

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
globalThis.MonacoBootstrapWindow = factory();
2222
}
2323
}(this, function () {
24+
const bootstrapLib = bootstrap();
2425
const preloadGlobals = globals();
2526
const sandbox = preloadGlobals.context.sandbox;
2627
const webFrame = preloadGlobals.webFrame;
@@ -89,7 +90,7 @@
8990

9091
window.document.documentElement.setAttribute('lang', locale);
9192

92-
// do not advertise AMD to avoid confusing UMD modules loaded with nodejs (TODO@sandbox non-sandboxed only)
93+
// do not advertise AMD to avoid confusing UMD modules loaded with nodejs
9394
if (!sandbox) {
9495
window['define'] = undefined;
9596
}
@@ -102,8 +103,8 @@
102103
window['MonacoEnvironment'] = {};
103104

104105
const loaderConfig = {
105-
baseUrl: `${uriFromPath(configuration.appRoot)}/out`,
106-
'vs/nls': nlsConfig,
106+
baseUrl: `${bootstrapLib.fileUriFromPath(configuration.appRoot, safeProcess.platform === 'win32')}/out`,
107+
'vs/nls': nlsConfig
107108
};
108109

109110
if (sandbox) {
@@ -236,36 +237,19 @@
236237
}
237238

238239
/**
239-
* @return {typeof import('./vs/base/parts/sandbox/electron-sandbox/globals')}
240+
* @return {{ fileUriFromPath: (path: string, isWindows: boolean) => string; }}
240241
*/
241-
function globals() {
242-
// @ts-ignore (defined in globals.js)
243-
return window.vscode;
242+
function bootstrap() {
243+
// @ts-ignore (defined in bootstrap.js)
244+
return window.MonacoBootstrap;
244245
}
245246

246247
/**
247-
* TODO@sandbox this should not use the file:// protocol at all
248-
* and be consolidated with the fileUriFromPath() method in
249-
* bootstrap.js.
250-
*
251-
* @param {string} path
252-
* @returns {string}
248+
* @return {typeof import('./vs/base/parts/sandbox/electron-sandbox/globals')}
253249
*/
254-
function uriFromPath(path) {
255-
let pathName = path.replace(/\\/g, '/');
256-
if (pathName.length > 0 && pathName.charAt(0) !== '/') {
257-
pathName = `/${pathName}`;
258-
}
259-
260-
/** @type {string} */
261-
let uri;
262-
if (safeProcess.platform === 'win32' && pathName.startsWith('//')) { // specially handle Windows UNC paths
263-
uri = encodeURI(`file:${pathName}`);
264-
} else {
265-
uri = encodeURI(`file://${pathName}`);
266-
}
267-
268-
return uri.replace(/#/g, '%23');
250+
function globals() {
251+
// @ts-ignore (defined in globals.js)
252+
return window.vscode;
269253
}
270254

271255
return {

src/bootstrap.js

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,12 @@
1616

1717
// Browser
1818
else {
19-
try {
20-
globalThis.MonacoBootstrap = factory();
21-
} catch (error) {
22-
console.warn(error); // expected when e.g. running with sandbox: true (TODO@sandbox eventually consolidate this)
23-
}
19+
globalThis.MonacoBootstrap = factory();
2420
}
2521
}(this, function () {
26-
const Module = require('module');
27-
const path = require('path');
28-
const fs = require('fs');
22+
const Module = typeof require === 'function' ? require('module') : undefined;
23+
const path = typeof require === 'function' ? require('path') : undefined;
24+
const fs = typeof require === 'function' ? require('fs') : undefined;
2925

3026
//#region global bootstrapping
3127

@@ -34,9 +30,11 @@
3430

3531
// Workaround for Electron not installing a handler to ignore SIGPIPE
3632
// (https://github.com/electron/electron/issues/13254)
37-
process.on('SIGPIPE', () => {
38-
console.error(new Error('Unexpected SIGPIPE'));
39-
});
33+
if (typeof process !== 'undefined') {
34+
process.on('SIGPIPE', () => {
35+
console.error(new Error('Unexpected SIGPIPE'));
36+
});
37+
}
4038

4139
//#endregion
4240

@@ -47,6 +45,11 @@
4745
* @param {string} appRoot
4846
*/
4947
function enableASARSupport(appRoot) {
48+
if (!path || !Module) {
49+
console.warn('enableASARSupport() is only available in node.js environments');
50+
return;
51+
}
52+
5053
let NODE_MODULES_PATH = appRoot ? path.join(appRoot, 'node_modules') : undefined;
5154
if (!NODE_MODULES_PATH) {
5255
NODE_MODULES_PATH = path.join(__dirname, '../node_modules');
@@ -84,18 +87,19 @@
8487
//#region URI helpers
8588

8689
/**
87-
* @param {string} _path
90+
* @param {string} path
91+
* @param {boolean} isWindows
8892
* @returns {string}
8993
*/
90-
function fileUriFromPath(_path) {
91-
let pathName = path.resolve(_path).replace(/\\/g, '/');
94+
function fileUriFromPath(path, isWindows) {
95+
let pathName = path.replace(/\\/g, '/');
9296
if (pathName.length > 0 && pathName.charAt(0) !== '/') {
9397
pathName = `/${pathName}`;
9498
}
9599

96100
/** @type {string} */
97101
let uri;
98-
if (process.platform === 'win32' && pathName.startsWith('//')) { // specially handle Windows UNC paths
102+
if (isWindows && pathName.startsWith('//')) { // specially handle Windows UNC paths
99103
uri = encodeURI(`file:${pathName}`);
100104
} else {
101105
uri = encodeURI(`file://${pathName}`);
@@ -113,6 +117,10 @@
113117
* @returns {{locale?: string, availableLanguages: {[lang: string]: string;}, pseudo?: boolean }}
114118
*/
115119
function setupNLS() {
120+
if (!path || !fs) {
121+
console.warn('setupNLS() is only available in node.js environments');
122+
return;
123+
}
116124

117125
// Get the nls configuration into the process.env as early as possible.
118126
let nlsConfig = { availableLanguages: {} };
@@ -163,9 +171,14 @@
163171

164172
/**
165173
* @param {{ portable: string; applicationName: string; }} product
166-
* @returns {{portableDataPath: string;isPortable: boolean;}}
174+
* @returns {{ portableDataPath: string; isPortable: boolean; }}
167175
*/
168176
function configurePortable(product) {
177+
if (!path || !fs) {
178+
console.warn('configurePortable() is only available in node.js environments');
179+
return;
180+
}
181+
169182
const appRoot = path.dirname(__dirname);
170183

171184
function getApplicationPath() {
@@ -228,6 +241,11 @@
228241
// Prevents appinsights from monkey patching modules.
229242
// This should be called before importing the applicationinsights module
230243
function avoidMonkeyPatchFromAppInsights() {
244+
if (typeof process === 'undefined') {
245+
console.warn('avoidMonkeyPatchFromAppInsights() is only available in node.js environments');
246+
return;
247+
}
248+
231249
// @ts-ignore
232250
process.env['APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL'] = true; // Skip monkey patching of 3rd party modules by appinsights
233251
global['diagnosticsSource'] = {}; // Prevents diagnostic channel (which patches "require") from initializing entirely

test/unit/electron/renderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function initLoader(opts) {
3232
nodeRequire: require,
3333
nodeMain: __filename,
3434
catchError: true,
35-
baseUrl: bootstrap.fileUriFromPath(path.join(__dirname, '../../../src')),
35+
baseUrl: bootstrap.fileUriFromPath(path.join(__dirname, '../../../src'), process.platform === 'win32'),
3636
paths: {
3737
'vs': `../${outdir}/vs`,
3838
'lib': `../${outdir}/lib`,

0 commit comments

Comments
 (0)