From 07f0b3b7a1671f23988f6ba18e405fbb08fb5426 Mon Sep 17 00:00:00 2001 From: Duc Trung Le Date: Wed, 23 Oct 2024 23:39:37 +0200 Subject: [PATCH 1/3] Register all kernels in one plugin --- src/index.ts | 164 ++++++++++++++++++++++++++++----------------------- 1 file changed, 89 insertions(+), 75 deletions(-) diff --git a/src/index.ts b/src/index.ts index 459e78d..743077f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,86 +14,102 @@ import { IKernel, IKernelSpecs } from '@jupyterlite/kernel'; import { WebWorkerKernel } from './web_worker_kernel'; import { IEmpackEnvMetaFile } from './tokens'; -function getJson(url: string) { - const json_url = URLExt.join(PageConfig.getBaseUrl(), url); - const xhr = new XMLHttpRequest(); - xhr.open('GET', json_url, false); - xhr.send(null); - return JSON.parse(xhr.responseText); -} +/** + * Fetches JSON data from the specified URL asynchronously. + * + * This function constructs the full URL using the base URL from the PageConfig and + * the provided relative URL. It then performs a GET request using the Fetch API + * and returns the parsed JSON data. + * + * @param {string} url - The relative URL to fetch the JSON data from. + * @returns {Promise} - A promise that resolves to the parsed JSON data. + * @throws {Error} - Throws an error if the HTTP request fails. + * + */ +async function getJson(url: string) { + const jsonUrl = URLExt.join(PageConfig.getBaseUrl(), url); + const response = await fetch(jsonUrl, { method: 'GET' }); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } -let kernel_list: string[] = []; -try { - kernel_list = getJson('xeus/kernels.json'); -} catch (err) { - console.log(`Could not fetch xeus/kernels.json: ${err}`); - throw err; + const data = await response.json(); + return data; } -const plugins = kernel_list.map( - (kernel): JupyterLiteServerPlugin => { - return { - id: `@jupyterlite/xeus-${kernel}:register`, - autoStart: true, - requires: [IKernelSpecs], - optional: [ - IServiceWorkerManager, - IBroadcastChannelWrapper, - IEmpackEnvMetaFile - ], - activate: ( - app: JupyterLiteServer, - kernelspecs: IKernelSpecs, - serviceWorker?: IServiceWorkerManager, - broadcastChannel?: IBroadcastChannelWrapper, - empackEnvMetaFile?: IEmpackEnvMetaFile - ) => { - // Fetch kernel spec - const kernelspec = getJson('xeus/kernels/' + kernel + '/kernel.json'); - kernelspec.name = kernel; - kernelspec.dir = kernel; - for (const [key, value] of Object.entries(kernelspec.resources)) { - kernelspec.resources[key] = URLExt.join( - PageConfig.getBaseUrl(), - value as string +const kernelPlugin: JupyterLiteServerPlugin = { + id: '@jupyterlite/xeus-kernel:register', + autoStart: true, + requires: [IKernelSpecs], + optional: [ + IServiceWorkerManager, + IBroadcastChannelWrapper, + IEmpackEnvMetaFile + ], + activate: async ( + app: JupyterLiteServer, + kernelspecs: IKernelSpecs, + serviceWorker?: IServiceWorkerManager, + broadcastChannel?: IBroadcastChannelWrapper, + empackEnvMetaFile?: IEmpackEnvMetaFile + ) => { + // Fetch kernel list + let kernelList: string[] = []; + try { + kernelList = await getJson('xeus/kernels.json'); + } catch (err) { + console.log(`Could not fetch xeus/kernels.json: ${err}`); + throw err; + } + const contentsManager = app.serviceManager.contents; + + for (const kernel of kernelList) { + // Fetch kernel spec + const kernelspec = await getJson( + 'xeus/kernels/' + kernel + '/kernel.json' + ); + kernelspec.name = kernel; + kernelspec.dir = kernel; + for (const [key, value] of Object.entries(kernelspec.resources)) { + kernelspec.resources[key] = URLExt.join( + PageConfig.getBaseUrl(), + value as string + ); + } + kernelspecs.register({ + spec: kernelspec, + create: async (options: IKernel.IOptions): Promise => { + const mountDrive = !!( + (serviceWorker?.enabled && broadcastChannel?.enabled) || + crossOriginIsolated ); - } - const contentsManager = app.serviceManager.contents; - kernelspecs.register({ - spec: kernelspec, - create: async (options: IKernel.IOptions): Promise => { - const mountDrive = !!( - (serviceWorker?.enabled && broadcastChannel?.enabled) || - crossOriginIsolated + if (mountDrive) { + console.info( + `${kernelspec.name} contents will be synced with Jupyter Contents` + ); + } else { + console.warn( + `${kernelspec.name} contents will NOT be synced with Jupyter Contents` ); - - if (mountDrive) { - console.info( - `${kernelspec.name} contents will be synced with Jupyter Contents` - ); - } else { - console.warn( - `${kernelspec.name} contents will NOT be synced with Jupyter Contents` - ); - } - const link = empackEnvMetaFile - ? await empackEnvMetaFile.getLink(kernelspec) - : ''; - - return new WebWorkerKernel({ - ...options, - contentsManager, - mountDrive, - kernelSpec: kernelspec, - empackEnvMetaLink: link - }); } - }); - } - }; + const link = empackEnvMetaFile + ? await empackEnvMetaFile.getLink(kernelspec) + : ''; + + return new WebWorkerKernel({ + ...options, + contentsManager, + mountDrive, + kernelSpec: kernelspec, + empackEnvMetaLink: link + }); + } + }); + } } -); +}; const empackEnvMetaPlugin: JupyterLiteServerPlugin = { id: '@jupyterlite/xeus:empack-env-meta', @@ -113,6 +129,4 @@ const empackEnvMetaPlugin: JupyterLiteServerPlugin = { } }; -plugins.push(empackEnvMetaPlugin); - -export default plugins; +export default [empackEnvMetaPlugin, kernelPlugin]; From 88da91125a71c76147decacdff521fb3ffc001b3 Mon Sep 17 00:00:00 2001 From: Duc Trung Le Date: Thu, 24 Oct 2024 01:15:01 +0200 Subject: [PATCH 2/3] Refresh spec --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index 743077f..2a62796 100644 --- a/src/index.ts +++ b/src/index.ts @@ -108,6 +108,7 @@ const kernelPlugin: JupyterLiteServerPlugin = { } }); } + await app.serviceManager.kernelspecs.refreshSpecs(); } }; From 86e866ab54284ce721088d403a59b32dfbc7a858 Mon Sep 17 00:00:00 2001 From: Duc Trung Le Date: Thu, 24 Oct 2024 01:41:55 +0200 Subject: [PATCH 3/3] Export `IEmpackEnvMetaFile` token from index --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index 2a62796..4edc7de 100644 --- a/src/index.ts +++ b/src/index.ts @@ -131,3 +131,4 @@ const empackEnvMetaPlugin: JupyterLiteServerPlugin = { }; export default [empackEnvMetaPlugin, kernelPlugin]; +export { IEmpackEnvMetaFile };