@jupyterlite/services 0.1.0 → 0.7.0-rc.1

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