@jupyterlite/terminal 0.2.0-a0 → 0.2.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.
package/src/manager.ts DELETED
@@ -1,207 +0,0 @@
1
- import { BaseManager, Terminal, TerminalManager } from '@jupyterlab/services';
2
- import { ISignal, Signal } from '@lumino/signaling';
3
- import { LiteTerminalConnection } from './terminal';
4
-
5
- /**
6
- * Interface for Lite terminal manager, supports setting browserContextId.
7
- */
8
- interface ILiteTerminalManager extends Terminal.IManager {
9
- browsingContextId: string;
10
- }
11
-
12
- /**
13
- * Type guard for ILiteTerminalManager.
14
- */
15
- export function isILiteTerminalManager(
16
- obj: Terminal.IManager
17
- ): obj is ILiteTerminalManager {
18
- return 'browsingContextId' in obj;
19
- }
20
-
21
- /**
22
- * A terminal session manager.
23
- */
24
- export class LiteTerminalManager
25
- extends BaseManager
26
- implements ILiteTerminalManager
27
- {
28
- /**
29
- * Construct a new terminal manager.
30
- */
31
- constructor(options: TerminalManager.IOptions = {}) {
32
- super(options);
33
-
34
- // Initialize internal data.
35
- this._ready = (async () => {
36
- this._isReady = true;
37
- })();
38
- }
39
-
40
- /**
41
- * Set identifier for communicating with service worker.
42
- */
43
- set browsingContextId(browsingContextId: string) {
44
- console.log('==> LiteTerminalManager browsingContextId', browsingContextId);
45
- this._browsingContextId = browsingContextId;
46
- }
47
-
48
- /**
49
- * A signal emitted when there is a connection failure.
50
- */
51
- get connectionFailure(): ISignal<this, Error> {
52
- return this._connectionFailure;
53
- }
54
-
55
- /*
56
- * Connect to a running terminal.
57
- *
58
- * @param options - The options used to connect to the terminal.
59
- *
60
- * @returns The new terminal connection instance.
61
- *
62
- * #### Notes
63
- * The manager `serverSettings` will be used.
64
- */
65
- connectTo(
66
- options: Omit<Terminal.ITerminalConnection.IOptions, 'serverSettings'>
67
- ): Terminal.ITerminalConnection {
68
- const { model } = options;
69
- const { name } = model;
70
- console.log('==> LiteTerminalManager.connectTo', name);
71
- const { serverSettings } = this;
72
-
73
- const terminal = new LiteTerminalConnection({
74
- browsingContextId: this._browsingContextId,
75
- model,
76
- serverSettings
77
- });
78
- terminal.disposed.connect(() => this.shutdown(name));
79
- return terminal;
80
- }
81
-
82
- /**
83
- * Whether the terminal service is available.
84
- */
85
- isAvailable(): boolean {
86
- return true;
87
- }
88
-
89
- /**
90
- * Test whether the manager is ready.
91
- */
92
- get isReady(): boolean {
93
- return this._isReady;
94
- }
95
-
96
- /**
97
- * A promise that fulfills when the manager is ready.
98
- */
99
- get ready(): Promise<void> {
100
- return this._ready;
101
- }
102
-
103
- /**
104
- * Force a refresh of the running terminals.
105
- *
106
- * @returns A promise that with the list of running terminals.
107
- *
108
- * #### Notes
109
- * This is intended to be called only in response to a user action,
110
- * since the manager maintains its internal state.
111
- */
112
- async refreshRunning(): Promise<void> {
113
- this._runningChanged.emit(this._models);
114
- }
115
-
116
- /**
117
- * Create an iterator over the most recent running terminals.
118
- *
119
- * @returns A new iterator over the running terminals.
120
- */
121
- running(): IterableIterator<Terminal.IModel> {
122
- return this._models[Symbol.iterator]();
123
- }
124
-
125
- /**
126
- * A signal emitted when the running terminals change.
127
- */
128
- get runningChanged(): ISignal<this, Terminal.IModel[]> {
129
- return this._runningChanged;
130
- }
131
-
132
- /**
133
- * Shut down a terminal session by name.
134
- */
135
- async shutdown(name: string): Promise<void> {
136
- const terminal = this._terminalConnections.get(name);
137
- if (terminal !== undefined) {
138
- this._terminalConnections.delete(name);
139
- terminal.dispose();
140
- this.refreshRunning();
141
- }
142
- }
143
-
144
- /**
145
- * Shut down all terminal sessions.
146
- *
147
- * @returns A promise that resolves when all of the sessions are shut down.
148
- */
149
- async shutdownAll(): Promise<void> {
150
- await Promise.all(this._models.map(model => this.shutdown(model.name)));
151
- this.refreshRunning();
152
- }
153
-
154
- /**
155
- * Create a new terminal session.
156
- *
157
- * @param options - The options used to create the terminal.
158
- *
159
- * @returns A promise that resolves with the terminal connection instance.
160
- *
161
- * #### Notes
162
- * The manager `serverSettings` will be used unless overridden in the
163
- * options.
164
- */
165
- async startNew(
166
- options: Terminal.ITerminal.IOptions
167
- ): Promise<Terminal.ITerminalConnection> {
168
- const name = options.name ?? this._nextAvailableName();
169
- const model: Terminal.IModel = { name };
170
- const { serverSettings } = this;
171
-
172
- const terminal = new LiteTerminalConnection({
173
- browsingContextId: this._browsingContextId,
174
- model,
175
- serverSettings
176
- });
177
- terminal.disposed.connect(() => this.shutdown(name));
178
- this._terminalConnections.set(name, terminal);
179
- await this.refreshRunning();
180
- return terminal;
181
- }
182
-
183
- private get _models(): Terminal.IModel[] {
184
- return Array.from(this._terminalConnections, ([name, value]) => {
185
- return { name };
186
- });
187
- }
188
-
189
- private _nextAvailableName(): string {
190
- for (let i = 1; ; ++i) {
191
- const name = `${i}`;
192
- if (!this._terminalConnections.has(name)) {
193
- return name;
194
- }
195
- }
196
- }
197
-
198
- private _browsingContextId?: string;
199
- private _connectionFailure = new Signal<this, Error>(this);
200
- private _isReady = false;
201
- private _ready: Promise<void>;
202
- private _runningChanged = new Signal<this, Terminal.IModel[]>(this);
203
- private _terminalConnections = new Map<
204
- string,
205
- Terminal.ITerminalConnection
206
- >();
207
- }
package/src/terminal.ts DELETED
@@ -1,195 +0,0 @@
1
- import { ServerConnection, Terminal } from '@jupyterlab/services';
2
- import { ISignal, Signal } from '@lumino/signaling';
3
- import { Shell } from './shell';
4
- import { IShell } from '@jupyterlite/cockle';
5
-
6
- /**
7
- * An implementation of a terminal interface.
8
- */
9
- export class LiteTerminalConnection implements Terminal.ITerminalConnection {
10
- /**
11
- * Construct a new terminal session.
12
- */
13
- constructor(options: LiteTerminalConnection.IOptions) {
14
- this._name = options.model.name;
15
- this._serverSettings = options.serverSettings!;
16
- const { baseUrl } = this._serverSettings;
17
- const { browsingContextId } = options;
18
-
19
- this._shell = new Shell({
20
- mountpoint: '/drive',
21
- driveFsBaseUrl: baseUrl,
22
- wasmBaseUrl: baseUrl + 'extensions/@jupyterlite/terminal/static/wasm/',
23
- outputCallback: this._outputCallback.bind(this),
24
- browsingContextId
25
- });
26
- this._shell.disposed.connect(() => this.dispose());
27
-
28
- this._shell.start().then(() => this._updateConnectionStatus('connected'));
29
- }
30
-
31
- /**
32
- * The current connection status of the terminal connection.
33
- */
34
- get connectionStatus(): Terminal.ConnectionStatus {
35
- return this._connectionStatus;
36
- }
37
-
38
- /**
39
- * A signal emitted when the terminal connection status changes.
40
- */
41
- get connectionStatusChanged(): ISignal<this, Terminal.ConnectionStatus> {
42
- return this._connectionStatusChanged;
43
- }
44
-
45
- /**
46
- * Dispose of the resources held by the session.
47
- */
48
- dispose(): void {
49
- if (this._isDisposed) {
50
- return;
51
- }
52
-
53
- this._isDisposed = true;
54
- this._shell.dispose();
55
- this._disposed.emit();
56
-
57
- this._updateConnectionStatus('disconnected');
58
-
59
- Signal.clearData(this);
60
- }
61
-
62
- /**
63
- * A signal emitted when the session is disposed.
64
- */
65
- get disposed(): ISignal<this, void> {
66
- return this._disposed;
67
- }
68
-
69
- /**
70
- * Test whether the session is disposed.
71
- */
72
- get isDisposed(): boolean {
73
- return this._isDisposed;
74
- }
75
-
76
- /**
77
- * A signal emitted when a message is received from the server.
78
- */
79
- get messageReceived(): ISignal<
80
- Terminal.ITerminalConnection,
81
- Terminal.IMessage
82
- > {
83
- return this._messageReceived;
84
- }
85
-
86
- /**
87
- * Get the model for the terminal session.
88
- */
89
- get model(): Terminal.IModel {
90
- return { name: this._name };
91
- }
92
-
93
- /**
94
- * Get the name of the terminal session.
95
- */
96
- get name(): string {
97
- return this._name;
98
- }
99
-
100
- /**
101
- * Reconnect to a terminal.
102
- *
103
- * #### Notes
104
- * This may try multiple times to reconnect to a terminal, and will sever
105
- * any existing connection.
106
- */
107
- async reconnect(): Promise<void> {
108
- console.log('==> LiteTerminalConnection.reconnect not implemented');
109
- }
110
-
111
- /**
112
- * Send a message to the terminal session.
113
- *
114
- * #### Notes
115
- * If the connection is down, the message will be queued for sending when
116
- * the connection comes back up.
117
- */
118
- send(message: Terminal.IMessage): void {
119
- const { content } = message;
120
- if (content === undefined) {
121
- return;
122
- }
123
-
124
- switch (message.type) {
125
- case 'stdin':
126
- this._shell.input(content[0] as string); // async
127
- break;
128
- case 'set_size': {
129
- const rows = content[0] as number;
130
- const columns = content[1] as number;
131
- this._shell.setSize(rows, columns); // async
132
- break;
133
- }
134
- }
135
- }
136
-
137
- /**
138
- * The server settings for the session.
139
- */
140
- get serverSettings(): ServerConnection.ISettings {
141
- return this._serverSettings;
142
- }
143
-
144
- /**
145
- * Shut down the terminal session.
146
- */
147
- async shutdown(): Promise<void> {
148
- this.dispose();
149
- }
150
-
151
- private _outputCallback(text: string): void {
152
- // 'stdout' or 'disconnect' as MessageType.
153
- // Cockle is not yet using the 'disconnect'.
154
- this._messageReceived.emit({ type: 'stdout', content: [text] });
155
- }
156
-
157
- /**
158
- * Handle connection status changes.
159
- */
160
- private _updateConnectionStatus(
161
- connectionStatus: Terminal.ConnectionStatus
162
- ): void {
163
- if (this._connectionStatus === connectionStatus) {
164
- return;
165
- }
166
-
167
- this._connectionStatus = connectionStatus;
168
-
169
- // Notify others that the connection status changed.
170
- this._connectionStatusChanged.emit(connectionStatus);
171
- }
172
-
173
- private _isDisposed = false;
174
- private _disposed = new Signal<this, void>(this);
175
-
176
- private _name: string;
177
- private _serverSettings: ServerConnection.ISettings;
178
- private _connectionStatus: Terminal.ConnectionStatus = 'connecting';
179
- private _connectionStatusChanged = new Signal<
180
- this,
181
- Terminal.ConnectionStatus
182
- >(this);
183
- private _messageReceived = new Signal<this, Terminal.IMessage>(this);
184
-
185
- private _shell: IShell;
186
- }
187
-
188
- export namespace LiteTerminalConnection {
189
- export interface IOptions extends Terminal.ITerminalConnection.IOptions {
190
- /**
191
- * The ID of the browsing context where the request originated.
192
- */
193
- browsingContextId?: string;
194
- }
195
- }