@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,251 @@
1
+ import { ServerConnection, Session } from '@jupyterlab/services';
2
+
3
+ import { PathExt } from '@jupyterlab/coreutils';
4
+
5
+ import { LiteKernelClient } from '../kernel';
6
+
7
+ import { ArrayExt } from '@lumino/algorithm';
8
+
9
+ import { UUID } from '@lumino/coreutils';
10
+
11
+ import { ISessionAPIClient } from '@jupyterlab/services/lib/session/session';
12
+
13
+ type DeepPartial<T> = {
14
+ [P in keyof T]?: DeepPartial<T[P]>;
15
+ };
16
+
17
+ /**
18
+ * A class to handle requests to /api/sessions
19
+ */
20
+ export class LiteSessionClient implements ISessionAPIClient {
21
+ /**
22
+ * Construct a new LiteSessionClient.
23
+ *
24
+ * @param options The instantiation options for a LiteSessionClient.
25
+ */
26
+ constructor(options: LiteSessionClient.IOptions) {
27
+ this._kernelClient = options.kernelClient;
28
+ this._serverSettings = options.serverSettings ?? ServerConnection.makeSettings();
29
+ // Listen for kernel removals
30
+ this._kernelClient.changed.connect((_, args) => {
31
+ switch (args.type) {
32
+ case 'remove': {
33
+ const kernelId = args.oldValue?.id;
34
+ if (!kernelId) {
35
+ return;
36
+ }
37
+ // find the session associated with the kernel
38
+ const session = this._sessions.find((s) => s.kernel?.id === kernelId);
39
+ if (!session) {
40
+ return;
41
+ }
42
+ // Track the kernel ID for restart detection
43
+ this._pendingRestarts.add(kernelId);
44
+ setTimeout(async () => {
45
+ // If after a short delay the kernel hasn't been re-added, it was terminated
46
+ if (this._pendingRestarts.has(kernelId)) {
47
+ this._pendingRestarts.delete(kernelId);
48
+ await this.shutdown(session.id);
49
+ }
50
+ }, 100);
51
+ break;
52
+ }
53
+ case 'add': {
54
+ // If this was a restart, remove it from pending
55
+ const kernelId = args.newValue?.id;
56
+ if (!kernelId) {
57
+ return;
58
+ }
59
+ this._pendingRestarts.delete(kernelId);
60
+ break;
61
+ }
62
+ }
63
+ });
64
+ }
65
+
66
+ /**
67
+ * The server settings for the session client.
68
+ */
69
+ get serverSettings(): ServerConnection.ISettings {
70
+ return this._serverSettings;
71
+ }
72
+
73
+ /**
74
+ * Get a session by id.
75
+ *
76
+ * @param id The id of the session.
77
+ */
78
+ async getModel(id: string): Promise<Session.IModel> {
79
+ const session = this._sessions.find((s) => s.id === id);
80
+ if (!session) {
81
+ throw Error(`Session ${id} not found`);
82
+ }
83
+ return session;
84
+ }
85
+
86
+ /**
87
+ * List the running sessions
88
+ */
89
+ async listRunning(): Promise<Session.IModel[]> {
90
+ return this._sessions;
91
+ }
92
+
93
+ /**
94
+ * Patch an existing session.
95
+ * This can be used to rename a session.
96
+ *
97
+ * - path updates session to track renamed paths
98
+ * - kernel.name starts a new kernel with a given kernelspec
99
+ *
100
+ * @param options The options to patch the session.
101
+ */
102
+ async update(options: DeepPartial<Session.IModel>): Promise<Session.IModel> {
103
+ const { id, path, name, kernel } = options;
104
+ const index = this._sessions.findIndex((s) => s.id === id);
105
+ const session = this._sessions[index];
106
+ if (!session) {
107
+ throw Error(`Session ${id} not found`);
108
+ }
109
+ const patched = {
110
+ ...session,
111
+ path: path ?? session.path,
112
+ name: name ?? session.name,
113
+ };
114
+
115
+ if (kernel) {
116
+ // Kernel id takes precedence over name.
117
+ if (kernel.id) {
118
+ const session = this._sessions.find(
119
+ (session) => session.kernel?.id === kernel?.id,
120
+ );
121
+ if (session) {
122
+ patched.kernel = session.kernel;
123
+ }
124
+ } else if (kernel.name) {
125
+ const newKernel = await this._kernelClient.startNew({
126
+ id: UUID.uuid4(),
127
+ name: kernel.name,
128
+ location: PathExt.dirname(patched.path),
129
+ });
130
+
131
+ if (newKernel) {
132
+ patched.kernel = newKernel;
133
+ }
134
+
135
+ // clean up the session on kernel shutdown
136
+ void this._handleKernelShutdown({
137
+ kernelId: newKernel.id,
138
+ sessionId: session.id,
139
+ });
140
+ }
141
+ }
142
+
143
+ this._sessions[index] = patched;
144
+ return patched;
145
+ }
146
+
147
+ /**
148
+ * Start a new session
149
+ * TODO: read path and name
150
+ *
151
+ * @param options The options to start a new session.
152
+ */
153
+ async startNew(options: Session.ISessionOptions): Promise<Session.IModel> {
154
+ const { path, name } = options;
155
+ const running = this._sessions.find((s) => s.name === name);
156
+ if (running) {
157
+ return running;
158
+ }
159
+ const kernelName = options.kernel?.name ?? '';
160
+ const id = UUID.uuid4();
161
+ const nameOrPath = options.name ?? options.path;
162
+ const dirname = PathExt.dirname(options.name) || PathExt.dirname(options.path);
163
+ const hasDrive = nameOrPath.includes(':');
164
+ const driveName = hasDrive ? nameOrPath.split(':')[0] : '';
165
+ // add drive name if missing (top level directory)
166
+ const location = dirname.includes(driveName) ? dirname : `${driveName}:${dirname}`;
167
+ const kernel = await this._kernelClient.startNew({
168
+ id,
169
+ name: kernelName,
170
+ location,
171
+ });
172
+ const session: Session.IModel = {
173
+ id,
174
+ path,
175
+ name: name ?? path,
176
+ type: 'notebook',
177
+ kernel: {
178
+ id: kernel.id,
179
+ name: kernel.name,
180
+ },
181
+ };
182
+ this._sessions.push(session);
183
+
184
+ // clean up the session on kernel shutdown
185
+ void this._handleKernelShutdown({ kernelId: id, sessionId: session.id });
186
+
187
+ return session;
188
+ }
189
+
190
+ /**
191
+ * Shut down a session.
192
+ *
193
+ * @param id The id of the session to shut down.
194
+ */
195
+ async shutdown(id: string): Promise<void> {
196
+ const session = this._sessions.find((s) => s.id === id);
197
+ if (!session) {
198
+ throw Error(`Session ${id} not found`);
199
+ }
200
+ const kernelId = session.kernel?.id;
201
+ if (kernelId) {
202
+ await this._kernelClient.shutdown(kernelId);
203
+ }
204
+ ArrayExt.removeFirstOf(this._sessions, session);
205
+ }
206
+
207
+ /**
208
+ * Shut down all sessions.
209
+ */
210
+ async shutdownAll(): Promise<void> {
211
+ await Promise.all(this._sessions.map((s) => this.shutdown(s.id)));
212
+ }
213
+
214
+ /**
215
+ * Handle kernel shutdown
216
+ */
217
+ private async _handleKernelShutdown({
218
+ kernelId,
219
+ sessionId,
220
+ }: {
221
+ kernelId: string;
222
+ sessionId: string;
223
+ }): Promise<void> {
224
+ // No need to handle kernel shutdown here anymore since we're using the changed signal
225
+ }
226
+
227
+ private _kernelClient: LiteKernelClient;
228
+ private _serverSettings: ServerConnection.ISettings;
229
+ private _sessions: Session.IModel[] = [];
230
+ private _pendingRestarts = new Set<string>();
231
+ }
232
+
233
+ /**
234
+ * A namespace for LiteSessionClient statics.
235
+ */
236
+ export namespace LiteSessionClient {
237
+ /**
238
+ * The instantiation options for the session client.
239
+ */
240
+ export interface IOptions {
241
+ /**
242
+ * A reference to the kernels service.
243
+ */
244
+ kernelClient: LiteKernelClient;
245
+
246
+ /**
247
+ * Server settings for the session client.
248
+ */
249
+ serverSettings?: ServerConnection.ISettings;
250
+ }
251
+ }
@@ -0,0 +1,4 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ export * from './client';
@@ -0,0 +1,4 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ export * from './settings';
@@ -0,0 +1,236 @@
1
+ import { PageConfig, URLExt } from '@jupyterlab/coreutils';
2
+
3
+ import { ServerConnection, Setting, SettingManager } from '@jupyterlab/services';
4
+
5
+ import { ISettingRegistry } from '@jupyterlab/settingregistry';
6
+
7
+ import { PromiseDelegate } from '@lumino/coreutils';
8
+
9
+ import * as json5 from 'json5';
10
+
11
+ import type localforage from 'localforage';
12
+
13
+ /**
14
+ * The settings file to request
15
+ */
16
+ export type SettingsFile = 'all.json' | 'all_federated.json';
17
+
18
+ /**
19
+ * The name of the local storage.
20
+ */
21
+ const DEFAULT_STORAGE_NAME = 'JupyterLite Storage';
22
+
23
+ /**
24
+ * A class to manage settings in the browser.
25
+ */
26
+ export class Settings extends SettingManager implements Setting.IManager {
27
+ /**
28
+ * Create a new settings service.
29
+ */
30
+ constructor(options: Settings.IOptions) {
31
+ super({
32
+ serverSettings: options.serverSettings,
33
+ });
34
+ this._localforage = options.localforage;
35
+ this._storageName = options.storageName || DEFAULT_STORAGE_NAME;
36
+ this._storageDrivers = options.storageDrivers || null;
37
+
38
+ this._ready = new PromiseDelegate();
39
+ void this.initialize().catch(console.warn);
40
+ }
41
+
42
+ /**
43
+ * A promise that resolves when the settings storage is fully initialized
44
+ */
45
+ get ready(): Promise<void> {
46
+ return this._ready.promise;
47
+ }
48
+
49
+ /**
50
+ * A lazy reference to initialized storage
51
+ */
52
+ protected get storage(): Promise<LocalForage> {
53
+ return this.ready.then(() => this._storage as LocalForage);
54
+ }
55
+
56
+ /**
57
+ * Finish any initialization after server has started and all extensions are applied.
58
+ */
59
+ async initialize() {
60
+ await this.initStorage();
61
+ this._ready.resolve(void 0);
62
+ }
63
+
64
+ /**
65
+ * Prepare the storage
66
+ */
67
+ protected async initStorage() {
68
+ this._storage = this.defaultSettingsStorage();
69
+ }
70
+
71
+ /**
72
+ * Get default options for localForage instances
73
+ */
74
+ protected get defaultStorageOptions(): LocalForageOptions {
75
+ const driver = this._storageDrivers?.length ? this._storageDrivers : null;
76
+ return {
77
+ version: 1,
78
+ name: this._storageName,
79
+ ...(driver ? { driver } : {}),
80
+ };
81
+ }
82
+
83
+ /**
84
+ * Create a settings store.
85
+ */
86
+ protected defaultSettingsStorage(): LocalForage {
87
+ return this._localforage.createInstance({
88
+ description: 'Offline Storage for Settings',
89
+ storeName: 'settings',
90
+ ...this.defaultStorageOptions,
91
+ });
92
+ }
93
+
94
+ /**
95
+ * Get settings by plugin id
96
+ *
97
+ * @param pluginId the id of the plugin
98
+ *
99
+ */
100
+ async fetch(pluginId: string): Promise<ISettingRegistry.IPlugin> {
101
+ const all = await this.list();
102
+ const settings = all.values as ISettingRegistry.IPlugin[];
103
+ const setting = settings.find((setting: ISettingRegistry.IPlugin) => {
104
+ return setting.id === pluginId;
105
+ });
106
+ if (!setting) {
107
+ throw new Error(`Setting ${pluginId} not found`);
108
+ }
109
+ return setting;
110
+ }
111
+
112
+ /**
113
+ * Get all the settings
114
+ */
115
+ async list(
116
+ query?: 'ids',
117
+ ): Promise<{ ids: string[]; values: ISettingRegistry.IPlugin[] }> {
118
+ const allCore = await this._getAll('all.json');
119
+ let allFederated: ISettingRegistry.IPlugin[] = [];
120
+ try {
121
+ allFederated = await this._getAll('all_federated.json');
122
+ } catch {
123
+ // handle the case where there is no federated extension
124
+ }
125
+
126
+ // JupyterLab 4 expects all settings to be returned in one go
127
+ // so append the settings from federated plugins to the core ones
128
+ const all = allCore.concat(allFederated);
129
+
130
+ // return existing user settings if they exist
131
+ const storage = await this.storage;
132
+ const settings = await Promise.all(
133
+ all.map(async (plugin) => {
134
+ const { id } = plugin;
135
+ const raw = ((await storage.getItem(id)) as string) ?? plugin.raw;
136
+ return {
137
+ ...Private.override(plugin),
138
+ raw,
139
+ settings: json5.parse(raw),
140
+ };
141
+ }),
142
+ );
143
+
144
+ // format the settings
145
+ const ids = settings.map((plugin: ISettingRegistry.IPlugin) => plugin.id) ?? [];
146
+
147
+ let values: ISettingRegistry.IPlugin[] = [];
148
+ if (!query) {
149
+ values =
150
+ settings.map((plugin: ISettingRegistry.IPlugin) => {
151
+ plugin.data = { composite: {}, user: {} };
152
+ return plugin;
153
+ }) ?? [];
154
+ }
155
+
156
+ return { ids, values };
157
+ }
158
+
159
+ /**
160
+ * Save settings for a given plugin id
161
+ *
162
+ * @param pluginId The id of the plugin
163
+ * @param raw The raw settings
164
+ *
165
+ */
166
+ async save(id: string, raw: string): Promise<void> {
167
+ await (await this.storage).setItem(id, raw);
168
+ }
169
+
170
+ /**
171
+ * Clear all stored settings
172
+ *
173
+ * @returns A promise which resolves when the settings are cleared
174
+ */
175
+ async clear(): Promise<void> {
176
+ await (await this.storage).clear();
177
+ }
178
+
179
+ /**
180
+ * Get all the settings for core or federated plugins
181
+ */
182
+ private async _getAll(file: SettingsFile): Promise<ISettingRegistry.IPlugin[]> {
183
+ const settingsUrl = PageConfig.getOption('settingsUrl') ?? '/';
184
+ const all = (await (
185
+ await fetch(URLExt.join(settingsUrl, file))
186
+ ).json()) as ISettingRegistry.IPlugin[];
187
+ return all;
188
+ }
189
+
190
+ private _storageName: string = DEFAULT_STORAGE_NAME;
191
+ private _storageDrivers: string[] | null = null;
192
+ private _storage: LocalForage | undefined;
193
+ private _localforage: typeof localforage;
194
+ private _ready: PromiseDelegate<void>;
195
+ }
196
+
197
+ /**
198
+ * A namespace for settings metadata.
199
+ */
200
+ export namespace Settings {
201
+ /**
202
+ * Initialization options for settings.
203
+ */
204
+ export interface IOptions {
205
+ localforage: typeof localforage;
206
+ storageName?: string | null;
207
+ storageDrivers?: string[] | null;
208
+ serverSettings?: ServerConnection.ISettings;
209
+ }
210
+ }
211
+
212
+ /**
213
+ * A namespace for private data
214
+ */
215
+ namespace Private {
216
+ const _overrides: Record<string, ISettingRegistry.IPlugin['schema']['default']> =
217
+ JSON.parse(PageConfig.getOption('settingsOverrides') || '{}');
218
+
219
+ /**
220
+ * Override the defaults of the schema with ones from PageConfig
221
+ *
222
+ * @see https://github.com/jupyterlab/jupyterlab_server/blob/v2.5.2/jupyterlab_server/settings_handler.py#L216-L227
223
+ */
224
+ export function override(plugin: ISettingRegistry.IPlugin): ISettingRegistry.IPlugin {
225
+ if (_overrides[plugin.id]) {
226
+ if (!plugin.schema.properties) {
227
+ // probably malformed, or only provides keyboard shortcuts, etc.
228
+ plugin.schema.properties = {};
229
+ }
230
+ for (const [prop, propDefault] of Object.entries(_overrides[plugin.id] || {})) {
231
+ plugin.schema.properties[prop].default = propDefault;
232
+ }
233
+ }
234
+ return plugin;
235
+ }
236
+ }
@@ -0,0 +1,6 @@
1
+ /*-----------------------------------------------------------------------------
2
+ | Copyright (c) Jupyter Development Team.
3
+ | Distributed under the terms of the Modified BSD License.
4
+ |----------------------------------------------------------------------------*/
5
+
6
+ /* This file is intentionally empty */
package/style/index.js ADDED
@@ -0,0 +1,6 @@
1
+ /*-----------------------------------------------------------------------------
2
+ | Copyright (c) Jupyter Development Team.
3
+ | Distributed under the terms of the Modified BSD License.
4
+ |----------------------------------------------------------------------------*/
5
+
6
+ import './index.css';