|
2 | 2 | // Distributed under the terms of the Modified BSD License.
|
3 | 3 |
|
4 | 4 | import {
|
5 |
| - JupyterLiteServer, |
6 |
| - JupyterLiteServerPlugin, |
7 |
| - Router |
8 |
| -} from '@jupyterlite/server'; |
| 5 | + JupyterFrontEnd, |
| 6 | + JupyterFrontEndPlugin |
| 7 | +} from '@jupyterlab/application'; |
| 8 | +import { |
| 9 | + ITerminalManager, |
| 10 | + ServiceManagerPlugin, |
| 11 | + Terminal |
| 12 | +} from '@jupyterlab/services'; |
| 13 | +import { IServiceWorkerManager } from '@jupyterlite/server'; |
9 | 14 |
|
10 |
| -import { TerminalManager } from './manager'; |
11 |
| -import { ITerminalManager } from './tokens'; |
| 15 | +import { isILiteTerminalManager, LiteTerminalManager } from './manager'; |
12 | 16 |
|
13 | 17 | /**
|
14 |
| - * The terminals service plugin. |
| 18 | + * The terminal manager plugin, replacing the JupyterLab terminal manager. |
15 | 19 | */
|
16 |
| -const terminalsPlugin: JupyterLiteServerPlugin<ITerminalManager> = { |
| 20 | +const terminalManagerPlugin: ServiceManagerPlugin<Terminal.IManager> = { |
17 | 21 | id: '@jupyterlite/terminal:plugin',
|
18 |
| - description: 'A terminal for JupyterLite', |
| 22 | + description: 'A JupyterLite extension providing a custom terminal manager', |
19 | 23 | autoStart: true,
|
20 | 24 | provides: ITerminalManager,
|
21 |
| - activate: async (app: JupyterLiteServer) => { |
| 25 | + activate: (_: null): Terminal.IManager => { |
22 | 26 | console.log(
|
23 | 27 | 'JupyterLite extension @jupyterlite/terminal:plugin is activated!'
|
24 | 28 | );
|
25 |
| - |
26 |
| - const { serviceManager } = app; |
27 |
| - const { serverSettings, terminals } = serviceManager; |
28 |
| - console.log('terminals available:', terminals.isAvailable()); |
29 |
| - console.log('terminals ready:', terminals.isReady); // Not ready |
30 |
| - console.log('terminals active:', terminals.isActive); |
31 |
| - |
32 |
| - // Not sure this is necessary? |
33 |
| - await terminals.ready; |
34 |
| - console.log('terminals ready after await:', terminals.isReady); // Ready |
35 |
| - |
36 |
| - return new TerminalManager(serverSettings.wsUrl); |
| 29 | + return new LiteTerminalManager(); |
37 | 30 | }
|
38 | 31 | };
|
39 | 32 |
|
40 | 33 | /**
|
41 |
| - * A plugin providing the routes for the terminals service |
| 34 | + * A plugin that sets the browsingContextId of the terminal manager. |
42 | 35 | */
|
43 |
| -const terminalsRoutesPlugin: JupyterLiteServerPlugin<void> = { |
44 |
| - id: '@jupyterlite/terminal:routes-plugin', |
| 36 | +const browsingContextIdSetter: JupyterFrontEndPlugin<void> = { |
| 37 | + id: '@jupyterlite/terminal:browsing-context-id', |
45 | 38 | autoStart: true,
|
| 39 | + optional: [IServiceWorkerManager], |
46 | 40 | requires: [ITerminalManager],
|
47 |
| - activate: (app: JupyterLiteServer, terminalManager: ITerminalManager) => { |
48 |
| - console.log( |
49 |
| - 'JupyterLite extension @jupyterlite/terminal:routes-plugin is activated!', |
50 |
| - terminalManager |
51 |
| - ); |
52 |
| - |
53 |
| - // GET /api/terminals - List the running terminals |
54 |
| - app.router.get('/api/terminals', async (req: Router.IRequest) => { |
55 |
| - const res = await terminalManager.listRunning(); |
56 |
| - // Should return last_activity for each too, |
57 |
| - return new Response(JSON.stringify(res)); |
58 |
| - }); |
59 |
| - |
60 |
| - // POST /api/terminals - Start a terminal |
61 |
| - app.router.post('/api/terminals', async (req: Router.IRequest) => { |
62 |
| - const res = await terminalManager.startNew(); |
63 |
| - // Should return last_activity too. |
64 |
| - return new Response(JSON.stringify(res)); |
65 |
| - }); |
66 |
| - |
67 |
| - // DELETE /api/terminals/{terminal name} - Delete a terminal |
68 |
| - app.router.delete( |
69 |
| - '/api/terminals/(.+)', |
70 |
| - async (req: Router.IRequest, name: string) => { |
71 |
| - const exists = terminalManager.has(name); |
72 |
| - if (exists) { |
73 |
| - await terminalManager.shutdownTerminal(name); |
74 |
| - } else { |
75 |
| - const msg = `The terminal session "${name}"" does not exist`; |
76 |
| - console.warn(msg); |
77 |
| - } |
78 |
| - |
79 |
| - return new Response(null, { status: exists ? 204 : 404 }); |
| 41 | + activate: ( |
| 42 | + _: JupyterFrontEnd, |
| 43 | + terminalManager: Terminal.IManager, |
| 44 | + serviceWorkerManager?: IServiceWorkerManager |
| 45 | + ): void => { |
| 46 | + if (serviceWorkerManager !== undefined) { |
| 47 | + if (isILiteTerminalManager(terminalManager)) { |
| 48 | + const { browsingContextId } = serviceWorkerManager; |
| 49 | + terminalManager.browsingContextId = browsingContextId; |
| 50 | + } else { |
| 51 | + console.warn( |
| 52 | + 'Terminal manager does not support setting browsingContextId' |
| 53 | + ); |
80 | 54 | }
|
81 |
| - ); |
| 55 | + } |
82 | 56 | }
|
83 | 57 | };
|
84 | 58 |
|
85 |
| -export default [terminalsPlugin, terminalsRoutesPlugin]; |
| 59 | +export default [terminalManagerPlugin, browsingContextIdSetter]; |
0 commit comments