forked from jupyterlite/xeus-python-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxeus_server_kernel.ts
206 lines (177 loc) · 4.89 KB
/
xeus_server_kernel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (c) Thorsten Beier
// Copyright (c) JupyterLite Contributors
// Distributed under the terms of the Modified BSD License.
import { wrap } from 'comlink';
import type { Remote } from 'comlink';
import { ISignal, Signal } from '@lumino/signaling';
import { PromiseDelegate } from '@lumino/coreutils';
import { PageConfig } from '@jupyterlab/coreutils';
import { KernelMessage } from '@jupyterlab/services';
import { IKernel } from '@jupyterlite/kernel';
interface IXeusKernel {
ready(): Promise<void>;
mount(driveName: string, mountpoint: string, baseUrl: string): Promise<void>;
cd(path: string): Promise<void>;
processMessage(msg: any): Promise<void>;
}
export class XeusServerKernel implements IKernel {
/**
* Instantiate a new XeusServerKernel
*
* @param options The instantiation options for a new XeusServerKernel
*/
constructor(options: XeusServerKernel.IOptions) {
const { id, name, sendMessage, location } = options;
this._id = id;
this._name = name;
this._location = location;
this._sendMessage = sendMessage;
this._worker = new Worker(new URL('./worker.js', import.meta.url), {
type: 'module'
});
this._worker.onmessage = e => {
this._processWorkerMessage(e.data);
};
this._remote = wrap(this._worker);
this.initFileSytem(options);
}
async handleMessage(msg: KernelMessage.IMessage): Promise<void> {
this._parent = msg;
this._parentHeader = msg.header;
await this._sendMessageToWorker(msg);
}
private async _sendMessageToWorker(msg: any): Promise<void> {
// TODO Remove this??
if (msg.header.msg_type !== 'input_reply') {
this._executeDelegate = new PromiseDelegate<void>();
}
await this._remote.processMessage({ msg, parent: this.parent });
if (msg.header.msg_type !== 'input_reply') {
return await this._executeDelegate.promise;
}
}
/**
* Get the last parent header
*/
get parentHeader():
| KernelMessage.IHeader<KernelMessage.MessageType>
| undefined {
return this._parentHeader;
}
/**
* Get the last parent message (mimick ipykernel's get_parent)
*/
get parent(): KernelMessage.IMessage | undefined {
return this._parent;
}
/**
* Get the kernel location
*/
get location(): string {
return this._location;
}
/**
* Process a message coming from the pyodide web worker.
*
* @param msg The worker message to process.
*/
private _processWorkerMessage(msg: any): void {
if (!msg.header) {
return;
}
msg.header.session = this._parentHeader?.session ?? '';
msg.session = this._parentHeader?.session ?? '';
this._sendMessage(msg);
// resolve promise
if (
msg.header.msg_type === 'status' &&
msg.content.execution_state === 'idle'
) {
this._executeDelegate.resolve();
}
}
/**
* A promise that is fulfilled when the kernel is ready.
*/
get ready(): Promise<void> {
return Promise.resolve();
}
/**
* Return whether the kernel is disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* A signal emitted when the kernel is disposed.
*/
get disposed(): ISignal<this, void> {
return this._disposed;
}
/**
* Dispose the kernel.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._worker.terminate();
(this._worker as any) = null;
(this._remote as any) = null;
this._isDisposed = true;
this._disposed.emit(void 0);
}
/**
* Get the kernel id
*/
get id(): string {
return this._id;
}
/**
* Get the name of the kernel
*/
get name(): string {
return this._name;
}
private async initFileSytem(options: XeusServerKernel.IOptions) {
let driveName: string;
let localPath: string;
if (options.location.includes(':')) {
const parts = options.location.split(':');
driveName = parts[0];
localPath = parts[1];
} else {
driveName = '';
localPath = options.location;
}
await this._remote.ready();
if (options.mountDrive) {
await this._remote.mount(driveName, '/drive', PageConfig.getBaseUrl());
await this._remote.cd(localPath);
}
}
private _id: string;
private _name: string;
private _location: string;
private _remote: Remote<IXeusKernel>;
private _isDisposed = false;
private _disposed = new Signal<this, void>(this);
private _worker: Worker;
private _sendMessage: IKernel.SendMessage;
private _executeDelegate = new PromiseDelegate<void>();
private _parentHeader:
| KernelMessage.IHeader<KernelMessage.MessageType>
| undefined = undefined;
private _parent: KernelMessage.IMessage | undefined = undefined;
}
/**
* A namespace for XeusServerKernel statics.
*/
export namespace XeusServerKernel {
/**
* The instantiation options for a Pyodide kernel
*/
export interface IOptions extends IKernel.IOptions {
mountDrive: boolean;
}
}