@jupyterlite/services 0.1.0 → 0.7.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.
Files changed (87) hide show
  1. package/lib/contents/drive.d.ts +277 -0
  2. package/lib/contents/drive.js +888 -0
  3. package/lib/contents/drive.js.map +1 -0
  4. package/lib/contents/drivecontents.d.ts +92 -0
  5. package/lib/contents/drivecontents.js +132 -0
  6. package/lib/contents/drivecontents.js.map +1 -0
  7. package/lib/contents/drivefs.d.ts +245 -0
  8. package/lib/contents/drivefs.js +481 -0
  9. package/lib/contents/drivefs.js.map +1 -0
  10. package/lib/contents/emscripten.d.ts +96 -0
  11. package/lib/contents/emscripten.js +10 -0
  12. package/lib/contents/emscripten.js.map +1 -0
  13. package/lib/contents/index.d.ts +5 -0
  14. package/lib/contents/index.js +8 -0
  15. package/lib/contents/index.js.map +1 -0
  16. package/lib/contents/tokens.d.ts +21 -0
  17. package/lib/contents/tokens.js +55 -0
  18. package/lib/contents/tokens.js.map +1 -0
  19. package/lib/index.d.ts +9 -0
  20. package/lib/index.js +12 -0
  21. package/lib/index.js.map +1 -0
  22. package/lib/kernel/base.d.ts +245 -0
  23. package/lib/kernel/base.js +457 -0
  24. package/lib/kernel/base.js.map +1 -0
  25. package/lib/kernel/client.d.ts +114 -0
  26. package/lib/kernel/client.js +375 -0
  27. package/lib/kernel/client.js.map +1 -0
  28. package/lib/kernel/index.d.ts +5 -0
  29. package/lib/kernel/index.js +8 -0
  30. package/lib/kernel/index.js.map +1 -0
  31. package/lib/kernel/kernelspecclient.d.ts +39 -0
  32. package/lib/kernel/kernelspecclient.js +37 -0
  33. package/lib/kernel/kernelspecclient.js.map +1 -0
  34. package/lib/kernel/kernelspecs.d.ts +59 -0
  35. package/lib/kernel/kernelspecs.js +62 -0
  36. package/lib/kernel/kernelspecs.js.map +1 -0
  37. package/lib/kernel/tokens.d.ts +163 -0
  38. package/lib/kernel/tokens.js +20 -0
  39. package/lib/kernel/tokens.js.map +1 -0
  40. package/lib/nbconvert/exporters.d.ts +80 -0
  41. package/lib/nbconvert/exporters.js +154 -0
  42. package/lib/nbconvert/exporters.js.map +1 -0
  43. package/lib/nbconvert/index.d.ts +3 -0
  44. package/lib/nbconvert/index.js +6 -0
  45. package/lib/nbconvert/index.js.map +1 -0
  46. package/lib/nbconvert/manager.d.ts +66 -0
  47. package/lib/nbconvert/manager.js +77 -0
  48. package/lib/nbconvert/manager.js.map +1 -0
  49. package/lib/nbconvert/tokens.d.ts +49 -0
  50. package/lib/nbconvert/tokens.js +8 -0
  51. package/lib/nbconvert/tokens.js.map +1 -0
  52. package/lib/session/client.d.ts +85 -0
  53. package/lib/session/client.js +200 -0
  54. package/lib/session/client.js.map +1 -0
  55. package/lib/session/index.d.ts +1 -0
  56. package/lib/session/index.js +4 -0
  57. package/lib/session/index.js.map +1 -0
  58. package/lib/settings/index.d.ts +1 -0
  59. package/lib/settings/index.js +4 -0
  60. package/lib/settings/index.js.map +1 -0
  61. package/lib/settings/settings.d.ts +91 -0
  62. package/lib/settings/settings.js +185 -0
  63. package/lib/settings/settings.js.map +1 -0
  64. package/package.json +67 -8
  65. package/src/contents/drive.ts +1030 -0
  66. package/src/contents/drivecontents.ts +253 -0
  67. package/src/contents/drivefs.ts +824 -0
  68. package/src/contents/emscripten.ts +148 -0
  69. package/src/contents/index.ts +8 -0
  70. package/src/contents/tokens.ts +61 -0
  71. package/src/index.ts +13 -0
  72. package/src/kernel/base.ts +637 -0
  73. package/src/kernel/client.ts +483 -0
  74. package/src/kernel/index.ts +8 -0
  75. package/src/kernel/kernelspecclient.ts +64 -0
  76. package/src/kernel/kernelspecs.ts +103 -0
  77. package/src/kernel/tokens.ts +222 -0
  78. package/src/nbconvert/exporters.ts +177 -0
  79. package/src/nbconvert/index.ts +6 -0
  80. package/src/nbconvert/manager.ts +105 -0
  81. package/src/nbconvert/tokens.ts +60 -0
  82. package/src/session/client.ts +251 -0
  83. package/src/session/index.ts +4 -0
  84. package/src/settings/index.ts +4 -0
  85. package/src/settings/settings.ts +236 -0
  86. package/style/index.css +6 -0
  87. package/style/index.js +6 -0
@@ -0,0 +1,222 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import type { Remote } from 'comlink';
5
+
6
+ import { IObservableMap } from '@jupyterlab/observables';
7
+
8
+ import { Kernel, KernelMessage, KernelSpec } from '@jupyterlab/services';
9
+
10
+ import { Token } from '@lumino/coreutils';
11
+
12
+ import { IObservableDisposable } from '@lumino/disposable';
13
+
14
+ import { ISignal } from '@lumino/signaling';
15
+
16
+ import { KernelSpecs } from './kernelspecs';
17
+
18
+ /**
19
+ * The kernel name of last resort.
20
+ */
21
+ export const FALLBACK_KERNEL = 'javascript';
22
+
23
+ /**
24
+ * The token for the kernel client.
25
+ */
26
+ export const IKernelClient = new Token<IKernelClient>(
27
+ '@jupyterlite/services:IKernelClient',
28
+ );
29
+
30
+ /**
31
+ * An interface for the Kernels service.
32
+ */
33
+ export interface IKernelClient extends Kernel.IKernelAPIClient {
34
+ /**
35
+ * Signal emitted when the kernels map changes
36
+ */
37
+ readonly changed: ISignal<IKernelClient, IObservableMap.IChangedArgs<IKernel>>;
38
+
39
+ /**
40
+ * Handle stdin request received from Service Worker.
41
+ */
42
+ handleStdin(
43
+ inputRequest: KernelMessage.IInputRequestMsg,
44
+ ): Promise<KernelMessage.IInputReplyMsg>;
45
+
46
+ /**
47
+ * Shut down all kernels.
48
+ */
49
+ shutdownAll: () => Promise<void>;
50
+ }
51
+
52
+ /**
53
+ * The token for the kernel spec client.
54
+ */
55
+ export const IKernelSpecClient = new Token<IKernelSpecClient>(
56
+ '@jupyterlite/services:IKernelSpecClient',
57
+ );
58
+
59
+ /**
60
+ * An interface for the kernel spec client.
61
+ */
62
+ export interface IKernelSpecClient extends KernelSpec.IKernelSpecAPIClient {}
63
+
64
+ /**
65
+ * An interface for a kernel running in the browser.
66
+ */
67
+ export interface IKernel extends IObservableDisposable {
68
+ /**
69
+ * The id of the server-side kernel.
70
+ */
71
+ readonly id: string;
72
+
73
+ /**
74
+ * The name of the server-side kernel.
75
+ */
76
+ readonly name: string;
77
+
78
+ /**
79
+ * The location in the virtual filesystem from which the kernel was started.
80
+ */
81
+ readonly location: string;
82
+
83
+ /**
84
+ * A promise that is fulfilled when the kernel is ready.
85
+ */
86
+ readonly ready: Promise<void>;
87
+
88
+ /**
89
+ * Handle an incoming message from the client.
90
+ *
91
+ * @param msg The message to handle
92
+ */
93
+ handleMessage(msg: KernelMessage.IMessage): Promise<void>;
94
+ }
95
+
96
+ /**
97
+ * A namespace for IKernel statics.
98
+ */
99
+ export namespace IKernel {
100
+ /**
101
+ * The type for the send message function.
102
+ */
103
+ export type SendMessage = (msg: KernelMessage.IMessage) => void;
104
+
105
+ /**
106
+ * The instantiation options for an IKernel.
107
+ */
108
+ export interface IOptions {
109
+ /**
110
+ * The kernel id.
111
+ */
112
+ id: string;
113
+
114
+ /**
115
+ * The name of the kernel.
116
+ */
117
+ name: string;
118
+
119
+ /**
120
+ * The location in the virtual filesystem from which the kernel was started.
121
+ */
122
+ location: string;
123
+
124
+ /**
125
+ * The method to send messages back to the server.
126
+ */
127
+ sendMessage: SendMessage;
128
+ }
129
+ }
130
+
131
+ /**
132
+ * The token for the kernel spec service.
133
+ */
134
+ export const IKernelSpecs = new Token<IKernelSpecs>(
135
+ '@jupyterlite/services:IKernelSpecs',
136
+ );
137
+
138
+ /**
139
+ * The interface for the kernel specs service.
140
+ */
141
+ export interface IKernelSpecs {
142
+ /**
143
+ * Get the kernel specs.
144
+ */
145
+ readonly specs: KernelSpec.ISpecModels | null;
146
+
147
+ /**
148
+ * Get the default kernel name.
149
+ */
150
+ readonly defaultKernelName: string;
151
+
152
+ /**
153
+ * Get the kernel factories for the current kernels.
154
+ */
155
+ readonly factories: KernelSpecs.KernelFactories;
156
+
157
+ /**
158
+ * Signal emitted when the specs change.
159
+ */
160
+ readonly changed: ISignal<IKernelSpecs, KernelSpec.ISpecModels | null>;
161
+
162
+ /**
163
+ * Register a new kernel spec
164
+ *
165
+ * @param options The kernel spec options.
166
+ */
167
+ register: (options: KernelSpecs.IKernelOptions) => void;
168
+ }
169
+
170
+ /**
171
+ * An interface for a comlink-based worker kernel
172
+ */
173
+ export interface IWorkerKernel {
174
+ /**
175
+ * Handle any lazy setup activities.
176
+ */
177
+ initialize(options: IWorkerKernel.IOptions): Promise<void>;
178
+ execute(
179
+ content: KernelMessage.IExecuteRequestMsg['content'],
180
+ parent: any,
181
+ ): Promise<KernelMessage.IExecuteReplyMsg['content']>;
182
+ complete(
183
+ content: KernelMessage.ICompleteRequestMsg['content'],
184
+ parent: any,
185
+ ): Promise<KernelMessage.ICompleteReplyMsg['content']>;
186
+ inspect(
187
+ content: KernelMessage.IInspectRequestMsg['content'],
188
+ parent: any,
189
+ ): Promise<KernelMessage.IInspectReplyMsg['content']>;
190
+ isComplete(
191
+ content: KernelMessage.IIsCompleteRequestMsg['content'],
192
+ parent: any,
193
+ ): Promise<KernelMessage.IIsCompleteReplyMsg['content']>;
194
+ commInfo(
195
+ content: KernelMessage.ICommInfoRequestMsg['content'],
196
+ parent: any,
197
+ ): Promise<KernelMessage.ICommInfoReplyMsg['content']>;
198
+ commOpen(content: KernelMessage.ICommOpenMsg, parent: any): Promise<void>;
199
+ commMsg(content: KernelMessage.ICommMsgMsg, parent: any): Promise<void>;
200
+ commClose(content: KernelMessage.ICommCloseMsg, parent: any): Promise<void>;
201
+ inputReply(
202
+ content: KernelMessage.IInputReplyMsg['content'],
203
+ parent: any,
204
+ ): Promise<void>;
205
+ }
206
+
207
+ /**
208
+ * A namespace for worker kernels.
209
+ **/
210
+ export namespace IWorkerKernel {
211
+ /**
212
+ * Common values likely to be required by all kernels.
213
+ */
214
+ export interface IOptions {
215
+ /**
216
+ * The base URL of the kernel server.
217
+ */
218
+ baseUrl: string;
219
+ }
220
+ }
221
+
222
+ export interface IRemoteKernel extends Remote<IWorkerKernel> {}
@@ -0,0 +1,177 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { Contents } from '@jupyterlab/services';
5
+
6
+ import { IExporter } from './tokens';
7
+
8
+ /**
9
+ * Base class for notebook exporters.
10
+ */
11
+ export abstract class BaseExporter implements IExporter {
12
+ /**
13
+ * The MIME type of the exported format.
14
+ */
15
+ abstract readonly mimeType: string;
16
+
17
+ /**
18
+ * Export a notebook to this format.
19
+ *
20
+ * @param model The notebook model to export
21
+ * @param path The path to the notebook
22
+ */
23
+ abstract export(model: Contents.IModel, path: string): Promise<void>;
24
+
25
+ /**
26
+ * Trigger a browser download of the exported content.
27
+ *
28
+ * @param content The content to download
29
+ * @param mimeType The MIME type of the content
30
+ * @param filename The filename for the download
31
+ */
32
+ protected triggerDownload(content: string, mimeType: string, filename: string): void {
33
+ const element = document.createElement('a');
34
+ element.href = `data:${mimeType};charset=utf-8,${encodeURIComponent(content)}`;
35
+ element.download = filename;
36
+ document.body.appendChild(element);
37
+ element.click();
38
+ document.body.removeChild(element);
39
+ }
40
+ }
41
+
42
+ /**
43
+ * Exporter for notebook format (.ipynb).
44
+ */
45
+ export class NotebookExporter extends BaseExporter {
46
+ /**
47
+ * The MIME type of the exported format.
48
+ */
49
+ readonly mimeType = 'application/x-ipynb+json';
50
+
51
+ /**
52
+ * Export a notebook to .ipynb format.
53
+ *
54
+ * @param model The notebook model to export
55
+ * @param path The path to the notebook
56
+ */
57
+ async export(model: Contents.IModel, path: string): Promise<void> {
58
+ const mime = model.mimetype ?? 'application/json';
59
+ const content = JSON.stringify(model.content, null, 2);
60
+ this.triggerDownload(content, mime, path);
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Exporter for executable script format.
66
+ */
67
+ export class ScriptExporter extends BaseExporter {
68
+ /**
69
+ * The MIME type of the exported format.
70
+ */
71
+ readonly mimeType = 'text/x-script';
72
+
73
+ /**
74
+ * Export a notebook to executable script format.
75
+ *
76
+ * @param model The notebook model to export
77
+ * @param path The path to the notebook
78
+ */
79
+ async export(model: Contents.IModel, path: string): Promise<void> {
80
+ const { content, extension } = this.convertToScript(model.content);
81
+ const filename = path.replace(/\.ipynb$/, extension);
82
+ this.triggerDownload(content, 'text/plain', filename);
83
+ }
84
+
85
+ /**
86
+ * Convert a notebook to a script file.
87
+ *
88
+ * @param content The notebook content
89
+ * @returns The script content and file extension
90
+ */
91
+ private convertToScript(content: any): {
92
+ content: string;
93
+ extension: string;
94
+ } {
95
+ // Get the language from the notebook metadata
96
+ const languageInfo = content.metadata?.language_info;
97
+ const language = languageInfo?.name || 'python';
98
+ const fileExtension = languageInfo?.file_extension || '.py';
99
+
100
+ // Extract code cells and convert to script
101
+ const cells = content.cells || [];
102
+ const scriptLines: string[] = [];
103
+
104
+ for (const cell of cells) {
105
+ if (cell.cell_type === 'code') {
106
+ // Add code cell content
107
+ const source = Array.isArray(cell.source) ? cell.source.join('') : cell.source;
108
+ scriptLines.push(source);
109
+ // Add blank line between cells
110
+ scriptLines.push('');
111
+ } else if (cell.cell_type === 'markdown' || cell.cell_type === 'raw') {
112
+ // Add markdown and raw cells as comments
113
+ const source = Array.isArray(cell.source) ? cell.source.join('') : cell.source;
114
+ const commentedSource = this.commentLines(source, language);
115
+ scriptLines.push(commentedSource);
116
+ // Add blank line between cells
117
+ scriptLines.push('');
118
+ }
119
+ }
120
+
121
+ return {
122
+ content: scriptLines.join('\n') + '\n',
123
+ extension: fileExtension,
124
+ };
125
+ }
126
+
127
+ /**
128
+ * Comment out lines based on the language.
129
+ *
130
+ * @param text The text to comment
131
+ * @param language The programming language
132
+ * @returns The commented text
133
+ */
134
+ private commentLines(text: string, language: string): string {
135
+ const lines = text.split('\n');
136
+ const commentChar = this.getCommentChar(language);
137
+
138
+ return lines.map((line) => `${commentChar} ${line}`).join('\n');
139
+ }
140
+
141
+ /**
142
+ * Get the comment character for a given language.
143
+ *
144
+ * @param language The programming language
145
+ * @returns The comment character(s) for that language
146
+ */
147
+ private getCommentChar(language: string): string {
148
+ // Map of languages to their comment characters
149
+ const commentMap: { [key: string]: string } = {
150
+ python: '#',
151
+ r: '#',
152
+ julia: '#',
153
+ ruby: '#',
154
+ bash: '#',
155
+ shell: '#',
156
+ perl: '#',
157
+ javascript: '//',
158
+ typescript: '//',
159
+ java: '//',
160
+ c: '//',
161
+ cpp: '//',
162
+ 'c++': '//',
163
+ scala: '//',
164
+ go: '//',
165
+ rust: '//',
166
+ swift: '//',
167
+ kotlin: '//',
168
+ matlab: '%',
169
+ octave: '%',
170
+ lua: '--',
171
+ sql: '--',
172
+ haskell: '--',
173
+ };
174
+
175
+ return commentMap[language.toLowerCase()] || '#';
176
+ }
177
+ }
@@ -0,0 +1,6 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ export * from './exporters';
5
+ export * from './manager';
6
+ export * from './tokens';
@@ -0,0 +1,105 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { Contents, NbConvert, NbConvertManager } from '@jupyterlab/services';
5
+
6
+ import { IExporter, INbConvertExporters } from './tokens';
7
+
8
+ /**
9
+ * Options for creating a LiteNbConvertManager.
10
+ */
11
+ export interface ILiteNbConvertManagerOptions extends NbConvertManager.IOptions {
12
+ /**
13
+ * The contents manager.
14
+ */
15
+ contentsManager: Contents.IManager;
16
+
17
+ /**
18
+ * The exporter registry.
19
+ */
20
+ exporters: INbConvertExporters;
21
+ }
22
+
23
+ /**
24
+ * Custom NbConvert manager for JupyterLite with client-side export.
25
+ */
26
+ export class LiteNbConvertManager extends NbConvertManager {
27
+ /**
28
+ * Construct a new LiteNbConvertManager.
29
+ *
30
+ * @param options The manager options
31
+ */
32
+ constructor(options: ILiteNbConvertManagerOptions) {
33
+ super(options);
34
+ this._contentsManager = options.contentsManager;
35
+ this._exporters = options.exporters;
36
+ }
37
+
38
+ /**
39
+ * Get the list of export formats available.
40
+ */
41
+ async getExportFormats(): Promise<NbConvert.IExportFormats> {
42
+ return this._exporters.getExportFormats();
43
+ }
44
+
45
+ /**
46
+ * Export a notebook to a given format.
47
+ *
48
+ * @param options The export options
49
+ */
50
+ async exportAs(options: NbConvert.IExportOptions): Promise<void> {
51
+ const { format, path } = options;
52
+
53
+ const model = await this._contentsManager.get(path, { content: true });
54
+ const exporter = this._exporters.get(format);
55
+ if (!exporter) {
56
+ throw new Error(`Unknown export format: ${format}`);
57
+ }
58
+ await exporter.export(model, path);
59
+ }
60
+
61
+ private _contentsManager: Contents.IManager;
62
+ private _exporters: INbConvertExporters;
63
+ }
64
+
65
+ /**
66
+ * Implementation of the exporter registry.
67
+ */
68
+ export class Exporters implements INbConvertExporters {
69
+ /**
70
+ * Register a new exporter.
71
+ *
72
+ * @param format The export format name
73
+ * @param exporter The exporter instance
74
+ */
75
+ register(format: string, exporter: IExporter): void {
76
+ this._exporters.set(format, exporter);
77
+ }
78
+
79
+ /**
80
+ * Get an exporter by format.
81
+ *
82
+ * @param format The export format name
83
+ * @returns The exporter or undefined if not found
84
+ */
85
+ get(format: string): IExporter | undefined {
86
+ return this._exporters.get(format);
87
+ }
88
+
89
+ /**
90
+ * Get all registered export formats.
91
+ *
92
+ * @returns A map of format names to their MIME types
93
+ */
94
+ getExportFormats(): Record<string, { output_mimetype: string }> {
95
+ const formats: Record<string, { output_mimetype: string }> = {};
96
+
97
+ for (const [format, exporter] of this._exporters) {
98
+ formats[format] = { output_mimetype: exporter.mimeType };
99
+ }
100
+
101
+ return formats;
102
+ }
103
+
104
+ private _exporters = new Map<string, IExporter>();
105
+ }
@@ -0,0 +1,60 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { Token } from '@lumino/coreutils';
5
+
6
+ import { Contents } from '@jupyterlab/services';
7
+
8
+ /**
9
+ * The token for the exporter registry.
10
+ */
11
+ export const INbConvertExporters = new Token<INbConvertExporters>(
12
+ '@jupyterlite/services:INbConvertExporters',
13
+ 'A service for managing notebook exporters in JupyterLite',
14
+ );
15
+
16
+ /**
17
+ * Interface for the exporter registry.
18
+ */
19
+ export interface INbConvertExporters {
20
+ /**
21
+ * Register a new exporter.
22
+ *
23
+ * @param format The export format name
24
+ * @param exporter The exporter instance
25
+ */
26
+ register(format: string, exporter: IExporter): void;
27
+
28
+ /**
29
+ * Get an exporter by format.
30
+ *
31
+ * @param format The export format name
32
+ * @returns The exporter or undefined if not found
33
+ */
34
+ get(format: string): IExporter | undefined;
35
+
36
+ /**
37
+ * Get all registered export formats.
38
+ *
39
+ * @returns A map of format names to their MIME types
40
+ */
41
+ getExportFormats(): Record<string, { output_mimetype: string }>;
42
+ }
43
+
44
+ /**
45
+ * Interface for a notebook exporter.
46
+ */
47
+ export interface IExporter {
48
+ /**
49
+ * The MIME type of the exported format.
50
+ */
51
+ readonly mimeType: string;
52
+
53
+ /**
54
+ * Export a notebook to this format.
55
+ *
56
+ * @param model The notebook model to export
57
+ * @param path The path to the notebook
58
+ */
59
+ export(model: Contents.IModel, path: string): Promise<void>;
60
+ }