|
5 | 5 |
|
6 | 6 | (function () {
|
7 | 7 |
|
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 | + }); |
27 | 52 |
|
28 | 53 | function canUseEval(): boolean {
|
29 | 54 | try {
|
|
0 commit comments