mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 10:25:42 +01:00
Fixes worker sandbox problems (#4975)
This commit is contained in:
parent
a4d7907bd4
commit
6f3fbe8c3a
5 changed files with 114 additions and 32 deletions
60
build/amd/plugin.js
Normal file
60
build/amd/plugin.js
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
/*---------------------------------------------------------------------------------------------
|
||||||
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||||
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||||
|
*--------------------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {() => import('rollup').Plugin}
|
||||||
|
*/
|
||||||
|
export function urlToEsmPlugin() {
|
||||||
|
return {
|
||||||
|
name: 'import-meta-url',
|
||||||
|
async transform(code, id) {
|
||||||
|
if (this.environment?.mode === 'dev') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let idx = 0;
|
||||||
|
|
||||||
|
// Look for `new URL("...?worker", import.meta.url)` patterns.
|
||||||
|
const regex = /new\s+URL\s*\(\s*(['"`])(.*?)\?worker\1\s*,\s*import\.meta\.url\s*\)?/g;
|
||||||
|
|
||||||
|
let match;
|
||||||
|
let modified = false;
|
||||||
|
let result = code;
|
||||||
|
let offset = 0;
|
||||||
|
/** @type {string[]} */
|
||||||
|
const additionalImports = [];
|
||||||
|
|
||||||
|
while ((match = regex.exec(code)) !== null) {
|
||||||
|
let path = match[2];
|
||||||
|
|
||||||
|
if (!path.startsWith('.') && !path.startsWith('/')) {
|
||||||
|
path = `./${path}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const start = match.index;
|
||||||
|
const end = start + match[0].length;
|
||||||
|
|
||||||
|
const varName = `__worker_url_${idx++}__`;
|
||||||
|
additionalImports.push(`import ${varName} from ${JSON.stringify(path + '?worker&url')};`);
|
||||||
|
|
||||||
|
const replacement = varName;
|
||||||
|
|
||||||
|
result = result.slice(0, start + offset) + replacement + result.slice(end + offset);
|
||||||
|
offset += replacement.length - (end - start);
|
||||||
|
modified = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!modified) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = additionalImports.join('\n') + '\n' + result;
|
||||||
|
|
||||||
|
return {
|
||||||
|
code: result,
|
||||||
|
map: null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -1,37 +1,58 @@
|
||||||
import * as require_ from 'require';
|
import * as require from 'require';
|
||||||
|
|
||||||
self.MonacoEnvironment = {
|
self.MonacoEnvironment = {
|
||||||
getWorker: function (_moduleId, label) {
|
getWorker: function (_moduleId, label) {
|
||||||
const require = require_;
|
|
||||||
if (!require) {
|
|
||||||
label = label; // NOOP
|
|
||||||
}
|
|
||||||
if (label === 'json') {
|
if (label === 'json') {
|
||||||
return new Worker(new URL('../../../src/language/json/json.worker.ts', import.meta.url), {
|
return new Worker(
|
||||||
type: 'module'
|
getWorkerBootstrapUrl(
|
||||||
});
|
new URL('../../../src/language/json/json.worker.ts?worker', import.meta.url)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if (label === 'css' || label === 'scss' || label === 'less') {
|
if (label === 'css' || label === 'scss' || label === 'less') {
|
||||||
return new Worker(new URL('../../../src/language/css/css.worker.ts', import.meta.url), {
|
return new Worker(
|
||||||
type: 'module'
|
getWorkerBootstrapUrl(
|
||||||
});
|
new URL('../../../src/language/css/css.worker.ts?worker', import.meta.url)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if (label === 'html' || label === 'handlebars' || label === 'razor') {
|
if (label === 'html' || label === 'handlebars' || label === 'razor') {
|
||||||
return new Worker(new URL('../../../src/language/html/html.worker.ts', import.meta.url), {
|
return new Worker(
|
||||||
type: 'module'
|
getWorkerBootstrapUrl(
|
||||||
});
|
new URL('../../../src/language/html/html.worker.ts?worker', import.meta.url)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if (label === 'typescript' || label === 'javascript') {
|
if (label === 'typescript' || label === 'javascript') {
|
||||||
return new Worker(new URL('../../../src/language/typescript/ts.worker.ts', import.meta.url), {
|
return new Worker(
|
||||||
type: 'module'
|
getWorkerBootstrapUrl(
|
||||||
});
|
new URL('../../../src/language/typescript/ts.worker.ts?worker', import.meta.url)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return new Worker(new URL('../../../src/editor/editor.worker.ts', import.meta.url), {
|
return new Worker(
|
||||||
type: 'module'
|
getWorkerBootstrapUrl(new URL('../../../src/editor/editor.worker.ts?worker', import.meta.url))
|
||||||
});
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function getWorkerBootstrapUrl(workerScriptUrl) {
|
||||||
|
const blob = new Blob(
|
||||||
|
[
|
||||||
|
[
|
||||||
|
`const ttPolicy = globalThis.trustedTypes?.createPolicy('defaultWorkerFactory', { createScriptURL: value => value });`,
|
||||||
|
`globalThis.workerttPolicy = ttPolicy;`,
|
||||||
|
`importScripts(ttPolicy?.createScriptURL(${JSON.stringify(
|
||||||
|
workerScriptUrl
|
||||||
|
)}) ?? ${JSON.stringify(workerScriptUrl)});`,
|
||||||
|
`globalThis.postMessage({ type: 'vscode-worker-ready' });`
|
||||||
|
].join('')
|
||||||
|
],
|
||||||
|
{ type: 'application/javascript' }
|
||||||
|
);
|
||||||
|
return URL.createObjectURL(blob);
|
||||||
|
}
|
||||||
|
|
||||||
import 'vs/nls.messages-loader!';
|
import 'vs/nls.messages-loader!';
|
||||||
import '../../../src/basic-languages/monaco.contribution';
|
import '../../../src/basic-languages/monaco.contribution';
|
||||||
import '../../../src/language/css/monaco.contribution';
|
import '../../../src/language/css/monaco.contribution';
|
||||||
|
|
@ -39,7 +60,7 @@ import '../../../src/language/html/monaco.contribution';
|
||||||
import '../../../src/language/json/monaco.contribution';
|
import '../../../src/language/json/monaco.contribution';
|
||||||
import '../../../src/language/typescript/monaco.contribution';
|
import '../../../src/language/typescript/monaco.contribution';
|
||||||
|
|
||||||
const styleSheetUrl = require_.toUrl('vs/style.css');
|
const styleSheetUrl = require.toUrl('vs/style.css');
|
||||||
|
|
||||||
const link = document.createElement('link');
|
const link = document.createElement('link');
|
||||||
link.rel = 'stylesheet';
|
link.rel = 'stylesheet';
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ import { glob } from 'node:fs/promises';
|
||||||
import { basename, dirname, join, resolve } from 'node:path';
|
import { basename, dirname, join, resolve } from 'node:path';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import { defineConfig } from 'vite';
|
import { defineConfig } from 'vite';
|
||||||
|
import { urlToEsmPlugin } from './plugin';
|
||||||
|
|
||||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||||
|
|
||||||
|
|
@ -77,7 +78,8 @@ export default defineConfig(async (args) => {
|
||||||
source: readFileSync(resolve(__dirname, './src/loader.js'), 'utf-8')
|
source: readFileSync(resolve(__dirname, './src/loader.js'), 'utf-8')
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
urlToEsmPlugin()
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
||||||
17
package-lock.json
generated
17
package-lock.json
generated
|
|
@ -29,7 +29,7 @@
|
||||||
"jsdom": "^19.0.0",
|
"jsdom": "^19.0.0",
|
||||||
"jsonc-parser": "^3.0.0",
|
"jsonc-parser": "^3.0.0",
|
||||||
"mocha": "^9.2.0",
|
"mocha": "^9.2.0",
|
||||||
"monaco-editor-core": "^0.53.0-dev-20250828",
|
"monaco-editor-core": "^0.53.0-dev-20250905",
|
||||||
"parcel": "^2.7.0",
|
"parcel": "^2.7.0",
|
||||||
"pin-github-action": "^1.8.0",
|
"pin-github-action": "^1.8.0",
|
||||||
"prettier": "^2.5.1",
|
"prettier": "^2.5.1",
|
||||||
|
|
@ -5496,11 +5496,10 @@
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"node_modules/monaco-editor-core": {
|
"node_modules/monaco-editor-core": {
|
||||||
"version": "0.53.0-dev-20250828",
|
"version": "0.53.0-dev-20250905",
|
||||||
"resolved": "https://registry.npmjs.org/monaco-editor-core/-/monaco-editor-core-0.53.0-dev-20250828.tgz",
|
"resolved": "https://registry.npmjs.org/monaco-editor-core/-/monaco-editor-core-0.53.0-dev-20250905.tgz",
|
||||||
"integrity": "sha512-KDlcU32Fe+stH/FSInNbcGdE0Vi9INYwH0I6veTc2IC9xuiLL9JsmYQ0zLIeFXjfYEtsQWyj0BJYixxCV4GeLg==",
|
"integrity": "sha512-naSEvkM5l8ubpYwBW69tb4SAQg8bzG1EduXSXdidgnqk6SB7c5n6hSUlVJvRktJHjuaoUMfwA0+ppiUb5F05jg==",
|
||||||
"dev": true,
|
"dev": true
|
||||||
"license": "MIT"
|
|
||||||
},
|
},
|
||||||
"node_modules/mri": {
|
"node_modules/mri": {
|
||||||
"version": "1.2.0",
|
"version": "1.2.0",
|
||||||
|
|
@ -11295,9 +11294,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"monaco-editor-core": {
|
"monaco-editor-core": {
|
||||||
"version": "0.53.0-dev-20250828",
|
"version": "0.53.0-dev-20250905",
|
||||||
"resolved": "https://registry.npmjs.org/monaco-editor-core/-/monaco-editor-core-0.53.0-dev-20250828.tgz",
|
"resolved": "https://registry.npmjs.org/monaco-editor-core/-/monaco-editor-core-0.53.0-dev-20250905.tgz",
|
||||||
"integrity": "sha512-KDlcU32Fe+stH/FSInNbcGdE0Vi9INYwH0I6veTc2IC9xuiLL9JsmYQ0zLIeFXjfYEtsQWyj0BJYixxCV4GeLg==",
|
"integrity": "sha512-naSEvkM5l8ubpYwBW69tb4SAQg8bzG1EduXSXdidgnqk6SB7c5n6hSUlVJvRktJHjuaoUMfwA0+ppiUb5F05jg==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"mri": {
|
"mri": {
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@
|
||||||
"jsdom": "^19.0.0",
|
"jsdom": "^19.0.0",
|
||||||
"jsonc-parser": "^3.0.0",
|
"jsonc-parser": "^3.0.0",
|
||||||
"mocha": "^9.2.0",
|
"mocha": "^9.2.0",
|
||||||
"monaco-editor-core": "^0.53.0-dev-20250828",
|
"monaco-editor-core": "^0.53.0-dev-20250905",
|
||||||
"parcel": "^2.7.0",
|
"parcel": "^2.7.0",
|
||||||
"pin-github-action": "^1.8.0",
|
"pin-github-action": "^1.8.0",
|
||||||
"prettier": "^2.5.1",
|
"prettier": "^2.5.1",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue