@jupyterlite/pyodide-kernel 0.4.0-alpha.2 → 0.4.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './_pypi';
2
+ export * from './coincident.worker';
2
3
  export * from './comlink.worker';
3
4
  export * from './kernel';
4
5
  export * from './tokens';
package/lib/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // Copyright (c) Jupyter Development Team.
2
2
  // Distributed under the terms of the Modified BSD License.
3
3
  export * from './_pypi';
4
+ export * from './coincident.worker';
4
5
  export * from './comlink.worker';
5
6
  export * from './kernel';
6
7
  export * from './tokens';
package/lib/kernel.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { KernelMessage } from '@jupyterlab/services';
1
+ import { Contents, KernelMessage } from '@jupyterlab/services';
2
2
  import { BaseKernel, IKernel } from '@jupyterlite/kernel';
3
3
  import { IPyodideWorkerKernel } from './tokens';
4
4
  /**
@@ -20,7 +20,14 @@ export declare class PyodideKernel extends BaseKernel implements IKernel {
20
20
  * webpack to find it.
21
21
  */
22
22
  protected initWorker(options: PyodideKernel.IOptions): Worker;
23
- protected initRemote(options: PyodideKernel.IOptions): Promise<void>;
23
+ /**
24
+ * Initialize the remote kernel.
25
+ * Use coincident if crossOriginIsolated, comlink otherwise
26
+ * See the two following issues for more context:
27
+ * - https://github.com/jupyterlite/jupyterlite/issues/1424
28
+ * - https://github.com/jupyterlite/pyodide-kernel/pull/126
29
+ */
30
+ protected initRemote(options: PyodideKernel.IOptions): IPyodideWorkerKernel;
24
31
  protected initRemoteOptions(options: PyodideKernel.IOptions): IPyodideWorkerKernel.IOptions;
25
32
  /**
26
33
  * Dispose the kernel.
@@ -100,6 +107,8 @@ export declare class PyodideKernel extends BaseKernel implements IKernel {
100
107
  * @param content - The content of the reply.
101
108
  */
102
109
  inputReply(content: KernelMessage.IInputReplyMsg['content']): Promise<void>;
110
+ private _contentsManager;
111
+ private _contentsProcessor;
103
112
  private _worker;
104
113
  private _remoteKernel;
105
114
  private _ready;
@@ -140,5 +149,9 @@ export declare namespace PyodideKernel {
140
149
  lockFileURL: string;
141
150
  packages: string[];
142
151
  };
152
+ /**
153
+ * The Jupyterlite content manager
154
+ */
155
+ contentsManager: Contents.IManager;
143
156
  }
144
157
  }
package/lib/kernel.js CHANGED
@@ -1,8 +1,10 @@
1
+ import coincident from 'coincident';
2
+ import { proxy, wrap } from 'comlink';
1
3
  import { PromiseDelegate } from '@lumino/coreutils';
2
4
  import { PageConfig } from '@jupyterlab/coreutils';
3
5
  import { BaseKernel } from '@jupyterlite/kernel';
4
- import { wrap } from 'comlink';
5
6
  import { allJSONUrl, pipliteWheelUrl } from './_pypi';
7
+ import { DriveContentsProcessor, } from '@jupyterlite/contents';
6
8
  /**
7
9
  * A kernel that executes Python code with Pyodide.
8
10
  */
@@ -16,9 +18,8 @@ export class PyodideKernel extends BaseKernel {
16
18
  super(options);
17
19
  this._ready = new PromiseDelegate();
18
20
  this._worker = this.initWorker(options);
19
- this._worker.onmessage = (e) => this._processWorkerMessage(e.data);
20
- this._remoteKernel = wrap(this._worker);
21
- this.initRemote(options);
21
+ this._remoteKernel = this.initRemote(options);
22
+ this._contentsManager = options.contentsManager;
22
23
  }
23
24
  /**
24
25
  * Load the worker.
@@ -29,14 +30,49 @@ export class PyodideKernel extends BaseKernel {
29
30
  * webpack to find it.
30
31
  */
31
32
  initWorker(options) {
32
- return new Worker(new URL('./comlink.worker.js', import.meta.url), {
33
- type: 'module',
34
- });
33
+ if (crossOriginIsolated) {
34
+ return new Worker(new URL('./coincident.worker.js', import.meta.url), {
35
+ type: 'module',
36
+ });
37
+ }
38
+ else {
39
+ return new Worker(new URL('./comlink.worker.js', import.meta.url), {
40
+ type: 'module',
41
+ });
42
+ }
35
43
  }
36
- async initRemote(options) {
44
+ /**
45
+ * Initialize the remote kernel.
46
+ * Use coincident if crossOriginIsolated, comlink otherwise
47
+ * See the two following issues for more context:
48
+ * - https://github.com/jupyterlite/jupyterlite/issues/1424
49
+ * - https://github.com/jupyterlite/pyodide-kernel/pull/126
50
+ */
51
+ initRemote(options) {
52
+ let remote;
53
+ if (crossOriginIsolated) {
54
+ remote = coincident(this._worker);
55
+ remote.processWorkerMessage = this._processWorkerMessage.bind(this);
56
+ // The coincident worker uses its own filesystem API:
57
+ remote.processDriveRequest = async (data) => {
58
+ if (!DriveContentsProcessor) {
59
+ throw new Error('File system calls over Atomics.wait is only supported with jupyterlite>=0.4.0a3');
60
+ }
61
+ if (this._contentsProcessor === undefined) {
62
+ this._contentsProcessor = new DriveContentsProcessor({
63
+ contentsManager: this._contentsManager,
64
+ });
65
+ }
66
+ return await this._contentsProcessor.processDriveRequest(data);
67
+ };
68
+ }
69
+ else {
70
+ remote = wrap(this._worker);
71
+ remote.registerCallback(proxy(this._processWorkerMessage.bind(this)));
72
+ }
37
73
  const remoteOptions = this.initRemoteOptions(options);
38
- await this._remoteKernel.initialize(remoteOptions);
39
- this._ready.resolve();
74
+ remote.initialize(remoteOptions).then(this._ready.resolve.bind(this._ready));
75
+ return remote;
40
76
  }
41
77
  initRemoteOptions(options) {
42
78
  const { pyodideUrl } = options;
package/lib/tokens.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Definitions for the Pyodide kernel.
3
3
  */
4
- import type { Remote } from 'comlink';
4
+ import { TDriveMethod, TDriveRequest, TDriveResponse } from '@jupyterlite/contents';
5
5
  import { IWorkerKernel } from '@jupyterlite/kernel';
6
6
  /**
7
7
  * The schema for a Warehouse-like index, as used by piplite.
@@ -15,11 +15,25 @@ export interface IPyodideWorkerKernel extends IWorkerKernel {
15
15
  * Handle any lazy initialization activities.
16
16
  */
17
17
  initialize(options: IPyodideWorkerKernel.IOptions): Promise<void>;
18
+ /**
19
+ * Process drive request
20
+ * @param data
21
+ */
22
+ processDriveRequest<T extends TDriveMethod>(data: TDriveRequest<T>): TDriveResponse<T>;
23
+ /**
24
+ * Process worker message
25
+ * @param msg
26
+ */
27
+ processWorkerMessage(msg: any): void;
28
+ /**
29
+ * Register a callback for handling messages from the worker.
30
+ */
31
+ registerCallback(callback: (msg: any) => void): void;
18
32
  }
19
33
  /**
20
- * An convenience interface for Pyodide workers wrapped by a comlink Remote.
34
+ * Deprecated.
21
35
  */
22
- export type IRemotePyodideWorkerKernel = Remote<IPyodideWorkerKernel>;
36
+ export type IRemotePyodideWorkerKernel = IPyodideWorkerKernel;
23
37
  /**
24
38
  * An namespace for Pyodide workers.
25
39
  */
package/lib/worker.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type Pyodide from 'pyodide';
2
2
  import type { DriveFS } from '@jupyterlite/contents';
3
+ import { KernelMessage } from '@jupyterlab/services';
3
4
  import type { IPyodideWorkerKernel } from './tokens';
4
5
  export declare class PyodideRemoteKernel {
5
6
  constructor();
@@ -26,6 +27,11 @@ export declare class PyodideRemoteKernel {
26
27
  * @param res The result object from the Pyodide evaluation
27
28
  */
28
29
  formatResult(res: any): any;
30
+ /**
31
+ * Register the callback function to send messages from the worker back to the main thread.
32
+ * @param callback the callback to register
33
+ */
34
+ registerCallback(callback: (msg: any) => void): void;
29
35
  /**
30
36
  * Makes sure pyodide is ready before continuing, and cache the parent message.
31
37
  */
@@ -65,10 +71,7 @@ export declare class PyodideRemoteKernel {
65
71
  *
66
72
  * @param content The incoming message with the comm target name.
67
73
  */
68
- commInfo(content: any, parent: any): Promise<{
69
- comms: any;
70
- status: string;
71
- }>;
74
+ commInfo(content: any, parent: any): Promise<KernelMessage.ICommInfoReplyMsg['content']>;
72
75
  /**
73
76
  * Respond to the commOpen.
74
77
  *
@@ -131,4 +134,5 @@ export declare class PyodideRemoteKernel {
131
134
  protected _stderr_stream: any;
132
135
  protected _resolveInputReply: any;
133
136
  protected _driveFS: DriveFS | null;
137
+ protected _sendWorkerMessage: (msg: any) => void;
134
138
  }