-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathkernel.ts
319 lines (276 loc) Β· 9.03 KB
/
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Ref: https://github.com/jupyterlite/jupyterlite/blob/f2ecc9cf7189cb19722bec2f0fc7ff5dfd233d47/packages/pyolite-kernel/src/kernel.ts
import { PromiseDelegate } from "@lumino/coreutils";
import { makeAbsoluteWheelURL } from "./url";
import { CrossOriginWorkerMaker as Worker } from "./cross-origin-worker";
// Since v0.19.0, Pyodide raises an exception when importing not pure Python 3 wheels, whose path does not end with "py3-none-any.whl",
// so configuration on file-loader here is necessary so that the hash is not included in the bundled URL.
// About this change on Pyodide, see the links below:
// https://github.com/pyodide/pyodide/pull/1859
// https://pyodide.org/en/stable/project/changelog.html#micropip
import TORNADO_WHEEL from "!!file-loader?name=pypi/[name].[ext]&context=.!../py/tornado/dist/tornado-6.2-py3-none-any.whl"; // TODO: Extract the import statement to an auto-generated file like `_pypi.ts` in JupyterLite: https://github.com/jupyterlite/jupyterlite/blob/f2ecc9cf7189cb19722bec2f0fc7ff5dfd233d47/packages/pyolite-kernel/src/_pypi.ts
import PYARROW_WHEEL from "!!file-loader?name=pypi/[name].[ext]&context=.!../py/stlite-pyarrow/dist/stlite_pyarrow-0.1.0-py3-none-any.whl";
import STREAMLIT_WHEEL from "!!file-loader?name=pypi/[name].[ext]&context=.!../py/streamlit/lib/dist/streamlit-1.18.1-py2.py3-none-any.whl";
// Ref: https://github.com/streamlit/streamlit/blob/1.12.2/frontend/src/lib/UriUtil.ts#L32-L33
const FINAL_SLASH_RE = /\/+$/;
const INITIAL_SLASH_RE = /^\/+/;
export interface StliteKernelOptions {
/**
* The file path on the Pyodide File System (Emscripten FS) to be set as a target of the `run` command.
*/
entrypoint: string;
/**
* A list of package names to be install at the booting-up phase.
*/
requirements: string[];
/**
* Files to mount.
*/
files: Record<
string,
{ data: string | ArrayBufferView; opts?: Record<string, any> }
>;
/**
* The URL of `pyodide.js` to be loaded via `importScripts()` in the worker.
* If not specified, the default one is used.
*/
pyodideEntrypointUrl?: string;
/**
*
*/
wheelBaseUrl?: string;
/**
* If specified, the worker restores the site-packages directories from this archive file
* and skip installing the wheels and required packages.
*/
mountedSitePackagesSnapshotFilePath?: string;
/**
* The `pathname` that will be used as both
* a base path of the custom component URLs
* ana the path of the main page in MPA.
*
* If not specified, the value of `window.location.pathname` at the time of the class initialization is used.
* This default is good enough for most cases, however,
* it may be a problem if the server is configured to return the main page even if the URL is pointing to the subpath.
* In such a setting, problems like https://github.com/whitphx/stlite/issues/171 may happen,
* so explicitly setting `basePath` is recommended.
*/
basePath?: string;
onProgress?: (message: string) => void;
onLoad?: () => void;
onError?: (error: Error) => void;
}
export class StliteKernel {
private _isDisposed = false;
private _worker: StliteWorker;
private _loaded = new PromiseDelegate<void>();
private _workerInitData: WorkerInitialData;
public readonly basePath: string; // TODO: Move this prop to outside this class. This is not a member of the kernel business logic, but just a globally referred value.
private onProgress: StliteKernelOptions["onProgress"];
private onLoad: StliteKernelOptions["onLoad"];
private onError: StliteKernelOptions["onError"];
constructor(options: StliteKernelOptions) {
this.basePath = (options.basePath ?? window.location.pathname)
.replace(FINAL_SLASH_RE, "")
.replace(INITIAL_SLASH_RE, "");
this.onProgress = options.onProgress;
this.onLoad = options.onLoad;
this.onError = options.onError;
// HACK: Use `CrossOriginWorkerMaker` imported as `Worker` here.
// Read the comment in `cross-origin-worker.ts` for the detail.
const workerMaker = new Worker(new URL("./worker.js", import.meta.url));
this._worker = workerMaker.worker;
this._worker.onmessage = (e) => {
this._processWorkerMessage(e.data);
};
let wheels: WorkerInitialData["wheels"] = undefined;
if (options.mountedSitePackagesSnapshotFilePath == null) {
console.debug("Custom wheel URLs:", {
TORNADO_WHEEL,
PYARROW_WHEEL,
STREAMLIT_WHEEL,
});
const tornadoWheelUrl = makeAbsoluteWheelURL(
TORNADO_WHEEL as unknown as string,
options.wheelBaseUrl
);
const pyarrowWheelUrl = makeAbsoluteWheelURL(
PYARROW_WHEEL as unknown as string,
options.wheelBaseUrl
);
const streamlitWheelUrl = makeAbsoluteWheelURL(
STREAMLIT_WHEEL as unknown as string,
options.wheelBaseUrl
);
wheels = {
tornado: tornadoWheelUrl,
pyarrow: pyarrowWheelUrl,
streamlit: streamlitWheelUrl,
};
console.debug("Custom wheel resolved URLs:", wheels);
}
this._workerInitData = {
entrypoint: options.entrypoint,
files: options.files,
requirements: options.requirements,
pyodideEntrypointUrl: options.pyodideEntrypointUrl,
wheels,
mountedSitePackagesSnapshotFilePath:
options.mountedSitePackagesSnapshotFilePath,
};
}
get loaded(): Promise<void> {
return this._loaded.promise;
}
public connectWebSocket(path: string): Promise<void> {
return this._asyncPostMessage({
type: "websocket:connect",
data: {
path,
},
});
}
public sendWebSocketMessage(payload: Uint8Array) {
return this._asyncPostMessage({
type: "websocket:send",
data: {
payload,
},
});
}
private handleWebSocketMessage:
| ((payload: Uint8Array | string) => void)
| null = null;
public onWebSocketMessage(handler: (payload: Uint8Array | string) => void) {
this.handleWebSocketMessage = handler;
}
public sendHttpRequest(request: HttpRequest): Promise<HttpResponse> {
return this._asyncPostMessage(
{
type: "http:request",
data: {
request,
},
},
"http:response"
).then((data) => {
return data.response;
});
}
public writeFile(
path: string,
data: string | ArrayBufferView,
opts?: Record<string, any>
): Promise<void> {
return this._asyncPostMessage({
type: "file:write",
data: {
path,
data,
opts,
},
});
}
public renameFile(oldPath: string, newPath: string): Promise<void> {
return this._asyncPostMessage({
type: "file:rename",
data: {
oldPath,
newPath,
},
});
}
public unlink(path: string): Promise<void> {
return this._asyncPostMessage({
type: "file:unlink",
data: {
path,
},
});
}
public install(requirements: string[]): Promise<void> {
return this._asyncPostMessage({
type: "install",
data: {
requirements,
},
});
}
private _asyncPostMessage(
message: InMessage
): Promise<GeneralReplyMessage["data"]>;
private _asyncPostMessage<T extends ReplyMessage["type"]>(
message: InMessage,
expectedReplyType: T
): Promise<Extract<ReplyMessage, { type: T }>["data"]>;
private _asyncPostMessage(
message: InMessage,
expectedReplyType = "reply"
): Promise<ReplyMessage["data"]> {
return new Promise((resolve, reject) => {
const channel = new MessageChannel();
channel.port1.onmessage = (e: MessageEvent<ReplyMessage>) => {
channel.port1.close();
const msg = e.data;
if (msg.error) {
reject(msg.error);
} else {
if (msg.type !== expectedReplyType) {
throw new Error(`Unexpected reply type "${msg.type}"`);
}
resolve(msg.data);
}
};
this._worker.postMessage(message, [channel.port2]);
});
}
/**
* Process a message coming from the pyodide web worker.
*
* @param msg The worker message to process.
*/
private _processWorkerMessage(msg: OutMessage): void {
switch (msg.type) {
case "event:start": {
this._worker.postMessage({
type: "initData",
data: this._workerInitData,
});
break;
}
case "event:progress": {
this.onProgress && this.onProgress(msg.data.message);
break;
}
case "event:error": {
this.onError && this.onError(msg.data.error);
break;
}
case "event:loaded": {
this._loaded.resolve();
this.onLoad && this.onLoad();
break;
}
case "websocket:message": {
const { payload } = msg.data;
this.handleWebSocketMessage && this.handleWebSocketMessage(payload);
break;
}
}
}
/**
* Return whether the kernel is disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* Dispose the kernel.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._worker.terminate();
this._isDisposed = true;
}
}