Skip to content

Commit 8092bb2

Browse files
authored
Merge pull request microsoft#184148 from microsoft/alexd/disciplinary-macaw
Fix usage of types coming outside of TS base lib & allow embedders to intercept TT calls also in the worker
2 parents a1bcb27 + c759698 commit 8092bb2

File tree

3 files changed

+78
-27
lines changed

3 files changed

+78
-27
lines changed

build/monaco/monaco.d.ts.recipe

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,25 @@ declare namespace monaco {
3434
*/
3535
getWorkerUrl?(workerId: string, label: string): string;
3636
/**
37-
* Create a trust types policy (same API as window.trustedTypes.createPolicy)
37+
* Create a trusted types policy (same API as window.trustedTypes.createPolicy)
3838
*/
39-
createTrustedTypesPolicy<Options extends TrustedTypePolicyOptions>(
39+
createTrustedTypesPolicy(
4040
policyName: string,
41-
policyOptions?: Options,
42-
): undefined | Pick<TrustedTypePolicy<Options>, 'name' | Extract<keyof Options, keyof TrustedTypePolicyOptions>>;
41+
policyOptions?: ITrustedTypePolicyOptions,
42+
): undefined | ITrustedTypePolicy;
43+
}
44+
45+
export interface ITrustedTypePolicyOptions {
46+
createHTML?: (input: string, ...arguments: any[]) => string;
47+
createScript?: (input: string, ...arguments: any[]) => string;
48+
createScriptURL?: (input: string, ...arguments: any[]) => string;
49+
}
50+
51+
export interface ITrustedTypePolicy {
52+
readonly name: string;
53+
createHTML?(input: string): any;
54+
createScript?(input: string): any;
55+
createScriptURL?(input: string): any;
4356
}
4457

4558
export interface IDisposable {

src/vs/base/worker/workerMain.ts

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,50 @@
55

66
(function () {
77

8-
const MonacoEnvironment = (<any>globalThis).MonacoEnvironment;
9-
const monacoBaseUrl = MonacoEnvironment && MonacoEnvironment.baseUrl ? MonacoEnvironment.baseUrl : '../../../';
10-
11-
const trustedTypesPolicy = (
12-
typeof self.trustedTypes?.createPolicy === 'function'
13-
? self.trustedTypes?.createPolicy('amdLoader', {
14-
createScriptURL: value => value,
15-
createScript: (_, ...args: string[]) => {
16-
// workaround a chrome issue not allowing to create new functions
17-
// see https://github.com/w3c/webappsec-trusted-types/wiki/Trusted-Types-for-function-constructor
18-
const fnArgs = args.slice(0, -1).join(',');
19-
const fnBody = args.pop()!.toString();
20-
// Do not add a new line to fnBody, as this will confuse source maps.
21-
const body = `(function anonymous(${fnArgs}) { ${fnBody}\n})`;
22-
return body;
23-
}
24-
})
25-
: undefined
26-
);
8+
interface IMonacoEnvironment {
9+
baseUrl?: string;
10+
createTrustedTypesPolicy<Options extends TrustedTypePolicyOptions>(
11+
policyName: string,
12+
policyOptions?: Options,
13+
): undefined | Pick<TrustedTypePolicy<Options>, 'name' | Extract<keyof Options, keyof TrustedTypePolicyOptions>>;
14+
}
15+
const monacoEnvironment: IMonacoEnvironment | undefined = (globalThis as any).MonacoEnvironment;
16+
const monacoBaseUrl = monacoEnvironment && monacoEnvironment.baseUrl ? monacoEnvironment.baseUrl : '../../../';
17+
18+
function createTrustedTypesPolicy<Options extends TrustedTypePolicyOptions>(
19+
policyName: string,
20+
policyOptions?: Options,
21+
): undefined | Pick<TrustedTypePolicy<Options>, 'name' | Extract<keyof Options, keyof TrustedTypePolicyOptions>> {
22+
23+
if (monacoEnvironment?.createTrustedTypesPolicy) {
24+
try {
25+
return monacoEnvironment.createTrustedTypesPolicy(policyName, policyOptions);
26+
} catch (err) {
27+
console.warn(err);
28+
return undefined;
29+
}
30+
}
31+
32+
try {
33+
return self.trustedTypes?.createPolicy(policyName, policyOptions);
34+
} catch (err) {
35+
console.warn(err);
36+
return undefined;
37+
}
38+
}
39+
40+
const trustedTypesPolicy = createTrustedTypesPolicy('amdLoader', {
41+
createScriptURL: value => value,
42+
createScript: (_, ...args: string[]) => {
43+
// workaround a chrome issue not allowing to create new functions
44+
// see https://github.com/w3c/webappsec-trusted-types/wiki/Trusted-Types-for-function-constructor
45+
const fnArgs = args.slice(0, -1).join(',');
46+
const fnBody = args.pop()!.toString();
47+
// Do not add a new line to fnBody, as this will confuse source maps.
48+
const body = `(function anonymous(${fnArgs}) { ${fnBody}\n})`;
49+
return body;
50+
}
51+
});
2752

2853
function canUseEval(): boolean {
2954
try {

src/vs/monaco.d.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,25 @@ declare namespace monaco {
3434
*/
3535
getWorkerUrl?(workerId: string, label: string): string;
3636
/**
37-
* Create a trust types policy (same API as window.trustedTypes.createPolicy)
37+
* Create a trusted types policy (same API as window.trustedTypes.createPolicy)
3838
*/
39-
createTrustedTypesPolicy<Options extends TrustedTypePolicyOptions>(
39+
createTrustedTypesPolicy(
4040
policyName: string,
41-
policyOptions?: Options,
42-
): undefined | Pick<TrustedTypePolicy<Options>, 'name' | Extract<keyof Options, keyof TrustedTypePolicyOptions>>;
41+
policyOptions?: ITrustedTypePolicyOptions,
42+
): undefined | ITrustedTypePolicy;
43+
}
44+
45+
export interface ITrustedTypePolicyOptions {
46+
createHTML?: (input: string, ...arguments: any[]) => string;
47+
createScript?: (input: string, ...arguments: any[]) => string;
48+
createScriptURL?: (input: string, ...arguments: any[]) => string;
49+
}
50+
51+
export interface ITrustedTypePolicy {
52+
readonly name: string;
53+
createHTML?(input: string): any;
54+
createScript?(input: string): any;
55+
createScriptURL?(input: string): any;
4356
}
4457

4558
export interface IDisposable {

0 commit comments

Comments
 (0)