From 9157b0e33f36ff1cc260a2f1d8cd1a8b0dbcc749 Mon Sep 17 00:00:00 2001 From: Thorsten Beier Date: Wed, 13 Dec 2023 12:15:20 +0100 Subject: [PATCH] cleanup pass (#9) --- src/index.ts | 22 ++++++++++------------ src/web_worker_kernel.ts | 15 ++++++++------- src/worker.ts | 29 +++++++++++++---------------- 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2fd1d6d..7c9cb0a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -59,10 +59,10 @@ const kernel_specs = kernel_dir.map(kernel_dir => { console.log(kernel_specs); -const server_kernels = kernel_specs.map(spec => { +const server_kernels = kernel_specs.map(kernelspec => { const server_kernel: JupyterLiteServerPlugin = { // use name from spec - id: `@jupyterlite/${spec.name}-extension:kernel`, + id: `@jupyterlite/${kernelspec.name}-extension:kernel`, autoStart: true, requires: [IKernelSpecs], optional: [IServiceWorkerManager, IBroadcastChannelWrapper], @@ -73,7 +73,7 @@ const server_kernels = kernel_specs.map(spec => { broadcastChannel?: IBroadcastChannelWrapper ) => { kernelspecs.register({ - spec: spec, + spec: kernelspec, create: async (options: IKernel.IOptions): Promise => { // const mountDrive = !!( // serviceWorker?.enabled && broadcastChannel?.enabled @@ -82,21 +82,19 @@ const server_kernels = kernel_specs.map(spec => { if (mountDrive) { console.info( - `${spec.name} contents will be synced with Jupyter Contents` + `${kernelspec.name} contents will be synced with Jupyter Contents` ); } else { console.warn( - `${spec.name} contents will NOT be synced with Jupyter Contents` + `${kernelspec.name} contents will NOT be synced with Jupyter Contents` ); } - return new WebWorkerKernel( - { - ...options, - mountDrive - }, - spec - ); + return new WebWorkerKernel({ + ...options, + mountDrive, + kernelspec + }); } }); } diff --git a/src/web_worker_kernel.ts b/src/web_worker_kernel.ts index 95086dc..bdeae5f 100644 --- a/src/web_worker_kernel.ts +++ b/src/web_worker_kernel.ts @@ -29,13 +29,13 @@ export class WebWorkerKernel implements IKernel { * * @param options The instantiation options for a new WebWorkerKernel */ - constructor(options: WebWorkerKernel.IOptions, spec: any) { + constructor(options: WebWorkerKernel.IOptions) { console.log('constructing WebWorkerKernel kernel'); const { id, name, sendMessage, location } = options; this._id = id; this._name = name; this._location = location; - this._spec = spec; + this._kernelspec = options.kernelspec; this._sendMessage = sendMessage; console.log('constructing WebWorkerKernel worker'); this._worker = new Worker(new URL('./worker.js', import.meta.url), { @@ -54,10 +54,10 @@ export class WebWorkerKernel implements IKernel { this._remote.processMessage({ msg: { header: { - msg_type: 'initialize' - } - }, - spec: this._spec + msg_type: '__initialize__' + }, + kernelspec: this._kernelspec + } }); console.log('init filesystem'); @@ -208,7 +208,7 @@ export class WebWorkerKernel implements IKernel { } } - private _spec: any; + private _kernelspec: any; private _id: string; private _name: string; private _location: string; @@ -233,5 +233,6 @@ export namespace WebWorkerKernel { */ export interface IOptions extends IKernel.IOptions { mountDrive: boolean; + kernelspec: any; } } diff --git a/src/worker.ts b/src/worker.ts index f022354..d55550d 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -17,13 +17,6 @@ declare function createXeusModule(options: any): any; globalThis.Module = {}; -console.log('worker here'); - -// const WASM_KERNEL_FILE = 'kernels/xlite/xlite.js'; -// const WASM_FILE = 'kernels/xlite/xlite.wasm'; -// TODO Remove this. This is to ensure we always perform node ops on Nodes and -// not Streams, but why is it needed??? Why do we get Streams and not Nodes from -// emscripten in the case of xeus-python??? class StreamNodeOps extends DriveFSEmscriptenNodeOps { private getNode(nodeOrStream: any) { if (nodeOrStream['node']) { @@ -143,9 +136,8 @@ class XeusKernel { async processMessage(event: any): Promise { const msg_type = event.msg.header.msg_type; - if (msg_type === 'initialize') { - const spec = event.spec; - this._spec = spec; + if (msg_type === '__initialize__') { + this._kernelspec = event.msg.kernelspec; await this.initialize(); return; @@ -171,12 +163,13 @@ class XeusKernel { } private async initialize() { - const dir = this._spec.dir; - const binary_js = this._spec.argv[0]; - const binary_wasm = binary_js.replace('.js', '.wasm'); + // the location of the kernel on the server + // ie `share/jupyter/kernels/${dir}` + const dir = this._kernelspec.dir; - console.log(binary_js); - console.log(binary_wasm); + // location of the kernel binary on the server + const binary_js = this._kernelspec.argv[0]; + const binary_wasm = binary_js.replace('.js', '.wasm'); importScripts(binary_js); globalThis.Module = await createXeusModule({ @@ -191,6 +184,10 @@ class XeusKernel { await this.waitRunDependency(); console.log(globalThis.Module); + // each kernel can have a `async_init` function + // which can do kernel specific **async** initialization + // This function is usually implemented in the pre/post.js + // in the emscripten build of that kernel if (globalThis.Module['async_init'] !== undefined) { const kernel_root_url = `share/jupyter/kernels/${dir}`; const pkg_root_url = 'share/jupyter/kernel_packages'; @@ -239,7 +236,7 @@ class XeusKernel { return promise; } private _resolve: any; - private _spec: any; + private _kernelspec: any; private _raw_xkernel: any; private _raw_xserver: any; private _drive: DriveFS | null = null;