From 31f78efffccc739e8497e43e2473aaf55e7fccd8 Mon Sep 17 00:00:00 2001 From: Paul Marechal Date: Mon, 21 Jun 2021 12:14:36 -0400 Subject: [PATCH] monaco: use fetch instead of old AJAX Update the code to use `fetch` to get Onigasm's wasm code. Update our bindings such as Onigasm is lazily loaded instead of hardcoded and downloaded on module loading. --- .../monaco-textmate-frontend-bindings.ts | 48 ++++++++----------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/packages/monaco/src/browser/textmate/monaco-textmate-frontend-bindings.ts b/packages/monaco/src/browser/textmate/monaco-textmate-frontend-bindings.ts index eb9b4a94f3cf7..0e2275beb39f0 100644 --- a/packages/monaco/src/browser/textmate/monaco-textmate-frontend-bindings.ts +++ b/packages/monaco/src/browser/textmate/monaco-textmate-frontend-bindings.ts @@ -24,27 +24,6 @@ import { MonacoThemeRegistry } from './monaco-theme-registry'; import { loadWASM, OnigScanner, OnigString } from 'onigasm'; import { IOnigLib } from 'vscode-textmate'; -export function fetchOnigasm(): Promise { - return new Promise((resolve, reject) => { - const onigasmPath = require('onigasm/lib/onigasm.wasm'); // webpack doing its magic here - const request = new XMLHttpRequest(); - - request.onreadystatechange = function (): void { - if (this.readyState === XMLHttpRequest.DONE) { - if (this.status === 200) { - resolve(this.response); - } else { - reject(new Error('Could not fetch onigasm')); - } - } - }; - - request.open('GET', onigasmPath, true); - request.responseType = 'arraybuffer'; - request.send(); - }); -} - export class OnigasmLib implements IOnigLib { createOnigScanner(sources: string[]): OnigScanner { return new OnigScanner(sources); @@ -55,15 +34,30 @@ export class OnigasmLib implements IOnigLib { } export default (bind: interfaces.Bind, unbind: interfaces.Unbind, isBound: interfaces.IsBound, rebind: interfaces.Rebind) => { - const onigasmPromise: Promise = isBasicWasmSupported ? fetchOnigasm().then(async buffer => { - await loadWASM(buffer); - return new OnigasmLib(); - }) : Promise.reject(new Error('wasm not supported')); - bind(OnigasmPromise).toConstantValue(onigasmPromise); - + bind(OnigasmPromise).toDynamicValue(dynamicOnigasmLib).inSingletonScope(); bind(MonacoTextmateService).toSelf().inSingletonScope(); bind(FrontendApplicationContribution).toService(MonacoTextmateService); bindContributionProvider(bind, LanguageGrammarDefinitionContribution); bind(TextmateRegistry).toSelf().inSingletonScope(); bind(MonacoThemeRegistry).toDynamicValue(() => MonacoThemeRegistry.SINGLETON).inSingletonScope(); }; + +export async function dynamicOnigasmLib(ctx: interfaces.Context): Promise { + return createOnigasmLib(); +} + +export async function createOnigasmLib(): Promise { + if (!isBasicWasmSupported) { + throw new Error('wasm not supported'); + } + const wasm = await fetchOnigasm(); + await loadWASM(wasm); + return new OnigasmLib(); +} + +export async function fetchOnigasm(): Promise { + // Using Webpack's wasm loader should give us a URL to fetch the resource from: + const onigasmPath: string = require('onigasm/lib/onigasm.wasm'); + const response = await fetch(onigasmPath, { method: 'GET' }); + return response.arrayBuffer(); +}