-
Notifications
You must be signed in to change notification settings - Fork 276
/
index.ts
610 lines (554 loc) · 18.7 KB
/
index.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { PageConfig } from '@jupyterlab/coreutils';
import { Contents as ServerContents, KernelSpec } from '@jupyterlab/services';
import {
BroadcastChannelWrapper,
Contents,
IContents,
IBroadcastChannelWrapper,
} from '@jupyterlite/contents';
import { IKernels, Kernels, IKernelSpecs, KernelSpecs } from '@jupyterlite/kernel';
import { ILicenses, Licenses } from '@jupyterlite/licenses';
import {
JupyterLiteServer,
JupyterLiteServerPlugin,
Router,
IServiceWorkerManager,
ServiceWorkerManager,
} from '@jupyterlite/server';
import { ISessions, Sessions } from '@jupyterlite/session';
import { ISettings, Settings } from '@jupyterlite/settings';
import { ITranslation, Translation } from '@jupyterlite/translation';
import { ILocalForage, ensureMemoryStorage } from '@jupyterlite/localforage';
import localforage from 'localforage';
/**
* The localforage plugin
*/
const localforagePlugin: JupyterLiteServerPlugin<ILocalForage> = {
id: '@jupyterlite/server-extension:localforage',
autoStart: true,
provides: ILocalForage,
activate: (app: JupyterLiteServer) => {
return { localforage };
},
};
/**
* The volatile localforage memory plugin
*/
const localforageMemoryPlugin: JupyterLiteServerPlugin<void> = {
id: '@jupyterlite/server-extension:localforage-memory-storage',
autoStart: true,
requires: [ILocalForage],
activate: async (app: JupyterLiteServer, forage: ILocalForage) => {
if (JSON.parse(PageConfig.getOption('enableMemoryStorage') || 'false')) {
console.warn(
'Memory storage fallback enabled: contents and settings may not be saved',
);
await ensureMemoryStorage(forage.localforage);
}
},
};
/**
* A plugin providing the routes for the config section.
* TODO: implement logic to persist the config sections?
*/
const configSectionRoutesPlugin: JupyterLiteServerPlugin<void> = {
id: '@jupyterlite/server-extension:config-section-routes',
autoStart: true,
activate: (app: JupyterLiteServer) => {
const sections: {
[id: string]: string;
} = {};
app.router.get('/api/config/(.*)', async (req: Router.IRequest, id: string) => {
const section = sections[id] ?? JSON.stringify({});
return new Response(section);
});
app.router.patch('/api/config/(.*)', async (req: Router.IRequest, id: string) => {
const payload = req.body as any;
sections[id] = payload;
return new Response(payload);
});
},
};
/**
* The contents service plugin.
*/
const contentsPlugin: JupyterLiteServerPlugin<IContents> = {
id: '@jupyterlite/server-extension:contents',
requires: [ILocalForage],
autoStart: true,
provides: IContents,
activate: (app: JupyterLiteServer, forage: ILocalForage) => {
const storageName = PageConfig.getOption('contentsStorageName');
const storageDrivers = JSON.parse(
PageConfig.getOption('contentsStorageDrivers') || 'null',
);
const { localforage } = forage;
const contents = new Contents({
storageName,
storageDrivers,
localforage,
});
app.started.then(() => contents.initialize().catch(console.warn));
return contents;
},
};
/**
* A plugin providing the routes for the contents service.
*/
const contentsRoutesPlugin: JupyterLiteServerPlugin<void> = {
id: '@jupyterlite/server-extension:contents-routes',
autoStart: true,
requires: [IContents],
activate: (app: JupyterLiteServer, contents: IContents) => {
// GET /api/contents/{path}/checkpoints - Get a list of checkpoints for a file
app.router.get(
'/api/contents/(.+)/checkpoints',
async (req: Router.IRequest, filename: string) => {
const res = await contents.listCheckpoints(filename);
return new Response(JSON.stringify(res));
},
);
// POST /api/contents/{path}/checkpoints/{checkpoint_id} - Restore a file to a particular checkpointed state
app.router.post(
'/api/contents/(.+)/checkpoints/(.*)',
async (req: Router.IRequest, filename: string, checkpoint: string) => {
const res = await contents.restoreCheckpoint(filename, checkpoint);
return new Response(JSON.stringify(res), { status: 204 });
},
);
// POST /api/contents/{path}/checkpoints - Create a new checkpoint for a file
app.router.post(
'/api/contents/(.+)/checkpoints',
async (req: Router.IRequest, filename: string) => {
const res = await contents.createCheckpoint(filename);
return new Response(JSON.stringify(res), { status: 201 });
},
);
// DELETE /api/contents/{path}/checkpoints/{checkpoint_id} - Delete a checkpoint
app.router.delete(
'/api/contents/(.+)/checkpoints/(.*)',
async (req: Router.IRequest, filename: string, checkpoint: string) => {
const res = await contents.deleteCheckpoint(filename, checkpoint);
return new Response(JSON.stringify(res), { status: 204 });
},
);
// GET /api/contents/{path} - Get contents of file or directory
app.router.get(
'/api/contents(.*)',
async (req: Router.IRequest, filename: string) => {
const options: ServerContents.IFetchOptions = {
content: req.query?.content === '1',
};
const nb = await contents.get(filename, options);
if (!nb) {
return new Response(null, { status: 404 });
}
return new Response(JSON.stringify(nb));
},
);
// POST /api/contents/{path} - Create a new file in the specified path
app.router.post('/api/contents(.*)', async (req: Router.IRequest, path: string) => {
const options = req.body;
const copyFrom = options?.copy_from as string;
let file: ServerContents.IModel | null;
if (copyFrom) {
file = await contents.copy(copyFrom, path);
} else {
file = await contents.newUntitled(options);
}
if (!file) {
return new Response(null, { status: 400 });
}
return new Response(JSON.stringify(file), { status: 201 });
});
// PATCH /api/contents/{path} - Rename a file or directory without re-uploading content
app.router.patch(
'/api/contents(.*)',
async (req: Router.IRequest, filename: string) => {
const newPath = (req.body?.path as string) ?? '';
filename = filename[0] === '/' ? filename.slice(1) : filename;
const nb = await contents.rename(filename, newPath);
return new Response(JSON.stringify(nb));
},
);
// PUT /api/contents/{path} - Save or upload a file
app.router.put(
'/api/contents/(.+)',
async (req: Router.IRequest, filename: string) => {
const body = req.body;
const nb = await contents.save(filename, body);
return new Response(JSON.stringify(nb));
},
);
// DELETE /api/contents/{path} - Delete a file in the given path
app.router.delete(
'/api/contents/(.+)',
async (req: Router.IRequest, filename: string) => {
await contents.delete(filename);
return new Response(null, { status: 204 });
},
);
},
};
/**
* A plugin installing the service worker.
*/
const serviceWorkerPlugin: JupyterLiteServerPlugin<IServiceWorkerManager> = {
id: '@jupyterlite/server-extension:service-worker',
autoStart: true,
provides: IServiceWorkerManager,
activate: (app: JupyterLiteServer) => {
return new ServiceWorkerManager();
},
};
/**
* A plugin for handling communication with the Emscpriten file system.
*/
const emscriptenFileSystemPlugin: JupyterLiteServerPlugin<IBroadcastChannelWrapper> = {
id: '@jupyterlite/server-extension:emscripten-filesystem',
autoStart: true,
optional: [IServiceWorkerManager],
provides: IBroadcastChannelWrapper,
activate: (
app: JupyterLiteServer,
serviceWorkerRegistrationWrapper?: IServiceWorkerManager,
): IBroadcastChannelWrapper => {
const { contents } = app.serviceManager;
const broadcaster = new BroadcastChannelWrapper({ contents });
const what = 'Kernel filesystem and JupyterLite contents';
function logStatus(msg?: string, err?: any) {
if (err) {
console.warn(err);
}
if (msg) {
console.warn(msg);
}
if (err || msg) {
console.warn(`${what} will NOT be synced`);
} else {
// eslint-disable-next-line no-console
console.info(`${what} will be synced`);
}
}
if (!serviceWorkerRegistrationWrapper) {
logStatus('JupyterLite ServiceWorker not available');
} else {
serviceWorkerRegistrationWrapper.ready
.then(() => {
broadcaster.enable();
logStatus();
})
.catch((err: any) => {
logStatus('JupyterLite ServiceWorker failed to become available', err);
});
}
return broadcaster;
},
};
/**
* The kernels service plugin.
*/
const kernelsPlugin: JupyterLiteServerPlugin<IKernels> = {
id: '@jupyterlite/server-extension:kernels',
autoStart: true,
provides: IKernels,
requires: [IKernelSpecs],
activate: (app: JupyterLiteServer, kernelspecs: IKernelSpecs) => {
return new Kernels({ kernelspecs });
},
};
/**
* A plugin providing the routes for the kernels service
*/
const kernelsRoutesPlugin: JupyterLiteServerPlugin<void> = {
id: '@jupyterlite/server-extension:kernels-routes',
autoStart: true,
requires: [IKernels],
activate: (app: JupyterLiteServer, kernels: IKernels) => {
// GET /api/kernels - List the running kernels
app.router.get('/api/kernels', async (req: Router.IRequest) => {
const res = await kernels.list();
return new Response(JSON.stringify(res));
});
// POST /api/kernels/{kernel_id} - Restart a kernel
app.router.post(
'/api/kernels/(.*)/restart',
async (req: Router.IRequest, kernelId: string) => {
const res = await kernels.restart(kernelId);
return new Response(JSON.stringify(res));
},
);
// DELETE /api/kernels/{kernel_id} - Kill a kernel and delete the kernel id
app.router.delete(
'/api/kernels/(.*)',
async (req: Router.IRequest, kernelId: string) => {
const res = await kernels.shutdown(kernelId);
return new Response(JSON.stringify(res), { status: 204 });
},
);
},
};
/**
* The kernel spec service plugin.
*/
const kernelSpecPlugin: JupyterLiteServerPlugin<IKernelSpecs> = {
id: '@jupyterlite/server-extension:kernelspec',
autoStart: true,
provides: IKernelSpecs,
activate: (app: JupyterLiteServer) => {
return new KernelSpecs();
},
};
/**
* A plugin providing the routes for the kernelspec service.
*/
const kernelSpecRoutesPlugin: JupyterLiteServerPlugin<void> = {
id: '@jupyterlite/server-extension:kernelspec-routes',
autoStart: true,
requires: [IKernelSpecs],
activate: (app: JupyterLiteServer, kernelspecs: IKernelSpecs) => {
app.router.get('/api/kernelspecs', async (req: Router.IRequest) => {
const { specs } = kernelspecs;
if (!specs) {
return new Response(null);
}
// follow the same format as in Jupyter Server
const allKernelSpecs: {
[name: string]: {
name: string;
spec: KernelSpec.ISpecModel | undefined;
resources: { [name: string]: string } | undefined;
};
} = {};
const allSpecs = specs.kernelspecs;
Object.keys(allSpecs).forEach((name) => {
const spec = allSpecs[name];
const { resources } = spec ?? {};
allKernelSpecs[name] = {
name,
spec,
resources,
};
});
const res = {
default: specs.default,
kernelspecs: allKernelSpecs,
};
return new Response(JSON.stringify(res));
});
},
};
/**
* The licenses service plugin
*/
const licensesPlugin: JupyterLiteServerPlugin<ILicenses> = {
id: '@jupyterlite/server-extension:licenses',
autoStart: true,
provides: ILicenses,
activate: (app: JupyterLiteServer) => {
return new Licenses();
},
};
/**
* A plugin providing the routes for the licenses service.
*/
const licensesRoutesPlugin: JupyterLiteServerPlugin<void> = {
id: '@jupyterlite/server-extension:licenses-routes',
autoStart: true,
requires: [ILicenses],
activate(app: JupyterLiteServer, licenses: ILicenses) {
app.router.get('/api/licenses', async (req: Router.IRequest) => {
const res = await licenses.get();
return new Response(JSON.stringify(res));
});
},
};
/**
* A plugin providing the routes for the lsp service.
* TODO: provide the service in a separate plugin?
*/
const lspRoutesPlugin: JupyterLiteServerPlugin<void> = {
id: '@jupyterlite/server-extension:lsp-routes',
autoStart: true,
activate: (app: JupyterLiteServer) => {
app.router.get('/lsp/status', async (req: Router.IRequest) => {
return new Response(JSON.stringify({ version: 2, sessions: {}, specs: {} }));
});
},
};
/**
* A plugin providing the routes for the nbconvert service.
* TODO: provide the service in a separate plugin?
*/
const nbconvertRoutesPlugin: JupyterLiteServerPlugin<void> = {
id: '@jupyterlite/server-extension:nbconvert-routes',
autoStart: true,
activate: (app: JupyterLiteServer) => {
app.router.get('/api/nbconvert', async (req: Router.IRequest) => {
return new Response(JSON.stringify({}));
});
},
};
/**
* The sessions service plugin.
*/
const sessionsPlugin: JupyterLiteServerPlugin<ISessions> = {
id: '@jupyterlite/server-extension:sessions',
autoStart: true,
provides: ISessions,
requires: [IKernels],
activate: (app: JupyterLiteServer, kernels: IKernels) => {
return new Sessions({ kernels });
},
};
/**
* A plugin providing the routes for the session service.
*/
const sessionsRoutesPlugin: JupyterLiteServerPlugin<void> = {
id: '@jupyterlite/server-extension:sessions-routes',
autoStart: true,
requires: [ISessions],
activate: (app: JupyterLiteServer, sessions: ISessions) => {
// GET /api/sessions/{session} - Get session
app.router.get('/api/sessions/(.+)', async (req: Router.IRequest, id: string) => {
const session = await sessions.get(id);
return new Response(JSON.stringify(session), { status: 200 });
});
// GET /api/sessions - List available sessions
app.router.get('/api/sessions', async (req: Router.IRequest) => {
const list = await sessions.list();
return new Response(JSON.stringify(list), { status: 200 });
});
// PATCH /api/sessions/{session} - This can be used to rename a session
app.router.patch('/api/sessions(.*)', async (req: Router.IRequest, id: string) => {
const options = req.body as any;
const session = await sessions.patch(options);
return new Response(JSON.stringify(session), { status: 200 });
});
// DELETE /api/sessions/{session} - Delete a session
app.router.delete(
'/api/sessions/(.+)',
async (req: Router.IRequest, id: string) => {
await sessions.shutdown(id);
return new Response(null, { status: 204 });
},
);
// POST /api/sessions - Create a new session or return an existing session if a session of the same name already exists
app.router.post('/api/sessions', async (req: Router.IRequest) => {
const options = req.body as any;
const session = await sessions.startNew(options);
return new Response(JSON.stringify(session), { status: 201 });
});
},
};
/**
* The settings service plugin.
*/
const settingsPlugin: JupyterLiteServerPlugin<ISettings> = {
id: '@jupyterlite/server-extension:settings',
autoStart: true,
requires: [ILocalForage],
provides: ISettings,
activate: (app: JupyterLiteServer, forage: ILocalForage) => {
const storageName = PageConfig.getOption('settingsStorageName');
const storageDrivers = JSON.parse(
PageConfig.getOption('settingsStorageDrivers') || 'null',
);
const { localforage } = forage;
const settings = new Settings({ storageName, storageDrivers, localforage });
app.started.then(() => settings.initialize().catch(console.warn));
return settings;
},
};
/**
* A plugin providing the routes for the settings service.
*/
const settingsRoutesPlugin: JupyterLiteServerPlugin<void> = {
id: '@jupyterlite/server-extension:settings-routes',
autoStart: true,
requires: [ISettings],
activate: (app: JupyterLiteServer, settings: ISettings) => {
// TODO: improve the regex
// const pluginPattern = new RegExp(/(?:@([^/]+?)[/])?([^/]+?):(\w+)/);
const pluginPattern = '/api/settings/((?:@([^/]+?)[/])?([^/]+?):([^:]+))$';
app.router.get(pluginPattern, async (req: Router.IRequest, pluginId: string) => {
const setting = await settings.get(pluginId);
return new Response(JSON.stringify(setting));
});
app.router.put(pluginPattern, async (req: Router.IRequest, pluginId: string) => {
const body = req.body as any;
const { raw } = body;
await settings.save(pluginId, raw);
return new Response(null, { status: 204 });
});
app.router.get('/api/settings', async (req: Router.IRequest) => {
const plugins = await settings.getAll();
return new Response(JSON.stringify(plugins));
});
},
};
/**
* The translation service plugin.
*/
const translationPlugin: JupyterLiteServerPlugin<ITranslation> = {
id: '@jupyterlite/server-extension:translation',
autoStart: true,
provides: ITranslation,
activate: (app: JupyterLiteServer) => {
const translation = new Translation();
app.router.get(
'/api/translations/?(.*)',
async (req: Router.IRequest, locale: string) => {
if (locale === 'default') {
locale = 'en';
}
const data = await translation.get(locale || 'all');
return new Response(JSON.stringify(data));
},
);
return translation;
},
};
/**
* A plugin providing the routes for the translation service.
*/
const translationRoutesPlugin: JupyterLiteServerPlugin<void> = {
id: '@jupyterlite/server-extension:translation-routes',
autoStart: true,
requires: [ITranslation],
activate: (app: JupyterLiteServer, translation: ITranslation) => {
app.router.get(
'/api/translations/?(.*)',
async (req: Router.IRequest, locale: string) => {
const data = await translation.get(locale || 'all');
return new Response(JSON.stringify(data));
},
);
},
};
const plugins: JupyterLiteServerPlugin<any>[] = [
configSectionRoutesPlugin,
contentsPlugin,
contentsRoutesPlugin,
emscriptenFileSystemPlugin,
kernelsPlugin,
kernelsRoutesPlugin,
kernelSpecPlugin,
kernelSpecRoutesPlugin,
licensesPlugin,
licensesRoutesPlugin,
localforageMemoryPlugin,
localforagePlugin,
lspRoutesPlugin,
nbconvertRoutesPlugin,
serviceWorkerPlugin,
sessionsPlugin,
sessionsRoutesPlugin,
settingsPlugin,
settingsRoutesPlugin,
translationPlugin,
translationRoutesPlugin,
];
export default plugins;